Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2084: Ensure tags are unique when renaming #2086

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 14 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 All @@ -396,6 +399,17 @@ public function testRenameTagNotExists()
$this->assertEquals(['tag1', 'tag2', 'chair'], $bookmark->getTags());
}

/**
* Test renameTag() avoid duplicating existing tag
*/
public function testRenameTagNotDuplicated()
{
$bookmark = new Bookmark();
$bookmark->setTags(['tag1', 'tag2', 'chair']);
$bookmark->renameTag('tag1', 'chair');
$this->assertEquals(['tag2', 'chair'], $bookmark->getTags());
}

/**
* Test deleteTag()
*/
Expand Down
133 changes: 85 additions & 48 deletions tests/front/controller/admin/ManageTagControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
use Shaarli\TestCase;
use Slim\Http\Request;
use Slim\Http\Response;
// These are declared for the bookmark service
use malkusch\lock\mutex\NoMutex;
nodiscc marked this conversation as resolved.
Show resolved Hide resolved
use Shaarli\History;
use Shaarli\Plugin\PluginManager;
use Shaarli\Tests\Utils\FakeBookmarkService;
use Shaarli\Tests\Utils\ReferenceLinkDB;

class ManageTagControllerTest extends TestCase
{
Expand All @@ -21,11 +27,46 @@ class ManageTagControllerTest extends TestCase
/** @var ManageTagController */
protected $controller;

/** @var string Path of test data store */
protected static $testDatastore = 'sandbox/datastore.php';

/** @var BookmarkServiceInterface instance */
protected $bookmarkService;

/** @var BookmarkFilter instance */
protected $linkFilter;

/** @var ReferenceLinkDB instance */
protected static $refDB;

/** @var PluginManager */
protected static $pluginManager;

public function setUp(): void
{
$this->createContainer();

$this->controller = new ManageTagController($this->container);

$mutex = new NoMutex();
$conf = new ConfigManager('tests/utils/config/configJson');
$conf->set('resource.datastore', self::$testDatastore);
static::$pluginManager = new PluginManager($conf);
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
);
}

/**
Expand Down Expand Up @@ -80,10 +121,23 @@ public function testSaveRenameTagValid(): void
$session = [];
$this->assignSessionVars($session);

$this->assertEquals(
3,
count($this->linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'cartoon'))
);
$this->assertEquals(
4,
count($this->linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'web'))
);
$this->assertEquals(
2,
count($this->linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'cartoon web'))
);

$requestParameters = [
'renametag' => 'rename',
'fromtag' => 'old-tag',
'totag' => 'new-tag',
'fromtag' => 'cartoon',
'totag' => 'web',
];
$request = $this->createMock(Request::class);
$request
Expand All @@ -94,36 +148,33 @@ public function testSaveRenameTagValid(): void
})
;
$response = new Response();

$bookmark1 = $this->createMock(Bookmark::class);
$bookmark2 = $this->createMock(Bookmark::class);
$this->container->bookmarkService
->expects(static::once())
->method('search')
->with(['searchtags' => 'old-tag'], BookmarkFilter::$ALL, true)
->willReturnCallback(function () use ($bookmark1, $bookmark2): SearchResult {
$bookmark1->expects(static::once())->method('renameTag')->with('old-tag', 'new-tag');
$bookmark2->expects(static::once())->method('renameTag')->with('old-tag', 'new-tag');

return SearchResult::getSearchResult([$bookmark1, $bookmark2]);
})
;
$this->container->bookmarkService
->expects(static::exactly(2))
->method('set')
->withConsecutive([$bookmark1, false], [$bookmark2, false])
;
$this->container->bookmarkService->expects(static::once())->method('save');

$result = $this->controller->save($request, $response);

static::assertSame(302, $result->getStatusCode());
static::assertSame(['/subfolder/?searchtags=new-tag'], $result->getHeader('location'));
static::assertSame(['/subfolder/?searchtags=web'], $result->getHeader('location'));

static::assertArrayNotHasKey(SessionManager::KEY_ERROR_MESSAGES, $session);
static::assertArrayNotHasKey(SessionManager::KEY_WARNING_MESSAGES, $session);
static::assertArrayHasKey(SessionManager::KEY_SUCCESS_MESSAGES, $session);
static::assertSame(['The tag was renamed in 2 bookmarks.'], $session[SessionManager::KEY_SUCCESS_MESSAGES]);
static::assertSame(['The tag was renamed in 3 bookmarks.'], $session[SessionManager::KEY_SUCCESS_MESSAGES]);

$this->assertEquals(
0,
count($this->linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'cartoon'))
);
$new = $this->linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'web');
$this->assertEquals(
5,
count($new)
);

// Make sure there are no duplicate tags
foreach ($new as $bookmark) {
$tags = $bookmark->getTags();
$this->assertEquals(
count($tags),
count(array_unique($tags))
);
}
}

/**
Expand All @@ -134,10 +185,16 @@ public function testSaveDeleteTagValid(): void
$session = [];
$this->assignSessionVars($session);

$this->assertEquals(
3,
count($this->linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'cartoon'))
);

$requestParameters = [
'deletetag' => 'delete',
'fromtag' => 'old-tag',
'fromtag' => 'cartoon',
];

$request = $this->createMock(Request::class);
$request
->expects(static::atLeastOnce())
Expand All @@ -148,26 +205,6 @@ public function testSaveDeleteTagValid(): void
;
$response = new Response();

$bookmark1 = $this->createMock(Bookmark::class);
$bookmark2 = $this->createMock(Bookmark::class);
$this->container->bookmarkService
->expects(static::once())
->method('search')
->with(['searchtags' => 'old-tag'], BookmarkFilter::$ALL, true)
->willReturnCallback(function () use ($bookmark1, $bookmark2): SearchResult {
$bookmark1->expects(static::once())->method('deleteTag')->with('old-tag');
$bookmark2->expects(static::once())->method('deleteTag')->with('old-tag');

return SearchResult::getSearchResult([$bookmark1, $bookmark2]);
})
;
$this->container->bookmarkService
->expects(static::exactly(2))
->method('set')
->withConsecutive([$bookmark1, false], [$bookmark2, false])
;
$this->container->bookmarkService->expects(static::once())->method('save');

$result = $this->controller->save($request, $response);

static::assertSame(302, $result->getStatusCode());
Expand All @@ -176,7 +213,7 @@ public function testSaveDeleteTagValid(): void
static::assertArrayNotHasKey(SessionManager::KEY_ERROR_MESSAGES, $session);
static::assertArrayNotHasKey(SessionManager::KEY_WARNING_MESSAGES, $session);
static::assertArrayHasKey(SessionManager::KEY_SUCCESS_MESSAGES, $session);
static::assertSame(['The tag was removed from 2 bookmarks.'], $session[SessionManager::KEY_SUCCESS_MESSAGES]);
static::assertSame(['The tag was removed from 3 bookmarks.'], $session[SessionManager::KEY_SUCCESS_MESSAGES]);
}

/**
Expand Down