]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Merge pull request #2324 from wallabag/edit-tagging-rule
authorJeremy Benoist <j0k3r@users.noreply.github.com>
Sun, 2 Oct 2016 09:05:10 +0000 (11:05 +0200)
committerGitHub <noreply@github.com>
Sun, 2 Oct 2016 09:05:10 +0000 (11:05 +0200)
Add ability to edit a tagging rule

14 files changed:
src/Wallabag/CoreBundle/Controller/ConfigController.php
src/Wallabag/CoreBundle/Resources/translations/messages.da.yml
src/Wallabag/CoreBundle/Resources/translations/messages.de.yml
src/Wallabag/CoreBundle/Resources/translations/messages.en.yml
src/Wallabag/CoreBundle/Resources/translations/messages.es.yml
src/Wallabag/CoreBundle/Resources/translations/messages.fa.yml
src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml
src/Wallabag/CoreBundle/Resources/translations/messages.it.yml
src/Wallabag/CoreBundle/Resources/translations/messages.oc.yml
src/Wallabag/CoreBundle/Resources/translations/messages.pl.yml
src/Wallabag/CoreBundle/Resources/translations/messages.ro.yml
src/Wallabag/CoreBundle/Resources/views/themes/baggy/Config/index.html.twig
src/Wallabag/CoreBundle/Resources/views/themes/material/Config/index.html.twig
tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php

index 75a9af0b5c1f6bb5259f317b84e92f99f01bfaba..f1e212d989e849da34a45afe66a7803f709a90a0 100644 (file)
@@ -108,7 +108,21 @@ class ConfigController extends Controller
 
         // handle tagging rule
         $taggingRule = new TaggingRule();
