-
-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathBookmarkMapperTest.php
More file actions
403 lines (358 loc) Β· 15 KB
/
Copy pathBookmarkMapperTest.php
File metadata and controls
403 lines (358 loc) Β· 15 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<?php
namespace OCA\Bookmarks\Tests;
use OCA\Bookmarks\Db;
use OCA\Bookmarks\Exception\AlreadyExistsError;
use OCA\Bookmarks\Exception\UrlParseError;
use OCA\Bookmarks\Exception\UserLimitExceededError;
use OCA\Bookmarks\Service\FolderService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Entity;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\IUserManager;
use OCP\Share\IShare;
class BookmarkMapperTest extends TestCase {
/**
* @var mixed|Db\BookmarkMapper
*/
private $bookmarkMapper;
/**
* @var Db\TreeMapper
*/
private $treeMapper;
/**
* @var Db\FolderMapper
*/
private $folderMapper;
/**
* @var string
*/
private $userId;
/**
* @var \OC\User\Manager
*/
private $userManager;
/**
* @var string
*/
private $user;
/**
* @throws \OCP\AppFramework\QueryException
*/
protected function setUp(): void {
parent::setUp();
$this->cleanUp();
/**
* @var Db\BookmarkMapper
*/
$this->bookmarkMapper = \OCP\Server::get(Db\BookmarkMapper::class);
$this->treeMapper = \OCP\Server::get(Db\TreeMapper::class);
$this->folderMapper = \OCP\Server::get(Db\FolderMapper::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 Entity $bookmark
* @return void
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
* @throws AlreadyExistsError
* @throws UrlParseError
* @throws UserLimitExceededError
*/
public function testInsertAndFind(Entity $bookmark) {
$bookmark->setUserId($this->userId);
$bookmark = $this->bookmarkMapper->insert($bookmark);
$foundEntity = $this->bookmarkMapper->find($bookmark->getId());
$this->assertSame($bookmark->getUrl(), $foundEntity->getUrl());
$this->assertSame((string)$bookmark->getTitle(), (string)$foundEntity->getTitle());
$this->assertSame((string)$bookmark->getDescription(), (string)$foundEntity->getDescription());
}
/**
* @depends testInsertAndFind
* @dataProvider singleBookmarksProvider
* @param Entity $bookmark
* @return void
* @throws AlreadyExistsError
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
* @throws UrlParseError
* @throws UserLimitExceededError
*/
public function testUpdate(Entity $bookmark) {
$bookmark->setUserId($this->userId);
$bookmark = $this->bookmarkMapper->insert($bookmark);
$entity = $this->bookmarkMapper->find($bookmark->getId());
$entity->setTitle('foobar');
$this->bookmarkMapper->update($entity);
$foundEntity = $this->bookmarkMapper->find($entity->getId());
$this->assertSame((string)$entity->getTitle(), (string)$foundEntity->getTitle());
}
/**
* @depends testInsertAndFind
* @dataProvider singleBookmarksProvider
* @param Entity $bookmark
* @return void
* @throws AlreadyExistsError
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
* @throws UrlParseError
* @throws UserLimitExceededError
*/
public function testDelete(Entity $bookmark) {
$bookmark->setUserId($this->userId);
$bookmark = $this->bookmarkMapper->insert($bookmark);
$foundEntity = $this->bookmarkMapper->find($bookmark->getId());
$this->bookmarkMapper->delete($foundEntity);
$this->expectException(DoesNotExistException::class);
$this->bookmarkMapper->find($foundEntity->getId());
}
/**
* Creates an owner and a recipient user, gives the owner a folder (mounted under
* their root) that contains one subfolder, and shares that folder with the
* recipient. The recipient therefore only reaches the subfolder *through* the
* share β exactly the case the count*() methods used to miss because they queried
* the raw bookmarks_tree table instead of the recursive folder_tree CTE.
*
* @param string $suffix unique suffix so each test gets its own users/folders
* @return array{0: string, 1: string, 2: int, 3: int} [ownerId, recipientId, sharedFolderId, subFolderId]
*/
private function createSharedFolderWithSubfolder(string $suffix): array {
$owner = 'count_share_owner_' . $suffix;
$recipient = 'count_share_recipient_' . $suffix;
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();
$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());
/** @var FolderService $folderService */
$folderService = \OCP\Server::get(FolderService::class);
$folderService->createShare(
$sharedFolder->getId(),
$recipient,
IShare::TYPE_USER,
true,
false,
);
return [$ownerId, $recipientId, $sharedFolder->getId(), $subFolder->getId()];
}
/**
* Regression test: countDuplicated() must count a bookmark that is duplicated
* across two subfolders of a *shared* folder, for the sharee. The previous
* implementation queried the raw bookmarks_tree table (owner's bookmarks +
* bookmarks directly inside a shared folder) and therefore under-counted: it
* never saw duplicates living in subfolders of shared folders. The count must
* match what the "Duplicated" list (findAll + setDuplicated) actually shows.
*
* @throws \OCA\Bookmarks\Exception\AlreadyExistsError
* @throws \OCA\Bookmarks\Exception\UserLimitExceededError
* @throws UrlParseError
* @throws MultipleObjectsReturnedException
*/
public function testCountDuplicatedInSubfolderOfSharedFolder() {
[$ownerId, $recipientId, $sharedFolderId, $subFolderA] = $this->createSharedFolderWithSubfolder('dup');
// A second subfolder, so the bookmark can live in two distinct places.
$subFolderB = new Db\Folder();
$subFolderB->setTitle('sub-b');
$subFolderB->setUserId($ownerId);
$this->folderMapper->insert($subFolderB);
$this->treeMapper->move(Db\TreeMapper::TYPE_FOLDER, $subFolderB->getId(), $sharedFolderId);
// A single bookmark placed in BOTH subfolders -> it is duplicated.
$bookmark = Db\Bookmark::fromArray([
'userId' => $ownerId,
'url' => 'https://example.org/duplicated-in-shared-subfolders',
'title' => 'Nested duplicate',
'description' => '',
]);
$bookmark = $this->bookmarkMapper->insertOrUpdate($bookmark);
$this->treeMapper->addToFolders(
Db\TreeMapper::TYPE_BOOKMARK,
$bookmark->getId(),
[$subFolderA, $subFolderB->getId()],
);
// The owner reaches both subfolders directly -> sees 1 duplicate.
$this->assertSame(1, $this->bookmarkMapper->countDuplicated($ownerId));
// The sharee reaches both subfolders through the share -> must also see 1
// duplicate. This is the case the old implementation missed (returned 0).
$this->assertSame(1, $this->bookmarkMapper->countDuplicated($recipientId));
// And the count must agree with the actual "Duplicated" list.
$params = new \OCA\Bookmarks\QueryParameters();
$duplicatedList = $this->bookmarkMapper->findAll($recipientId, $params->setDuplicated(true));
$this->assertCount(1, $duplicatedList);
}
/**
* A bookmark that lives in two folders but whose second copy has been trashed is
* NOT a duplicate: only live copies count. countDuplicated() and the "Duplicated"
* list (findAll + setDuplicated) must both ignore the trashed copy and agree.
*
* @throws AlreadyExistsError
* @throws UserLimitExceededError
* @throws UrlParseError
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
*/
public function testCountDuplicatedIgnoresTrashedCopies() {
$rootFolderId = $this->folderMapper->findRootFolder($this->userId)->getId();
$folderA = new Db\Folder();
$folderA->setTitle('dup-a');
$folderA->setUserId($this->userId);
$this->folderMapper->insert($folderA);
$this->treeMapper->move(Db\TreeMapper::TYPE_FOLDER, $folderA->getId(), $rootFolderId);
$folderB = new Db\Folder();
$folderB->setTitle('dup-b');
$folderB->setUserId($this->userId);
$this->folderMapper->insert($folderB);
$this->treeMapper->move(Db\TreeMapper::TYPE_FOLDER, $folderB->getId(), $rootFolderId);
// One bookmark placed in both folders -> duplicated while both copies are live.
$bookmark = Db\Bookmark::fromArray([
'userId' => $this->userId,
'url' => 'https://example.org/duplicated-then-trashed',
'title' => 'Trashed duplicate',
'description' => '',
]);
$bookmark = $this->bookmarkMapper->insertOrUpdate($bookmark);
$this->treeMapper->addToFolders(
Db\TreeMapper::TYPE_BOOKMARK,
$bookmark->getId(),
[$folderA->getId(), $folderB->getId()],
);
$this->assertSame(1, $this->bookmarkMapper->countDuplicated($this->userId));
// Trash the copy in folder B -> only one live copy remains -> not a duplicate.
$this->treeMapper->softDeleteEntry(Db\TreeMapper::TYPE_BOOKMARK, $bookmark->getId(), $folderB->getId());
$this->assertSame(0, $this->bookmarkMapper->countDuplicated($this->userId));
$params = new \OCA\Bookmarks\QueryParameters();
$duplicatedList = $this->bookmarkMapper->findAll($this->userId, $params->setDuplicated(true));
$this->assertCount(0, $duplicatedList);
}
/**
* Regression test: countUnavailable() must count an unavailable bookmark living
* in a subfolder of a shared folder, for the sharee. The old implementation only
* looked at bookmarks owned by the user or *directly* in a shared folder, so it
* missed the nested case. The count must match the "Unavailable" list.
*
* @throws \OCA\Bookmarks\Exception\AlreadyExistsError
* @throws \OCA\Bookmarks\Exception\UserLimitExceededError
* @throws UrlParseError
* @throws MultipleObjectsReturnedException
*/
public function testCountUnavailableInSubfolderOfSharedFolder() {
[$ownerId, $recipientId, , $subFolderId] = $this->createSharedFolderWithSubfolder('unavail');
$bookmark = Db\Bookmark::fromArray([
'userId' => $ownerId,
'url' => 'https://example.org/unavailable-in-shared-subfolder',
'title' => 'Nested unavailable',
'description' => '',
]);
$bookmark = $this->bookmarkMapper->insertOrUpdate($bookmark);
$bookmark->setAvailable(false);
$this->bookmarkMapper->update($bookmark);
$this->treeMapper->addToFolders(Db\TreeMapper::TYPE_BOOKMARK, $bookmark->getId(), [$subFolderId]);
// Owner reaches the subfolder directly; sharee reaches it through the share.
// The old implementation missed the nested case for the sharee (returned 0).
$this->assertSame(1, $this->bookmarkMapper->countUnavailable($ownerId));
$this->assertSame(1, $this->bookmarkMapper->countUnavailable($recipientId));
// And the count must agree with the actual "Unavailable" list.
$params = new \OCA\Bookmarks\QueryParameters();
$unavailableList = $this->bookmarkMapper->findAll($recipientId, $params->setUnavailable(true));
$this->assertCount(1, $unavailableList);
}
/**
* Regression test: countWithClicks() must count a clicked bookmark living in a
* subfolder of a shared folder, for the sharee. The old implementation missed the
* nested case (returned 0 for the sharee).
*
* @throws \OCA\Bookmarks\Exception\AlreadyExistsError
* @throws \OCA\Bookmarks\Exception\UserLimitExceededError
* @throws UrlParseError
* @throws MultipleObjectsReturnedException
*/
public function testCountWithClicksInSubfolderOfSharedFolder() {
[$ownerId, $recipientId, , $subFolderId] = $this->createSharedFolderWithSubfolder('withclicks');
$bookmark = Db\Bookmark::fromArray([
'userId' => $ownerId,
'url' => 'https://example.org/clicked-in-shared-subfolder',
'title' => 'Nested clicked',
'description' => '',
]);
$bookmark = $this->bookmarkMapper->insertOrUpdate($bookmark);
$bookmark->setClickcount(3);
$this->bookmarkMapper->update($bookmark);
$this->treeMapper->addToFolders(Db\TreeMapper::TYPE_BOOKMARK, $bookmark->getId(), [$subFolderId]);
// Owner reaches the subfolder directly; sharee reaches it through the share.
$this->assertSame(1, $this->bookmarkMapper->countWithClicks($ownerId));
$this->assertSame(1, $this->bookmarkMapper->countWithClicks($recipientId));
}
/**
* Regression test: countAllClicks() must (a) include clicks of a bookmark living
* in a subfolder of a shared folder for the sharee, and (b) count each bookmark's
* clicks only once even when it is reachable through several folders. The old
* implementation summed per tree row (over-counting duplicates) and missed the
* nested case for the sharee entirely.
*
* @throws \OCA\Bookmarks\Exception\AlreadyExistsError
* @throws \OCA\Bookmarks\Exception\UserLimitExceededError
* @throws UrlParseError
* @throws MultipleObjectsReturnedException
*/
public function testCountAllClicksInSubfolderOfSharedFolderCountsEachBookmarkOnce() {
[$ownerId, $recipientId, $sharedFolderId, $subFolderA] = $this->createSharedFolderWithSubfolder('allclicks');
// A second subfolder so the same bookmark is reachable through two nested folders.
$subFolderB = new Db\Folder();
$subFolderB->setTitle('sub-b');
$subFolderB->setUserId($ownerId);
$this->folderMapper->insert($subFolderB);
$this->treeMapper->move(Db\TreeMapper::TYPE_FOLDER, $subFolderB->getId(), $sharedFolderId);
$bookmark = Db\Bookmark::fromArray([
'userId' => $ownerId,
'url' => 'https://example.org/clicks-in-shared-subfolders',
'title' => 'Nested clicks',
'description' => '',
]);
$bookmark = $this->bookmarkMapper->insertOrUpdate($bookmark);
$bookmark->setClickcount(7);
$this->bookmarkMapper->update($bookmark);
$this->treeMapper->addToFolders(
Db\TreeMapper::TYPE_BOOKMARK,
$bookmark->getId(),
[$subFolderA, $subFolderB->getId()],
);
// Reachable through two nested folders, but its 7 clicks must be summed once.
// (Owner: old code summed 14; sharee: old code returned 0.)
$this->assertSame(7, $this->bookmarkMapper->countAllClicks($ownerId));
$this->assertSame(7, $this->bookmarkMapper->countAllClicks($recipientId));
}
/**
* @return array
*/
public function singleBookmarksProvider(): array {
return array_map(function ($props) {
return [Db\Bookmark::fromArray($props)];
}, [
'Simple URL with title and description' => ['url' => 'https://google.com/', 'title' => 'Google', 'description' => 'Search engine'],
'Simple URL with title' => ['url' => 'https://nextcloud.com/', 'title' => 'Nextcloud', 'description' => ''],
'Simple URL' => ['url' => 'https://php.net/', 'title' => '', 'description' => ''],
'URL with unicode' => ['url' => 'https://de.wikipedia.org/wiki/%C3%9C', 'title' => '', 'description' => ''],
]);
}
}