Browse Source

Add tests for multiple counters.

ndm2 12 years ago
parent
commit
3c7e56bf82

+ 54 - 0
Test/Case/Model/Behavior/SoftDeleteBehaviorTest.php

@@ -148,6 +148,33 @@ class SoftDeleteBehaviorTest extends CakeTestCase {
 	}
 
 	/**
+	 * testSoftDeleteWithMultipleCounterCache
+	 *
+	 * @return void
+	 */
+	public function testSoftDeleteWithMultipleCounterCache() {
+		$this->Post->belongsTo['Category']['counterCache'] = array(
+			'post_count' => array('Post.deleted' => false),
+			'deleted_post_count' => array('Post.deleted' => true)
+		);
+
+		$this->Post->Category->id = 1;
+		$count = $this->Post->Category->field('post_count');
+		$this->assertEquals(2, $count);
+		$count = $this->Post->Category->field('deleted_post_count');
+		$this->assertEquals(0, $count);
+
+		$this->assertFalse($this->Post->softDeleted);
+		$this->Post->delete(1);
+		$this->assertTrue($this->Post->softDeleted);
+
+		$count = $this->Post->Category->field('post_count');
+		$this->assertEquals(1, $count);
+		$count = $this->Post->Category->field('deleted_post_count');
+		$this->assertEquals(1, $count);
+	}
+
+	/**
 	 * testSoftDeleteWithoutCounterCache
 	 *
 	 * @return void
@@ -179,6 +206,33 @@ class SoftDeleteBehaviorTest extends CakeTestCase {
 	}
 
 	/**
+	 * testUnDeleteWithMultipleCounterCache
+	 *
+	 * @return void
+	 */
+	public function testUnDeleteWithMultipleCounterCache() {
+		$this->Post->belongsTo['Category']['counterCache'] = array(
+			'post_count' => array('Post.deleted' => false),
+			'deleted_post_count' => array('Post.deleted' => true)
+		);
+
+		$this->Post->Category->id = 2;
+		$count = $this->Post->Category->field('post_count');
+		$this->assertEquals($count, 0);
+		$count = $this->Post->Category->field('deleted_post_count');
+		$this->assertEquals($count, 1);
+
+		$this->assertEmpty($this->Post->read(null, 3));
+
+		$this->Post->undelete(3);
+
+		$count = $this->Post->Category->field('post_count');
+		$this->assertEquals(1, $count);
+		$count = $this->Post->Category->field('deleted_post_count');
+		$this->assertEquals(0, $count);
+	}
+
+	/**
 	 * testUnDeleteWithoutCounterCache
 	 *
 	 * @return void

+ 3 - 0
Test/Fixture/SoftDeleteCategoryFixture.php

@@ -13,6 +13,7 @@ class SoftDeleteCategoryFixture extends CakeTestFixture {
 	public $fields = array(
 		'id' => array('type' => 'integer', 'key' => 'primary'),
 		'post_count' => array('type' => 'integer'),
+		'deleted_post_count' => array('type' => 'integer'),
 		'title' => array('type' => 'string', 'null' => false));
 
 	/**
@@ -24,10 +25,12 @@ class SoftDeleteCategoryFixture extends CakeTestFixture {
 		array(
 			'id' => 1,
 			'post_count' => 2,
+			'deleted_post_count' => 0,
 			'title' => 'Category A'),
 		array(
 			'id' => 2,
 			'post_count' => 0,
+			'deleted_post_count' => 1,
 			'title' => 'Category B'));
 
 }