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

Match tag names case-insensitively #776

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions app/Models/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,30 @@ public static function nameHasChanged($tagId, string $newName): bool
return $oldName !== $newName;
}

/**
* Override inherited function such that it finds existing tags case-insensitively, but creates them with the right casing
*
* @param array $attributes
* @param array $values
* @return Builder
*/
public static function firstOrCreate(array $attributes = [], array $values = []): self
{
if (isset($attributes['name'])) {
// SQL lower() function is checked to be supported by MariaDB, SQLite, PostgreSQL, and MSSQL
$existing = self::whereRaw('lower(name) = lower(?)', [$attributes['name']])->first();
if (!is_null($existing)) {
return $existing;
}
}

if (!is_null($instance = self::where($attributes)->first())) {
return $instance;
}

return self::createOrFirst($attributes, $values);
}

/*
| ========================================================================
| SCOPES
Expand Down
37 changes: 37 additions & 0 deletions tests/Models/LinkCreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,41 @@ public function testValidLinkCreation(): void

$this->assertDatabaseHas('links', $assertedData);
}

public function testCaseInsensitiveTagAssignation(): void
{
$this->be($this->user);

$tag = 'CERN';
$tagVariant = ucfirst(strtolower($tag));

$this->assertNotEquals($tag, $tagVariant);

$link1Data = [
'title' => 'Link1Name',
'url' => 'https://cern.int/science/physics/antimatter',
'tags' => $tag,
];
$link2Data = [
'title' => 'Link2Name',
'url' => 'https://cern.int/science/accelerators/large-hadron-collider/safety-lhc',
'tags' => $tagVariant,
];

$link1 = LinkRepository::create($link1Data);
$link2 = LinkRepository::create($link2Data);

// Ensure that the casing in $tag is preserved
$this->assertEquals($link1->tags->first()->name, $tag);

// Ensure that $tag was identified as a duplicate of $tagVariant and used instead of the variant
$this->assertEquals($link2->tags->first()->name, $tag);
$this->assertEquals($link1->tags->first()->id, $link2->tags->first()->id);

// Ensure that only the correct variant exists
$negativeTagData = [
'name' => $tagVariant,
];
$this->assertDatabaseMissing('tags', $negativeTagData);
}
}