SoftDeleteBehaviorTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * Copyright 2009-2010, Cake Development Corporation (http://cakedc.com)
  4. *
  5. * Licensed under The MIT License
  6. * Redistributions of files must retain the above copyright notice.
  7. *
  8. * @copyright Copyright 2009-2010, Cake Development Corporation (http://cakedc.com)
  9. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  10. */
  11. App::uses('Model', 'Model');
  12. App::uses('Behavior', 'Model');
  13. App::uses('SoftDeleteBehavior', 'Tools.Model/Behavior');
  14. /**
  15. * SoftDeleteBehavior Test case
  16. */
  17. class SoftDeleteBehaviorTest extends CakeTestCase {
  18. /**
  19. * Fixtures property
  20. *
  21. * @var array
  22. */
  23. public $fixtures = array('plugin.tools.soft_delete_post');
  24. /**
  25. * Creates the model instance
  26. *
  27. * @return void
  28. */
  29. public function setUp() {
  30. parent::setUp();
  31. $this->Post = new SoftDeletedPost();
  32. $this->Behavior = new SoftDeleteTestBehavior();
  33. }
  34. /**
  35. * Destroy the model instance
  36. *
  37. * @return void
  38. */
  39. public function tearDown() {
  40. parent::tearDown();
  41. unset($this->Post);
  42. unset($this->Behavior);
  43. ClassRegistry::flush();
  44. }
  45. /**
  46. * Test saving a item
  47. *
  48. * @return void
  49. */
  50. public function testSoftDelete() {
  51. $data = $this->Post->read(null, 1);
  52. $this->assertEquals($data[$this->Post->alias][$this->Post->primaryKey], 1);
  53. $this->assertFalse($this->Post->softDeleted);
  54. $result = $this->Post->delete(1);
  55. $this->assertFalse($result);
  56. $this->assertTrue($this->Post->softDeleted);
  57. $data = $this->Post->read(null, 1);
  58. $this->assertEmpty($data);
  59. $this->Post->Behaviors->unload('SoftDeleteTest');
  60. $data = $this->Post->read(null, 1);
  61. $this->assertEquals($data['Post']['deleted'], 1);
  62. //$result = abs(strtotime($data['Post']['updated']) - strtotime($data['Post']['deleted_date']));
  63. //$this->assertWithinMargin($result, 0, 1, $data['Post']['updated'].'/'.$data['Post']['deleted_date']);
  64. }
  65. /**
  66. * Test that overwriting delete() on AppModel level makes SoftDelete return true for delete()
  67. *
  68. * @return void
  69. */
  70. public function testSoftDeleteReturningTrue() {
  71. $this->Post = new ModifiedSoftDeletedPost();
  72. $this->Post->Behaviors->load('Tools.SoftDelete');
  73. $data = $this->Post->read(null, 1);
  74. $this->assertEquals($data[$this->Post->alias][$this->Post->primaryKey], 1);
  75. //$this->assertFalse($this->Post->softDeleted);
  76. $result = $this->Post->delete(1);
  77. $this->assertTrue($result);
  78. //$this->assertTrue($this->Post->softDeleted);
  79. }
  80. /**
  81. * testUnDelete
  82. *
  83. * @return void
  84. */
  85. public function testUnDelete() {
  86. $data = $this->Post->read(null, 1);
  87. $result = $this->Post->delete(1);
  88. $result = $this->Post->undelete(1);
  89. $data = $this->Post->read(null, 1);
  90. $this->assertEquals($data['Post']['deleted'], 0);
  91. }
  92. /**
  93. * testSoftDeletePurge
  94. *
  95. * @return void
  96. */
  97. public function testSoftDeletePurge() {
  98. $this->Post->Behaviors->disable('SoftDeleteTest');
  99. $data = $this->Post->read(null, 3);
  100. $this->assertTrue(!empty($data));
  101. $this->Post->Behaviors->enable('SoftDeleteTest');
  102. $data = $this->Post->read(null, 3);
  103. $this->assertEmpty($data);
  104. $count = $this->Post->purgeDeletedCount();
  105. $this->assertEquals($count, 1);
  106. $this->Post->purgeDeleted();
  107. $data = $this->Post->read(null, 3);
  108. $this->assertEmpty($data);
  109. $this->Post->Behaviors->disable('SoftDeleteTest');
  110. $data = $this->Post->read(null, 3);
  111. $this->assertEmpty($data);
  112. }
  113. // $result = $this->Model->read();
  114. // $this->assertEquals($result['SoftDeletedPost']['slug'], 'fourth_Post');
  115. ///Should not update
  116. // $this->Model->saveField('title', 'Fourth Post (Part 1)');
  117. // $result = $this->Model->read();
  118. // $this->assertEquals($result['SoftDeletedPost']['slug'], 'fourth_Post');
  119. ////Should update
  120. // $this->Model->Behaviors->SluggableTest->settings['SoftDeletedPost']['update'] = true;
  121. // $this->Model->saveField('title', 'Fourth Post (Part 2)');
  122. // $result = $this->Model->read();
  123. // $this->assertEquals($result['SoftDeletedPost']['slug'], 'fourth_Post_part_2');
  124. ////Updating the item should not update the slug
  125. // $this->Model->saveField('body', 'Here goes the content.');
  126. // $result = $this->Model->read();
  127. // $this->assertEquals($result['SoftDeletedPost']['slug'], 'fourth_Post_part_2');
  128. }
  129. /**
  130. * SoftDeleteTestBehavior
  131. *
  132. */
  133. class SoftDeleteTestBehavior extends SoftDeleteBehavior {
  134. }
  135. /**
  136. * SoftDeletedPost
  137. *
  138. */
  139. class SoftDeletedPost extends CakeTestModel {
  140. /**
  141. * Use Table
  142. *
  143. * @var string
  144. */
  145. public $useTable = 'soft_delete_posts';
  146. /**
  147. * Behaviors
  148. *
  149. * @var array
  150. */
  151. public $actsAs = array('Tools.SoftDeleteTest');
  152. /**
  153. * Alias
  154. *
  155. * @var string
  156. */
  157. public $alias = 'Post';
  158. }
  159. /**
  160. * SoftDeletedPost returning true on delete()
  161. *
  162. */
  163. class ModifiedSoftDeletedPost extends SoftDeletedPost {
  164. public function delete($id = null, $cascade = true) {
  165. $result = parent::delete($id, $cascade);
  166. if (!$result && $this->Behaviors->loaded('SoftDelete')) {
  167. return $this->softDeleted;
  168. }
  169. return $result;
  170. }
  171. }