6 namespace Shaarli\Bookmark
;
9 use PHPUnit\Framework\TestCase
;
13 use Shaarli\Bookmark\Exception\BookmarkNotFoundException
;
14 use Shaarli\Config\ConfigManager
;
15 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()
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 * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
140 public function testGetUndefined()
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 * @expectedExceptionMessage Shaarli\Bookmark\Exception\BookmarkNotFoundException
236 public function testAddMinimalNoWrite()
238 $bookmark = new Bookmark();
239 $this->privateLinkDB
->add($bookmark);
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 * @expectedException \Exception
254 * @expectedExceptionMessage You're not authorized to alter the datastore
256 public function testAddLoggedOut()
258 $this->publicLinkDB
->add(new Bookmark());
262 * Test add() method with an entry which is not a bookmark instance
264 * @expectedException \Exception
265 * @expectedExceptionMessage Provided data is invalid
267 public function testAddNotABookmark()
269 $this->privateLinkDB
->add(['title' => 'hi!']);
273 * Test add() method with a Bookmark already containing an ID
275 * @expectedException \Exception
276 * @expectedExceptionMessage This bookmarks already exists
278 public function testAddWithId()
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 * @expectedException \Exception
402 * @expectedExceptionMessage You're not authorized to alter the datastore
404 public function testSetLoggedOut()
406 $this->publicLinkDB
->set(new Bookmark());
410 * Test set() method with an entry which is not a bookmark instance
412 * @expectedException \Exception
413 * @expectedExceptionMessage Provided data is invalid
415 public function testSetNotABookmark()
417 $this->privateLinkDB
->set(['title' => 'hi!']);
421 * Test set() method with a Bookmark without an ID defined.
423 * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
425 public function testSetWithoutId()
427 $bookmark = new Bookmark();
428 $this->privateLinkDB
->set($bookmark);
432 * Test set() method with a Bookmark with an unknow ID
434 * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
436 public function testSetWithUnknownId()
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 * @expectedException \Exception
486 * @expectedExceptionMessage You're not authorized to alter the datastore
488 public function testAddOrSetLoggedOut()
490 $this->publicLinkDB
->addOrSet(new Bookmark());
494 * Test addOrSet() method with an entry which is not a bookmark instance
496 * @expectedException \Exception
497 * @expectedExceptionMessage Provided data is invalid
499 public function testAddOrSetNotABookmark()
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 * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
530 public function testRemoveExisting()
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 * @expectedException \Exception
553 * @expectedExceptionMessage You're not authorized to alter the datastore
555 public function testRemoveLoggedOut()
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 * @expectedException \Exception
565 * @expectedExceptionMessage Provided data is invalid
567 public function testRemoveNotABookmark()
569 $this->privateLinkDB
->remove(['title' => 'hi!']);
573 * Test remove() method with a Bookmark with an unknown ID
575 * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
577 public function testRemoveWithUnknownId()
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 +
2, $this->privateLinkDB
->count());
620 'My secret stuff... - Pastebin.com',
621 $this->privateLinkDB
->get(43)->getTitle()
624 'The personal, minimalist, super-fast, database free, bookmarking service',
625 $this->privateLinkDB
->get(44)->getTitle()
630 * The following tests have been taken from the legacy LinkDB test and adapted
631 * to make sure that nothing have been broken in the migration process.
632 * They mostly cover search/filters. Some of them might be redundant with the previous ones.
636 * Attempt to instantiate a LinkDB whereas the datastore is not writable
638 * @expectedException Shaarli\Bookmark\Exception\NotWritableDataStoreException
639 * @expectedExceptionMessageRegExp #Couldn't load data from the data store file "null".*#
641 public function testConstructDatastoreNotWriteable()
643 $conf = new ConfigManager('tests/utils/config/configJson');
644 $conf->set('resource.datastore', 'null/store.db');
645 new BookmarkFileService($conf, $this->history
, true);
649 * The DB doesn't exist, ensure it is created with an empty datastore
651 public function testCheckDBNewLoggedIn()
653 unlink(self
::$testDatastore);
654 $this->assertFileNotExists(self
::$testDatastore);
655 new BookmarkFileService($this->conf
, $this->history
, true);
656 $this->assertFileExists(self
::$testDatastore);
658 // ensure the correct data has been written
659 $this->assertGreaterThan(0, filesize(self
::$testDatastore));
663 * The DB doesn't exist, but not logged in, ensure it initialized, but the file is not written
665 public function testCheckDBNewLoggedOut()
667 unlink(self
::$testDatastore);
668 $this->assertFileNotExists(self
::$testDatastore);
669 $db = new \
FakeBookmarkService($this->conf
, $this->history
, false);
670 $this->assertFileNotExists(self
::$testDatastore);
671 $this->assertInstanceOf(BookmarkArray
::class, $db->getBookmarks());
672 $this->assertCount(0, $db->getBookmarks());
676 * Load public bookmarks from the DB
678 public function testReadPublicDB()
681 $this->refDB
->countPublicLinks(),
682 $this->publicLinkDB
->count()
687 * Load public and private bookmarks from the DB
689 public function testReadPrivateDB()
692 $this->refDB
->countLinks(),
693 $this->privateLinkDB
->count()
698 * Save the bookmarks to the DB
700 public function testSave()
702 $testDB = new BookmarkFileService($this->conf
, $this->history
, true);
703 $dbSize = $testDB->count();
705 $bookmark = new Bookmark();
706 $testDB->add($bookmark);
708 $testDB = new BookmarkFileService($this->conf
, $this->history
, true);
709 $this->assertEquals($dbSize +
1, $testDB->count());
713 * Count existing bookmarks - public bookmarks hidden
715 public function testCountHiddenPublic()
717 $this->conf
->set('privacy.hide_public_links', true);
718 $linkDB = new BookmarkFileService($this->conf
, $this->history
, false);
720 $this->assertEquals(0, $linkDB->count());
724 * List the days for which bookmarks have been posted
726 public function testDays()
729 ['20100309', '20100310', '20121206', '20121207', '20130614', '20150310'],
730 $this->publicLinkDB
->days()
734 ['20100309', '20100310', '20121206', '20121207', '20130614', '20141125', '20150310'],
735 $this->privateLinkDB
->days()
740 * The URL corresponds to an existing entry in the DB
742 public function testGetKnownLinkFromURL()
744 $link = $this->publicLinkDB
->findByUrl('http://mediagoblin.org/');
746 $this->assertNotEquals(false, $link);
747 $this->assertContains(
748 'A free software media publishing platform',
749 $link->getDescription()
754 * The URL is not in the DB
756 public function testGetUnknownLinkFromURL()
760 $this->publicLinkDB
->findByUrl('http://dev.null')
767 public function testAllTags()
782 // The DB contains a link with `sTuff` and another one with `stuff` tag.
783 // They need to be grouped with the first case found - order by date DESC: `sTuff`.
787 $this->publicLinkDB
->bookmarksCountPerTag()
815 $this->privateLinkDB
->bookmarksCountPerTag()
831 $this->privateLinkDB
->bookmarksCountPerTag(['web'])
840 $this->privateLinkDB
->bookmarksCountPerTag(['web'], 'private')
845 * Test filter with string.
847 public function testFilterString()
849 $tags = 'dev cartoon';
850 $request = ['searchtags' => $tags];
853 count($this->privateLinkDB
->search($request, null, true))
858 * Test filter with array.
860 public function testFilterArray()
862 $tags = ['dev', 'cartoon'];
863 $request = ['searchtags' => $tags];
866 count($this->privateLinkDB
->search($request, null, true))
871 * Test hidden tags feature:
872 * tags starting with a dot '.' are only visible when logged in.
874 public function testHiddenTags()
877 $request = ['searchtags' => $tags];
880 count($this->privateLinkDB
->search($request, 'all', true))
885 count($this->publicLinkDB
->search($request, 'public', true))
890 * Test filterHash() with a valid smallhash.
892 public function testFilterHashValid()
894 $request = smallHash('20150310_114651');
897 $this->publicLinkDB
->findByHash($request)->getShortUrl()
899 $request = smallHash('20150310_114633' . 8);
902 $this->publicLinkDB
->findByHash($request)->getShortUrl()
907 * Test filterHash() with an invalid smallhash.
909 public function testFilterHashInValid1()
911 $this->expectException(BookmarkNotFoundException
::class);
914 $this->publicLinkDB
->findByHash($request);
918 * Test filterHash() with an empty smallhash.
920 public function testFilterHashInValid()
922 $this->expectException(BookmarkNotFoundException
::class);
924 $this->publicLinkDB
->findByHash('');
928 * Test linksCountPerTag all tags without filter.
929 * Equal occurrences should be sorted alphabetically.
931 public function testCountLinkPerTagAllNoFilter()
957 $tags = $this->privateLinkDB
->bookmarksCountPerTag();
959 $this->assertEquals($expected, $tags, var_export($tags, true));
963 * Test linksCountPerTag all tags with filter.
964 * Equal occurrences should be sorted alphabetically.
966 public function testCountLinkPerTagAllWithFilter()
979 $tags = $this->privateLinkDB
->bookmarksCountPerTag(['gnu']);
981 $this->assertEquals($expected, $tags, var_export($tags, true));
985 * Test linksCountPerTag public tags with filter.
986 * Equal occurrences should be sorted alphabetically.
988 public function testCountLinkPerTagPublicWithFilter()
1001 $tags = $this->privateLinkDB
->bookmarksCountPerTag(['gnu'], 'public');
1003 $this->assertEquals($expected, $tags, var_export($tags, true));
1007 * Test linksCountPerTag public tags with filter.
1008 * Equal occurrences should be sorted alphabetically.
1010 public function testCountLinkPerTagPrivateWithFilter()
1019 $tags = $this->privateLinkDB
->bookmarksCountPerTag(['dev'], 'private');
1021 $this->assertEquals($expected, $tags, var_export($tags, true));
1025 * Test linksCountPerTag public tags with filter.
1026 * Equal occurrences should be sorted alphabetically.
1028 public function testCountTagsNoMarkdown()
1048 'newTagToCount' => 1,
1055 $bookmark = new Bookmark();
1056 $bookmark->setTags(['newTagToCount', BookmarkMarkdownFormatter
::NO_MD_TAG
]);
1057 $this->privateLinkDB
->add($bookmark);
1059 $tags = $this->privateLinkDB
->bookmarksCountPerTag();
1061 $this->assertEquals($expected, $tags, var_export($tags, true));
1065 * Allows to test LinkDB's private methods
1068 * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html
1069 * http://stackoverflow.com/a/2798203
1071 protected static function getMethod($name)
1073 $class = new ReflectionClass('Shaarli\Bookmark\BookmarkFileService');
1074 $method = $class->getMethod($name);
1075 $method->setAccessible(true);