From 567421af5019bf5937aa2b4214b405d87a1f1f86 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Wed, 10 Feb 2016 17:41:28 +0100 Subject: [PATCH] remove tag from entry #1377 --- .../CoreBundle/Controller/TagController.php | 20 +++++++++++++ src/Wallabag/CoreBundle/Entity/Tag.php | 10 +++++++ .../CoreBundle/Repository/TagRepository.php | 16 +++++++++++ .../themes/material/Entry/entry.html.twig | 2 +- .../Tests/Controller/TagControllerTest.php | 28 ++++++++++++++++++- 5 files changed, 74 insertions(+), 2 deletions(-) diff --git a/src/Wallabag/CoreBundle/Controller/TagController.php b/src/Wallabag/CoreBundle/Controller/TagController.php index ff4d64a8..7b34939d 100644 --- a/src/Wallabag/CoreBundle/Controller/TagController.php +++ b/src/Wallabag/CoreBundle/Controller/TagController.php @@ -55,6 +55,26 @@ class TagController extends Controller )); } + /** + * Removes tag from entry. + * + * @Route("/remove-tag/{entry}/{tag}", requirements={"entry" = "\d+", "tag" = "\d+"}, name="remove_tag") + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function removeTagFromEntry(Request $request, Entry $entry, Tag $tag) + { + $entry->removeTag($tag); + $em = $this->getDoctrine()->getManager(); + $em->flush(); + if (count($tag->getEntries()) == 0) { + $em->remove($tag); + } + $em->flush(); + + return $this->redirect($request->headers->get('referer')); + } + /** * Shows tags for current user. * diff --git a/src/Wallabag/CoreBundle/Entity/Tag.php b/src/Wallabag/CoreBundle/Entity/Tag.php index c1940e99..a6e2d023 100644 --- a/src/Wallabag/CoreBundle/Entity/Tag.php +++ b/src/Wallabag/CoreBundle/Entity/Tag.php @@ -107,4 +107,14 @@ class Tag { return $this->entries->contains($entry); } + + /** + * Get entries for this tag. + * + * @return ArrayCollection + */ + public function getEntries() + { + return $this->entries; + } } diff --git a/src/Wallabag/CoreBundle/Repository/TagRepository.php b/src/Wallabag/CoreBundle/Repository/TagRepository.php index c4aeb594..8d9cf85c 100644 --- a/src/Wallabag/CoreBundle/Repository/TagRepository.php +++ b/src/Wallabag/CoreBundle/Repository/TagRepository.php @@ -51,4 +51,20 @@ class TagRepository extends EntityRepository ->getQuery() ->getResult(); } + + /** + * Used only in test case to get a tag for our entry. + * + * @return Tag + */ + public function findOnebyEntryAndLabel($entry, $label) + { + return $this->createQueryBuilder('t') + ->leftJoin('t.entries', 'e') + ->where('e.id = :entryId')->setParameter('entryId', $entry->getId()) + ->andWhere('t.label = :label')->setParameter('label', $label) + ->setMaxResults(1) + ->getQuery() + ->getSingleResult(); + } } diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entry.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entry.html.twig index 2d3fd516..87af07dd 100644 --- a/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entry.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entry.html.twig @@ -159,7 +159,7 @@ main {
{% for tag in entry.tags %}
- {{ tag.label }} + {{ tag.label }} ✘
{% endfor %}
diff --git a/src/Wallabag/CoreBundle/Tests/Controller/TagControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/TagControllerTest.php index dc93dd6b..ae29a2a6 100644 --- a/src/Wallabag/CoreBundle/Tests/Controller/TagControllerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Controller/TagControllerTest.php @@ -6,6 +6,8 @@ use Wallabag\CoreBundle\Tests\WallabagCoreTestCase; class TagControllerTest extends WallabagCoreTestCase { + public $tagName = 'opensource'; + public function testList() { $this->logInAs('admin'); @@ -31,7 +33,7 @@ class TagControllerTest extends WallabagCoreTestCase $form = $crawler->filter('button[id=tag_save]')->form(); $data = array( - 'tag[label]' => 'opensource', + 'tag[label]' => $this->tagName, ); $client->submit($form, $data); @@ -65,4 +67,28 @@ class TagControllerTest extends WallabagCoreTestCase $this->assertEquals(2, count($newEntry->getTags())); } + + public function testRemoveTagFromEntry() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $entry = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:Entry') + ->findOneByUsernameAndNotArchived('admin'); + + $tag = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:Tag') + ->findOnebyEntryAndLabel($entry, $this->tagName); + + $client->request('GET', '/remove-tag/'.$entry->getId().'/'.$tag->getId()); + + $this->assertEquals(302, $client->getResponse()->getStatusCode()); + + $client->request('GET', '/remove-tag/'.$entry->getId().'/'.$tag->getId()); + + $this->assertEquals(404, $client->getResponse()->getStatusCode()); + } } -- 2.41.0