Skip to content

Commit

Permalink
fix: update the way to compare and create tags for blueprints (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
rancoud authored Jan 8, 2025
1 parent 5ae1384 commit 8e38b08
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
4 changes: 2 additions & 2 deletions app/models/TagModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ public function findTagWithSlug(string $slug): ?array
*
* @return array|null
*/
public function findTagsWithNames(array $tagsToSeek): ?array
public function findTagsWithSlugs(array $tagsToSeek): ?array
{
$sqlParts = [];
$params = [];
$i = 0;
foreach ($tagsToSeek as $tagToSeek) {
$sqlParts[] = 'name = :tag_' . $i;
$sqlParts[] = 'slug = :tag_' . $i;
$params['tag_' . $i] = $tagToSeek;
++$i;
}
Expand Down
24 changes: 13 additions & 11 deletions app/services/www/TagService.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public static function createAndFindTagsWithTextareaTags(string $textareaTagsRaw
$tagsRaw = \explode("\n", $textareaTagsRaw);

// extract non empty tags
$tagsToSeek = [];
$tagsToCreate = [];
$tagsSlugToSeek = [];
$itemsCount = 0;
foreach ($tagsRaw as $tagRaw) {
$tagRaw = Helper::trim($tagRaw);
Expand All @@ -102,33 +103,34 @@ public static function createAndFindTagsWithTextareaTags(string $textareaTagsRaw

$tagRaw = \preg_replace('/\s+/', ' ', $tagRaw);

$tagsToSeek[] = \mb_strtolower($tagRaw);
$tagsToCreate[] = \mb_strtolower($tagRaw);
$tagsSlugToSeek[] = static::slugify($tagRaw);
++$itemsCount;

if ($itemsCount > static::$maxTags) {
break;
}
}

if (empty($tagsToSeek)) {
if (empty($tagsToCreate)) {
return null;
}

// seek tag already created
$tagModel = new TagModel(Application::getDatabase());
$tagsFound = $tagModel->findTagsWithNames($tagsToSeek) ?? [];
$tagsToCreate = $tagsToSeek;
$tagsCleaned = [];
$tagsFound = $tagModel->findTagsWithSlugs($tagsSlugToSeek) ?? [];
$tagsAlreadyPresent = [];

foreach ($tagsFound as $tagFound) {
if (\in_array($tagFound['name'], $tagsToSeek, true)) {
$tagsCleaned[] = $tagFound['id'];
unset($tagsToCreate[\array_search($tagFound['name'], $tagsToCreate, true)]);
$idx = \array_search($tagFound['slug'], $tagsSlugToSeek, true);
if ($idx !== false) {
$tagsAlreadyPresent[] = $tagFound['id'];
unset($tagsToCreate[$idx], $tagsSlugToSeek[$idx]);
}
}

if (empty($tagsToCreate)) {
return \implode(',', $tagsCleaned);
return \implode(',', $tagsAlreadyPresent);
}

// create new tags
Expand All @@ -140,7 +142,7 @@ public static function createAndFindTagsWithTextareaTags(string $textareaTagsRaw
}
}

return \implode(',', \array_merge($tagsCleaned, $newTagsIDs));
return \implode(',', \array_merge($tagsAlreadyPresent, $newTagsIDs));
}

/**
Expand Down
30 changes: 30 additions & 0 deletions tests/www/IntegrationTest/Service/Tag/TagsTextareaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,39 @@ public static function dataCasesTwoTagsInTextarea(): array
];
}

public static function dataCasesCaseSensitiveTags(): array
{
return [
'4 tags in textarea - 4 tags before - no creation' => [
'tagsSQLBefore' => "INSERT INTO tags (`id`, `name`, `slug`) VALUES (1, 'Camera', 'camera'), (2, 'Line Trace', 'line-trace'), (3, 'Test-Debug', 'test-debug'), (4, 'WASD', 'wasd')",
'textarea' => <<<TEXTAREA
camera
line-trace
test debug
WASD
TEXTAREA,
'tagsIDs' => '1,2,3,4',
'tagsAfter' => [['id' => '1', 'name' => 'Camera', 'slug' => 'camera'], ['id' => '2', 'name' => 'Line Trace', 'slug' => 'line-trace'], ['id' => '3', 'name' => 'Test-Debug', 'slug' => 'test-debug'], ['id' => '4', 'name' => 'WASD', 'slug' => 'wasd']],
],
'4 tags in textarea (2 duplicate) - 0 tag before - 2 creation' => [
'tagsSQLBefore' => null,
'textarea' => <<<TEXTAREA
4.19
4-19
Third Person Movement
third-person-movement
TEXTAREA,
'tagsIDs' => '1,2',
'tagsAfter' => [['id' => '1', 'name' => '4.19', 'slug' => '4-19'], ['id' => '2', 'name' => 'third person movement', 'slug' => 'third-person-movement']],
]
];
}

/**
* @dataProvider dataCasesEmptyTextarea
* @dataProvider dataCasesOneTagInTextarea
* @dataProvider dataCasesTwoTagsInTextarea
* @dataProvider dataCasesCaseSensitiveTags
*
* @param string|null $tagsSQLBefore
* @param string $textarea
Expand All @@ -122,6 +151,7 @@ public static function dataCasesTwoTagsInTextarea(): array
#[DataProvider('dataCasesEmptyTextarea')]
#[DataProvider('dataCasesOneTagInTextarea')]
#[DataProvider('dataCasesTwoTagsInTextarea')]
#[DataProvider('dataCasesCaseSensitiveTags')]
public function testCreate(?string $tagsSQLBefore, string $textarea, ?string $tagsIDs, array $tagsAfter): void
{
static::setDatabase();
Expand Down

0 comments on commit 8e38b08

Please sign in to comment.