From e26e2060f5470ce8bf4c5973284bae07b8af170a Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Fri, 17 Jan 2020 21:34:12 +0100 Subject: Add and update unit test for the new system (Bookmark + Service) See #1307 --- tests/bookmark/BookmarkFileServiceTest.php | 1042 ++++++++++++++++++++++++++++ 1 file changed, 1042 insertions(+) create mode 100644 tests/bookmark/BookmarkFileServiceTest.php (limited to 'tests/bookmark/BookmarkFileServiceTest.php') diff --git a/tests/bookmark/BookmarkFileServiceTest.php b/tests/bookmark/BookmarkFileServiceTest.php new file mode 100644 index 00000000..1b438a7f --- /dev/null +++ b/tests/bookmark/BookmarkFileServiceTest.php @@ -0,0 +1,1042 @@ +conf = new ConfigManager(self::$testConf); + $this->conf->set('resource.datastore', self::$testDatastore); + $this->conf->set('resource.updates', self::$testUpdates); + $this->refDB = new \ReferenceLinkDB(); + $this->refDB->write(self::$testDatastore); + $this->history = new History('sandbox/history.php'); + $this->publicLinkDB = new BookmarkFileService($this->conf, $this->history, false); + $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); + } + + /** + * Test migrate() method with a legacy datastore. + */ + public function testDatabaseMigration() + { + if (!defined('SHAARLI_VERSION')) { + define('SHAARLI_VERSION', 'dev'); + } + + $this->refDB = new \ReferenceLinkDB(true); + $this->refDB->write(self::$testDatastore); + $db = self::getMethod('migrate'); + $db->invokeArgs($this->privateLinkDB, []); + + $db = new \FakeBookmarkService($this->conf, $this->history, true); + $this->assertInstanceOf(BookmarkArray::class, $db->getBookmarks()); + $this->assertEquals($this->refDB->countLinks(), $db->count()); + } + + /** + * Test get() method for a defined and saved bookmark + */ + public function testGetDefinedSaved() + { + $bookmark = $this->privateLinkDB->get(42); + $this->assertEquals(42, $bookmark->getId()); + $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle()); + } + + /** + * Test get() method for a defined and not saved bookmark + */ + public function testGetDefinedNotSaved() + { + $bookmark = new Bookmark(); + $this->privateLinkDB->add($bookmark); + $createdBookmark = $this->privateLinkDB->get(43); + $this->assertEquals(43, $createdBookmark->getId()); + $this->assertEmpty($createdBookmark->getDescription()); + } + + /** + * Test get() method for an undefined bookmark + * + * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException + */ + public function testGetUndefined() + { + $this->privateLinkDB->get(666); + } + + /** + * Test add() method for a bookmark fully built + */ + public function testAddFull() + { + $bookmark = new Bookmark(); + $bookmark->setUrl($url = 'https://domain.tld/index.php'); + $bookmark->setShortUrl('abc'); + $bookmark->setTitle($title = 'This a brand new bookmark'); + $bookmark->setDescription($desc = 'It should be created and written'); + $bookmark->setTags($tags = ['tag1', 'tagssss']); + $bookmark->setThumbnail($thumb = 'http://thumb.tld/dle.png'); + $bookmark->setPrivate(true); + $bookmark->setSticky(true); + $bookmark->setCreated($created = DateTime::createFromFormat('Ymd_His', '20190518_140354')); + $bookmark->setUpdated($updated = DateTime::createFromFormat('Ymd_His', '20190518_150354')); + + $this->privateLinkDB->add($bookmark); + $bookmark = $this->privateLinkDB->get(43); + $this->assertEquals(43, $bookmark->getId()); + $this->assertEquals($url, $bookmark->getUrl()); + $this->assertEquals('abc', $bookmark->getShortUrl()); + $this->assertEquals($title, $bookmark->getTitle()); + $this->assertEquals($desc, $bookmark->getDescription()); + $this->assertEquals($tags, $bookmark->getTags()); + $this->assertEquals($thumb, $bookmark->getThumbnail()); + $this->assertTrue($bookmark->isPrivate()); + $this->assertTrue($bookmark->isSticky()); + $this->assertEquals($created, $bookmark->getCreated()); + $this->assertEquals($updated, $bookmark->getUpdated()); + + // reload from file + $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); + + $bookmark = $this->privateLinkDB->get(43); + $this->assertEquals(43, $bookmark->getId()); + $this->assertEquals($url, $bookmark->getUrl()); + $this->assertEquals('abc', $bookmark->getShortUrl()); + $this->assertEquals($title, $bookmark->getTitle()); + $this->assertEquals($desc, $bookmark->getDescription()); + $this->assertEquals($tags, $bookmark->getTags()); + $this->assertEquals($thumb, $bookmark->getThumbnail()); + $this->assertTrue($bookmark->isPrivate()); + $this->assertTrue($bookmark->isSticky()); + $this->assertEquals($created, $bookmark->getCreated()); + $this->assertEquals($updated, $bookmark->getUpdated()); + } + + /** + * Test add() method for a bookmark without any field set + */ + public function testAddMinimal() + { + $bookmark = new Bookmark(); + $this->privateLinkDB->add($bookmark); + + $bookmark = $this->privateLinkDB->get(43); + $this->assertEquals(43, $bookmark->getId()); + $this->assertRegExp('/\?[\w\-]{6}/', $bookmark->getUrl()); + $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl()); + $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle()); + $this->assertEmpty($bookmark->getDescription()); + $this->assertEmpty($bookmark->getTags()); + $this->assertEmpty($bookmark->getThumbnail()); + $this->assertFalse($bookmark->isPrivate()); + $this->assertFalse($bookmark->isSticky()); + $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getCreated()); + $this->assertNull($bookmark->getUpdated()); + + // reload from file + $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); + + $bookmark = $this->privateLinkDB->get(43); + $this->assertEquals(43, $bookmark->getId()); + $this->assertRegExp('/\?[\w\-]{6}/', $bookmark->getUrl()); + $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl()); + $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle()); + $this->assertEmpty($bookmark->getDescription()); + $this->assertEmpty($bookmark->getTags()); + $this->assertEmpty($bookmark->getThumbnail()); + $this->assertFalse($bookmark->isPrivate()); + $this->assertFalse($bookmark->isSticky()); + $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getCreated()); + $this->assertNull($bookmark->getUpdated()); + } + + /** + * Test add() method for a bookmark without any field set and without writing the data store + * + * @expectedExceptionMessage Shaarli\Bookmark\Exception\BookmarkNotFoundException + */ + public function testAddMinimalNoWrite() + { + $bookmark = new Bookmark(); + $this->privateLinkDB->add($bookmark); + + $bookmark = $this->privateLinkDB->get(43); + $this->assertEquals(43, $bookmark->getId()); + + // reload from file + $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); + + $this->privateLinkDB->get(43); + } + + /** + * Test add() method while logged out + * + * @expectedException \Exception + * @expectedExceptionMessage You're not authorized to alter the datastore + */ + public function testAddLoggedOut() + { + $this->publicLinkDB->add(new Bookmark()); + } + + /** + * Test add() method with an entry which is not a bookmark instance + * + * @expectedException \Exception + * @expectedExceptionMessage Provided data is invalid + */ + public function testAddNotABookmark() + { + $this->privateLinkDB->add(['title' => 'hi!']); + } + + /** + * Test add() method with a Bookmark already containing an ID + * + * @expectedException \Exception + * @expectedExceptionMessage This bookmarks already exists + */ + public function testAddWithId() + { + $bookmark = new Bookmark(); + $bookmark->setId(43); + $this->privateLinkDB->add($bookmark); + } + + /** + * Test set() method for a bookmark fully built + */ + public function testSetFull() + { + $bookmark = $this->privateLinkDB->get(42); + $bookmark->setUrl($url = 'https://domain.tld/index.php'); + $bookmark->setShortUrl('abc'); + $bookmark->setTitle($title = 'This a brand new bookmark'); + $bookmark->setDescription($desc = 'It should be created and written'); + $bookmark->setTags($tags = ['tag1', 'tagssss']); + $bookmark->setThumbnail($thumb = 'http://thumb.tld/dle.png'); + $bookmark->setPrivate(true); + $bookmark->setSticky(true); + $bookmark->setCreated($created = DateTime::createFromFormat('Ymd_His', '20190518_140354')); + $bookmark->setUpdated($updated = DateTime::createFromFormat('Ymd_His', '20190518_150354')); + + $this->privateLinkDB->set($bookmark); + $bookmark = $this->privateLinkDB->get(42); + $this->assertEquals(42, $bookmark->getId()); + $this->assertEquals($url, $bookmark->getUrl()); + $this->assertEquals('abc', $bookmark->getShortUrl()); + $this->assertEquals($title, $bookmark->getTitle()); + $this->assertEquals($desc, $bookmark->getDescription()); + $this->assertEquals($tags, $bookmark->getTags()); + $this->assertEquals($thumb, $bookmark->getThumbnail()); + $this->assertTrue($bookmark->isPrivate()); + $this->assertTrue($bookmark->isSticky()); + $this->assertEquals($created, $bookmark->getCreated()); + $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated()); + + // reload from file + $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); + + $bookmark = $this->privateLinkDB->get(42); + $this->assertEquals(42, $bookmark->getId()); + $this->assertEquals($url, $bookmark->getUrl()); + $this->assertEquals('abc', $bookmark->getShortUrl()); + $this->assertEquals($title, $bookmark->getTitle()); + $this->assertEquals($desc, $bookmark->getDescription()); + $this->assertEquals($tags, $bookmark->getTags()); + $this->assertEquals($thumb, $bookmark->getThumbnail()); + $this->assertTrue($bookmark->isPrivate()); + $this->assertTrue($bookmark->isSticky()); + $this->assertEquals($created, $bookmark->getCreated()); + $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated()); + } + + /** + * Test set() method for a bookmark without any field set + */ + public function testSetMinimal() + { + $bookmark = $this->privateLinkDB->get(42); + $this->privateLinkDB->set($bookmark); + + $bookmark = $this->privateLinkDB->get(42); + $this->assertEquals(42, $bookmark->getId()); + $this->assertEquals('?WDWyig', $bookmark->getUrl()); + $this->assertEquals('1eYJ1Q', $bookmark->getShortUrl()); + $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle()); + $this->assertEquals('Used to test bookmarks reordering.', $bookmark->getDescription()); + $this->assertEquals(['ut'], $bookmark->getTags()); + $this->assertFalse($bookmark->getThumbnail()); + $this->assertFalse($bookmark->isPrivate()); + $this->assertFalse($bookmark->isSticky()); + $this->assertEquals( + DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20100310_101010'), + $bookmark->getCreated() + ); + $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated()); + + // reload from file + $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); + + $bookmark = $this->privateLinkDB->get(42); + $this->assertEquals(42, $bookmark->getId()); + $this->assertEquals('?WDWyig', $bookmark->getUrl()); + $this->assertEquals('1eYJ1Q', $bookmark->getShortUrl()); + $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle()); + $this->assertEquals('Used to test bookmarks reordering.', $bookmark->getDescription()); + $this->assertEquals(['ut'], $bookmark->getTags()); + $this->assertFalse($bookmark->getThumbnail()); + $this->assertFalse($bookmark->isPrivate()); + $this->assertFalse($bookmark->isSticky()); + $this->assertEquals( + DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20100310_101010'), + $bookmark->getCreated() + ); + $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated()); + } + + /** + * Test set() method for a bookmark without any field set and without writing the data store + */ + public function testSetMinimalNoWrite() + { + $bookmark = $this->privateLinkDB->get(42); + $bookmark->setTitle($title = 'hi!'); + $this->privateLinkDB->set($bookmark, false); + + $bookmark = $this->privateLinkDB->get(42); + $this->assertEquals(42, $bookmark->getId()); + $this->assertEquals($title, $bookmark->getTitle()); + + // reload from file + $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); + + $bookmark = $this->privateLinkDB->get(42); + $this->assertEquals(42, $bookmark->getId()); + $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle()); + } + + /** + * Test set() method while logged out + * + * @expectedException \Exception + * @expectedExceptionMessage You're not authorized to alter the datastore + */ + public function testSetLoggedOut() + { + $this->publicLinkDB->set(new Bookmark()); + } + + /** + * Test set() method with an entry which is not a bookmark instance + * + * @expectedException \Exception + * @expectedExceptionMessage Provided data is invalid + */ + public function testSetNotABookmark() + { + $this->privateLinkDB->set(['title' => 'hi!']); + } + + /** + * Test set() method with a Bookmark without an ID defined. + * + * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException + */ + public function testSetWithoutId() + { + $bookmark = new Bookmark(); + $this->privateLinkDB->set($bookmark); + } + + /** + * Test set() method with a Bookmark with an unknow ID + * + * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException + */ + public function testSetWithUnknownId() + { + $bookmark = new Bookmark(); + $bookmark->setId(666); + $this->privateLinkDB->set($bookmark); + } + + /** + * Test addOrSet() method with a new ID + */ + public function testAddOrSetNew() + { + $bookmark = new Bookmark(); + $this->privateLinkDB->addOrSet($bookmark); + + $bookmark = $this->privateLinkDB->get(43); + $this->assertEquals(43, $bookmark->getId()); + + // reload from file + $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); + + $bookmark = $this->privateLinkDB->get(43); + $this->assertEquals(43, $bookmark->getId()); + } + + /** + * Test addOrSet() method with an existing ID + */ + public function testAddOrSetExisting() + { + $bookmark = $this->privateLinkDB->get(42); + $bookmark->setTitle($title = 'hi!'); + $this->privateLinkDB->addOrSet($bookmark); + + $bookmark = $this->privateLinkDB->get(42); + $this->assertEquals(42, $bookmark->getId()); + $this->assertEquals($title, $bookmark->getTitle()); + + // reload from file + $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); + + $bookmark = $this->privateLinkDB->get(42); + $this->assertEquals(42, $bookmark->getId()); + $this->assertEquals($title, $bookmark->getTitle()); + } + + /** + * Test addOrSet() method while logged out + * + * @expectedException \Exception + * @expectedExceptionMessage You're not authorized to alter the datastore + */ + public function testAddOrSetLoggedOut() + { + $this->publicLinkDB->addOrSet(new Bookmark()); + } + + /** + * Test addOrSet() method with an entry which is not a bookmark instance + * + * @expectedException \Exception + * @expectedExceptionMessage Provided data is invalid + */ + public function testAddOrSetNotABookmark() + { + $this->privateLinkDB->addOrSet(['title' => 'hi!']); + } + + /** + * Test addOrSet() method for a bookmark without any field set and without writing the data store + */ + public function testAddOrSetMinimalNoWrite() + { + $bookmark = $this->privateLinkDB->get(42); + $bookmark->setTitle($title = 'hi!'); + $this->privateLinkDB->addOrSet($bookmark, false); + + $bookmark = $this->privateLinkDB->get(42); + $this->assertEquals(42, $bookmark->getId()); + $this->assertEquals($title, $bookmark->getTitle()); + + // reload from file + $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); + + $bookmark = $this->privateLinkDB->get(42); + $this->assertEquals(42, $bookmark->getId()); + $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle()); + } + + /** + * Test remove() method with an existing Bookmark + * + * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException + */ + public function testRemoveExisting() + { + $bookmark = $this->privateLinkDB->get(42); + $this->privateLinkDB->remove($bookmark); + + $exception = null; + try { + $this->privateLinkDB->get(42); + } catch (BookmarkNotFoundException $e) { + $exception = $e; + } + $this->assertInstanceOf(BookmarkNotFoundException::class, $exception); + + // reload from file + $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); + + $this->privateLinkDB->get(42); + } + + /** + * Test remove() method while logged out + * + * @expectedException \Exception + * @expectedExceptionMessage You're not authorized to alter the datastore + */ + public function testRemoveLoggedOut() + { + $bookmark = $this->privateLinkDB->get(42); + $this->publicLinkDB->remove($bookmark); + } + + /** + * Test remove() method with an entry which is not a bookmark instance + * + * @expectedException \Exception + * @expectedExceptionMessage Provided data is invalid + */ + public function testRemoveNotABookmark() + { + $this->privateLinkDB->remove(['title' => 'hi!']); + } + + /** + * Test remove() method with a Bookmark with an unknown ID + * + * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException + */ + public function testRemoveWithUnknownId() + { + $bookmark = new Bookmark(); + $bookmark->setId(666); + $this->privateLinkDB->remove($bookmark); + } + + /** + * Test exists() method + */ + public function testExists() + { + $this->assertTrue($this->privateLinkDB->exists(42)); // public + $this->assertTrue($this->privateLinkDB->exists(6)); // private + + $this->assertTrue($this->privateLinkDB->exists(42, BookmarkFilter::$ALL)); + $this->assertTrue($this->privateLinkDB->exists(6, BookmarkFilter::$ALL)); + + $this->assertTrue($this->privateLinkDB->exists(42, BookmarkFilter::$PUBLIC)); + $this->assertFalse($this->privateLinkDB->exists(6, BookmarkFilter::$PUBLIC)); + + $this->assertFalse($this->privateLinkDB->exists(42, BookmarkFilter::$PRIVATE)); + $this->assertTrue($this->privateLinkDB->exists(6, BookmarkFilter::$PRIVATE)); + + $this->assertTrue($this->publicLinkDB->exists(42)); + $this->assertFalse($this->publicLinkDB->exists(6)); + + $this->assertTrue($this->publicLinkDB->exists(42, BookmarkFilter::$PUBLIC)); + $this->assertFalse($this->publicLinkDB->exists(6, BookmarkFilter::$PUBLIC)); + + $this->assertFalse($this->publicLinkDB->exists(42, BookmarkFilter::$PRIVATE)); + $this->assertTrue($this->publicLinkDB->exists(6, BookmarkFilter::$PRIVATE)); + } + + /** + * Test initialize() method + */ + public function testInitialize() + { + $dbSize = $this->privateLinkDB->count(); + $this->privateLinkDB->initialize(); + $this->assertEquals($dbSize + 2, $this->privateLinkDB->count()); + $this->assertEquals( + 'My secret stuff... - Pastebin.com', + $this->privateLinkDB->get(43)->getTitle() + ); + $this->assertEquals( + 'The personal, minimalist, super-fast, database free, bookmarking service', + $this->privateLinkDB->get(44)->getTitle() + ); + } + + /* + * The following tests have been taken from the legacy LinkDB test and adapted + * to make sure that nothing have been broken in the migration process. + * They mostly cover search/filters. Some of them might be redundant with the previous ones. + */ + + /** + * Attempt to instantiate a LinkDB whereas the datastore is not writable + * + * @expectedException Shaarli\Bookmark\Exception\NotWritableDataStoreException + * @expectedExceptionMessageRegExp #Couldn't load data from the data store file "null".*# + */ + public function testConstructDatastoreNotWriteable() + { + $conf = new ConfigManager('tests/utils/config/configJson'); + $conf->set('resource.datastore', 'null/store.db'); + new BookmarkFileService($conf, $this->history, true); + } + + /** + * The DB doesn't exist, ensure it is created with an empty datastore + */ + public function testCheckDBNewLoggedIn() + { + unlink(self::$testDatastore); + $this->assertFileNotExists(self::$testDatastore); + new BookmarkFileService($this->conf, $this->history, true); + $this->assertFileExists(self::$testDatastore); + + // ensure the correct data has been written + $this->assertGreaterThan(0, filesize(self::$testDatastore)); + } + + /** + * The DB doesn't exist, but not logged in, ensure it initialized, but the file is not written + */ + public function testCheckDBNewLoggedOut() + { + unlink(self::$testDatastore); + $this->assertFileNotExists(self::$testDatastore); + $db = new \FakeBookmarkService($this->conf, $this->history, false); + $this->assertFileNotExists(self::$testDatastore); + $this->assertInstanceOf(BookmarkArray::class, $db->getBookmarks()); + $this->assertCount(0, $db->getBookmarks()); + } + + /** + * Load public bookmarks from the DB + */ + public function testReadPublicDB() + { + $this->assertEquals( + $this->refDB->countPublicLinks(), + $this->publicLinkDB->count() + ); + } + + /** + * Load public and private bookmarks from the DB + */ + public function testReadPrivateDB() + { + $this->assertEquals( + $this->refDB->countLinks(), + $this->privateLinkDB->count() + ); + } + + /** + * Save the bookmarks to the DB + */ + public function testSave() + { + $testDB = new BookmarkFileService($this->conf, $this->history, true); + $dbSize = $testDB->count(); + + $bookmark = new Bookmark(); + $testDB->add($bookmark); + + $testDB = new BookmarkFileService($this->conf, $this->history, true); + $this->assertEquals($dbSize + 1, $testDB->count()); + } + + /** + * Count existing bookmarks - public bookmarks hidden + */ + public function testCountHiddenPublic() + { + $this->conf->set('privacy.hide_public_links', true); + $linkDB = new BookmarkFileService($this->conf, $this->history, false); + + $this->assertEquals(0, $linkDB->count()); + } + + /** + * List the days for which bookmarks have been posted + */ + public function testDays() + { + $this->assertEquals( + ['20100309', '20100310', '20121206', '20121207', '20130614', '20150310'], + $this->publicLinkDB->days() + ); + + $this->assertEquals( + ['20100309', '20100310', '20121206', '20121207', '20130614', '20141125', '20150310'], + $this->privateLinkDB->days() + ); + } + + /** + * The URL corresponds to an existing entry in the DB + */ + public function testGetKnownLinkFromURL() + { + $link = $this->publicLinkDB->findByUrl('http://mediagoblin.org/'); + + $this->assertNotEquals(false, $link); + $this->assertContains( + 'A free software media publishing platform', + $link->getDescription() + ); + } + + /** + * The URL is not in the DB + */ + public function testGetUnknownLinkFromURL() + { + $this->assertEquals( + false, + $this->publicLinkDB->findByUrl('http://dev.null') + ); + } + + /** + * Lists all tags + */ + public function testAllTags() + { + $this->assertEquals( + [ + 'web' => 3, + 'cartoon' => 2, + 'gnu' => 2, + 'dev' => 1, + 'samba' => 1, + 'media' => 1, + 'software' => 1, + 'stallman' => 1, + 'free' => 1, + '-exclude' => 1, + 'hashtag' => 2, + // The DB contains a link with `sTuff` and another one with `stuff` tag. + // They need to be grouped with the first case found - order by date DESC: `sTuff`. + 'sTuff' => 2, + 'ut' => 1, + ], + $this->publicLinkDB->bookmarksCountPerTag() + ); + + $this->assertEquals( + [ + 'web' => 4, + 'cartoon' => 3, + 'gnu' => 2, + 'dev' => 2, + 'samba' => 1, + 'media' => 1, + 'software' => 1, + 'stallman' => 1, + 'free' => 1, + 'html' => 1, + 'w3c' => 1, + 'css' => 1, + 'Mercurial' => 1, + 'sTuff' => 2, + '-exclude' => 1, + '.hidden' => 1, + 'hashtag' => 2, + 'tag1' => 1, + 'tag2' => 1, + 'tag3' => 1, + 'tag4' => 1, + 'ut' => 1, + ], + $this->privateLinkDB->bookmarksCountPerTag() + ); + $this->assertEquals( + [ + 'web' => 4, + 'cartoon' => 2, + 'gnu' => 1, + 'dev' => 1, + 'samba' => 1, + 'media' => 1, + 'html' => 1, + 'w3c' => 1, + 'css' => 1, + 'Mercurial' => 1, + '.hidden' => 1, + 'hashtag' => 1, + ], + $this->privateLinkDB->bookmarksCountPerTag(['web']) + ); + $this->assertEquals( + [ + 'web' => 1, + 'html' => 1, + 'w3c' => 1, + 'css' => 1, + 'Mercurial' => 1, + ], + $this->privateLinkDB->bookmarksCountPerTag(['web'], 'private') + ); + } + + /** + * Test filter with string. + */ + public function testFilterString() + { + $tags = 'dev cartoon'; + $request = ['searchtags' => $tags]; + $this->assertEquals( + 2, + count($this->privateLinkDB->search($request, null, true)) + ); + } + + /** + * Test filter with array. + */ + public function testFilterArray() + { + $tags = ['dev', 'cartoon']; + $request = ['searchtags' => $tags]; + $this->assertEquals( + 2, + count($this->privateLinkDB->search($request, null, true)) + ); + } + + /** + * Test hidden tags feature: + * tags starting with a dot '.' are only visible when logged in. + */ + public function testHiddenTags() + { + $tags = '.hidden'; + $request = ['searchtags' => $tags]; + $this->assertEquals( + 1, + count($this->privateLinkDB->search($request, 'all', true)) + ); + + $this->assertEquals( + 0, + count($this->publicLinkDB->search($request, 'public', true)) + ); + } + + /** + * Test filterHash() with a valid smallhash. + */ + public function testFilterHashValid() + { + $request = smallHash('20150310_114651'); + $this->assertEquals( + 1, + count($this->publicLinkDB->findByHash($request)) + ); + $request = smallHash('20150310_114633' . 8); + $this->assertEquals( + 1, + count($this->publicLinkDB->findByHash($request)) + ); + } + + /** + * Test filterHash() with an invalid smallhash. + * + * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException + */ + public function testFilterHashInValid1() + { + $request = 'blabla'; + $this->publicLinkDB->findByHash($request); + } + + /** + * Test filterHash() with an empty smallhash. + * + * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException + */ + public function testFilterHashInValid() + { + $this->publicLinkDB->findByHash(''); + } + + /** + * Test linksCountPerTag all tags without filter. + * Equal occurrences should be sorted alphabetically. + */ + public function testCountLinkPerTagAllNoFilter() + { + $expected = [ + 'web' => 4, + 'cartoon' => 3, + 'dev' => 2, + 'gnu' => 2, + 'hashtag' => 2, + 'sTuff' => 2, + '-exclude' => 1, + '.hidden' => 1, + 'Mercurial' => 1, + 'css' => 1, + 'free' => 1, + 'html' => 1, + 'media' => 1, + 'samba' => 1, + 'software' => 1, + 'stallman' => 1, + 'tag1' => 1, + 'tag2' => 1, + 'tag3' => 1, + 'tag4' => 1, + 'ut' => 1, + 'w3c' => 1, + ]; + $tags = $this->privateLinkDB->bookmarksCountPerTag(); + + $this->assertEquals($expected, $tags, var_export($tags, true)); + } + + /** + * Test linksCountPerTag all tags with filter. + * Equal occurrences should be sorted alphabetically. + */ + public function testCountLinkPerTagAllWithFilter() + { + $expected = [ + 'gnu' => 2, + 'hashtag' => 2, + '-exclude' => 1, + '.hidden' => 1, + 'free' => 1, + 'media' => 1, + 'software' => 1, + 'stallman' => 1, + 'stuff' => 1, + 'web' => 1, + ]; + $tags = $this->privateLinkDB->bookmarksCountPerTag(['gnu']); + + $this->assertEquals($expected, $tags, var_export($tags, true)); + } + + /** + * Test linksCountPerTag public tags with filter. + * Equal occurrences should be sorted alphabetically. + */ + public function testCountLinkPerTagPublicWithFilter() + { + $expected = [ + 'gnu' => 2, + 'hashtag' => 2, + '-exclude' => 1, + '.hidden' => 1, + 'free' => 1, + 'media' => 1, + 'software' => 1, + 'stallman' => 1, + 'stuff' => 1, + 'web' => 1, + ]; + $tags = $this->privateLinkDB->bookmarksCountPerTag(['gnu'], 'public'); + + $this->assertEquals($expected, $tags, var_export($tags, true)); + } + + /** + * Test linksCountPerTag public tags with filter. + * Equal occurrences should be sorted alphabetically. + */ + public function testCountLinkPerTagPrivateWithFilter() + { + $expected = [ + 'cartoon' => 1, + 'dev' => 1, + 'tag1' => 1, + 'tag2' => 1, + 'tag3' => 1, + 'tag4' => 1, + ]; + $tags = $this->privateLinkDB->bookmarksCountPerTag(['dev'], 'private'); + + $this->assertEquals($expected, $tags, var_export($tags, true)); + } + + /** + * Allows to test LinkDB's private methods + * + * @see + * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html + * http://stackoverflow.com/a/2798203 + */ + protected static function getMethod($name) + { + $class = new ReflectionClass('Shaarli\Bookmark\BookmarkFileService'); + $method = $class->getMethod($name); + $method->setAccessible(true); + return $method; + } +} -- cgit v1.2.3 From a39acb2518f272df8a601af72c13eabe2719dcb8 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 18 Jan 2020 11:33:23 +0100 Subject: Fix an issue with private tags and fix nomarkdown tag The new bookmark service wasn't handling private tags properly. nomarkdown tag is now shown only for logged in user in bookmarks, and hidden for everyone in tag clouds/lists. Fixes #726 --- tests/bookmark/BookmarkFileServiceTest.php | 41 ++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'tests/bookmark/BookmarkFileServiceTest.php') diff --git a/tests/bookmark/BookmarkFileServiceTest.php b/tests/bookmark/BookmarkFileServiceTest.php index 1b438a7f..4900d41d 100644 --- a/tests/bookmark/BookmarkFileServiceTest.php +++ b/tests/bookmark/BookmarkFileServiceTest.php @@ -12,6 +12,7 @@ use ReflectionClass; use Shaarli; use Shaarli\Bookmark\Exception\BookmarkNotFoundException; use Shaarli\Config\ConfigManager; +use Shaarli\Formatter\BookmarkMarkdownFormatter; use Shaarli\History; /** @@ -1025,6 +1026,46 @@ class BookmarkFileServiceTest extends TestCase $this->assertEquals($expected, $tags, var_export($tags, true)); } + /** + * Test linksCountPerTag public tags with filter. + * Equal occurrences should be sorted alphabetically. + */ + public function testCountTagsNoMarkdown() + { + $expected = [ + 'cartoon' => 3, + 'dev' => 2, + 'tag1' => 1, + 'tag2' => 1, + 'tag3' => 1, + 'tag4' => 1, + 'web' => 4, + 'gnu' => 2, + 'hashtag' => 2, + 'sTuff' => 2, + '-exclude' => 1, + '.hidden' => 1, + 'Mercurial' => 1, + 'css' => 1, + 'free' => 1, + 'html' => 1, + 'media' => 1, + 'newTagToCount' => 1, + 'samba' => 1, + 'software' => 1, + 'stallman' => 1, + 'ut' => 1, + 'w3c' => 1, + ]; + $bookmark = new Bookmark(); + $bookmark->setTags(['newTagToCount', BookmarkMarkdownFormatter::NO_MD_TAG]); + $this->privateLinkDB->add($bookmark); + + $tags = $this->privateLinkDB->bookmarksCountPerTag(); + + $this->assertEquals($expected, $tags, var_export($tags, true)); + } + /** * Allows to test LinkDB's private methods * -- cgit v1.2.3 From c79473bd84ab5aba7836d2caaf61847cabaf1e53 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 16 May 2020 13:13:00 +0200 Subject: Handle tag filtering in the Bookmark service --- tests/bookmark/BookmarkFileServiceTest.php | 5 ----- 1 file changed, 5 deletions(-) (limited to 'tests/bookmark/BookmarkFileServiceTest.php') diff --git a/tests/bookmark/BookmarkFileServiceTest.php b/tests/bookmark/BookmarkFileServiceTest.php index 4900d41d..5adc26aa 100644 --- a/tests/bookmark/BookmarkFileServiceTest.php +++ b/tests/bookmark/BookmarkFileServiceTest.php @@ -816,7 +816,6 @@ class BookmarkFileServiceTest extends TestCase ); $this->assertEquals( [ - 'web' => 4, 'cartoon' => 2, 'gnu' => 1, 'dev' => 1, @@ -833,7 +832,6 @@ class BookmarkFileServiceTest extends TestCase ); $this->assertEquals( [ - 'web' => 1, 'html' => 1, 'w3c' => 1, 'css' => 1, @@ -968,7 +966,6 @@ class BookmarkFileServiceTest extends TestCase public function testCountLinkPerTagAllWithFilter() { $expected = [ - 'gnu' => 2, 'hashtag' => 2, '-exclude' => 1, '.hidden' => 1, @@ -991,7 +988,6 @@ class BookmarkFileServiceTest extends TestCase public function testCountLinkPerTagPublicWithFilter() { $expected = [ - 'gnu' => 2, 'hashtag' => 2, '-exclude' => 1, '.hidden' => 1, @@ -1015,7 +1011,6 @@ class BookmarkFileServiceTest extends TestCase { $expected = [ 'cartoon' => 1, - 'dev' => 1, 'tag1' => 1, 'tag2' => 1, 'tag3' => 1, -- cgit v1.2.3 From 1a8ac737e52cb25a5c346232ee398f5908cee7d7 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Mon, 6 Jul 2020 08:04:35 +0200 Subject: Process main page (linklist) through Slim controller Including a bunch of improvements on the container, and helper used across new controllers. --- tests/bookmark/BookmarkFileServiceTest.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'tests/bookmark/BookmarkFileServiceTest.php') diff --git a/tests/bookmark/BookmarkFileServiceTest.php b/tests/bookmark/BookmarkFileServiceTest.php index 5adc26aa..b19c8250 100644 --- a/tests/bookmark/BookmarkFileServiceTest.php +++ b/tests/bookmark/BookmarkFileServiceTest.php @@ -892,35 +892,35 @@ class BookmarkFileServiceTest extends TestCase public function testFilterHashValid() { $request = smallHash('20150310_114651'); - $this->assertEquals( - 1, - count($this->publicLinkDB->findByHash($request)) + $this->assertSame( + $request, + $this->publicLinkDB->findByHash($request)->getShortUrl() ); $request = smallHash('20150310_114633' . 8); - $this->assertEquals( - 1, - count($this->publicLinkDB->findByHash($request)) + $this->assertSame( + $request, + $this->publicLinkDB->findByHash($request)->getShortUrl() ); } /** * Test filterHash() with an invalid smallhash. - * - * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException */ public function testFilterHashInValid1() { + $this->expectException(BookmarkNotFoundException::class); + $request = 'blabla'; $this->publicLinkDB->findByHash($request); } /** * Test filterHash() with an empty smallhash. - * - * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException */ public function testFilterHashInValid() { + $this->expectException(BookmarkNotFoundException::class); + $this->publicLinkDB->findByHash(''); } -- cgit v1.2.3 From 301c7ab1a079d937ab41c6f52b8804e5731008e6 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 28 Jul 2020 20:46:11 +0200 Subject: Better support for notes permalink --- tests/bookmark/BookmarkFileServiceTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/bookmark/BookmarkFileServiceTest.php') diff --git a/tests/bookmark/BookmarkFileServiceTest.php b/tests/bookmark/BookmarkFileServiceTest.php index b19c8250..a8bf47cb 100644 --- a/tests/bookmark/BookmarkFileServiceTest.php +++ b/tests/bookmark/BookmarkFileServiceTest.php @@ -200,7 +200,7 @@ class BookmarkFileServiceTest extends TestCase $bookmark = $this->privateLinkDB->get(43); $this->assertEquals(43, $bookmark->getId()); - $this->assertRegExp('/\?[\w\-]{6}/', $bookmark->getUrl()); + $this->assertRegExp('#/shaare/[\w\-]{6}#', $bookmark->getUrl()); $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl()); $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle()); $this->assertEmpty($bookmark->getDescription()); @@ -216,7 +216,7 @@ class BookmarkFileServiceTest extends TestCase $bookmark = $this->privateLinkDB->get(43); $this->assertEquals(43, $bookmark->getId()); - $this->assertRegExp('/\?[\w\-]{6}/', $bookmark->getUrl()); + $this->assertRegExp('#/shaare/[\w\-]{6}#', $bookmark->getUrl()); $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl()); $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle()); $this->assertEmpty($bookmark->getDescription()); -- cgit v1.2.3 From f7f08ceec1b218e1525153e8bd3d0199f2fb1c9d Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 28 Jul 2020 22:24:41 +0200 Subject: Fix basePath in unit tests reference DB --- tests/bookmark/BookmarkFileServiceTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/bookmark/BookmarkFileServiceTest.php') diff --git a/tests/bookmark/BookmarkFileServiceTest.php b/tests/bookmark/BookmarkFileServiceTest.php index a8bf47cb..7b1906d3 100644 --- a/tests/bookmark/BookmarkFileServiceTest.php +++ b/tests/bookmark/BookmarkFileServiceTest.php @@ -340,7 +340,7 @@ class BookmarkFileServiceTest extends TestCase $bookmark = $this->privateLinkDB->get(42); $this->assertEquals(42, $bookmark->getId()); - $this->assertEquals('?WDWyig', $bookmark->getUrl()); + $this->assertEquals('/shaare/WDWyig', $bookmark->getUrl()); $this->assertEquals('1eYJ1Q', $bookmark->getShortUrl()); $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle()); $this->assertEquals('Used to test bookmarks reordering.', $bookmark->getDescription()); @@ -359,7 +359,7 @@ class BookmarkFileServiceTest extends TestCase $bookmark = $this->privateLinkDB->get(42); $this->assertEquals(42, $bookmark->getId()); - $this->assertEquals('?WDWyig', $bookmark->getUrl()); + $this->assertEquals('/shaare/WDWyig', $bookmark->getUrl()); $this->assertEquals('1eYJ1Q', $bookmark->getShortUrl()); $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle()); $this->assertEquals('Used to test bookmarks reordering.', $bookmark->getDescription()); -- cgit v1.2.3 From 27ddfec3c3847f10ab0de246f4a174b751c5f19e Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sun, 6 Sep 2020 14:11:02 +0200 Subject: Fix visibility issue on daily page This filter (links by day) didn't apply any visibility parameter. Fixes #1543 --- tests/bookmark/BookmarkFileServiceTest.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'tests/bookmark/BookmarkFileServiceTest.php') diff --git a/tests/bookmark/BookmarkFileServiceTest.php b/tests/bookmark/BookmarkFileServiceTest.php index 7b1906d3..c59fd443 100644 --- a/tests/bookmark/BookmarkFileServiceTest.php +++ b/tests/bookmark/BookmarkFileServiceTest.php @@ -1061,6 +1061,36 @@ class BookmarkFileServiceTest extends TestCase $this->assertEquals($expected, $tags, var_export($tags, true)); } + /** + * Test filterDay while logged in + */ + public function testFilterDayLoggedIn(): void + { + $bookmarks = $this->privateLinkDB->filterDay('20121206'); + $expectedIds = [4, 9, 1, 0]; + + static::assertCount(4, $bookmarks); + foreach ($bookmarks as $bookmark) { + $i = ($i ?? -1) + 1; + static::assertSame($expectedIds[$i], $bookmark->getId()); + } + } + + /** + * Test filterDay while logged out + */ + public function testFilterDayLoggedOut(): void + { + $bookmarks = $this->publicLinkDB->filterDay('20121206'); + $expectedIds = [4, 9, 1]; + + static::assertCount(3, $bookmarks); + foreach ($bookmarks as $bookmark) { + $i = ($i ?? -1) + 1; + static::assertSame($expectedIds[$i], $bookmark->getId()); + } + } + /** * Allows to test LinkDB's private methods * -- cgit v1.2.3 From da7acb98302b99ec729bcde3e3c9f4bb164a1b34 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sun, 6 Sep 2020 13:42:45 +0200 Subject: Improve default bookmarks after install Used @nodiscc suggestion in #1148 (slightly edited). It provides a description of what Shaarli does, Markdown rendering demo, and a thumbnail link. Fixes #1148 --- tests/bookmark/BookmarkFileServiceTest.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'tests/bookmark/BookmarkFileServiceTest.php') diff --git a/tests/bookmark/BookmarkFileServiceTest.php b/tests/bookmark/BookmarkFileServiceTest.php index 7b1906d3..a91f374f 100644 --- a/tests/bookmark/BookmarkFileServiceTest.php +++ b/tests/bookmark/BookmarkFileServiceTest.php @@ -615,14 +615,18 @@ class BookmarkFileServiceTest extends TestCase { $dbSize = $this->privateLinkDB->count(); $this->privateLinkDB->initialize(); - $this->assertEquals($dbSize + 2, $this->privateLinkDB->count()); - $this->assertEquals( - 'My secret stuff... - Pastebin.com', - $this->privateLinkDB->get(43)->getTitle() + $this->assertEquals($dbSize + 3, $this->privateLinkDB->count()); + $this->assertStringStartsWith( + 'Shaarli will automatically pick up the thumbnail for links to a variety of websites.', + $this->privateLinkDB->get(43)->getDescription() ); - $this->assertEquals( - 'The personal, minimalist, super-fast, database free, bookmarking service', - $this->privateLinkDB->get(44)->getTitle() + $this->assertStringStartsWith( + 'Adding a shaare without entering a URL creates a text-only "note" post such as this one.', + $this->privateLinkDB->get(44)->getDescription() + ); + $this->assertStringStartsWith( + 'Welcome to Shaarli!', + $this->privateLinkDB->get(45)->getDescription() ); } -- cgit v1.2.3 From 8f60e1206e45e67c96a7630d4ff94e72fe875f09 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sat, 26 Sep 2020 15:08:39 +0200 Subject: Comply with PHPUnit V8: setup/teardown functions must return void --- tests/bookmark/BookmarkFileServiceTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/bookmark/BookmarkFileServiceTest.php') diff --git a/tests/bookmark/BookmarkFileServiceTest.php b/tests/bookmark/BookmarkFileServiceTest.php index a4ec1013..aed4dc76 100644 --- a/tests/bookmark/BookmarkFileServiceTest.php +++ b/tests/bookmark/BookmarkFileServiceTest.php @@ -66,7 +66,7 @@ class BookmarkFileServiceTest extends TestCase * * Resets test data for each test */ - protected function setUp() + protected function setUp(): void { if (file_exists(self::$testDatastore)) { unlink(self::$testDatastore); -- cgit v1.2.3 From b1baca99f280570d0336b4d71ad1f9dca213a35b Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sun, 27 Sep 2020 14:07:08 +0200 Subject: Convert legacy PHPUnit @expected* to new ->expect* Converted automatically using https://github.com/ArthurHoaro/convert-legacy-phpunit-expect --- tests/bookmark/BookmarkFileServiceTest.php | 84 +++++++++++++++--------------- 1 file changed, 42 insertions(+), 42 deletions(-) (limited to 'tests/bookmark/BookmarkFileServiceTest.php') diff --git a/tests/bookmark/BookmarkFileServiceTest.php b/tests/bookmark/BookmarkFileServiceTest.php index aed4dc76..9cff0fb3 100644 --- a/tests/bookmark/BookmarkFileServiceTest.php +++ b/tests/bookmark/BookmarkFileServiceTest.php @@ -134,11 +134,11 @@ class BookmarkFileServiceTest extends TestCase /** * Test get() method for an undefined bookmark - * - * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException */ public function testGetUndefined() { + $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class); + $this->privateLinkDB->get(666); } @@ -230,13 +230,13 @@ class BookmarkFileServiceTest extends TestCase /** * Test add() method for a bookmark without any field set and without writing the data store - * - * @expectedExceptionMessage Shaarli\Bookmark\Exception\BookmarkNotFoundException */ public function testAddMinimalNoWrite() { + $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class); + $bookmark = new Bookmark(); - $this->privateLinkDB->add($bookmark); + $this->privateLinkDB->add($bookmark, false); $bookmark = $this->privateLinkDB->get(43); $this->assertEquals(43, $bookmark->getId()); @@ -249,34 +249,34 @@ class BookmarkFileServiceTest extends TestCase /** * Test add() method while logged out - * - * @expectedException \Exception - * @expectedExceptionMessage You're not authorized to alter the datastore */ public function testAddLoggedOut() { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('You\'re not authorized to alter the datastore'); + $this->publicLinkDB->add(new Bookmark()); } /** * Test add() method with an entry which is not a bookmark instance - * - * @expectedException \Exception - * @expectedExceptionMessage Provided data is invalid */ public function testAddNotABookmark() { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('Provided data is invalid'); + $this->privateLinkDB->add(['title' => 'hi!']); } /** * Test add() method with a Bookmark already containing an ID - * - * @expectedException \Exception - * @expectedExceptionMessage This bookmarks already exists */ public function testAddWithId() { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('This bookmarks already exists'); + $bookmark = new Bookmark(); $bookmark->setId(43); $this->privateLinkDB->add($bookmark); @@ -397,44 +397,44 @@ class BookmarkFileServiceTest extends TestCase /** * Test set() method while logged out - * - * @expectedException \Exception - * @expectedExceptionMessage You're not authorized to alter the datastore */ public function testSetLoggedOut() { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('You\'re not authorized to alter the datastore'); + $this->publicLinkDB->set(new Bookmark()); } /** * Test set() method with an entry which is not a bookmark instance - * - * @expectedException \Exception - * @expectedExceptionMessage Provided data is invalid */ public function testSetNotABookmark() { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('Provided data is invalid'); + $this->privateLinkDB->set(['title' => 'hi!']); } /** * Test set() method with a Bookmark without an ID defined. - * - * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException */ public function testSetWithoutId() { + $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class); + $bookmark = new Bookmark(); $this->privateLinkDB->set($bookmark); } /** * Test set() method with a Bookmark with an unknow ID - * - * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException */ public function testSetWithUnknownId() { + $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class); + $bookmark = new Bookmark(); $bookmark->setId(666); $this->privateLinkDB->set($bookmark); @@ -481,23 +481,23 @@ class BookmarkFileServiceTest extends TestCase /** * Test addOrSet() method while logged out - * - * @expectedException \Exception - * @expectedExceptionMessage You're not authorized to alter the datastore */ public function testAddOrSetLoggedOut() { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('You\'re not authorized to alter the datastore'); + $this->publicLinkDB->addOrSet(new Bookmark()); } /** * Test addOrSet() method with an entry which is not a bookmark instance - * - * @expectedException \Exception - * @expectedExceptionMessage Provided data is invalid */ public function testAddOrSetNotABookmark() { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('Provided data is invalid'); + $this->privateLinkDB->addOrSet(['title' => 'hi!']); } @@ -524,11 +524,11 @@ class BookmarkFileServiceTest extends TestCase /** * Test remove() method with an existing Bookmark - * - * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException */ public function testRemoveExisting() { + $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class); + $bookmark = $this->privateLinkDB->get(42); $this->privateLinkDB->remove($bookmark); @@ -548,34 +548,34 @@ class BookmarkFileServiceTest extends TestCase /** * Test remove() method while logged out - * - * @expectedException \Exception - * @expectedExceptionMessage You're not authorized to alter the datastore */ public function testRemoveLoggedOut() { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('You\'re not authorized to alter the datastore'); + $bookmark = $this->privateLinkDB->get(42); $this->publicLinkDB->remove($bookmark); } /** * Test remove() method with an entry which is not a bookmark instance - * - * @expectedException \Exception - * @expectedExceptionMessage Provided data is invalid */ public function testRemoveNotABookmark() { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('Provided data is invalid'); + $this->privateLinkDB->remove(['title' => 'hi!']); } /** * Test remove() method with a Bookmark with an unknown ID - * - * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException */ public function testRemoveWithUnknownId() { + $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class); + $bookmark = new Bookmark(); $bookmark->setId(666); $this->privateLinkDB->remove($bookmark); @@ -635,15 +635,15 @@ class BookmarkFileServiceTest extends TestCase * to make sure that nothing have been broken in the migration process. * They mostly cover search/filters. Some of them might be redundant with the previous ones. */ - /** * Attempt to instantiate a LinkDB whereas the datastore is not writable * * @expectedException Shaarli\Bookmark\Exception\NotWritableDataStoreException - * @expectedExceptionMessageRegExp #Couldn't load data from the data store file "null".*# */ public function testConstructDatastoreNotWriteable() { + $this->expectExceptionMessageRegExp('#Couldn\'t load data from the data store file "null".*#'); + $conf = new ConfigManager('tests/utils/config/configJson'); $conf->set('resource.datastore', 'null/store.db'); new BookmarkFileService($conf, $this->history, true); -- cgit v1.2.3 From a5a9cf23acd1248585173aa32757d9720b5f2d62 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 29 Sep 2020 14:41:40 +0200 Subject: Compatibility with PHPUnit 9 --- tests/bookmark/BookmarkFileServiceTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/bookmark/BookmarkFileServiceTest.php') diff --git a/tests/bookmark/BookmarkFileServiceTest.php b/tests/bookmark/BookmarkFileServiceTest.php index 9cff0fb3..51e71a85 100644 --- a/tests/bookmark/BookmarkFileServiceTest.php +++ b/tests/bookmark/BookmarkFileServiceTest.php @@ -6,7 +6,6 @@ namespace Shaarli\Bookmark; use DateTime; -use PHPUnit\Framework\TestCase; use ReferenceLinkDB; use ReflectionClass; use Shaarli; @@ -14,6 +13,7 @@ use Shaarli\Bookmark\Exception\BookmarkNotFoundException; use Shaarli\Config\ConfigManager; use Shaarli\Formatter\BookmarkMarkdownFormatter; use Shaarli\History; +use Shaarli\TestCase; /** * Unitary tests for LegacyLinkDBTest @@ -748,7 +748,7 @@ class BookmarkFileServiceTest extends TestCase $link = $this->publicLinkDB->findByUrl('http://mediagoblin.org/'); $this->assertNotEquals(false, $link); - $this->assertContains( + $this->assertContainsPolyfill( 'A free software media publishing platform', $link->getDescription() ); -- cgit v1.2.3 From f447edb73b1bcb52e86286467d3ec7b7bdc29948 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 29 Sep 2020 18:41:21 +0200 Subject: Fix missing @expectedException convertion --- tests/bookmark/BookmarkFileServiceTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'tests/bookmark/BookmarkFileServiceTest.php') diff --git a/tests/bookmark/BookmarkFileServiceTest.php b/tests/bookmark/BookmarkFileServiceTest.php index 51e71a85..c399822b 100644 --- a/tests/bookmark/BookmarkFileServiceTest.php +++ b/tests/bookmark/BookmarkFileServiceTest.php @@ -637,11 +637,10 @@ class BookmarkFileServiceTest extends TestCase */ /** * Attempt to instantiate a LinkDB whereas the datastore is not writable - * - * @expectedException Shaarli\Bookmark\Exception\NotWritableDataStoreException */ public function testConstructDatastoreNotWriteable() { + $this->expectException(\Shaarli\Bookmark\Exception\NotWritableDataStoreException::class); $this->expectExceptionMessageRegExp('#Couldn\'t load data from the data store file "null".*#'); $conf = new ConfigManager('tests/utils/config/configJson'); -- cgit v1.2.3