2019-10-19 16:00:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace OCA\Bookmarks\Tests;
|
|
|
|
|
|
|
|
use OCA\Bookmarks\Db;
|
|
|
|
use OCA\Bookmarks\Db\Bookmark;
|
2019-10-22 20:01:09 +00:00
|
|
|
use OCA\Bookmarks\Exception\UrlParseError;
|
2020-09-11 15:53:54 +00:00
|
|
|
use OCA\Bookmarks\QueryParameters;
|
2019-10-19 16:00:58 +00:00
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
|
|
|
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
|
2019-10-19 16:25:37 +00:00
|
|
|
use OCP\AppFramework\QueryException;
|
2020-10-10 15:58:36 +00:00
|
|
|
use OCP\IUserManager;
|
2019-10-26 12:10:49 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2019-10-19 16:00:58 +00:00
|
|
|
|
|
|
|
class TagMapperTest extends TestCase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Db\BookmarkMapper
|
|
|
|
*/
|
|
|
|
private $bookmarkMapper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Db\TagMapper
|
|
|
|
*/
|
|
|
|
private $tagMapper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $userId;
|
2019-10-25 15:33:27 +00:00
|
|
|
/**
|
|
|
|
* @var \stdClass
|
|
|
|
*/
|
|
|
|
protected $folderMapper;
|
2019-10-19 16:00:58 +00:00
|
|
|
|
|
|
|
|
2019-10-19 16:25:37 +00:00
|
|
|
/**
|
|
|
|
* @throws QueryException
|
|
|
|
*/
|
2019-10-19 16:00:58 +00:00
|
|
|
protected function setUp(): void {
|
|
|
|
parent::setUp();
|
2019-10-26 12:10:49 +00:00
|
|
|
|
2020-10-10 15:58:36 +00:00
|
|
|
$this->bookmarkMapper = \OC::$server->get(Db\BookmarkMapper::class);
|
|
|
|
$this->tagMapper = \OC::$server->get(Db\TagMapper::class);
|
|
|
|
$this->folderMapper = \OC::$server->get(Db\FolderMapper::class);
|
2022-06-10 13:55:15 +00:00
|
|
|
$this->treeMapper = \OC::$server->get(Db\TreeMapper::class);
|
2019-10-26 12:10:49 +00:00
|
|
|
|
2020-10-10 15:58:36 +00:00
|
|
|
$this->userManager = \OC::$server->get(IUserManager::class);
|
2019-10-26 12:10:49 +00:00
|
|
|
$this->user = 'test';
|
|
|
|
if (!$this->userManager->userExists($this->user)) {
|
|
|
|
$this->userManager->createUser($this->user, 'password');
|
|
|
|
}
|
|
|
|
$this->userId = $this->userManager->get($this->user)->getUID();
|
2019-10-19 16:00:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-19 19:58:08 +00:00
|
|
|
* @dataProvider singleBookmarksProvider
|
|
|
|
* @param array $tags
|
|
|
|
* @param Bookmark $bookmark
|
2019-10-22 14:01:40 +00:00
|
|
|
* @throws MultipleObjectsReturnedException
|
2019-10-22 20:01:09 +00:00
|
|
|
* @throws UrlParseError
|
2019-11-23 20:37:53 +00:00
|
|
|
* @throws \OCA\Bookmarks\Exception\AlreadyExistsError
|
|
|
|
* @throws \OCA\Bookmarks\Exception\UserLimitExceededError
|
2019-10-19 16:00:58 +00:00
|
|
|
*/
|
2019-10-19 19:58:08 +00:00
|
|
|
public function testAddToAndFind(array $tags, Bookmark $bookmark) {
|
|
|
|
$bookmark->setUserId($this->userId);
|
|
|
|
$bookmark = $this->bookmarkMapper->insertOrUpdate($bookmark);
|
|
|
|
$this->tagMapper->addTo($tags, $bookmark->getId());
|
2022-06-10 13:55:15 +00:00
|
|
|
$this->treeMapper->addToFolders(Db\TreeMapper::TYPE_BOOKMARK, $bookmark->getId(), [$this->folderMapper->findRootFolder($this->userId)->getId()]);
|
2019-10-19 19:58:08 +00:00
|
|
|
|
|
|
|
$actualTags = $this->tagMapper->findByBookmark($bookmark->getId());
|
2020-08-02 14:05:52 +00:00
|
|
|
foreach ($tags as $tag) {
|
2019-10-19 19:58:08 +00:00
|
|
|
$this->assertContains($tag, $actualTags);
|
2019-10-19 16:00:58 +00:00
|
|
|
}
|
2019-10-19 19:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testAddToAndFind
|
|
|
|
*/
|
|
|
|
public function testFindAll() {
|
2019-10-19 16:00:58 +00:00
|
|
|
$allTags = $this->tagMapper->findAll($this->userId);
|
|
|
|
$this->assertContains('one', $allTags);
|
|
|
|
$this->assertContains('two', $allTags);
|
|
|
|
$this->assertContains('three', $allTags);
|
|
|
|
$this->assertContains('four', $allTags);
|
|
|
|
|
2019-10-19 16:25:37 +00:00
|
|
|
$allTagsWithCount = $this->tagMapper->findAllWithCount($this->userId);
|
2020-02-08 11:26:16 +00:00
|
|
|
$this->assertContains(['name' => 'one', 'count' => 3], $allTagsWithCount);
|
|
|
|
$this->assertContains(['name' => 'two', 'count' => 2], $allTagsWithCount);
|
|
|
|
$this->assertContains(['name' => 'three', 'count' => 1], $allTagsWithCount);
|
|
|
|
$this->assertContains(['name' => 'four', 'count' => 1], $allTagsWithCount);
|
2019-10-19 16:00:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 18:30:51 +00:00
|
|
|
/**
|
|
|
|
* @depends testAddToAndFind
|
|
|
|
*/
|
|
|
|
public function testRename() {
|
|
|
|
$this->tagMapper->renameTag($this->userId, 'four', 'one');
|
|
|
|
$allTags = $this->tagMapper->findAll($this->userId);
|
|
|
|
$this->assertContains('one', $allTags);
|
|
|
|
$this->assertContains('two', $allTags);
|
|
|
|
$this->assertContains('three', $allTags);
|
|
|
|
$this->assertNotContains('four', $allTags);
|
|
|
|
|
|
|
|
$allTagsWithCount = $this->tagMapper->findAllWithCount($this->userId);
|
2020-02-08 11:26:16 +00:00
|
|
|
$this->assertContains(['name' => 'one', 'count' => 3], $allTagsWithCount);
|
|
|
|
$this->assertContains(['name' => 'two', 'count' => 2], $allTagsWithCount);
|
|
|
|
$this->assertContains(['name' => 'three', 'count' => 1], $allTagsWithCount);
|
|
|
|
$this->assertNotContains(['name' => 'four', 'count' => 1], $allTagsWithCount);
|
|
|
|
$this->assertNotContains(['name' => 'four', 'count' => 0], $allTagsWithCount);
|
2019-10-22 18:30:51 +00:00
|
|
|
}
|
|
|
|
|
2020-06-28 11:26:31 +00:00
|
|
|
/**
|
|
|
|
* @depends testAddToAndFind
|
|
|
|
*/
|
|
|
|
public function testDelete() {
|
|
|
|
$this->tagMapper->deleteTag($this->userId, 'one');
|
|
|
|
$allTags = $this->tagMapper->findAll($this->userId);
|
|
|
|
$this->assertNotContains('one', $allTags);
|
|
|
|
$this->assertContains('two', $allTags);
|
|
|
|
$this->assertContains('three', $allTags);
|
|
|
|
|
|
|
|
$allTagsWithCount = $this->tagMapper->findAllWithCount($this->userId);
|
|
|
|
$this->assertContains(['name' => 'two', 'count' => 2], $allTagsWithCount);
|
|
|
|
$this->assertContains(['name' => 'three', 'count' => 1], $allTagsWithCount);
|
|
|
|
}
|
|
|
|
|
2019-10-19 16:00:58 +00:00
|
|
|
/**
|
|
|
|
* @depends testAddToAndFind
|
2019-10-19 19:58:08 +00:00
|
|
|
* @dataProvider singleBookmarksProvider
|
|
|
|
* @param array $tags
|
|
|
|
* @param Bookmark $bookmark
|
2019-10-19 16:00:58 +00:00
|
|
|
* @throws DoesNotExistException
|
|
|
|
* @throws MultipleObjectsReturnedException
|
2019-11-23 20:37:53 +00:00
|
|
|
* @throws UrlParseError
|
2019-10-19 16:00:58 +00:00
|
|
|
*/
|
2019-10-19 19:58:08 +00:00
|
|
|
public function testRemoveAllFrom(array $tags, Bookmark $bookmark) {
|
2020-09-11 15:53:54 +00:00
|
|
|
$params = new QueryParameters();
|
|
|
|
$bookmark = $this->bookmarkMapper->findAll($this->userId, $params->setUrl($bookmark->getUrl()))[0];
|
2019-10-19 16:00:58 +00:00
|
|
|
$this->tagMapper->removeAllFrom($bookmark->getId());
|
|
|
|
$tags = $this->tagMapper->findByBookmark($bookmark->getId());
|
|
|
|
$this->assertEmpty($tags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testRemoveAllFrom
|
2019-10-19 20:25:11 +00:00
|
|
|
* @dataProvider singleBookmarksProvider
|
2019-10-19 19:58:08 +00:00
|
|
|
* @param array $tags
|
|
|
|
* @param Bookmark $bookmark
|
2019-10-19 16:00:58 +00:00
|
|
|
* @return void
|
|
|
|
* @throws DoesNotExistException
|
|
|
|
* @throws MultipleObjectsReturnedException
|
2019-11-23 20:37:53 +00:00
|
|
|
* @throws UrlParseError
|
2019-10-19 16:00:58 +00:00
|
|
|
*/
|
2019-10-19 19:58:08 +00:00
|
|
|
public function testSetOn(array $tags, Bookmark $bookmark) {
|
2020-09-11 15:53:54 +00:00
|
|
|
$params = new QueryParameters();
|
|
|
|
$bookmark = $this->bookmarkMapper->findAll($this->userId, $params->setUrl($bookmark->getUrl()))[0];
|
2019-10-19 16:00:58 +00:00
|
|
|
$newTags = ['foo', 'bar'];
|
|
|
|
$this->tagMapper->setOn($newTags, $bookmark->getId());
|
2019-10-19 19:58:08 +00:00
|
|
|
$actualTags = $this->tagMapper->findByBookmark($bookmark->getId());
|
2020-08-02 14:05:52 +00:00
|
|
|
foreach ($newTags as $tag) {
|
2019-10-19 19:58:08 +00:00
|
|
|
$this->assertContains($tag, $actualTags);
|
2019-10-19 16:00:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2019-10-19 19:58:08 +00:00
|
|
|
public function singleBookmarksProvider() {
|
|
|
|
return array_map(function ($data) {
|
|
|
|
return [$data[0], Db\Bookmark::fromArray($data[1])];
|
2022-07-11 12:58:56 +00:00
|
|
|
}, [
|
|
|
|
[['one'], ['url' => 'https://google.com/', 'title' => 'Google', 'description' => 'Search engine']],
|
|
|
|
[['two'], ['url' => 'https://nextcloud.com/', 'title' => 'Nextcloud']],
|
|
|
|
[['three', 'one'], ['url' => 'https://php.net/']],
|
|
|
|
[['two', 'four', 'one'], ['url' => 'https://de.wikipedia.org/wiki/%C3%9C']],
|
|
|
|
]);
|
2019-10-19 16:00:58 +00:00
|
|
|
}
|
|
|
|
}
|