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 an entry which is not a bookmark instance
270 public function testAddNotABookmark()
272 $this->expectException(\Exception
::class);
273 $this->expectExceptionMessage('Provided data is invalid');
275 $this->privateLinkDB
->add(['title' => 'hi!']);
279 * Test add() method with a Bookmark already containing an ID
281 public function testAddWithId()
283 $this->expectException(\Exception
::class);
284 $this->expectExceptionMessage('This bookmarks already exists');
286 $bookmark = new Bookmark();
287 $bookmark->setId(43);
288 $this->privateLinkDB
->add($bookmark);
292 * Test set() method for a bookmark fully built
294 public function testSetFull()
296 $bookmark = $this->privateLinkDB
->get(42);
297 $bookmark->setUrl($url = 'https://domain.tld/index.php');
298 $bookmark->setShortUrl('abc');
299 $bookmark->setTitle($title = 'This a brand new bookmark');
300 $bookmark->setDescription($desc = 'It should be created and written');
301 $bookmark->setTags($tags = ['tag1', 'tagssss']);
302 $bookmark->setThumbnail($thumb = 'http://thumb.tld/dle.png');
303 $bookmark->setPrivate(true);
304 $bookmark->setSticky(true);
305 $bookmark->setCreated($created = DateTime
::createFromFormat('Ymd_His', '20190518_140354'));
306 $bookmark->setUpdated($updated = DateTime
::createFromFormat('Ymd_His', '20190518_150354'));
308 $this->privateLinkDB
->set($bookmark);
309 $bookmark = $this->privateLinkDB
->get(42);
310 $this->assertEquals(42, $bookmark->getId());
311 $this->assertEquals($url, $bookmark->getUrl());
312 $this->assertEquals('abc', $bookmark->getShortUrl());
313 $this->assertEquals($title, $bookmark->getTitle());
314 $this->assertEquals($desc, $bookmark->getDescription());
315 $this->assertEquals($tags, $bookmark->getTags());
316 $this->assertEquals($thumb, $bookmark->getThumbnail());
317 $this->assertTrue($bookmark->isPrivate());
318 $this->assertTrue($bookmark->isSticky());
319 $this->assertEquals($created, $bookmark->getCreated());
320 $this->assertTrue(new \
DateTime('5 seconds ago') < $bookmark->getUpdated());
323 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
325 $bookmark = $this->privateLinkDB
->get(42);
326 $this->assertEquals(42, $bookmark->getId());
327 $this->assertEquals($url, $bookmark->getUrl());
328 $this->assertEquals('abc', $bookmark->getShortUrl());
329 $this->assertEquals($title, $bookmark->getTitle());
330 $this->assertEquals($desc, $bookmark->getDescription());
331 $this->assertEquals($tags, $bookmark->getTags());
332 $this->assertEquals($thumb, $bookmark->getThumbnail());
333 $this->assertTrue($bookmark->isPrivate());
334 $this->assertTrue($bookmark->isSticky());
335 $this->assertEquals($created, $bookmark->getCreated());
336 $this->assertTrue(new \
DateTime('5 seconds ago') < $bookmark->getUpdated());
340 * Test set() method for a bookmark without any field set
342 public function testSetMinimal()
344 $bookmark = $this->privateLinkDB
->get(42);
345 $this->privateLinkDB
->set($bookmark);
347 $bookmark = $this->privateLinkDB
->get(42);
348 $this->assertEquals(42, $bookmark->getId());
349 $this->assertEquals('/shaare/WDWyig', $bookmark->getUrl());
350 $this->assertEquals('1eYJ1Q', $bookmark->getShortUrl());
351 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
352 $this->assertEquals('Used to test bookmarks reordering.', $bookmark->getDescription());
353 $this->assertEquals(['ut'], $bookmark->getTags());
354 $this->assertFalse($bookmark->getThumbnail());
355 $this->assertFalse($bookmark->isPrivate());
356 $this->assertFalse($bookmark->isSticky());
358 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20100310_101010'),
359 $bookmark->getCreated()
361 $this->assertTrue(new \
DateTime('5 seconds ago') < $bookmark->getUpdated());
364 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
366 $bookmark = $this->privateLinkDB
->get(42);
367 $this->assertEquals(42, $bookmark->getId());
368 $this->assertEquals('/shaare/WDWyig', $bookmark->getUrl());
369 $this->assertEquals('1eYJ1Q', $bookmark->getShortUrl());
370 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
371 $this->assertEquals('Used to test bookmarks reordering.', $bookmark->getDescription());
372 $this->assertEquals(['ut'], $bookmark->getTags());
373 $this->assertFalse($bookmark->getThumbnail());
374 $this->assertFalse($bookmark->isPrivate());
375 $this->assertFalse($bookmark->isSticky());
377 DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20100310_101010'),
378 $bookmark->getCreated()
380 $this->assertTrue(new \
DateTime('5 seconds ago') < $bookmark->getUpdated());
384 * Test set() method for a bookmark without any field set and without writing the data store
386 public function testSetMinimalNoWrite()
388 $bookmark = $this->privateLinkDB
->get(42);
389 $bookmark->setTitle($title = 'hi!');
390 $this->privateLinkDB
->set($bookmark, false);
392 $bookmark = $this->privateLinkDB
->get(42);
393 $this->assertEquals(42, $bookmark->getId());
394 $this->assertEquals($title, $bookmark->getTitle());
397 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
399 $bookmark = $this->privateLinkDB
->get(42);
400 $this->assertEquals(42, $bookmark->getId());
401 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
405 * Test set() method while logged out
407 public function testSetLoggedOut()
409 $this->expectException(\Exception
::class);
410 $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
412 $this->publicLinkDB
->set(new Bookmark());
416 * Test set() method with an entry which is not a bookmark instance
418 public function testSetNotABookmark()
420 $this->expectException(\Exception
::class);
421 $this->expectExceptionMessage('Provided data is invalid');
423 $this->privateLinkDB
->set(['title' => 'hi!']);
427 * Test set() method with a Bookmark without an ID defined.
429 public function testSetWithoutId()
431 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException
::class);
433 $bookmark = new Bookmark();
434 $this->privateLinkDB
->set($bookmark);
438 * Test set() method with a Bookmark with an unknow ID
440 public function testSetWithUnknownId()
442 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException
::class);
444 $bookmark = new Bookmark();
445 $bookmark->setId(666);
446 $this->privateLinkDB
->set($bookmark);
450 * Test addOrSet() method with a new ID
452 public function testAddOrSetNew()
454 $bookmark = new Bookmark();
455 $this->privateLinkDB
->addOrSet($bookmark);
457 $bookmark = $this->privateLinkDB
->get(43);
458 $this->assertEquals(43, $bookmark->getId());
461 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
463 $bookmark = $this->privateLinkDB
->get(43);
464 $this->assertEquals(43, $bookmark->getId());
468 * Test addOrSet() method with an existing ID
470 public function testAddOrSetExisting()
472 $bookmark = $this->privateLinkDB
->get(42);
473 $bookmark->setTitle($title = 'hi!');
474 $this->privateLinkDB
->addOrSet($bookmark);
476 $bookmark = $this->privateLinkDB
->get(42);
477 $this->assertEquals(42, $bookmark->getId());
478 $this->assertEquals($title, $bookmark->getTitle());
481 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
483 $bookmark = $this->privateLinkDB
->get(42);
484 $this->assertEquals(42, $bookmark->getId());
485 $this->assertEquals($title, $bookmark->getTitle());
489 * Test addOrSet() method while logged out
491 public function testAddOrSetLoggedOut()
493 $this->expectException(\Exception
::class);
494 $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
496 $this->publicLinkDB
->addOrSet(new Bookmark());
500 * Test addOrSet() method with an entry which is not a bookmark instance
502 public function testAddOrSetNotABookmark()
504 $this->expectException(\Exception
::class);
505 $this->expectExceptionMessage('Provided data is invalid');
507 $this->privateLinkDB
->addOrSet(['title' => 'hi!']);
511 * Test addOrSet() method for a bookmark without any field set and without writing the data store
513 public function testAddOrSetMinimalNoWrite()
515 $bookmark = $this->privateLinkDB
->get(42);
516 $bookmark->setTitle($title = 'hi!');
517 $this->privateLinkDB
->addOrSet($bookmark, false);
519 $bookmark = $this->privateLinkDB
->get(42);
520 $this->assertEquals(42, $bookmark->getId());
521 $this->assertEquals($title, $bookmark->getTitle());
524 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
526 $bookmark = $this->privateLinkDB
->get(42);
527 $this->assertEquals(42, $bookmark->getId());
528 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
532 * Test remove() method with an existing Bookmark
534 public function testRemoveExisting()
536 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException
::class);
538 $bookmark = $this->privateLinkDB
->get(42);
539 $this->privateLinkDB
->remove($bookmark);
543 $this->privateLinkDB
->get(42);
544 } catch (BookmarkNotFoundException
$e) {
547 $this->assertInstanceOf(BookmarkNotFoundException
::class, $exception);
550 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
552 $this->privateLinkDB
->get(42);
556 * Test remove() method while logged out
558 public function testRemoveLoggedOut()
560 $this->expectException(\Exception
::class);
561 $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
563 $bookmark = $this->privateLinkDB
->get(42);
564 $this->publicLinkDB
->remove($bookmark);
568 * Test remove() method with an entry which is not a bookmark instance
570 public function testRemoveNotABookmark()
572 $this->expectException(\Exception
::class);
573 $this->expectExceptionMessage('Provided data is invalid');
575 $this->privateLinkDB
->remove(['title' => 'hi!']);
579 * Test remove() method with a Bookmark with an unknown ID
581 public function testRemoveWithUnknownId()
583 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException
::class);
585 $bookmark = new Bookmark();
586 $bookmark->setId(666);
587 $this->privateLinkDB
->remove($bookmark);
591 * Test exists() method
593 public function testExists()
595 $this->assertTrue($this->privateLinkDB
->exists(42)); // public
596 $this->assertTrue($this->privateLinkDB
->exists(6)); // private
598 $this->assertTrue($this->privateLinkDB
->exists(42, BookmarkFilter
::$ALL));
599 $this->assertTrue($this->privateLinkDB
->exists(6, BookmarkFilter
::$ALL));
601 $this->assertTrue($this->privateLinkDB
->exists(42, BookmarkFilter
::$PUBLIC));
602 $this->assertFalse($this->privateLinkDB
->exists(6, BookmarkFilter
::$PUBLIC));
604 $this->assertFalse($this->privateLinkDB
->exists(42, BookmarkFilter
::$PRIVATE));
605 $this->assertTrue($this->privateLinkDB
->exists(6, BookmarkFilter
::$PRIVATE));
607 $this->assertTrue($this->publicLinkDB
->exists(42));
608 $this->assertFalse($this->publicLinkDB
->exists(6));
610 $this->assertTrue($this->publicLinkDB
->exists(42, BookmarkFilter
::$PUBLIC));
611 $this->assertFalse($this->publicLinkDB
->exists(6, BookmarkFilter
::$PUBLIC));
613 $this->assertFalse($this->publicLinkDB
->exists(42, BookmarkFilter
::$PRIVATE));
614 $this->assertTrue($this->publicLinkDB
->exists(6, BookmarkFilter
::$PRIVATE));
618 * Test initialize() method
620 public function testInitialize()
622 $dbSize = $this->privateLinkDB
->count();
623 $this->privateLinkDB
->initialize();
624 $this->assertEquals($dbSize +
3, $this->privateLinkDB
->count());
625 $this->assertStringStartsWith(
626 'Shaarli will automatically pick up the thumbnail for links to a variety of websites.',
627 $this->privateLinkDB
->get(43)->getDescription()
629 $this->assertStringStartsWith(
630 'Adding a shaare without entering a URL creates a text-only "note" post such as this one.',
631 $this->privateLinkDB
->get(44)->getDescription()
633 $this->assertStringStartsWith(
634 'Welcome to Shaarli!',
635 $this->privateLinkDB
->get(45)->getDescription()
640 * The following tests have been taken from the legacy LinkDB test and adapted
641 * to make sure that nothing have been broken in the migration process.
642 * They mostly cover search/filters. Some of them might be redundant with the previous ones.
645 * Attempt to instantiate a LinkDB whereas the datastore is not writable
647 public function testConstructDatastoreNotWriteable()
649 $this->expectException(\Shaarli\Bookmark\Exception\NotWritableDataStoreException
::class);
650 $this->expectExceptionMessageRegExp('#Couldn\'t load data from the data store file "null".*#');
652 $conf = new ConfigManager('tests/utils/config/configJson');
653 $conf->set('resource.datastore', 'null/store.db');
654 new BookmarkFileService($conf, $this->history
, $this->mutex
, true);
658 * The DB doesn't exist, ensure it is created with an empty datastore
660 public function testCheckDBNewLoggedIn()
662 unlink(self
::$testDatastore);
663 $this->assertFileNotExists(self
::$testDatastore);
664 new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
665 $this->assertFileExists(self
::$testDatastore);
667 // ensure the correct data has been written
668 $this->assertGreaterThan(0, filesize(self
::$testDatastore));
672 * The DB doesn't exist, but not logged in, ensure it initialized, but the file is not written
674 public function testCheckDBNewLoggedOut()
676 unlink(self
::$testDatastore);
677 $this->assertFileNotExists(self
::$testDatastore);
678 $db = new \
FakeBookmarkService($this->conf
, $this->history
, $this->mutex
, false);
679 $this->assertFileNotExists(self
::$testDatastore);
680 $this->assertInstanceOf(BookmarkArray
::class, $db->getBookmarks());
681 $this->assertCount(0, $db->getBookmarks());
685 * Load public bookmarks from the DB
687 public function testReadPublicDB()
690 $this->refDB
->countPublicLinks(),
691 $this->publicLinkDB
->count()
696 * Load public and private bookmarks from the DB
698 public function testReadPrivateDB()
701 $this->refDB
->countLinks(),
702 $this->privateLinkDB
->count()
707 * Save the bookmarks to the DB
709 public function testSave()
711 $testDB = new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
712 $dbSize = $testDB->count();
714 $bookmark = new Bookmark();
715 $testDB->add($bookmark);
717 $testDB = new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, true);
718 $this->assertEquals($dbSize +
1, $testDB->count());
722 * Count existing bookmarks - public bookmarks hidden
724 public function testCountHiddenPublic()
726 $this->conf
->set('privacy.hide_public_links', true);
727 $linkDB = new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, false);
729 $this->assertEquals(0, $linkDB->count());
733 * List the days for which bookmarks have been posted
735 public function testDays()
738 ['20100309', '20100310', '20121206', '20121207', '20130614', '20150310'],
739 $this->publicLinkDB
->days()
743 ['20100309', '20100310', '20121206', '20121207', '20130614', '20141125', '20150310'],
744 $this->privateLinkDB
->days()
749 * The URL corresponds to an existing entry in the DB
751 public function testGetKnownLinkFromURL()
753 $link = $this->publicLinkDB
->findByUrl('http://mediagoblin.org/');
755 $this->assertNotEquals(false, $link);
756 $this->assertContainsPolyfill(
757 'A free software media publishing platform',
758 $link->getDescription()
763 * The URL is not in the DB
765 public function testGetUnknownLinkFromURL()
769 $this->publicLinkDB
->findByUrl('http://dev.null')
776 public function testAllTags()
791 // The DB contains a link with `sTuff` and another one with `stuff` tag.
792 // They need to be grouped with the first case found - order by date DESC: `sTuff`.
796 $this->publicLinkDB
->bookmarksCountPerTag()
824 $this->privateLinkDB
->bookmarksCountPerTag()
840 $this->privateLinkDB
->bookmarksCountPerTag(['web'])
849 $this->privateLinkDB
->bookmarksCountPerTag(['web'], 'private')
854 * Test filter with string.
856 public function testFilterString()
858 $tags = 'dev cartoon';
859 $request = ['searchtags' => $tags];
862 count($this->privateLinkDB
->search($request, null, true))
867 * Test filter with array.
869 public function testFilterArray()
871 $tags = ['dev', 'cartoon'];
872 $request = ['searchtags' => $tags];
875 count($this->privateLinkDB
->search($request, null, true))
880 * Test hidden tags feature:
881 * tags starting with a dot '.' are only visible when logged in.
883 public function testHiddenTags()
886 $request = ['searchtags' => $tags];
889 count($this->privateLinkDB
->search($request, 'all', true))
894 count($this->publicLinkDB
->search($request, 'public', true))
899 * Test filterHash() with a valid smallhash.
901 public function testFilterHashValid()
903 $request = smallHash('20150310_114651');
906 $this->publicLinkDB
->findByHash($request)->getShortUrl()
908 $request = smallHash('20150310_114633' . 8);
911 $this->publicLinkDB
->findByHash($request)->getShortUrl()
916 * Test filterHash() with an invalid smallhash.
918 public function testFilterHashInValid1()
920 $this->expectException(BookmarkNotFoundException
::class);
923 $this->publicLinkDB
->findByHash($request);
927 * Test filterHash() with an empty smallhash.
929 public function testFilterHashInValid()
931 $this->expectException(BookmarkNotFoundException
::class);
933 $this->publicLinkDB
->findByHash('');
937 * Test linksCountPerTag all tags without filter.
938 * Equal occurrences should be sorted alphabetically.
940 public function testCountLinkPerTagAllNoFilter()
966 $tags = $this->privateLinkDB
->bookmarksCountPerTag();
968 $this->assertEquals($expected, $tags, var_export($tags, true));
972 * Test linksCountPerTag all tags with filter.
973 * Equal occurrences should be sorted alphabetically.
975 public function testCountLinkPerTagAllWithFilter()
988 $tags = $this->privateLinkDB
->bookmarksCountPerTag(['gnu']);
990 $this->assertEquals($expected, $tags, var_export($tags, true));
994 * Test linksCountPerTag public tags with filter.
995 * Equal occurrences should be sorted alphabetically.
997 public function testCountLinkPerTagPublicWithFilter()
1010 $tags = $this->privateLinkDB
->bookmarksCountPerTag(['gnu'], 'public');
1012 $this->assertEquals($expected, $tags, var_export($tags, true));
1016 * Test linksCountPerTag public tags with filter.
1017 * Equal occurrences should be sorted alphabetically.
1019 public function testCountLinkPerTagPrivateWithFilter()
1028 $tags = $this->privateLinkDB
->bookmarksCountPerTag(['dev'], 'private');
1030 $this->assertEquals($expected, $tags, var_export($tags, true));
1034 * Test linksCountPerTag public tags with filter.
1035 * Equal occurrences should be sorted alphabetically.
1037 public function testCountTagsNoMarkdown()
1057 'newTagToCount' => 1,
1064 $bookmark = new Bookmark();
1065 $bookmark->setTags(['newTagToCount', BookmarkMarkdownFormatter
::NO_MD_TAG
]);
1066 $this->privateLinkDB
->add($bookmark);
1068 $tags = $this->privateLinkDB
->bookmarksCountPerTag();
1070 $this->assertEquals($expected, $tags, var_export($tags, true));
1074 * Test filterDay while logged in
1076 public function testFilterDayLoggedIn(): void
1078 $bookmarks = $this->privateLinkDB
->filterDay('20121206');
1079 $expectedIds = [4, 9, 1, 0];
1081 static::assertCount(4, $bookmarks);
1082 foreach ($bookmarks as $bookmark) {
1083 $i = ($i ?? -1) +
1;
1084 static::assertSame($expectedIds[$i], $bookmark->getId());
1089 * Test filterDay while logged out
1091 public function testFilterDayLoggedOut(): void
1093 $bookmarks = $this->publicLinkDB
->filterDay('20121206');
1094 $expectedIds = [4, 9, 1];
1096 static::assertCount(3, $bookmarks);
1097 foreach ($bookmarks as $bookmark) {
1098 $i = ($i ?? -1) +
1;
1099 static::assertSame($expectedIds[$i], $bookmark->getId());
1104 * Allows to test LinkDB's private methods
1107 * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html
1108 * http://stackoverflow.com/a/2798203
1110 protected static function getMethod($name)
1112 $class = new ReflectionClass('Shaarli\Bookmark\BookmarkFileService');
1113 $method = $class->getMethod($name);
1114 $method->setAccessible(true);