SoftDeleteBehaviorTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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(
  24. 'plugin.tools.soft_delete_category',
  25. 'plugin.tools.soft_delete_post'
  26. );
  27. /**
  28. * Creates the model instance
  29. *
  30. * @return void
  31. */
  32. public function setUp() {
  33. parent::setUp();
  34. $this->Post = new SoftDeletedPost();
  35. $this->Behavior = new SoftDeleteTestBehavior();
  36. }
  37. /**
  38. * Destroy the model instance
  39. *
  40. * @return void
  41. */
  42. public function tearDown() {
  43. parent::tearDown();
  44. unset($this->Post);
  45. unset($this->Behavior);
  46. ClassRegistry::flush();
  47. }
  48. /**
  49. * Test saving a item
  50. *
  51. * @return void
  52. */
  53. public function testSoftDelete() {
  54. $data = $this->Post->read(null, 1);
  55. $this->assertEquals($data[$this->Post->alias][$this->Post->primaryKey], 1);
  56. $this->assertFalse($this->Post->softDeleted);
  57. $result = $this->Post->delete(1);
  58. $this->assertFalse($result);
  59. $this->assertTrue($this->Post->softDeleted);
  60. $data = $this->Post->read(null, 1);
  61. $this->assertEmpty($data);
  62. $this->Post->Behaviors->unload('SoftDeleteTest');
  63. $data = $this->Post->read(null, 1);
  64. $this->assertEquals($data['Post']['deleted'], 1);
  65. //$result = abs(strtotime($data['Post']['updated']) - strtotime($data['Post']['deleted_date']));
  66. //$this->assertWithinMargin($result, 0, 1, $data['Post']['updated'].'/'.$data['Post']['deleted_date']);
  67. }
  68. /**
  69. * Test that overwriting delete() on AppModel level makes SoftDelete return true for delete()
  70. *
  71. * @return void
  72. */
  73. public function testSoftDeleteReturningTrue() {
  74. $this->Post = new ModifiedSoftDeletedPost();
  75. $this->Post->Behaviors->load('Tools.SoftDelete');
  76. $data = $this->Post->read(null, 1);
  77. $this->assertEquals($data[$this->Post->alias][$this->Post->primaryKey], 1);
  78. //$this->assertFalse($this->Post->softDeleted);
  79. $result = $this->Post->delete(1);
  80. $this->assertTrue($result);
  81. //$this->assertTrue($this->Post->softDeleted);
  82. }
  83. /**
  84. * TestUnDelete
  85. *
  86. * @return void
  87. */
  88. public function testUnDelete() {
  89. $data = $this->Post->read(null, 1);
  90. $result = $this->Post->delete(1);
  91. $result = $this->Post->undelete(1);
  92. $data = $this->Post->read(null, 1);
  93. $this->assertEquals($data['Post']['deleted'], 0);
  94. }
  95. /**
  96. * TestSoftDeletePurge
  97. *
  98. * @return void
  99. */
  100. public function testSoftDeletePurge() {
  101. $this->Post->Behaviors->disable('SoftDeleteTest');
  102. $data = $this->Post->read(null, 3);
  103. $this->assertTrue(!empty($data));
  104. $this->Post->Behaviors->enable('SoftDeleteTest');
  105. $data = $this->Post->read(null, 3);
  106. $this->assertEmpty($data);
  107. $count = $this->Post->purgeDeletedCount();
  108. $this->assertEquals($count, 1);
  109. $this->Post->purgeDeleted();
  110. $data = $this->Post->read(null, 3);
  111. $this->assertEmpty($data);
  112. $this->Post->Behaviors->disable('SoftDeleteTest');
  113. $data = $this->Post->read(null, 3);
  114. $this->assertEmpty($data);
  115. }
  116. /**
  117. * testSoftDeleteWithCounterCache
  118. *
  119. * @return void
  120. */
  121. public function testSoftDeleteWithCounterCache() {
  122. $this->Post->Category->id = 1;
  123. $count = $this->Post->Category->field('post_count');
  124. $this->assertEquals(2, $count);
  125. $this->assertFalse($this->Post->softDeleted);
  126. $this->Post->delete(1);
  127. $this->assertTrue($this->Post->softDeleted);
  128. $count = $this->Post->Category->field('post_count');
  129. $this->assertEquals(1, $count);
  130. }
  131. /**
  132. * testSoftDeleteWithoutCounterCache
  133. *
  134. * @return void
  135. */
  136. public function testSoftDeleteWithoutCounterCache() {
  137. $Post = $this->getMock('SoftDeletedPost', array('updateCounterCache'));
  138. $Post->expects($this->never())->method('updateCounterCache');
  139. $Post->belongsTo = array();
  140. $Post->delete(1);
  141. }
  142. /**
  143. * testUnDeleteWithCounterCache
  144. *
  145. * @return void
  146. */
  147. public function testUnDeleteWithCounterCache() {
  148. $this->Post->Category->id = 2;
  149. $count = $this->Post->Category->field('post_count');
  150. $this->assertEquals($count, 0);
  151. $this->assertEmpty($this->Post->read(null, 3));
  152. $this->Post->undelete(3);
  153. $count = $this->Post->Category->field('post_count');
  154. $this->assertEquals(1, $count);
  155. }
  156. /**
  157. * testUnDeleteWithoutCounterCache
  158. *
  159. * @return void
  160. */
  161. public function testUnDeleteWithoutCounterCache() {
  162. $Post = $this->getMock('SoftDeletedPost', array('updateCounterCache'));
  163. $Post->expects($this->never())->method('updateCounterCache');
  164. $Post->belongsTo = array();
  165. $Post->undelete(3);
  166. }
  167. // $result = $this->Model->read();
  168. // $this->assertEquals($result['SoftDeletedPost']['slug'], 'fourth_Post');
  169. ///Should not update
  170. // $this->Model->saveField('title', 'Fourth Post (Part 1)');
  171. // $result = $this->Model->read();
  172. // $this->assertEquals($result['SoftDeletedPost']['slug'], 'fourth_Post');
  173. ////Should update
  174. // $this->Model->Behaviors->SluggableTest->settings['SoftDeletedPost']['update'] = true;
  175. // $this->Model->saveField('title', 'Fourth Post (Part 2)');
  176. // $result = $this->Model->read();
  177. // $this->assertEquals($result['SoftDeletedPost']['slug'], 'fourth_Post_part_2');
  178. ////Updating the item should not update the slug
  179. // $this->Model->saveField('body', 'Here goes the content.');
  180. // $result = $this->Model->read();
  181. // $this->assertEquals($result['SoftDeletedPost']['slug'], 'fourth_Post_part_2');
  182. }
  183. /**
  184. * SoftDeleteTestBehavior
  185. *
  186. */
  187. class SoftDeleteTestBehavior extends SoftDeleteBehavior {
  188. }
  189. /**
  190. * SoftDeleteCategory
  191. *
  192. */
  193. class SoftDeleteCategory extends CakeTestModel {
  194. /**
  195. * Use Table
  196. *
  197. * @var string
  198. */
  199. public $useTable = 'soft_delete_categories';
  200. /**
  201. * Alias
  202. *
  203. * @var string
  204. */
  205. public $alias = 'Category';
  206. }
  207. /**
  208. * SoftDeletedPost
  209. *
  210. */
  211. class SoftDeletedPost extends CakeTestModel {
  212. /**
  213. * Use Table
  214. *
  215. * @var string
  216. */
  217. public $useTable = 'soft_delete_posts';
  218. /**
  219. * Behaviors
  220. *
  221. * @var array
  222. */
  223. public $actsAs = array('Tools.SoftDeleteTest');
  224. /**
  225. * Alias
  226. *
  227. * @var string
  228. */
  229. public $alias = 'Post';
  230. /**
  231. * belongsTo associations
  232. *
  233. * @var array
  234. */
  235. public $belongsTo = array(
  236. 'Category' => array(
  237. 'className' => 'SoftDeleteCategory',
  238. 'counterCache' => true
  239. )
  240. );
  241. }
  242. /**
  243. * SoftDeletedPost returning true on delete()
  244. *
  245. */
  246. class ModifiedSoftDeletedPost extends SoftDeletedPost {
  247. public function delete($id = null, $cascade = true) {
  248. $result = parent::delete($id, $cascade);
  249. if (!$result && $this->Behaviors->loaded('SoftDelete')) {
  250. return $this->softDeleted;
  251. }
  252. return $result;
  253. }
  254. }