]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/bookmark/BookmarkFileServiceTest.php
Merge pull request #1552 from ArthurHoaro/feature/better-initializer
[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('#/shaare/[\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('#/shaare/[\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('/shaare/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('/shaare/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 + 3, $this->privateLinkDB->count());
619 $this->assertStringStartsWith(
620 'Shaarli will automatically pick up the thumbnail for links to a variety of websites.',
621 $this->privateLinkDB->get(43)->getDescription()
622 );
623 $this->assertStringStartsWith(
624 'Adding a shaare without entering a URL creates a text-only "note" post such as this one.',
625 $this->privateLinkDB->get(44)->getDescription()
626 );
627 $this->assertStringStartsWith(
628 'Welcome to Shaarli!',
629 $this->privateLinkDB->get(45)->getDescription()
630 );
631 }
632
633 /*
634 * The following tests have been taken from the legacy LinkDB test and adapted
635 * to make sure that nothing have been broken in the migration process.
636 * They mostly cover search/filters. Some of them might be redundant with the previous ones.
637 */
638
639 /**
640 * Attempt to instantiate a LinkDB whereas the datastore is not writable
641 *
642 * @expectedException Shaarli\Bookmark\Exception\NotWritableDataStoreException
643 * @expectedExceptionMessageRegExp #Couldn't load data from the data store file "null".*#
644 */
645 public function testConstructDatastoreNotWriteable()
646 {
647 $conf = new ConfigManager('tests/utils/config/configJson');
648 $conf->set('resource.datastore', 'null/store.db');
649 new BookmarkFileService($conf, $this->history, true);
650 }
651
652 /**
653 * The DB doesn't exist, ensure it is created with an empty datastore
654 */
655 public function testCheckDBNewLoggedIn()
656 {
657 unlink(self::$testDatastore);
658 $this->assertFileNotExists(self::$testDatastore);
659 new BookmarkFileService($this->conf, $this->history, true);
660 $this->assertFileExists(self::$testDatastore);
661
662 // ensure the correct data has been written
663 $this->assertGreaterThan(0, filesize(self::$testDatastore));
664 }
665
666 /**
667 * The DB doesn't exist, but not logged in, ensure it initialized, but the file is not written
668 */
669 public function testCheckDBNewLoggedOut()
670 {
671 unlink(self::$testDatastore);
672 $this->assertFileNotExists(self::$testDatastore);
673 $db = new \FakeBookmarkService($this->conf, $this->history, false);
674 $this->assertFileNotExists(self::$testDatastore);
675 $this->assertInstanceOf(BookmarkArray::class, $db->getBookmarks());
676 $this->assertCount(0, $db->getBookmarks());
677 }
678
679 /**
680 * Load public bookmarks from the DB
681 */
682 public function testReadPublicDB()
683 {
684 $this->assertEquals(
685 $this->refDB->countPublicLinks(),
686 $this->publicLinkDB->count()
687 );
688 }
689
690 /**
691 * Load public and private bookmarks from the DB
692 */
693 public function testReadPrivateDB()
694 {
695 $this->assertEquals(
696 $this->refDB->countLinks(),
697 $this->privateLinkDB->count()
698 );
699 }
700
701 /**
702 * Save the bookmarks to the DB
703 */
704 public function testSave()
705 {
706 $testDB = new BookmarkFileService($this->conf, $this->history, true);
707 $dbSize = $testDB->count();
708
709 $bookmark = new Bookmark();
710 $testDB->add($bookmark);
711
712 $testDB = new BookmarkFileService($this->conf, $this->history, true);
713 $this->assertEquals($dbSize + 1, $testDB->count());
714 }
715
716 /**
717 * Count existing bookmarks - public bookmarks hidden
718 */
719 public function testCountHiddenPublic()
720 {
721 $this->conf->set('privacy.hide_public_links', true);
722 $linkDB = new BookmarkFileService($this->conf, $this->history, false);
723
724 $this->assertEquals(0, $linkDB->count());
725 }
726
727 /**
728 * List the days for which bookmarks have been posted
729 */
730 public function testDays()
731 {
732 $this->assertEquals(
733 ['20100309', '20100310', '20121206', '20121207', '20130614', '20150310'],
734 $this->publicLinkDB->days()
735 );
736
737 $this->assertEquals(
738 ['20100309', '20100310', '20121206', '20121207', '20130614', '20141125', '20150310'],
739 $this->privateLinkDB->days()
740 );
741 }
742
743 /**
744 * The URL corresponds to an existing entry in the DB
745 */
746 public function testGetKnownLinkFromURL()
747 {
748 $link = $this->publicLinkDB->findByUrl('http://mediagoblin.org/');
749
750 $this->assertNotEquals(false, $link);
751 $this->assertContains(
752 'A free software media publishing platform',
753 $link->getDescription()
754 );
755 }
756
757 /**
758 * The URL is not in the DB
759 */
760 public function testGetUnknownLinkFromURL()
761 {
762 $this->assertEquals(
763 false,
764 $this->publicLinkDB->findByUrl('http://dev.null')
765 );
766 }
767
768 /**
769 * Lists all tags
770 */
771 public function testAllTags()
772 {
773 $this->assertEquals(
774 [
775 'web' => 3,
776 'cartoon' => 2,
777 'gnu' => 2,
778 'dev' => 1,
779 'samba' => 1,
780 'media' => 1,
781 'software' => 1,
782 'stallman' => 1,
783 'free' => 1,
784 '-exclude' => 1,
785 'hashtag' => 2,
786 // The DB contains a link with `sTuff` and another one with `stuff` tag.
787 // They need to be grouped with the first case found - order by date DESC: `sTuff`.
788 'sTuff' => 2,
789 'ut' => 1,
790 ],
791 $this->publicLinkDB->bookmarksCountPerTag()
792 );
793
794 $this->assertEquals(
795 [
796 'web' => 4,
797 'cartoon' => 3,
798 'gnu' => 2,
799 'dev' => 2,
800 'samba' => 1,
801 'media' => 1,
802 'software' => 1,
803 'stallman' => 1,
804 'free' => 1,
805 'html' => 1,
806 'w3c' => 1,
807 'css' => 1,
808 'Mercurial' => 1,
809 'sTuff' => 2,
810 '-exclude' => 1,
811 '.hidden' => 1,
812 'hashtag' => 2,
813 'tag1' => 1,
814 'tag2' => 1,
815 'tag3' => 1,
816 'tag4' => 1,
817 'ut' => 1,
818 ],
819 $this->privateLinkDB->bookmarksCountPerTag()
820 );
821 $this->assertEquals(
822 [
823 'cartoon' => 2,
824 'gnu' => 1,
825 'dev' => 1,
826 'samba' => 1,
827 'media' => 1,
828 'html' => 1,
829 'w3c' => 1,
830 'css' => 1,
831 'Mercurial' => 1,
832 '.hidden' => 1,
833 'hashtag' => 1,
834 ],
835 $this->privateLinkDB->bookmarksCountPerTag(['web'])
836 );
837 $this->assertEquals(
838 [
839 'html' => 1,
840 'w3c' => 1,
841 'css' => 1,
842 'Mercurial' => 1,
843 ],
844 $this->privateLinkDB->bookmarksCountPerTag(['web'], 'private')
845 );
846 }
847
848 /**
849 * Test filter with string.
850 */
851 public function testFilterString()
852 {
853 $tags = 'dev cartoon';
854 $request = ['searchtags' => $tags];
855 $this->assertEquals(
856 2,
857 count($this->privateLinkDB->search($request, null, true))
858 );
859 }
860
861 /**
862 * Test filter with array.
863 */
864 public function testFilterArray()
865 {
866 $tags = ['dev', 'cartoon'];
867 $request = ['searchtags' => $tags];
868 $this->assertEquals(
869 2,
870 count($this->privateLinkDB->search($request, null, true))
871 );
872 }
873
874 /**
875 * Test hidden tags feature:
876 * tags starting with a dot '.' are only visible when logged in.
877 */
878 public function testHiddenTags()
879 {
880 $tags = '.hidden';
881 $request = ['searchtags' => $tags];
882 $this->assertEquals(
883 1,
884 count($this->privateLinkDB->search($request, 'all', true))
885 );
886
887 $this->assertEquals(
888 0,
889 count($this->publicLinkDB->search($request, 'public', true))
890 );
891 }
892
893 /**
894 * Test filterHash() with a valid smallhash.
895 */
896 public function testFilterHashValid()
897 {
898 $request = smallHash('20150310_114651');
899 $this->assertSame(
900 $request,
901 $this->publicLinkDB->findByHash($request)->getShortUrl()
902 );
903 $request = smallHash('20150310_114633' . 8);
904 $this->assertSame(
905 $request,
906 $this->publicLinkDB->findByHash($request)->getShortUrl()
907 );
908 }
909
910 /**
911 * Test filterHash() with an invalid smallhash.
912 */
913 public function testFilterHashInValid1()
914 {
915 $this->expectException(BookmarkNotFoundException::class);
916
917 $request = 'blabla';
918 $this->publicLinkDB->findByHash($request);
919 }
920
921 /**
922 * Test filterHash() with an empty smallhash.
923 */
924 public function testFilterHashInValid()
925 {
926 $this->expectException(BookmarkNotFoundException::class);
927
928 $this->publicLinkDB->findByHash('');
929 }
930
931 /**
932 * Test linksCountPerTag all tags without filter.
933 * Equal occurrences should be sorted alphabetically.
934 */
935 public function testCountLinkPerTagAllNoFilter()
936 {
937 $expected = [
938 'web' => 4,
939 'cartoon' => 3,
940 'dev' => 2,
941 'gnu' => 2,
942 'hashtag' => 2,
943 'sTuff' => 2,
944 '-exclude' => 1,
945 '.hidden' => 1,
946 'Mercurial' => 1,
947 'css' => 1,
948 'free' => 1,
949 'html' => 1,
950 'media' => 1,
951 'samba' => 1,
952 'software' => 1,
953 'stallman' => 1,
954 'tag1' => 1,
955 'tag2' => 1,
956 'tag3' => 1,
957 'tag4' => 1,
958 'ut' => 1,
959 'w3c' => 1,
960 ];
961 $tags = $this->privateLinkDB->bookmarksCountPerTag();
962
963 $this->assertEquals($expected, $tags, var_export($tags, true));
964 }
965
966 /**
967 * Test linksCountPerTag all tags with filter.
968 * Equal occurrences should be sorted alphabetically.
969 */
970 public function testCountLinkPerTagAllWithFilter()
971 {
972 $expected = [
973 'hashtag' => 2,
974 '-exclude' => 1,
975 '.hidden' => 1,
976 'free' => 1,
977 'media' => 1,
978 'software' => 1,
979 'stallman' => 1,
980 'stuff' => 1,
981 'web' => 1,
982 ];
983 $tags = $this->privateLinkDB->bookmarksCountPerTag(['gnu']);
984
985 $this->assertEquals($expected, $tags, var_export($tags, true));
986 }
987
988 /**
989 * Test linksCountPerTag public tags with filter.
990 * Equal occurrences should be sorted alphabetically.
991 */
992 public function testCountLinkPerTagPublicWithFilter()
993 {
994 $expected = [
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 'tag1' => 1,
1019 'tag2' => 1,
1020 'tag3' => 1,
1021 'tag4' => 1,
1022 ];
1023 $tags = $this->privateLinkDB->bookmarksCountPerTag(['dev'], 'private');
1024
1025 $this->assertEquals($expected, $tags, var_export($tags, true));
1026 }
1027
1028 /**
1029 * Test linksCountPerTag public tags with filter.
1030 * Equal occurrences should be sorted alphabetically.
1031 */
1032 public function testCountTagsNoMarkdown()
1033 {
1034 $expected = [
1035 'cartoon' => 3,
1036 'dev' => 2,
1037 'tag1' => 1,
1038 'tag2' => 1,
1039 'tag3' => 1,
1040 'tag4' => 1,
1041 'web' => 4,
1042 'gnu' => 2,
1043 'hashtag' => 2,
1044 'sTuff' => 2,
1045 '-exclude' => 1,
1046 '.hidden' => 1,
1047 'Mercurial' => 1,
1048 'css' => 1,
1049 'free' => 1,
1050 'html' => 1,
1051 'media' => 1,
1052 'newTagToCount' => 1,
1053 'samba' => 1,
1054 'software' => 1,
1055 'stallman' => 1,
1056 'ut' => 1,
1057 'w3c' => 1,
1058 ];
1059 $bookmark = new Bookmark();
1060 $bookmark->setTags(['newTagToCount', BookmarkMarkdownFormatter::NO_MD_TAG]);
1061 $this->privateLinkDB->add($bookmark);
1062
1063 $tags = $this->privateLinkDB->bookmarksCountPerTag();
1064
1065 $this->assertEquals($expected, $tags, var_export($tags, true));
1066 }
1067
1068 /**
1069 * Test filterDay while logged in
1070 */
1071 public function testFilterDayLoggedIn(): void
1072 {
1073 $bookmarks = $this->privateLinkDB->filterDay('20121206');
1074 $expectedIds = [4, 9, 1, 0];
1075
1076 static::assertCount(4, $bookmarks);
1077 foreach ($bookmarks as $bookmark) {
1078 $i = ($i ?? -1) + 1;
1079 static::assertSame($expectedIds[$i], $bookmark->getId());
1080 }
1081 }
1082
1083 /**
1084 * Test filterDay while logged out
1085 */
1086 public function testFilterDayLoggedOut(): void
1087 {
1088 $bookmarks = $this->publicLinkDB->filterDay('20121206');
1089 $expectedIds = [4, 9, 1];
1090
1091 static::assertCount(3, $bookmarks);
1092 foreach ($bookmarks as $bookmark) {
1093 $i = ($i ?? -1) + 1;
1094 static::assertSame($expectedIds[$i], $bookmark->getId());
1095 }
1096 }
1097
1098 /**
1099 * Allows to test LinkDB's private methods
1100 *
1101 * @see
1102 * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html
1103 * http://stackoverflow.com/a/2798203
1104 */
1105 protected static function getMethod($name)
1106 {
1107 $class = new ReflectionClass('Shaarli\Bookmark\BookmarkFileService');
1108 $method = $class->getMethod($name);
1109 $method->setAccessible(true);
1110 return $method;
1111 }
1112 }