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. $this->assertWithinMargin(abs(strtotime($data['Post']['updated']) - strtotime($data['Post']['deleted_date'])), 0, 1);
  63. }
  64. /**
  65. * Test that overwriting delete() on AppModel level makes SoftDelete return true for delete()
  66. *
  67. * @return void
  68. */
  69. public function testSoftDeleteReturningTrue() {
  70. $this->Post = new ModifiedSoftDeletedPost();
  71. $this->Post->Behaviors->load('Tools.SoftDelete');
  72. $data = $this->Post->read(null, 1);
  73. $this->assertEquals($data[$this->Post->alias][$this->Post->primaryKey], 1);
  74. //$this->assertFalse($this->Post->softDeleted);
  75. $result = $this->Post->delete(1);
  76. $this->assertTrue($result);
  77. //$this->assertTrue($this->Post->softDeleted);
  78. }
  79. /**
  80. * testUnDelete
  81. *
  82. * @return void
  83. */
  84. public function testUnDelete() {
  85. $data = $this->Post->read(null, 1);
  86. $result = $this->Post->delete(1);
  87. $result = $this->Post->undelete(1);
  88. $data = $this->Post->read(null, 1);
  89. $this->assertEquals($data['Post']['deleted'], 0);
  90. }
  91. /**
  92. * testSoftDeletePurge
  93. *
  94. * @return void
  95. */
  96. public function testSoftDeletePurge() {
  97. $this->Post->Behaviors->disable('SoftDeleteTest');
  98. $data = $this->Post->read(null, 3);
  99. $this->assertTrue(!empty($data));
  100. $this->Post->Behaviors->enable('SoftDeleteTest');
  101. $data = $this->Post->read(null, 3);
  102. $this->assertEmpty($data);
  103. $count = $this->Post->purgeDeletedCount();
  104. $this->assertEquals($count, 1);
  105. $this->Post->purgeDeleted();
  106. $data = $this->Post->read(null, 3);
  107. $this->assertEmpty($data);
  108. $this->Post->Behaviors->disable('SoftDeleteTest');
  109. $data = $this->Post->read(null, 3);
  110. $this->assertEmpty($data);
  111. }
  112. // $result = $this->Model->read();
  113. // $this->assertEquals($result['SoftDeletedPost']['slug'], 'fourth_Post');
  114. ///Should not update
  115. // $this->Model->saveField('title', 'Fourth Post (Part 1)');
  116. // $result = $this->Model->read();
  117. // $this->assertEquals($result['SoftDeletedPost']['slug'], 'fourth_Post');
  118. ////Should update
  119. // $this->Model->Behaviors->SluggableTest->settings['SoftDeletedPost']['update'] = true;
  120. // $this->Model->saveField('title', 'Fourth Post (Part 2)');
  121. // $result = $this->Model->read();
  122. // $this->assertEquals($result['SoftDeletedPost']['slug'], 'fourth_Post_part_2');
  123. ////Updating the item should not update the slug
  124. // $this->Model->saveField('body', 'Here goes the content.');
  125. // $result = $this->Model->read();
  126. // $this->assertEquals($result['SoftDeletedPost']['slug'], 'fourth_Post_part_2');
  127. }
  128. /**
  129. * SoftDeleteTestBehavior
  130. *
  131. */
  132. class SoftDeleteTestBehavior extends SoftDeleteBehavior {
  133. }
  134. /**
  135. * SoftDeletedPost
  136. *
  137. */
  138. class SoftDeletedPost extends CakeTestModel {
  139. /**
  140. * Use Table
  141. *
  142. * @var string
  143. */
  144. public $useTable = 'soft_delete_posts';
  145. /**
  146. * Behaviors
  147. *
  148. * @var array
  149. */
  150. public $actsAs = array('Tools.SoftDeleteTest');
  151. /**
  152. * Alias
  153. *
  154. * @var string
  155. */
  156. public $alias = 'Post';
  157. }
  158. /**
  159. * SoftDeletedPost returning true on delete()
  160. *
  161. */
  162. class ModifiedSoftDeletedPost extends SoftDeletedPost {
  163. public function delete($id = null, $cascade = true) {
  164. $result = parent::delete($id, $cascade);
  165. if (!$result && $this->Behaviors->loaded('SoftDelete')) {
  166. return $this->softDeleted;
  167. }
  168. return $result;
  169. }
  170. }