Skip to content

Commit 70a68e8

Browse files
Merge pull request #36837 from nextcloud/fix/truncate-overlong-tagnames
fix(SystemTagManager): Truncate overlong tag names
2 parents 15f660f + 4f7d774 commit 70a68e8

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

lib/private/SystemTag/SystemTagManager.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,12 @@ public function getTag(string $tagName, bool $userVisible, bool $userAssignable)
193193
* {@inheritdoc}
194194
*/
195195
public function createTag(string $tagName, bool $userVisible, bool $userAssignable): ISystemTag {
196+
// Length of name column is 64
197+
$truncatedTagName = substr($tagName, 0, 64);
196198
$query = $this->connection->getQueryBuilder();
197199
$query->insert(self::TAG_TABLE)
198200
->values([
199-
'name' => $query->createNamedParameter($tagName),
201+
'name' => $query->createNamedParameter($truncatedTagName),
200202
'visibility' => $query->createNamedParameter($userVisible ? 1 : 0),
201203
'editable' => $query->createNamedParameter($userAssignable ? 1 : 0),
202204
]);
@@ -205,7 +207,7 @@ public function createTag(string $tagName, bool $userVisible, bool $userAssignab
205207
$query->execute();
206208
} catch (UniqueConstraintViolationException $e) {
207209
throw new TagAlreadyExistsException(
208-
'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') already exists',
210+
'Tag ("' . $truncatedTagName . '", '. $userVisible . ', ' . $userAssignable . ') already exists',
209211
0,
210212
$e
211213
);
@@ -215,7 +217,7 @@ public function createTag(string $tagName, bool $userVisible, bool $userAssignab
215217

216218
$tag = new SystemTag(
217219
(string)$tagId,
218-
$tagName,
220+
$truncatedTagName,
219221
$userVisible,
220222
$userAssignable
221223
);

tests/lib/SystemTag/SystemTagManagerTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ public function testCreateDuplicate($name, $userVisible, $userAssignable) {
259259
$this->tagManager->createTag($name, $userVisible, $userAssignable);
260260
}
261261

262+
public function testCreateOverlongName() {
263+
$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);
264+
$this->assertSame('Zona circundante do Palácio Nacional da Ajuda (Jardim das Damas', $tag->getName()); // 63 characters but 64 bytes due to "á"
265+
}
266+
262267
/**
263268
* @dataProvider oneTagMultipleFlagsProvider
264269
*/
@@ -281,22 +286,22 @@ public function testGetExistingTagById() {
281286
$this->assertSameTag($tag2, $tagList[$tag2->getId()]);
282287
}
283288

284-
289+
285290
public function testGetNonExistingTag() {
286291
$this->expectException(\OCP\SystemTag\TagNotFoundException::class);
287292

288293
$this->tagManager->getTag('nonexist', false, false);
289294
}
290295

291-
296+
292297
public function testGetNonExistingTagsById() {
293298
$this->expectException(\OCP\SystemTag\TagNotFoundException::class);
294299

295300
$tag1 = $this->tagManager->createTag('one', true, false);
296301
$this->tagManager->getTagsByIds([$tag1->getId(), 100, 101]);
297302
}
298303

299-
304+
300305
public function testGetInvalidTagIdFormat() {
301306
$this->expectException(\InvalidArgumentException::class);
302307

@@ -391,7 +396,7 @@ public function testDeleteTags() {
391396
$this->assertEmpty($this->tagManager->getAllTags());
392397
}
393398

394-
399+
395400
public function testDeleteNonExistingTag() {
396401
$this->expectException(\OCP\SystemTag\TagNotFoundException::class);
397402

0 commit comments

Comments
 (0)