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