mirror of
https://github.com/wallabag/wallabag.git
synced 2025-01-22 06:38:08 +00:00
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
This commit is contained in:
parent
82899c0402
commit
fc031e5706
3 changed files with 69 additions and 0 deletions
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -462,6 +462,14 @@ class Entry
|
||||||
return;
|
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;
|
$this->tags[] = $tag;
|
||||||
$tag->addEntry($this);
|
$tag->addEntry($this);
|
||||||
}
|
}
|
||||||
|
|
|
@ -121,6 +121,26 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->assertSame($tag, $tags[0]);
|
$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 = [])
|
private function getUser(array $taggingRules = [])
|
||||||
{
|
{
|
||||||
$user = new User();
|
$user = new User();
|
||||||
|
|
Loading…
Reference in a new issue