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 * The URL corresponds to an existing entry in the DB
691 public function testGetKnownLinkFromURL()
693 $link = $this->publicLinkDB
->findByUrl('http://mediagoblin.org/');
695 $this->assertNotEquals(false, $link);
696 $this->assertContainsPolyfill(
697 'A free software media publishing platform',
698 $link->getDescription()
703 * The URL is not in the DB
705 public function testGetUnknownLinkFromURL()
709 $this->publicLinkDB
->findByUrl('http://dev.null')
716 public function testAllTags()
731 // The DB contains a link with `sTuff` and another one with `stuff` tag.
732 // They need to be grouped with the first case found - order by date DESC: `sTuff`.
740 $this->publicLinkDB
->bookmarksCountPerTag()
772 $this->privateLinkDB
->bookmarksCountPerTag()
788 $this->privateLinkDB
->bookmarksCountPerTag(['web'])
797 $this->privateLinkDB
->bookmarksCountPerTag(['web'], 'private')
802 * Test filter with string.
804 public function testFilterString()
806 $tags = 'dev cartoon';
807 $request = ['searchtags' => $tags];
810 count($this->privateLinkDB
->search($request, null, true))
815 * Test filter with array.
817 public function testFilterArray()
819 $tags = ['dev', 'cartoon'];
820 $request = ['searchtags' => $tags];
823 count($this->privateLinkDB
->search($request, null, true))
828 * Test hidden tags feature:
829 * tags starting with a dot '.' are only visible when logged in.
831 public function testHiddenTags()
834 $request = ['searchtags' => $tags];
837 count($this->privateLinkDB
->search($request, 'all', true))
842 count($this->publicLinkDB
->search($request, 'public', true))
847 * Test filterHash() with a valid smallhash.
849 public function testFilterHashValid()
851 $request = smallHash('20150310_114651');
854 $this->publicLinkDB
->findByHash($request)->getShortUrl()
856 $request = smallHash('20150310_114633' . 8);
859 $this->publicLinkDB
->findByHash($request)->getShortUrl()
864 * Test filterHash() with an invalid smallhash.
866 public function testFilterHashInValid1()
868 $this->expectException(BookmarkNotFoundException
::class);
871 $this->publicLinkDB
->findByHash($request);
875 * Test filterHash() with an empty smallhash.
877 public function testFilterHashInValid()
879 $this->expectException(BookmarkNotFoundException
::class);
881 $this->publicLinkDB
->findByHash('');
885 * Test filterHash() on a private bookmark while logged out.
887 public function testFilterHashPrivateWhileLoggedOut()
889 $this->expectException(BookmarkNotFoundException
::class);
890 $this->expectExceptionMessage('The link you are trying to reach does not exist or has been deleted');
892 $hash = smallHash('20141125_084734' . 6);
894 $this->publicLinkDB
->findByHash($hash);
898 * Test filterHash() with private key.
900 public function testFilterHashWithPrivateKey()
902 $hash = smallHash('20141125_084734' . 6);
903 $privateKey = 'this is usually auto generated';
905 $bookmark = $this->privateLinkDB
->findByHash($hash);
906 $bookmark->addAdditionalContentEntry('private_key', $privateKey);
907 $this->privateLinkDB
->save();
909 $this->privateLinkDB
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, false);
910 $bookmark = $this->privateLinkDB
->findByHash($hash, $privateKey);
912 static::assertSame(6, $bookmark->getId());
916 * Test linksCountPerTag all tags without filter.
917 * Equal occurrences should be sorted alphabetically.
919 public function testCountLinkPerTagAllNoFilter()
949 $tags = $this->privateLinkDB
->bookmarksCountPerTag();
951 $this->assertEquals($expected, $tags, var_export($tags, true));
955 * Test linksCountPerTag all tags with filter.
956 * Equal occurrences should be sorted alphabetically.
958 public function testCountLinkPerTagAllWithFilter()
971 $tags = $this->privateLinkDB
->bookmarksCountPerTag(['gnu']);
973 $this->assertEquals($expected, $tags, var_export($tags, true));
977 * Test linksCountPerTag public tags with filter.
978 * Equal occurrences should be sorted alphabetically.
980 public function testCountLinkPerTagPublicWithFilter()
993 $tags = $this->privateLinkDB
->bookmarksCountPerTag(['gnu'], 'public');
995 $this->assertEquals($expected, $tags, var_export($tags, true));
999 * Test linksCountPerTag public tags with filter.
1000 * Equal occurrences should be sorted alphabetically.
1002 public function testCountLinkPerTagPrivateWithFilter()
1011 $tags = $this->privateLinkDB
->bookmarksCountPerTag(['dev'], 'private');
1013 $this->assertEquals($expected, $tags, var_export($tags, true));
1017 * Test linksCountPerTag public tags with filter.
1018 * Equal occurrences should be sorted alphabetically.
1020 public function testCountTagsNoMarkdown()
1040 'newTagToCount' => 1,
1047 'coding-style' => 1,
1051 $bookmark = new Bookmark();
1052 $bookmark->setTags(['newTagToCount', BookmarkMarkdownFormatter
::NO_MD_TAG
]);
1053 $this->privateLinkDB
->add($bookmark);
1055 $tags = $this->privateLinkDB
->bookmarksCountPerTag();
1057 $this->assertEquals($expected, $tags, var_export($tags, true));
1061 * Test find by dates in the middle of the datastore (sorted by dates) with a single bookmark as a result.
1063 public function testFilterByDateMidTimePeriodSingleBookmark(): void
1065 $bookmarks = $this->privateLinkDB
->findByDate(
1066 DateTime
::createFromFormat('Ymd_His', '20121206_150000'),
1067 DateTime
::createFromFormat('Ymd_His', '20121206_160000'),
1072 static::assertCount(1, $bookmarks);
1074 static::assertSame(9, $bookmarks[0]->getId());
1075 static::assertEquals(DateTime
::createFromFormat('Ymd_His', '20121206_142300'), $before);
1076 static::assertEquals(DateTime
::createFromFormat('Ymd_His', '20121206_172539'), $after);
1080 * Test find by dates in the middle of the datastore (sorted by dates) with a multiple bookmarks as a result.
1082 public function testFilterByDateMidTimePeriodMultipleBookmarks(): void
1084 $bookmarks = $this->privateLinkDB
->findByDate(
1085 DateTime
::createFromFormat('Ymd_His', '20121206_150000'),
1086 DateTime
::createFromFormat('Ymd_His', '20121206_180000'),
1091 static::assertCount(2, $bookmarks);
1093 static::assertSame(1, $bookmarks[0]->getId());
1094 static::assertSame(9, $bookmarks[1]->getId());
1095 static::assertEquals(DateTime
::createFromFormat('Ymd_His', '20121206_142300'), $before);
1096 static::assertEquals(DateTime
::createFromFormat('Ymd_His', '20121206_182539'), $after);
1100 * Test find by dates at the end of the datastore (sorted by dates).
1102 public function testFilterByDateLastTimePeriod(): void
1104 $after = new DateTime();
1105 $bookmarks = $this->privateLinkDB
->findByDate(
1106 DateTime
::createFromFormat('Ymd_His', '20150310_114640'),
1107 DateTime
::createFromFormat('Ymd_His', '20450101_010101'),
1112 static::assertCount(1, $bookmarks);
1114 static::assertSame(41, $bookmarks[0]->getId());
1115 static::assertEquals(DateTime
::createFromFormat('Ymd_His', '20150310_114633'), $before);
1116 static::assertNull($after);
1120 * Test find by dates at the beginning of the datastore (sorted by dates).
1122 public function testFilterByDateFirstTimePeriod(): void
1124 $before = new DateTime();
1125 $bookmarks = $this->privateLinkDB
->findByDate(
1126 DateTime
::createFromFormat('Ymd_His', '20000101_101010'),
1127 DateTime
::createFromFormat('Ymd_His', '20100309_110000'),
1132 static::assertCount(1, $bookmarks);
1134 static::assertSame(11, $bookmarks[0]->getId());
1135 static::assertNull($before);
1136 static::assertEquals(DateTime
::createFromFormat('Ymd_His', '20100310_101010'), $after);
1140 * Test getLatest with a sticky bookmark: it should be ignored and return the latest by creation date instead.
1142 public function testGetLatestWithSticky(): void
1144 $bookmark = $this->publicLinkDB
->getLatest();
1146 static::assertSame(41, $bookmark->getId());
1150 * Test getLatest with a sticky bookmark: it should be ignored and return the latest by creation date instead.
1152 public function testGetLatestEmptyDatastore(): void
1154 unlink($this->conf
->get('resource.datastore'));
1155 $this->publicLinkDB
= new BookmarkFileService($this->conf
, $this->history
, $this->mutex
, false);
1157 $bookmark = $this->publicLinkDB
->getLatest();
1159 static::assertNull($bookmark);
1163 * Allows to test LinkDB's private methods
1166 * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html
1167 * http://stackoverflow.com/a/2798203
1169 protected static function getMethod($name)
1171 $class = new ReflectionClass('Shaarli\Bookmark\BookmarkFileService');
1172 $method = $class->getMethod($name);
1173 $method->setAccessible(true);