aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/bookmark/BookmarkFileServiceTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/bookmark/BookmarkFileServiceTest.php')
-rw-r--r--tests/bookmark/BookmarkFileServiceTest.php373
1 files changed, 233 insertions, 140 deletions
diff --git a/tests/bookmark/BookmarkFileServiceTest.php b/tests/bookmark/BookmarkFileServiceTest.php
index 4900d41d..f619aff3 100644
--- a/tests/bookmark/BookmarkFileServiceTest.php
+++ b/tests/bookmark/BookmarkFileServiceTest.php
@@ -6,7 +6,7 @@
6namespace Shaarli\Bookmark; 6namespace Shaarli\Bookmark;
7 7
8use DateTime; 8use DateTime;
9use PHPUnit\Framework\TestCase; 9use malkusch\lock\mutex\NoMutex;
10use ReferenceLinkDB; 10use ReferenceLinkDB;
11use ReflectionClass; 11use ReflectionClass;
12use Shaarli; 12use Shaarli;
@@ -14,6 +14,7 @@ use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
14use Shaarli\Config\ConfigManager; 14use Shaarli\Config\ConfigManager;
15use Shaarli\Formatter\BookmarkMarkdownFormatter; 15use Shaarli\Formatter\BookmarkMarkdownFormatter;
16use Shaarli\History; 16use Shaarli\History;
17use Shaarli\TestCase;
17 18
18/** 19/**
19 * Unitary tests for LegacyLinkDBTest 20 * Unitary tests for LegacyLinkDBTest
@@ -52,6 +53,9 @@ class BookmarkFileServiceTest extends TestCase
52 */ 53 */
53 protected $privateLinkDB = null; 54 protected $privateLinkDB = null;
54 55
56 /** @var NoMutex */
57 protected $mutex;
58
55 /** 59 /**
56 * Instantiates public and private LinkDBs with test data 60 * Instantiates public and private LinkDBs with test data
57 * 61 *
@@ -66,8 +70,10 @@ class BookmarkFileServiceTest extends TestCase
66 * 70 *
67 * Resets test data for each test 71 * Resets test data for each test
68 */ 72 */
69 protected function setUp() 73 protected function setUp(): void
70 { 74 {
75 $this->mutex = new NoMutex();
76
71 if (file_exists(self::$testDatastore)) { 77 if (file_exists(self::$testDatastore)) {
72 unlink(self::$testDatastore); 78 unlink(self::$testDatastore);
73 } 79 }
@@ -87,8 +93,8 @@ class BookmarkFileServiceTest extends TestCase
87 $this->refDB = new \ReferenceLinkDB(); 93 $this->refDB = new \ReferenceLinkDB();
88 $this->refDB->write(self::$testDatastore); 94 $this->refDB->write(self::$testDatastore);
89 $this->history = new History('sandbox/history.php'); 95 $this->history = new History('sandbox/history.php');
90 $this->publicLinkDB = new BookmarkFileService($this->conf, $this->history, false); 96 $this->publicLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, false);
91 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); 97 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
92 } 98 }
93 99
94 /** 100 /**
@@ -105,7 +111,7 @@ class BookmarkFileServiceTest extends TestCase
105 $db = self::getMethod('migrate'); 111 $db = self::getMethod('migrate');
106 $db->invokeArgs($this->privateLinkDB, []); 112 $db->invokeArgs($this->privateLinkDB, []);
107 113
108 $db = new \FakeBookmarkService($this->conf, $this->history, true); 114 $db = new \FakeBookmarkService($this->conf, $this->history, $this->mutex, true);
109 $this->assertInstanceOf(BookmarkArray::class, $db->getBookmarks()); 115 $this->assertInstanceOf(BookmarkArray::class, $db->getBookmarks());
110 $this->assertEquals($this->refDB->countLinks(), $db->count()); 116 $this->assertEquals($this->refDB->countLinks(), $db->count());
111 } 117 }
@@ -134,11 +140,11 @@ class BookmarkFileServiceTest extends TestCase
134 140
135 /** 141 /**
136 * Test get() method for an undefined bookmark 142 * Test get() method for an undefined bookmark
137 *
138 * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
139 */ 143 */
140 public function testGetUndefined() 144 public function testGetUndefined()
141 { 145 {
146 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class);
147
142 $this->privateLinkDB->get(666); 148 $this->privateLinkDB->get(666);
143 } 149 }
144 150
@@ -174,7 +180,7 @@ class BookmarkFileServiceTest extends TestCase
174 $this->assertEquals($updated, $bookmark->getUpdated()); 180 $this->assertEquals($updated, $bookmark->getUpdated());
175 181
176 // reload from file 182 // reload from file
177 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); 183 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
178 184
179 $bookmark = $this->privateLinkDB->get(43); 185 $bookmark = $this->privateLinkDB->get(43);
180 $this->assertEquals(43, $bookmark->getId()); 186 $this->assertEquals(43, $bookmark->getId());
@@ -200,7 +206,7 @@ class BookmarkFileServiceTest extends TestCase
200 206
201 $bookmark = $this->privateLinkDB->get(43); 207 $bookmark = $this->privateLinkDB->get(43);
202 $this->assertEquals(43, $bookmark->getId()); 208 $this->assertEquals(43, $bookmark->getId());
203 $this->assertRegExp('/\?[\w\-]{6}/', $bookmark->getUrl()); 209 $this->assertRegExp('#/shaare/[\w\-]{6}#', $bookmark->getUrl());
204 $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl()); 210 $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl());
205 $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle()); 211 $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle());
206 $this->assertEmpty($bookmark->getDescription()); 212 $this->assertEmpty($bookmark->getDescription());
@@ -212,11 +218,11 @@ class BookmarkFileServiceTest extends TestCase
212 $this->assertNull($bookmark->getUpdated()); 218 $this->assertNull($bookmark->getUpdated());
213 219
214 // reload from file 220 // reload from file
215 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); 221 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
216 222
217 $bookmark = $this->privateLinkDB->get(43); 223 $bookmark = $this->privateLinkDB->get(43);
218 $this->assertEquals(43, $bookmark->getId()); 224 $this->assertEquals(43, $bookmark->getId());
219 $this->assertRegExp('/\?[\w\-]{6}/', $bookmark->getUrl()); 225 $this->assertRegExp('#/shaare/[\w\-]{6}#', $bookmark->getUrl());
220 $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl()); 226 $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl());
221 $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle()); 227 $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle());
222 $this->assertEmpty($bookmark->getDescription()); 228 $this->assertEmpty($bookmark->getDescription());
@@ -230,53 +236,42 @@ class BookmarkFileServiceTest extends TestCase
230 236
231 /** 237 /**
232 * Test add() method for a bookmark without any field set and without writing the data store 238 * Test add() method for a bookmark without any field set and without writing the data store
233 *
234 * @expectedExceptionMessage Shaarli\Bookmark\Exception\BookmarkNotFoundException
235 */ 239 */
236 public function testAddMinimalNoWrite() 240 public function testAddMinimalNoWrite()
237 { 241 {
242 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class);
243
238 $bookmark = new Bookmark(); 244 $bookmark = new Bookmark();
239 $this->privateLinkDB->add($bookmark); 245 $this->privateLinkDB->add($bookmark, false);
240 246
241 $bookmark = $this->privateLinkDB->get(43); 247 $bookmark = $this->privateLinkDB->get(43);
242 $this->assertEquals(43, $bookmark->getId()); 248 $this->assertEquals(43, $bookmark->getId());
243 249
244 // reload from file 250 // reload from file
245 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); 251 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
246 252
247 $this->privateLinkDB->get(43); 253 $this->privateLinkDB->get(43);
248 } 254 }
249 255
250 /** 256 /**
251 * Test add() method while logged out 257 * Test add() method while logged out
252 *
253 * @expectedException \Exception
254 * @expectedExceptionMessage You're not authorized to alter the datastore
255 */ 258 */
256 public function testAddLoggedOut() 259 public function testAddLoggedOut()
257 { 260 {
258 $this->publicLinkDB->add(new Bookmark()); 261 $this->expectException(\Exception::class);
259 } 262 $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
260 263
261 /** 264 $this->publicLinkDB->add(new Bookmark());
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 } 265 }
271 266
272 /** 267 /**
273 * Test add() method with a Bookmark already containing an ID 268 * Test add() method with a Bookmark already containing an ID
274 *
275 * @expectedException \Exception
276 * @expectedExceptionMessage This bookmarks already exists
277 */ 269 */
278 public function testAddWithId() 270 public function testAddWithId()
279 { 271 {
272 $this->expectException(\Exception::class);
273 $this->expectExceptionMessage('This bookmarks already exists');
274
280 $bookmark = new Bookmark(); 275 $bookmark = new Bookmark();
281 $bookmark->setId(43); 276 $bookmark->setId(43);
282 $this->privateLinkDB->add($bookmark); 277 $this->privateLinkDB->add($bookmark);
@@ -314,7 +309,7 @@ class BookmarkFileServiceTest extends TestCase
314 $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated()); 309 $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated());
315 310
316 // reload from file 311 // reload from file
317 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); 312 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
318 313
319 $bookmark = $this->privateLinkDB->get(42); 314 $bookmark = $this->privateLinkDB->get(42);
320 $this->assertEquals(42, $bookmark->getId()); 315 $this->assertEquals(42, $bookmark->getId());
@@ -340,7 +335,7 @@ class BookmarkFileServiceTest extends TestCase
340 335
341 $bookmark = $this->privateLinkDB->get(42); 336 $bookmark = $this->privateLinkDB->get(42);
342 $this->assertEquals(42, $bookmark->getId()); 337 $this->assertEquals(42, $bookmark->getId());
343 $this->assertEquals('?WDWyig', $bookmark->getUrl()); 338 $this->assertEquals('/shaare/WDWyig', $bookmark->getUrl());
344 $this->assertEquals('1eYJ1Q', $bookmark->getShortUrl()); 339 $this->assertEquals('1eYJ1Q', $bookmark->getShortUrl());
345 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle()); 340 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
346 $this->assertEquals('Used to test bookmarks reordering.', $bookmark->getDescription()); 341 $this->assertEquals('Used to test bookmarks reordering.', $bookmark->getDescription());
@@ -355,11 +350,11 @@ class BookmarkFileServiceTest extends TestCase
355 $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated()); 350 $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated());
356 351
357 // reload from file 352 // reload from file
358 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); 353 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
359 354
360 $bookmark = $this->privateLinkDB->get(42); 355 $bookmark = $this->privateLinkDB->get(42);
361 $this->assertEquals(42, $bookmark->getId()); 356 $this->assertEquals(42, $bookmark->getId());
362 $this->assertEquals('?WDWyig', $bookmark->getUrl()); 357 $this->assertEquals('/shaare/WDWyig', $bookmark->getUrl());
363 $this->assertEquals('1eYJ1Q', $bookmark->getShortUrl()); 358 $this->assertEquals('1eYJ1Q', $bookmark->getShortUrl());
364 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle()); 359 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
365 $this->assertEquals('Used to test bookmarks reordering.', $bookmark->getDescription()); 360 $this->assertEquals('Used to test bookmarks reordering.', $bookmark->getDescription());
@@ -388,7 +383,7 @@ class BookmarkFileServiceTest extends TestCase
388 $this->assertEquals($title, $bookmark->getTitle()); 383 $this->assertEquals($title, $bookmark->getTitle());
389 384
390 // reload from file 385 // reload from file
391 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); 386 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
392 387
393 $bookmark = $this->privateLinkDB->get(42); 388 $bookmark = $this->privateLinkDB->get(42);
394 $this->assertEquals(42, $bookmark->getId()); 389 $this->assertEquals(42, $bookmark->getId());
@@ -397,44 +392,33 @@ class BookmarkFileServiceTest extends TestCase
397 392
398 /** 393 /**
399 * Test set() method while logged out 394 * Test set() method while logged out
400 *
401 * @expectedException \Exception
402 * @expectedExceptionMessage You're not authorized to alter the datastore
403 */ 395 */
404 public function testSetLoggedOut() 396 public function testSetLoggedOut()
405 { 397 {
406 $this->publicLinkDB->set(new Bookmark()); 398 $this->expectException(\Exception::class);
407 } 399 $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
408 400
409 /** 401 $this->publicLinkDB->set(new Bookmark());
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 } 402 }
419 403
420 /** 404 /**
421 * Test set() method with a Bookmark without an ID defined. 405 * Test set() method with a Bookmark without an ID defined.
422 *
423 * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
424 */ 406 */
425 public function testSetWithoutId() 407 public function testSetWithoutId()
426 { 408 {
409 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class);
410
427 $bookmark = new Bookmark(); 411 $bookmark = new Bookmark();
428 $this->privateLinkDB->set($bookmark); 412 $this->privateLinkDB->set($bookmark);
429 } 413 }
430 414
431 /** 415 /**
432 * Test set() method with a Bookmark with an unknow ID 416 * Test set() method with a Bookmark with an unknow ID
433 *
434 * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
435 */ 417 */
436 public function testSetWithUnknownId() 418 public function testSetWithUnknownId()
437 { 419 {
420 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class);
421
438 $bookmark = new Bookmark(); 422 $bookmark = new Bookmark();
439 $bookmark->setId(666); 423 $bookmark->setId(666);
440 $this->privateLinkDB->set($bookmark); 424 $this->privateLinkDB->set($bookmark);
@@ -452,7 +436,7 @@ class BookmarkFileServiceTest extends TestCase
452 $this->assertEquals(43, $bookmark->getId()); 436 $this->assertEquals(43, $bookmark->getId());
453 437
454 // reload from file 438 // reload from file
455 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); 439 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
456 440
457 $bookmark = $this->privateLinkDB->get(43); 441 $bookmark = $this->privateLinkDB->get(43);
458 $this->assertEquals(43, $bookmark->getId()); 442 $this->assertEquals(43, $bookmark->getId());
@@ -472,7 +456,7 @@ class BookmarkFileServiceTest extends TestCase
472 $this->assertEquals($title, $bookmark->getTitle()); 456 $this->assertEquals($title, $bookmark->getTitle());
473 457
474 // reload from file 458 // reload from file
475 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); 459 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
476 460
477 $bookmark = $this->privateLinkDB->get(42); 461 $bookmark = $this->privateLinkDB->get(42);
478 $this->assertEquals(42, $bookmark->getId()); 462 $this->assertEquals(42, $bookmark->getId());
@@ -481,24 +465,13 @@ class BookmarkFileServiceTest extends TestCase
481 465
482 /** 466 /**
483 * Test addOrSet() method while logged out 467 * Test addOrSet() method while logged out
484 *
485 * @expectedException \Exception
486 * @expectedExceptionMessage You're not authorized to alter the datastore
487 */ 468 */
488 public function testAddOrSetLoggedOut() 469 public function testAddOrSetLoggedOut()
489 { 470 {
490 $this->publicLinkDB->addOrSet(new Bookmark()); 471 $this->expectException(\Exception::class);
491 } 472 $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
492 473
493 /** 474 $this->publicLinkDB->addOrSet(new Bookmark());
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 } 475 }
503 476
504 /** 477 /**
@@ -515,7 +488,7 @@ class BookmarkFileServiceTest extends TestCase
515 $this->assertEquals($title, $bookmark->getTitle()); 488 $this->assertEquals($title, $bookmark->getTitle());
516 489
517 // reload from file 490 // reload from file
518 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); 491 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
519 492
520 $bookmark = $this->privateLinkDB->get(42); 493 $bookmark = $this->privateLinkDB->get(42);
521 $this->assertEquals(42, $bookmark->getId()); 494 $this->assertEquals(42, $bookmark->getId());
@@ -524,11 +497,11 @@ class BookmarkFileServiceTest extends TestCase
524 497
525 /** 498 /**
526 * Test remove() method with an existing Bookmark 499 * Test remove() method with an existing Bookmark
527 *
528 * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
529 */ 500 */
530 public function testRemoveExisting() 501 public function testRemoveExisting()
531 { 502 {
503 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class);
504
532 $bookmark = $this->privateLinkDB->get(42); 505 $bookmark = $this->privateLinkDB->get(42);
533 $this->privateLinkDB->remove($bookmark); 506 $this->privateLinkDB->remove($bookmark);
534 507
@@ -541,41 +514,30 @@ class BookmarkFileServiceTest extends TestCase
541 $this->assertInstanceOf(BookmarkNotFoundException::class, $exception); 514 $this->assertInstanceOf(BookmarkNotFoundException::class, $exception);
542 515
543 // reload from file 516 // reload from file
544 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); 517 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
545 518
546 $this->privateLinkDB->get(42); 519 $this->privateLinkDB->get(42);
547 } 520 }
548 521
549 /** 522 /**
550 * Test remove() method while logged out 523 * Test remove() method while logged out
551 *
552 * @expectedException \Exception
553 * @expectedExceptionMessage You're not authorized to alter the datastore
554 */ 524 */
555 public function testRemoveLoggedOut() 525 public function testRemoveLoggedOut()
556 { 526 {
527 $this->expectException(\Exception::class);
528 $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
529
557 $bookmark = $this->privateLinkDB->get(42); 530 $bookmark = $this->privateLinkDB->get(42);
558 $this->publicLinkDB->remove($bookmark); 531 $this->publicLinkDB->remove($bookmark);
559 } 532 }
560 533
561 /** 534 /**
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 535 * Test remove() method with a Bookmark with an unknown ID
574 *
575 * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
576 */ 536 */
577 public function testRemoveWithUnknownId() 537 public function testRemoveWithUnknownId()
578 { 538 {
539 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class);
540
579 $bookmark = new Bookmark(); 541 $bookmark = new Bookmark();
580 $bookmark->setId(666); 542 $bookmark->setId(666);
581 $this->privateLinkDB->remove($bookmark); 543 $this->privateLinkDB->remove($bookmark);
@@ -615,14 +577,18 @@ class BookmarkFileServiceTest extends TestCase
615 { 577 {
616 $dbSize = $this->privateLinkDB->count(); 578 $dbSize = $this->privateLinkDB->count();
617 $this->privateLinkDB->initialize(); 579 $this->privateLinkDB->initialize();
618 $this->assertEquals($dbSize + 2, $this->privateLinkDB->count()); 580 $this->assertEquals($dbSize + 3, $this->privateLinkDB->count());
619 $this->assertEquals( 581 $this->assertStringStartsWith(
620 'My secret stuff... - Pastebin.com', 582 'Shaarli will automatically pick up the thumbnail for links to a variety of websites.',
621 $this->privateLinkDB->get(43)->getTitle() 583 $this->privateLinkDB->get(43)->getDescription()
622 ); 584 );
623 $this->assertEquals( 585 $this->assertStringStartsWith(
624 'The personal, minimalist, super-fast, database free, bookmarking service', 586 'Adding a shaare without entering a URL creates a text-only "note" post such as this one.',
625 $this->privateLinkDB->get(44)->getTitle() 587 $this->privateLinkDB->get(44)->getDescription()
588 );
589 $this->assertStringStartsWith(
590 'Welcome to Shaarli!',
591 $this->privateLinkDB->get(45)->getDescription()
626 ); 592 );
627 } 593 }
628 594
@@ -631,18 +597,17 @@ class BookmarkFileServiceTest extends TestCase
631 * to make sure that nothing have been broken in the migration process. 597 * 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. 598 * They mostly cover search/filters. Some of them might be redundant with the previous ones.
633 */ 599 */
634
635 /** 600 /**
636 * Attempt to instantiate a LinkDB whereas the datastore is not writable 601 * 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 */ 602 */
641 public function testConstructDatastoreNotWriteable() 603 public function testConstructDatastoreNotWriteable()
642 { 604 {
605 $this->expectException(\Shaarli\Bookmark\Exception\NotWritableDataStoreException::class);
606 $this->expectExceptionMessageRegExp('#Couldn\'t load data from the data store file "null".*#');
607
643 $conf = new ConfigManager('tests/utils/config/configJson'); 608 $conf = new ConfigManager('tests/utils/config/configJson');
644 $conf->set('resource.datastore', 'null/store.db'); 609 $conf->set('resource.datastore', 'null/store.db');
645 new BookmarkFileService($conf, $this->history, true); 610 new BookmarkFileService($conf, $this->history, $this->mutex, true);
646 } 611 }
647 612
648 /** 613 /**
@@ -652,7 +617,7 @@ class BookmarkFileServiceTest extends TestCase
652 { 617 {
653 unlink(self::$testDatastore); 618 unlink(self::$testDatastore);
654 $this->assertFileNotExists(self::$testDatastore); 619 $this->assertFileNotExists(self::$testDatastore);
655 new BookmarkFileService($this->conf, $this->history, true); 620 new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
656 $this->assertFileExists(self::$testDatastore); 621 $this->assertFileExists(self::$testDatastore);
657 622
658 // ensure the correct data has been written 623 // ensure the correct data has been written
@@ -666,7 +631,7 @@ class BookmarkFileServiceTest extends TestCase
666 { 631 {
667 unlink(self::$testDatastore); 632 unlink(self::$testDatastore);
668 $this->assertFileNotExists(self::$testDatastore); 633 $this->assertFileNotExists(self::$testDatastore);
669 $db = new \FakeBookmarkService($this->conf, $this->history, false); 634 $db = new \FakeBookmarkService($this->conf, $this->history, $this->mutex, false);
670 $this->assertFileNotExists(self::$testDatastore); 635 $this->assertFileNotExists(self::$testDatastore);
671 $this->assertInstanceOf(BookmarkArray::class, $db->getBookmarks()); 636 $this->assertInstanceOf(BookmarkArray::class, $db->getBookmarks());
672 $this->assertCount(0, $db->getBookmarks()); 637 $this->assertCount(0, $db->getBookmarks());
@@ -699,13 +664,13 @@ class BookmarkFileServiceTest extends TestCase
699 */ 664 */
700 public function testSave() 665 public function testSave()
701 { 666 {
702 $testDB = new BookmarkFileService($this->conf, $this->history, true); 667 $testDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
703 $dbSize = $testDB->count(); 668 $dbSize = $testDB->count();
704 669
705 $bookmark = new Bookmark(); 670 $bookmark = new Bookmark();
706 $testDB->add($bookmark); 671 $testDB->add($bookmark);
707 672
708 $testDB = new BookmarkFileService($this->conf, $this->history, true); 673 $testDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, true);
709 $this->assertEquals($dbSize + 1, $testDB->count()); 674 $this->assertEquals($dbSize + 1, $testDB->count());
710 } 675 }
711 676
@@ -715,28 +680,12 @@ class BookmarkFileServiceTest extends TestCase
715 public function testCountHiddenPublic() 680 public function testCountHiddenPublic()
716 { 681 {
717 $this->conf->set('privacy.hide_public_links', true); 682 $this->conf->set('privacy.hide_public_links', true);
718 $linkDB = new BookmarkFileService($this->conf, $this->history, false); 683 $linkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, false);
719 684
720 $this->assertEquals(0, $linkDB->count()); 685 $this->assertEquals(0, $linkDB->count());
721 } 686 }
722 687
723 /** 688 /**
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 689 * The URL corresponds to an existing entry in the DB
741 */ 690 */
742 public function testGetKnownLinkFromURL() 691 public function testGetKnownLinkFromURL()
@@ -744,7 +693,7 @@ class BookmarkFileServiceTest extends TestCase
744 $link = $this->publicLinkDB->findByUrl('http://mediagoblin.org/'); 693 $link = $this->publicLinkDB->findByUrl('http://mediagoblin.org/');
745 694
746 $this->assertNotEquals(false, $link); 695 $this->assertNotEquals(false, $link);
747 $this->assertContains( 696 $this->assertContainsPolyfill(
748 'A free software media publishing platform', 697 'A free software media publishing platform',
749 $link->getDescription() 698 $link->getDescription()
750 ); 699 );
@@ -783,6 +732,10 @@ class BookmarkFileServiceTest extends TestCase
783 // They need to be grouped with the first case found - order by date DESC: `sTuff`. 732 // They need to be grouped with the first case found - order by date DESC: `sTuff`.
784 'sTuff' => 2, 733 'sTuff' => 2,
785 'ut' => 1, 734 'ut' => 1,
735 'assurance' => 1,
736 'coding-style' => 1,
737 'quality' => 1,
738 'standards' => 1,
786 ], 739 ],
787 $this->publicLinkDB->bookmarksCountPerTag() 740 $this->publicLinkDB->bookmarksCountPerTag()
788 ); 741 );
@@ -811,12 +764,15 @@ class BookmarkFileServiceTest extends TestCase
811 'tag3' => 1, 764 'tag3' => 1,
812 'tag4' => 1, 765 'tag4' => 1,
813 'ut' => 1, 766 'ut' => 1,
767 'assurance' => 1,
768 'coding-style' => 1,
769 'quality' => 1,
770 'standards' => 1,
814 ], 771 ],
815 $this->privateLinkDB->bookmarksCountPerTag() 772 $this->privateLinkDB->bookmarksCountPerTag()
816 ); 773 );
817 $this->assertEquals( 774 $this->assertEquals(
818 [ 775 [
819 'web' => 4,
820 'cartoon' => 2, 776 'cartoon' => 2,
821 'gnu' => 1, 777 'gnu' => 1,
822 'dev' => 1, 778 'dev' => 1,
@@ -833,7 +789,6 @@ class BookmarkFileServiceTest extends TestCase
833 ); 789 );
834 $this->assertEquals( 790 $this->assertEquals(
835 [ 791 [
836 'web' => 1,
837 'html' => 1, 792 'html' => 1,
838 'w3c' => 1, 793 'w3c' => 1,
839 'css' => 1, 794 'css' => 1,
@@ -894,39 +849,70 @@ class BookmarkFileServiceTest extends TestCase
894 public function testFilterHashValid() 849 public function testFilterHashValid()
895 { 850 {
896 $request = smallHash('20150310_114651'); 851 $request = smallHash('20150310_114651');
897 $this->assertEquals( 852 $this->assertSame(
898 1, 853 $request,
899 count($this->publicLinkDB->findByHash($request)) 854 $this->publicLinkDB->findByHash($request)->getShortUrl()
900 ); 855 );
901 $request = smallHash('20150310_114633' . 8); 856 $request = smallHash('20150310_114633' . 8);
902 $this->assertEquals( 857 $this->assertSame(
903 1, 858 $request,
904 count($this->publicLinkDB->findByHash($request)) 859 $this->publicLinkDB->findByHash($request)->getShortUrl()
905 ); 860 );
906 } 861 }
907 862
908 /** 863 /**
909 * Test filterHash() with an invalid smallhash. 864 * Test filterHash() with an invalid smallhash.
910 *
911 * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException
912 */ 865 */
913 public function testFilterHashInValid1() 866 public function testFilterHashInValid1()
914 { 867 {
868 $this->expectException(BookmarkNotFoundException::class);
869
915 $request = 'blabla'; 870 $request = 'blabla';
916 $this->publicLinkDB->findByHash($request); 871 $this->publicLinkDB->findByHash($request);
917 } 872 }
918 873
919 /** 874 /**
920 * Test filterHash() with an empty smallhash. 875 * Test filterHash() with an empty smallhash.
921 *
922 * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException
923 */ 876 */
924 public function testFilterHashInValid() 877 public function testFilterHashInValid()
925 { 878 {
879 $this->expectException(BookmarkNotFoundException::class);
880
926 $this->publicLinkDB->findByHash(''); 881 $this->publicLinkDB->findByHash('');
927 } 882 }
928 883
929 /** 884 /**
885 * Test filterHash() on a private bookmark while logged out.
886 */
887 public function testFilterHashPrivateWhileLoggedOut()
888 {
889 $this->expectException(BookmarkNotFoundException::class);
890 $this->expectExceptionMessage('The link you are trying to reach does not exist or has been deleted');
891
892 $hash = smallHash('20141125_084734' . 6);
893
894 $this->publicLinkDB->findByHash($hash);
895 }
896
897 /**
898 * Test filterHash() with private key.
899 */
900 public function testFilterHashWithPrivateKey()
901 {
902 $hash = smallHash('20141125_084734' . 6);
903 $privateKey = 'this is usually auto generated';
904
905 $bookmark = $this->privateLinkDB->findByHash($hash);
906 $bookmark->addAdditionalContentEntry('private_key', $privateKey);
907 $this->privateLinkDB->save();
908
909 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, false);
910 $bookmark = $this->privateLinkDB->findByHash($hash, $privateKey);
911
912 static::assertSame(6, $bookmark->getId());
913 }
914
915 /**
930 * Test linksCountPerTag all tags without filter. 916 * Test linksCountPerTag all tags without filter.
931 * Equal occurrences should be sorted alphabetically. 917 * Equal occurrences should be sorted alphabetically.
932 */ 918 */
@@ -955,6 +941,10 @@ class BookmarkFileServiceTest extends TestCase
955 'tag4' => 1, 941 'tag4' => 1,
956 'ut' => 1, 942 'ut' => 1,
957 'w3c' => 1, 943 'w3c' => 1,
944 'assurance' => 1,
945 'coding-style' => 1,
946 'quality' => 1,
947 'standards' => 1,
958 ]; 948 ];
959 $tags = $this->privateLinkDB->bookmarksCountPerTag(); 949 $tags = $this->privateLinkDB->bookmarksCountPerTag();
960 950
@@ -968,7 +958,6 @@ class BookmarkFileServiceTest extends TestCase
968 public function testCountLinkPerTagAllWithFilter() 958 public function testCountLinkPerTagAllWithFilter()
969 { 959 {
970 $expected = [ 960 $expected = [
971 'gnu' => 2,
972 'hashtag' => 2, 961 'hashtag' => 2,
973 '-exclude' => 1, 962 '-exclude' => 1,
974 '.hidden' => 1, 963 '.hidden' => 1,
@@ -991,7 +980,6 @@ class BookmarkFileServiceTest extends TestCase
991 public function testCountLinkPerTagPublicWithFilter() 980 public function testCountLinkPerTagPublicWithFilter()
992 { 981 {
993 $expected = [ 982 $expected = [
994 'gnu' => 2,
995 'hashtag' => 2, 983 'hashtag' => 2,
996 '-exclude' => 1, 984 '-exclude' => 1,
997 '.hidden' => 1, 985 '.hidden' => 1,
@@ -1015,7 +1003,6 @@ class BookmarkFileServiceTest extends TestCase
1015 { 1003 {
1016 $expected = [ 1004 $expected = [
1017 'cartoon' => 1, 1005 'cartoon' => 1,
1018 'dev' => 1,
1019 'tag1' => 1, 1006 'tag1' => 1,
1020 'tag2' => 1, 1007 'tag2' => 1,
1021 'tag3' => 1, 1008 'tag3' => 1,
@@ -1056,6 +1043,10 @@ class BookmarkFileServiceTest extends TestCase
1056 'stallman' => 1, 1043 'stallman' => 1,
1057 'ut' => 1, 1044 'ut' => 1,
1058 'w3c' => 1, 1045 'w3c' => 1,
1046 'assurance' => 1,
1047 'coding-style' => 1,
1048 'quality' => 1,
1049 'standards' => 1,
1059 ]; 1050 ];
1060 $bookmark = new Bookmark(); 1051 $bookmark = new Bookmark();
1061 $bookmark->setTags(['newTagToCount', BookmarkMarkdownFormatter::NO_MD_TAG]); 1052 $bookmark->setTags(['newTagToCount', BookmarkMarkdownFormatter::NO_MD_TAG]);
@@ -1067,6 +1058,108 @@ class BookmarkFileServiceTest extends TestCase
1067 } 1058 }
1068 1059
1069 /** 1060 /**
1061 * Test find by dates in the middle of the datastore (sorted by dates) with a single bookmark as a result.
1062 */
1063 public function testFilterByDateMidTimePeriodSingleBookmark(): void
1064 {
1065 $bookmarks = $this->privateLinkDB->findByDate(
1066 DateTime::createFromFormat('Ymd_His', '20121206_150000'),
1067 DateTime::createFromFormat('Ymd_His', '20121206_160000'),
1068 $before,
1069 $after
1070 );
1071
1072 static::assertCount(1, $bookmarks);
1073
1074 static::assertSame(9, $bookmarks[0]->getId());
1075 static::assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_142300'), $before);
1076 static::assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_172539'), $after);
1077 }
1078
1079 /**
1080 * Test find by dates in the middle of the datastore (sorted by dates) with a multiple bookmarks as a result.
1081 */
1082 public function testFilterByDateMidTimePeriodMultipleBookmarks(): void
1083 {
1084 $bookmarks = $this->privateLinkDB->findByDate(
1085 DateTime::createFromFormat('Ymd_His', '20121206_150000'),
1086 DateTime::createFromFormat('Ymd_His', '20121206_180000'),
1087 $before,
1088 $after
1089 );
1090
1091 static::assertCount(2, $bookmarks);
1092
1093 static::assertSame(1, $bookmarks[0]->getId());
1094 static::assertSame(9, $bookmarks[1]->getId());
1095 static::assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_142300'), $before);
1096 static::assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_182539'), $after);
1097 }
1098
1099 /**
1100 * Test find by dates at the end of the datastore (sorted by dates).
1101 */
1102 public function testFilterByDateLastTimePeriod(): void
1103 {
1104 $after = new DateTime();
1105 $bookmarks = $this->privateLinkDB->findByDate(
1106 DateTime::createFromFormat('Ymd_His', '20150310_114640'),
1107 DateTime::createFromFormat('Ymd_His', '20450101_010101'),
1108 $before,
1109 $after
1110 );
1111
1112 static::assertCount(1, $bookmarks);
1113
1114 static::assertSame(41, $bookmarks[0]->getId());
1115 static::assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114633'), $before);
1116 static::assertNull($after);
1117 }
1118
1119 /**
1120 * Test find by dates at the beginning of the datastore (sorted by dates).
1121 */
1122 public function testFilterByDateFirstTimePeriod(): void
1123 {
1124 $before = new DateTime();
1125 $bookmarks = $this->privateLinkDB->findByDate(
1126 DateTime::createFromFormat('Ymd_His', '20000101_101010'),
1127 DateTime::createFromFormat('Ymd_His', '20100309_110000'),
1128 $before,
1129 $after
1130 );
1131
1132 static::assertCount(1, $bookmarks);
1133
1134 static::assertSame(11, $bookmarks[0]->getId());
1135 static::assertNull($before);
1136 static::assertEquals(DateTime::createFromFormat('Ymd_His', '20100310_101010'), $after);
1137 }
1138
1139 /**
1140 * Test getLatest with a sticky bookmark: it should be ignored and return the latest by creation date instead.
1141 */
1142 public function testGetLatestWithSticky(): void
1143 {
1144 $bookmark = $this->publicLinkDB->getLatest();
1145
1146 static::assertSame(41, $bookmark->getId());
1147 }
1148
1149 /**
1150 * Test getLatest with a sticky bookmark: it should be ignored and return the latest by creation date instead.
1151 */
1152 public function testGetLatestEmptyDatastore(): void
1153 {
1154 unlink($this->conf->get('resource.datastore'));
1155 $this->publicLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, false);
1156
1157 $bookmark = $this->publicLinkDB->getLatest();
1158
1159 static::assertNull($bookmark);
1160 }
1161
1162 /**
1070 * Allows to test LinkDB's private methods 1163 * Allows to test LinkDB's private methods
1071 * 1164 *
1072 * @see 1165 * @see