Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix create tag with same name multiple times #180

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions Content/Infrastructure/Doctrine/TagFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,41 @@ public function create(array $tagNames): array
// sort tags by the given names order
$excerptTags = [];
foreach ($tags as $tag) {
$excerptTags[array_search($tag->getName(), $tagNames, true)] = $tag;
$index = array_search($tag->getName(), $tagNames, true);
$excerptTags[$index] = $tag;
unset($tagNames[$index]);
}

// create tags which not exist yet
foreach ($tagNames as $key => $tagName) {
if (isset($excerptTags[$key])) {
continue;
// check if a tag with the same name was yet persisted and use that instead of create one
// this avoids a unique constraint error to create multiple tag with same name
if (\count($tagNames)) {
// we use here the unitOfWork instead of an own cache this avoid us listing for
// flush, clear or deletion events and so we don't need to invalid an cache ourselves
foreach ($this->entityManager->getUnitOfWork()->getScheduledEntityInsertions() as $object) {
if (!$object instanceof TagInterface) {
continue;
}

$index = array_search($object->getName(), $tagNames, true);

if (false === $index) {
continue;
}

$excerptTags[$index] = $object;
unset($tagNames[$index]);
}
}

// create missing tags which not exist yet
foreach ($tagNames as $index => $tagName) {
/** @var TagInterface $tag */
$tag = $this->tagRepository->createNew();
$tag->setName($tagName);

$this->entityManager->persist($tag);

$excerptTags[$key] = $tag;
$excerptTags[$index] = $tag;
}

return $excerptTags;
Expand Down
92 changes: 69 additions & 23 deletions Tests/Functional/Content/Infrastructure/Doctrine/TagFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,25 @@

namespace Sulu\Bundle\ContentBundle\Tests\Functional\Content\Infrastructure\Doctrine;

use Sulu\Bundle\ContactBundle\Entity\Contact;
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\TagFactoryInterface;
use Sulu\Bundle\TagBundle\Tag\TagInterface;
use Sulu\Bundle\TagBundle\Tag\TagRepositoryInterface;
use Sulu\Bundle\TestBundle\Testing\SuluTestCase;

class TagFactoryTest extends SuluTestCase
{
/**
* @var TagFactoryInterface
*/
private $tagFactory;

protected function setUp(): void
{
self::bootKernel();
self::purgeDatabase();
}

/**
* @param string[] $existTagNames
*/
protected function createTagFactory(array $existTagNames = []): TagFactoryInterface
{
/** @var TagRepositoryInterface $tagRepository */
$tagRepository = self::$container->get('sulu.repository.tag');

foreach ($existTagNames as $existTagName) {
$existTag = $tagRepository->createNew();
$existTag->setName($existTagName);
self::getEntityManager()->persist($existTag);
}

if (\count($existTagNames)) {
self::getEntityManager()->flush();
self::getEntityManager()->clear();
}

return self::$container->get('sulu_content.tag_factory');
$this->tagFactory = self::$container->get('sulu_content.tag_factory');
}

/**
Expand All @@ -56,19 +42,49 @@ protected function createTagFactory(array $existTagNames = []): TagFactoryInterf
*/
public function testCreate(array $tagNames, array $existTags): void
{
$tagFactory = $this->createTagFactory($existTags);
$this->createTags($existTags);

$tags = $this->tagFactory->create($tagNames);

$this->assertSame(
$tagNames,
array_map(
function (TagInterface $tag) {
return $tag->getName();
},
$tagFactory->create($tagNames)
$tags
)
);
}

public function testCreateSameTagTwice(): void
{
$tags1 = $this->tagFactory->create(['Tag 1']);
$tags2 = $this->tagFactory->create(['Tag 1']);

$this->assertSame($tags1, $tags2);

$this->getEntityManager()->flush();
}

public function testCreateSameTagTwiceWithOtherEntityInUnitOfWork(): void
{
$this->getEntityManager()->persist($this->createOtherEntity());

/** @var TagRepositoryInterface $tagRepository */
$tagRepository = self::$container->get('sulu.repository.tag');
$tag = $tagRepository->createNew();
$tag->setName('Other Tag');
$this->getEntityManager()->persist($tag);

$tags1 = $this->tagFactory->create(['Tag 1']);
$tags2 = $this->tagFactory->create(['Tag 1']);

$this->assertSame($tags1, $tags2);

$this->getEntityManager()->flush();
}

/**
* @return \Generator<mixed[]>
*/
Expand Down Expand Up @@ -100,6 +116,7 @@ public function dataProvider(): \Generator
],
[
'Exist Tag 1',
'Other Exist 3',
],
];

Expand All @@ -114,4 +131,33 @@ public function dataProvider(): \Generator
],
];
}

/**
* @param string[] $existTagNames
*/
private function createTags(array $existTagNames = []): void
{
/** @var TagRepositoryInterface $tagRepository */
$tagRepository = self::$container->get('sulu.repository.tag');

foreach ($existTagNames as $existTagName) {
$existTag = $tagRepository->createNew();
$existTag->setName($existTagName);
self::getEntityManager()->persist($existTag);
}

if (\count($existTagNames)) {
self::getEntityManager()->flush();
self::getEntityManager()->clear();
}
}

private function createOtherEntity(): object
{
$contact = new Contact();
$contact->setFirstName('Dummy');
$contact->setLastName('Entity');

return $contact;
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
"Tests/Application/bin/adminconsole sulu:build dev --env dev"
],
"bootstrap-test-environment": [
"Tests/Application/bin/adminconsole doctrine:database:create --if-not-exists --env test",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

schema update not always work when having foreign key references so I drop in the bootstrap test command the whole database and recreate it.

"Tests/Application/bin/adminconsole doctrine:database:drop --if-exists --force --env test",
"Tests/Application/bin/adminconsole doctrine:database:create --env test",
"Tests/Application/bin/adminconsole doctrine:schema:update --force --env test"
],
"lint": [
Expand Down