-        $newTaggingRule = $this->createForm(TaggingRuleType::class, $taggingRule, ['action' => $this->generateUrl('config').'#set5']);
+        $action = $this->generateUrl('config').'#set5';
+
+        if ($request->query->has('tagging-rule')) {
+            $taggingRule = $this->getDoctrine()
+                ->getRepository('WallabagCoreBundle:TaggingRule')
+                ->find($request->query->get('tagging-rule'));
+
+            if ($this->getUser()->getId() !== $taggingRule->getConfig()->getUser()->getId()) {
+                return $this->redirect($action);
+            }
+
+            $action = $this->generateUrl('config').'?tagging-rule='.$taggingRule->getId().'#set5';
+        }
+
+        $newTaggingRule = $this->createForm(TaggingRuleType::class, $taggingRule, ['action' => $action]);
         $newTaggingRule->handleRequest($request);
 
         if ($newTaggingRule->isValid()) {
@@ -205,9 +219,7 @@ class ConfigController extends Controller
      */
     public function deleteTaggingRuleAction(TaggingRule $rule)
     {
-        if ($this->getUser()->getId() != $rule->getConfig()->getUser()->getId()) {
-            throw $this->createAccessDeniedException('You can not access this tagging rule.');
-        }
+        $this->validateRuleAction($rule);
 
         $em = $this->getDoctrine()->getManager();
         $em->remove($rule);
@@ -221,6 +233,34 @@ class ConfigController extends Controller
         return $this->redirect($this->generateUrl('config').'#set5');
     }
 
+    /**
+     * Edit a tagging rule.
+     *
+     * @param TaggingRule $rule
+     *
+     * @Route("/tagging-rule/edit/{id}", requirements={"id" = "\d+"}, name="edit_tagging_rule")
+     *
+     * @return RedirectResponse
+     */
+    public function editTaggingRuleAction(TaggingRule $rule)
+    {
+        $this->validateRuleAction($rule);
+
+        return $this->redirect($this->generateUrl('config').'?tagging-rule='.$rule->getId().'#set5');
+    }
+
+    /**
+     * Validate that a rule can be edited/deleted by the current user.
+     *
+     * @param TaggingRule $rule
+     */
+    private function validateRuleAction(TaggingRule $rule)
+    {
+        if ($this->getUser()->getId() != $rule->getConfig()->getUser()->getId()) {
+            throw $this->createAccessDeniedException('You can not access this tagging rule.');
+        }
+    }
+
     /**
      * Retrieve config for the current user.
      * If no config were found, create a new one.
index 72374a4a0f98b031c74d0fe17037d4302ee25535..628cdbdb2dfbb9e2a3e787cc4de768eb99ceb0dc 100644 (file)
@@ -95,6 +95,7 @@ config:
         # if_label: 'if'
         # then_tag_as_label: 'then tag as'
         # delete_rule_label: 'delete'
+        # edit_rule_label: 'edit'
         # rule_label: 'Rule'
         # tags_label: 'Tags'
         # faq:
index 3013e780fd1ee57e4b95fe19c0973cc0403744b4..2e683b8c92da2fdb1e818611ad006b5293e1fb1e 100644 (file)
@@ -95,6 +95,7 @@ config:
         if_label: 'Wenn'
         then_tag_as_label: 'dann tagge als'
         delete_rule_label: 'löschen'
+        # edit_rule_label: 'edit'
         rule_label: 'Regel'
         tags_label: 'Tags'
         faq:
index 7bd85cd616809f940c8d56cfdec57063bfe095f6..d629312c13d1c6c95292b43c1681ddf62ba07fdd 100644 (file)
@@ -95,6 +95,7 @@ config:
         if_label: 'if'
         then_tag_as_label: 'then tag as'
         delete_rule_label: 'delete'
+        edit_rule_label: 'edit'
         rule_label: 'Rule'
         tags_label: 'Tags'
         faq:
index cab0f302f48ade791de351312b6dbbb9975c8a0f..860c466f0f1b9c6ce63beec9417b42249308cb3c 100644 (file)
@@ -95,6 +95,7 @@ config:
         if_label: 'si'
         then_tag_as_label: 'Etiquete como'
         delete_rule_label: 'Borre'
+        # edit_rule_label: 'edit'
         rule_label: 'Regla'
         tags_label: 'Etiquetas'
         faq:
index 202f9d2dd623ef274c99aaa0b077686d89379edf..6e02010c59f27ea88b6a34f86b50cbfa2f9b1783 100644 (file)
@@ -95,6 +95,7 @@ config:
         if_label: 'اگر'
         then_tag_as_label: 'این برچسب را بزن'
         delete_rule_label: 'پاک کن'
+        # edit_rule_label: 'edit'
         rule_label: 'قانون'
         tags_label: 'برچسب‌ها'
         faq:
index 591b598b2b896a99275953a357f4b984900a9760..0eeda011fae48c4b321a2901a231debfdef4625a 100644 (file)
@@ -95,6 +95,7 @@ config:
         if_label: 'si'
         then_tag_as_label: 'alors attribuer les tags'
         delete_rule_label: 'supprimer'
+        edit_rule_label: 'éditer'
         rule_label: 'Règle'
         tags_label: 'Tags'
         faq:
index b767e5803d99eb893b6c57cc22a77e592447fdf0..34f660f74c584364f1f40f7bd6f0aa8bff12bc5b 100644 (file)
@@ -95,6 +95,7 @@ config:
         if_label: 'se'
         then_tag_as_label: 'allora tagga come'
         delete_rule_label: 'elimina'
+        # edit_rule_label: 'edit'
         rule_label: 'Regola'
         tags_label: 'Tag'
         faq:
index 39eac6427effae1c9788035cfcbcc2cb78a81d5d..bcc59b29127f185549ab08e356febe842685605e 100644 (file)
@@ -95,6 +95,7 @@ config:
         if_label: 'se'
         then_tag_as_label: 'alara atribuir las etiquetas'
         delete_rule_label: 'suprimir'
+        # edit_rule_label: 'edit'
         rule_label: 'Règla'
         tags_label: 'Etiquetas'
         faq:
index 50da9ff2d31cdf7c2e69b13d16fb1975b3dd4e5b..ce5ebab9eca238fe300754c7ad2638950ce2f2bd 100644 (file)
@@ -95,6 +95,7 @@ config:
         if_label: 'jeżeli'
         then_tag_as_label: 'wtedy otaguj jako'
         delete_rule_label: 'usuń'
+        # edit_rule_label: 'edit'
         rule_label: 'Reguła'
         tags_label: 'Tagi'
         faq:
index 315d6aa0cc3c40660537a99e7b4769183e92b0bb..a54efb4ceef14d27263b282cf357d35c39345e11 100644 (file)
@@ -95,6 +95,7 @@ config:
         # if_label: 'if'
         # then_tag_as_label: 'then tag as'
         # delete_rule_label: 'delete'
+        # edit_rule_label: 'edit'
         # rule_label: 'Rule'
         # tags_label: 'Tags'
         # faq:
index 6446cf2c33823d2f5fd724fa454bcb7b5dbaa37a..dd4f7b009e941cc8940a6ddbee7c1c51320ca8c5 100644 (file)
             « {{ tagging_rule.rule }} »
             {{ 'config.form_rules.then_tag_as_label'|trans }}
             « {{ tagging_rule.tags|join(', ') }} »
+            <a href="{{ path('edit_tagging_rule', {id: tagging_rule.id}) }}" title="{{ 'config.form_rules.edit_rule_label'|trans }}" class="tool mode_edit">✎</a>
             <a href="{{ path('delete_tagging_rule', {id: tagging_rule.id}) }}" title="{{ 'config.form_rules.delete_rule_label'|trans }}" class="tool delete icon-trash icon"></a>
         </li>
         {% endfor %}
index 5330c35392d92f3dc7c442141e09e88b95201fcf..650a3ae2a03ce061ea148e318724799934ff4e38 100644 (file)
                                         « {{ tagging_rule.rule }} »
                                         {{ 'config.form_rules.then_tag_as_label'|trans }}
                                         « {{ tagging_rule.tags|join(', ') }} »
+                                        <a href="{{ path('edit_tagging_rule', {id: tagging_rule.id}) }}" title="{{ 'config.form_rules.edit_rule_label'|trans }}">
+                                            <i class="tool grey-text mode_edit material-icons">mode_edit</i>
+                                        </a>
                                         <a href="{{ path('delete_tagging_rule', {id: tagging_rule.id}) }}" title="{{ 'config.form_rules.delete_rule_label'|trans }}">
                                             <i class="tool grey-text delete material-icons">delete</i>
                                         </a>
index 7193f9b0e4331976f2768557425af73ec36faa2a..bb3ea9e2cfea3a00a88dab2ed18ba5fb6e26343f 100644 (file)
@@ -56,8 +56,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
 
         $crawler = $client->followRedirect();
 
-        $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(['_text']));
-        $this->assertContains('flashes.config.notice.config_saved', $alert[0]);
+        $this->assertContains('flashes.config.notice.config_saved', $crawler->filter('body')->extract(['_text'])[0]);
     }
 
     public function testChangeReadingSpeed()
@@ -213,8 +212,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
 
         $crawler = $client->followRedirect();
 
-        $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(['_text']));
-        $this->assertContains('flashes.config.notice.password_updated', $alert[0]);
+        $this->assertContains('flashes.config.notice.password_updated', $crawler->filter('body')->extract(['_text'])[0]);
     }
 
     public function dataForUserFailed()
@@ -382,8 +380,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
 
         $crawler = $client->followRedirect();
 
-        $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(['_text']));
-        $this->assertContains('flashes.config.notice.user_added', $alert[0]);
+        $this->assertContains('flashes.config.notice.user_added', $crawler->filter('body')->extract(['_text'])[0]);
 
         $em = $client->getContainer()->get('doctrine.orm.entity_manager');
         $user = $em
@@ -474,8 +471,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
 
         $crawler = $client->followRedirect();
 
-        $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(['_text']));
-        $this->assertContains('flashes.config.notice.rss_updated', $alert[0]);
+        $this->assertContains('flashes.config.notice.rss_updated', $crawler->filter('body')->extract(['_text'])[0]);
     }
 
     public function dataForRssFailed()
@@ -540,8 +536,32 @@ class ConfigControllerTest extends WallabagCoreTestCase
 
         $crawler = $client->followRedirect();
 
-        $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(['_text']));
-        $this->assertContains('flashes.config.notice.tagging_rules_updated', $alert[0]);
+        $this->assertContains('flashes.config.notice.tagging_rules_updated', $crawler->filter('body')->extract(['_text'])[0]);
+
+        $editLink = $crawler->filter('.mode_edit')->last()->link();
+
+        $crawler = $client->click($editLink);
+        $this->assertEquals(302, $client->getResponse()->getStatusCode());
+        $this->assertContains('?tagging-rule=', $client->getResponse()->headers->get('location'));
+
+        $crawler = $client->followRedirect();
+
+        $form = $crawler->filter('button[id=tagging_rule_save]')->form();
+
+        $data = [
+            'tagging_rule[rule]' => 'readingTime <= 30',
+            'tagging_rule[tags]' => 'short reading',
+        ];
+
+        $client->submit($form, $data);
+
+        $this->assertEquals(302, $client->getResponse()->getStatusCode());
+
+        $crawler = $client->followRedirect();
+
+        $this->assertContains('flashes.config.notice.tagging_rules_updated', $crawler->filter('body')->extract(['_text'])[0]);
+
+        $this->assertContains('readingTime <= 30', $crawler->filter('body')->extract(['_text'])[0]);
 
         $deleteLink = $crawler->filter('.delete')->last()->link();
 
@@ -549,8 +569,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
         $this->assertEquals(302, $client->getResponse()->getStatusCode());
 
         $crawler = $client->followRedirect();
-        $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(['_text']));
-        $this->assertContains('flashes.config.notice.tagging_rules_deleted', $alert[0]);
+        $this->assertContains('flashes.config.notice.tagging_rules_deleted', $crawler->filter('body')->extract(['_text'])[0]);
     }
 
     public function dataForTaggingRuleFailed()
@@ -613,7 +632,23 @@ class ConfigControllerTest extends WallabagCoreTestCase
             ->getRepository('WallabagCoreBundle:TaggingRule')
             ->findAll()[0];
 
-        $crawler = $client->request('GET', '/tagging-rule/delete/'.$rule->getId());
+        $crawler = $client->request('GET', '/tagging-rule/edit/'.$rule->getId());
+
+        $this->assertEquals(403, $client->getResponse()->getStatusCode());
+        $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
+        $this->assertContains('You can not access this tagging rule', $body[0]);
+    }
+
+    public function testEditingTaggingRuleFromAnOtherUser()
+    {
+        $this->logInAs('bob');
+        $client = $this->getClient();
+
+        $rule = $client->getContainer()->get('doctrine.orm.entity_manager')
+            ->getRepository('WallabagCoreBundle:TaggingRule')
+            ->findAll()[0];
+
+        $crawler = $client->request('GET', '/tagging-rule/edit/'.$rule->getId());
 
         $this->assertEquals(403, $client->getResponse()->getStatusCode());
         $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));