SoftDeleteBehaviorTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 http://opensource.org/licenses/mit-license.php MIT
  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. 'plugin.tools.soft_delete_user'
  27. );
  28. /**
  29. * Creates the model instance
  30. *
  31. * @return void
  32. */
  33. public function setUp() {
  34. parent::setUp();
  35. $this->Post = new SoftDeletedPost();
  36. $this->Behavior = new SoftDeleteTestBehavior();
  37. }
  38. /**
  39. * Destroy the model instance
  40. *
  41. * @return void
  42. */
  43. public function tearDown() {
  44. parent::tearDown();
  45. unset($this->Post);
  46. unset($this->Behavior);
  47. ClassRegistry::flush();
  48. }
  49. /**
  50. * Test saving a item
  51. *
  52. * @return void
  53. */
  54. public function testSoftDelete() {
  55. $data = $this->Post->read(null, 1);
  56. $this->assertEquals($data[$this->Post->alias][$this->Post->primaryKey], 1);
  57. $this->assertFalse($this->Post->softDeleted);
  58. $result = $this->Post->delete(1);
  59. $this->assertFalse($result);
  60. $this->assertTrue($this->Post->softDeleted);
  61. $data = $this->Post->read(null, 1);
  62. $this->assertEmpty($data);
  63. $this->Post->Behaviors->unload('SoftDeleteTest');
  64. $data = $this->Post->read(null, 1);
  65. $this->assertEquals($data['Post']['deleted'], 1);
  66. //$result = abs(strtotime($data['Post']['updated']) - strtotime($data['Post']['deleted_date']));
  67. //$this->assertWithinMargin($result, 0, 1, $data['Post']['updated'].'/'.$data['Post']['deleted_date']);
  68. }
  69. /**
  70. * Test that overwriting delete() on AppModel level makes SoftDelete return true for delete()
  71. *
  72. * @return void
  73. */
  74. public function testSoftDeleteReturningTrue() {
  75. $this->Post = new ModifiedSoftDeletedPost();
  76. $this->Post->Behaviors->load('Tools.SoftDelete');
  77. $data = $this->Post->read(null, 1);
  78. $this->assertEquals($data[$this->Post->alias][$this->Post->primaryKey], 1);
  79. //$this->assertFalse($this->Post->softDeleted);
  80. $result = $this->Post->delete(1);
  81. $this->assertTrue($result);
  82. //$this->assertTrue($this->Post->softDeleted);
  83. }
  84. /**
  85. * TestUnDelete
  86. *
  87. * @return void
  88. */
  89. public function testUnDelete() {
  90. $data = $this->Post->read(null, 1);
  91. $result = $this->Post->delete(1);
  92. $result = $this->Post->undelete(1);
  93. $data = $this->Post->read(null, 1);
  94. $this->assertEquals($data['Post']['deleted'], 0);
  95. }
  96. /**
  97. * TestSoftDeletePurge
  98. *
  99. * @return void
  100. */
  101. public function testSoftDeletePurge() {
  102. $this->Post->Behaviors->disable('SoftDeleteTest');
  103. $data = $this->Post->read(null, 3);
  104. $this->assertTrue(!empty($data));
  105. $this->Post->Behaviors->enable('SoftDeleteTest');
  106. $data = $this->Post->read(null, 3);
  107. $this->assertEmpty($data);
  108. $count = $this->Post->purgeDeletedCount();
  109. $this->assertEquals($count, 1);
  110. $this->Post->purgeDeleted();
  111. $data = $this->Post->read(null, 3);
  112. $this->assertEmpty($data);
  113. $this->Post->Behaviors->disable('SoftDeleteTest');
  114. $data = $this->Post->read(null, 3);
  115. $this->assertEmpty($data);
  116. }
  117. /**
  118. * TestSoftDeleteWithCounterCache
  119. *
  120. * @return void
  121. */
  122. public function testSoftDeleteWithCounterCache() {
  123. $this->Post->Category->id = 1;
  124. $count = $this->Post->Category->field('post_count');
  125. $this->assertEquals(2, $count);
  126. $this->assertFalse($this->Post->softDeleted);
  127. $this->Post->delete(1);
  128. $this->assertTrue($this->Post->softDeleted);
  129. $count = $this->Post->Category->field('post_count');
  130. $this->assertEquals(1, $count);
  131. }
  132. /**
  133. * TestSoftDeleteWithMultipleCounterCache
  134. *
  135. * @return void
  136. */
  137. public function testSoftDeleteWithMultipleCounterCache() {
  138. $this->Post->belongsTo['Category']['counterCache'] = array(
  139. 'post_count' => array('Post.deleted' => false),
  140. 'deleted_post_count' => array('Post.deleted' => true)
  141. );
  142. $this->Post->Category->id = 1;
  143. $count = $this->Post->Category->field('post_count');
  144. $this->assertEquals(2, $count);
  145. $count = $this->Post->Category->field('deleted_post_count');
  146. $this->assertEquals(0, $count);
  147. $this->assertFalse($this->Post->softDeleted);
  148. $this->Post->delete(1);
  149. $this->assertTrue($this->Post->softDeleted);
  150. $count = $this->Post->Category->field('post_count');
  151. $this->assertEquals(1, $count);
  152. $count = $this->Post->Category->field('deleted_post_count');
  153. $this->assertEquals(1, $count);
  154. }
  155. /**
  156. * TestSoftDeleteWithCounterCacheOnMultipleAssociations
  157. *
  158. * @return void
  159. */
  160. public function testSoftDeleteWithCounterCacheOnMultipleAssociations() {
  161. $this->Post->bindModel(array(
  162. 'belongsTo' => array(
  163. 'User' => array(
  164. 'className' => 'SoftDeleteUser',
  165. 'counterCache' => true
  166. )
  167. )
  168. ),
  169. false);
  170. $this->Post->Category->id = 1;
  171. $this->Post->User->id = 1;
  172. $count = $this->Post->Category->field('post_count');
  173. $this->assertEquals(2, $count);
  174. $count = $this->Post->User->field('post_count');
  175. $this->assertEquals(2, $count);
  176. $this->assertFalse($this->Post->softDeleted);
  177. $this->Post->delete(1);
  178. $this->assertTrue($this->Post->softDeleted);
  179. $count = $this->Post->Category->field('post_count');
  180. $this->assertEquals(1, $count);
  181. $count = $this->Post->User->field('post_count');
  182. $this->assertEquals(1, $count);
  183. }
  184. /**
  185. * TestSoftDeleteWithoutCounterCache
  186. *
  187. * @return void
  188. */
  189. public function testSoftDeleteWithoutCounterCache() {
  190. $Post = $this->getMock('SoftDeletedPost', array('updateCounterCache'));
  191. $Post->expects($this->never())->method('updateCounterCache');
  192. $Post->belongsTo = array();
  193. $Post->delete(1);
  194. }
  195. /**
  196. * TestUnDeleteWithCounterCache
  197. *
  198. * @return void
  199. */
  200. public function testUnDeleteWithCounterCache() {
  201. $this->Post->Category->id = 2;
  202. $count = $this->Post->Category->field('post_count');
  203. $this->assertEquals(0, $count);
  204. $this->assertEmpty($this->Post->read(null, 3));
  205. $this->Post->undelete(3);
  206. $count = $this->Post->Category->field('post_count');
  207. $this->assertEquals(1, $count);
  208. }
  209. /**
  210. * TestUnDeleteWithMultipleCounterCache
  211. *
  212. * @return void
  213. */
  214. public function testUnDeleteWithMultipleCounterCache() {
  215. $this->Post->belongsTo['Category']['counterCache'] = array(
  216. 'post_count' => array('Post.deleted' => false),
  217. 'deleted_post_count' => array('Post.deleted' => true)
  218. );
  219. $this->Post->Category->id = 2;
  220. $count = $this->Post->Category->field('post_count');
  221. $this->assertEquals(0, $count);
  222. $count = $this->Post->Category->field('deleted_post_count');
  223. $this->assertEquals(1, $count);
  224. $this->assertEmpty($this->Post->read(null, 3));
  225. $this->Post->undelete(3);
  226. $count = $this->Post->Category->field('post_count');
  227. $this->assertEquals(1, $count);
  228. $count = $this->Post->Category->field('deleted_post_count');
  229. $this->assertEquals(0, $count);
  230. }
  231. /**
  232. * TestUnDeleteWithCounterCacheOnMultipleAssociations
  233. *
  234. * @return void
  235. */
  236. public function testUnDeleteWithCounterCacheOnMultipleAssociations() {
  237. $this->Post->bindModel(array(
  238. 'belongsTo' => array(
  239. 'User' => array(
  240. 'className' => 'SoftDeleteUser',
  241. 'counterCache' => true
  242. )
  243. )
  244. ),
  245. false);
  246. $this->Post->Category->id = 2;
  247. $this->Post->User->id = 1;
  248. $count = $this->Post->Category->field('post_count');
  249. $this->assertEquals(0, $count);
  250. $count = $this->Post->User->field('post_count');
  251. $this->assertEquals(2, $count);
  252. $this->assertEmpty($this->Post->read(null, 3));
  253. $this->Post->undelete(3);
  254. $count = $this->Post->Category->field('post_count');
  255. $this->assertEquals(1, $count);
  256. $count = $this->Post->User->field('post_count');
  257. $this->assertEquals(3, $count);
  258. }
  259. /**
  260. * TestUnDeleteWithoutCounterCache
  261. *
  262. * @return void
  263. */
  264. public function testUnDeleteWithoutCounterCache() {
  265. $Post = $this->getMock('SoftDeletedPost', array('updateCounterCache'));
  266. $Post->expects($this->never())->method('updateCounterCache');
  267. $Post->belongsTo = array();
  268. $Post->undelete(3);
  269. }
  270. // $result = $this->Model->read();
  271. // $this->assertEquals($result['SoftDeletedPost']['slug'], 'fourth_Post');
  272. ///Should not update
  273. // $this->Model->saveField('title', 'Fourth Post (Part 1)');
  274. // $result = $this->Model->read();
  275. // $this->assertEquals($result['SoftDeletedPost']['slug'], 'fourth_Post');
  276. ////Should update
  277. // $this->Model->Behaviors->SluggableTest->settings['SoftDeletedPost']['update'] = true;
  278. // $this->Model->saveField('title', 'Fourth Post (Part 2)');
  279. // $result = $this->Model->read();
  280. // $this->assertEquals($result['SoftDeletedPost']['slug'], 'fourth_Post_part_2');
  281. ////Updating the item should not update the slug
  282. // $this->Model->saveField('body', 'Here goes the content.');
  283. // $result = $this->Model->read();
  284. // $this->assertEquals($result['SoftDeletedPost']['slug'], 'fourth_Post_part_2');
  285. }
  286. /**
  287. * SoftDeleteTestBehavior
  288. *
  289. */
  290. class SoftDeleteTestBehavior extends SoftDeleteBehavior {
  291. }
  292. /**
  293. * SoftDeleteCategory
  294. *
  295. */
  296. class SoftDeleteCategory extends CakeTestModel {
  297. /**
  298. * Use Table
  299. *
  300. * @var string
  301. */
  302. public $useTable = 'soft_delete_categories';
  303. /**
  304. * Alias
  305. *
  306. * @var string
  307. */
  308. public $alias = 'Category';
  309. }
  310. /**
  311. * SoftDeleteUser
  312. *
  313. */
  314. class SoftDeleteUser extends CakeTestModel {
  315. /**
  316. * Use Table
  317. *
  318. * @var string
  319. */
  320. public $useTable = 'soft_delete_users';
  321. /**
  322. * Alias
  323. *
  324. * @var string
  325. */
  326. public $alias = 'User';
  327. }
  328. /**
  329. * SoftDeletedPost
  330. *
  331. */
  332. class SoftDeletedPost extends CakeTestModel {
  333. /**
  334. * Use Table
  335. *
  336. * @var string
  337. */
  338. public $useTable = 'soft_delete_posts';
  339. /**
  340. * Behaviors
  341. *
  342. * @var array
  343. */
  344. public $actsAs = array('Tools.SoftDeleteTest');
  345. /**
  346. * Alias
  347. *
  348. * @var string
  349. */
  350. public $alias = 'Post';
  351. /**
  352. * belongsTo associations
  353. *
  354. * @var array
  355. */
  356. public $belongsTo = array(
  357. 'Category' => array(
  358. 'className' => 'SoftDeleteCategory',
  359. 'counterCache' => true
  360. )
  361. );
  362. }
  363. /**
  364. * SoftDeletedPost returning true on delete()
  365. *
  366. */
  367. class ModifiedSoftDeletedPost extends SoftDeletedPost {
  368. public function delete($id = null, $cascade = true) {
  369. $result = parent::delete($id, $cascade);
  370. if (!$result && $this->Behaviors->loaded('SoftDelete')) {
  371. return $this->softDeleted;
  372. }
  373. return $result;
  374. }
  375. }