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
;
20 * Unitary tests for LegacyLinkDBTest
22 class BookmarkFileServiceTest
extends TestCase
24 // datastore to test write operations
25 protected static $testDatastore = 'sandbox/datastore.php';
27 protected static $testConf = 'sandbox/config';
29 protected static $testUpdates = 'sandbox/updates.txt';
32 * @var ConfigManager instance.
37 * @var History instance.
42 * @var ReferenceLinkDB instance.
44 protected $refDB = null;
47 * @var BookmarkFileService public LinkDB instance.
49 protected $publicLinkDB = null;
52 * @var BookmarkFileService private LinkDB instance.
54 protected $privateLinkDB = null;
60 * Instantiates public and private LinkDBs with test data
62 * The reference datastore contains public and private bookmarks that
63 * will be used to test LinkDB's methods:
64 * - access filtering (public/private),
71 * Resets test data for each test
73 protected function setUp(): void
75 $this->mutex
= new NoMutex();
77 if (file_exists(self
::$testDatastore)) {
78 unlink(self
::$testDatastore);
81 if (file_exists(self
::$testConf .'.json.php')) {
82 unlink(self
::$testConf .'.json.php');
85 if (file_exists(self
::$testUpdates)) {
86 unlink(self
::$testUpdates);
89 copy('tests/utils/config/configJson.json.php', self
::$testConf .'.json.php');
90 $this->conf
= new ConfigManager(self
::$testConf);
91 $this->conf
->set('resource.datastore', self
::$testDatastore);
92 $this->conf
->set('resource.updates', self
::$testUpdates);
93 $this->refDB
= new \
ReferenceLinkDB();
94 $this->refDB
->write(self
::$testDatastore);
95 $this->history
= new History('sandbox/history.php');
96 $this->publicLinkDB
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, false);
97 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
101 * Test migrate() method with a legacy datastore.
103 public function testDatabaseMigration()
105 if (!defined('SHAARLI_VERSION')) {
106 define('SHAARLI_VERSION', 'dev');
109 $this->refDB
= new \
ReferenceLinkDB(true);
110 $this->refDB
->write(self
::$testDatastore);
111 $db = self
::getMethod('migrate');
112 $db->invokeArgs($this->privateLinkDB
, []);
114 $db = new \
FakeBookmarkService($this->conf
, $this->history
, $this->mutex
, true);
115 $this->assertInstanceOf(BookmarkArray
::class, $db->getBookmarks());
116 $this->assertEquals($this->refDB
->countLinks(), $db->count());
120 * Test get() method for a defined and saved bookmark
122 public function testGetDefinedSaved()
124 $bookmark = $this->privateLinkDB
->get(42);
125 $this->assertEquals(42, $bookmark->getId());
126 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
130 * Test get() method for a defined and not saved bookmark
132 public function testGetDefinedNotSaved()
134 $bookmark = new Bookmark();
135 $this->privateLinkDB
->add($bookmark);
136 $createdBookmark = $this->privateLinkDB
->get(43);
137 $this->assertEquals(43, $createdBookmark->getId());
138 $this->assertEmpty($createdBookmark->getDescription());
142 * Test get() method for an undefined bookmark
144 public function testGetUndefined()
146 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException
::class);
148 $this->privateLinkDB
->get(666);
152 * Test add() method for a bookmark fully built
154 public function testAddFull()
156 $bookmark = new Bookmark();
157 $bookmark->setUrl($url = 'https://domain.tld/index.php');
158 $bookmark->setShortUrl('abc');
159 $bookmark->setTitle($title = 'This a brand new bookmark');
160 $bookmark->setDescription($desc = 'It should be created and written');
161 $bookmark->setTags($tags = ['tag1', 'tagssss']);
162 $bookmark->setThumbnail($thumb = 'http://thumb.tld/dle.png');
163 $bookmark->setPrivate(true);
164 $bookmark->setSticky(true);
165 $bookmark->setCreated($created = DateTime
::createFromFormat('Ymd_His', '20190518_140354'));
166 $bookmark->setUpdated($updated = DateTime
::createFromFormat('Ymd_His', '20190518_150354'));
168 $this->privateLinkDB
->add($bookmark);
169 $bookmark = $this->privateLinkDB
->get(43);
170 $this->assertEquals(43, $bookmark->getId());
171 $this->assertEquals($url, $bookmark->getUrl());
172 $this->assertEquals('abc', $bookmark->getShortUrl());
173 $this->assertEquals($title, $bookmark->getTitle());
174 $this->assertEquals($desc, $bookmark->getDescription());
175 $this->assertEquals($tags, $bookmark->getTags());
176 $this->assertEquals($thumb, $bookmark->getThumbnail());
177 $this->assertTrue($bookmark->isPrivate());
178 $this->assertTrue($bookmark->isSticky());
179 $this->assertEquals($created, $bookmark->getCreated());
180 $this->assertEquals($updated, $bookmark->getUpdated());
183 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
185 $bookmark = $this->privateLinkDB
->get(43);
186 $this->assertEquals(43, $bookmark->getId());
187 $this->assertEquals($url, $bookmark->getUrl());
188 $this->assertEquals('abc', $bookmark->getShortUrl());
189 $this->assertEquals($title, $bookmark->getTitle());
190 $this->assertEquals($desc, $bookmark->getDescription());
191 $this->assertEquals($tags, $bookmark->getTags());
192 $this->assertEquals($thumb, $bookmark->getThumbnail());
193 $this->assertTrue($bookmark->isPrivate());
194 $this->assertTrue($bookmark->isSticky());
195 $this->assertEquals($created, $bookmark->getCreated());
196 $this->assertEquals($updated, $bookmark->getUpdated());
200 * Test add() method for a bookmark without any field set
202 public function testAddMinimal()
204 $bookmark = new Bookmark();
205 $this->privateLinkDB
->add($bookmark);
207 $bookmark = $this->privateLinkDB
->get(43);
208 $this->assertEquals(43, $bookmark->getId());
209 $this->assertRegExp('#/shaare/[\w\-]{6}#', $bookmark->getUrl());
210 $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl());
211 $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle());
212 $this->assertEmpty($bookmark->getDescription());
213 $this->assertEmpty($bookmark->getTags());
214 $this->assertEmpty($bookmark->getThumbnail());
215 $this->assertFalse($bookmark->isPrivate());
216 $this->assertFalse($bookmark->isSticky());
217 $this->assertTrue(new \
DateTime('5 seconds ago') < $bookmark->getCreated());
218 $this->assertNull($bookmark->getUpdated());
221 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
223 $bookmark = $this->privateLinkDB
->get(43);
224 $this->assertEquals(43, $bookmark->getId());
225 $this->assertRegExp('#/shaare/[\w\-]{6}#', $bookmark->getUrl());
226 $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl());
227 $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle());
228 $this->assertEmpty($bookmark->getDescription());
229 $this->assertEmpty($bookmark->getTags());
230 $this->assertEmpty($bookmark->getThumbnail());
231 $this->assertFalse($bookmark->isPrivate());
232 $this->assertFalse($bookmark->isSticky());
233 $this->assertTrue(new \
DateTime('5 seconds ago') < $bookmark->getCreated());
234 $this->assertNull($bookmark->getUpdated());
238 * Test add() method for a bookmark without any field set and without writing the data store
240 public function testAddMinimalNoWrite()
242 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException
::class);
244 $bookmark = new Bookmark();
245 $this->privateLinkDB
->add($bookmark, false);
247 $bookmark = $this->privateLinkDB
->get(43);
248 $this->assertEquals(43, $bookmark->getId());
251 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
253 $this->privateLinkDB
->get(43);
257 * Test add() method while logged out
259 public function testAddLoggedOut()
261 $this->expectException(\Exception
::class);
262 $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
264 $this->publicLinkDB
->add(new Bookmark());
268 * Test add() method with a Bookmark already containing an ID
270 public function testAddWithId()
272 $this->expectException(\Exception
::class);
273 $this->expectExceptionMessage('This bookmarks already exists');
275 $bookmark = new Bookmark();
276 $bookmark->setId(43);
277 $this->privateLinkDB
->add($bookmark);
281 * Test set() method for a bookmark fully built
283 public function testSetFull()
285 $bookmark = $this->privateLinkDB
->get(42);
286 $bookmark->setUrl($url = 'https://domain.tld/index.php');
287 $bookmark->setShortUrl('abc');
288 $bookmark->setTitle($title = 'This a brand new bookmark');
289 $bookmark->setDescription($desc = 'It should be created and written');
290 $bookmark->setTags($tags = ['tag1', 'tagssss']);
291 $bookmark->setThumbnail($thumb = 'http://thumb.tld/dle.png');
292 $bookmark->setPrivate(true);
293 $bookmark->setSticky(true);
294 $bookmark->setCreated($created = DateTime
::createFromFormat('Ymd_His', '20190518_140354'));
295 $bookmark->setUpdated($updated = DateTime
::createFromFormat('Ymd_His', '20190518_150354'));
297 $this->privateLinkDB
->set($bookmark);
298 $bookmark = $this->privateLinkDB
->get(42);
299 $this->assertEquals(42, $bookmark->getId());
300 $this->assertEquals($url, $bookmark->getUrl());
301 $this->assertEquals('abc', $bookmark->getShortUrl());
302 $this->assertEquals($title, $bookmark->getTitle());
303 $this->assertEquals($desc, $bookmark->getDescription());
304 $this->assertEquals($tags, $bookmark->getTags());
305 $this->assertEquals($thumb, $bookmark->getThumbnail());
306 $this->assertTrue($bookmark->isPrivate());
307 $this->assertTrue($bookmark->isSticky());
308 $this->assertEquals($created, $bookmark->getCreated());
309 $this->assertTrue(new \
DateTime('5 seconds ago') < $bookmark->getUpdated());
312 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
314 $bookmark = $this->privateLinkDB
->get(42);
315 $this->assertEquals(42, $bookmark->getId());
316 $this->assertEquals($url, $bookmark->getUrl());
317 $this->assertEquals('abc', $bookmark->getShortUrl());
318 $this->assertEquals($title, $bookmark->getTitle());
319 $this->assertEquals($desc, $bookmark->getDescription());
320 $this->assertEquals($tags, $bookmark->getTags());
321 $this->assertEquals($thumb, $bookmark->getThumbnail());
322 $this->assertTrue($bookmark->isPrivate());
323 $this->assertTrue($bookmark->isSticky());
324 $this->assertEquals($created, $bookmark->getCreated());
325 $this->assertTrue(new \
DateTime('5 seconds ago') < $bookmark->getUpdated());
329 * Test set() method for a bookmark without any field set
331 public function testSetMinimal()
333 $bookmark = $this->privateLinkDB
->get(42);
334 $this->privateLinkDB
->set($bookmark);
336 $bookmark = $this->privateLinkDB
->get(42);
337 $this->assertEquals(42, $bookmark->getId());
338 $this->assertEquals('/shaare/WDWyig', $bookmark->getUrl());
339 $this->assertEquals('1eYJ1Q', $bookmark->getShortUrl());
340 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
341 $this->assertEquals('Used to test bookmarks reordering.', $bookmark->getDescription());
342 $this->assertEquals(['ut'], $bookmark->getTags());
343 $this->assertFalse($bookmark->getThumbnail());
344 $this->assertFalse($bookmark->isPrivate());
345 $this->assertFalse($bookmark->isSticky());
347 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20100310_101010'),
348 $bookmark->getCreated()
350 $this->assertTrue(new \
DateTime('5 seconds ago') < $bookmark->getUpdated());
353 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
355 $bookmark = $this->privateLinkDB
->get(42);
356 $this->assertEquals(42, $bookmark->getId());
357 $this->assertEquals('/shaare/WDWyig', $bookmark->getUrl());
358 $this->assertEquals('1eYJ1Q', $bookmark->getShortUrl());
359 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
360 $this->assertEquals('Used to test bookmarks reordering.', $bookmark->getDescription());
361 $this->assertEquals(['ut'], $bookmark->getTags());
362 $this->assertFalse($bookmark->getThumbnail());
363 $this->assertFalse($bookmark->isPrivate());
364 $this->assertFalse($bookmark->isSticky());
366 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20100310_101010'),
367 $bookmark->getCreated()
369 $this->assertTrue(new \
DateTime('5 seconds ago') < $bookmark->getUpdated());
373 * Test set() method for a bookmark without any field set and without writing the data store
375 public function testSetMinimalNoWrite()
377 $bookmark = $this->privateLinkDB
->get(42);
378 $bookmark->setTitle($title = 'hi!');
379 $this->privateLinkDB
->set($bookmark, false);
381 $bookmark = $this->privateLinkDB
->get(42);
382 $this->assertEquals(42, $bookmark->getId());
383 $this->assertEquals($title, $bookmark->getTitle());
386 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
388 $bookmark = $this->privateLinkDB
->get(42);
389 $this->assertEquals(42, $bookmark->getId());
390 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
394 * Test set() method while logged out
396 public function testSetLoggedOut()
398 $this->expectException(\Exception
::class);
399 $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
401 $this->publicLinkDB
->set(new Bookmark());
405 * Test set() method with a Bookmark without an ID defined.
407 public function testSetWithoutId()
409 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException
::class);
411 $bookmark = new Bookmark();
412 $this->privateLinkDB
->set($bookmark);
416 * Test set() method with a Bookmark with an unknow ID
418 public function testSetWithUnknownId()
420 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException
::class);
422 $bookmark = new Bookmark();
423 $bookmark->setId(666);
424 $this->privateLinkDB
->set($bookmark);
428 * Test addOrSet() method with a new ID
430 public function testAddOrSetNew()
432 $bookmark = new Bookmark();
433 $this->privateLinkDB
->addOrSet($bookmark);
435 $bookmark = $this->privateLinkDB
->get(43);
436 $this->assertEquals(43, $bookmark->getId());
439 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
441 $bookmark = $this->privateLinkDB
->get(43);
442 $this->assertEquals(43, $bookmark->getId());
446 * Test addOrSet() method with an existing ID
448 public function testAddOrSetExisting()
450 $bookmark = $this->privateLinkDB
->get(42);
451 $bookmark->setTitle($title = 'hi!');
452 $this->privateLinkDB
->addOrSet($bookmark);
454 $bookmark = $this->privateLinkDB
->get(42);
455 $this->assertEquals(42, $bookmark->getId());
456 $this->assertEquals($title, $bookmark->getTitle());
459 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
461 $bookmark = $this->privateLinkDB
->get(42);
462 $this->assertEquals(42, $bookmark->getId());
463 $this->assertEquals($title, $bookmark->getTitle());
467 * Test addOrSet() method while logged out
469 public function testAddOrSetLoggedOut()
471 $this->expectException(\Exception
::class);
472 $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
474 $this->publicLinkDB
->addOrSet(new Bookmark());
478 * Test addOrSet() method for a bookmark without any field set and without writing the data store
480 public function testAddOrSetMinimalNoWrite()
482 $bookmark = $this->privateLinkDB
->get(42);
483 $bookmark->setTitle($title = 'hi!');
484 $this->privateLinkDB
->addOrSet($bookmark, false);
486 $bookmark = $this->privateLinkDB
->get(42);
487 $this->assertEquals(42, $bookmark->getId());
488 $this->assertEquals($title, $bookmark->getTitle());
491 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
493 $bookmark = $this->privateLinkDB
->get(42);
494 $this->assertEquals(42, $bookmark->getId());
495 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
499 * Test remove() method with an existing Bookmark
501 public function testRemoveExisting()
503 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException
::class);
505 $bookmark = $this->privateLinkDB
->get(42);
506 $this->privateLinkDB
->remove($bookmark);
510 $this->privateLinkDB
->get(42);
511 } catch (BookmarkNotFoundException
$e) {
514 $this->assertInstanceOf(BookmarkNotFoundException
::class, $exception);
517 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
519 $this->privateLinkDB
->get(42);
523 * Test remove() method while logged out
525 public function testRemoveLoggedOut()
527 $this->expectException(\Exception
::class);
528 $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
530 $bookmark = $this->privateLinkDB
->get(42);
531 $this->publicLinkDB
->remove($bookmark);
535 * Test remove() method with a Bookmark with an unknown ID
537 public function testRemoveWithUnknownId()
539 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException
::class);
541 $bookmark = new Bookmark();
542 $bookmark->setId(666);
543 $this->privateLinkDB
->remove($bookmark);
547 * Test exists() method
549 public function testExists()
551 $this->assertTrue($this->privateLinkDB
->exists(42)); // public
552 $this->assertTrue($this->privateLinkDB
->exists(6)); // private
554 $this->assertTrue($this->privateLinkDB
->exists(42, BookmarkFilter
::$ALL));
555 $this->assertTrue($this->privateLinkDB
->exists(6, BookmarkFilter
::$ALL));
557 $this->assertTrue($this->privateLinkDB
->exists(42, BookmarkFilter
::$PUBLIC));
558 $this->assertFalse($this->privateLinkDB
->exists(6, BookmarkFilter
::$PUBLIC));
560 $this->assertFalse($this->privateLinkDB
->exists(42, BookmarkFilter
::$PRIVATE));
561 $this->assertTrue($this->privateLinkDB
->exists(6, BookmarkFilter
::$PRIVATE));
563 $this->assertTrue($this->publicLinkDB
->exists(42));
564 $this->assertFalse($this->publicLinkDB
->exists(6));
566 $this->assertTrue($this->publicLinkDB
->exists(42, BookmarkFilter
::$PUBLIC));
567 $this->assertFalse($this->publicLinkDB
->exists(6, BookmarkFilter
::$PUBLIC));
569 $this->assertFalse($this->publicLinkDB
->exists(42, BookmarkFilter
::$PRIVATE));
570 $this->assertTrue($this->publicLinkDB
->exists(6, BookmarkFilter
::$PRIVATE));
574 * Test initialize() method
576 public function testInitialize()
578 $dbSize = $this->privateLinkDB
->count();
579 $this->privateLinkDB
->initialize();
580 $this->assertEquals($dbSize +
3, $this->privateLinkDB
->count());
581 $this->assertStringStartsWith(
582 'Shaarli will automatically pick up the thumbnail for links to a variety of websites.',
583 $this->privateLinkDB
->get(43)->getDescription()
585 $this->assertStringStartsWith(
586 'Adding a shaare without entering a URL creates a text-only "note" post such as this one.',
587 $this->privateLinkDB
->get(44)->getDescription()
589 $this->assertStringStartsWith(
590 'Welcome to Shaarli!',
591 $this->privateLinkDB
->get(45)->getDescription()
596 * The following tests have been taken from the legacy LinkDB test and adapted
597 * to make sure that nothing have been broken in the migration process.
598 * They mostly cover search/filters. Some of them might be redundant with the previous ones.
601 * Attempt to instantiate a LinkDB whereas the datastore is not writable
603 public function testConstructDatastoreNotWriteable()
605 $this->expectException(\Shaarli\Bookmark\Exception\NotWritableDataStoreException
::class);
606 $this->expectExceptionMessageRegExp('#Couldn\'t load data from the data store file "null".*#');
608 $conf = new ConfigManager('tests/utils/config/configJson');
609 $conf->set('resource.datastore', 'null/store.db');
610 new BookmarkFileService($conf, $this->history
, $this->mutex
, true);
614 * The DB doesn't exist, ensure it is created with an empty datastore
616 public function testCheckDBNewLoggedIn()
618 unlink(self
::$testDatastore);
619 $this->assertFileNotExists(self
::$testDatastore);
620 new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
621 $this->assertFileExists(self
::$testDatastore);
623 // ensure the correct data has been written
624 $this->assertGreaterThan(0, filesize(self
::$testDatastore));
628 * The DB doesn't exist, but not logged in, ensure it initialized, but the file is not written
630 public function testCheckDBNewLoggedOut()
632 unlink(self
::$testDatastore);
633 $this->assertFileNotExists(self
::$testDatastore);
634 $db = new \
FakeBookmarkService($this->conf
, $this->history
, $this->mutex
, false);
635 $this->assertFileNotExists(self
::$testDatastore);
636 $this->assertInstanceOf(BookmarkArray
::class, $db->getBookmarks());
637 $this->assertCount(0, $db->getBookmarks());
641 * Load public bookmarks from the DB
643 public function testReadPublicDB()
646 $this->refDB
->countPublicLinks(),
647 $this->publicLinkDB
->count()
652 * Load public and private bookmarks from the DB
654 public function testReadPrivateDB()
657 $this->refDB
->countLinks(),
658 $this->privateLinkDB
->count()
663 * Save the bookmarks to the DB
665 public function testSave()
667 $testDB = new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
668 $dbSize = $testDB->count();
670 $bookmark = new Bookmark();
671 $testDB->add($bookmark);
673 $testDB = new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
674 $this->assertEquals($dbSize +
1, $testDB->count());
678 * Count existing bookmarks - public bookmarks hidden
680 public function testCountHiddenPublic()
682 $this->conf
->set('privacy.hide_public_links', true);
683 $linkDB = new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, false);
685 $this->assertEquals(0, $linkDB->count());
689 * List the days for which bookmarks have been posted
691 public function testDays()
694 ['20100309', '20100310', '20121206', '20121207', '20130614', '20150310'],
695 $this->publicLinkDB
->days()
699 ['20100309', '20100310', '20121206', '20121207', '20130614', '20141125', '20150310'],
700 $this->privateLinkDB
->days()
705 * The URL corresponds to an existing entry in the DB
707 public function testGetKnownLinkFromURL()
709 $link = $this->publicLinkDB
->findByUrl('http://mediagoblin.org/');
711 $this->assertNotEquals(false, $link);
712 $this->assertContainsPolyfill(
713 'A free software media publishing platform',
714 $link->getDescription()
719 * The URL is not in the DB
721 public function testGetUnknownLinkFromURL()
725 $this->publicLinkDB
->findByUrl('http://dev.null')
732 public function testAllTags()
747 // The DB contains a link with `sTuff` and another one with `stuff` tag.
748 // They need to be grouped with the first case found - order by date DESC: `sTuff`.
756 $this->publicLinkDB
->bookmarksCountPerTag()
788 $this->privateLinkDB
->bookmarksCountPerTag()
804 $this->privateLinkDB
->bookmarksCountPerTag(['web'])
813 $this->privateLinkDB
->bookmarksCountPerTag(['web'], 'private')
818 * Test filter with string.
820 public function testFilterString()
822 $tags = 'dev cartoon';
823 $request = ['searchtags' => $tags];
826 count($this->privateLinkDB
->search($request, null, true))
831 * Test filter with array.
833 public function testFilterArray()
835 $tags = ['dev', 'cartoon'];
836 $request = ['searchtags' => $tags];
839 count($this->privateLinkDB
->search($request, null, true))
844 * Test hidden tags feature:
845 * tags starting with a dot '.' are only visible when logged in.
847 public function testHiddenTags()
850 $request = ['searchtags' => $tags];
853 count($this->privateLinkDB
->search($request, 'all', true))
858 count($this->publicLinkDB
->search($request, 'public', true))
863 * Test filterHash() with a valid smallhash.
865 public function testFilterHashValid()
867 $request = smallHash('20150310_114651');
870 $this->publicLinkDB
->findByHash($request)->getShortUrl()
872 $request = smallHash('20150310_114633' . 8);
875 $this->publicLinkDB
->findByHash($request)->getShortUrl()
880 * Test filterHash() with an invalid smallhash.
882 public function testFilterHashInValid1()
884 $this->expectException(BookmarkNotFoundException
::class);
887 $this->publicLinkDB
->findByHash($request);
891 * Test filterHash() with an empty smallhash.
893 public function testFilterHashInValid()
895 $this->expectException(BookmarkNotFoundException
::class);
897 $this->publicLinkDB
->findByHash('');
901 * Test filterHash() on a private bookmark while logged out.
903 public function testFilterHashPrivateWhileLoggedOut()
905 $this->expectException(\Exception
::class);
906 $this->expectExceptionMessage('Not authorized');
908 $hash = smallHash('20141125_084734' . 6);
910 $this->publicLinkDB
->findByHash($hash);
914 * Test filterHash() with private key.
916 public function testFilterHashWithPrivateKey()
918 $hash = smallHash('20141125_084734' . 6);
919 $privateKey = 'this is usually auto generated';
921 $bookmark = $this->privateLinkDB
->findByHash($hash);
922 $bookmark->addAdditionalContentEntry('private_key', $privateKey);
923 $this->privateLinkDB
->save();
925 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, false);
926 $bookmark = $this->privateLinkDB
->findByHash($hash, $privateKey);
928 static::assertSame(6, $bookmark->getId());
932 * Test linksCountPerTag all tags without filter.
933 * Equal occurrences should be sorted alphabetically.
935 public function testCountLinkPerTagAllNoFilter()
965 $tags = $this->privateLinkDB
->bookmarksCountPerTag();
967 $this->assertEquals($expected, $tags, var_export($tags, true));
971 * Test linksCountPerTag all tags with filter.
972 * Equal occurrences should be sorted alphabetically.
974 public function testCountLinkPerTagAllWithFilter()
987 $tags = $this->privateLinkDB
->bookmarksCountPerTag(['gnu']);
989 $this->assertEquals($expected, $tags, var_export($tags, true));
993 * Test linksCountPerTag public tags with filter.
994 * Equal occurrences should be sorted alphabetically.
996 public function testCountLinkPerTagPublicWithFilter()
1009 $tags = $this->privateLinkDB
->bookmarksCountPerTag(['gnu'], 'public');
1011 $this->assertEquals($expected, $tags, var_export($tags, true));
1015 * Test linksCountPerTag public tags with filter.
1016 * Equal occurrences should be sorted alphabetically.
1018 public function testCountLinkPerTagPrivateWithFilter()
1027 $tags = $this->privateLinkDB
->bookmarksCountPerTag(['dev'], 'private');
1029 $this->assertEquals($expected, $tags, var_export($tags, true));
1033 * Test linksCountPerTag public tags with filter.
1034 * Equal occurrences should be sorted alphabetically.
1036 public function testCountTagsNoMarkdown()
1056 'newTagToCount' => 1,
1063 'coding-style' => 1,
1067 $bookmark = new Bookmark();
1068 $bookmark->setTags(['newTagToCount', BookmarkMarkdownFormatter
::NO_MD_TAG
]);
1069 $this->privateLinkDB
->add($bookmark);
1071 $tags = $this->privateLinkDB
->bookmarksCountPerTag();
1073 $this->assertEquals($expected, $tags, var_export($tags, true));
1077 * Test filterDay while logged in
1079 public function testFilterDayLoggedIn(): void
1081 $bookmarks = $this->privateLinkDB
->filterDay('20121206');
1082 $expectedIds = [4, 9, 1, 0];
1084 static::assertCount(4, $bookmarks);
1085 foreach ($bookmarks as $bookmark) {
1086 $i = ($i ?? -1) +
1;
1087 static::assertSame($expectedIds[$i], $bookmark->getId());
1092 * Test filterDay while logged out
1094 public function testFilterDayLoggedOut(): void
1096 $bookmarks = $this->publicLinkDB
->filterDay('20121206');
1097 $expectedIds = [4, 9, 1];
1099 static::assertCount(3, $bookmarks);
1100 foreach ($bookmarks as $bookmark) {
1101 $i = ($i ?? -1) +
1;
1102 static::assertSame($expectedIds[$i], $bookmark->getId());
1107 * Allows to test LinkDB's private methods
1110 * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html
1111 * http://stackoverflow.com/a/2798203
1113 protected static function getMethod($name)
1115 $class = new ReflectionClass('Shaarli\Bookmark\BookmarkFileService');
1116 $method = $class->getMethod($name);
1117 $method->setAccessible(true);