-
-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathTagMapperTest.php
More file actions
267 lines (240 loc) Β· 8.74 KB
/
Copy pathTagMapperTest.php
File metadata and controls
267 lines (240 loc) Β· 8.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
namespace OCA\Bookmarks\Tests;
use OCA\Bookmarks\Db;
use OCA\Bookmarks\Db\Bookmark;
use OCA\Bookmarks\Exception\UrlParseError;
use OCA\Bookmarks\QueryParameters;
use OCA\Bookmarks\Service\FolderService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\QueryException;
use OCP\IUserManager;
use OCP\Share\IShare;
use PHPUnit\Framework\TestCase;
class TagMapperTest extends TestCase {
/**
* @var Db\BookmarkMapper
*/
private $bookmarkMapper;
/**
* @var Db\TagMapper
*/
private $tagMapper;
/**
* @var string
*/
private $userId;
/**
* @var \stdClass
*/
protected $folderMapper;
/**
* @throws QueryException
*/
protected function setUp(): void {
parent::setUp();
$this->bookmarkMapper = \OCP\Server::get(Db\BookmarkMapper::class);
$this->tagMapper = \OCP\Server::get(Db\TagMapper::class);
$this->folderMapper = \OCP\Server::get(Db\FolderMapper::class);
$this->treeMapper = \OCP\Server::get(Db\TreeMapper::class);
$this->userManager = \OCP\Server::get(IUserManager::class);
$this->user = 'test';
if (!$this->userManager->userExists($this->user)) {
$this->userManager->createUser($this->user, 'password');
}
$this->userId = $this->userManager->get($this->user)->getUID();
}
/**
* @dataProvider singleBookmarksProvider
* @param array $tags
* @param Bookmark $bookmark
* @throws MultipleObjectsReturnedException
* @throws UrlParseError
* @throws \OCA\Bookmarks\Exception\AlreadyExistsError
* @throws \OCA\Bookmarks\Exception\UserLimitExceededError
*/
public function testAddToAndFind(array $tags, Bookmark $bookmark) {
$bookmark->setUserId($this->userId);
$bookmark = $this->bookmarkMapper->insertOrUpdate($bookmark);
$this->tagMapper->addTo($tags, $bookmark->getId());
$this->treeMapper->addToFolders(Db\TreeMapper::TYPE_BOOKMARK, $bookmark->getId(), [$this->folderMapper->findRootFolder($this->userId)->getId()]);
$actualTags = $this->tagMapper->findByBookmark($bookmark->getId());
foreach ($tags as $tag) {
$this->assertContains($tag, $actualTags);
}
}
/**
* @depends testAddToAndFind
*/
public function testFindAll() {
$allTags = $this->tagMapper->findAll($this->userId);
$this->assertContains('one', $allTags);
$this->assertContains('two', $allTags);
$this->assertContains('three', $allTags);
$this->assertContains('four', $allTags);
$allTagsWithCount = $this->tagMapper->findAllWithCount($this->userId);
$this->assertTrue(in_array(['name' => 'one', 'count' => 3], $allTagsWithCount));
$this->assertTrue(in_array(['name' => 'two', 'count' => 2], $allTagsWithCount));
$this->assertTrue(in_array(['name' => 'three', 'count' => 1], $allTagsWithCount));
$this->assertTrue(in_array(['name' => 'four', 'count' => 1], $allTagsWithCount));
}
/**
* @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);
$this->assertTrue(in_array(['name' => 'one', 'count' => 3], $allTagsWithCount));
$this->assertTrue(in_array(['name' => 'two', 'count' => 2], $allTagsWithCount));
$this->assertTrue(in_array(['name' => 'three', 'count' => 1], $allTagsWithCount));
$this->assertFalse(in_array(['name' => 'four', 'count' => 1], $allTagsWithCount));
$this->assertFalse(in_array(['name' => 'four', 'count' => 0], $allTagsWithCount));
}
/**
* @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->assertTrue(in_array(['name' => 'two', 'count' => 2], $allTagsWithCount));
$this->assertTrue(in_array(['name' => 'three', 'count' => 1], $allTagsWithCount));
}
/**
* @depends testAddToAndFind
* @dataProvider singleBookmarksProvider
* @param array $tags
* @param Bookmark $bookmark
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
* @throws UrlParseError
*/
public function testRemoveAllFrom(array $tags, Bookmark $bookmark) {
$params = new QueryParameters();
$bookmark = $this->bookmarkMapper->findAll($this->userId, $params->setUrl($bookmark->getUrl()))[0];
$this->tagMapper->removeAllFrom($bookmark->getId());
$tags = $this->tagMapper->findByBookmark($bookmark->getId());
$this->assertEmpty($tags);
}
/**
* @depends testRemoveAllFrom
* @dataProvider singleBookmarksProvider
* @param array $tags
* @param Bookmark $bookmark
* @return void
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
* @throws UrlParseError
*/
public function testSetOn(array $tags, Bookmark $bookmark) {
$params = new QueryParameters();
$bookmark = $this->bookmarkMapper->findAll($this->userId, $params->setUrl($bookmark->getUrl()))[0];
$newTags = ['foo', 'bar'];
$this->tagMapper->setOn($newTags, $bookmark->getId());
$actualTags = $this->tagMapper->findByBookmark($bookmark->getId());
foreach ($newTags as $tag) {
$this->assertContains($tag, $actualTags);
}
}
/**
* Regression test for https://github.com/nextcloud/bookmarks/issues/1982:
* tags on bookmarks placed inside a subfolder of a shared folder must be
* visible to the sharee, not just tags on bookmarks directly in the shared
* folder.
*
* @throws \OCA\Bookmarks\Exception\AlreadyExistsError
* @throws \OCA\Bookmarks\Exception\UnsupportedOperation
* @throws \OCA\Bookmarks\Exception\UserLimitExceededError
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
* @throws UrlParseError
* @throws \OCP\DB\Exception
*/
public function testFindAllSeesTagsInSubfolderOfSharedFolder() {
$owner = 'tag_share_owner';
$recipient = 'tag_share_recipient';
if (!$this->userManager->userExists($owner)) {
$this->userManager->createUser($owner, 'password');
}
if (!$this->userManager->userExists($recipient)) {
$this->userManager->createUser($recipient, 'password');
}
$ownerId = $this->userManager->get($owner)->getUID();
$recipientId = $this->userManager->get($recipient)->getUID();
/** @var FolderService $folderService */
$folderService = \OCP\Server::get(FolderService::class);
$sharedFolder = new Db\Folder();
$sharedFolder->setTitle('shared-root');
$sharedFolder->setUserId($ownerId);
$this->folderMapper->insert($sharedFolder);
$this->treeMapper->move(
Db\TreeMapper::TYPE_FOLDER,
$sharedFolder->getId(),
$this->folderMapper->findRootFolder($ownerId)->getId(),
);
$subFolder = new Db\Folder();
$subFolder->setTitle('sub');
$subFolder->setUserId($ownerId);
$this->folderMapper->insert($subFolder);
$this->treeMapper->move(
Db\TreeMapper::TYPE_FOLDER,
$subFolder->getId(),
$sharedFolder->getId(),
);
$bookmark = Bookmark::fromArray([
'userId' => $ownerId,
'url' => 'https://example.org/issue-1982',
'title' => 'Nested',
'description' => '',
]);
$bookmark = $this->bookmarkMapper->insertOrUpdate($bookmark);
$nestedTag = 'nested-shared-1982';
$this->tagMapper->addTo([$nestedTag], $bookmark->getId());
$this->treeMapper->addToFolders(
Db\TreeMapper::TYPE_BOOKMARK,
$bookmark->getId(),
[$subFolder->getId()],
);
$folderService->createShare(
$sharedFolder->getId(),
$recipient,
IShare::TYPE_USER,
true,
false,
);
$recipientTags = $this->tagMapper->findAll($recipientId);
$this->assertContains(
$nestedTag,
$recipientTags,
'Tag on a bookmark inside a subfolder of a shared folder must be visible to the sharee',
);
$recipientTagsWithCount = $this->tagMapper->findAllWithCount($recipientId);
$matched = array_values(array_filter(
$recipientTagsWithCount,
static fn ($row) => $row['name'] === $nestedTag,
));
$this->assertCount(1, $matched);
$this->assertEquals(1, (int)$matched[0]['count']);
}
/**
* @return array
*/
public function singleBookmarksProvider() {
return array_map(function ($data) {
return [$data[0], Db\Bookmark::fromArray($data[1])];
}, [
[['one'], ['url' => 'https://google.com/', 'title' => 'Google', 'description' => 'Search engine']],
[['two'], ['url' => 'https://nextcloud.com/', 'title' => 'Nextcloud', 'description' => '']],
[['three', 'one'], ['url' => 'https://php.net/', 'title' => '', 'description' => '']],
[['two', 'four', 'one'], ['url' => 'https://de.wikipedia.org/wiki/%C3%9C', 'title' => '', 'description' => '']],
]);
}
}