]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | /** | |
3 | * Link datastore tests | |
4 | */ | |
5 | ||
6 | namespace Shaarli\Bookmark; | |
7 | ||
8 | use DateTime; | |
9 | use PHPUnit\Framework\TestCase; | |
10 | use ReferenceLinkDB; | |
11 | use ReflectionClass; | |
12 | use Shaarli; | |
13 | use Shaarli\Bookmark\Exception\BookmarkNotFoundException; | |
14 | use Shaarli\Config\ConfigManager; | |
15 | use Shaarli\History; | |
16 | ||
17 | /** | |
18 | * Unitary tests for LegacyLinkDBTest | |
19 | */ | |
20 | class BookmarkFileServiceTest extends TestCase | |
21 | { | |
22 | // datastore to test write operations | |
23 | protected static $testDatastore = 'sandbox/datastore.php'; | |
24 | ||
25 | protected static $testConf = 'sandbox/config'; | |
26 | ||
27 | protected static $testUpdates = 'sandbox/updates.txt'; | |
28 | ||
29 | /** | |
30 | * @var ConfigManager instance. | |
31 | */ | |
32 | protected $conf; | |
33 | ||
34 | /** | |
35 | * @var History instance. | |
36 | */ | |
37 | protected $history; | |
38 | ||
39 | /** | |
40 | * @var ReferenceLinkDB instance. | |
41 | */ | |
42 | protected $refDB = null; | |
43 | ||
44 | /** | |
45 | * @var BookmarkFileService public LinkDB instance. | |
46 | */ | |
47 | protected $publicLinkDB = null; | |
48 | ||
49 | /** | |
50 | * @var BookmarkFileService private LinkDB instance. | |
51 | */ | |
52 | protected $privateLinkDB = null; | |
53 | ||
54 | /** | |
55 | * Instantiates public and private LinkDBs with test data | |
56 | * | |
57 | * The reference datastore contains public and private bookmarks that | |
58 | * will be used to test LinkDB's methods: | |
59 | * - access filtering (public/private), | |
60 | * - link searches: | |
61 | * - by day, | |
62 | * - by tag, | |
63 | * - by text, | |
64 | * - etc. | |
65 | * | |
66 | * Resets test data for each test | |
67 | */ | |
68 | protected function setUp() | |
69 | { | |
70 | if (file_exists(self::$testDatastore)) { | |
71 | unlink(self::$testDatastore); | |
72 | } | |
73 | ||
74 | if (file_exists(self::$testConf .'.json.php')) { | |
75 | unlink(self::$testConf .'.json.php'); | |
76 | } | |
77 | ||
78 | if (file_exists(self::$testUpdates)) { | |
79 | unlink(self::$testUpdates); | |
80 | } | |
81 | ||
82 | copy('tests/utils/config/configJson.json.php', self::$testConf .'.json.php'); | |
83 | $this->conf = new ConfigManager(self::$testConf); | |
84 | $this->conf->set('resource.datastore', self::$testDatastore); | |
85 | $this->conf->set('resource.updates', self::$testUpdates); | |
86 | $this->refDB = new \ReferenceLinkDB(); | |
87 | $this->refDB->write(self::$testDatastore); | |
88 | $this->history = new History('sandbox/history.php'); | |
89 | $this->publicLinkDB = new BookmarkFileService($this->conf, $this->history, false); | |
90 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | |
91 | } | |
92 | ||
93 | /** | |
94 | * Test migrate() method with a legacy datastore. | |
95 | */ | |
96 | public function testDatabaseMigration() | |
97 | { | |
98 | if (!defined('SHAARLI_VERSION')) { | |
99 | define('SHAARLI_VERSION', 'dev'); | |
100 | } | |
101 | ||
102 | $this->refDB = new \ReferenceLinkDB(true); | |
103 | $this->refDB->write(self::$testDatastore); | |
104 | $db = self::getMethod('migrate'); | |
105 | $db->invokeArgs($this->privateLinkDB, []); | |
106 | ||
107 | $db = new \FakeBookmarkService($this->conf, $this->history, true); | |
108 | $this->assertInstanceOf(BookmarkArray::class, $db->getBookmarks()); | |
109 | $this->assertEquals($this->refDB->countLinks(), $db->count()); | |
110 | } | |
111 | ||
112 | /** | |
113 | * Test get() method for a defined and saved bookmark | |
114 | */ | |
115 | public function testGetDefinedSaved() | |
116 | { | |
117 | $bookmark = $this->privateLinkDB->get(42); | |
118 | $this->assertEquals(42, $bookmark->getId()); | |
119 | $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle()); | |
120 | } | |
121 | ||
122 | /** | |
123 | * Test get() method for a defined and not saved bookmark | |
124 | */ | |
125 | public function testGetDefinedNotSaved() | |
126 | { | |
127 | $bookmark = new Bookmark(); | |
128 | $this->privateLinkDB->add($bookmark); | |
129 | $createdBookmark = $this->privateLinkDB->get(43); | |
130 | $this->assertEquals(43, $createdBookmark->getId()); | |
131 | $this->assertEmpty($createdBookmark->getDescription()); | |
132 | } | |
133 | ||
134 | /** | |
135 | * Test get() method for an undefined bookmark | |
136 | * | |
137 | * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException | |
138 | */ | |
139 | public function testGetUndefined() | |
140 | { | |
141 | $this->privateLinkDB->get(666); | |
142 | } | |
143 | ||
144 | /** | |
145 | * Test add() method for a bookmark fully built | |
146 | */ | |
147 | public function testAddFull() | |
148 | { | |
149 | $bookmark = new Bookmark(); | |
150 | $bookmark->setUrl($url = 'https://domain.tld/index.php'); | |
151 | $bookmark->setShortUrl('abc'); | |
152 | $bookmark->setTitle($title = 'This a brand new bookmark'); | |
153 | $bookmark->setDescription($desc = 'It should be created and written'); | |
154 | $bookmark->setTags($tags = ['tag1', 'tagssss']); | |
155 | $bookmark->setThumbnail($thumb = 'http://thumb.tld/dle.png'); | |
156 | $bookmark->setPrivate(true); | |
157 | $bookmark->setSticky(true); | |
158 | $bookmark->setCreated($created = DateTime::createFromFormat('Ymd_His', '20190518_140354')); | |
159 | $bookmark->setUpdated($updated = DateTime::createFromFormat('Ymd_His', '20190518_150354')); | |
160 | ||
161 | $this->privateLinkDB->add($bookmark); | |
162 | $bookmark = $this->privateLinkDB->get(43); | |
163 | $this->assertEquals(43, $bookmark->getId()); | |
164 | $this->assertEquals($url, $bookmark->getUrl()); | |
165 | $this->assertEquals('abc', $bookmark->getShortUrl()); | |
166 | $this->assertEquals($title, $bookmark->getTitle()); | |
167 | $this->assertEquals($desc, $bookmark->getDescription()); | |
168 | $this->assertEquals($tags, $bookmark->getTags()); | |
169 | $this->assertEquals($thumb, $bookmark->getThumbnail()); | |
170 | $this->assertTrue($bookmark->isPrivate()); | |
171 | $this->assertTrue($bookmark->isSticky()); | |
172 | $this->assertEquals($created, $bookmark->getCreated()); | |
173 | $this->assertEquals($updated, $bookmark->getUpdated()); | |
174 | ||
175 | // reload from file | |
176 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | |
177 | ||
178 | $bookmark = $this->privateLinkDB->get(43); | |
179 | $this->assertEquals(43, $bookmark->getId()); | |
180 | $this->assertEquals($url, $bookmark->getUrl()); | |
181 | $this->assertEquals('abc', $bookmark->getShortUrl()); | |
182 | $this->assertEquals($title, $bookmark->getTitle()); | |
183 | $this->assertEquals($desc, $bookmark->getDescription()); | |
184 | $this->assertEquals($tags, $bookmark->getTags()); | |
185 | $this->assertEquals($thumb, $bookmark->getThumbnail()); | |
186 | $this->assertTrue($bookmark->isPrivate()); | |
187 | $this->assertTrue($bookmark->isSticky()); | |
188 | $this->assertEquals($created, $bookmark->getCreated()); | |
189 | $this->assertEquals($updated, $bookmark->getUpdated()); | |
190 | } | |
191 | ||
192 | /** | |
193 | * Test add() method for a bookmark without any field set | |
194 | */ | |
195 | public function testAddMinimal() | |
196 | { | |
197 | $bookmark = new Bookmark(); | |
198 | $this->privateLinkDB->add($bookmark); | |
199 | ||
200 | $bookmark = $this->privateLinkDB->get(43); | |
201 | $this->assertEquals(43, $bookmark->getId()); | |
202 | $this->assertRegExp('/\?[\w\-]{6}/', $bookmark->getUrl()); | |
203 | $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl()); | |
204 | $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle()); | |
205 | $this->assertEmpty($bookmark->getDescription()); | |
206 | $this->assertEmpty($bookmark->getTags()); | |
207 | $this->assertEmpty($bookmark->getThumbnail()); | |
208 | $this->assertFalse($bookmark->isPrivate()); | |
209 | $this->assertFalse($bookmark->isSticky()); | |
210 | $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getCreated()); | |
211 | $this->assertNull($bookmark->getUpdated()); | |
212 | ||
213 | // reload from file | |
214 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | |
215 | ||
216 | $bookmark = $this->privateLinkDB->get(43); | |
217 | $this->assertEquals(43, $bookmark->getId()); | |
218 | $this->assertRegExp('/\?[\w\-]{6}/', $bookmark->getUrl()); | |
219 | $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl()); | |
220 | $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle()); | |
221 | $this->assertEmpty($bookmark->getDescription()); | |
222 | $this->assertEmpty($bookmark->getTags()); | |
223 | $this->assertEmpty($bookmark->getThumbnail()); | |
224 | $this->assertFalse($bookmark->isPrivate()); | |
225 | $this->assertFalse($bookmark->isSticky()); | |
226 | $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getCreated()); | |
227 | $this->assertNull($bookmark->getUpdated()); | |
228 | } | |
229 | ||
230 | /** | |
231 | * Test add() method for a bookmark without any field set and without writing the data store | |
232 | * | |
233 | * @expectedExceptionMessage Shaarli\Bookmark\Exception\BookmarkNotFoundException | |
234 | */ | |
235 | public function testAddMinimalNoWrite() | |
236 | { | |
237 | $bookmark = new Bookmark(); | |
238 | $this->privateLinkDB->add($bookmark); | |
239 | ||
240 | $bookmark = $this->privateLinkDB->get(43); | |
241 | $this->assertEquals(43, $bookmark->getId()); | |
242 | ||
243 | // reload from file | |
244 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | |
245 | ||
246 | $this->privateLinkDB->get(43); | |
247 | } | |
248 | ||
249 | /** | |
250 | * Test add() method while logged out | |
251 | * | |
252 | * @expectedException \Exception | |
253 | * @expectedExceptionMessage You're not authorized to alter the datastore | |
254 | */ | |
255 | public function testAddLoggedOut() | |
256 | { | |
257 | $this->publicLinkDB->add(new Bookmark()); | |
258 | } | |
259 | ||
260 | /** | |
261 | * Test add() method with an entry which is not a bookmark instance | |
262 | * | |
263 | * @expectedException \Exception | |
264 | * @expectedExceptionMessage Provided data is invalid | |
265 | */ | |
266 | public function testAddNotABookmark() | |
267 | { | |
268 | $this->privateLinkDB->add(['title' => 'hi!']); | |
269 | } | |
270 | ||
271 | /** | |
272 | * Test add() method with a Bookmark already containing an ID | |
273 | * | |
274 | * @expectedException \Exception | |
275 | * @expectedExceptionMessage This bookmarks already exists | |
276 | */ | |
277 | public function testAddWithId() | |
278 | { | |
279 | $bookmark = new Bookmark(); | |
280 | $bookmark->setId(43); | |
281 | $this->privateLinkDB->add($bookmark); | |
282 | } | |
283 | ||
284 | /** | |
285 | * Test set() method for a bookmark fully built | |
286 | */ | |
287 | public function testSetFull() | |
288 | { | |
289 | $bookmark = $this->privateLinkDB->get(42); | |
290 | $bookmark->setUrl($url = 'https://domain.tld/index.php'); | |
291 | $bookmark->setShortUrl('abc'); | |
292 | $bookmark->setTitle($title = 'This a brand new bookmark'); | |
293 | $bookmark->setDescription($desc = 'It should be created and written'); | |
294 | $bookmark->setTags($tags = ['tag1', 'tagssss']); | |
295 | $bookmark->setThumbnail($thumb = 'http://thumb.tld/dle.png'); | |
296 | $bookmark->setPrivate(true); | |
297 | $bookmark->setSticky(true); | |
298 | $bookmark->setCreated($created = DateTime::createFromFormat('Ymd_His', '20190518_140354')); | |
299 | $bookmark->setUpdated($updated = DateTime::createFromFormat('Ymd_His', '20190518_150354')); | |
300 | ||
301 | $this->privateLinkDB->set($bookmark); | |
302 | $bookmark = $this->privateLinkDB->get(42); | |
303 | $this->assertEquals(42, $bookmark->getId()); | |
304 | $this->assertEquals($url, $bookmark->getUrl()); | |
305 | $this->assertEquals('abc', $bookmark->getShortUrl()); | |
306 | $this->assertEquals($title, $bookmark->getTitle()); | |
307 | $this->assertEquals($desc, $bookmark->getDescription()); | |
308 | $this->assertEquals($tags, $bookmark->getTags()); | |
309 | $this->assertEquals($thumb, $bookmark->getThumbnail()); | |
310 | $this->assertTrue($bookmark->isPrivate()); | |
311 | $this->assertTrue($bookmark->isSticky()); | |
312 | $this->assertEquals($created, $bookmark->getCreated()); | |
313 | $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated()); | |
314 | ||
315 | // reload from file | |
316 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | |
317 | ||
318 | $bookmark = $this->privateLinkDB->get(42); | |
319 | $this->assertEquals(42, $bookmark->getId()); | |
320 | $this->assertEquals($url, $bookmark->getUrl()); | |
321 | $this->assertEquals('abc', $bookmark->getShortUrl()); | |
322 | $this->assertEquals($title, $bookmark->getTitle()); | |
323 | $this->assertEquals($desc, $bookmark->getDescription()); | |
324 | $this->assertEquals($tags, $bookmark->getTags()); | |
325 | $this->assertEquals($thumb, $bookmark->getThumbnail()); | |
326 | $this->assertTrue($bookmark->isPrivate()); | |
327 | $this->assertTrue($bookmark->isSticky()); | |
328 | $this->assertEquals($created, $bookmark->getCreated()); | |
329 | $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated()); | |
330 | } | |
331 | ||
332 | /** | |
333 | * Test set() method for a bookmark without any field set | |
334 | */ | |
335 | public function testSetMinimal() | |
336 | { | |
337 | $bookmark = $this->privateLinkDB->get(42); | |
338 | $this->privateLinkDB->set($bookmark); | |
339 | ||
340 | $bookmark = $this->privateLinkDB->get(42); | |
341 | $this->assertEquals(42, $bookmark->getId()); | |
342 | $this->assertEquals('?WDWyig', $bookmark->getUrl()); | |
343 | $this->assertEquals('1eYJ1Q', $bookmark->getShortUrl()); | |
344 | $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle()); | |
345 | $this->assertEquals('Used to test bookmarks reordering.', $bookmark->getDescription()); | |
346 | $this->assertEquals(['ut'], $bookmark->getTags()); | |
347 | $this->assertFalse($bookmark->getThumbnail()); | |
348 | $this->assertFalse($bookmark->isPrivate()); | |
349 | $this->assertFalse($bookmark->isSticky()); | |
350 | $this->assertEquals( | |
351 | DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20100310_101010'), | |
352 | $bookmark->getCreated() | |
353 | ); | |
354 | $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated()); | |
355 | ||
356 | // reload from file | |
357 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | |
358 | ||
359 | $bookmark = $this->privateLinkDB->get(42); | |
360 | $this->assertEquals(42, $bookmark->getId()); | |
361 | $this->assertEquals('?WDWyig', $bookmark->getUrl()); | |
362 | $this->assertEquals('1eYJ1Q', $bookmark->getShortUrl()); | |
363 | $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle()); | |
364 | $this->assertEquals('Used to test bookmarks reordering.', $bookmark->getDescription()); | |
365 | $this->assertEquals(['ut'], $bookmark->getTags()); | |
366 | $this->assertFalse($bookmark->getThumbnail()); | |
367 | $this->assertFalse($bookmark->isPrivate()); | |
368 | $this->assertFalse($bookmark->isSticky()); | |
369 | $this->assertEquals( | |
370 | DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20100310_101010'), | |
371 | $bookmark->getCreated() | |
372 | ); | |
373 | $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated()); | |
374 | } | |
375 | ||
376 | /** | |
377 | * Test set() method for a bookmark without any field set and without writing the data store | |
378 | */ | |
379 | public function testSetMinimalNoWrite() | |
380 | { | |
381 | $bookmark = $this->privateLinkDB->get(42); | |
382 | $bookmark->setTitle($title = 'hi!'); | |
383 | $this->privateLinkDB->set($bookmark, false); | |
384 | ||
385 | $bookmark = $this->privateLinkDB->get(42); | |
386 | $this->assertEquals(42, $bookmark->getId()); | |
387 | $this->assertEquals($title, $bookmark->getTitle()); | |
388 | ||
389 | // reload from file | |
390 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | |
391 | ||
392 | $bookmark = $this->privateLinkDB->get(42); | |
393 | $this->assertEquals(42, $bookmark->getId()); | |
394 | $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle()); | |
395 | } | |
396 | ||
397 | /** | |
398 | * Test set() method while logged out | |
399 | * | |
400 | * @expectedException \Exception | |
401 | * @expectedExceptionMessage You're not authorized to alter the datastore | |
402 | */ | |
403 | public function testSetLoggedOut() | |
404 | { | |
405 | $this->publicLinkDB->set(new Bookmark()); | |
406 | } | |
407 | ||
408 | /** | |
409 | * Test set() method with an entry which is not a bookmark instance | |
410 | * | |
411 | * @expectedException \Exception | |
412 | * @expectedExceptionMessage Provided data is invalid | |
413 | */ | |
414 | public function testSetNotABookmark() | |
415 | { | |
416 | $this->privateLinkDB->set(['title' => 'hi!']); | |
417 | } | |
418 | ||
419 | /** | |
420 | * Test set() method with a Bookmark without an ID defined. | |
421 | * | |
422 | * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException | |
423 | */ | |
424 | public function testSetWithoutId() | |
425 | { | |
426 | $bookmark = new Bookmark(); | |
427 | $this->privateLinkDB->set($bookmark); | |
428 | } | |
429 | ||
430 | /** | |
431 | * Test set() method with a Bookmark with an unknow ID | |
432 | * | |
433 | * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException | |
434 | */ | |
435 | public function testSetWithUnknownId() | |
436 | { | |
437 | $bookmark = new Bookmark(); | |
438 | $bookmark->setId(666); | |
439 | $this->privateLinkDB->set($bookmark); | |
440 | } | |
441 | ||
442 | /** | |
443 | * Test addOrSet() method with a new ID | |
444 | */ | |
445 | public function testAddOrSetNew() | |
446 | { | |
447 | $bookmark = new Bookmark(); | |
448 | $this->privateLinkDB->addOrSet($bookmark); | |
449 | ||
450 | $bookmark = $this->privateLinkDB->get(43); | |
451 | $this->assertEquals(43, $bookmark->getId()); | |
452 | ||
453 | // reload from file | |
454 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | |
455 | ||
456 | $bookmark = $this->privateLinkDB->get(43); | |
457 | $this->assertEquals(43, $bookmark->getId()); | |
458 | } | |
459 | ||
460 | /** | |
461 | * Test addOrSet() method with an existing ID | |
462 | */ | |
463 | public function testAddOrSetExisting() | |
464 | { | |
465 | $bookmark = $this->privateLinkDB->get(42); | |
466 | $bookmark->setTitle($title = 'hi!'); | |
467 | $this->privateLinkDB->addOrSet($bookmark); | |
468 | ||
469 | $bookmark = $this->privateLinkDB->get(42); | |
470 | $this->assertEquals(42, $bookmark->getId()); | |
471 | $this->assertEquals($title, $bookmark->getTitle()); | |
472 | ||
473 | // reload from file | |
474 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | |
475 | ||
476 | $bookmark = $this->privateLinkDB->get(42); | |
477 | $this->assertEquals(42, $bookmark->getId()); | |
478 | $this->assertEquals($title, $bookmark->getTitle()); | |
479 | } | |
480 | ||
481 | /** | |
482 | * Test addOrSet() method while logged out | |
483 | * | |
484 | * @expectedException \Exception | |
485 | * @expectedExceptionMessage You're not authorized to alter the datastore | |
486 | */ | |
487 | public function testAddOrSetLoggedOut() | |
488 | { | |
489 | $this->publicLinkDB->addOrSet(new Bookmark()); | |
490 | } | |
491 | ||
492 | /** | |
493 | * Test addOrSet() method with an entry which is not a bookmark instance | |
494 | * | |
495 | * @expectedException \Exception | |
496 | * @expectedExceptionMessage Provided data is invalid | |
497 | */ | |
498 | public function testAddOrSetNotABookmark() | |
499 | { | |
500 | $this->privateLinkDB->addOrSet(['title' => 'hi!']); | |
501 | } | |
502 | ||
503 | /** | |
504 | * Test addOrSet() method for a bookmark without any field set and without writing the data store | |
505 | */ | |
506 | public function testAddOrSetMinimalNoWrite() | |
507 | { | |
508 | $bookmark = $this->privateLinkDB->get(42); | |
509 | $bookmark->setTitle($title = 'hi!'); | |
510 | $this->privateLinkDB->addOrSet($bookmark, false); | |
511 | ||
512 | $bookmark = $this->privateLinkDB->get(42); | |
513 | $this->assertEquals(42, $bookmark->getId()); | |
514 | $this->assertEquals($title, $bookmark->getTitle()); | |
515 | ||
516 | // reload from file | |
517 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | |
518 | ||
519 | $bookmark = $this->privateLinkDB->get(42); | |
520 | $this->assertEquals(42, $bookmark->getId()); | |
521 | $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle()); | |
522 | } | |
523 | ||
524 | /** | |
525 | * Test remove() method with an existing Bookmark | |
526 | * | |
527 | * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException | |
528 | */ | |
529 | public function testRemoveExisting() | |
530 | { | |
531 | $bookmark = $this->privateLinkDB->get(42); | |
532 | $this->privateLinkDB->remove($bookmark); | |
533 | ||
534 | $exception = null; | |
535 | try { | |
536 | $this->privateLinkDB->get(42); | |
537 | } catch (BookmarkNotFoundException $e) { | |
538 | $exception = $e; | |
539 | } | |
540 | $this->assertInstanceOf(BookmarkNotFoundException::class, $exception); | |
541 | ||
542 | // reload from file | |
543 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | |
544 | ||
545 | $this->privateLinkDB->get(42); | |
546 | } | |
547 | ||
548 | /** | |
549 | * Test remove() method while logged out | |
550 | * | |
551 | * @expectedException \Exception | |
552 | * @expectedExceptionMessage You're not authorized to alter the datastore | |
553 | */ | |
554 | public function testRemoveLoggedOut() | |
555 | { | |
556 | $bookmark = $this->privateLinkDB->get(42); | |
557 | $this->publicLinkDB->remove($bookmark); | |
558 | } | |
559 | ||
560 | /** | |
561 | * Test remove() method with an entry which is not a bookmark instance | |
562 | * | |
563 | * @expectedException \Exception | |
564 | * @expectedExceptionMessage Provided data is invalid | |
565 | */ | |
566 | public function testRemoveNotABookmark() | |
567 | { | |
568 | $this->privateLinkDB->remove(['title' => 'hi!']); | |
569 | } | |
570 | ||
571 | /** | |
572 | * Test remove() method with a Bookmark with an unknown ID | |
573 | * | |
574 | * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException | |
575 | */ | |
576 | public function testRemoveWithUnknownId() | |
577 | { | |
578 | $bookmark = new Bookmark(); | |
579 | $bookmark->setId(666); | |
580 | $this->privateLinkDB->remove($bookmark); | |
581 | } | |
582 | ||
583 | /** | |
584 | * Test exists() method | |
585 | */ | |
586 | public function testExists() | |
587 | { | |
588 | $this->assertTrue($this->privateLinkDB->exists(42)); // public | |
589 | $this->assertTrue($this->privateLinkDB->exists(6)); // private | |
590 | ||
591 | $this->assertTrue($this->privateLinkDB->exists(42, BookmarkFilter::$ALL)); | |
592 | $this->assertTrue($this->privateLinkDB->exists(6, BookmarkFilter::$ALL)); | |
593 | ||
594 | $this->assertTrue($this->privateLinkDB->exists(42, BookmarkFilter::$PUBLIC)); | |
595 | $this->assertFalse($this->privateLinkDB->exists(6, BookmarkFilter::$PUBLIC)); | |
596 | ||
597 | $this->assertFalse($this->privateLinkDB->exists(42, BookmarkFilter::$PRIVATE)); | |
598 | $this->assertTrue($this->privateLinkDB->exists(6, BookmarkFilter::$PRIVATE)); | |
599 | ||
600 | $this->assertTrue($this->publicLinkDB->exists(42)); | |
601 | $this->assertFalse($this->publicLinkDB->exists(6)); | |
602 | ||
603 | $this->assertTrue($this->publicLinkDB->exists(42, BookmarkFilter::$PUBLIC)); | |
604 | $this->assertFalse($this->publicLinkDB->exists(6, BookmarkFilter::$PUBLIC)); | |
605 | ||
606 | $this->assertFalse($this->publicLinkDB->exists(42, BookmarkFilter::$PRIVATE)); | |
607 | $this->assertTrue($this->publicLinkDB->exists(6, BookmarkFilter::$PRIVATE)); | |
608 | } | |
609 | ||
610 | /** | |
611 | * Test initialize() method | |
612 | */ | |
613 | public function testInitialize() | |
614 | { | |
615 | $dbSize = $this->privateLinkDB->count(); | |
616 | $this->privateLinkDB->initialize(); | |
617 | $this->assertEquals($dbSize + 2, $this->privateLinkDB->count()); | |
618 | $this->assertEquals( | |
619 | 'My secret stuff... - Pastebin.com', | |
620 | $this->privateLinkDB->get(43)->getTitle() | |
621 | ); | |
622 | $this->assertEquals( | |
623 | 'The personal, minimalist, super-fast, database free, bookmarking service', | |
624 | $this->privateLinkDB->get(44)->getTitle() | |
625 | ); | |
626 | } | |
627 | ||
628 | /* | |
629 | * The following tests have been taken from the legacy LinkDB test and adapted | |
630 | * to make sure that nothing have been broken in the migration process. | |
631 | * They mostly cover search/filters. Some of them might be redundant with the previous ones. | |
632 | */ | |
633 | ||
634 | /** | |
635 | * Attempt to instantiate a LinkDB whereas the datastore is not writable | |
636 | * | |
637 | * @expectedException Shaarli\Bookmark\Exception\NotWritableDataStoreException | |
638 | * @expectedExceptionMessageRegExp #Couldn't load data from the data store file "null".*# | |
639 | */ | |
640 | public function testConstructDatastoreNotWriteable() | |
641 | { | |
642 | $conf = new ConfigManager('tests/utils/config/configJson'); | |
643 | $conf->set('resource.datastore', 'null/store.db'); | |
644 | new BookmarkFileService($conf, $this->history, true); | |
645 | } | |
646 | ||
647 | /** | |
648 | * The DB doesn't exist, ensure it is created with an empty datastore | |
649 | */ | |
650 | public function testCheckDBNewLoggedIn() | |
651 | { | |
652 | unlink(self::$testDatastore); | |
653 | $this->assertFileNotExists(self::$testDatastore); | |
654 | new BookmarkFileService($this->conf, $this->history, true); | |
655 | $this->assertFileExists(self::$testDatastore); | |
656 | ||
657 | // ensure the correct data has been written | |
658 | $this->assertGreaterThan(0, filesize(self::$testDatastore)); | |
659 | } | |
660 | ||
661 | /** | |
662 | * The DB doesn't exist, but not logged in, ensure it initialized, but the file is not written | |
663 | */ | |
664 | public function testCheckDBNewLoggedOut() | |
665 | { | |
666 | unlink(self::$testDatastore); | |
667 | $this->assertFileNotExists(self::$testDatastore); | |
668 | $db = new \FakeBookmarkService($this->conf, $this->history, false); | |
669 | $this->assertFileNotExists(self::$testDatastore); | |
670 | $this->assertInstanceOf(BookmarkArray::class, $db->getBookmarks()); | |
671 | $this->assertCount(0, $db->getBookmarks()); | |
672 | } | |
673 | ||
674 | /** | |
675 | * Load public bookmarks from the DB | |
676 | */ | |
677 | public function testReadPublicDB() | |
678 | { | |
679 | $this->assertEquals( | |
680 | $this->refDB->countPublicLinks(), | |
681 | $this->publicLinkDB->count() | |
682 | ); | |
683 | } | |
684 | ||
685 | /** | |
686 | * Load public and private bookmarks from the DB | |
687 | */ | |
688 | public function testReadPrivateDB() | |
689 | { | |
690 | $this->assertEquals( | |
691 | $this->refDB->countLinks(), | |
692 | $this->privateLinkDB->count() | |
693 | ); | |
694 | } | |
695 | ||
696 | /** | |
697 | * Save the bookmarks to the DB | |
698 | */ | |
699 | public function testSave() | |
700 | { | |
701 | $testDB = new BookmarkFileService($this->conf, $this->history, true); | |
702 | $dbSize = $testDB->count(); | |
703 | ||
704 | $bookmark = new Bookmark(); | |
705 | $testDB->add($bookmark); | |
706 | ||
707 | $testDB = new BookmarkFileService($this->conf, $this->history, true); | |
708 | $this->assertEquals($dbSize + 1, $testDB->count()); | |
709 | } | |
710 | ||
711 | /** | |
712 | * Count existing bookmarks - public bookmarks hidden | |
713 | */ | |
714 | public function testCountHiddenPublic() | |
715 | { | |
716 | $this->conf->set('privacy.hide_public_links', true); | |
717 | $linkDB = new BookmarkFileService($this->conf, $this->history, false); | |
718 | ||
719 | $this->assertEquals(0, $linkDB->count()); | |
720 | } | |
721 | ||
722 | /** | |
723 | * List the days for which bookmarks have been posted | |
724 | */ | |
725 | public function testDays() | |
726 | { | |
727 | $this->assertEquals( | |
728 | ['20100309', '20100310', '20121206', '20121207', '20130614', '20150310'], | |
729 | $this->publicLinkDB->days() | |
730 | ); | |
731 | ||
732 | $this->assertEquals( | |
733 | ['20100309', '20100310', '20121206', '20121207', '20130614', '20141125', '20150310'], | |
734 | $this->privateLinkDB->days() | |
735 | ); | |
736 | } | |
737 | ||
738 | /** | |
739 | * The URL corresponds to an existing entry in the DB | |
740 | */ | |
741 | public function testGetKnownLinkFromURL() | |
742 | { | |
743 | $link = $this->publicLinkDB->findByUrl('http://mediagoblin.org/'); | |
744 | ||
745 | $this->assertNotEquals(false, $link); | |
746 | $this->assertContains( | |
747 | 'A free software media publishing platform', | |
748 | $link->getDescription() | |
749 | ); | |
750 | } | |
751 | ||
752 | /** | |
753 | * The URL is not in the DB | |
754 | */ | |
755 | public function testGetUnknownLinkFromURL() | |
756 | { | |
757 | $this->assertEquals( | |
758 | false, | |
759 | $this->publicLinkDB->findByUrl('http://dev.null') | |
760 | ); | |
761 | } | |
762 | ||
763 | /** | |
764 | * Lists all tags | |
765 | */ | |
766 | public function testAllTags() | |
767 | { | |
768 | $this->assertEquals( | |
769 | [ | |
770 | 'web' => 3, | |
771 | 'cartoon' => 2, | |
772 | 'gnu' => 2, | |
773 | 'dev' => 1, | |
774 | 'samba' => 1, | |
775 | 'media' => 1, | |
776 | 'software' => 1, | |
777 | 'stallman' => 1, | |
778 | 'free' => 1, | |
779 | '-exclude' => 1, | |
780 | 'hashtag' => 2, | |
781 | // The DB contains a link with `sTuff` and another one with `stuff` tag. | |
782 | // They need to be grouped with the first case found - order by date DESC: `sTuff`. | |
783 | 'sTuff' => 2, | |
784 | 'ut' => 1, | |
785 | ], | |
786 | $this->publicLinkDB->bookmarksCountPerTag() | |
787 | ); | |
788 | ||
789 | $this->assertEquals( | |
790 | [ | |
791 | 'web' => 4, | |
792 | 'cartoon' => 3, | |
793 | 'gnu' => 2, | |
794 | 'dev' => 2, | |
795 | 'samba' => 1, | |
796 | 'media' => 1, | |
797 | 'software' => 1, | |
798 | 'stallman' => 1, | |
799 | 'free' => 1, | |
800 | 'html' => 1, | |
801 | 'w3c' => 1, | |
802 | 'css' => 1, | |
803 | 'Mercurial' => 1, | |
804 | 'sTuff' => 2, | |
805 | '-exclude' => 1, | |
806 | '.hidden' => 1, | |
807 | 'hashtag' => 2, | |
808 | 'tag1' => 1, | |
809 | 'tag2' => 1, | |
810 | 'tag3' => 1, | |
811 | 'tag4' => 1, | |
812 | 'ut' => 1, | |
813 | ], | |
814 | $this->privateLinkDB->bookmarksCountPerTag() | |
815 | ); | |
816 | $this->assertEquals( | |
817 | [ | |
818 | 'web' => 4, | |
819 | 'cartoon' => 2, | |
820 | 'gnu' => 1, | |
821 | 'dev' => 1, | |
822 | 'samba' => 1, | |
823 | 'media' => 1, | |
824 | 'html' => 1, | |
825 | 'w3c' => 1, | |
826 | 'css' => 1, | |
827 | 'Mercurial' => 1, | |
828 | '.hidden' => 1, | |
829 | 'hashtag' => 1, | |
830 | ], | |
831 | $this->privateLinkDB->bookmarksCountPerTag(['web']) | |
832 | ); | |
833 | $this->assertEquals( | |
834 | [ | |
835 | 'web' => 1, | |
836 | 'html' => 1, | |
837 | 'w3c' => 1, | |
838 | 'css' => 1, | |
839 | 'Mercurial' => 1, | |
840 | ], | |
841 | $this->privateLinkDB->bookmarksCountPerTag(['web'], 'private') | |
842 | ); | |
843 | } | |
844 | ||
845 | /** | |
846 | * Test filter with string. | |
847 | */ | |
848 | public function testFilterString() | |
849 | { | |
850 | $tags = 'dev cartoon'; | |
851 | $request = ['searchtags' => $tags]; | |
852 | $this->assertEquals( | |
853 | 2, | |
854 | count($this->privateLinkDB->search($request, null, true)) | |
855 | ); | |
856 | } | |
857 | ||
858 | /** | |
859 | * Test filter with array. | |
860 | */ | |
861 | public function testFilterArray() | |
862 | { | |
863 | $tags = ['dev', 'cartoon']; | |
864 | $request = ['searchtags' => $tags]; | |
865 | $this->assertEquals( | |
866 | 2, | |
867 | count($this->privateLinkDB->search($request, null, true)) | |
868 | ); | |
869 | } | |
870 | ||
871 | /** | |
872 | * Test hidden tags feature: | |
873 | * tags starting with a dot '.' are only visible when logged in. | |
874 | */ | |
875 | public function testHiddenTags() | |
876 | { | |
877 | $tags = '.hidden'; | |
878 | $request = ['searchtags' => $tags]; | |
879 | $this->assertEquals( | |
880 | 1, | |
881 | count($this->privateLinkDB->search($request, 'all', true)) | |
882 | ); | |
883 | ||
884 | $this->assertEquals( | |
885 | 0, | |
886 | count($this->publicLinkDB->search($request, 'public', true)) | |
887 | ); | |
888 | } | |
889 | ||
890 | /** | |
891 | * Test filterHash() with a valid smallhash. | |
892 | */ | |
893 | public function testFilterHashValid() | |
894 | { | |
895 | $request = smallHash('20150310_114651'); | |
896 | $this->assertEquals( | |
897 | 1, | |
898 | count($this->publicLinkDB->findByHash($request)) | |
899 | ); | |
900 | $request = smallHash('20150310_114633' . 8); | |
901 | $this->assertEquals( | |
902 | 1, | |
903 | count($this->publicLinkDB->findByHash($request)) | |
904 | ); | |
905 | } | |
906 | ||
907 | /** | |
908 | * Test filterHash() with an invalid smallhash. | |
909 | * | |
910 | * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException | |
911 | */ | |
912 | public function testFilterHashInValid1() | |
913 | { | |
914 | $request = 'blabla'; | |
915 | $this->publicLinkDB->findByHash($request); | |
916 | } | |
917 | ||
918 | /** | |
919 | * Test filterHash() with an empty smallhash. | |
920 | * | |
921 | * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException | |
922 | */ | |
923 | public function testFilterHashInValid() | |
924 | { | |
925 | $this->publicLinkDB->findByHash(''); | |
926 | } | |
927 | ||
928 | /** | |
929 | * Test linksCountPerTag all tags without filter. | |
930 | * Equal occurrences should be sorted alphabetically. | |
931 | */ | |
932 | public function testCountLinkPerTagAllNoFilter() | |
933 | { | |
934 | $expected = [ | |
935 | 'web' => 4, | |
936 | 'cartoon' => 3, | |
937 | 'dev' => 2, | |
938 | 'gnu' => 2, | |
939 | 'hashtag' => 2, | |
940 | 'sTuff' => 2, | |
941 | '-exclude' => 1, | |
942 | '.hidden' => 1, | |
943 | 'Mercurial' => 1, | |
944 | 'css' => 1, | |
945 | 'free' => 1, | |
946 | 'html' => 1, | |
947 | 'media' => 1, | |
948 | 'samba' => 1, | |
949 | 'software' => 1, | |
950 | 'stallman' => 1, | |
951 | 'tag1' => 1, | |
952 | 'tag2' => 1, | |
953 | 'tag3' => 1, | |
954 | 'tag4' => 1, | |
955 | 'ut' => 1, | |
956 | 'w3c' => 1, | |
957 | ]; | |
958 | $tags = $this->privateLinkDB->bookmarksCountPerTag(); | |
959 | ||
960 | $this->assertEquals($expected, $tags, var_export($tags, true)); | |
961 | } | |
962 | ||
963 | /** | |
964 | * Test linksCountPerTag all tags with filter. | |
965 | * Equal occurrences should be sorted alphabetically. | |
966 | */ | |
967 | public function testCountLinkPerTagAllWithFilter() | |
968 | { | |
969 | $expected = [ | |
970 | 'gnu' => 2, | |
971 | 'hashtag' => 2, | |
972 | '-exclude' => 1, | |
973 | '.hidden' => 1, | |
974 | 'free' => 1, | |
975 | 'media' => 1, | |
976 | 'software' => 1, | |
977 | 'stallman' => 1, | |
978 | 'stuff' => 1, | |
979 | 'web' => 1, | |
980 | ]; | |
981 | $tags = $this->privateLinkDB->bookmarksCountPerTag(['gnu']); | |
982 | ||
983 | $this->assertEquals($expected, $tags, var_export($tags, true)); | |
984 | } | |
985 | ||
986 | /** | |
987 | * Test linksCountPerTag public tags with filter. | |
988 | * Equal occurrences should be sorted alphabetically. | |
989 | */ | |
990 | public function testCountLinkPerTagPublicWithFilter() | |
991 | { | |
992 | $expected = [ | |
993 | 'gnu' => 2, | |
994 | 'hashtag' => 2, | |
995 | '-exclude' => 1, | |
996 | '.hidden' => 1, | |
997 | 'free' => 1, | |
998 | 'media' => 1, | |
999 | 'software' => 1, | |
1000 | 'stallman' => 1, | |
1001 | 'stuff' => 1, | |
1002 | 'web' => 1, | |
1003 | ]; | |
1004 | $tags = $this->privateLinkDB->bookmarksCountPerTag(['gnu'], 'public'); | |
1005 | ||
1006 | $this->assertEquals($expected, $tags, var_export($tags, true)); | |
1007 | } | |
1008 | ||
1009 | /** | |
1010 | * Test linksCountPerTag public tags with filter. | |
1011 | * Equal occurrences should be sorted alphabetically. | |
1012 | */ | |
1013 | public function testCountLinkPerTagPrivateWithFilter() | |
1014 | { | |
1015 | $expected = [ | |
1016 | 'cartoon' => 1, | |
1017 | 'dev' => 1, | |
1018 | 'tag1' => 1, | |
1019 | 'tag2' => 1, | |
1020 | 'tag3' => 1, | |
1021 | 'tag4' => 1, | |
1022 | ]; | |
1023 | $tags = $this->privateLinkDB->bookmarksCountPerTag(['dev'], 'private'); | |
1024 | ||
1025 | $this->assertEquals($expected, $tags, var_export($tags, true)); | |
1026 | } | |
1027 | ||
1028 | /** | |
1029 | * Allows to test LinkDB's private methods | |
1030 | * | |
1031 | * @see | |
1032 | * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html | |
1033 | * http://stackoverflow.com/a/2798203 | |
1034 | */ | |
1035 | protected static function getMethod($name) | |
1036 | { | |
1037 | $class = new ReflectionClass('Shaarli\Bookmark\BookmarkFileService'); | |
1038 | $method = $class->getMethod($name); | |
1039 | $method->setAccessible(true); | |
1040 | return $method; | |
1041 | } | |
1042 | } |