From fc031e5706acf89ff21f22ca8004ddc7f9b43089 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sun, 27 Dec 2015 22:26:49 +0100 Subject: [PATCH] Avoid multiple tag creation When a new tag is created but not yet persisted, it can be duplicated. It could happen when multiple rules match the content and at least 2 of them should attach same new tag. Fix #1528 --- .../DataFixtures/ORM/LoadTaggingRuleData.php | 41 +++++++++++++++++++ src/Wallabag/CoreBundle/Entity/Entry.php | 8 ++++ .../Tests/Helper/RuleBasedTaggerTest.php | 20 +++++++++ 3 files changed, 69 insertions(+) create mode 100644 src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTaggingRuleData.php diff --git a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTaggingRuleData.php b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTaggingRuleData.php new file mode 100644 index 00000000..09a08bb2 --- /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 608ed2f0..2813c944 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/Tests/Helper/RuleBasedTaggerTest.php b/src/Wallabag/CoreBundle/Tests/Helper/RuleBasedTaggerTest.php index 37e137bf..70951d46 100644 --- a/src/Wallabag/CoreBundle/Tests/Helper/RuleBasedTaggerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Helper/RuleBasedTaggerTest.php @@ -121,6 +121,26 @@ 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); + } + private function getUser(array $taggingRules = []) { $user = new User(); -- 2.41.0