SoftDeleteableEntityTest.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. SoftDeleteable\Fixture\Entity\Child,
  15. Gedmo\SoftDeleteable\SoftDeleteableListener;
  16. /**
  17. * These are tests for SoftDeleteable behavior
  18. *
  19. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  20. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  21. * @author Patrik Votoček <patrik@votocek.cz>
  22. * @package Gedmo.SoftDeleteable
  23. * @link http://www.gediminasm.org
  24. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  25. */
  26. class SoftDeleteableEntityTest extends BaseTestCaseORM
  27. {
  28. const ARTICLE_CLASS = 'SoftDeleteable\Fixture\Entity\Article';
  29. const COMMENT_CLASS = 'SoftDeleteable\Fixture\Entity\Comment';
  30. const PAGE_CLASS = 'SoftDeleteable\Fixture\Entity\Page';
  31. const MEGA_PAGE_CLASS = 'SoftDeleteable\Fixture\Entity\MegaPage';
  32. const MODULE_CLASS = 'SoftDeleteable\Fixture\Entity\Module';
  33. const OTHER_ARTICLE_CLASS = 'SoftDeleteable\Fixture\Entity\OtherArticle';
  34. const OTHER_COMMENT_CLASS = 'SoftDeleteable\Fixture\Entity\OtherComment';
  35. const USER_CLASS = 'SoftDeleteable\Fixture\Entity\User';
  36. const MAPPED_SUPERCLASS_CHILD_CLASS = 'SoftDeleteable\Fixture\Entity\Child';
  37. const SOFT_DELETEABLE_FILTER_NAME = 'soft-deleteable';
  38. private $softDeleteableListener;
  39. protected function setUp()
  40. {
  41. parent::setUp();
  42. $evm = new EventManager();
  43. $this->softDeleteableListener = new SoftDeleteableListener();
  44. $evm->addEventSubscriber($this->softDeleteableListener);
  45. $config = $this->getMockAnnotatedConfig();
  46. $config->addFilter(self::SOFT_DELETEABLE_FILTER_NAME, 'Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter');
  47. $this->em = $this->getMockSqliteEntityManager($evm, $config);
  48. $this->em->getFilters()->enable(self::SOFT_DELETEABLE_FILTER_NAME);
  49. }
  50. /**
  51. * @test
  52. */
  53. public function shouldSoftlyDeleteIfColumnNameDifferFromPropertyName()
  54. {
  55. $repo = $this->em->getRepository(self::USER_CLASS);
  56. $newUser = new User();
  57. $username = 'test_user';
  58. $newUser->setUsername($username);
  59. $this->em->persist($newUser);
  60. $this->em->flush();
  61. $user = $repo->findOneBy(array('username' => $username));
  62. $this->assertNull($user->getDeletedAt());
  63. $this->em->remove($user);
  64. $this->em->flush();
  65. $user = $repo->findOneBy(array('username' => $username));
  66. $this->assertNull($user);
  67. }
  68. public function testSoftDeleteable()
  69. {
  70. $repo = $this->em->getRepository(self::ARTICLE_CLASS);
  71. $commentRepo = $this->em->getRepository(self::COMMENT_CLASS);
  72. $comment = new Comment();
  73. $commentField = 'comment';
  74. $commentValue = 'Comment 1';
  75. $comment->setComment($commentValue);
  76. $art0 = new Article();
  77. $field = 'title';
  78. $value = 'Title 1';
  79. $art0->setTitle($value);
  80. $art0->addComment($comment);
  81. $this->em->persist($art0);
  82. $this->em->flush();
  83. $art = $repo->findOneBy(array($field => $value));
  84. $this->assertNull($art->getDeletedAt());
  85. $this->assertNull($comment->getDeletedAt());
  86. $this->em->remove($art);
  87. $this->em->flush();
  88. $art = $repo->findOneBy(array($field => $value));
  89. $this->assertNull($art);
  90. $comment = $commentRepo->findOneBy(array($commentField => $commentValue));
  91. $this->assertNull($comment);
  92. // Now we deactivate the filter so we test if the entity appears in the result
  93. $this->em->getFilters()->disable(self::SOFT_DELETEABLE_FILTER_NAME);
  94. $this->em->clear();
  95. $art = $repo->findOneBy(array($field => $value));
  96. $this->assertTrue(is_object($art));
  97. $this->assertTrue(is_object($art->getDeletedAt()));
  98. $this->assertTrue($art->getDeletedAt() instanceof \DateTime);
  99. $comment = $commentRepo->findOneBy(array($commentField => $commentValue));
  100. $this->assertTrue(is_object($comment));
  101. $this->assertTrue(is_object($comment->getDeletedAt()));
  102. $this->assertTrue($comment->getDeletedAt() instanceof \DateTime);
  103. $this->em->createQuery('UPDATE '.self::ARTICLE_CLASS.' a SET a.deletedAt = NULL')->execute();
  104. $this->em->refresh($art);
  105. $this->em->refresh($comment);
  106. // Now we try with a DQL Delete query
  107. $this->em->getFilters()->enable(self::SOFT_DELETEABLE_FILTER_NAME);
  108. $dql = sprintf('DELETE FROM %s a WHERE a.%s = :%s',
  109. self::ARTICLE_CLASS, $field, $field);
  110. $query = $this->em->createQuery($dql);
  111. $query->setParameter($field, $value);
  112. $query->setHint(
  113. \Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
  114. 'Gedmo\SoftDeleteable\Query\TreeWalker\SoftDeleteableWalker'
  115. );
  116. $query->execute();
  117. $art = $repo->findOneBy(array($field => $value));
  118. $this->assertNull($art);
  119. // Now we deactivate the filter so we test if the entity appears in the result
  120. $this->em->getFilters()->disable(self::SOFT_DELETEABLE_FILTER_NAME);
  121. $this->em->clear();
  122. $art = $repo->findOneBy(array($field => $value));
  123. $this->assertTrue(is_object($art));
  124. $this->assertTrue(is_object($art->getDeletedAt()));
  125. $this->assertTrue($art->getDeletedAt() instanceof \DateTime);
  126. // Inheritance tree DELETE DQL
  127. $this->em->getFilters()->enable(self::SOFT_DELETEABLE_FILTER_NAME);
  128. $megaPageRepo = $this->em->getRepository(self::MEGA_PAGE_CLASS);
  129. $module = new Module();
  130. $module->setTitle('Module 1');
  131. $page = new MegaPage();
  132. $page->setTitle('Page 1');
  133. $page->addModule($module);
  134. $module->setPage($page);
  135. $this->em->persist($page);
  136. $this->em->persist($module);
  137. $this->em->flush();
  138. $dql = sprintf('DELETE FROM %s p',
  139. self::PAGE_CLASS);
  140. $query = $this->em->createQuery($dql);
  141. $query->setHint(
  142. \Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
  143. 'Gedmo\SoftDeleteable\Query\TreeWalker\SoftDeleteableWalker'
  144. );
  145. $query->execute();
  146. $p = $megaPageRepo->findOneBy(array('title' => 'Page 1'));
  147. $this->assertNull($p);
  148. // Now we deactivate the filter so we test if the entity appears in the result
  149. $this->em->getFilters()->disable(self::SOFT_DELETEABLE_FILTER_NAME);
  150. $this->em->clear();
  151. $p = $megaPageRepo->findOneBy(array('title' => 'Page 1'));
  152. $this->assertTrue(is_object($p));
  153. $this->assertTrue(is_object($p->getDeletedAt()));
  154. $this->assertTrue($p->getDeletedAt() instanceof \DateTime);
  155. // Test of #301
  156. $this->em->getFilters()->enable(self::SOFT_DELETEABLE_FILTER_NAME);
  157. $otherArticleRepo = $this->em->getRepository(self::OTHER_ARTICLE_CLASS);
  158. $otherCommentRepo = $this->em->getRepository(self::OTHER_COMMENT_CLASS);
  159. $otherArt = new OtherArticle();
  160. $otherComment = new OtherComment();
  161. $otherArt->setTitle('Page 1');
  162. $otherComment->setComment('Comment');
  163. $otherArt->addComment($otherComment);
  164. $otherComment->setArticle($otherArt);
  165. $this->em->persist($otherArt);
  166. $this->em->persist($otherComment);
  167. $this->em->flush();
  168. $this->em->refresh($otherArt);
  169. $this->em->refresh($otherComment);
  170. $artId = $otherArt->getId();
  171. $commentId = $otherComment->getId();
  172. $this->em->remove($otherArt);
  173. $this->em->flush();
  174. $foundArt = $otherArticleRepo->findOneBy(array('id' => $artId));
  175. $foundComment = $otherCommentRepo->findOneBy(array('id' => $commentId));
  176. $this->assertNull($foundArt);
  177. $this->assertTrue(is_object($foundComment));
  178. $this->assertInstanceOf(self::OTHER_COMMENT_CLASS, $foundComment);
  179. $this->em->getFilters()->disable(self::SOFT_DELETEABLE_FILTER_NAME);
  180. $foundArt = $otherArticleRepo->findOneById($artId);
  181. $foundComment = $otherCommentRepo->findOneById($commentId);
  182. $this->assertTrue(is_object($foundArt));
  183. $this->assertTrue(is_object($foundArt->getDeletedAt()));
  184. $this->assertTrue($foundArt->getDeletedAt() instanceof \DateTime);
  185. $this->assertTrue(is_object($foundComment));
  186. $this->assertInstanceOf(self::OTHER_COMMENT_CLASS, $foundComment);
  187. }
  188. /**
  189. * Make sure that soft delete also works when configured on a mapped superclass
  190. */
  191. public function testMappedSuperclass()
  192. {
  193. $child = new Child();
  194. $child->setTitle('test title');
  195. $this->em->persist($child);
  196. $this->em->flush();
  197. $this->em->remove($child);
  198. $this->em->flush();
  199. $this->em->clear();
  200. $repo = $this->em->getRepository(self::MAPPED_SUPERCLASS_CHILD_CLASS);
  201. $this->assertNull($repo->findOneById($child->getId()));
  202. $this->em->getFilters()->enable(self::SOFT_DELETEABLE_FILTER_NAME);
  203. $this->assertNotNull($repo->findById($child->getId()));
  204. }
  205. public function testSoftDeleteableFilter()
  206. {
  207. $filter = $this->em->getFilters()->enable(self::SOFT_DELETEABLE_FILTER_NAME);
  208. $filter->disableForEntity(self::USER_CLASS);
  209. $repo = $this->em->getRepository(self::USER_CLASS);
  210. $newUser = new User();
  211. $username = 'test_user';
  212. $newUser->setUsername($username);
  213. $this->em->persist($newUser);
  214. $this->em->flush();
  215. $user = $repo->findOneBy(array('username' => $username));
  216. $this->assertNull($user->getDeletedAt());
  217. $this->em->remove($user);
  218. $this->em->flush();
  219. $user = $repo->findOneBy(array('username' => $username));
  220. $this->assertNotNull($user->getDeletedAt());
  221. $filter->enableForEntity(self::USER_CLASS);
  222. $user = $repo->findOneBy(array('username' => $username));
  223. $this->assertNull($user);
  224. }
  225. public function testPostSoftDeleteEventIsDispatched()
  226. {
  227. $subscriber = $this->getMock(
  228. "Doctrine\Common\EventSubscriber",
  229. array(
  230. "getSubscribedEvents",
  231. "preSoftDelete",
  232. "postSoftDelete"
  233. )
  234. );
  235. $subscriber->expects($this->once())
  236. ->method("getSubscribedEvents")
  237. ->will($this->returnValue(array(SoftDeleteableListener::PRE_SOFT_DELETE, SoftDeleteableListener::POST_SOFT_DELETE)));
  238. $subscriber->expects($this->exactly(2))
  239. ->method("preSoftDelete")
  240. ->with($this->anything());
  241. $subscriber->expects($this->exactly(2))
  242. ->method("postSoftDelete")
  243. ->with($this->anything());
  244. $this->em->getEventManager()->addEventSubscriber($subscriber);
  245. $repo = $this->em->getRepository(self::ARTICLE_CLASS);
  246. $commentRepo = $this->em->getRepository(self::COMMENT_CLASS);
  247. $comment = new Comment();
  248. $commentField = 'comment';
  249. $commentValue = 'Comment 1';
  250. $comment->setComment($commentValue);
  251. $art0 = new Article();
  252. $field = 'title';
  253. $value = 'Title 1';
  254. $art0->setTitle($value);
  255. $art0->addComment($comment);
  256. $this->em->persist($art0);
  257. $this->em->flush();
  258. $art = $repo->findOneBy(array($field => $value));
  259. $this->assertNull($art->getDeletedAt());
  260. $this->assertNull($comment->getDeletedAt());
  261. $this->em->remove($art);
  262. $this->em->flush();
  263. }
  264. protected function getUsedEntityFixtures()
  265. {
  266. return array(
  267. self::ARTICLE_CLASS,
  268. self::PAGE_CLASS,
  269. self::MEGA_PAGE_CLASS,
  270. self::MODULE_CLASS,
  271. self::COMMENT_CLASS,
  272. self::USER_CLASS,
  273. self::OTHER_ARTICLE_CLASS,
  274. self::OTHER_COMMENT_CLASS,
  275. self::MAPPED_SUPERCLASS_CHILD_CLASS,
  276. );
  277. }
  278. }