6 namespace Shaarli\Bookmark
;
9 use malkusch\lock\mutex\NoMutex
;
13 use Shaarli\Bookmark\Exception\BookmarkNotFoundException
;
14 use Shaarli\Config\ConfigManager
;
15 use Shaarli\Formatter\BookmarkMarkdownFormatter
;
17 use Shaarli\Plugin\PluginManager
;
21 * Unitary tests for LegacyLinkDBTest
23 class BookmarkFileServiceTest
extends TestCase
25 // datastore to test write operations
26 protected static $testDatastore = 'sandbox/datastore.php';
28 protected static $testConf = 'sandbox/config';
30 protected static $testUpdates = 'sandbox/updates.txt';
33 * @var ConfigManager instance.
38 * @var History instance.
43 * @var ReferenceLinkDB instance.
45 protected $refDB = null;
48 * @var BookmarkFileService public LinkDB instance.
50 protected $publicLinkDB = null;
53 * @var BookmarkFileService private LinkDB instance.
55 protected $privateLinkDB = null;
60 /** @var PluginManager */
61 protected $pluginManager;
64 * Instantiates public and private LinkDBs with test data
66 * The reference datastore contains public and private bookmarks that
67 * will be used to test LinkDB's methods:
68 * - access filtering (public/private),
75 * Resets test data for each test
77 protected function setUp(): void
79 $this->mutex
= new NoMutex();
81 if (file_exists(self
::$testDatastore)) {
82 unlink(self
::$testDatastore);
85 if (file_exists(self
::$testConf .'.json.php')) {
86 unlink(self
::$testConf .'.json.php');
89 if (file_exists(self
::$testUpdates)) {
90 unlink(self
::$testUpdates);
93 copy('tests/utils/config/configJson.json.php', self
::$testConf .'.json.php');
94 $this->conf
= new ConfigManager(self
::$testConf);
95 $this->conf
->set('resource.datastore', self
::$testDatastore);
96 $this->conf
->set('resource.updates', self
::$testUpdates);
97 $this->refDB
= new \
ReferenceLinkDB();
98 $this->refDB
->write(self
::$testDatastore);
99 $this->history
= new History('sandbox/history.php');
100 $this->pluginManager
= new PluginManager($this->conf
);
101 $this->publicLinkDB
= new BookmarkFileService(
103 $this->pluginManager
,
108 $this->privateLinkDB
= new BookmarkFileService(
110 $this->pluginManager
,
118 * Test migrate() method with a legacy datastore.
120 public function testDatabaseMigration()
122 if (!defined('SHAARLI_VERSION')) {
123 define('SHAARLI_VERSION', 'dev');
126 $this->refDB
= new \
ReferenceLinkDB(true);
127 $this->refDB
->write(self
::$testDatastore);
128 $db = self
::getMethod('migrate');
129 $db->invokeArgs($this->privateLinkDB
, []);
131 $db = new \
FakeBookmarkService(
133 $this->pluginManager
,
138 $this->assertInstanceOf(BookmarkArray
::class, $db->getBookmarks());
139 $this->assertEquals($this->refDB
->countLinks(), $db->count());
143 * Test get() method for a defined and saved bookmark
145 public function testGetDefinedSaved()
147 $bookmark = $this->privateLinkDB
->get(42);
148 $this->assertEquals(42, $bookmark->getId());
149 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
153 * Test get() method for a defined and not saved bookmark
155 public function testGetDefinedNotSaved()
157 $bookmark = new Bookmark();
158 $this->privateLinkDB
->add($bookmark);
159 $createdBookmark = $this->privateLinkDB
->get(43);
160 $this->assertEquals(43, $createdBookmark->getId());
161 $this->assertEmpty($createdBookmark->getDescription());
165 * Test get() method for an undefined bookmark
167 public function testGetUndefined()
169 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException
::class);
171 $this->privateLinkDB
->get(666);
175 * Test add() method for a bookmark fully built
177 public function testAddFull()
179 $bookmark = new Bookmark();
180 $bookmark->setUrl($url = 'https://domain.tld/index.php');
181 $bookmark->setShortUrl('abc');
182 $bookmark->setTitle($title = 'This a brand new bookmark');
183 $bookmark->setDescription($desc = 'It should be created and written');
184 $bookmark->setTags($tags = ['tag1', 'tagssss']);
185 $bookmark->setThumbnail($thumb = 'http://thumb.tld/dle.png');
186 $bookmark->setPrivate(true);
187 $bookmark->setSticky(true);
188 $bookmark->setCreated($created = DateTime
::createFromFormat('Ymd_His', '20190518_140354'));
189 $bookmark->setUpdated($updated = DateTime
::createFromFormat('Ymd_His', '20190518_150354'));
191 $this->privateLinkDB
->add($bookmark);
192 $bookmark = $this->privateLinkDB
->get(43);
193 $this->assertEquals(43, $bookmark->getId());
194 $this->assertEquals($url, $bookmark->getUrl());
195 $this->assertEquals('abc', $bookmark->getShortUrl());
196 $this->assertEquals($title, $bookmark->getTitle());
197 $this->assertEquals($desc, $bookmark->getDescription());
198 $this->assertEquals($tags, $bookmark->getTags());
199 $this->assertEquals($thumb, $bookmark->getThumbnail());
200 $this->assertTrue($bookmark->isPrivate());
201 $this->assertTrue($bookmark->isSticky());
202 $this->assertEquals($created, $bookmark->getCreated());
203 $this->assertEquals($updated, $bookmark->getUpdated());
206 $this->privateLinkDB
= new \
FakeBookmarkService(
208 $this->pluginManager
,
214 $bookmark = $this->privateLinkDB
->get(43);
215 $this->assertEquals(43, $bookmark->getId());
216 $this->assertEquals($url, $bookmark->getUrl());
217 $this->assertEquals('abc', $bookmark->getShortUrl());
218 $this->assertEquals($title, $bookmark->getTitle());
219 $this->assertEquals($desc, $bookmark->getDescription());
220 $this->assertEquals($tags, $bookmark->getTags());
221 $this->assertEquals($thumb, $bookmark->getThumbnail());
222 $this->assertTrue($bookmark->isPrivate());
223 $this->assertTrue($bookmark->isSticky());
224 $this->assertEquals($created, $bookmark->getCreated());
225 $this->assertEquals($updated, $bookmark->getUpdated());
229 * Test add() method for a bookmark without any field set
231 public function testAddMinimal()
233 $bookmark = new Bookmark();
234 $this->privateLinkDB
->add($bookmark);
236 $bookmark = $this->privateLinkDB
->get(43);
237 $this->assertEquals(43, $bookmark->getId());
238 $this->assertRegExp('#/shaare/[\w\-]{6}#', $bookmark->getUrl());
239 $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl());
240 $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle());
241 $this->assertEmpty($bookmark->getDescription());
242 $this->assertEmpty($bookmark->getTags());
243 $this->assertEmpty($bookmark->getThumbnail());
244 $this->assertFalse($bookmark->isPrivate());
245 $this->assertFalse($bookmark->isSticky());
246 $this->assertTrue(new \
DateTime('5 seconds ago') < $bookmark->getCreated());
247 $this->assertNull($bookmark->getUpdated());
250 $this->privateLinkDB
= new BookmarkFileService(
252 $this->pluginManager
,
258 $bookmark = $this->privateLinkDB
->get(43);
259 $this->assertEquals(43, $bookmark->getId());
260 $this->assertRegExp('#/shaare/[\w\-]{6}#', $bookmark->getUrl());
261 $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl());
262 $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle());
263 $this->assertEmpty($bookmark->getDescription());
264 $this->assertEmpty($bookmark->getTags());
265 $this->assertEmpty($bookmark->getThumbnail());
266 $this->assertFalse($bookmark->isPrivate());
267 $this->assertFalse($bookmark->isSticky());
268 $this->assertTrue(new \
DateTime('5 seconds ago') < $bookmark->getCreated());
269 $this->assertNull($bookmark->getUpdated());
273 * Test add() method for a bookmark without any field set and without writing the data store
275 public function testAddMinimalNoWrite()
277 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException
::class);
279 $bookmark = new Bookmark();
280 $this->privateLinkDB
->add($bookmark, false);
282 $bookmark = $this->privateLinkDB
->get(43);
283 $this->assertEquals(43, $bookmark->getId());
286 $this->privateLinkDB
= new BookmarkFileService(
288 $this->pluginManager
,
294 $this->privateLinkDB
->get(43);
298 * Test add() method while logged out
300 public function testAddLoggedOut()
302 $this->expectException(\Exception
::class);
303 $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
305 $this->publicLinkDB
->add(new Bookmark());
309 * Test add() method with a Bookmark already containing an ID
311 public function testAddWithId()
313 $this->expectException(\Exception
::class);
314 $this->expectExceptionMessage('This bookmarks already exists');
316 $bookmark = new Bookmark();
317 $bookmark->setId(43);
318 $this->privateLinkDB
->add($bookmark);
322 * Test set() method for a bookmark fully built
324 public function testSetFull()
326 $bookmark = $this->privateLinkDB
->get(42);
327 $bookmark->setUrl($url = 'https://domain.tld/index.php');
328 $bookmark->setShortUrl('abc');
329 $bookmark->setTitle($title = 'This a brand new bookmark');
330 $bookmark->setDescription($desc = 'It should be created and written');
331 $bookmark->setTags($tags = ['tag1', 'tagssss']);
332 $bookmark->setThumbnail($thumb = 'http://thumb.tld/dle.png');
333 $bookmark->setPrivate(true);
334 $bookmark->setSticky(true);
335 $bookmark->setCreated($created = DateTime
::createFromFormat('Ymd_His', '20190518_140354'));
336 $bookmark->setUpdated($updated = DateTime
::createFromFormat('Ymd_His', '20190518_150354'));
338 $this->privateLinkDB
->set($bookmark);
339 $bookmark = $this->privateLinkDB
->get(42);
340 $this->assertEquals(42, $bookmark->getId());
341 $this->assertEquals($url, $bookmark->getUrl());
342 $this->assertEquals('abc', $bookmark->getShortUrl());
343 $this->assertEquals($title, $bookmark->getTitle());
344 $this->assertEquals($desc, $bookmark->getDescription());
345 $this->assertEquals($tags, $bookmark->getTags());
346 $this->assertEquals($thumb, $bookmark->getThumbnail());
347 $this->assertTrue($bookmark->isPrivate());
348 $this->assertTrue($bookmark->isSticky());
349 $this->assertEquals($created, $bookmark->getCreated());
350 $this->assertTrue(new \
DateTime('5 seconds ago') < $bookmark->getUpdated());
353 $this->privateLinkDB
= new BookmarkFileService(
355 $this->pluginManager
,
361 $bookmark = $this->privateLinkDB
->get(42);
362 $this->assertEquals(42, $bookmark->getId());
363 $this->assertEquals($url, $bookmark->getUrl());
364 $this->assertEquals('abc', $bookmark->getShortUrl());
365 $this->assertEquals($title, $bookmark->getTitle());
366 $this->assertEquals($desc, $bookmark->getDescription());
367 $this->assertEquals($tags, $bookmark->getTags());
368 $this->assertEquals($thumb, $bookmark->getThumbnail());
369 $this->assertTrue($bookmark->isPrivate());
370 $this->assertTrue($bookmark->isSticky());
371 $this->assertEquals($created, $bookmark->getCreated());
372 $this->assertTrue(new \
DateTime('5 seconds ago') < $bookmark->getUpdated());
376 * Test set() method for a bookmark without any field set
378 public function testSetMinimal()
380 $bookmark = $this->privateLinkDB
->get(42);
381 $this->privateLinkDB
->set($bookmark);
383 $bookmark = $this->privateLinkDB
->get(42);
384 $this->assertEquals(42, $bookmark->getId());
385 $this->assertEquals('/shaare/WDWyig', $bookmark->getUrl());
386 $this->assertEquals('1eYJ1Q', $bookmark->getShortUrl());
387 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
388 $this->assertEquals('Used to test bookmarks reordering.', $bookmark->getDescription());
389 $this->assertEquals(['ut'], $bookmark->getTags());
390 $this->assertFalse($bookmark->getThumbnail());
391 $this->assertFalse($bookmark->isPrivate());
392 $this->assertFalse($bookmark->isSticky());
394 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20100310_101010'),
395 $bookmark->getCreated()
397 $this->assertTrue(new \
DateTime('5 seconds ago') < $bookmark->getUpdated());
400 $this->privateLinkDB
= new BookmarkFileService(
402 $this->pluginManager
,
408 $bookmark = $this->privateLinkDB
->get(42);
409 $this->assertEquals(42, $bookmark->getId());
410 $this->assertEquals('/shaare/WDWyig', $bookmark->getUrl());
411 $this->assertEquals('1eYJ1Q', $bookmark->getShortUrl());
412 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
413 $this->assertEquals('Used to test bookmarks reordering.', $bookmark->getDescription());
414 $this->assertEquals(['ut'], $bookmark->getTags());
415 $this->assertFalse($bookmark->getThumbnail());
416 $this->assertFalse($bookmark->isPrivate());
417 $this->assertFalse($bookmark->isSticky());
419 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20100310_101010'),
420 $bookmark->getCreated()
422 $this->assertTrue(new \
DateTime('5 seconds ago') < $bookmark->getUpdated());
426 * Test set() method for a bookmark without any field set and without writing the data store
428 public function testSetMinimalNoWrite()
430 $bookmark = $this->privateLinkDB
->get(42);
431 $bookmark->setTitle($title = 'hi!');
432 $this->privateLinkDB
->set($bookmark, false);
434 $bookmark = $this->privateLinkDB
->get(42);
435 $this->assertEquals(42, $bookmark->getId());
436 $this->assertEquals($title, $bookmark->getTitle());
439 $this->privateLinkDB
= new BookmarkFileService(
441 $this->pluginManager
,
447 $bookmark = $this->privateLinkDB
->get(42);
448 $this->assertEquals(42, $bookmark->getId());
449 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
453 * Test set() method while logged out
455 public function testSetLoggedOut()
457 $this->expectException(\Exception
::class);
458 $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
460 $this->publicLinkDB
->set(new Bookmark());
464 * Test set() method with a Bookmark without an ID defined.
466 public function testSetWithoutId()
468 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException
::class);
470 $bookmark = new Bookmark();
471 $this->privateLinkDB
->set($bookmark);
475 * Test set() method with a Bookmark with an unknow ID
477 public function testSetWithUnknownId()
479 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException
::class);
481 $bookmark = new Bookmark();
482 $bookmark->setId(666);
483 $this->privateLinkDB
->set($bookmark);
487 * Test addOrSet() method with a new ID
489 public function testAddOrSetNew()
491 $bookmark = new Bookmark();
492 $this->privateLinkDB
->addOrSet($bookmark);
494 $bookmark = $this->privateLinkDB
->get(43);
495 $this->assertEquals(43, $bookmark->getId());
498 $this->privateLinkDB
= new BookmarkFileService(
500 $this->pluginManager
,
506 $bookmark = $this->privateLinkDB
->get(43);
507 $this->assertEquals(43, $bookmark->getId());
511 * Test addOrSet() method with an existing ID
513 public function testAddOrSetExisting()
515 $bookmark = $this->privateLinkDB
->get(42);
516 $bookmark->setTitle($title = 'hi!');
517 $this->privateLinkDB
->addOrSet($bookmark);
519 $bookmark = $this->privateLinkDB
->get(42);
520 $this->assertEquals(42, $bookmark->getId());
521 $this->assertEquals($title, $bookmark->getTitle());
524 $this->privateLinkDB
= new BookmarkFileService(
526 $this->pluginManager
,
532 $bookmark = $this->privateLinkDB
->get(42);
533 $this->assertEquals(42, $bookmark->getId());
534 $this->assertEquals($title, $bookmark->getTitle());
538 * Test addOrSet() method while logged out
540 public function testAddOrSetLoggedOut()
542 $this->expectException(\Exception
::class);
543 $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
545 $this->publicLinkDB
->addOrSet(new Bookmark());
549 * Test addOrSet() method for a bookmark without any field set and without writing the data store
551 public function testAddOrSetMinimalNoWrite()
553 $bookmark = $this->privateLinkDB
->get(42);
554 $bookmark->setTitle($title = 'hi!');
555 $this->privateLinkDB
->addOrSet($bookmark, false);
557 $bookmark = $this->privateLinkDB
->get(42);
558 $this->assertEquals(42, $bookmark->getId());
559 $this->assertEquals($title, $bookmark->getTitle());
562 $this->privateLinkDB
= new BookmarkFileService(
564 $this->pluginManager
,
570 $bookmark = $this->privateLinkDB
->get(42);
571 $this->assertEquals(42, $bookmark->getId());
572 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
576 * Test remove() method with an existing Bookmark
578 public function testRemoveExisting()
580 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException
::class);
582 $bookmark = $this->privateLinkDB
->get(42);
583 $this->privateLinkDB
->remove($bookmark);
587 $this->privateLinkDB
->get(42);
588 } catch (BookmarkNotFoundException
$e) {
591 $this->assertInstanceOf(BookmarkNotFoundException
::class, $exception);
594 $this->privateLinkDB
= new BookmarkFileService(
596 $this->pluginManager
,
602 $this->privateLinkDB
->get(42);
606 * Test remove() method while logged out
608 public function testRemoveLoggedOut()
610 $this->expectException(\Exception
::class);
611 $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
613 $bookmark = $this->privateLinkDB
->get(42);
614 $this->publicLinkDB
->remove($bookmark);
618 * Test remove() method with a Bookmark with an unknown ID
620 public function testRemoveWithUnknownId()
622 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException
::class);
624 $bookmark = new Bookmark();
625 $bookmark->setId(666);
626 $this->privateLinkDB
->remove($bookmark);
630 * Test exists() method
632 public function testExists()
634 $this->assertTrue($this->privateLinkDB
->exists(42)); // public
635 $this->assertTrue($this->privateLinkDB
->exists(6)); // private
637 $this->assertTrue($this->privateLinkDB
->exists(42, BookmarkFilter
::$ALL));
638 $this->assertTrue($this->privateLinkDB
->exists(6, BookmarkFilter
::$ALL));
640 $this->assertTrue($this->privateLinkDB
->exists(42, BookmarkFilter
::$PUBLIC));
641 $this->assertFalse($this->privateLinkDB
->exists(6, BookmarkFilter
::$PUBLIC));
643 $this->assertFalse($this->privateLinkDB
->exists(42, BookmarkFilter
::$PRIVATE));
644 $this->assertTrue($this->privateLinkDB
->exists(6, BookmarkFilter
::$PRIVATE));
646 $this->assertTrue($this->publicLinkDB
->exists(42));
647 $this->assertFalse($this->publicLinkDB
->exists(6));
649 $this->assertTrue($this->publicLinkDB
->exists(42, BookmarkFilter
::$PUBLIC));
650 $this->assertFalse($this->publicLinkDB
->exists(6, BookmarkFilter
::$PUBLIC));
652 $this->assertFalse($this->publicLinkDB
->exists(42, BookmarkFilter
::$PRIVATE));
653 $this->assertTrue($this->publicLinkDB
->exists(6, BookmarkFilter
::$PRIVATE));
657 * Test initialize() method
659 public function testInitialize()
661 $dbSize = $this->privateLinkDB
->count();
662 $this->privateLinkDB
->initialize();
663 $this->assertEquals($dbSize +
3, $this->privateLinkDB
->count());
664 $this->assertStringStartsWith(
665 'Shaarli will automatically pick up the thumbnail for links to a variety of websites.',
666 $this->privateLinkDB
->get(43)->getDescription()
668 $this->assertStringStartsWith(
669 'Adding a shaare without entering a URL creates a text-only "note" post such as this one.',
670 $this->privateLinkDB
->get(44)->getDescription()
672 $this->assertStringStartsWith(
673 'Welcome to Shaarli!',
674 $this->privateLinkDB
->get(45)->getDescription()
679 * The following tests have been taken from the legacy LinkDB test and adapted
680 * to make sure that nothing have been broken in the migration process.
681 * They mostly cover search/filters. Some of them might be redundant with the previous ones.
684 * Attempt to instantiate a LinkDB whereas the datastore is not writable
686 public function testConstructDatastoreNotWriteable()
688 $this->expectException(\Shaarli\Bookmark\Exception\NotWritableDataStoreException
::class);
689 $this->expectExceptionMessageRegExp('#Couldn\'t load data from the data store file "null".*#');
691 $conf = new ConfigManager('tests/utils/config/configJson');
692 $conf->set('resource.datastore', 'null/store.db');
693 new BookmarkFileService($conf, $this->pluginManager
, $this->history
, $this->mutex
, true);
697 * The DB doesn't exist, ensure it is created with an empty datastore
699 public function testCheckDBNewLoggedIn()
701 unlink(self
::$testDatastore);
702 $this->assertFileNotExists(self
::$testDatastore);
703 new BookmarkFileService($this->conf
, $this->pluginManager
, $this->history
, $this->mutex
, true);
704 $this->assertFileExists(self
::$testDatastore);
706 // ensure the correct data has been written
707 $this->assertGreaterThan(0, filesize(self
::$testDatastore));
711 * The DB doesn't exist, but not logged in, ensure it initialized, but the file is not written
713 public function testCheckDBNewLoggedOut()
715 unlink(self
::$testDatastore);
716 $this->assertFileNotExists(self
::$testDatastore);
717 $db = new \
FakeBookmarkService($this->conf
, $this->pluginManager
, $this->history
, $this->mutex
, false);
718 $this->assertFileNotExists(self
::$testDatastore);
719 $this->assertInstanceOf(BookmarkArray
::class, $db->getBookmarks());
720 $this->assertCount(0, $db->getBookmarks());
724 * Load public bookmarks from the DB
726 public function testReadPublicDB()
729 $this->refDB
->countPublicLinks(),
730 $this->publicLinkDB
->count()
735 * Load public and private bookmarks from the DB
737 public function testReadPrivateDB()
740 $this->refDB
->countLinks(),
741 $this->privateLinkDB
->count()
746 * Save the bookmarks to the DB
748 public function testSave()
750 $testDB = new BookmarkFileService($this->conf
, $this->pluginManager
, $this->history
, $this->mutex
, true);
751 $dbSize = $testDB->count();
753 $bookmark = new Bookmark();
754 $testDB->add($bookmark);
756 $testDB = new BookmarkFileService($this->conf
, $this->pluginManager
, $this->history
, $this->mutex
, true);
757 $this->assertEquals($dbSize +
1, $testDB->count());
761 * Count existing bookmarks - public bookmarks hidden
763 public function testCountHiddenPublic()
765 $this->conf
->set('privacy.hide_public_links', true);
766 $linkDB = new BookmarkFileService($this->conf
, $this->pluginManager
, $this->history
, $this->mutex
, false);
768 $this->assertEquals(0, $linkDB->count());
772 * The URL corresponds to an existing entry in the DB
774 public function testGetKnownLinkFromURL()
776 $link = $this->publicLinkDB
->findByUrl('http://mediagoblin.org/');
778 $this->assertNotEquals(false, $link);
779 $this->assertContainsPolyfill(
780 'A free software media publishing platform',
781 $link->getDescription()
786 * The URL is not in the DB
788 public function testGetUnknownLinkFromURL()
792 $this->publicLinkDB
->findByUrl('http://dev.null')
799 public function testAllTags()
814 // The DB contains a link with `sTuff` and another one with `stuff` tag.
815 // They need to be grouped with the first case found - order by date DESC: `sTuff`.
823 $this->publicLinkDB
->bookmarksCountPerTag()
855 $this->privateLinkDB
->bookmarksCountPerTag()
871 $this->privateLinkDB
->bookmarksCountPerTag(['web'])
880 $this->privateLinkDB
->bookmarksCountPerTag(['web'], 'private')
885 * Test filter with string.
887 public function testFilterString()
889 $tags = 'dev cartoon';
890 $request = ['searchtags' => $tags];
893 count($this->privateLinkDB
->search($request, null, true)->getBookmarks())
898 * Test filter with array.
900 public function testFilterArray()
902 $tags = ['dev', 'cartoon'];
903 $request = ['searchtags' => $tags];
906 count($this->privateLinkDB
->search($request, null, true)->getBookmarks())
911 * Test hidden tags feature:
912 * tags starting with a dot '.' are only visible when logged in.
914 public function testHiddenTags()
917 $request = ['searchtags' => $tags];
920 count($this->privateLinkDB
->search($request, 'all', true)->getBookmarks())
925 count($this->publicLinkDB
->search($request, 'public', true)->getBookmarks())
930 * Test filterHash() with a valid smallhash.
932 public function testFilterHashValid()
934 $request = smallHash('20150310_114651');
937 $this->publicLinkDB
->findByHash($request)->getShortUrl()
939 $request = smallHash('20150310_114633' . 8);
942 $this->publicLinkDB
->findByHash($request)->getShortUrl()
947 * Test filterHash() with an invalid smallhash.
949 public function testFilterHashInValid1()
951 $this->expectException(BookmarkNotFoundException
::class);
954 $this->publicLinkDB
->findByHash($request);
958 * Test filterHash() with an empty smallhash.
960 public function testFilterHashInValid()
962 $this->expectException(BookmarkNotFoundException
::class);
964 $this->publicLinkDB
->findByHash('');
968 * Test filterHash() on a private bookmark while logged out.
970 public function testFilterHashPrivateWhileLoggedOut()
972 $this->expectException(BookmarkNotFoundException
::class);
973 $this->expectExceptionMessage('The link you are trying to reach does not exist or has been deleted');
975 $hash = smallHash('20141125_084734' . 6);
977 $this->publicLinkDB
->findByHash($hash);
981 * Test filterHash() with private key.
983 public function testFilterHashWithPrivateKey()
985 $hash = smallHash('20141125_084734' . 6);
986 $privateKey = 'this is usually auto generated';
988 $bookmark = $this->privateLinkDB
->findByHash($hash);
989 $bookmark->addAdditionalContentEntry('private_key', $privateKey);
990 $this->privateLinkDB
->save();
992 $this->privateLinkDB
= new BookmarkFileService(
994 $this->pluginManager
,
999 $bookmark = $this->privateLinkDB
->findByHash($hash, $privateKey);
1001 static::assertSame(6, $bookmark->getId());
1005 * Test linksCountPerTag all tags without filter.
1006 * Equal occurrences should be sorted alphabetically.
1008 public function testCountLinkPerTagAllNoFilter()
1034 'coding-style' => 1,
1038 $tags = $this->privateLinkDB
->bookmarksCountPerTag();
1040 $this->assertEquals($expected, $tags, var_export($tags, true));
1044 * Test linksCountPerTag all tags with filter.
1045 * Equal occurrences should be sorted alphabetically.
1047 public function testCountLinkPerTagAllWithFilter()
1060 $tags = $this->privateLinkDB
->bookmarksCountPerTag(['gnu']);
1062 $this->assertEquals($expected, $tags, var_export($tags, true));
1066 * Test linksCountPerTag public tags with filter.
1067 * Equal occurrences should be sorted alphabetically.
1069 public function testCountLinkPerTagPublicWithFilter()
1082 $tags = $this->privateLinkDB
->bookmarksCountPerTag(['gnu'], 'public');
1084 $this->assertEquals($expected, $tags, var_export($tags, true));
1088 * Test linksCountPerTag public tags with filter.
1089 * Equal occurrences should be sorted alphabetically.
1091 public function testCountLinkPerTagPrivateWithFilter()
1100 $tags = $this->privateLinkDB
->bookmarksCountPerTag(['dev'], 'private');
1102 $this->assertEquals($expected, $tags, var_export($tags, true));
1106 * Test linksCountPerTag public tags with filter.
1107 * Equal occurrences should be sorted alphabetically.
1109 public function testCountTagsNoMarkdown()
1129 'newTagToCount' => 1,
1136 'coding-style' => 1,
1140 $bookmark = new Bookmark();
1141 $bookmark->setTags(['newTagToCount', BookmarkMarkdownFormatter
::NO_MD_TAG
]);
1142 $this->privateLinkDB
->add($bookmark);
1144 $tags = $this->privateLinkDB
->bookmarksCountPerTag();
1146 $this->assertEquals($expected, $tags, var_export($tags, true));
1150 * Test find by dates in the middle of the datastore (sorted by dates) with a single bookmark as a result.
1152 public function testFilterByDateMidTimePeriodSingleBookmark(): void
1154 $bookmarks = $this->privateLinkDB
->findByDate(
1155 DateTime
::createFromFormat('Ymd_His', '20121206_150000'),
1156 DateTime
::createFromFormat('Ymd_His', '20121206_160000'),
1161 static::assertCount(1, $bookmarks);
1163 static::assertSame(9, $bookmarks[0]->getId());
1164 static::assertEquals(DateTime
::createFromFormat('Ymd_His', '20121206_142300'), $before);
1165 static::assertEquals(DateTime
::createFromFormat('Ymd_His', '20121206_172539'), $after);
1169 * Test find by dates in the middle of the datastore (sorted by dates) with a multiple bookmarks as a result.
1171 public function testFilterByDateMidTimePeriodMultipleBookmarks(): void
1173 $bookmarks = $this->privateLinkDB
->findByDate(
1174 DateTime
::createFromFormat('Ymd_His', '20121206_150000'),
1175 DateTime
::createFromFormat('Ymd_His', '20121206_180000'),
1180 static::assertCount(2, $bookmarks);
1182 static::assertSame(1, $bookmarks[0]->getId());
1183 static::assertSame(9, $bookmarks[1]->getId());
1184 static::assertEquals(DateTime
::createFromFormat('Ymd_His', '20121206_142300'), $before);
1185 static::assertEquals(DateTime
::createFromFormat('Ymd_His', '20121206_182539'), $after);
1189 * Test find by dates at the end of the datastore (sorted by dates).
1191 public function testFilterByDateLastTimePeriod(): void
1193 $after = new DateTime();
1194 $bookmarks = $this->privateLinkDB
->findByDate(
1195 DateTime
::createFromFormat('Ymd_His', '20150310_114640'),
1196 DateTime
::createFromFormat('Ymd_His', '20450101_010101'),
1201 static::assertCount(1, $bookmarks);
1203 static::assertSame(41, $bookmarks[0]->getId());
1204 static::assertEquals(DateTime
::createFromFormat('Ymd_His', '20150310_114633'), $before);
1205 static::assertNull($after);
1209 * Test find by dates at the beginning of the datastore (sorted by dates).
1211 public function testFilterByDateFirstTimePeriod(): void
1213 $before = new DateTime();
1214 $bookmarks = $this->privateLinkDB
->findByDate(
1215 DateTime
::createFromFormat('Ymd_His', '20000101_101010'),
1216 DateTime
::createFromFormat('Ymd_His', '20100309_110000'),
1221 static::assertCount(1, $bookmarks);
1223 static::assertSame(11, $bookmarks[0]->getId());
1224 static::assertNull($before);
1225 static::assertEquals(DateTime
::createFromFormat('Ymd_His', '20100310_101010'), $after);
1229 * Test getLatest with a sticky bookmark: it should be ignored and return the latest by creation date instead.
1231 public function testGetLatestWithSticky(): void
1233 $bookmark = $this->publicLinkDB
->getLatest();
1235 static::assertSame(41, $bookmark->getId());
1239 * Test getLatest with a sticky bookmark: it should be ignored and return the latest by creation date instead.
1241 public function testGetLatestEmptyDatastore(): void
1243 unlink($this->conf
->get('resource.datastore'));
1244 $this->publicLinkDB
= new BookmarkFileService(
1246 $this->pluginManager
,
1252 $bookmark = $this->publicLinkDB
->getLatest();
1254 static::assertNull($bookmark);
1258 * Allows to test LinkDB's private methods
1261 * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html
1262 * http://stackoverflow.com/a/2798203
1264 protected static function getMethod($name)
1266 $class = new ReflectionClass('Shaarli\Bookmark\BookmarkFileService');
1267 $method = $class->getMethod($name);
1268 $method->setAccessible(true);