diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-10-07 23:31:53 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-10-07 23:31:53 +0200 |
commit | ac8cf632bb3a225c1b69d16e714ff60a2e988c89 (patch) | |
tree | 6789145b0518564d943399fd1b37830089cc4376 /src | |
parent | 3049afe190d125e4861059b6bbad7c6fbea6f1bb (diff) | |
download | wallabag-ac8cf632bb3a225c1b69d16e714ff60a2e988c89.tar.gz wallabag-ac8cf632bb3a225c1b69d16e714ff60a2e988c89.tar.zst wallabag-ac8cf632bb3a225c1b69d16e714ff60a2e988c89.zip |
Ensure orphan tag are remove in API
When the association between a tag and an entry is removed, if the tag doesn’t have other entries, we can remove it.
Also add more tests for that part and ensure TagControllerTest is isolated from the rest of the test suite (finally!)
Diffstat (limited to 'src')
3 files changed, 33 insertions, 3 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php index a0d9d4f3..cc6923a0 100644 --- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php +++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php | |||
@@ -400,6 +400,8 @@ class WallabagRestController extends FOSRestController | |||
400 | ->getRepository('WallabagCoreBundle:Entry') | 400 | ->getRepository('WallabagCoreBundle:Entry') |
401 | ->removeTag($this->getUser()->getId(), $tag); | 401 | ->removeTag($this->getUser()->getId(), $tag); |
402 | 402 | ||
403 | $this->cleanOrphanTag($tag); | ||
404 | |||
403 | $json = $this->get('serializer')->serialize($tag, 'json'); | 405 | $json = $this->get('serializer')->serialize($tag, 'json'); |
404 | 406 | ||
405 | return (new JsonResponse())->setJson($json); | 407 | return (new JsonResponse())->setJson($json); |
@@ -440,6 +442,8 @@ class WallabagRestController extends FOSRestController | |||
440 | ->getRepository('WallabagCoreBundle:Entry') | 442 | ->getRepository('WallabagCoreBundle:Entry') |
441 | ->removeTags($this->getUser()->getId(), $tags); | 443 | ->removeTags($this->getUser()->getId(), $tags); |
442 | 444 | ||
445 | $this->cleanOrphanTag($tags); | ||
446 | |||
443 | $json = $this->get('serializer')->serialize($tags, 'json'); | 447 | $json = $this->get('serializer')->serialize($tags, 'json'); |
444 | 448 | ||
445 | return (new JsonResponse())->setJson($json); | 449 | return (new JsonResponse())->setJson($json); |
@@ -464,6 +468,8 @@ class WallabagRestController extends FOSRestController | |||
464 | ->getRepository('WallabagCoreBundle:Entry') | 468 | ->getRepository('WallabagCoreBundle:Entry') |
465 | ->removeTag($this->getUser()->getId(), $tag); | 469 | ->removeTag($this->getUser()->getId(), $tag); |
466 | 470 | ||
471 | $this->cleanOrphanTag($tag); | ||
472 | |||
467 | $json = $this->get('serializer')->serialize($tag, 'json'); | 473 | $json = $this->get('serializer')->serialize($tag, 'json'); |
468 | 474 | ||
469 | return (new JsonResponse())->setJson($json); | 475 | return (new JsonResponse())->setJson($json); |
@@ -486,6 +492,28 @@ class WallabagRestController extends FOSRestController | |||
486 | } | 492 | } |
487 | 493 | ||
488 | /** | 494 | /** |
495 | * Remove orphan tag in case no entries are associated to it. | ||
496 | * | ||
497 | * @param Tag|array $tags | ||
498 | */ | ||
499 | private function cleanOrphanTag($tags) | ||
500 | { | ||
501 | if (!is_array($tags)) { | ||
502 | $tags = [$tags]; | ||
503 | } | ||
504 | |||
505 | $em = $this->getDoctrine()->getManager(); | ||
506 | |||
507 | foreach ($tags as $tag) { | ||
508 | if (count($tag->getEntries()) === 0) { | ||
509 | $em->remove($tag); | ||
510 | } | ||
511 | } | ||
512 | |||
513 | $em->flush(); | ||
514 | } | ||
515 | |||
516 | /** | ||
489 | * Validate that the first id is equal to the second one. | 517 | * Validate that the first id is equal to the second one. |
490 | * If not, throw exception. It means a user try to access information from an other user. | 518 | * If not, throw exception. It means a user try to access information from an other user. |
491 | * | 519 | * |
diff --git a/src/Wallabag/CoreBundle/Controller/TagController.php b/src/Wallabag/CoreBundle/Controller/TagController.php index 623a6146..c5746734 100644 --- a/src/Wallabag/CoreBundle/Controller/TagController.php +++ b/src/Wallabag/CoreBundle/Controller/TagController.php | |||
@@ -63,10 +63,12 @@ class TagController extends Controller | |||
63 | $entry->removeTag($tag); | 63 | $entry->removeTag($tag); |
64 | $em = $this->getDoctrine()->getManager(); | 64 | $em = $this->getDoctrine()->getManager(); |
65 | $em->flush(); | 65 | $em->flush(); |
66 | if (count($tag->getEntries()) == 0) { | 66 | |
67 | // remove orphan tag in case no entries are associated to it | ||
68 | if (count($tag->getEntries()) === 0) { | ||
67 | $em->remove($tag); | 69 | $em->remove($tag); |
70 | $em->flush(); | ||
68 | } | 71 | } |
69 | $em->flush(); | ||
70 | 72 | ||
71 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer')); | 73 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer')); |
72 | 74 | ||
diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml index fb97454e..a4b727f4 100644 --- a/src/Wallabag/CoreBundle/Resources/config/services.yml +++ b/src/Wallabag/CoreBundle/Resources/config/services.yml | |||
@@ -29,7 +29,7 @@ services: | |||
29 | arguments: | 29 | arguments: |
30 | - "@doctrine" | 30 | - "@doctrine" |
31 | 31 | ||
32 | wallabag_core.table_prefix_subscriber: | 32 | wallabag_core.subscriber.table_prefix: |
33 | class: Wallabag\CoreBundle\Subscriber\TablePrefixSubscriber | 33 | class: Wallabag\CoreBundle\Subscriber\TablePrefixSubscriber |
34 | arguments: | 34 | arguments: |
35 | - "%database_table_prefix%" | 35 | - "%database_table_prefix%" |