SoftDeleteableEntityTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace Gedmo\SoftDeleteable;
  3. use Tool\BaseTestCaseORM;
  4. use Doctrine\Common\EventManager;
  5. use Doctrine\Common\Util\Debug,
  6. SoftDeleteable\Fixture\Entity\Article,
  7. SoftDeleteable\Fixture\Entity\Comment,
  8. SoftDeleteable\Fixture\Entity\User,
  9. SoftDeleteable\Fixture\Entity\Page,
  10. SoftDeleteable\Fixture\Entity\MegaPage,
  11. SoftDeleteable\Fixture\Entity\Module,
  12. SoftDeleteable\Fixture\Entity\OtherArticle,
  13. SoftDeleteable\Fixture\Entity\OtherComment,
  14. Gedmo\SoftDeleteable\SoftDeleteableListener;
  15. /**
  16. * These are tests for SoftDeleteable behavior
  17. *
  18. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  19. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  20. * @package Gedmo.SoftDeleteable
  21. * @link http://www.gediminasm.org
  22. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  23. */
  24. class SoftDeleteableEntityTest extends BaseTestCaseORM
  25. {
  26. const ARTICLE_CLASS = 'SoftDeleteable\Fixture\Entity\Article';
  27. const COMMENT_CLASS = 'SoftDeleteable\Fixture\Entity\Comment';
  28. const PAGE_CLASS = 'SoftDeleteable\Fixture\Entity\Page';
  29. const MEGA_PAGE_CLASS = 'SoftDeleteable\Fixture\Entity\MegaPage';
  30. const MODULE_CLASS = 'SoftDeleteable\Fixture\Entity\Module';
  31. const OTHER_ARTICLE_CLASS = 'SoftDeleteable\Fixture\Entity\OtherArticle';
  32. const OTHER_COMMENT_CLASS = 'SoftDeleteable\Fixture\Entity\OtherComment';
  33. const USER_CLASS = 'SoftDeleteable\Fixture\Entity\User';
  34. const SOFT_DELETEABLE_FILTER_NAME = 'soft-deleteable';
  35. private $softDeleteableListener;
  36. protected function setUp()
  37. {
  38. parent::setUp();
  39. $evm = new EventManager;
  40. $this->softDeleteableListener = new SoftDeleteableListener();
  41. $evm->addEventSubscriber($this->softDeleteableListener);
  42. $config = $this->getMockAnnotatedConfig();
  43. $config->addFilter(self::SOFT_DELETEABLE_FILTER_NAME, 'Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter');
  44. $this->em = $this->getMockSqliteEntityManager($evm, $config);
  45. $this->em->getFilters()->enable(self::SOFT_DELETEABLE_FILTER_NAME);
  46. }
  47. /**
  48. * @test
  49. */
  50. public function shouldSoftlyDeleteIfColumnNameDifferFromPropertyName()
  51. {
  52. $repo = $this->em->getRepository(self::USER_CLASS);
  53. $newUser = new User();
  54. $username = 'test_user';
  55. $newUser->setUsername($username);
  56. $this->em->persist($newUser);
  57. $this->em->flush();
  58. $user = $repo->findOneBy(array('username' => $username));
  59. $this->assertNull($user->getDeletedAt());
  60. $this->em->remove($user);
  61. $this->em->flush();
  62. $user = $repo->findOneBy(array('username' => $username));
  63. $this->assertNull($user);
  64. }
  65. public function testSoftDeleteable()
  66. {
  67. $repo = $this->em->getRepository(self::ARTICLE_CLASS);
  68. $commentRepo = $this->em->getRepository(self::COMMENT_CLASS);
  69. $comment = new Comment();
  70. $commentField = 'comment';
  71. $commentValue = 'Comment 1';
  72. $comment->setComment($commentValue);
  73. $art0 = new Article();
  74. $field = 'title';
  75. $value = 'Title 1';
  76. $art0->setTitle($value);
  77. $art0->addComment($comment);
  78. $this->em->persist($art0);
  79. $this->em->flush();
  80. $art = $repo->findOneBy(array($field => $value));
  81. $this->assertNull($art->getDeletedAt());
  82. $this->assertNull($comment->getDeletedAt());
  83. $this->em->remove($art);
  84. $this->em->flush();
  85. $art = $repo->findOneBy(array($field => $value));
  86. $this->assertNull($art);
  87. $comment = $commentRepo->findOneBy(array($commentField => $commentValue));
  88. $this->assertNull($comment);
  89. // Now we deactivate the filter so we test if the entity appears in the result
  90. $this->em->getFilters()->disable(self::SOFT_DELETEABLE_FILTER_NAME);
  91. $this->em->clear();
  92. $art = $repo->findOneBy(array($field => $value));
  93. $this->assertTrue(is_object($art));
  94. $this->assertTrue(is_object($art->getDeletedAt()));
  95. $this->assertTrue($art->getDeletedAt() instanceof \DateTime);
  96. $comment = $commentRepo->findOneBy(array($commentField => $commentValue));
  97. $this->assertTrue(is_object($comment));
  98. $this->assertTrue(is_object($comment->getDeletedAt()));
  99. $this->assertTrue($comment->getDeletedAt() instanceof \DateTime);
  100. $this->em->createQuery('UPDATE '.self::ARTICLE_CLASS.' a SET a.deletedAt = NULL')->execute();
  101. $this->em->refresh($art);
  102. $this->em->refresh($comment);
  103. // Now we try with a DQL Delete query
  104. $this->em->getFilters()->enable(self::SOFT_DELETEABLE_FILTER_NAME);
  105. $dql = sprintf('DELETE FROM %s a WHERE a.%s = :%s',
  106. self::ARTICLE_CLASS, $field, $field);
  107. $query = $this->em->createQuery($dql);
  108. $query->setParameter($field, $value);
  109. $query->setHint(
  110. \Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
  111. 'Gedmo\SoftDeleteable\Query\TreeWalker\SoftDeleteableWalker'
  112. );
  113. $query->execute();
  114. $art = $repo->findOneBy(array($field => $value));
  115. $this->assertNull($art);
  116. // Now we deactivate the filter so we test if the entity appears in the result
  117. $this->em->getFilters()->disable(self::SOFT_DELETEABLE_FILTER_NAME);
  118. $this->em->clear();
  119. $art = $repo->findOneBy(array($field => $value));
  120. $this->assertTrue(is_object($art));
  121. $this->assertTrue(is_object($art->getDeletedAt()));
  122. $this->assertTrue($art->getDeletedAt() instanceof \DateTime);
  123. // Inheritance tree DELETE DQL
  124. $this->em->getFilters()->enable(self::SOFT_DELETEABLE_FILTER_NAME);
  125. $megaPageRepo = $this->em->getRepository(self::MEGA_PAGE_CLASS);
  126. $module = new Module();
  127. $module->setTitle('Module 1');
  128. $page = new MegaPage();
  129. $page->setTitle('Page 1');
  130. $page->addModule($module);
  131. $module->setPage($page);
  132. $this->em->persist($page);
  133. $this->em->persist($module);
  134. $this->em->flush();
  135. $dql = sprintf('DELETE FROM %s p',
  136. self::PAGE_CLASS);
  137. $query = $this->em->createQuery($dql);
  138. $query->setHint(
  139. \Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
  140. 'Gedmo\SoftDeleteable\Query\TreeWalker\SoftDeleteableWalker'
  141. );
  142. $query->execute();
  143. $p = $megaPageRepo->findOneBy(array('title' => 'Page 1'));
  144. $this->assertNull($p);
  145. // Now we deactivate the filter so we test if the entity appears in the result
  146. $this->em->getFilters()->disable(self::SOFT_DELETEABLE_FILTER_NAME);
  147. $this->em->clear();
  148. $p = $megaPageRepo->findOneBy(array('title' => 'Page 1'));
  149. $this->assertTrue(is_object($p));
  150. $this->assertTrue(is_object($p->getDeletedAt()));
  151. $this->assertTrue($p->getDeletedAt() instanceof \DateTime);
  152. // Test of #301
  153. $this->em->getFilters()->enable(self::SOFT_DELETEABLE_FILTER_NAME);
  154. $otherArticleRepo = $this->em->getRepository(self::OTHER_ARTICLE_CLASS);
  155. $otherCommentRepo = $this->em->getRepository(self::OTHER_COMMENT_CLASS);
  156. $otherArt = new OtherArticle();
  157. $otherComment = new OtherComment();
  158. $otherArt->setTitle('Page 1');
  159. $otherComment->setComment('Comment');
  160. $otherArt->addComment($otherComment);
  161. $otherComment->setArticle($otherArt);
  162. $this->em->persist($otherArt);
  163. $this->em->persist($otherComment);
  164. $this->em->flush();
  165. $this->em->refresh($otherArt);
  166. $this->em->refresh($otherComment);
  167. $artId = $otherArt->getId();
  168. $commentId = $otherComment->getId();
  169. $this->em->remove($otherArt);
  170. $this->em->flush();
  171. $foundArt = $otherArticleRepo->findOneBy(array('id' => $artId));
  172. $foundComment = $otherCommentRepo->findOneBy(array('id' => $commentId));
  173. $this->assertNull($foundArt);
  174. $this->assertTrue(is_object($foundComment));
  175. $this->assertInstanceOf(self::OTHER_COMMENT_CLASS, $foundComment);
  176. $this->em->getFilters()->disable(self::SOFT_DELETEABLE_FILTER_NAME);
  177. $foundArt = $otherArticleRepo->findOneById($artId);
  178. $foundComment = $otherCommentRepo->findOneById($commentId);
  179. $this->assertTrue(is_object($foundArt));
  180. $this->assertTrue(is_object($foundArt->getDeletedAt()));
  181. $this->assertTrue($foundArt->getDeletedAt() instanceof \DateTime);
  182. $this->assertTrue(is_object($foundComment));
  183. $this->assertInstanceOf(self::OTHER_COMMENT_CLASS, $foundComment);
  184. }
  185. protected function getUsedEntityFixtures()
  186. {
  187. return array(
  188. self::ARTICLE_CLASS,
  189. self::PAGE_CLASS,
  190. self::MEGA_PAGE_CLASS,
  191. self::MODULE_CLASS,
  192. self::COMMENT_CLASS,
  193. self::USER_CLASS,
  194. self::OTHER_ARTICLE_CLASS,
  195. self::OTHER_COMMENT_CLASS
  196. );
  197. }
  198. }