Skip to content

Commit

Permalink
Making slight correction to max name logic
Browse files Browse the repository at this point in the history
  • Loading branch information
snake14 committed Nov 20, 2024
1 parent b9aa22a commit 6fea022
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
7 changes: 4 additions & 3 deletions Dao/BaseDao.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Piwik\Common;
use Piwik\Date;
use Piwik\Db;
use Piwik\Plugins\TagManager\Input\Name;

abstract class BaseDao
{
Expand Down Expand Up @@ -151,9 +152,9 @@ protected function incrementNameWithNumber(string $name): string
$newName = str_replace($matches[0], '', $name);
}

// Make sure that we don't exceed the max length of 255 characters
if (strlen($newName . " ($number)") > 255) {
$newName = substr($newName, 0, 251);
// Make sure that we don't exceed the max length the name fields
if (strlen($newName . " ($number)") > Name::MAX_LENGTH) {
$newName = substr($newName, 0, (Name::MAX_LENGTH - 3) - strlen((string) $number));
}

$newName .= " ($number)";
Expand Down
14 changes: 13 additions & 1 deletion tests/Integration/Dao/TagsDaoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Piwik\Common;
use Piwik\DbHelper;
use Piwik\Plugins\TagManager\Dao\TagsDao;
use Piwik\Plugins\TagManager\Input\Name;
use Piwik\Plugins\TagManager\Model\Tag;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

Expand Down Expand Up @@ -693,7 +694,8 @@ public function testMakeCopyNameUnique(string $name, array $tags, string $expect
}

$updatedName = $this->dao->makeCopyNameUnique($idSite, $name, $idContainerVersion);
$this->assertLessThanOrEqual(255, strlen($updatedName), 'The name should not exceed the 255 characters');
$maxChars = Name::MAX_LENGTH;
$this->assertLessThanOrEqual($maxChars, strlen($updatedName), "The name should not exceed the {$maxChars} characters");
$this->assertSame($expected, $updatedName);
}

Expand Down Expand Up @@ -724,6 +726,16 @@ public function getMakeCopyNameUniqueTestData(): array
[0],
'Test tag with a really long name. Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890A (1)'
],
[
'Test tag with a really long name. Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890A (9)',
[0],
'Test tag with a really long name. Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890 (10)'
],
[
'Test tag with a really long name. Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890 (99)',
[0],
'Test tag with a really long name. Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz123456789 (100)'
],
];
}

Expand Down

0 comments on commit 6fea022

Please sign in to comment.