6 namespace Shaarli\Bookmark
;
12 use Shaarli\Bookmark\Exception\BookmarkNotFoundException
;
13 use Shaarli\Config\ConfigManager
;
14 use Shaarli\Formatter\BookmarkMarkdownFormatter
;
19 * Unitary tests for LegacyLinkDBTest
21 class BookmarkFileServiceTest
extends TestCase
23 // datastore to test write operations
24 protected static $testDatastore = 'sandbox/datastore.php';
26 protected static $testConf = 'sandbox/config';
28 protected static $testUpdates = 'sandbox/updates.txt';
31 * @var ConfigManager instance.
36 * @var History instance.
41 * @var ReferenceLinkDB instance.
43 protected $refDB = null;
46 * @var BookmarkFileService public LinkDB instance.
48 protected $publicLinkDB = null;
51 * @var BookmarkFileService private LinkDB instance.
53 protected $privateLinkDB = null;
56 * Instantiates public and private LinkDBs with test data
58 * The reference datastore contains public and private bookmarks that
59 * will be used to test LinkDB's methods:
60 * - access filtering (public/private),
67 * Resets test data for each test
69 protected function setUp(): void
71 if (file_exists(self
::$testDatastore)) {
72 unlink(self
::$testDatastore);
75 if (file_exists(self
::$testConf .'.json.php')) {
76 unlink(self
::$testConf .'.json.php');
79 if (file_exists(self
::$testUpdates)) {
80 unlink(self
::$testUpdates);
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);
95 * Test migrate() method with a legacy datastore.
97 public function testDatabaseMigration()
99 if (!defined('SHAARLI_VERSION')) {
100 define('SHAARLI_VERSION', 'dev');
103 $this->refDB
= new \
ReferenceLinkDB(true);
104 $this->refDB
->write(self
::$testDatastore);
105 $db = self
::getMethod('migrate');
106 $db->invokeArgs($this->privateLinkDB
, []);
108 $db = new \
FakeBookmarkService($this->conf
, $this->history
, true);
109 $this->assertInstanceOf(BookmarkArray
::class, $db->getBookmarks());
110 $this->assertEquals($this->refDB
->countLinks(), $db->count());
114 * Test get() method for a defined and saved bookmark
116 public function testGetDefinedSaved()
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());
124 * Test get() method for a defined and not saved bookmark
126 public function testGetDefinedNotSaved()
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());
136 * Test get() method for an undefined bookmark
138 public function testGetUndefined()
140 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException
::class);
142 $this->privateLinkDB
->get(666);
146 * Test add() method for a bookmark fully built
148 public function testAddFull()
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'));
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());
177 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, true);
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());
194 * Test add() method for a bookmark without any field set
196 public function testAddMinimal()
198 $bookmark = new Bookmark();
199 $this->privateLinkDB
->add($bookmark);
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());
215 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, true);
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());
232 * Test add() method for a bookmark without any field set and without writing the data store
234 public function testAddMinimalNoWrite()
236 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException
::class);
238 $bookmark = new Bookmark();
239 $this->privateLinkDB
->add($bookmark, false);
241 $bookmark = $this->privateLinkDB
->get(43);
242 $this->assertEquals(43, $bookmark->getId());
245 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, true);
247 $this->privateLinkDB
->get(43);
251 * Test add() method while logged out
253 public function testAddLoggedOut()
255 $this->expectException(\Exception
::class);
256 $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
258 $this->publicLinkDB
->add(new Bookmark());
262 * Test add() method with an entry which is not a bookmark instance
264 public function testAddNotABookmark()
266 $this->expectException(\Exception
::class);
267 $this->expectExceptionMessage('Provided data is invalid');
269 $this->privateLinkDB
->add(['title' => 'hi!']);
273 * Test add() method with a Bookmark already containing an ID
275 public function testAddWithId()
277 $this->expectException(\Exception
::class);
278 $this->expectExceptionMessage('This bookmarks already exists');
280 $bookmark = new Bookmark();
281 $bookmark->setId(43);
282 $this->privateLinkDB
->add($bookmark);
286 * Test set() method for a bookmark fully built
288 public function testSetFull()
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'));
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());
317 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, true);
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());
334 * Test set() method for a bookmark without any field set
336 public function testSetMinimal()
338 $bookmark = $this->privateLinkDB
->get(42);
339 $this->privateLinkDB
->set($bookmark);
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());
352 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20100310_101010'),
353 $bookmark->getCreated()
355 $this->assertTrue(new \
DateTime('5 seconds ago') < $bookmark->getUpdated());
358 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, true);
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());
371 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20100310_101010'),
372 $bookmark->getCreated()
374 $this->assertTrue(new \
DateTime('5 seconds ago') < $bookmark->getUpdated());
378 * Test set() method for a bookmark without any field set and without writing the data store
380 public function testSetMinimalNoWrite()
382 $bookmark = $this->privateLinkDB
->get(42);
383 $bookmark->setTitle($title = 'hi!');
384 $this->privateLinkDB
->set($bookmark, false);
386 $bookmark = $this->privateLinkDB
->get(42);
387 $this->assertEquals(42, $bookmark->getId());
388 $this->assertEquals($title, $bookmark->getTitle());
391 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, true);
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());
399 * Test set() method while logged out
401 public function testSetLoggedOut()
403 $this->expectException(\Exception
::class);
404 $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
406 $this->publicLinkDB
->set(new Bookmark());
410 * Test set() method with an entry which is not a bookmark instance
412 public function testSetNotABookmark()
414 $this->expectException(\Exception
::class);
415 $this->expectExceptionMessage('Provided data is invalid');
417 $this->privateLinkDB
->set(['title' => 'hi!']);
421 * Test set() method with a Bookmark without an ID defined.
423 public function testSetWithoutId()
425 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException
::class);
427 $bookmark = new Bookmark();
428 $this->privateLinkDB
->set($bookmark);
432 * Test set() method with a Bookmark with an unknow ID
434 public function testSetWithUnknownId()
436 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException
::class);
438 $bookmark = new Bookmark();
439 $bookmark->setId(666);
440 $this->privateLinkDB
->set($bookmark);
444 * Test addOrSet() method with a new ID
446 public function testAddOrSetNew()
448 $bookmark = new Bookmark();
449 $this->privateLinkDB
->addOrSet($bookmark);
451 $bookmark = $this->privateLinkDB
->get(43);
452 $this->assertEquals(43, $bookmark->getId());
455 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, true);
457 $bookmark = $this->privateLinkDB
->get(43);
458 $this->assertEquals(43, $bookmark->getId());
462 * Test addOrSet() method with an existing ID
464 public function testAddOrSetExisting()
466 $bookmark = $this->privateLinkDB
->get(42);
467 $bookmark->setTitle($title = 'hi!');
468 $this->privateLinkDB
->addOrSet($bookmark);
470 $bookmark = $this->privateLinkDB
->get(42);
471 $this->assertEquals(42, $bookmark->getId());
472 $this->assertEquals($title, $bookmark->getTitle());
475 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, true);
477 $bookmark = $this->privateLinkDB
->get(42);
478 $this->assertEquals(42, $bookmark->getId());
479 $this->assertEquals($title, $bookmark->getTitle());
483 * Test addOrSet() method while logged out
485 public function testAddOrSetLoggedOut()
487 $this->expectException(\Exception
::class);
488 $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
490 $this->publicLinkDB
->addOrSet(new Bookmark());
494 * Test addOrSet() method with an entry which is not a bookmark instance
496 public function testAddOrSetNotABookmark()
498 $this->expectException(\Exception
::class);
499 $this->expectExceptionMessage('Provided data is invalid');
501 $this->privateLinkDB
->addOrSet(['title' => 'hi!']);
505 * Test addOrSet() method for a bookmark without any field set and without writing the data store
507 public function testAddOrSetMinimalNoWrite()
509 $bookmark = $this->privateLinkDB
->get(42);
510 $bookmark->setTitle($title = 'hi!');
511 $this->privateLinkDB
->addOrSet($bookmark, false);
513 $bookmark = $this->privateLinkDB
->get(42);
514 $this->assertEquals(42, $bookmark->getId());
515 $this->assertEquals($title, $bookmark->getTitle());
518 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, true);
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());
526 * Test remove() method with an existing Bookmark
528 public function testRemoveExisting()
530 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException
::class);
532 $bookmark = $this->privateLinkDB
->get(42);
533 $this->privateLinkDB
->remove($bookmark);
537 $this->privateLinkDB
->get(42);
538 } catch (BookmarkNotFoundException
$e) {
541 $this->assertInstanceOf(BookmarkNotFoundException
::class, $exception);
544 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, true);
546 $this->privateLinkDB
->get(42);
550 * Test remove() method while logged out
552 public function testRemoveLoggedOut()
554 $this->expectException(\Exception
::class);
555 $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
557 $bookmark = $this->privateLinkDB
->get(42);
558 $this->publicLinkDB
->remove($bookmark);
562 * Test remove() method with an entry which is not a bookmark instance
564 public function testRemoveNotABookmark()
566 $this->expectException(\Exception
::class);
567 $this->expectExceptionMessage('Provided data is invalid');
569 $this->privateLinkDB
->remove(['title' => 'hi!']);
573 * Test remove() method with a Bookmark with an unknown ID
575 public function testRemoveWithUnknownId()
577 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException
::class);
579 $bookmark = new Bookmark();
580 $bookmark->setId(666);
581 $this->privateLinkDB
->remove($bookmark);
585 * Test exists() method
587 public function testExists()
589 $this->assertTrue($this->privateLinkDB
->exists(42)); // public
590 $this->assertTrue($this->privateLinkDB
->exists(6)); // private
592 $this->assertTrue($this->privateLinkDB
->exists(42, BookmarkFilter
::$ALL));
593 $this->assertTrue($this->privateLinkDB
->exists(6, BookmarkFilter
::$ALL));
595 $this->assertTrue($this->privateLinkDB
->exists(42, BookmarkFilter
::$PUBLIC));
596 $this->assertFalse($this->privateLinkDB
->exists(6, BookmarkFilter
::$PUBLIC));
598 $this->assertFalse($this->privateLinkDB
->exists(42, BookmarkFilter
::$PRIVATE));
599 $this->assertTrue($this->privateLinkDB
->exists(6, BookmarkFilter
::$PRIVATE));
601 $this->assertTrue($this->publicLinkDB
->exists(42));
602 $this->assertFalse($this->publicLinkDB
->exists(6));
604 $this->assertTrue($this->publicLinkDB
->exists(42, BookmarkFilter
::$PUBLIC));
605 $this->assertFalse($this->publicLinkDB
->exists(6, BookmarkFilter
::$PUBLIC));
607 $this->assertFalse($this->publicLinkDB
->exists(42, BookmarkFilter
::$PRIVATE));
608 $this->assertTrue($this->publicLinkDB
->exists(6, BookmarkFilter
::$PRIVATE));
612 * Test initialize() method
614 public function testInitialize()
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()
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()
627 $this->assertStringStartsWith(
628 'Welcome to Shaarli!',
629 $this->privateLinkDB
->get(45)->getDescription()
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.
639 * Attempt to instantiate a LinkDB whereas the datastore is not writable
641 public function testConstructDatastoreNotWriteable()
643 $this->expectException(\Shaarli\Bookmark\Exception\NotWritableDataStoreException
::class);
644 $this->expectExceptionMessageRegExp('#Couldn\'t load data from the data store file "null".*#');
646 $conf = new ConfigManager('tests/utils/config/configJson');
647 $conf->set('resource.datastore', 'null/store.db');
648 new BookmarkFileService($conf, $this->history
, true);
652 * The DB doesn't exist, ensure it is created with an empty datastore
654 public function testCheckDBNewLoggedIn()
656 unlink(self
::$testDatastore);
657 $this->assertFileNotExists(self
::$testDatastore);
658 new BookmarkFileService($this->conf
, $this->history
, true);
659 $this->assertFileExists(self
::$testDatastore);
661 // ensure the correct data has been written
662 $this->assertGreaterThan(0, filesize(self
::$testDatastore));
666 * The DB doesn't exist, but not logged in, ensure it initialized, but the file is not written
668 public function testCheckDBNewLoggedOut()
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());
679 * Load public bookmarks from the DB
681 public function testReadPublicDB()
684 $this->refDB
->countPublicLinks(),
685 $this->publicLinkDB
->count()
690 * Load public and private bookmarks from the DB
692 public function testReadPrivateDB()
695 $this->refDB
->countLinks(),
696 $this->privateLinkDB
->count()
701 * Save the bookmarks to the DB
703 public function testSave()
705 $testDB = new BookmarkFileService($this->conf
, $this->history
, true);
706 $dbSize = $testDB->count();
708 $bookmark = new Bookmark();
709 $testDB->add($bookmark);
711 $testDB = new BookmarkFileService($this->conf
, $this->history
, true);
712 $this->assertEquals($dbSize +
1, $testDB->count());
716 * Count existing bookmarks - public bookmarks hidden
718 public function testCountHiddenPublic()
720 $this->conf
->set('privacy.hide_public_links', true);
721 $linkDB = new BookmarkFileService($this->conf
, $this->history
, false);
723 $this->assertEquals(0, $linkDB->count());
727 * List the days for which bookmarks have been posted
729 public function testDays()
732 ['20100309', '20100310', '20121206', '20121207', '20130614', '20150310'],
733 $this->publicLinkDB
->days()
737 ['20100309', '20100310', '20121206', '20121207', '20130614', '20141125', '20150310'],
738 $this->privateLinkDB
->days()
743 * The URL corresponds to an existing entry in the DB
745 public function testGetKnownLinkFromURL()
747 $link = $this->publicLinkDB
->findByUrl('http://mediagoblin.org/');
749 $this->assertNotEquals(false, $link);
750 $this->assertContainsPolyfill(
751 'A free software media publishing platform',
752 $link->getDescription()
757 * The URL is not in the DB
759 public function testGetUnknownLinkFromURL()
763 $this->publicLinkDB
->findByUrl('http://dev.null')
770 public function testAllTags()
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`.
790 $this->publicLinkDB
->bookmarksCountPerTag()
818 $this->privateLinkDB
->bookmarksCountPerTag()
834 $this->privateLinkDB
->bookmarksCountPerTag(['web'])
843 $this->privateLinkDB
->bookmarksCountPerTag(['web'], 'private')
848 * Test filter with string.
850 public function testFilterString()
852 $tags = 'dev cartoon';
853 $request = ['searchtags' => $tags];
856 count($this->privateLinkDB
->search($request, null, true))
861 * Test filter with array.
863 public function testFilterArray()
865 $tags = ['dev', 'cartoon'];
866 $request = ['searchtags' => $tags];
869 count($this->privateLinkDB
->search($request, null, true))
874 * Test hidden tags feature:
875 * tags starting with a dot '.' are only visible when logged in.
877 public function testHiddenTags()
880 $request = ['searchtags' => $tags];
883 count($this->privateLinkDB
->search($request, 'all', true))
888 count($this->publicLinkDB
->search($request, 'public', true))
893 * Test filterHash() with a valid smallhash.
895 public function testFilterHashValid()
897 $request = smallHash('20150310_114651');
900 $this->publicLinkDB
->findByHash($request)->getShortUrl()
902 $request = smallHash('20150310_114633' . 8);
905 $this->publicLinkDB
->findByHash($request)->getShortUrl()
910 * Test filterHash() with an invalid smallhash.
912 public function testFilterHashInValid1()
914 $this->expectException(BookmarkNotFoundException
::class);
917 $this->publicLinkDB
->findByHash($request);
921 * Test filterHash() with an empty smallhash.
923 public function testFilterHashInValid()
925 $this->expectException(BookmarkNotFoundException
::class);
927 $this->publicLinkDB
->findByHash('');
931 * Test linksCountPerTag all tags without filter.
932 * Equal occurrences should be sorted alphabetically.
934 public function testCountLinkPerTagAllNoFilter()
960 $tags = $this->privateLinkDB
->bookmarksCountPerTag();
962 $this->assertEquals($expected, $tags, var_export($tags, true));
966 * Test linksCountPerTag all tags with filter.
967 * Equal occurrences should be sorted alphabetically.
969 public function testCountLinkPerTagAllWithFilter()
982 $tags = $this->privateLinkDB
->bookmarksCountPerTag(['gnu']);
984 $this->assertEquals($expected, $tags, var_export($tags, true));
988 * Test linksCountPerTag public tags with filter.
989 * Equal occurrences should be sorted alphabetically.
991 public function testCountLinkPerTagPublicWithFilter()
1004 $tags = $this->privateLinkDB
->bookmarksCountPerTag(['gnu'], 'public');
1006 $this->assertEquals($expected, $tags, var_export($tags, true));
1010 * Test linksCountPerTag public tags with filter.
1011 * Equal occurrences should be sorted alphabetically.
1013 public function testCountLinkPerTagPrivateWithFilter()
1022 $tags = $this->privateLinkDB
->bookmarksCountPerTag(['dev'], 'private');
1024 $this->assertEquals($expected, $tags, var_export($tags, true));
1028 * Test linksCountPerTag public tags with filter.
1029 * Equal occurrences should be sorted alphabetically.
1031 public function testCountTagsNoMarkdown()
1051 'newTagToCount' => 1,
1058 $bookmark = new Bookmark();
1059 $bookmark->setTags(['newTagToCount', BookmarkMarkdownFormatter
::NO_MD_TAG
]);
1060 $this->privateLinkDB
->add($bookmark);
1062 $tags = $this->privateLinkDB
->bookmarksCountPerTag();
1064 $this->assertEquals($expected, $tags, var_export($tags, true));
1068 * Test filterDay while logged in
1070 public function testFilterDayLoggedIn(): void
1072 $bookmarks = $this->privateLinkDB
->filterDay('20121206');
1073 $expectedIds = [4, 9, 1, 0];
1075 static::assertCount(4, $bookmarks);
1076 foreach ($bookmarks as $bookmark) {
1077 $i = ($i ?? -1) +
1;
1078 static::assertSame($expectedIds[$i], $bookmark->getId());
1083 * Test filterDay while logged out
1085 public function testFilterDayLoggedOut(): void
1087 $bookmarks = $this->publicLinkDB
->filterDay('20121206');
1088 $expectedIds = [4, 9, 1];
1090 static::assertCount(3, $bookmarks);
1091 foreach ($bookmarks as $bookmark) {
1092 $i = ($i ?? -1) +
1;
1093 static::assertSame($expectedIds[$i], $bookmark->getId());
1098 * Allows to test LinkDB's private methods
1101 * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html
1102 * http://stackoverflow.com/a/2798203
1104 protected static function getMethod($name)
1106 $class = new ReflectionClass('Shaarli\Bookmark\BookmarkFileService');
1107 $method = $class->getMethod($name);
1108 $method->setAccessible(true);