From 8151b1b8b04315d7b43329c433a9ef52fe016787 Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Thu, 23 Feb 2023 17:34:53 +0100 Subject: [PATCH 1/3] fix(SystemTagManager): Truncate overlong tag names Signed-off-by: Marcel Klehr --- lib/private/SystemTag/SystemTagManager.php | 8 +++++--- tests/lib/SystemTag/SystemTagManagerTest.php | 16 ++++++++++++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/private/SystemTag/SystemTagManager.php b/lib/private/SystemTag/SystemTagManager.php index 4524aeaf7bce4..79c5adcf450ad 100644 --- a/lib/private/SystemTag/SystemTagManager.php +++ b/lib/private/SystemTag/SystemTagManager.php @@ -193,10 +193,12 @@ public function getTag(string $tagName, bool $userVisible, bool $userAssignable) * {@inheritdoc} */ public function createTag(string $tagName, bool $userVisible, bool $userAssignable): ISystemTag { + // Length of name column is 64 + $truncatedTagName = substr($tagName, 0, 64); $query = $this->connection->getQueryBuilder(); $query->insert(self::TAG_TABLE) ->values([ - 'name' => $query->createNamedParameter($tagName), + 'name' => $query->createNamedParameter($truncatedTagName), 'visibility' => $query->createNamedParameter($userVisible ? 1 : 0), 'editable' => $query->createNamedParameter($userAssignable ? 1 : 0), ]); @@ -205,7 +207,7 @@ public function createTag(string $tagName, bool $userVisible, bool $userAssignab $query->execute(); } catch (UniqueConstraintViolationException $e) { throw new TagAlreadyExistsException( - 'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') already exists', + 'Tag ("' . $truncatedTagName . '", '. $userVisible . ', ' . $userAssignable . ') already exists', 0, $e ); @@ -215,7 +217,7 @@ public function createTag(string $tagName, bool $userVisible, bool $userAssignab $tag = new SystemTag( (string)$tagId, - $tagName, + $truncatedTagName, $userVisible, $userAssignable ); diff --git a/tests/lib/SystemTag/SystemTagManagerTest.php b/tests/lib/SystemTag/SystemTagManagerTest.php index ef0546dbd8891..3d8d2d843c420 100644 --- a/tests/lib/SystemTag/SystemTagManagerTest.php +++ b/tests/lib/SystemTag/SystemTagManagerTest.php @@ -259,6 +259,14 @@ public function testCreateDuplicate($name, $userVisible, $userAssignable) { $this->tagManager->createTag($name, $userVisible, $userAssignable); } + public function testCreateOverlongName() { + try { + $this->tagManager->createTag('Zona circundante do Palácio Nacional da Ajuda (Jardim das Damas, Salão de Física, Torre Sineira, Paço Velho e Jardim Botânico)', true, true); + } catch (\Exception $e) { + $this->assertTrue(false, 'No exception thrown for create call'); + } + } + /** * @dataProvider oneTagMultipleFlagsProvider */ @@ -281,14 +289,14 @@ public function testGetExistingTagById() { $this->assertSameTag($tag2, $tagList[$tag2->getId()]); } - + public function testGetNonExistingTag() { $this->expectException(\OCP\SystemTag\TagNotFoundException::class); $this->tagManager->getTag('nonexist', false, false); } - + public function testGetNonExistingTagsById() { $this->expectException(\OCP\SystemTag\TagNotFoundException::class); @@ -296,7 +304,7 @@ public function testGetNonExistingTagsById() { $this->tagManager->getTagsByIds([$tag1->getId(), 100, 101]); } - + public function testGetInvalidTagIdFormat() { $this->expectException(\InvalidArgumentException::class); @@ -391,7 +399,7 @@ public function testDeleteTags() { $this->assertEmpty($this->tagManager->getAllTags()); } - + public function testDeleteNonExistingTag() { $this->expectException(\OCP\SystemTag\TagNotFoundException::class); From 889caf46328a54e474e2340a7b7f0408e855c7fe Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Fri, 24 Feb 2023 14:22:41 +0100 Subject: [PATCH 2/3] Update tests/lib/SystemTag/SystemTagManagerTest.php Co-authored-by: Joas Schilling <213943+nickvergessen@users.noreply.github.com> Signed-off-by: Marcel Klehr --- tests/lib/SystemTag/SystemTagManagerTest.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tests/lib/SystemTag/SystemTagManagerTest.php b/tests/lib/SystemTag/SystemTagManagerTest.php index 3d8d2d843c420..c1f4ce83ee566 100644 --- a/tests/lib/SystemTag/SystemTagManagerTest.php +++ b/tests/lib/SystemTag/SystemTagManagerTest.php @@ -260,11 +260,8 @@ public function testCreateDuplicate($name, $userVisible, $userAssignable) { } public function testCreateOverlongName() { - try { - $this->tagManager->createTag('Zona circundante do Palácio Nacional da Ajuda (Jardim das Damas, Salão de Física, Torre Sineira, Paço Velho e Jardim Botânico)', true, true); - } catch (\Exception $e) { - $this->assertTrue(false, 'No exception thrown for create call'); - } + $tag = $this->tagManager->createTag('Zona circundante do Palácio Nacional da Ajuda (Jardim das Damas, Salão de Física, Torre Sineira, Paço Velho e Jardim Botânico)', true, true); + $this->assertSame('Zona circundante do Palácio Nacional da Ajuda (Jardim das Damas,', $tag->getName()); } /** From 4f7d77406d71bc7af919adb20bc42ea7e861e3c9 Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Fri, 24 Feb 2023 14:40:33 +0100 Subject: [PATCH 3/3] Fix SystemTagManagerTest#createTag test Signed-off-by: Marcel Klehr --- tests/lib/SystemTag/SystemTagManagerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/lib/SystemTag/SystemTagManagerTest.php b/tests/lib/SystemTag/SystemTagManagerTest.php index c1f4ce83ee566..e261a68aaf796 100644 --- a/tests/lib/SystemTag/SystemTagManagerTest.php +++ b/tests/lib/SystemTag/SystemTagManagerTest.php @@ -261,7 +261,7 @@ public function testCreateDuplicate($name, $userVisible, $userAssignable) { public function testCreateOverlongName() { $tag = $this->tagManager->createTag('Zona circundante do Palácio Nacional da Ajuda (Jardim das Damas, Salão de Física, Torre Sineira, Paço Velho e Jardim Botânico)', true, true); - $this->assertSame('Zona circundante do Palácio Nacional da Ajuda (Jardim das Damas,', $tag->getName()); + $this->assertSame('Zona circundante do Palácio Nacional da Ajuda (Jardim das Damas', $tag->getName()); // 63 characters but 64 bytes due to "á" } /**