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