Skip to content

Commit 2db759f

Browse files
kcarannodiscc
authored andcommitted
Ensure tags are unique when renaming
1 parent 16880fb commit 2db759f

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

application/bookmark/Bookmark.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -505,15 +505,19 @@ public function getAdditionalContentEntry(string $key, $default = null)
505505
}
506506

507507
/**
508-
* Rename a tag in tags list.
508+
* Rename a tag in tags list. If the new tag already exists, merge them
509509
*
510510
* @param string $fromTag
511511
* @param string $toTag
512512
*/
513513
public function renameTag(string $fromTag, string $toTag): void
514514
{
515515
if (($pos = array_search($fromTag, $this->tags ?? [])) !== false) {
516-
$this->tags[$pos] = trim($toTag);
516+
if (in_array($toTag, $this->tags ?? []) !== false) {
517+
$this->deleteTag($fromTag);
518+
} else {
519+
$this->tags[$pos] = trim($toTag);
520+
}
517521
}
518522
}
519523

tests/bookmark/BookmarkTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,9 @@ public function testSetTags()
374374
public function testRenameTag()
375375
{
376376
$bookmark = new Bookmark();
377+
$bookmark->renameTag('chair', 'table');
378+
$this->assertEquals([], $bookmark->getTags());
379+
377380
$bookmark->setTags(['tag1', 'tag2', 'chair']);
378381
$bookmark->renameTag('chair', 'table');
379382
$this->assertEquals(['tag1', 'tag2', 'table'], $bookmark->getTags());

tests/front/controller/admin/ManageTagControllerTest.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Shaarli\TestCase;
1414
use Slim\Http\Request;
1515
use Slim\Http\Response;
16-
1716
// These are declared for the bookmark service
1817
use malkusch\lock\mutex\NoMutex;
1918
use Shaarli\History;
@@ -56,8 +55,18 @@ public function setUp(): void
5655
self::$refDB = new ReferenceLinkDB();
5756
self::$refDB->write(self::$testDatastore);
5857
$history = new History('sandbox/history.php');
59-
$this->container->bookmarkService = new FakeBookmarkService($conf, static::$pluginManager, $history, $mutex, true);
60-
$this->linkFilter = new BookmarkFilter($this->container->bookmarkService->getBookmarks(), $conf, static::$pluginManager);
58+
$this->container->bookmarkService = new FakeBookmarkService(
59+
$conf,
60+
static::$pluginManager,
61+
$history,
62+
$mutex,
63+
true
64+
);
65+
$this->linkFilter = new BookmarkFilter(
66+
$this->container->bookmarkService->getBookmarks(),
67+
$conf,
68+
static::$pluginManager
69+
);
6170
}
6271

6372
/**

0 commit comments

Comments
 (0)