From 28db6c22ebe514aa3ea83fc1a16610ffbed8092a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Sat, 17 Jun 2023 15:19:59 +0200 Subject: [PATCH] Fix duplicate tags creation when assigning search results to tag Fixes #6330 --- src/Wallabag/CoreBundle/Controller/TagController.php | 11 ++++++++--- .../CoreBundle/Controller/TagControllerTest.php | 7 +++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Wallabag/CoreBundle/Controller/TagController.php b/src/Wallabag/CoreBundle/Controller/TagController.php index 0063ec54a..c15b6947d 100644 --- a/src/Wallabag/CoreBundle/Controller/TagController.php +++ b/src/Wallabag/CoreBundle/Controller/TagController.php @@ -247,11 +247,16 @@ class TagController extends AbstractController $filter ); - $this->entityManager->persist($entry); + // check to avoid duplicate tags creation + foreach ($this->entityManager->getUnitOfWork()->getScheduledEntityInsertions() as $entity) { + if ($entity instanceof Tag && strtolower($entity->getLabel()) === strtolower($filter)) { + continue 2; + } + $this->entityManager->persist($entry); + } + $this->entityManager->flush(); } - $this->entityManager->flush(); - return $this->redirect($this->redirectHelper->to($request->headers->get('referer'), '', true)); } diff --git a/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php b/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php index 538df700d..9c2247f7f 100644 --- a/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php @@ -560,5 +560,12 @@ class TagControllerTest extends WallabagCoreTestCase $this->assertContains('title', $tags); } + + $tag = $client->getContainer() + ->get(EntityManagerInterface::class) + ->getRepository(Tag::class) + ->findByLabelsAndUser(['title'], $this->getLoggedInUserId()); + + $this->assertCount(1, $tag); } }