diff --git a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTaggingRuleData.php b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTaggingRuleData.php new file mode 100644 index 000000000..09a08bb20 --- /dev/null +++ b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTaggingRuleData.php @@ -0,0 +1,41 @@ +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; + } +} diff --git a/src/Wallabag/CoreBundle/Entity/Entry.php b/src/Wallabag/CoreBundle/Entity/Entry.php index 608ed2f0d..2813c9442 100644 --- a/src/Wallabag/CoreBundle/Entity/Entry.php +++ b/src/Wallabag/CoreBundle/Entity/Entry.php @@ -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); } diff --git a/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php b/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php index fb2d1f87d..41ef25b8c 100644 --- a/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php +++ b/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php @@ -65,7 +65,6 @@ class RuleBasedTagger $tag = $this->getTag($user, $label); $entry->addTag($tag); - $entries[] = $entry; } } } diff --git a/src/Wallabag/CoreBundle/Tests/Helper/RuleBasedTaggerTest.php b/src/Wallabag/CoreBundle/Tests/Helper/RuleBasedTaggerTest.php index 37e137bfc..1de134b8a 100644 --- a/src/Wallabag/CoreBundle/Tests/Helper/RuleBasedTaggerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Helper/RuleBasedTaggerTest.php @@ -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();