| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- <?php
- /**
- * Copyright 2009-2010, Cake Development Corporation (http://cakedc.com)
- *
- * Licensed under The MIT License
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright 2009-2010, Cake Development Corporation (http://cakedc.com)
- * @license http://opensource.org/licenses/mit-license.php MIT
- */
- App::uses('Model', 'Model');
- App::uses('Behavior', 'Model');
- App::uses('SoftDeleteBehavior', 'Tools.Model/Behavior');
- /**
- * SoftDeleteBehavior Test case
- */
- class SoftDeleteBehaviorTest extends CakeTestCase {
- /**
- * Fixtures property
- *
- * @var array
- */
- public $fixtures = array(
- 'plugin.tools.soft_delete_category',
- 'plugin.tools.soft_delete_post',
- 'plugin.tools.soft_delete_user'
- );
- /**
- * Creates the model instance
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- $this->Post = new SoftDeletedPost();
- $this->Behavior = new SoftDeleteTestBehavior();
- }
- /**
- * Destroy the model instance
- *
- * @return void
- */
- public function tearDown() {
- parent::tearDown();
- unset($this->Post);
- unset($this->Behavior);
- ClassRegistry::flush();
- }
- /**
- * Test saving a item
- *
- * @return void
- */
- public function testSoftDelete() {
- $data = $this->Post->read(null, 1);
- $this->assertEquals($data[$this->Post->alias][$this->Post->primaryKey], 1);
- $this->assertFalse($this->Post->softDeleted);
- $result = $this->Post->delete(1);
- $this->assertFalse($result);
- $this->assertTrue($this->Post->softDeleted);
- $data = $this->Post->read(null, 1);
- $this->assertEmpty($data);
- $this->Post->Behaviors->unload('SoftDeleteTest');
- $data = $this->Post->read(null, 1);
- $this->assertEquals($data['Post']['deleted'], 1);
- //$result = abs(strtotime($data['Post']['updated']) - strtotime($data['Post']['deleted_date']));
- //$this->assertWithinMargin($result, 0, 1, $data['Post']['updated'].'/'.$data['Post']['deleted_date']);
- }
- /**
- * Test that overwriting delete() on AppModel level makes SoftDelete return true for delete()
- *
- * @return void
- */
- public function testSoftDeleteReturningTrue() {
- $this->Post = new ModifiedSoftDeletedPost();
- $this->Post->Behaviors->load('Tools.SoftDelete');
- $data = $this->Post->read(null, 1);
- $this->assertEquals($data[$this->Post->alias][$this->Post->primaryKey], 1);
- //$this->assertFalse($this->Post->softDeleted);
- $result = $this->Post->delete(1);
- $this->assertTrue($result);
- //$this->assertTrue($this->Post->softDeleted);
- }
- /**
- * TestUnDelete
- *
- * @return void
- */
- public function testUnDelete() {
- $data = $this->Post->read(null, 1);
- $result = $this->Post->delete(1);
- $result = $this->Post->undelete(1);
- $data = $this->Post->read(null, 1);
- $this->assertEquals($data['Post']['deleted'], 0);
- }
- /**
- * TestSoftDeletePurge
- *
- * @return void
- */
- public function testSoftDeletePurge() {
- $this->Post->Behaviors->disable('SoftDeleteTest');
- $data = $this->Post->read(null, 3);
- $this->assertTrue(!empty($data));
- $this->Post->Behaviors->enable('SoftDeleteTest');
- $data = $this->Post->read(null, 3);
- $this->assertEmpty($data);
- $count = $this->Post->purgeDeletedCount();
- $this->assertEquals($count, 1);
- $this->Post->purgeDeleted();
- $data = $this->Post->read(null, 3);
- $this->assertEmpty($data);
- $this->Post->Behaviors->disable('SoftDeleteTest');
- $data = $this->Post->read(null, 3);
- $this->assertEmpty($data);
- }
- /**
- * TestSoftDeleteWithCounterCache
- *
- * @return void
- */
- public function testSoftDeleteWithCounterCache() {
- $this->Post->Category->id = 1;
- $count = $this->Post->Category->field('post_count');
- $this->assertEquals(2, $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);
- }
- /**
- * 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);
- }
- /**
- * TestSoftDeleteWithCounterCacheOnMultipleAssociations
- *
- * @return void
- */
- public function testSoftDeleteWithCounterCacheOnMultipleAssociations() {
- $this->Post->bindModel(array(
- 'belongsTo' => array(
- 'User' => array(
- 'className' => 'SoftDeleteUser',
- 'counterCache' => true
- )
- )
- ),
- false);
- $this->Post->Category->id = 1;
- $this->Post->User->id = 1;
- $count = $this->Post->Category->field('post_count');
- $this->assertEquals(2, $count);
- $count = $this->Post->User->field('post_count');
- $this->assertEquals(2, $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->User->field('post_count');
- $this->assertEquals(1, $count);
- }
- /**
- * TestSoftDeleteWithoutCounterCache
- *
- * @return void
- */
- public function testSoftDeleteWithoutCounterCache() {
- $Post = $this->getMock('SoftDeletedPost', array('updateCounterCache'));
- $Post->expects($this->never())->method('updateCounterCache');
- $Post->belongsTo = array();
- $Post->delete(1);
- }
- /**
- * TestUnDeleteWithCounterCache
- *
- * @return void
- */
- public function testUnDeleteWithCounterCache() {
- $this->Post->Category->id = 2;
- $count = $this->Post->Category->field('post_count');
- $this->assertEquals(0, $count);
- $this->assertEmpty($this->Post->read(null, 3));
- $this->Post->undelete(3);
- $count = $this->Post->Category->field('post_count');
- $this->assertEquals(1, $count);
- }
- /**
- * 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(0, $count);
- $count = $this->Post->Category->field('deleted_post_count');
- $this->assertEquals(1, $count);
- $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);
- }
- /**
- * TestUnDeleteWithCounterCacheOnMultipleAssociations
- *
- * @return void
- */
- public function testUnDeleteWithCounterCacheOnMultipleAssociations() {
- $this->Post->bindModel(array(
- 'belongsTo' => array(
- 'User' => array(
- 'className' => 'SoftDeleteUser',
- 'counterCache' => true
- )
- )
- ),
- false);
- $this->Post->Category->id = 2;
- $this->Post->User->id = 1;
- $count = $this->Post->Category->field('post_count');
- $this->assertEquals(0, $count);
- $count = $this->Post->User->field('post_count');
- $this->assertEquals(2, $count);
- $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->User->field('post_count');
- $this->assertEquals(3, $count);
- }
- /**
- * TestUnDeleteWithoutCounterCache
- *
- * @return void
- */
- public function testUnDeleteWithoutCounterCache() {
- $Post = $this->getMock('SoftDeletedPost', array('updateCounterCache'));
- $Post->expects($this->never())->method('updateCounterCache');
- $Post->belongsTo = array();
- $Post->undelete(3);
- }
- // $result = $this->Model->read();
- // $this->assertEquals($result['SoftDeletedPost']['slug'], 'fourth_Post');
- ///Should not update
- // $this->Model->saveField('title', 'Fourth Post (Part 1)');
- // $result = $this->Model->read();
- // $this->assertEquals($result['SoftDeletedPost']['slug'], 'fourth_Post');
- ////Should update
- // $this->Model->Behaviors->SluggableTest->settings['SoftDeletedPost']['update'] = true;
- // $this->Model->saveField('title', 'Fourth Post (Part 2)');
- // $result = $this->Model->read();
- // $this->assertEquals($result['SoftDeletedPost']['slug'], 'fourth_Post_part_2');
- ////Updating the item should not update the slug
- // $this->Model->saveField('body', 'Here goes the content.');
- // $result = $this->Model->read();
- // $this->assertEquals($result['SoftDeletedPost']['slug'], 'fourth_Post_part_2');
- }
- /**
- * SoftDeleteTestBehavior
- *
- */
- class SoftDeleteTestBehavior extends SoftDeleteBehavior {
- }
- /**
- * SoftDeleteCategory
- *
- */
- class SoftDeleteCategory extends CakeTestModel {
- /**
- * Use Table
- *
- * @var string
- */
- public $useTable = 'soft_delete_categories';
- /**
- * Alias
- *
- * @var string
- */
- public $alias = 'Category';
- }
- /**
- * SoftDeleteUser
- *
- */
- class SoftDeleteUser extends CakeTestModel {
- /**
- * Use Table
- *
- * @var string
- */
- public $useTable = 'soft_delete_users';
- /**
- * Alias
- *
- * @var string
- */
- public $alias = 'User';
- }
- /**
- * SoftDeletedPost
- *
- */
- class SoftDeletedPost extends CakeTestModel {
- /**
- * Use Table
- *
- * @var string
- */
- public $useTable = 'soft_delete_posts';
- /**
- * Behaviors
- *
- * @var array
- */
- public $actsAs = array('Tools.SoftDeleteTest');
- /**
- * Alias
- *
- * @var string
- */
- public $alias = 'Post';
- /**
- * belongsTo associations
- *
- * @var array
- */
- public $belongsTo = array(
- 'Category' => array(
- 'className' => 'SoftDeleteCategory',
- 'counterCache' => true
- )
- );
- }
- /**
- * SoftDeletedPost returning true on delete()
- *
- */
- class ModifiedSoftDeletedPost extends SoftDeletedPost {
- public function delete($id = null, $cascade = true) {
- $result = parent::delete($id, $cascade);
- if (!$result && $this->Behaviors->loaded('SoftDelete')) {
- return $this->softDeleted;
- }
- return $result;
- }
- }
|