From 7088857e0dc78569968c467a1968f0faf66dd1fa Mon Sep 17 00:00:00 2001 From: Keith Carangelo Date: Tue, 14 May 2024 11:40:17 -0400 Subject: [PATCH] Ensure tags are unique when renaming --- application/bookmark/Bookmark.php | 8 ++++++-- tests/bookmark/BookmarkTest.php | 3 +++ .../controller/admin/ManageTagControllerTest.php | 15 ++++++++++++--- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/application/bookmark/Bookmark.php b/application/bookmark/Bookmark.php index 56751e15d..6a83bef00 100644 --- a/application/bookmark/Bookmark.php +++ b/application/bookmark/Bookmark.php @@ -505,7 +505,7 @@ public function getAdditionalContentEntry(string $key, $default = null) } /** - * Rename a tag in tags list. + * Rename a tag in tags list. If the new tag already exists, merge them * * @param string $fromTag * @param string $toTag @@ -513,7 +513,11 @@ public function getAdditionalContentEntry(string $key, $default = null) public function renameTag(string $fromTag, string $toTag): void { if (($pos = array_search($fromTag, $this->tags ?? [])) !== false) { - $this->tags[$pos] = trim($toTag); + if (in_array($toTag, $this->tags ?? []) !== false) { + $this->deleteTag($fromTag); + } else { + $this->tags[$pos] = trim($toTag); + } } } diff --git a/tests/bookmark/BookmarkTest.php b/tests/bookmark/BookmarkTest.php index a6dc3cce2..41ed8aa55 100644 --- a/tests/bookmark/BookmarkTest.php +++ b/tests/bookmark/BookmarkTest.php @@ -374,6 +374,9 @@ public function testSetTags() public function testRenameTag() { $bookmark = new Bookmark(); + $bookmark->renameTag('chair', 'table'); + $this->assertEquals([], $bookmark->getTags()); + $bookmark->setTags(['tag1', 'tag2', 'chair']); $bookmark->renameTag('chair', 'table'); $this->assertEquals(['tag1', 'tag2', 'table'], $bookmark->getTags()); diff --git a/tests/front/controller/admin/ManageTagControllerTest.php b/tests/front/controller/admin/ManageTagControllerTest.php index 34a770a0c..b5c92fc17 100644 --- a/tests/front/controller/admin/ManageTagControllerTest.php +++ b/tests/front/controller/admin/ManageTagControllerTest.php @@ -13,7 +13,6 @@ use Shaarli\TestCase; use Slim\Http\Request; use Slim\Http\Response; - // These are declared for the bookmark service use malkusch\lock\mutex\NoMutex; use Shaarli\History; @@ -56,8 +55,18 @@ public function setUp(): void self::$refDB = new ReferenceLinkDB(); self::$refDB->write(self::$testDatastore); $history = new History('sandbox/history.php'); - $this->container->bookmarkService = new FakeBookmarkService($conf, static::$pluginManager, $history, $mutex, true); - $this->linkFilter = new BookmarkFilter($this->container->bookmarkService->getBookmarks(), $conf, static::$pluginManager); + $this->container->bookmarkService = new FakeBookmarkService( + $conf, + static::$pluginManager, + $history, + $mutex, + true + ); + $this->linkFilter = new BookmarkFilter( + $this->container->bookmarkService->getBookmarks(), + $conf, + static::$pluginManager + ); } /**