SoftDeleteBehaviorTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. * testSoftDeleteWithMultipleCounterCache
  133. *
  134. * @return void
  135. */
  136. public function testSoftDeleteWithMultipleCounterCache() {
  137. $this->Post->belongsTo['Category']['counterCache'] = array(
  138. 'post_count' => array('Post.deleted' => false),
  139. 'deleted_post_count' => array('Post.deleted' => true)
  140. );
  141. $this->Post->Category->id = 1;
  142. $count = $this->Post->Category->field('post_count');
  143. $this->assertEquals(2, $count);
  144. $count = $this->Post->Category->field('deleted_post_count');
  145. $this->assertEquals(0, $count);
  146. $this->assertFalse($this->Post->softDeleted);
  147. $this->Post->delete(1);
  148. $this->assertTrue($this->Post->softDeleted);
  149. $count = $this->Post->Category->field('post_count');
  150. $this->assertEquals(1, $count);
  151. $count = $this->Post->Category->field('deleted_post_count');
  152. $this->assertEquals(1, $count);
  153. }
  154. /**
  155. * testSoftDeleteWithoutCounterCache
  156. *
  157. * @return void
  158. */
  159. public function testSoftDeleteWithoutCounterCache() {
  160. $Post = $this->getMock('SoftDeletedPost', array('updateCounterCache'));
  161. $Post->expects($this->never())->method('updateCounterCache');
  162. $Post->belongsTo = array();
  163. $Post->delete(1);
  164. }
  165. /**
  166. * testUnDeleteWithCounterCache
  167. *
  168. * @return void
  169. */
  170. public function testUnDeleteWithCounterCache() {
  171. $this->Post->Category->id = 2;
  172. $count = $this->Post->Category->field('post_count');
  173. $this->assertEquals($count, 0);
  174. $this->assertEmpty($this->Post->read(null, 3));
  175. $this->Post->undelete(3);
  176. $count = $this->Post->Category->field('post_count');
  177. $this->assertEquals(1, $count);
  178. }
  179. /**
  180. * testUnDeleteWithMultipleCounterCache
  181. *
  182. * @return void
  183. */
  184. public function testUnDeleteWithMultipleCounterCache() {
  185. $this->Post->belongsTo['Category']['counterCache'] = array(
  186. 'post_count' => array('Post.deleted' => false),
  187. 'deleted_post_count' => array('Post.deleted' => true)
  188. );
  189. $this->Post->Category->id = 2;
  190. $count = $this->Post->Category->field('post_count');
  191. $this->assertEquals($count, 0);
  192. $count = $this->Post->Category->field('deleted_post_count');
  193. $this->assertEquals($count, 1);
  194. $this->assertEmpty($this->Post->read(null, 3));
  195. $this->Post->undelete(3);
  196. $count = $this->Post->Category->field('post_count');
  197. $this->assertEquals(1, $count);
  198. $count = $this->Post->Category->field('deleted_post_count');
  199. $this->assertEquals(0, $count);
  200. }
  201. /**
  202. * testUnDeleteWithoutCounterCache
  203. *
  204. * @return void
  205. */
  206. public function testUnDeleteWithoutCounterCache() {
  207. $Post = $this->getMock('SoftDeletedPost', array('updateCounterCache'));
  208. $Post->expects($this->never())->method('updateCounterCache');
  209. $Post->belongsTo = array();
  210. $Post->undelete(3);
  211. }
  212. // $result = $this->Model->read();
  213. // $this->assertEquals($result['SoftDeletedPost']['slug'], 'fourth_Post');
  214. ///Should not update
  215. // $this->Model->saveField('title', 'Fourth Post (Part 1)');
  216. // $result = $this->Model->read();
  217. // $this->assertEquals($result['SoftDeletedPost']['slug'], 'fourth_Post');
  218. ////Should update
  219. // $this->Model->Behaviors->SluggableTest->settings['SoftDeletedPost']['update'] = true;
  220. // $this->Model->saveField('title', 'Fourth Post (Part 2)');
  221. // $result = $this->Model->read();
  222. // $this->assertEquals($result['SoftDeletedPost']['slug'], 'fourth_Post_part_2');
  223. ////Updating the item should not update the slug
  224. // $this->Model->saveField('body', 'Here goes the content.');
  225. // $result = $this->Model->read();
  226. // $this->assertEquals($result['SoftDeletedPost']['slug'], 'fourth_Post_part_2');
  227. }
  228. /**
  229. * SoftDeleteTestBehavior
  230. *
  231. */
  232. class SoftDeleteTestBehavior extends SoftDeleteBehavior {
  233. }
  234. /**
  235. * SoftDeleteCategory
  236. *
  237. */
  238. class SoftDeleteCategory extends CakeTestModel {
  239. /**
  240. * Use Table
  241. *
  242. * @var string
  243. */
  244. public $useTable = 'soft_delete_categories';
  245. /**
  246. * Alias
  247. *
  248. * @var string
  249. */
  250. public $alias = 'Category';
  251. }
  252. /**
  253. * SoftDeletedPost
  254. *
  255. */
  256. class SoftDeletedPost extends CakeTestModel {
  257. /**
  258. * Use Table
  259. *
  260. * @var string
  261. */
  262. public $useTable = 'soft_delete_posts';
  263. /**
  264. * Behaviors
  265. *
  266. * @var array
  267. */
  268. public $actsAs = array('Tools.SoftDeleteTest');
  269. /**
  270. * Alias
  271. *
  272. * @var string
  273. */
  274. public $alias = 'Post';
  275. /**
  276. * belongsTo associations
  277. *
  278. * @var array
  279. */
  280. public $belongsTo = array(
  281. 'Category' => array(
  282. 'className' => 'SoftDeleteCategory',
  283. 'counterCache' => true
  284. )
  285. );
  286. }
  287. /**
  288. * SoftDeletedPost returning true on delete()
  289. *
  290. */
  291. class ModifiedSoftDeletedPost extends SoftDeletedPost {
  292. public function delete($id = null, $cascade = true) {
  293. $result = parent::delete($id, $cascade);
  294. if (!$result && $this->Behaviors->loaded('SoftDelete')) {
  295. return $this->softDeleted;
  296. }
  297. return $result;
  298. }
  299. }