]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Merge pull request #1542 from wallabag/v2-avoid-duplicate-tag
authorNicolas Lœuillet <nicolas@loeuillet.org>
Mon, 28 Dec 2015 12:37:13 +0000 (13:37 +0100)
committerNicolas Lœuillet <nicolas@loeuillet.org>
Mon, 28 Dec 2015 12:37:13 +0000 (13:37 +0100)
v2 –  Avoid multiple tag creation

src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTaggingRuleData.php [new file with mode: 0644]
src/Wallabag/CoreBundle/Entity/Entry.php
src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php
src/Wallabag/CoreBundle/Tests/Helper/RuleBasedTaggerTest.php

diff --git a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTaggingRuleData.php b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTaggingRuleData.php
new file mode 100644 (file)
index 0000000..09a08bb
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+
+namespace Wallabag\CoreBundle\DataFixtures\ORM;
+
+use Doctrine\Common\DataFixtures\AbstractFixture;
+use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
+use Doctrine\Common\Persistence\ObjectManager;
+use Wallabag\CoreBundle\Entity\TaggingRule;
+
+class LoadTaggingRuleData extends AbstractFixture implements OrderedFixtureInterface
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function load(ObjectManager $manager)
+    {
+        $tr1 = new TaggingRule();
+        $tr1->setRule('content matches "spurs"');
+        $tr1->setTags(array('sport'));
+        $tr1->setConfig($this->getReference('admin-config'));
+
+        $manager->persist($tr1);
+
+        $tr2 = new TaggingRule();
+        $tr2->setRule('content matches "basket"');
+        $tr2->setTags(array('sport'));
+        $tr2->setConfig($this->getReference('admin-config'));
+
+        $manager->persist($tr2);
+
+        $manager->flush();
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getOrder()
+    {
+        return 40;
+    }
+}
index 608ed2f0d4ababf41230640cfb45804b137cf687..2813c94420c8a55d9523c18e5f2b0f3715f7cb4f 100644 (file)
@@ -462,6 +462,14 @@ class Entry
             return;
         }
 
+        // check if tag already exist but has not yet be persisted
+        // it seems that the previous condition with `contains()` doesn't check that case
+        foreach ($this->tags as $existingTag) {
+            if ($existingTag->getUser() !== $tag->getUser() || $existingTag->getLabel() === $tag->getLabel()) {
+                return;
+            }
+        }
+
         $this->tags[] = $tag;
         $tag->addEntry($this);
     }
index fb2d1f87d584bb8271f740ecedec49b28116e147..41ef25b8ce85f3e5df17088ef29601dfa45e3752 100644 (file)
@@ -65,7 +65,6 @@ class RuleBasedTagger
                     $tag = $this->getTag($user, $label);
 
                     $entry->addTag($tag);
-                    $entries[] = $entry;
                 }
             }
         }
index 37e137bfce42227bfc066a4586cfb8a2f06025e8..1de134b8a4bee5d0dc05101c54ae83acdda95d71 100644 (file)
@@ -121,6 +121,52 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase
         $this->assertSame($tag, $tags[0]);
     }
 
+    public function testSameTagWithDifferentfMatchingRules()
+    {
+        $taggingRule = $this->getTaggingRule('bla bla', array('hey'));
+        $otherTaggingRule = $this->getTaggingRule('rule as string', array('hey'));
+
+        $user = $this->getUser([$taggingRule, $otherTaggingRule]);
+        $entry = new Entry($user);
+
+        $this->rulerz
+            ->method('satisfies')
+            ->willReturn(true);
+
+        $this->tagger->tag($entry);
+
+        $this->assertFalse($entry->getTags()->isEmpty());
+
+        $tags = $entry->getTags();
+        $this->assertCount(1, $tags);
+    }
+
+    public function testTagAllEntriesForAUser()
+    {
+        $taggingRule = $this->getTaggingRule('bla bla', array('hey'));
+
+        $user = $this->getUser([$taggingRule]);
+
+        $this->rulerz
+            ->method('satisfies')
+            ->willReturn(true);
+
+        $this->rulerz
+            ->method('filter')
+            ->willReturn(array(new Entry($user), new Entry($user)));
+
+        $entries = $this->tagger->tagAllForUser($user);
+
+        $this->assertCount(2, $entries);
+
+        foreach ($entries as $entry) {
+            $tags = $entry->getTags();
+
+            $this->assertCount(1, $tags);
+            $this->assertEquals('hey', $tags[0]->getLabel());
+        }
+    }
+
     private function getUser(array $taggingRules = [])
     {
         $user = new User();