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