]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/bookmark/BookmarkFileServiceTest.php
postLink: change relative path to absolute path
[github/shaarli/Shaarli.git] / tests / bookmark / BookmarkFileServiceTest.php
1 <?php
2 /**
3 * Link datastore tests
4 */
5
6 namespace Shaarli\Bookmark;
7
8 use DateTime;
9 use PHPUnit\Framework\TestCase;
10 use ReferenceLinkDB;
11 use ReflectionClass;
12 use Shaarli;
13 use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
14 use Shaarli\Config\ConfigManager;
15 use Shaarli\Formatter\BookmarkMarkdownFormatter;
16 use Shaarli\History;
17
18 /**
19 * Unitary tests for LegacyLinkDBTest
20 */
21 class BookmarkFileServiceTest extends TestCase
22 {
23 // datastore to test write operations
24 protected static $testDatastore = 'sandbox/datastore.php';
25
26 protected static $testConf = 'sandbox/config';
27
28 protected static $testUpdates = 'sandbox/updates.txt';
29
30 /**
31 * @var ConfigManager instance.
32 */
33 protected $conf;
34
35 /**
36 * @var History instance.
37 */
38 protected $history;
39
40 /**
41 * @var ReferenceLinkDB instance.
42 */
43 protected $refDB = null;
44
45 /**
46 * @var BookmarkFileService public LinkDB instance.
47 */
48 protected $publicLinkDB = null;
49
50 /**
51 * @var BookmarkFileService private LinkDB instance.
52 */
53 protected $privateLinkDB = null;
54
55 /**
56 * Instantiates public and private LinkDBs with test data
57 *
58 * The reference datastore contains public and private bookmarks that
59 * will be used to test LinkDB's methods:
60 * - access filtering (public/private),
61 * - link searches:
62 * - by day,
63 * - by tag,
64 * - by text,
65 * - etc.
66 *
67 * Resets test data for each test
68 */
69 protected function setUp()
70 {
71 if (file_exists(self::$testDatastore)) {
72 unlink(self::$testDatastore);
73 }
74
75 if (file_exists(self::$testConf .'.json.php')) {
76 unlink(self::$testConf .'.json.php');
77 }
78
79 if (file_exists(self::$testUpdates)) {
80 unlink(self::$testUpdates);
81 }
82
83 copy('tests/utils/config/configJson.json.php', self::$testConf .'.json.php');
84 $this->conf = new ConfigManager(self::$testConf);
85 $this->conf->set('resource.datastore', self::$testDatastore);
86 $this->conf->set('resource.updates', self::$testUpdates);
87 $this->refDB = new \ReferenceLinkDB();
88 $this->refDB->write(self::$testDatastore);
89 $this->history = new History('sandbox/history.php');
90 $this->publicLinkDB = new BookmarkFileService($this->conf, $this->history, false);
91 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
92 }
93
94 /**
95 * Test migrate() method with a legacy datastore.
96 */
97 public function testDatabaseMigration()
98 {
99 if (!defined('SHAARLI_VERSION')) {
100 define('SHAARLI_VERSION', 'dev');
101 }
102
103 $this->refDB = new \ReferenceLinkDB(true);
104 $this->refDB->write(self::$testDatastore);
105 $db = self::getMethod('migrate');
106 $db->invokeArgs($this->privateLinkDB, []);
107
108 $db = new \FakeBookmarkService($this->conf, $this->history, true);
109 $this->assertInstanceOf(BookmarkArray::class, $db->getBookmarks());
110 $this->assertEquals($this->refDB->countLinks(), $db->count());
111 }
112
113 /**
114 * Test get() method for a defined and saved bookmark
115 */
116 public function testGetDefinedSaved()
117 {
118 $bookmark = $this->privateLinkDB->get(42);
119 $this->assertEquals(42, $bookmark->getId());
120 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
121 }
122
123 /**
124 * Test get() method for a defined and not saved bookmark
125 */
126 public function testGetDefinedNotSaved()
127 {
128 $bookmark = new Bookmark();
129 $this->privateLinkDB->add($bookmark);
130 $createdBookmark = $this->privateLinkDB->get(43);
131 $this->assertEquals(43, $createdBookmark->getId());
132 $this->assertEmpty($createdBookmark->getDescription());
133 }
134
135 /**
136 * Test get() method for an undefined bookmark
137 *
138 * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
139 */
140 public function testGetUndefined()
141 {
142 $this->privateLinkDB->get(666);
143 }
144
145 /**
146 * Test add() method for a bookmark fully built
147 */
148 public function testAddFull()
149 {
150 $bookmark = new Bookmark();
151 $bookmark->setUrl($url = 'https://domain.tld/index.php');
152 $bookmark->setShortUrl('abc');
153 $bookmark->setTitle($title = 'This a brand new bookmark');
154 $bookmark->setDescription($desc = 'It should be created and written');
155 $bookmark->setTags($tags = ['tag1', 'tagssss']);
156 $bookmark->setThumbnail($thumb = 'http://thumb.tld/dle.png');
157 $bookmark->setPrivate(true);
158 $bookmark->setSticky(true);
159 $bookmark->setCreated($created = DateTime::createFromFormat('Ymd_His', '20190518_140354'));
160 $bookmark->setUpdated($updated = DateTime::createFromFormat('Ymd_His', '20190518_150354'));
161
162 $this->privateLinkDB->add($bookmark);
163 $bookmark = $this->privateLinkDB->get(43);
164 $this->assertEquals(43, $bookmark->getId());
165 $this->assertEquals($url, $bookmark->getUrl());
166 $this->assertEquals('abc', $bookmark->getShortUrl());
167 $this->assertEquals($title, $bookmark->getTitle());
168 $this->assertEquals($desc, $bookmark->getDescription());
169 $this->assertEquals($tags, $bookmark->getTags());
170 $this->assertEquals($thumb, $bookmark->getThumbnail());
171 $this->assertTrue($bookmark->isPrivate());
172 $this->assertTrue($bookmark->isSticky());
173 $this->assertEquals($created, $bookmark->getCreated());
174 $this->assertEquals($updated, $bookmark->getUpdated());
175
176 // reload from file
177 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
178
179 $bookmark = $this->privateLinkDB->get(43);
180 $this->assertEquals(43, $bookmark->getId());
181 $this->assertEquals($url, $bookmark->getUrl());
182 $this->assertEquals('abc', $bookmark->getShortUrl());
183 $this->assertEquals($title, $bookmark->getTitle());
184 $this->assertEquals($desc, $bookmark->getDescription());
185 $this->assertEquals($tags, $bookmark->getTags());
186 $this->assertEquals($thumb, $bookmark->getThumbnail());
187 $this->assertTrue($bookmark->isPrivate());
188 $this->assertTrue($bookmark->isSticky());
189 $this->assertEquals($created, $bookmark->getCreated());
190 $this->assertEquals($updated, $bookmark->getUpdated());
191 }
192
193 /**
194 * Test add() method for a bookmark without any field set
195 */
196 public function testAddMinimal()
197 {
198 $bookmark = new Bookmark();
199 $this->privateLinkDB->add($bookmark);
200
201 $bookmark = $this->privateLinkDB->get(43);
202 $this->assertEquals(43, $bookmark->getId());
203 $this->assertRegExp('/\?[\w\-]{6}/', $bookmark->getUrl());
204 $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl());
205 $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle());
206 $this->assertEmpty($bookmark->getDescription());
207 $this->assertEmpty($bookmark->getTags());
208 $this->assertEmpty($bookmark->getThumbnail());
209 $this->assertFalse($bookmark->isPrivate());
210 $this->assertFalse($bookmark->isSticky());
211 $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getCreated());
212 $this->assertNull($bookmark->getUpdated());
213
214 // reload from file
215 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
216
217 $bookmark = $this->privateLinkDB->get(43);
218 $this->assertEquals(43, $bookmark->getId());
219 $this->assertRegExp('/\?[\w\-]{6}/', $bookmark->getUrl());
220 $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl());
221 $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle());
222 $this->assertEmpty($bookmark->getDescription());
223 $this->assertEmpty($bookmark->getTags());
224 $this->assertEmpty($bookmark->getThumbnail());
225 $this->assertFalse($bookmark->isPrivate());
226 $this->assertFalse($bookmark->isSticky());
227 $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getCreated());
228 $this->assertNull($bookmark->getUpdated());
229 }
230
231 /**
232 * Test add() method for a bookmark without any field set and without writing the data store
233 *
234 * @expectedExceptionMessage Shaarli\Bookmark\Exception\BookmarkNotFoundException
235 */
236 public function testAddMinimalNoWrite()
237 {
238 $bookmark = new Bookmark();
239 $this->privateLinkDB->add($bookmark);
240
241 $bookmark = $this->privateLinkDB->get(43);
242 $this->assertEquals(43, $bookmark->getId());
243
244 // reload from file
245 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
246
247 $this->privateLinkDB->get(43);
248 }
249
250 /**
251 * Test add() method while logged out
252 *
253 * @expectedException \Exception
254 * @expectedExceptionMessage You're not authorized to alter the datastore
255 */
256 public function testAddLoggedOut()
257 {
258 $this->publicLinkDB->add(new Bookmark());
259 }
260
261 /**
262 * Test add() method with an entry which is not a bookmark instance
263 *
264 * @expectedException \Exception
265 * @expectedExceptionMessage Provided data is invalid
266 */
267 public function testAddNotABookmark()
268 {
269 $this->privateLinkDB->add(['title' => 'hi!']);
270 }
271
272 /**
273 * Test add() method with a Bookmark already containing an ID
274 *
275 * @expectedException \Exception
276 * @expectedExceptionMessage This bookmarks already exists
277 */
278 public function testAddWithId()
279 {
280 $bookmark = new Bookmark();
281 $bookmark->setId(43);
282 $this->privateLinkDB->add($bookmark);
283 }
284
285 /**
286 * Test set() method for a bookmark fully built
287 */
288 public function testSetFull()
289 {
290 $bookmark = $this->privateLinkDB->get(42);
291 $bookmark->setUrl($url = 'https://domain.tld/index.php');
292 $bookmark->setShortUrl('abc');
293 $bookmark->setTitle($title = 'This a brand new bookmark');
294 $bookmark->setDescription($desc = 'It should be created and written');
295 $bookmark->setTags($tags = ['tag1', 'tagssss']);
296 $bookmark->setThumbnail($thumb = 'http://thumb.tld/dle.png');
297 $bookmark->setPrivate(true);
298 $bookmark->setSticky(true);
299 $bookmark->setCreated($created = DateTime::createFromFormat('Ymd_His', '20190518_140354'));
300 $bookmark->setUpdated($updated = DateTime::createFromFormat('Ymd_His', '20190518_150354'));
301
302 $this->privateLinkDB->set($bookmark);
303 $bookmark = $this->privateLinkDB->get(42);
304 $this->assertEquals(42, $bookmark->getId());
305 $this->assertEquals($url, $bookmark->getUrl());
306 $this->assertEquals('abc', $bookmark->getShortUrl());
307 $this->assertEquals($title, $bookmark->getTitle());
308 $this->assertEquals($desc, $bookmark->getDescription());
309 $this->assertEquals($tags, $bookmark->getTags());
310 $this->assertEquals($thumb, $bookmark->getThumbnail());
311 $this->assertTrue($bookmark->isPrivate());
312 $this->assertTrue($bookmark->isSticky());
313 $this->assertEquals($created, $bookmark->getCreated());
314 $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated());
315
316 // reload from file
317 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
318
319 $bookmark = $this->privateLinkDB->get(42);
320 $this->assertEquals(42, $bookmark->getId());
321 $this->assertEquals($url, $bookmark->getUrl());
322 $this->assertEquals('abc', $bookmark->getShortUrl());
323 $this->assertEquals($title, $bookmark->getTitle());
324 $this->assertEquals($desc, $bookmark->getDescription());
325 $this->assertEquals($tags, $bookmark->getTags());
326 $this->assertEquals($thumb, $bookmark->getThumbnail());
327 $this->assertTrue($bookmark->isPrivate());
328 $this->assertTrue($bookmark->isSticky());
329 $this->assertEquals($created, $bookmark->getCreated());
330 $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated());
331 }
332
333 /**
334 * Test set() method for a bookmark without any field set
335 */
336 public function testSetMinimal()
337 {
338 $bookmark = $this->privateLinkDB->get(42);
339 $this->privateLinkDB->set($bookmark);
340
341 $bookmark = $this->privateLinkDB->get(42);
342 $this->assertEquals(42, $bookmark->getId());
343 $this->assertEquals('?WDWyig', $bookmark->getUrl());
344 $this->assertEquals('1eYJ1Q', $bookmark->getShortUrl());
345 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
346 $this->assertEquals('Used to test bookmarks reordering.', $bookmark->getDescription());
347 $this->assertEquals(['ut'], $bookmark->getTags());
348 $this->assertFalse($bookmark->getThumbnail());
349 $this->assertFalse($bookmark->isPrivate());
350 $this->assertFalse($bookmark->isSticky());
351 $this->assertEquals(
352 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20100310_101010'),
353 $bookmark->getCreated()
354 );
355 $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated());
356
357 // reload from file
358 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
359
360 $bookmark = $this->privateLinkDB->get(42);
361 $this->assertEquals(42, $bookmark->getId());
362 $this->assertEquals('?WDWyig', $bookmark->getUrl());
363 $this->assertEquals('1eYJ1Q', $bookmark->getShortUrl());
364 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
365 $this->assertEquals('Used to test bookmarks reordering.', $bookmark->getDescription());
366 $this->assertEquals(['ut'], $bookmark->getTags());
367 $this->assertFalse($bookmark->getThumbnail());
368 $this->assertFalse($bookmark->isPrivate());
369 $this->assertFalse($bookmark->isSticky());
370 $this->assertEquals(
371 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20100310_101010'),
372 $bookmark->getCreated()
373 );
374 $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated());
375 }
376
377 /**
378 * Test set() method for a bookmark without any field set and without writing the data store
379 */
380 public function testSetMinimalNoWrite()
381 {
382 $bookmark = $this->privateLinkDB->get(42);
383 $bookmark->setTitle($title = 'hi!');
384 $this->privateLinkDB->set($bookmark, false);
385
386 $bookmark = $this->privateLinkDB->get(42);
387 $this->assertEquals(42, $bookmark->getId());
388 $this->assertEquals($title, $bookmark->getTitle());
389
390 // reload from file
391 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
392
393 $bookmark = $this->privateLinkDB->get(42);
394 $this->assertEquals(42, $bookmark->getId());
395 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
396 }
397
398 /**
399 * Test set() method while logged out
400 *
401 * @expectedException \Exception
402 * @expectedExceptionMessage You're not authorized to alter the datastore
403 */
404 public function testSetLoggedOut()
405 {
406 $this->publicLinkDB->set(new Bookmark());
407 }
408
409 /**
410 * Test set() method with an entry which is not a bookmark instance
411 *
412 * @expectedException \Exception
413 * @expectedExceptionMessage Provided data is invalid
414 */
415 public function testSetNotABookmark()
416 {
417 $this->privateLinkDB->set(['title' => 'hi!']);
418 }
419
420 /**
421 * Test set() method with a Bookmark without an ID defined.
422 *
423 * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
424 */
425 public function testSetWithoutId()
426 {
427 $bookmark = new Bookmark();
428 $this->privateLinkDB->set($bookmark);
429 }
430
431 /**
432 * Test set() method with a Bookmark with an unknow ID
433 *
434 * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
435 */
436 public function testSetWithUnknownId()
437 {
438 $bookmark = new Bookmark();
439 $bookmark->setId(666);
440 $this->privateLinkDB->set($bookmark);
441 }
442
443 /**
444 * Test addOrSet() method with a new ID
445 */
446 public function testAddOrSetNew()
447 {
448 $bookmark = new Bookmark();
449 $this->privateLinkDB->addOrSet($bookmark);
450
451 $bookmark = $this->privateLinkDB->get(43);
452 $this->assertEquals(43, $bookmark->getId());
453
454 // reload from file
455 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
456
457 $bookmark = $this->privateLinkDB->get(43);
458 $this->assertEquals(43, $bookmark->getId());
459 }
460
461 /**
462 * Test addOrSet() method with an existing ID
463 */
464 public function testAddOrSetExisting()
465 {
466 $bookmark = $this->privateLinkDB->get(42);
467 $bookmark->setTitle($title = 'hi!');
468 $this->privateLinkDB->addOrSet($bookmark);
469
470 $bookmark = $this->privateLinkDB->get(42);
471 $this->assertEquals(42, $bookmark->getId());
472 $this->assertEquals($title, $bookmark->getTitle());
473
474 // reload from file
475 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
476
477 $bookmark = $this->privateLinkDB->get(42);
478 $this->assertEquals(42, $bookmark->getId());
479 $this->assertEquals($title, $bookmark->getTitle());
480 }
481
482 /**
483 * Test addOrSet() method while logged out
484 *
485 * @expectedException \Exception
486 * @expectedExceptionMessage You're not authorized to alter the datastore
487 */
488 public function testAddOrSetLoggedOut()
489 {
490 $this->publicLinkDB->addOrSet(new Bookmark());
491 }
492
493 /**
494 * Test addOrSet() method with an entry which is not a bookmark instance
495 *
496 * @expectedException \Exception
497 * @expectedExceptionMessage Provided data is invalid
498 */
499 public function testAddOrSetNotABookmark()
500 {
501 $this->privateLinkDB->addOrSet(['title' => 'hi!']);
502 }
503
504 /**
505 * Test addOrSet() method for a bookmark without any field set and without writing the data store
506 */
507 public function testAddOrSetMinimalNoWrite()
508 {
509 $bookmark = $this->privateLinkDB->get(42);
510 $bookmark->setTitle($title = 'hi!');
511 $this->privateLinkDB->addOrSet($bookmark, false);
512
513 $bookmark = $this->privateLinkDB->get(42);
514 $this->assertEquals(42, $bookmark->getId());
515 $this->assertEquals($title, $bookmark->getTitle());
516
517 // reload from file
518 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
519
520 $bookmark = $this->privateLinkDB->get(42);
521 $this->assertEquals(42, $bookmark->getId());
522 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
523 }
524
525 /**
526 * Test remove() method with an existing Bookmark
527 *
528 * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
529 */
530 public function testRemoveExisting()
531 {
532 $bookmark = $this->privateLinkDB->get(42);
533 $this->privateLinkDB->remove($bookmark);
534
535 $exception = null;
536 try {
537 $this->privateLinkDB->get(42);
538 } catch (BookmarkNotFoundException $e) {
539 $exception = $e;
540 }
541 $this->assertInstanceOf(BookmarkNotFoundException::class, $exception);
542
543 // reload from file
544 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
545
546 $this->privateLinkDB->get(42);
547 }
548
549 /**
550 * Test remove() method while logged out
551 *
552 * @expectedException \Exception
553 * @expectedExceptionMessage You're not authorized to alter the datastore
554 */
555 public function testRemoveLoggedOut()
556 {
557 $bookmark = $this->privateLinkDB->get(42);
558 $this->publicLinkDB->remove($bookmark);
559 }
560
561 /**
562 * Test remove() method with an entry which is not a bookmark instance
563 *
564 * @expectedException \Exception
565 * @expectedExceptionMessage Provided data is invalid
566 */
567 public function testRemoveNotABookmark()
568 {
569 $this->privateLinkDB->remove(['title' => 'hi!']);
570 }
571
572 /**
573 * Test remove() method with a Bookmark with an unknown ID
574 *
575 * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
576 */
577 public function testRemoveWithUnknownId()
578 {
579 $bookmark = new Bookmark();
580 $bookmark->setId(666);
581 $this->privateLinkDB->remove($bookmark);
582 }
583
584 /**
585 * Test exists() method
586 */
587 public function testExists()
588 {
589 $this->assertTrue($this->privateLinkDB->exists(42)); // public
590 $this->assertTrue($this->privateLinkDB->exists(6)); // private
591
592 $this->assertTrue($this->privateLinkDB->exists(42, BookmarkFilter::$ALL));
593 $this->assertTrue($this->privateLinkDB->exists(6, BookmarkFilter::$ALL));
594
595 $this->assertTrue($this->privateLinkDB->exists(42, BookmarkFilter::$PUBLIC));
596 $this->assertFalse($this->privateLinkDB->exists(6, BookmarkFilter::$PUBLIC));
597
598 $this->assertFalse($this->privateLinkDB->exists(42, BookmarkFilter::$PRIVATE));
599 $this->assertTrue($this->privateLinkDB->exists(6, BookmarkFilter::$PRIVATE));
600
601 $this->assertTrue($this->publicLinkDB->exists(42));
602 $this->assertFalse($this->publicLinkDB->exists(6));
603
604 $this->assertTrue($this->publicLinkDB->exists(42, BookmarkFilter::$PUBLIC));
605 $this->assertFalse($this->publicLinkDB->exists(6, BookmarkFilter::$PUBLIC));
606
607 $this->assertFalse($this->publicLinkDB->exists(42, BookmarkFilter::$PRIVATE));
608 $this->assertTrue($this->publicLinkDB->exists(6, BookmarkFilter::$PRIVATE));
609 }
610
611 /**
612 * Test initialize() method
613 */
614 public function testInitialize()
615 {
616 $dbSize = $this->privateLinkDB->count();
617 $this->privateLinkDB->initialize();
618 $this->assertEquals($dbSize + 2, $this->privateLinkDB->count());
619 $this->assertEquals(
620 'My secret stuff... - Pastebin.com',
621 $this->privateLinkDB->get(43)->getTitle()
622 );
623 $this->assertEquals(
624 'The personal, minimalist, super-fast, database free, bookmarking service',
625 $this->privateLinkDB->get(44)->getTitle()
626 );
627 }
628
629 /*
630 * The following tests have been taken from the legacy LinkDB test and adapted
631 * to make sure that nothing have been broken in the migration process.
632 * They mostly cover search/filters. Some of them might be redundant with the previous ones.
633 */
634
635 /**
636 * Attempt to instantiate a LinkDB whereas the datastore is not writable
637 *
638 * @expectedException Shaarli\Bookmark\Exception\NotWritableDataStoreException
639 * @expectedExceptionMessageRegExp #Couldn't load data from the data store file "null".*#
640 */
641 public function testConstructDatastoreNotWriteable()
642 {
643 $conf = new ConfigManager('tests/utils/config/configJson');
644 $conf->set('resource.datastore', 'null/store.db');
645 new BookmarkFileService($conf, $this->history, true);
646 }
647
648 /**
649 * The DB doesn't exist, ensure it is created with an empty datastore
650 */
651 public function testCheckDBNewLoggedIn()
652 {
653 unlink(self::$testDatastore);
654 $this->assertFileNotExists(self::$testDatastore);
655 new BookmarkFileService($this->conf, $this->history, true);
656 $this->assertFileExists(self::$testDatastore);
657
658 // ensure the correct data has been written
659 $this->assertGreaterThan(0, filesize(self::$testDatastore));
660 }
661
662 /**
663 * The DB doesn't exist, but not logged in, ensure it initialized, but the file is not written
664 */
665 public function testCheckDBNewLoggedOut()
666 {
667 unlink(self::$testDatastore);
668 $this->assertFileNotExists(self::$testDatastore);
669 $db = new \FakeBookmarkService($this->conf, $this->history, false);
670 $this->assertFileNotExists(self::$testDatastore);
671 $this->assertInstanceOf(BookmarkArray::class, $db->getBookmarks());
672 $this->assertCount(0, $db->getBookmarks());
673 }
674
675 /**
676 * Load public bookmarks from the DB
677 */
678 public function testReadPublicDB()
679 {
680 $this->assertEquals(
681 $this->refDB->countPublicLinks(),
682 $this->publicLinkDB->count()
683 );
684 }
685
686 /**
687 * Load public and private bookmarks from the DB
688 */
689 public function testReadPrivateDB()
690 {
691 $this->assertEquals(
692 $this->refDB->countLinks(),
693 $this->privateLinkDB->count()
694 );
695 }
696
697 /**
698 * Save the bookmarks to the DB
699 */
700 public function testSave()
701 {
702 $testDB = new BookmarkFileService($this->conf, $this->history, true);
703 $dbSize = $testDB->count();
704
705 $bookmark = new Bookmark();
706 $testDB->add($bookmark);
707
708 $testDB = new BookmarkFileService($this->conf, $this->history, true);
709 $this->assertEquals($dbSize + 1, $testDB->count());
710 }
711
712 /**
713 * Count existing bookmarks - public bookmarks hidden
714 */
715 public function testCountHiddenPublic()
716 {
717 $this->conf->set('privacy.hide_public_links', true);
718 $linkDB = new BookmarkFileService($this->conf, $this->history, false);
719
720 $this->assertEquals(0, $linkDB->count());
721 }
722
723 /**
724 * List the days for which bookmarks have been posted
725 */
726 public function testDays()
727 {
728 $this->assertEquals(
729 ['20100309', '20100310', '20121206', '20121207', '20130614', '20150310'],
730 $this->publicLinkDB->days()
731 );
732
733 $this->assertEquals(
734 ['20100309', '20100310', '20121206', '20121207', '20130614', '20141125', '20150310'],
735 $this->privateLinkDB->days()
736 );
737 }
738
739 /**
740 * The URL corresponds to an existing entry in the DB
741 */
742 public function testGetKnownLinkFromURL()
743 {
744 $link = $this->publicLinkDB->findByUrl('http://mediagoblin.org/');
745
746 $this->assertNotEquals(false, $link);
747 $this->assertContains(
748 'A free software media publishing platform',
749 $link->getDescription()
750 );
751 }
752
753 /**
754 * The URL is not in the DB
755 */
756 public function testGetUnknownLinkFromURL()
757 {
758 $this->assertEquals(
759 false,
760 $this->publicLinkDB->findByUrl('http://dev.null')
761 );
762 }
763
764 /**
765 * Lists all tags
766 */
767 public function testAllTags()
768 {
769 $this->assertEquals(
770 [
771 'web' => 3,
772 'cartoon' => 2,
773 'gnu' => 2,
774 'dev' => 1,
775 'samba' => 1,
776 'media' => 1,
777 'software' => 1,
778 'stallman' => 1,
779 'free' => 1,
780 '-exclude' => 1,
781 'hashtag' => 2,
782 // The DB contains a link with `sTuff` and another one with `stuff` tag.
783 // They need to be grouped with the first case found - order by date DESC: `sTuff`.
784 'sTuff' => 2,
785 'ut' => 1,
786 ],
787 $this->publicLinkDB->bookmarksCountPerTag()
788 );
789
790 $this->assertEquals(
791 [
792 'web' => 4,
793 'cartoon' => 3,
794 'gnu' => 2,
795 'dev' => 2,
796 'samba' => 1,
797 'media' => 1,
798 'software' => 1,
799 'stallman' => 1,
800 'free' => 1,
801 'html' => 1,
802 'w3c' => 1,
803 'css' => 1,
804 'Mercurial' => 1,
805 'sTuff' => 2,
806 '-exclude' => 1,
807 '.hidden' => 1,
808 'hashtag' => 2,
809 'tag1' => 1,
810 'tag2' => 1,
811 'tag3' => 1,
812 'tag4' => 1,
813 'ut' => 1,
814 ],
815 $this->privateLinkDB->bookmarksCountPerTag()
816 );
817 $this->assertEquals(
818 [
819 'web' => 4,
820 'cartoon' => 2,
821 'gnu' => 1,
822 'dev' => 1,
823 'samba' => 1,
824 'media' => 1,
825 'html' => 1,
826 'w3c' => 1,
827 'css' => 1,
828 'Mercurial' => 1,
829 '.hidden' => 1,
830 'hashtag' => 1,
831 ],
832 $this->privateLinkDB->bookmarksCountPerTag(['web'])
833 );
834 $this->assertEquals(
835 [
836 'web' => 1,
837 'html' => 1,
838 'w3c' => 1,
839 'css' => 1,
840 'Mercurial' => 1,
841 ],
842 $this->privateLinkDB->bookmarksCountPerTag(['web'], 'private')
843 );
844 }
845
846 /**
847 * Test filter with string.
848 */
849 public function testFilterString()
850 {
851 $tags = 'dev cartoon';
852 $request = ['searchtags' => $tags];
853 $this->assertEquals(
854 2,
855 count($this->privateLinkDB->search($request, null, true))
856 );
857 }
858
859 /**
860 * Test filter with array.
861 */
862 public function testFilterArray()
863 {
864 $tags = ['dev', 'cartoon'];
865 $request = ['searchtags' => $tags];
866 $this->assertEquals(
867 2,
868 count($this->privateLinkDB->search($request, null, true))
869 );
870 }
871
872 /**
873 * Test hidden tags feature:
874 * tags starting with a dot '.' are only visible when logged in.
875 */
876 public function testHiddenTags()
877 {
878 $tags = '.hidden';
879 $request = ['searchtags' => $tags];
880 $this->assertEquals(
881 1,
882 count($this->privateLinkDB->search($request, 'all', true))
883 );
884
885 $this->assertEquals(
886 0,
887 count($this->publicLinkDB->search($request, 'public', true))
888 );
889 }
890
891 /**
892 * Test filterHash() with a valid smallhash.
893 */
894 public function testFilterHashValid()
895 {
896 $request = smallHash('20150310_114651');
897 $this->assertEquals(
898 1,
899 count($this->publicLinkDB->findByHash($request))
900 );
901 $request = smallHash('20150310_114633' . 8);
902 $this->assertEquals(
903 1,
904 count($this->publicLinkDB->findByHash($request))
905 );
906 }
907
908 /**
909 * Test filterHash() with an invalid smallhash.
910 *
911 * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException
912 */
913 public function testFilterHashInValid1()
914 {
915 $request = 'blabla';
916 $this->publicLinkDB->findByHash($request);
917 }
918
919 /**
920 * Test filterHash() with an empty smallhash.
921 *
922 * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException
923 */
924 public function testFilterHashInValid()
925 {
926 $this->publicLinkDB->findByHash('');
927 }
928
929 /**
930 * Test linksCountPerTag all tags without filter.
931 * Equal occurrences should be sorted alphabetically.
932 */
933 public function testCountLinkPerTagAllNoFilter()
934 {
935 $expected = [
936 'web' => 4,
937 'cartoon' => 3,
938 'dev' => 2,
939 'gnu' => 2,
940 'hashtag' => 2,
941 'sTuff' => 2,
942 '-exclude' => 1,
943 '.hidden' => 1,
944 'Mercurial' => 1,
945 'css' => 1,
946 'free' => 1,
947 'html' => 1,
948 'media' => 1,
949 'samba' => 1,
950 'software' => 1,
951 'stallman' => 1,
952 'tag1' => 1,
953 'tag2' => 1,
954 'tag3' => 1,
955 'tag4' => 1,
956 'ut' => 1,
957 'w3c' => 1,
958 ];
959 $tags = $this->privateLinkDB->bookmarksCountPerTag();
960
961 $this->assertEquals($expected, $tags, var_export($tags, true));
962 }
963
964 /**
965 * Test linksCountPerTag all tags with filter.
966 * Equal occurrences should be sorted alphabetically.
967 */
968 public function testCountLinkPerTagAllWithFilter()
969 {
970 $expected = [
971 'gnu' => 2,
972 'hashtag' => 2,
973 '-exclude' => 1,
974 '.hidden' => 1,
975 'free' => 1,
976 'media' => 1,
977 'software' => 1,
978 'stallman' => 1,
979 'stuff' => 1,
980 'web' => 1,
981 ];
982 $tags = $this->privateLinkDB->bookmarksCountPerTag(['gnu']);
983
984 $this->assertEquals($expected, $tags, var_export($tags, true));
985 }
986
987 /**
988 * Test linksCountPerTag public tags with filter.
989 * Equal occurrences should be sorted alphabetically.
990 */
991 public function testCountLinkPerTagPublicWithFilter()
992 {
993 $expected = [
994 'gnu' => 2,
995 'hashtag' => 2,
996 '-exclude' => 1,
997 '.hidden' => 1,
998 'free' => 1,
999 'media' => 1,
1000 'software' => 1,
1001 'stallman' => 1,
1002 'stuff' => 1,
1003 'web' => 1,
1004 ];
1005 $tags = $this->privateLinkDB->bookmarksCountPerTag(['gnu'], 'public');
1006
1007 $this->assertEquals($expected, $tags, var_export($tags, true));
1008 }
1009
1010 /**
1011 * Test linksCountPerTag public tags with filter.
1012 * Equal occurrences should be sorted alphabetically.
1013 */
1014 public function testCountLinkPerTagPrivateWithFilter()
1015 {
1016 $expected = [
1017 'cartoon' => 1,
1018 'dev' => 1,
1019 'tag1' => 1,
1020 'tag2' => 1,
1021 'tag3' => 1,
1022 'tag4' => 1,
1023 ];
1024 $tags = $this->privateLinkDB->bookmarksCountPerTag(['dev'], 'private');
1025
1026 $this->assertEquals($expected, $tags, var_export($tags, true));
1027 }
1028
1029 /**
1030 * Test linksCountPerTag public tags with filter.
1031 * Equal occurrences should be sorted alphabetically.
1032 */
1033 public function testCountTagsNoMarkdown()
1034 {
1035 $expected = [
1036 'cartoon' => 3,
1037 'dev' => 2,
1038 'tag1' => 1,
1039 'tag2' => 1,
1040 'tag3' => 1,
1041 'tag4' => 1,
1042 'web' => 4,
1043 'gnu' => 2,
1044 'hashtag' => 2,
1045 'sTuff' => 2,
1046 '-exclude' => 1,
1047 '.hidden' => 1,
1048 'Mercurial' => 1,
1049 'css' => 1,
1050 'free' => 1,
1051 'html' => 1,
1052 'media' => 1,
1053 'newTagToCount' => 1,
1054 'samba' => 1,
1055 'software' => 1,
1056 'stallman' => 1,
1057 'ut' => 1,
1058 'w3c' => 1,
1059 ];
1060 $bookmark = new Bookmark();
1061 $bookmark->setTags(['newTagToCount', BookmarkMarkdownFormatter::NO_MD_TAG]);
1062 $this->privateLinkDB->add($bookmark);
1063
1064 $tags = $this->privateLinkDB->bookmarksCountPerTag();
1065
1066 $this->assertEquals($expected, $tags, var_export($tags, true));
1067 }
1068
1069 /**
1070 * Allows to test LinkDB's private methods
1071 *
1072 * @see
1073 * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html
1074 * http://stackoverflow.com/a/2798203
1075 */
1076 protected static function getMethod($name)
1077 {
1078 $class = new ReflectionClass('Shaarli\Bookmark\BookmarkFileService');
1079 $method = $class->getMethod($name);
1080 $method->setAccessible(true);
1081 return $method;
1082 }
1083 }