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.php141
1 files changed, 87 insertions, 54 deletions
diff --git a/tests/bookmark/BookmarkFileServiceTest.php b/tests/bookmark/BookmarkFileServiceTest.php
index 7b1906d3..c399822b 100644
--- a/tests/bookmark/BookmarkFileServiceTest.php
+++ b/tests/bookmark/BookmarkFileServiceTest.php
@@ -6,7 +6,6 @@
6namespace Shaarli\Bookmark; 6namespace Shaarli\Bookmark;
7 7
8use DateTime; 8use DateTime;
9use PHPUnit\Framework\TestCase;
10use ReferenceLinkDB; 9use ReferenceLinkDB;
11use ReflectionClass; 10use ReflectionClass;
12use Shaarli; 11use Shaarli;
@@ -14,6 +13,7 @@ use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
14use Shaarli\Config\ConfigManager; 13use Shaarli\Config\ConfigManager;
15use Shaarli\Formatter\BookmarkMarkdownFormatter; 14use Shaarli\Formatter\BookmarkMarkdownFormatter;
16use Shaarli\History; 15use Shaarli\History;
16use Shaarli\TestCase;
17 17
18/** 18/**
19 * Unitary tests for LegacyLinkDBTest 19 * Unitary tests for LegacyLinkDBTest
@@ -66,7 +66,7 @@ class BookmarkFileServiceTest extends TestCase
66 * 66 *
67 * Resets test data for each test 67 * Resets test data for each test
68 */ 68 */
69 protected function setUp() 69 protected function setUp(): void
70 { 70 {
71 if (file_exists(self::$testDatastore)) { 71 if (file_exists(self::$testDatastore)) {
72 unlink(self::$testDatastore); 72 unlink(self::$testDatastore);
@@ -134,11 +134,11 @@ class BookmarkFileServiceTest extends TestCase
134 134
135 /** 135 /**
136 * Test get() method for an undefined bookmark 136 * Test get() method for an undefined bookmark
137 *
138 * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
139 */ 137 */
140 public function testGetUndefined() 138 public function testGetUndefined()
141 { 139 {
140 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class);
141
142 $this->privateLinkDB->get(666); 142 $this->privateLinkDB->get(666);
143 } 143 }
144 144
@@ -230,13 +230,13 @@ class BookmarkFileServiceTest extends TestCase
230 230
231 /** 231 /**
232 * Test add() method for a bookmark without any field set and without writing the data store 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 */ 233 */
236 public function testAddMinimalNoWrite() 234 public function testAddMinimalNoWrite()
237 { 235 {
236 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class);
237
238 $bookmark = new Bookmark(); 238 $bookmark = new Bookmark();
239 $this->privateLinkDB->add($bookmark); 239 $this->privateLinkDB->add($bookmark, false);
240 240
241 $bookmark = $this->privateLinkDB->get(43); 241 $bookmark = $this->privateLinkDB->get(43);
242 $this->assertEquals(43, $bookmark->getId()); 242 $this->assertEquals(43, $bookmark->getId());
@@ -249,34 +249,34 @@ class BookmarkFileServiceTest extends TestCase
249 249
250 /** 250 /**
251 * Test add() method while logged out 251 * Test add() method while logged out
252 *
253 * @expectedException \Exception
254 * @expectedExceptionMessage You're not authorized to alter the datastore
255 */ 252 */
256 public function testAddLoggedOut() 253 public function testAddLoggedOut()
257 { 254 {
255 $this->expectException(\Exception::class);
256 $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
257
258 $this->publicLinkDB->add(new Bookmark()); 258 $this->publicLinkDB->add(new Bookmark());
259 } 259 }
260 260
261 /** 261 /**
262 * Test add() method with an entry which is not a bookmark instance 262 * Test add() method with an entry which is not a bookmark instance
263 *
264 * @expectedException \Exception
265 * @expectedExceptionMessage Provided data is invalid
266 */ 263 */
267 public function testAddNotABookmark() 264 public function testAddNotABookmark()
268 { 265 {
266 $this->expectException(\Exception::class);
267 $this->expectExceptionMessage('Provided data is invalid');
268
269 $this->privateLinkDB->add(['title' => 'hi!']); 269 $this->privateLinkDB->add(['title' => 'hi!']);
270 } 270 }
271 271
272 /** 272 /**
273 * Test add() method with a Bookmark already containing an ID 273 * Test add() method with a Bookmark already containing an ID
274 *
275 * @expectedException \Exception
276 * @expectedExceptionMessage This bookmarks already exists
277 */ 274 */
278 public function testAddWithId() 275 public function testAddWithId()
279 { 276 {
277 $this->expectException(\Exception::class);
278 $this->expectExceptionMessage('This bookmarks already exists');
279
280 $bookmark = new Bookmark(); 280 $bookmark = new Bookmark();
281 $bookmark->setId(43); 281 $bookmark->setId(43);
282 $this->privateLinkDB->add($bookmark); 282 $this->privateLinkDB->add($bookmark);
@@ -397,44 +397,44 @@ class BookmarkFileServiceTest extends TestCase
397 397
398 /** 398 /**
399 * Test set() method while logged out 399 * Test set() method while logged out
400 *
401 * @expectedException \Exception
402 * @expectedExceptionMessage You're not authorized to alter the datastore
403 */ 400 */
404 public function testSetLoggedOut() 401 public function testSetLoggedOut()
405 { 402 {
403 $this->expectException(\Exception::class);
404 $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
405
406 $this->publicLinkDB->set(new Bookmark()); 406 $this->publicLinkDB->set(new Bookmark());
407 } 407 }
408 408
409 /** 409 /**
410 * Test set() method with an entry which is not a bookmark instance 410 * Test set() method with an entry which is not a bookmark instance
411 *
412 * @expectedException \Exception
413 * @expectedExceptionMessage Provided data is invalid
414 */ 411 */
415 public function testSetNotABookmark() 412 public function testSetNotABookmark()
416 { 413 {
414 $this->expectException(\Exception::class);
415 $this->expectExceptionMessage('Provided data is invalid');
416
417 $this->privateLinkDB->set(['title' => 'hi!']); 417 $this->privateLinkDB->set(['title' => 'hi!']);
418 } 418 }
419 419
420 /** 420 /**
421 * Test set() method with a Bookmark without an ID defined. 421 * Test set() method with a Bookmark without an ID defined.
422 *
423 * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
424 */ 422 */
425 public function testSetWithoutId() 423 public function testSetWithoutId()
426 { 424 {
425 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class);
426
427 $bookmark = new Bookmark(); 427 $bookmark = new Bookmark();
428 $this->privateLinkDB->set($bookmark); 428 $this->privateLinkDB->set($bookmark);
429 } 429 }
430 430
431 /** 431 /**
432 * Test set() method with a Bookmark with an unknow ID 432 * Test set() method with a Bookmark with an unknow ID
433 *
434 * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
435 */ 433 */
436 public function testSetWithUnknownId() 434 public function testSetWithUnknownId()
437 { 435 {
436 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class);
437
438 $bookmark = new Bookmark(); 438 $bookmark = new Bookmark();
439 $bookmark->setId(666); 439 $bookmark->setId(666);
440 $this->privateLinkDB->set($bookmark); 440 $this->privateLinkDB->set($bookmark);
@@ -481,23 +481,23 @@ class BookmarkFileServiceTest extends TestCase
481 481
482 /** 482 /**
483 * Test addOrSet() method while logged out 483 * Test addOrSet() method while logged out
484 *
485 * @expectedException \Exception
486 * @expectedExceptionMessage You're not authorized to alter the datastore
487 */ 484 */
488 public function testAddOrSetLoggedOut() 485 public function testAddOrSetLoggedOut()
489 { 486 {
487 $this->expectException(\Exception::class);
488 $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
489
490 $this->publicLinkDB->addOrSet(new Bookmark()); 490 $this->publicLinkDB->addOrSet(new Bookmark());
491 } 491 }
492 492
493 /** 493 /**
494 * Test addOrSet() method with an entry which is not a bookmark instance 494 * Test addOrSet() method with an entry which is not a bookmark instance
495 *
496 * @expectedException \Exception
497 * @expectedExceptionMessage Provided data is invalid
498 */ 495 */
499 public function testAddOrSetNotABookmark() 496 public function testAddOrSetNotABookmark()
500 { 497 {
498 $this->expectException(\Exception::class);
499 $this->expectExceptionMessage('Provided data is invalid');
500
501 $this->privateLinkDB->addOrSet(['title' => 'hi!']); 501 $this->privateLinkDB->addOrSet(['title' => 'hi!']);
502 } 502 }
503 503
@@ -524,11 +524,11 @@ class BookmarkFileServiceTest extends TestCase
524 524
525 /** 525 /**
526 * Test remove() method with an existing Bookmark 526 * Test remove() method with an existing Bookmark
527 *
528 * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
529 */ 527 */
530 public function testRemoveExisting() 528 public function testRemoveExisting()
531 { 529 {
530 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class);
531
532 $bookmark = $this->privateLinkDB->get(42); 532 $bookmark = $this->privateLinkDB->get(42);
533 $this->privateLinkDB->remove($bookmark); 533 $this->privateLinkDB->remove($bookmark);
534 534
@@ -548,34 +548,34 @@ class BookmarkFileServiceTest extends TestCase
548 548
549 /** 549 /**
550 * Test remove() method while logged out 550 * Test remove() method while logged out
551 *
552 * @expectedException \Exception
553 * @expectedExceptionMessage You're not authorized to alter the datastore
554 */ 551 */
555 public function testRemoveLoggedOut() 552 public function testRemoveLoggedOut()
556 { 553 {
554 $this->expectException(\Exception::class);
555 $this->expectExceptionMessage('You\'re not authorized to alter the datastore');
556
557 $bookmark = $this->privateLinkDB->get(42); 557 $bookmark = $this->privateLinkDB->get(42);
558 $this->publicLinkDB->remove($bookmark); 558 $this->publicLinkDB->remove($bookmark);
559 } 559 }
560 560
561 /** 561 /**
562 * Test remove() method with an entry which is not a bookmark instance 562 * Test remove() method with an entry which is not a bookmark instance
563 *
564 * @expectedException \Exception
565 * @expectedExceptionMessage Provided data is invalid
566 */ 563 */
567 public function testRemoveNotABookmark() 564 public function testRemoveNotABookmark()
568 { 565 {
566 $this->expectException(\Exception::class);
567 $this->expectExceptionMessage('Provided data is invalid');
568
569 $this->privateLinkDB->remove(['title' => 'hi!']); 569 $this->privateLinkDB->remove(['title' => 'hi!']);
570 } 570 }
571 571
572 /** 572 /**
573 * Test remove() method with a Bookmark with an unknown ID 573 * Test remove() method with a Bookmark with an unknown ID
574 *
575 * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
576 */ 574 */
577 public function testRemoveWithUnknownId() 575 public function testRemoveWithUnknownId()
578 { 576 {
577 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class);
578
579 $bookmark = new Bookmark(); 579 $bookmark = new Bookmark();
580 $bookmark->setId(666); 580 $bookmark->setId(666);
581 $this->privateLinkDB->remove($bookmark); 581 $this->privateLinkDB->remove($bookmark);
@@ -615,14 +615,18 @@ class BookmarkFileServiceTest extends TestCase
615 { 615 {
616 $dbSize = $this->privateLinkDB->count(); 616 $dbSize = $this->privateLinkDB->count();
617 $this->privateLinkDB->initialize(); 617 $this->privateLinkDB->initialize();
618 $this->assertEquals($dbSize + 2, $this->privateLinkDB->count()); 618 $this->assertEquals($dbSize + 3, $this->privateLinkDB->count());
619 $this->assertEquals( 619 $this->assertStringStartsWith(
620 'My secret stuff... - Pastebin.com', 620 'Shaarli will automatically pick up the thumbnail for links to a variety of websites.',
621 $this->privateLinkDB->get(43)->getTitle() 621 $this->privateLinkDB->get(43)->getDescription()
622 ); 622 );
623 $this->assertEquals( 623 $this->assertStringStartsWith(
624 'The personal, minimalist, super-fast, database free, bookmarking service', 624 'Adding a shaare without entering a URL creates a text-only "note" post such as this one.',
625 $this->privateLinkDB->get(44)->getTitle() 625 $this->privateLinkDB->get(44)->getDescription()
626 );
627 $this->assertStringStartsWith(
628 'Welcome to Shaarli!',
629 $this->privateLinkDB->get(45)->getDescription()
626 ); 630 );
627 } 631 }
628 632
@@ -631,15 +635,14 @@ class BookmarkFileServiceTest extends TestCase
631 * to make sure that nothing have been broken in the migration process. 635 * 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. 636 * They mostly cover search/filters. Some of them might be redundant with the previous ones.
633 */ 637 */
634
635 /** 638 /**
636 * Attempt to instantiate a LinkDB whereas the datastore is not writable 639 * 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 */ 640 */
641 public function testConstructDatastoreNotWriteable() 641 public function testConstructDatastoreNotWriteable()
642 { 642 {
643 $this->expectException(\Shaarli\Bookmark\Exception\NotWritableDataStoreException::class);
644 $this->expectExceptionMessageRegExp('#Couldn\'t load data from the data store file "null".*#');
645
643 $conf = new ConfigManager('tests/utils/config/configJson'); 646 $conf = new ConfigManager('tests/utils/config/configJson');
644 $conf->set('resource.datastore', 'null/store.db'); 647 $conf->set('resource.datastore', 'null/store.db');
645 new BookmarkFileService($conf, $this->history, true); 648 new BookmarkFileService($conf, $this->history, true);
@@ -744,7 +747,7 @@ class BookmarkFileServiceTest extends TestCase
744 $link = $this->publicLinkDB->findByUrl('http://mediagoblin.org/'); 747 $link = $this->publicLinkDB->findByUrl('http://mediagoblin.org/');
745 748
746 $this->assertNotEquals(false, $link); 749 $this->assertNotEquals(false, $link);
747 $this->assertContains( 750 $this->assertContainsPolyfill(
748 'A free software media publishing platform', 751 'A free software media publishing platform',
749 $link->getDescription() 752 $link->getDescription()
750 ); 753 );
@@ -1062,6 +1065,36 @@ class BookmarkFileServiceTest extends TestCase
1062 } 1065 }
1063 1066
1064 /** 1067 /**
1068 * Test filterDay while logged in
1069 */
1070 public function testFilterDayLoggedIn(): void
1071 {
1072 $bookmarks = $this->privateLinkDB->filterDay('20121206');
1073 $expectedIds = [4, 9, 1, 0];
1074
1075 static::assertCount(4, $bookmarks);
1076 foreach ($bookmarks as $bookmark) {
1077 $i = ($i ?? -1) + 1;
1078 static::assertSame($expectedIds[$i], $bookmark->getId());
1079 }
1080 }
1081
1082 /**
1083 * Test filterDay while logged out
1084 */
1085 public function testFilterDayLoggedOut(): void
1086 {
1087 $bookmarks = $this->publicLinkDB->filterDay('20121206');
1088 $expectedIds = [4, 9, 1];
1089
1090 static::assertCount(3, $bookmarks);
1091 foreach ($bookmarks as $bookmark) {
1092 $i = ($i ?? -1) + 1;
1093 static::assertSame($expectedIds[$i], $bookmark->getId());
1094 }
1095 }
1096
1097 /**
1065 * Allows to test LinkDB's private methods 1098 * Allows to test LinkDB's private methods
1066 * 1099 *
1067 * @see 1100 * @see