]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
TagController: prevent tag deletion when renaming a tag with the same label
authorKevin Decherf <kevin@kdecherf.com>
Sat, 4 Apr 2020 20:08:08 +0000 (22:08 +0200)
committerKevin Decherf <kevin@kdecherf.com>
Sat, 18 Apr 2020 16:09:07 +0000 (18:09 +0200)
Signed-off-by: Kevin Decherf <kevin@kdecherf.com>
src/Wallabag/CoreBundle/Controller/TagController.php
tests/Wallabag/CoreBundle/Controller/TagControllerTest.php

index c228c27a9f8c1018bdbef3f3c03e190d1154ce8e..f7b78f5d3bea3354726ccc5ad3fc491f7dd764db 100644 (file)
@@ -151,7 +151,10 @@ class TagController extends Controller
         $form = $this->createForm(RenameTagType::class, new Tag());
         $form->handleRequest($request);
 
-        if ($form->isSubmitted() && $form->isValid()) {
+        if ($form->isSubmitted()
+            && $form->isValid()
+            && $form->get('label')->getData() !== $tag->getLabel()
+           ) {
             $newTagLabel = $form->get('label')->getData();
             $newTag = new Tag();
             $newTag->setLabel($newTagLabel);
@@ -171,12 +174,12 @@ class TagController extends Controller
 
             $em = $this->getDoctrine()->getManager();
             $em->flush();
-        }
 
-        $this->get('session')->getFlashBag()->add(
-            'notice',
-            'flashes.tag.notice.tag_renamed'
-        );
+            $this->get('session')->getFlashBag()->add(
+                'notice',
+                'flashes.tag.notice.tag_renamed'
+            );
+        }
 
         $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'), '', true);
 
index 20e60c324197de90c913437c3444e4844d95260c..80903b950b527618731a60cd717783d15bd549d7 100644 (file)
@@ -211,6 +211,10 @@ class TagControllerTest extends WallabagCoreTestCase
         $client->submit($form, $data);
         $this->assertSame(302, $client->getResponse()->getStatusCode());
 
+        $crawler = $client->followRedirect();
+
+        $this->assertContains('flashes.tag.notice.tag_renamed', $crawler->filter('body')->extract(['_text'])[0]);
+
         $freshEntry = $client->getContainer()
             ->get('doctrine.orm.entity_manager')
             ->getRepository('WallabagCoreBundle:Entry')
@@ -246,6 +250,60 @@ class TagControllerTest extends WallabagCoreTestCase
         $this->assertTrue($newTag[0]->hasEntry($freshEntry2), 'New tag is assigned to the entry2.');
     }
 
+    public function testRenameTagWithSameLabel()
+    {
+        $tagLabel = 'same label';
+        $this->logInAs('admin');
+        $client = $this->getClient();
+
+        $tag = new Tag();
+        $tag->setLabel($tagLabel);
+
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl('http://0.0.0.0/foobar');
+        $entry->addTag($tag);
+        $this->getEntityManager()->persist($entry);
+
+        $this->getEntityManager()->flush();
+        $this->getEntityManager()->clear();
+
+        // We make a first request to set an history and test redirection after tag deletion
+        $crawler = $client->request('GET', '/tag/list');
+        $form = $crawler->filter('#tag-' . $tag->getId() . ' form')->form();
+
+        $data = [
+            'tag[label]' => $tagLabel,
+        ];
+
+        $client->submit($form, $data);
+        $this->assertSame(302, $client->getResponse()->getStatusCode());
+        $this->assertNotContains('flashes.tag.notice.tag_renamed', $crawler->filter('body')->extract(['_text'])[0]);
+
+        $freshEntry = $client->getContainer()
+            ->get('doctrine.orm.entity_manager')
+            ->getRepository('WallabagCoreBundle:Entry')
+            ->find($entry->getId());
+
+        $tags = [];
+
+        $tagsFromEntry = $freshEntry->getTags()->toArray();
+        foreach ($tagsFromEntry as $key => $item) {
+            $tags[$key] = $item->getLabel();
+        }
+
+        $this->assertNotFalse(array_search($tag->getLabel(), $tags, true), 'Tag is still assigned to the entry.');
+
+        $newTag = $client->getContainer()
+            ->get('doctrine.orm.entity_manager')
+            ->getRepository('WallabagCoreBundle:Tag')
+            ->findByLabel($tagLabel);
+
+        $this->assertCount(1, $newTag);
+        $this->assertSame($tag->getId(), $newTag[0]->getId(), 'Tag is unchanged.');
+
+        $this->assertTrue($newTag[0]->hasEntry($freshEntry), 'Tag is still assigned to the entry.');
+    }
+
     public function testAddUnicodeTagLabel()
     {
         $this->logInAs('admin');