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/netscape/BookmarkExportTest.php | 70 ++++- tests/netscape/BookmarkImportTest.php | 570 ++++++++++++++++------------------ 2 files changed, 318 insertions(+), 322 deletions(-) (limited to 'tests/netscape') diff --git a/tests/netscape/BookmarkExportTest.php b/tests/netscape/BookmarkExportTest.php index 6de9876d..011d19ac 100644 --- a/tests/netscape/BookmarkExportTest.php +++ b/tests/netscape/BookmarkExportTest.php @@ -1,7 +1,12 @@ set('resource.datastore', self::$testDatastore); self::$refDb = new \ReferenceLinkDB(); self::$refDb->write(self::$testDatastore); - self::$linkDb = new LinkDB(self::$testDatastore, true, false); + $history = new History('sandbox/history.php'); + self::$bookmarkService = new BookmarkFileService($conf, $history, true); + $factory = new FormatterFactory($conf); + self::$formatter = $factory->getFormatter('raw'); } /** @@ -42,15 +57,27 @@ class BookmarkExportTest extends \PHPUnit\Framework\TestCase */ public function testFilterAndFormatInvalid() { - NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'derp', false, ''); + NetscapeBookmarkUtils::filterAndFormat( + self::$bookmarkService, + self::$formatter, + 'derp', + false, + '' + ); } /** - * Prepare all links for export + * Prepare all bookmarks for export */ public function testFilterAndFormatAll() { - $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'all', false, ''); + $links = NetscapeBookmarkUtils::filterAndFormat( + self::$bookmarkService, + self::$formatter, + 'all', + false, + '' + ); $this->assertEquals(self::$refDb->countLinks(), sizeof($links)); foreach ($links as $link) { $date = $link['created']; @@ -66,11 +93,17 @@ class BookmarkExportTest extends \PHPUnit\Framework\TestCase } /** - * Prepare private links for export + * Prepare private bookmarks for export */ public function testFilterAndFormatPrivate() { - $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'private', false, ''); + $links = NetscapeBookmarkUtils::filterAndFormat( + self::$bookmarkService, + self::$formatter, + 'private', + false, + '' + ); $this->assertEquals(self::$refDb->countPrivateLinks(), sizeof($links)); foreach ($links as $link) { $date = $link['created']; @@ -86,11 +119,17 @@ class BookmarkExportTest extends \PHPUnit\Framework\TestCase } /** - * Prepare public links for export + * Prepare public bookmarks for export */ public function testFilterAndFormatPublic() { - $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'public', false, ''); + $links = NetscapeBookmarkUtils::filterAndFormat( + self::$bookmarkService, + self::$formatter, + 'public', + false, + '' + ); $this->assertEquals(self::$refDb->countPublicLinks(), sizeof($links)); foreach ($links as $link) { $date = $link['created']; @@ -110,7 +149,13 @@ class BookmarkExportTest extends \PHPUnit\Framework\TestCase */ public function testFilterAndFormatDoNotPrependNoteUrl() { - $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'public', false, ''); + $links = NetscapeBookmarkUtils::filterAndFormat( + self::$bookmarkService, + self::$formatter, + 'public', + false, + '' + ); $this->assertEquals( '?WDWyig', $links[2]['url'] @@ -124,7 +169,8 @@ class BookmarkExportTest extends \PHPUnit\Framework\TestCase { $indexUrl = 'http://localhost:7469/shaarli/'; $links = NetscapeBookmarkUtils::filterAndFormat( - self::$linkDb, + self::$bookmarkService, + self::$formatter, 'public', true, $indexUrl diff --git a/tests/netscape/BookmarkImportTest.php b/tests/netscape/BookmarkImportTest.php index ccafc161..fef7f6d1 100644 --- a/tests/netscape/BookmarkImportTest.php +++ b/tests/netscape/BookmarkImportTest.php @@ -2,6 +2,9 @@ namespace Shaarli\Netscape; use DateTime; +use Shaarli\Bookmark\Bookmark; +use Shaarli\Bookmark\BookmarkFilter; +use Shaarli\Bookmark\BookmarkFileService; use Shaarli\Bookmark\LinkDB; use Shaarli\Config\ConfigManager; use Shaarli\History; @@ -41,9 +44,9 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase protected static $historyFilePath = 'sandbox/history.php'; /** - * @var LinkDB private LinkDB instance + * @var BookmarkFileService private LinkDB instance */ - protected $linkDb = null; + protected $bookmarkService = null; /** * @var string Dummy page cache @@ -82,10 +85,12 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase } // start with an empty datastore file_put_contents(self::$testDatastore, ''); - $this->linkDb = new LinkDB(self::$testDatastore, true, false); + $this->conf = new ConfigManager('tests/utils/config/configJson'); $this->conf->set('resource.page_cache', $this->pagecache); + $this->conf->set('resource.datastore', self::$testDatastore); $this->history = new History(self::$historyFilePath); + $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true); } /** @@ -112,7 +117,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase .' Nothing was imported.', NetscapeBookmarkUtils::import(null, $files, null, $this->conf, $this->history) ); - $this->assertEquals(0, count($this->linkDb)); + $this->assertEquals(0, $this->bookmarkService->count()); } /** @@ -125,7 +130,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase 'File no_doctype.htm (350 bytes) has an unknown file format. Nothing was imported.', NetscapeBookmarkUtils::import(null, $files, null, $this->conf, $this->history) ); - $this->assertEquals(0, count($this->linkDb)); + $this->assertEquals(0, $this->bookmarkService->count()); } /** @@ -136,10 +141,10 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $files = file2array('lowercase_doctype.htm'); $this->assertStringMatchesFormat( 'File lowercase_doctype.htm (386 bytes) was successfully processed in %d seconds:' - .' 2 links imported, 0 links overwritten, 0 links skipped.', - NetscapeBookmarkUtils::import(null, $files, $this->linkDb, $this->conf, $this->history) + .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', + NetscapeBookmarkUtils::import(null, $files, $this->bookmarkService, $this->conf, $this->history) ); - $this->assertEquals(2, count($this->linkDb)); + $this->assertEquals(2, $this->bookmarkService->count()); } @@ -151,25 +156,24 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $files = file2array('internet_explorer_encoding.htm'); $this->assertStringMatchesFormat( 'File internet_explorer_encoding.htm (356 bytes) was successfully processed in %d seconds:' - .' 1 links imported, 0 links overwritten, 0 links skipped.', - NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history) + .' 1 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', + NetscapeBookmarkUtils::import([], $files, $this->bookmarkService, $this->conf, $this->history) ); - $this->assertEquals(1, count($this->linkDb)); - $this->assertEquals(0, count_private($this->linkDb)); + $this->assertEquals(1, $this->bookmarkService->count()); + $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); + $bookmark = $this->bookmarkService->findByUrl('http://hginit.com/'); + $this->assertEquals(0, $bookmark->getId()); $this->assertEquals( - array( - 'id' => 0, - 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160618_203944'), - 'title' => 'Hg Init a Mercurial tutorial by Joel Spolsky', - 'url' => 'http://hginit.com/', - 'description' => '', - 'private' => 0, - 'tags' => '', - 'shorturl' => 'La37cg', - ), - $this->linkDb->getLinkFromUrl('http://hginit.com/') + DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160618_203944'), + $bookmark->getCreated() ); + $this->assertEquals('Hg Init a Mercurial tutorial by Joel Spolsky', $bookmark->getTitle()); + $this->assertEquals('http://hginit.com/', $bookmark->getUrl()); + $this->assertEquals('', $bookmark->getDescription()); + $this->assertFalse($bookmark->isPrivate()); + $this->assertEquals('', $bookmark->getTagsString()); + $this->assertEquals('La37cg', $bookmark->getShortUrl()); } /** @@ -180,116 +184,115 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $files = file2array('netscape_nested.htm'); $this->assertStringMatchesFormat( 'File netscape_nested.htm (1337 bytes) was successfully processed in %d seconds:' - .' 8 links imported, 0 links overwritten, 0 links skipped.', - NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history) - ); - $this->assertEquals(8, count($this->linkDb)); - $this->assertEquals(2, count_private($this->linkDb)); - - $this->assertEquals( - array( - 'id' => 0, - 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235541'), - 'title' => 'Nested 1', - 'url' => 'http://nest.ed/1', - 'description' => '', - 'private' => 0, - 'tags' => 'tag1 tag2', - 'shorturl' => 'KyDNKA', - ), - $this->linkDb->getLinkFromUrl('http://nest.ed/1') - ); - $this->assertEquals( - array( - 'id' => 1, - 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235542'), - 'title' => 'Nested 1-1', - 'url' => 'http://nest.ed/1-1', - 'description' => '', - 'private' => 0, - 'tags' => 'folder1 tag1 tag2', - 'shorturl' => 'T2LnXg', - ), - $this->linkDb->getLinkFromUrl('http://nest.ed/1-1') - ); - $this->assertEquals( - array( - 'id' => 2, - 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235547'), - 'title' => 'Nested 1-2', - 'url' => 'http://nest.ed/1-2', - 'description' => '', - 'private' => 0, - 'tags' => 'folder1 tag3 tag4', - 'shorturl' => '46SZxA', - ), - $this->linkDb->getLinkFromUrl('http://nest.ed/1-2') - ); - $this->assertEquals( - array( - 'id' => 3, - 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160202_202222'), - 'title' => 'Nested 2-1', - 'url' => 'http://nest.ed/2-1', - 'description' => 'First link of the second section', - 'private' => 1, - 'tags' => 'folder2', - 'shorturl' => '4UHOSw', - ), - $this->linkDb->getLinkFromUrl('http://nest.ed/2-1') - ); - $this->assertEquals( - array( - 'id' => 4, - 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160119_230227'), - 'title' => 'Nested 2-2', - 'url' => 'http://nest.ed/2-2', - 'description' => 'Second link of the second section', - 'private' => 1, - 'tags' => 'folder2', - 'shorturl' => 'yfzwbw', - ), - $this->linkDb->getLinkFromUrl('http://nest.ed/2-2') - ); - $this->assertEquals( - array( - 'id' => 5, - 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160202_202222'), - 'title' => 'Nested 3-1', - 'url' => 'http://nest.ed/3-1', - 'description' => '', - 'private' => 0, - 'tags' => 'folder3 folder3-1 tag3', - 'shorturl' => 'UwxIUQ', - ), - $this->linkDb->getLinkFromUrl('http://nest.ed/3-1') - ); - $this->assertEquals( - array( - 'id' => 6, - 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160119_230227'), - 'title' => 'Nested 3-2', - 'url' => 'http://nest.ed/3-2', - 'description' => '', - 'private' => 0, - 'tags' => 'folder3 folder3-1', - 'shorturl' => 'p8dyZg', - ), - $this->linkDb->getLinkFromUrl('http://nest.ed/3-2') - ); - $this->assertEquals( - array( - 'id' => 7, - 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160229_111541'), - 'title' => 'Nested 2', - 'url' => 'http://nest.ed/2', - 'description' => '', - 'private' => 0, - 'tags' => 'tag4', - 'shorturl' => 'Gt3Uug', - ), - $this->linkDb->getLinkFromUrl('http://nest.ed/2') + .' 8 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', + NetscapeBookmarkUtils::import([], $files, $this->bookmarkService, $this->conf, $this->history) ); + $this->assertEquals(8, $this->bookmarkService->count()); + $this->assertEquals(2, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); + + $bookmark = $this->bookmarkService->findByUrl('http://nest.ed/1'); + $this->assertEquals(0, $bookmark->getId()); + $this->assertEquals( + DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160225_235541'), + $bookmark->getCreated() + ); + $this->assertEquals('Nested 1', $bookmark->getTitle()); + $this->assertEquals('http://nest.ed/1', $bookmark->getUrl()); + $this->assertEquals('', $bookmark->getDescription()); + $this->assertFalse($bookmark->isPrivate()); + $this->assertEquals('tag1 tag2', $bookmark->getTagsString()); + $this->assertEquals('KyDNKA', $bookmark->getShortUrl()); + + $bookmark = $this->bookmarkService->findByUrl('http://nest.ed/1-1'); + $this->assertEquals(1, $bookmark->getId()); + $this->assertEquals( + DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160225_235542'), + $bookmark->getCreated() + ); + $this->assertEquals('Nested 1-1', $bookmark->getTitle()); + $this->assertEquals('http://nest.ed/1-1', $bookmark->getUrl()); + $this->assertEquals('', $bookmark->getDescription()); + $this->assertFalse($bookmark->isPrivate()); + $this->assertEquals('folder1 tag1 tag2', $bookmark->getTagsString()); + $this->assertEquals('T2LnXg', $bookmark->getShortUrl()); + + $bookmark = $this->bookmarkService->findByUrl('http://nest.ed/1-2'); + $this->assertEquals(2, $bookmark->getId()); + $this->assertEquals( + DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160225_235547'), + $bookmark->getCreated() + ); + $this->assertEquals('Nested 1-2', $bookmark->getTitle()); + $this->assertEquals('http://nest.ed/1-2', $bookmark->getUrl()); + $this->assertEquals('', $bookmark->getDescription()); + $this->assertFalse($bookmark->isPrivate()); + $this->assertEquals('folder1 tag3 tag4', $bookmark->getTagsString()); + $this->assertEquals('46SZxA', $bookmark->getShortUrl()); + + $bookmark = $this->bookmarkService->findByUrl('http://nest.ed/2-1'); + $this->assertEquals(3, $bookmark->getId()); + $this->assertEquals( + DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160202_202222'), + $bookmark->getCreated() + ); + $this->assertEquals('Nested 2-1', $bookmark->getTitle()); + $this->assertEquals('http://nest.ed/2-1', $bookmark->getUrl()); + $this->assertEquals('First link of the second section', $bookmark->getDescription()); + $this->assertTrue($bookmark->isPrivate()); + $this->assertEquals('folder2', $bookmark->getTagsString()); + $this->assertEquals('4UHOSw', $bookmark->getShortUrl()); + + $bookmark = $this->bookmarkService->findByUrl('http://nest.ed/2-2'); + $this->assertEquals(4, $bookmark->getId()); + $this->assertEquals( + DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160119_230227'), + $bookmark->getCreated() + ); + $this->assertEquals('Nested 2-2', $bookmark->getTitle()); + $this->assertEquals('http://nest.ed/2-2', $bookmark->getUrl()); + $this->assertEquals('Second link of the second section', $bookmark->getDescription()); + $this->assertTrue($bookmark->isPrivate()); + $this->assertEquals('folder2', $bookmark->getTagsString()); + $this->assertEquals('yfzwbw', $bookmark->getShortUrl()); + + $bookmark = $this->bookmarkService->findByUrl('http://nest.ed/3-1'); + $this->assertEquals(5, $bookmark->getId()); + $this->assertEquals( + DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160202_202222'), + $bookmark->getCreated() + ); + $this->assertEquals('Nested 3-1', $bookmark->getTitle()); + $this->assertEquals('http://nest.ed/3-1', $bookmark->getUrl()); + $this->assertEquals('', $bookmark->getDescription()); + $this->assertFalse($bookmark->isPrivate()); + $this->assertEquals('folder3 folder3-1 tag3', $bookmark->getTagsString()); + $this->assertEquals('UwxIUQ', $bookmark->getShortUrl()); + + $bookmark = $this->bookmarkService->findByUrl('http://nest.ed/3-2'); + $this->assertEquals(6, $bookmark->getId()); + $this->assertEquals( + DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160119_230227'), + $bookmark->getCreated() + ); + $this->assertEquals('Nested 3-2', $bookmark->getTitle()); + $this->assertEquals('http://nest.ed/3-2', $bookmark->getUrl()); + $this->assertEquals('', $bookmark->getDescription()); + $this->assertFalse($bookmark->isPrivate()); + $this->assertEquals('folder3 folder3-1', $bookmark->getTagsString()); + $this->assertEquals('p8dyZg', $bookmark->getShortUrl()); + + $bookmark = $this->bookmarkService->findByUrl('http://nest.ed/2'); + $this->assertEquals(7, $bookmark->getId()); + $this->assertEquals( + DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160229_111541'), + $bookmark->getCreated() + ); + $this->assertEquals('Nested 2', $bookmark->getTitle()); + $this->assertEquals('http://nest.ed/2', $bookmark->getUrl()); + $this->assertEquals('', $bookmark->getDescription()); + $this->assertFalse($bookmark->isPrivate()); + $this->assertEquals('tag4', $bookmark->getTagsString()); + $this->assertEquals('Gt3Uug', $bookmark->getShortUrl()); } /** @@ -302,40 +305,38 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $files = file2array('netscape_basic.htm'); $this->assertStringMatchesFormat( 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' - .' 2 links imported, 0 links overwritten, 0 links skipped.', - NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history) + .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', + NetscapeBookmarkUtils::import([], $files, $this->bookmarkService, $this->conf, $this->history) ); - $this->assertEquals(2, count($this->linkDb)); - $this->assertEquals(1, count_private($this->linkDb)); + $this->assertEquals(2, $this->bookmarkService->count()); + $this->assertEquals(1, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); + $bookmark = $this->bookmarkService->findByUrl('https://private.tld'); + $this->assertEquals(0, $bookmark->getId()); $this->assertEquals( - array( - 'id' => 0, - // Old link - UTC+4 (note that TZ in the import file is ignored). - 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20001010_135536'), - 'title' => 'Secret stuff', - 'url' => 'https://private.tld', - 'description' => "Super-secret stuff you're not supposed to know about", - 'private' => 1, - 'tags' => 'private secret', - 'shorturl' => 'EokDtA', - ), - $this->linkDb->getLinkFromUrl('https://private.tld') + DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20001010_135536'), + $bookmark->getCreated() ); + $this->assertEquals('Secret stuff', $bookmark->getTitle()); + $this->assertEquals('https://private.tld', $bookmark->getUrl()); + $this->assertEquals('Super-secret stuff you\'re not supposed to know about', $bookmark->getDescription()); + $this->assertTrue($bookmark->isPrivate()); + $this->assertEquals('private secret', $bookmark->getTagsString()); + $this->assertEquals('EokDtA', $bookmark->getShortUrl()); + + $bookmark = $this->bookmarkService->findByUrl('http://public.tld'); + $this->assertEquals(1, $bookmark->getId()); $this->assertEquals( - array( - 'id' => 1, - 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235548'), - 'title' => 'Public stuff', - 'url' => 'http://public.tld', - 'description' => '', - 'private' => 0, - 'tags' => 'public hello world', - 'shorturl' => 'Er9ddA', - ), - $this->linkDb->getLinkFromUrl('http://public.tld') + DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160225_235548'), + $bookmark->getCreated() ); + $this->assertEquals('Public stuff', $bookmark->getTitle()); + $this->assertEquals('http://public.tld', $bookmark->getUrl()); + $this->assertEquals('', $bookmark->getDescription()); + $this->assertFalse($bookmark->isPrivate()); + $this->assertEquals('public hello world', $bookmark->getTagsString()); + $this->assertEquals('Er9ddA', $bookmark->getShortUrl()); } /** @@ -347,43 +348,42 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $files = file2array('netscape_basic.htm'); $this->assertStringMatchesFormat( 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' - .' 2 links imported, 0 links overwritten, 0 links skipped.', - NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) + .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', + NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history) ); - $this->assertEquals(2, count($this->linkDb)); - $this->assertEquals(1, count_private($this->linkDb)); + $this->assertEquals(2, $this->bookmarkService->count()); + $this->assertEquals(1, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); + + $bookmark = $this->bookmarkService->findByUrl('https://private.tld'); + $this->assertEquals(0, $bookmark->getId()); $this->assertEquals( - array( - 'id' => 0, - // Note that TZ in the import file is ignored. - 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20001010_135536'), - 'title' => 'Secret stuff', - 'url' => 'https://private.tld', - 'description' => "Super-secret stuff you're not supposed to know about", - 'private' => 1, - 'tags' => 'private secret', - 'shorturl' => 'EokDtA', - ), - $this->linkDb->getLinkFromUrl('https://private.tld') + DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20001010_135536'), + $bookmark->getCreated() ); + $this->assertEquals('Secret stuff', $bookmark->getTitle()); + $this->assertEquals('https://private.tld', $bookmark->getUrl()); + $this->assertEquals('Super-secret stuff you\'re not supposed to know about', $bookmark->getDescription()); + $this->assertTrue($bookmark->isPrivate()); + $this->assertEquals('private secret', $bookmark->getTagsString()); + $this->assertEquals('EokDtA', $bookmark->getShortUrl()); + + $bookmark = $this->bookmarkService->findByUrl('http://public.tld'); + $this->assertEquals(1, $bookmark->getId()); $this->assertEquals( - array( - 'id' => 1, - 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235548'), - 'title' => 'Public stuff', - 'url' => 'http://public.tld', - 'description' => '', - 'private' => 0, - 'tags' => 'public hello world', - 'shorturl' => 'Er9ddA', - ), - $this->linkDb->getLinkFromUrl('http://public.tld') + DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160225_235548'), + $bookmark->getCreated() ); + $this->assertEquals('Public stuff', $bookmark->getTitle()); + $this->assertEquals('http://public.tld', $bookmark->getUrl()); + $this->assertEquals('', $bookmark->getDescription()); + $this->assertFalse($bookmark->isPrivate()); + $this->assertEquals('public hello world', $bookmark->getTagsString()); + $this->assertEquals('Er9ddA', $bookmark->getShortUrl()); } /** - * Import links as public + * Import bookmarks as public */ public function testImportAsPublic() { @@ -391,23 +391,17 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $files = file2array('netscape_basic.htm'); $this->assertStringMatchesFormat( 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' - .' 2 links imported, 0 links overwritten, 0 links skipped.', - NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) - ); - $this->assertEquals(2, count($this->linkDb)); - $this->assertEquals(0, count_private($this->linkDb)); - $this->assertEquals( - 0, - $this->linkDb[0]['private'] - ); - $this->assertEquals( - 0, - $this->linkDb[1]['private'] + .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', + NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history) ); + $this->assertEquals(2, $this->bookmarkService->count()); + $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); + $this->assertFalse($this->bookmarkService->get(0)->isPrivate()); + $this->assertFalse($this->bookmarkService->get(1)->isPrivate()); } /** - * Import links as private + * Import bookmarks as private */ public function testImportAsPrivate() { @@ -415,45 +409,34 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $files = file2array('netscape_basic.htm'); $this->assertStringMatchesFormat( 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' - .' 2 links imported, 0 links overwritten, 0 links skipped.', - NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) - ); - $this->assertEquals(2, count($this->linkDb)); - $this->assertEquals(2, count_private($this->linkDb)); - $this->assertEquals( - 1, - $this->linkDb['0']['private'] - ); - $this->assertEquals( - 1, - $this->linkDb['1']['private'] + .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', + NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history) ); + $this->assertEquals(2, $this->bookmarkService->count()); + $this->assertEquals(2, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); + $this->assertTrue($this->bookmarkService->get(0)->isPrivate()); + $this->assertTrue($this->bookmarkService->get(1)->isPrivate()); } /** - * Overwrite private links so they become public + * Overwrite private bookmarks so they become public */ public function testOverwriteAsPublic() { $files = file2array('netscape_basic.htm'); - // import links as private + // import bookmarks as private $post = array('privacy' => 'private'); $this->assertStringMatchesFormat( 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' - .' 2 links imported, 0 links overwritten, 0 links skipped.', - NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) - ); - $this->assertEquals(2, count($this->linkDb)); - $this->assertEquals(2, count_private($this->linkDb)); - $this->assertEquals( - 1, - $this->linkDb[0]['private'] - ); - $this->assertEquals( - 1, - $this->linkDb[1]['private'] + .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', + NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history) ); + $this->assertEquals(2, $this->bookmarkService->count()); + $this->assertEquals(2, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); + $this->assertTrue($this->bookmarkService->get(0)->isPrivate()); + $this->assertTrue($this->bookmarkService->get(1)->isPrivate()); + // re-import as public, enable overwriting $post = array( 'privacy' => 'public', @@ -461,45 +444,33 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase ); $this->assertStringMatchesFormat( 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' - .' 2 links imported, 2 links overwritten, 0 links skipped.', - NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) - ); - $this->assertEquals(2, count($this->linkDb)); - $this->assertEquals(0, count_private($this->linkDb)); - $this->assertEquals( - 0, - $this->linkDb[0]['private'] - ); - $this->assertEquals( - 0, - $this->linkDb[1]['private'] + .' 2 bookmarks imported, 2 bookmarks overwritten, 0 bookmarks skipped.', + NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history) ); + $this->assertEquals(2, $this->bookmarkService->count()); + $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); + $this->assertFalse($this->bookmarkService->get(0)->isPrivate()); + $this->assertFalse($this->bookmarkService->get(1)->isPrivate()); } /** - * Overwrite public links so they become private + * Overwrite public bookmarks so they become private */ public function testOverwriteAsPrivate() { $files = file2array('netscape_basic.htm'); - // import links as public + // import bookmarks as public $post = array('privacy' => 'public'); $this->assertStringMatchesFormat( 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' - .' 2 links imported, 0 links overwritten, 0 links skipped.', - NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) - ); - $this->assertEquals(2, count($this->linkDb)); - $this->assertEquals(0, count_private($this->linkDb)); - $this->assertEquals( - 0, - $this->linkDb['0']['private'] - ); - $this->assertEquals( - 0, - $this->linkDb['1']['private'] + .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', + NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history) ); + $this->assertEquals(2, $this->bookmarkService->count()); + $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); + $this->assertFalse($this->bookmarkService->get(0)->isPrivate()); + $this->assertFalse($this->bookmarkService->get(1)->isPrivate()); // re-import as private, enable overwriting $post = array( @@ -508,23 +479,17 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase ); $this->assertStringMatchesFormat( 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' - .' 2 links imported, 2 links overwritten, 0 links skipped.', - NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) - ); - $this->assertEquals(2, count($this->linkDb)); - $this->assertEquals(2, count_private($this->linkDb)); - $this->assertEquals( - 1, - $this->linkDb['0']['private'] - ); - $this->assertEquals( - 1, - $this->linkDb['1']['private'] + .' 2 bookmarks imported, 2 bookmarks overwritten, 0 bookmarks skipped.', + NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history) ); + $this->assertEquals(2, $this->bookmarkService->count()); + $this->assertEquals(2, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); + $this->assertTrue($this->bookmarkService->get(0)->isPrivate()); + $this->assertTrue($this->bookmarkService->get(1)->isPrivate()); } /** - * Attept to import the same links twice without enabling overwriting + * Attept to import the same bookmarks twice without enabling overwriting */ public function testSkipOverwrite() { @@ -532,21 +497,21 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $files = file2array('netscape_basic.htm'); $this->assertStringMatchesFormat( 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' - .' 2 links imported, 0 links overwritten, 0 links skipped.', - NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) + .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', + NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history) ); - $this->assertEquals(2, count($this->linkDb)); - $this->assertEquals(0, count_private($this->linkDb)); + $this->assertEquals(2, $this->bookmarkService->count()); + $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); // re-import as private, DO NOT enable overwriting $post = array('privacy' => 'private'); $this->assertStringMatchesFormat( 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' - .' 0 links imported, 0 links overwritten, 2 links skipped.', - NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) + .' 0 bookmarks imported, 0 bookmarks overwritten, 2 bookmarks skipped.', + NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history) ); - $this->assertEquals(2, count($this->linkDb)); - $this->assertEquals(0, count_private($this->linkDb)); + $this->assertEquals(2, $this->bookmarkService->count()); + $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); } /** @@ -561,19 +526,13 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $files = file2array('netscape_basic.htm'); $this->assertStringMatchesFormat( 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' - .' 2 links imported, 0 links overwritten, 0 links skipped.', - NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) - ); - $this->assertEquals(2, count($this->linkDb)); - $this->assertEquals(0, count_private($this->linkDb)); - $this->assertEquals( - 'tag1 tag2 tag3 private secret', - $this->linkDb['0']['tags'] - ); - $this->assertEquals( - 'tag1 tag2 tag3 public hello world', - $this->linkDb['1']['tags'] + .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', + NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history) ); + $this->assertEquals(2, $this->bookmarkService->count()); + $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); + $this->assertEquals('tag1 tag2 tag3 private secret', $this->bookmarkService->get(0)->getTagsString()); + $this->assertEquals('tag1 tag2 tag3 public hello world', $this->bookmarkService->get(1)->getTagsString()); } /** @@ -588,18 +547,18 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $files = file2array('netscape_basic.htm'); $this->assertStringMatchesFormat( 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' - .' 2 links imported, 0 links overwritten, 0 links skipped.', - NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) + .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', + NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history) ); - $this->assertEquals(2, count($this->linkDb)); - $this->assertEquals(0, count_private($this->linkDb)); + $this->assertEquals(2, $this->bookmarkService->count()); + $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); $this->assertEquals( 'tag1& tag2 "tag3" private secret', - $this->linkDb['0']['tags'] + $this->bookmarkService->get(0)->getTagsString() ); $this->assertEquals( 'tag1& tag2 "tag3" public hello world', - $this->linkDb['1']['tags'] + $this->bookmarkService->get(1)->getTagsString() ); } @@ -613,23 +572,14 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $files = file2array('same_date.htm'); $this->assertStringMatchesFormat( 'File same_date.htm (453 bytes) was successfully processed in %d seconds:' - .' 3 links imported, 0 links overwritten, 0 links skipped.', - NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->conf, $this->history) - ); - $this->assertEquals(3, count($this->linkDb)); - $this->assertEquals(0, count_private($this->linkDb)); - $this->assertEquals( - 0, - $this->linkDb[0]['id'] - ); - $this->assertEquals( - 1, - $this->linkDb[1]['id'] - ); - $this->assertEquals( - 2, - $this->linkDb[2]['id'] - ); + .' 3 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', + NetscapeBookmarkUtils::import(array(), $files, $this->bookmarkService, $this->conf, $this->history) + ); + $this->assertEquals(3, $this->bookmarkService->count()); + $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); + $this->assertEquals(0, $this->bookmarkService->get(0)->getId()); + $this->assertEquals(1, $this->bookmarkService->get(1)->getId()); + $this->assertEquals(2, $this->bookmarkService->get(2)->getId()); } public function testImportCreateUpdateHistory() @@ -639,14 +589,14 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase 'overwrite' => 'true', ]; $files = file2array('netscape_basic.htm'); - NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history); + NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history); $history = $this->history->getHistory(); $this->assertEquals(1, count($history)); $this->assertEquals(History::IMPORT, $history[0]['event']); $this->assertTrue(new DateTime('-5 seconds') < $history[0]['datetime']); // re-import as private, enable overwriting - NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history); + NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history); $history = $this->history->getHistory(); $this->assertEquals(2, count($history)); $this->assertEquals(History::IMPORT, $history[0]['event']); -- 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/netscape/BookmarkExportTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/netscape') diff --git a/tests/netscape/BookmarkExportTest.php b/tests/netscape/BookmarkExportTest.php index 011d19ac..6c948bba 100644 --- a/tests/netscape/BookmarkExportTest.php +++ b/tests/netscape/BookmarkExportTest.php @@ -46,7 +46,7 @@ class BookmarkExportTest extends \PHPUnit\Framework\TestCase self::$refDb->write(self::$testDatastore); $history = new History('sandbox/history.php'); self::$bookmarkService = new BookmarkFileService($conf, $history, true); - $factory = new FormatterFactory($conf); + $factory = new FormatterFactory($conf, true); self::$formatter = $factory->getFormatter('raw'); } -- cgit v1.2.3 From e8a10f312a5c44314292402bb44e6ee2e71f3d5d Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Wed, 17 Jun 2020 15:55:31 +0200 Subject: Use NetscapeBookmarkUtils object instance instead of static calls --- tests/netscape/BookmarkExportTest.php | 65 ++++++++++++++++++++++------------- tests/netscape/BookmarkImportTest.php | 53 +++++++++++++++------------- 2 files changed, 72 insertions(+), 46 deletions(-) (limited to 'tests/netscape') diff --git a/tests/netscape/BookmarkExportTest.php b/tests/netscape/BookmarkExportTest.php index 6c948bba..344c1dc0 100644 --- a/tests/netscape/BookmarkExportTest.php +++ b/tests/netscape/BookmarkExportTest.php @@ -1,11 +1,12 @@ set('resource.datastore', self::$testDatastore); - self::$refDb = new \ReferenceLinkDB(); - self::$refDb->write(self::$testDatastore); - $history = new History('sandbox/history.php'); - self::$bookmarkService = new BookmarkFileService($conf, $history, true); - $factory = new FormatterFactory($conf, true); - self::$formatter = $factory->getFormatter('raw'); + static::$conf = new ConfigManager('tests/utils/config/configJson'); + static::$conf->set('resource.datastore', static::$testDatastore); + static::$refDb = new \ReferenceLinkDB(); + static::$refDb->write(static::$testDatastore); + static::$history = new History('sandbox/history.php'); + static::$bookmarkService = new BookmarkFileService(static::$conf, static::$history, true); + $factory = new FormatterFactory(static::$conf, true); + static::$formatter = $factory->getFormatter('raw'); + } + + public function setUp(): void + { + $this->netscapeBookmarkUtils = new NetscapeBookmarkUtils( + static::$bookmarkService, + static::$conf, + static::$history + ); } /** @@ -57,8 +82,7 @@ class BookmarkExportTest extends \PHPUnit\Framework\TestCase */ public function testFilterAndFormatInvalid() { - NetscapeBookmarkUtils::filterAndFormat( - self::$bookmarkService, + $this->netscapeBookmarkUtils->filterAndFormat( self::$formatter, 'derp', false, @@ -71,8 +95,7 @@ class BookmarkExportTest extends \PHPUnit\Framework\TestCase */ public function testFilterAndFormatAll() { - $links = NetscapeBookmarkUtils::filterAndFormat( - self::$bookmarkService, + $links = $this->netscapeBookmarkUtils->filterAndFormat( self::$formatter, 'all', false, @@ -97,8 +120,7 @@ class BookmarkExportTest extends \PHPUnit\Framework\TestCase */ public function testFilterAndFormatPrivate() { - $links = NetscapeBookmarkUtils::filterAndFormat( - self::$bookmarkService, + $links = $this->netscapeBookmarkUtils->filterAndFormat( self::$formatter, 'private', false, @@ -123,8 +145,7 @@ class BookmarkExportTest extends \PHPUnit\Framework\TestCase */ public function testFilterAndFormatPublic() { - $links = NetscapeBookmarkUtils::filterAndFormat( - self::$bookmarkService, + $links = $this->netscapeBookmarkUtils->filterAndFormat( self::$formatter, 'public', false, @@ -149,8 +170,7 @@ class BookmarkExportTest extends \PHPUnit\Framework\TestCase */ public function testFilterAndFormatDoNotPrependNoteUrl() { - $links = NetscapeBookmarkUtils::filterAndFormat( - self::$bookmarkService, + $links = $this->netscapeBookmarkUtils->filterAndFormat( self::$formatter, 'public', false, @@ -168,8 +188,7 @@ class BookmarkExportTest extends \PHPUnit\Framework\TestCase public function testFilterAndFormatPrependNoteUrl() { $indexUrl = 'http://localhost:7469/shaarli/'; - $links = NetscapeBookmarkUtils::filterAndFormat( - self::$bookmarkService, + $links = $this->netscapeBookmarkUtils->filterAndFormat( self::$formatter, 'public', true, diff --git a/tests/netscape/BookmarkImportTest.php b/tests/netscape/BookmarkImportTest.php index fef7f6d1..20b1c6f4 100644 --- a/tests/netscape/BookmarkImportTest.php +++ b/tests/netscape/BookmarkImportTest.php @@ -1,11 +1,12 @@ conf->set('resource.datastore', self::$testDatastore); $this->history = new History(self::$historyFilePath); $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true); + $this->netscapeBookmarkUtils = new NetscapeBookmarkUtils($this->bookmarkService, $this->conf, $this->history); } /** @@ -115,7 +122,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $this->assertEquals( 'File empty.htm (0 bytes) has an unknown file format.' .' Nothing was imported.', - NetscapeBookmarkUtils::import(null, $files, null, $this->conf, $this->history) + $this->netscapeBookmarkUtils->import(null, $files) ); $this->assertEquals(0, $this->bookmarkService->count()); } @@ -128,7 +135,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $files = file2array('no_doctype.htm'); $this->assertEquals( 'File no_doctype.htm (350 bytes) has an unknown file format. Nothing was imported.', - NetscapeBookmarkUtils::import(null, $files, null, $this->conf, $this->history) + $this->netscapeBookmarkUtils->import(null, $files) ); $this->assertEquals(0, $this->bookmarkService->count()); } @@ -142,7 +149,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $this->assertStringMatchesFormat( 'File lowercase_doctype.htm (386 bytes) was successfully processed in %d seconds:' .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', - NetscapeBookmarkUtils::import(null, $files, $this->bookmarkService, $this->conf, $this->history) + $this->netscapeBookmarkUtils->import(null, $files) ); $this->assertEquals(2, $this->bookmarkService->count()); } @@ -157,7 +164,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $this->assertStringMatchesFormat( 'File internet_explorer_encoding.htm (356 bytes) was successfully processed in %d seconds:' .' 1 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', - NetscapeBookmarkUtils::import([], $files, $this->bookmarkService, $this->conf, $this->history) + $this->netscapeBookmarkUtils->import([], $files) ); $this->assertEquals(1, $this->bookmarkService->count()); $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); @@ -185,7 +192,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $this->assertStringMatchesFormat( 'File netscape_nested.htm (1337 bytes) was successfully processed in %d seconds:' .' 8 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', - NetscapeBookmarkUtils::import([], $files, $this->bookmarkService, $this->conf, $this->history) + $this->netscapeBookmarkUtils->import([], $files) ); $this->assertEquals(8, $this->bookmarkService->count()); $this->assertEquals(2, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); @@ -306,7 +313,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $this->assertStringMatchesFormat( 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', - NetscapeBookmarkUtils::import([], $files, $this->bookmarkService, $this->conf, $this->history) + $this->netscapeBookmarkUtils->import([], $files) ); $this->assertEquals(2, $this->bookmarkService->count()); @@ -349,7 +356,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $this->assertStringMatchesFormat( 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', - NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history) + $this->netscapeBookmarkUtils->import($post, $files) ); $this->assertEquals(2, $this->bookmarkService->count()); @@ -392,7 +399,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $this->assertStringMatchesFormat( 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', - NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history) + $this->netscapeBookmarkUtils->import($post, $files) ); $this->assertEquals(2, $this->bookmarkService->count()); $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); @@ -410,7 +417,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $this->assertStringMatchesFormat( 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', - NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history) + $this->netscapeBookmarkUtils->import($post, $files) ); $this->assertEquals(2, $this->bookmarkService->count()); $this->assertEquals(2, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); @@ -430,7 +437,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $this->assertStringMatchesFormat( 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', - NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history) + $this->netscapeBookmarkUtils->import($post, $files) ); $this->assertEquals(2, $this->bookmarkService->count()); $this->assertEquals(2, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); @@ -445,7 +452,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $this->assertStringMatchesFormat( 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' .' 2 bookmarks imported, 2 bookmarks overwritten, 0 bookmarks skipped.', - NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history) + $this->netscapeBookmarkUtils->import($post, $files) ); $this->assertEquals(2, $this->bookmarkService->count()); $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); @@ -465,7 +472,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $this->assertStringMatchesFormat( 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', - NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history) + $this->netscapeBookmarkUtils->import($post, $files) ); $this->assertEquals(2, $this->bookmarkService->count()); $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); @@ -480,7 +487,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $this->assertStringMatchesFormat( 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' .' 2 bookmarks imported, 2 bookmarks overwritten, 0 bookmarks skipped.', - NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history) + $this->netscapeBookmarkUtils->import($post, $files) ); $this->assertEquals(2, $this->bookmarkService->count()); $this->assertEquals(2, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); @@ -498,7 +505,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $this->assertStringMatchesFormat( 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', - NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history) + $this->netscapeBookmarkUtils->import($post, $files) ); $this->assertEquals(2, $this->bookmarkService->count()); $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); @@ -508,7 +515,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $this->assertStringMatchesFormat( 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' .' 0 bookmarks imported, 0 bookmarks overwritten, 2 bookmarks skipped.', - NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history) + $this->netscapeBookmarkUtils->import($post, $files) ); $this->assertEquals(2, $this->bookmarkService->count()); $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); @@ -527,7 +534,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $this->assertStringMatchesFormat( 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', - NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history) + $this->netscapeBookmarkUtils->import($post, $files) ); $this->assertEquals(2, $this->bookmarkService->count()); $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); @@ -548,7 +555,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $this->assertStringMatchesFormat( 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', - NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history) + $this->netscapeBookmarkUtils->import($post, $files) ); $this->assertEquals(2, $this->bookmarkService->count()); $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); @@ -573,7 +580,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase $this->assertStringMatchesFormat( 'File same_date.htm (453 bytes) was successfully processed in %d seconds:' .' 3 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.', - NetscapeBookmarkUtils::import(array(), $files, $this->bookmarkService, $this->conf, $this->history) + $this->netscapeBookmarkUtils->import(array(), $files) ); $this->assertEquals(3, $this->bookmarkService->count()); $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE)); @@ -589,14 +596,14 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase 'overwrite' => 'true', ]; $files = file2array('netscape_basic.htm'); - NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history); + $this->netscapeBookmarkUtils->import($post, $files); $history = $this->history->getHistory(); $this->assertEquals(1, count($history)); $this->assertEquals(History::IMPORT, $history[0]['event']); $this->assertTrue(new DateTime('-5 seconds') < $history[0]['datetime']); // re-import as private, enable overwriting - NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history); + $this->netscapeBookmarkUtils->import($post, $files); $history = $this->history->getHistory(); $this->assertEquals(2, count($history)); $this->assertEquals(History::IMPORT, $history[0]['event']); -- cgit v1.2.3 From 78657347c5b463d7c22bfc8c87b7db39fe058833 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Wed, 17 Jun 2020 19:08:02 +0200 Subject: Process bookmarks import through Slim controller --- tests/netscape/BookmarkImportTest.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'tests/netscape') diff --git a/tests/netscape/BookmarkImportTest.php b/tests/netscape/BookmarkImportTest.php index 20b1c6f4..f678e26b 100644 --- a/tests/netscape/BookmarkImportTest.php +++ b/tests/netscape/BookmarkImportTest.php @@ -4,27 +4,28 @@ namespace Shaarli\Netscape; use DateTime; use PHPUnit\Framework\TestCase; +use Psr\Http\Message\UploadedFileInterface; use Shaarli\Bookmark\Bookmark; use Shaarli\Bookmark\BookmarkFileService; use Shaarli\Bookmark\BookmarkFilter; use Shaarli\Config\ConfigManager; use Shaarli\History; +use Slim\Http\UploadedFile; /** * Utility function to load a file's metadata in a $_FILES-like array * * @param string $filename Basename of the file * - * @return array A $_FILES-like array + * @return UploadedFileInterface Upload file in PSR-7 compatible object */ function file2array($filename) { - return array( - 'filetoupload' => array( - 'name' => $filename, - 'tmp_name' => __DIR__ . '/input/' . $filename, - 'size' => filesize(__DIR__ . '/input/' . $filename) - ) + return new UploadedFile( + __DIR__ . '/input/' . $filename, + $filename, + null, + filesize(__DIR__ . '/input/' . $filename) ); } -- 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/netscape/BookmarkExportTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/netscape') diff --git a/tests/netscape/BookmarkExportTest.php b/tests/netscape/BookmarkExportTest.php index 344c1dc0..509da51d 100644 --- a/tests/netscape/BookmarkExportTest.php +++ b/tests/netscape/BookmarkExportTest.php @@ -177,7 +177,7 @@ class BookmarkExportTest extends TestCase '' ); $this->assertEquals( - '?WDWyig', + '/shaare/WDWyig', $links[2]['url'] ); } @@ -195,7 +195,7 @@ class BookmarkExportTest extends TestCase $indexUrl ); $this->assertEquals( - $indexUrl . '?WDWyig', + $indexUrl . 'shaare/WDWyig', $links[2]['url'] ); } -- 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/netscape/BookmarkExportTest.php | 2 +- tests/netscape/BookmarkImportTest.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'tests/netscape') diff --git a/tests/netscape/BookmarkExportTest.php b/tests/netscape/BookmarkExportTest.php index 509da51d..13b85f3d 100644 --- a/tests/netscape/BookmarkExportTest.php +++ b/tests/netscape/BookmarkExportTest.php @@ -54,7 +54,7 @@ class BookmarkExportTest extends TestCase /** * Instantiate reference data */ - public static function setUpBeforeClass() + public static function setUpBeforeClass(): void { static::$conf = new ConfigManager('tests/utils/config/configJson'); static::$conf->set('resource.datastore', static::$testDatastore); diff --git a/tests/netscape/BookmarkImportTest.php b/tests/netscape/BookmarkImportTest.php index f678e26b..89ae4aa7 100644 --- a/tests/netscape/BookmarkImportTest.php +++ b/tests/netscape/BookmarkImportTest.php @@ -75,7 +75,7 @@ class BookmarkImportTest extends TestCase */ protected static $defaultTimeZone; - public static function setUpBeforeClass() + public static function setUpBeforeClass(): void { self::$defaultTimeZone = date_default_timezone_get(); // Timezone without DST for test consistency @@ -85,7 +85,7 @@ class BookmarkImportTest extends TestCase /** * Resets test data before each test */ - protected function setUp() + protected function setUp(): void { if (file_exists(self::$testDatastore)) { unlink(self::$testDatastore); @@ -104,12 +104,12 @@ class BookmarkImportTest extends TestCase /** * Delete history file. */ - public function tearDown() + protected function tearDown(): void { @unlink(self::$historyFilePath); } - public static function tearDownAfterClass() + public static function tearDownAfterClass(): void { date_default_timezone_set(self::$defaultTimeZone); } -- 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/netscape/BookmarkExportTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'tests/netscape') diff --git a/tests/netscape/BookmarkExportTest.php b/tests/netscape/BookmarkExportTest.php index 13b85f3d..a6eacae4 100644 --- a/tests/netscape/BookmarkExportTest.php +++ b/tests/netscape/BookmarkExportTest.php @@ -78,10 +78,11 @@ class BookmarkExportTest extends TestCase /** * Attempt to export an invalid link selection * @expectedException Exception - * @expectedExceptionMessageRegExp /Invalid export selection/ */ public function testFilterAndFormatInvalid() { + $this->expectExceptionMessageRegExp('/Invalid export selection/'); + $this->netscapeBookmarkUtils->filterAndFormat( self::$formatter, 'derp', -- 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/netscape/BookmarkExportTest.php | 2 +- tests/netscape/BookmarkImportTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/netscape') diff --git a/tests/netscape/BookmarkExportTest.php b/tests/netscape/BookmarkExportTest.php index a6eacae4..adf62f4f 100644 --- a/tests/netscape/BookmarkExportTest.php +++ b/tests/netscape/BookmarkExportTest.php @@ -2,12 +2,12 @@ namespace Shaarli\Netscape; -use PHPUnit\Framework\TestCase; use Shaarli\Bookmark\BookmarkFileService; use Shaarli\Config\ConfigManager; use Shaarli\Formatter\BookmarkFormatter; use Shaarli\Formatter\FormatterFactory; use Shaarli\History; +use Shaarli\TestCase; require_once 'tests/utils/ReferenceLinkDB.php'; diff --git a/tests/netscape/BookmarkImportTest.php b/tests/netscape/BookmarkImportTest.php index 89ae4aa7..c1e49b5f 100644 --- a/tests/netscape/BookmarkImportTest.php +++ b/tests/netscape/BookmarkImportTest.php @@ -3,13 +3,13 @@ namespace Shaarli\Netscape; use DateTime; -use PHPUnit\Framework\TestCase; use Psr\Http\Message\UploadedFileInterface; use Shaarli\Bookmark\Bookmark; use Shaarli\Bookmark\BookmarkFileService; use Shaarli\Bookmark\BookmarkFilter; use Shaarli\Config\ConfigManager; use Shaarli\History; +use Shaarli\TestCase; use Slim\Http\UploadedFile; /** -- 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/netscape/BookmarkExportTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/netscape') diff --git a/tests/netscape/BookmarkExportTest.php b/tests/netscape/BookmarkExportTest.php index adf62f4f..9b95ccc9 100644 --- a/tests/netscape/BookmarkExportTest.php +++ b/tests/netscape/BookmarkExportTest.php @@ -77,10 +77,10 @@ class BookmarkExportTest extends TestCase /** * Attempt to export an invalid link selection - * @expectedException Exception */ public function testFilterAndFormatInvalid() { + $this->expectException(\Exception::class); $this->expectExceptionMessageRegExp('/Invalid export selection/'); $this->netscapeBookmarkUtils->filterAndFormat( -- cgit v1.2.3