Skip to content

Commit

Permalink
Ensure tags are unique when renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
kcaran committed May 14, 2024
1 parent 2321bcf commit 7088857
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
8 changes: 6 additions & 2 deletions application/bookmark/Bookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,15 +505,19 @@ 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
*/
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);
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions tests/bookmark/BookmarkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
15 changes: 12 additions & 3 deletions tests/front/controller/admin/ManageTagControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
);
}

/**
Expand Down

0 comments on commit 7088857

Please sign in to comment.