From 15170b516429341ae8e1a9ad2111be5ef90bf6aa Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 2 Aug 2016 11:02:20 +0200 Subject: Parse plugin parameters description with the PluginManager Plugin parameter can contain a description in their meta file under the key: parameter.="" --- tests/PluginManagerTest.php | 10 ++++++++-- tests/config/ConfigPluginTest.php | 12 ++++++------ tests/plugins/test/test.meta | 4 +++- 3 files changed, 17 insertions(+), 9 deletions(-) (limited to 'tests') diff --git a/tests/PluginManagerTest.php b/tests/PluginManagerTest.php index 61efce68..f4826e2e 100644 --- a/tests/PluginManagerTest.php +++ b/tests/PluginManagerTest.php @@ -79,8 +79,14 @@ class PluginManagerTest extends PHPUnit_Framework_TestCase $this->pluginManager->load(array(self::$pluginName)); $expectedParameters = array( - 'pop' => '', - 'hip' => '', + 'pop' => array( + 'value' => '', + 'desc' => 'pop description', + ), + 'hip' => array( + 'value' => '', + 'desc' => '', + ), ); $meta = $this->pluginManager->getPluginsMeta(); $this->assertEquals('test plugin', $meta[self::$pluginName]['description']); diff --git a/tests/config/ConfigPluginTest.php b/tests/config/ConfigPluginTest.php index 716631b0..3b37cd79 100644 --- a/tests/config/ConfigPluginTest.php +++ b/tests/config/ConfigPluginTest.php @@ -101,9 +101,9 @@ class ConfigPluginTest extends PHPUnit_Framework_TestCase $plugins = array( 'plugin_name' => array( 'parameters' => array( - 'param1' => true, - 'param2' => false, - 'param3' => '', + 'param1' => array('value' => true), + 'param2' => array('value' => false), + 'param3' => array('value' => ''), ) ) ); @@ -114,8 +114,8 @@ class ConfigPluginTest extends PHPUnit_Framework_TestCase ); $result = load_plugin_parameter_values($plugins, $parameters); - $this->assertEquals('value1', $result['plugin_name']['parameters']['param1']); - $this->assertEquals('value2', $result['plugin_name']['parameters']['param2']); - $this->assertEquals('', $result['plugin_name']['parameters']['param3']); + $this->assertEquals('value1', $result['plugin_name']['parameters']['param1']['value']); + $this->assertEquals('value2', $result['plugin_name']['parameters']['param2']['value']); + $this->assertEquals('', $result['plugin_name']['parameters']['param3']['value']); } } diff --git a/tests/plugins/test/test.meta b/tests/plugins/test/test.meta index ab999ed4..26f243f0 100644 --- a/tests/plugins/test/test.meta +++ b/tests/plugins/test/test.meta @@ -1,2 +1,4 @@ description="test plugin" -parameters="pop;hip" \ No newline at end of file +parameters="pop;hip" +parameter.pop="pop description" +parameter.hip= \ No newline at end of file -- cgit v1.2.3 From c6d876bb2afe7e9ec1a64c74e766360e2fa441e0 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Wed, 3 Aug 2016 09:45:28 +0200 Subject: Set updated date for items in feeds RSS doesn't support updated date for items, so we use the ATOM extension. Updated dates also bump the global update --- tests/FeedBuilderTest.php | 14 +++++++++++--- tests/utils/ReferenceLinkDB.php | 6 ++++-- 2 files changed, 15 insertions(+), 5 deletions(-) (limited to 'tests') diff --git a/tests/FeedBuilderTest.php b/tests/FeedBuilderTest.php index 460fb0c5..c9ff397d 100644 --- a/tests/FeedBuilderTest.php +++ b/tests/FeedBuilderTest.php @@ -76,7 +76,7 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase // Test headers (RSS) $this->assertEquals(self::$RSS_LANGUAGE, $data['language']); $this->assertEmpty($data['pubsubhub_url']); - $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $data['last_update']); + $this->assertRegExp('/Wed, 03 Aug 2016 09:30:33 \+\d{4}/', $data['last_update']); $this->assertEquals(true, $data['show_dates']); $this->assertEquals('http://host.tld/index.php?do=feed', $data['self_link']); $this->assertEquals('http://host.tld/', $data['index_url']); @@ -88,7 +88,10 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase $this->assertEquals('20150310_114651', $link['linkdate']); $this->assertEquals('http://host.tld/?WDWyig', $link['guid']); $this->assertEquals('http://host.tld/?WDWyig', $link['url']); - $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['iso_date']); + $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['pub_iso_date']); + $pub = DateTime::createFromFormat(DateTime::RSS, $link['pub_iso_date']); + $up = DateTime::createFromFormat(DateTime::ATOM, $link['up_iso_date']); + $this->assertEquals($pub, $up); $this->assertContains('Stallman has a beard', $link['description']); $this->assertContains('Permalink', $link['description']); $this->assertContains('http://host.tld/?WDWyig', $link['description']); @@ -101,6 +104,9 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase // Test multitags. $this->assertEquals(5, count($data['links']['20141125_084734']['taglist'])); $this->assertEquals('css', $data['links']['20141125_084734']['taglist'][0]); + + // Test update date + $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links']['20150310_114633']['up_iso_date']); } /** @@ -112,8 +118,10 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase $feedBuilder->setLocale(self::$LOCALE); $data = $feedBuilder->buildData(); $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); + $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']); $link = array_shift($data['links']); - $this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:+\d{2}/', $link['iso_date']); + $this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:\d{2}/', $link['pub_iso_date']); + $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links']['20150310_114633']['up_iso_date']); } /** diff --git a/tests/utils/ReferenceLinkDB.php b/tests/utils/ReferenceLinkDB.php index fcc7a4f9..937961c8 100644 --- a/tests/utils/ReferenceLinkDB.php +++ b/tests/utils/ReferenceLinkDB.php @@ -30,7 +30,8 @@ class ReferenceLinkDB 'Richard Stallman and the Free Software Revolution. Read this. #hashtag', 0, '20150310_114633', - 'free gnu software stallman -exclude stuff hashtag' + 'free gnu software stallman -exclude stuff hashtag', + '20160803_093033' ); $this->addLink( @@ -82,7 +83,7 @@ class ReferenceLinkDB /** * Adds a new link */ - protected function addLink($title, $url, $description, $private, $date, $tags) + protected function addLink($title, $url, $description, $private, $date, $tags, $updated = '') { $link = array( 'title' => $title, @@ -91,6 +92,7 @@ class ReferenceLinkDB 'private' => $private, 'linkdate' => $date, 'tags' => $tags, + 'updated' => $updated, ); $this->_links[$date] = $link; -- cgit v1.2.3 From 50d179183810a7b719bc10da2b9c4a95fd9dddee Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Wed, 3 Aug 2016 10:36:47 +0200 Subject: Add trusted IPs in config and try to ban forwarded IP on failed login * Add a new settings (which needs to be manually set): `security.trusted_proxies` * On login failure, if the `REMOTE_ADDR` is in the trusted proxies, try to retrieve the forwarded IP in headers. * If found, the client address is added in ipbans, else we do nothing. Fixes #409 --- tests/HttpUtils/GetIpAdressFromProxyTest.php | 58 ++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 tests/HttpUtils/GetIpAdressFromProxyTest.php (limited to 'tests') diff --git a/tests/HttpUtils/GetIpAdressFromProxyTest.php b/tests/HttpUtils/GetIpAdressFromProxyTest.php new file mode 100644 index 00000000..6a74a45a --- /dev/null +++ b/tests/HttpUtils/GetIpAdressFromProxyTest.php @@ -0,0 +1,58 @@ +assertFalse(getIpAddressFromProxy(array(), array())); + } + + /** + * Test with a single IP in proxy header. + */ + public function testWithOneForwardedIp() + { + $ip = '1.1.1.1'; + $server = array('HTTP_X_FORWARDED_FOR' => $ip); + $this->assertEquals($ip, getIpAddressFromProxy($server, array())); + } + + /** + * Test with a multiple IPs in proxy header. + */ + public function testWithMultipleForwardedIp() + { + $ip = '1.1.1.1'; + $ip2 = '2.2.2.2'; + + $server = array('HTTP_X_FORWARDED_FOR' => $ip .','. $ip2); + $this->assertEquals($ip2, getIpAddressFromProxy($server, array())); + + $server = array('HTTP_X_FORWARDED_FOR' => $ip .' , '. $ip2); + $this->assertEquals($ip2, getIpAddressFromProxy($server, array())); + } + + /** + * Test with a trusted IP address. + */ + public function testWithTrustedIp() + { + $ip = '1.1.1.1'; + $ip2 = '2.2.2.2'; + + $server = array('HTTP_X_FORWARDED_FOR' => $ip); + $this->assertFalse(getIpAddressFromProxy($server, array($ip))); + + $server = array('HTTP_X_FORWARDED_FOR' => $ip .','. $ip2); + $this->assertEquals($ip2, getIpAddressFromProxy($server, array($ip))); + $this->assertFalse(getIpAddressFromProxy($server, array($ip, $ip2))); + } +} -- cgit v1.2.3 From a973afeac7b7399d35b881920f0afc1947765ccd Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Thu, 28 Jul 2016 22:54:33 +0200 Subject: Refactor bookmark import using a generic Netscape parser Relates to #607 Relates to #608 Relates to #493 (abandoned) Additions: - use Composer's autoload to load 3rd-party dependencies under vendor/ Modifications: - [import] replace the current parser with a generic, stable parser - move code to application/NetscapeBookmarkUtils - improve status report after parsing - [router] use the same endpoint for both bookmark upload and import dialog - [template] update bookmark import options - allow adding tags to all imported links - allow selecting the visibility (privacy) of imported links - [tests] ensure bookmarks are properly parsed and imported in the LinkDB - reuse reference input from the parser's test data See: - https://github.com/shaarli/netscape-bookmark-parser - https://getcomposer.org/doc/01-basic-usage.md#autoloading Signed-off-by: VirtualTam --- tests/NetscapeBookmarkUtils/BookmarkExportTest.php | 134 ++++++ tests/NetscapeBookmarkUtils/BookmarkImportTest.php | 518 +++++++++++++++++++++ tests/NetscapeBookmarkUtils/input/empty.htm | 0 .../NetscapeBookmarkUtils/input/netscape_basic.htm | 11 + .../input/netscape_nested.htm | 31 ++ tests/NetscapeBookmarkUtils/input/no_doctype.htm | 7 + tests/NetscapeBookmarkUtils/input/same_date.htm | 11 + tests/NetscapeBookmarkUtilsTest.php | 134 ------ 8 files changed, 712 insertions(+), 134 deletions(-) create mode 100644 tests/NetscapeBookmarkUtils/BookmarkExportTest.php create mode 100644 tests/NetscapeBookmarkUtils/BookmarkImportTest.php create mode 100644 tests/NetscapeBookmarkUtils/input/empty.htm create mode 100644 tests/NetscapeBookmarkUtils/input/netscape_basic.htm create mode 100644 tests/NetscapeBookmarkUtils/input/netscape_nested.htm create mode 100644 tests/NetscapeBookmarkUtils/input/no_doctype.htm create mode 100644 tests/NetscapeBookmarkUtils/input/same_date.htm delete mode 100644 tests/NetscapeBookmarkUtilsTest.php (limited to 'tests') diff --git a/tests/NetscapeBookmarkUtils/BookmarkExportTest.php b/tests/NetscapeBookmarkUtils/BookmarkExportTest.php new file mode 100644 index 00000000..cc54ab9f --- /dev/null +++ b/tests/NetscapeBookmarkUtils/BookmarkExportTest.php @@ -0,0 +1,134 @@ +write(self::$testDatastore); + self::$linkDb = new LinkDB(self::$testDatastore, true, false); + } + + /** + * Attempt to export an invalid link selection + * @expectedException Exception + * @expectedExceptionMessageRegExp /Invalid export selection/ + */ + public function testFilterAndFormatInvalid() + { + NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'derp', false, ''); + } + + /** + * Prepare all links for export + */ + public function testFilterAndFormatAll() + { + $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'all', false, ''); + $this->assertEquals(self::$refDb->countLinks(), sizeof($links)); + foreach ($links as $link) { + $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); + $this->assertEquals( + $date->getTimestamp(), + $link['timestamp'] + ); + $this->assertEquals( + str_replace(' ', ',', $link['tags']), + $link['taglist'] + ); + } + } + + /** + * Prepare private links for export + */ + public function testFilterAndFormatPrivate() + { + $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'private', false, ''); + $this->assertEquals(self::$refDb->countPrivateLinks(), sizeof($links)); + foreach ($links as $link) { + $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); + $this->assertEquals( + $date->getTimestamp(), + $link['timestamp'] + ); + $this->assertEquals( + str_replace(' ', ',', $link['tags']), + $link['taglist'] + ); + } + } + + /** + * Prepare public links for export + */ + public function testFilterAndFormatPublic() + { + $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'public', false, ''); + $this->assertEquals(self::$refDb->countPublicLinks(), sizeof($links)); + foreach ($links as $link) { + $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); + $this->assertEquals( + $date->getTimestamp(), + $link['timestamp'] + ); + $this->assertEquals( + str_replace(' ', ',', $link['tags']), + $link['taglist'] + ); + } + } + + /** + * Do not prepend notes with the Shaarli index's URL + */ + public function testFilterAndFormatDoNotPrependNoteUrl() + { + $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'public', false, ''); + $this->assertEquals( + '?WDWyig', + $links[0]['url'] + ); + } + + /** + * Prepend notes with the Shaarli index's URL + */ + public function testFilterAndFormatPrependNoteUrl() + { + $indexUrl = 'http://localhost:7469/shaarli/'; + $links = NetscapeBookmarkUtils::filterAndFormat( + self::$linkDb, + 'public', + true, + $indexUrl + ); + $this->assertEquals( + $indexUrl . '?WDWyig', + $links[0]['url'] + ); + } +} diff --git a/tests/NetscapeBookmarkUtils/BookmarkImportTest.php b/tests/NetscapeBookmarkUtils/BookmarkImportTest.php new file mode 100644 index 00000000..2d4e7557 --- /dev/null +++ b/tests/NetscapeBookmarkUtils/BookmarkImportTest.php @@ -0,0 +1,518 @@ + array( + 'name' => $filename, + 'tmp_name' => __DIR__ . '/input/' . $filename, + 'size' => filesize(__DIR__ . '/input/' . $filename) + ) + ); +} + + +/** + * Netscape bookmark import + */ +class BookmarkImportTest extends PHPUnit_Framework_TestCase +{ + /** + * @var string datastore to test write operations + */ + protected static $testDatastore = 'sandbox/datastore.php'; + + /** + * @var LinkDB private LinkDB instance + */ + protected $linkDb = null; + + /** + * @var string Dummy page cache + */ + protected $pagecache = 'tests'; + + /** + * Resets test data before each test + */ + protected function setUp() + { + if (file_exists(self::$testDatastore)) { + unlink(self::$testDatastore); + } + // start with an empty datastore + file_put_contents(self::$testDatastore, ''); + $this->linkDb = new LinkDB(self::$testDatastore, true, false); + } + + /** + * Attempt to import bookmarks from an empty file + */ + public function testImportEmptyData() + { + $files = file2array('empty.htm'); + $this->assertEquals( + 'File empty.htm (0 bytes) has an unknown file format.' + .' Nothing was imported.', + NetscapeBookmarkUtils::import(NULL, $files, NULL, NULL) + ); + $this->assertEquals(0, count($this->linkDb)); + } + + /** + * Attempt to import bookmarks from a file with no Doctype + */ + public function testImportNoDoctype() + { + $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, NULL) + ); + $this->assertEquals(0, count($this->linkDb)); + } + + /** + * Import bookmarks nested in a folder hierarchy + */ + public function testImportNested() + { + $files = file2array('netscape_nested.htm'); + $this->assertEquals( + 'File netscape_nested.htm (1337 bytes) was successfully processed:' + .' 8 links imported, 0 links overwritten, 0 links skipped.', + NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->pagecache) + ); + $this->assertEquals(8, count($this->linkDb)); + $this->assertEquals(2, count_private($this->linkDb)); + + $this->assertEquals( + array( + 'linkdate' => '20160225_205541', + 'title' => 'Nested 1', + 'url' => 'http://nest.ed/1', + 'description' => '', + 'private' => 0, + 'tags' => 'tag1 tag2' + ), + $this->linkDb->getLinkFromUrl('http://nest.ed/1') + ); + $this->assertEquals( + array( + 'linkdate' => '20160225_205542', + 'title' => 'Nested 1-1', + 'url' => 'http://nest.ed/1-1', + 'description' => '', + 'private' => 0, + 'tags' => 'folder1 tag1 tag2' + ), + $this->linkDb->getLinkFromUrl('http://nest.ed/1-1') + ); + $this->assertEquals( + array( + 'linkdate' => '20160225_205547', + 'title' => 'Nested 1-2', + 'url' => 'http://nest.ed/1-2', + 'description' => '', + 'private' => 0, + 'tags' => 'folder1 tag3 tag4' + ), + $this->linkDb->getLinkFromUrl('http://nest.ed/1-2') + ); + $this->assertEquals( + array( + 'linkdate' => '20160202_172222', + 'title' => 'Nested 2-1', + 'url' => 'http://nest.ed/2-1', + 'description' => 'First link of the second section', + 'private' => 1, + 'tags' => 'folder2' + ), + $this->linkDb->getLinkFromUrl('http://nest.ed/2-1') + ); + $this->assertEquals( + array( + 'linkdate' => '20160119_200227', + 'title' => 'Nested 2-2', + 'url' => 'http://nest.ed/2-2', + 'description' => 'Second link of the second section', + 'private' => 1, + 'tags' => 'folder2' + ), + $this->linkDb->getLinkFromUrl('http://nest.ed/2-2') + ); + $this->assertEquals( + array( + 'linkdate' => '20160202_172223', + 'title' => 'Nested 3-1', + 'url' => 'http://nest.ed/3-1', + 'description' => '', + 'private' => 0, + 'tags' => 'folder3 folder3-1 tag3' + ), + $this->linkDb->getLinkFromUrl('http://nest.ed/3-1') + ); + $this->assertEquals( + array( + 'linkdate' => '20160119_200228', + 'title' => 'Nested 3-2', + 'url' => 'http://nest.ed/3-2', + 'description' => '', + 'private' => 0, + 'tags' => 'folder3 folder3-1' + ), + $this->linkDb->getLinkFromUrl('http://nest.ed/3-2') + ); + $this->assertEquals( + array( + 'linkdate' => '20160229_081541', + 'title' => 'Nested 2', + 'url' => 'http://nest.ed/2', + 'description' => '', + 'private' => 0, + 'tags' => 'tag4' + ), + $this->linkDb->getLinkFromUrl('http://nest.ed/2') + ); + } + + /** + * Import bookmarks with the default privacy setting (reuse from file) + * + * The $_POST array is not set. + */ + public function testImportDefaultPrivacyNoPost() + { + $files = file2array('netscape_basic.htm'); + $this->assertEquals( + 'File netscape_basic.htm (482 bytes) was successfully processed:' + .' 2 links imported, 0 links overwritten, 0 links skipped.', + NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->pagecache) + ); + $this->assertEquals(2, count($this->linkDb)); + $this->assertEquals(1, count_private($this->linkDb)); + + $this->assertEquals( + array( + 'linkdate' => '20001010_105536', + 'title' => 'Secret stuff', + 'url' => 'https://private.tld', + 'description' => "Super-secret stuff you're not supposed to know about", + 'private' => 1, + 'tags' => 'private secret' + ), + $this->linkDb->getLinkFromUrl('https://private.tld') + ); + $this->assertEquals( + array( + 'linkdate' => '20160225_205548', + 'title' => 'Public stuff', + 'url' => 'http://public.tld', + 'description' => '', + 'private' => 0, + 'tags' => 'public hello world' + ), + $this->linkDb->getLinkFromUrl('http://public.tld') + ); + } + + /** + * Import bookmarks with the default privacy setting (reuse from file) + */ + public function testImportKeepPrivacy() + { + $post = array('privacy' => 'default'); + $files = file2array('netscape_basic.htm'); + $this->assertEquals( + 'File netscape_basic.htm (482 bytes) was successfully processed:' + .' 2 links imported, 0 links overwritten, 0 links skipped.', + NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache) + ); + $this->assertEquals(2, count($this->linkDb)); + $this->assertEquals(1, count_private($this->linkDb)); + + $this->assertEquals( + array( + 'linkdate' => '20001010_105536', + 'title' => 'Secret stuff', + 'url' => 'https://private.tld', + 'description' => "Super-secret stuff you're not supposed to know about", + 'private' => 1, + 'tags' => 'private secret' + ), + $this->linkDb->getLinkFromUrl('https://private.tld') + ); + $this->assertEquals( + array( + 'linkdate' => '20160225_205548', + 'title' => 'Public stuff', + 'url' => 'http://public.tld', + 'description' => '', + 'private' => 0, + 'tags' => 'public hello world' + ), + $this->linkDb->getLinkFromUrl('http://public.tld') + ); + } + + /** + * Import links as public + */ + public function testImportAsPublic() + { + $post = array('privacy' => 'public'); + $files = file2array('netscape_basic.htm'); + $this->assertEquals( + 'File netscape_basic.htm (482 bytes) was successfully processed:' + .' 2 links imported, 0 links overwritten, 0 links skipped.', + NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache) + ); + $this->assertEquals(2, count($this->linkDb)); + $this->assertEquals(0, count_private($this->linkDb)); + $this->assertEquals( + 0, + $this->linkDb['20001010_105536']['private'] + ); + $this->assertEquals( + 0, + $this->linkDb['20160225_205548']['private'] + ); + } + + /** + * Import links as private + */ + public function testImportAsPrivate() + { + $post = array('privacy' => 'private'); + $files = file2array('netscape_basic.htm'); + $this->assertEquals( + 'File netscape_basic.htm (482 bytes) was successfully processed:' + .' 2 links imported, 0 links overwritten, 0 links skipped.', + NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache) + ); + $this->assertEquals(2, count($this->linkDb)); + $this->assertEquals(2, count_private($this->linkDb)); + $this->assertEquals( + 1, + $this->linkDb['20001010_105536']['private'] + ); + $this->assertEquals( + 1, + $this->linkDb['20160225_205548']['private'] + ); + } + + /** + * Overwrite private links so they become public + */ + public function testOverwriteAsPublic() + { + $files = file2array('netscape_basic.htm'); + + // import links as private + $post = array('privacy' => 'private'); + $this->assertEquals( + 'File netscape_basic.htm (482 bytes) was successfully processed:' + .' 2 links imported, 0 links overwritten, 0 links skipped.', + NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache) + ); + $this->assertEquals(2, count($this->linkDb)); + $this->assertEquals(2, count_private($this->linkDb)); + $this->assertEquals( + 1, + $this->linkDb['20001010_105536']['private'] + ); + $this->assertEquals( + 1, + $this->linkDb['20160225_205548']['private'] + ); + + // re-import as public, enable overwriting + $post = array( + 'privacy' => 'public', + 'overwrite' => 'true' + ); + $this->assertEquals( + 'File netscape_basic.htm (482 bytes) was successfully processed:' + .' 2 links imported, 2 links overwritten, 0 links skipped.', + NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache) + ); + $this->assertEquals(2, count($this->linkDb)); + $this->assertEquals(0, count_private($this->linkDb)); + $this->assertEquals( + 0, + $this->linkDb['20001010_105536']['private'] + ); + $this->assertEquals( + 0, + $this->linkDb['20160225_205548']['private'] + ); + } + + /** + * Overwrite public links so they become private + */ + public function testOverwriteAsPrivate() + { + $files = file2array('netscape_basic.htm'); + + // import links as public + $post = array('privacy' => 'public'); + $this->assertEquals( + 'File netscape_basic.htm (482 bytes) was successfully processed:' + .' 2 links imported, 0 links overwritten, 0 links skipped.', + NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache) + ); + $this->assertEquals(2, count($this->linkDb)); + $this->assertEquals(0, count_private($this->linkDb)); + $this->assertEquals( + 0, + $this->linkDb['20001010_105536']['private'] + ); + $this->assertEquals( + 0, + $this->linkDb['20160225_205548']['private'] + ); + + // re-import as private, enable overwriting + $post = array( + 'privacy' => 'private', + 'overwrite' => 'true' + ); + $this->assertEquals( + 'File netscape_basic.htm (482 bytes) was successfully processed:' + .' 2 links imported, 2 links overwritten, 0 links skipped.', + NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache) + ); + $this->assertEquals(2, count($this->linkDb)); + $this->assertEquals(2, count_private($this->linkDb)); + $this->assertEquals( + 1, + $this->linkDb['20001010_105536']['private'] + ); + $this->assertEquals( + 1, + $this->linkDb['20160225_205548']['private'] + ); + } + + /** + * Attept to import the same links twice without enabling overwriting + */ + public function testSkipOverwrite() + { + $post = array('privacy' => 'public'); + $files = file2array('netscape_basic.htm'); + $this->assertEquals( + 'File netscape_basic.htm (482 bytes) was successfully processed:' + .' 2 links imported, 0 links overwritten, 0 links skipped.', + NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache) + ); + $this->assertEquals(2, count($this->linkDb)); + $this->assertEquals(0, count_private($this->linkDb)); + + // re-import as private, DO NOT enable overwriting + $post = array('privacy' => 'private'); + $this->assertEquals( + 'File netscape_basic.htm (482 bytes) was successfully processed:' + .' 0 links imported, 0 links overwritten, 2 links skipped.', + NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache) + ); + $this->assertEquals(2, count($this->linkDb)); + $this->assertEquals(0, count_private($this->linkDb)); + } + + /** + * Add user-specified tags to all imported bookmarks + */ + public function testSetDefaultTags() + { + $post = array( + 'privacy' => 'public', + 'default_tags' => 'tag1,tag2 tag3' + ); + $files = file2array('netscape_basic.htm'); + $this->assertEquals( + 'File netscape_basic.htm (482 bytes) was successfully processed:' + .' 2 links imported, 0 links overwritten, 0 links skipped.', + NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache) + ); + $this->assertEquals(2, count($this->linkDb)); + $this->assertEquals(0, count_private($this->linkDb)); + $this->assertEquals( + 'tag1 tag2 tag3 private secret', + $this->linkDb['20001010_105536']['tags'] + ); + $this->assertEquals( + 'tag1 tag2 tag3 public hello world', + $this->linkDb['20160225_205548']['tags'] + ); + } + + /** + * The user-specified tags contain characters to be escaped + */ + public function testSanitizeDefaultTags() + { + $post = array( + 'privacy' => 'public', + 'default_tags' => 'tag1&,tag2 "tag3"' + ); + $files = file2array('netscape_basic.htm'); + $this->assertEquals( + 'File netscape_basic.htm (482 bytes) was successfully processed:' + .' 2 links imported, 0 links overwritten, 0 links skipped.', + NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache) + ); + $this->assertEquals(2, count($this->linkDb)); + $this->assertEquals(0, count_private($this->linkDb)); + $this->assertEquals( + 'tag1& tag2 "tag3" private secret', + $this->linkDb['20001010_105536']['tags'] + ); + $this->assertEquals( + 'tag1& tag2 "tag3" public hello world', + $this->linkDb['20160225_205548']['tags'] + ); + } + + /** + * Ensure each imported bookmark has a unique linkdate + * + * See https://github.com/shaarli/Shaarli/issues/351 + */ + public function testImportSameDate() + { + $files = file2array('same_date.htm'); + $this->assertEquals( + 'File same_date.htm (453 bytes) was successfully processed:' + .' 3 links imported, 0 links overwritten, 0 links skipped.', + NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->pagecache) + ); + $this->assertEquals(3, count($this->linkDb)); + $this->assertEquals(0, count_private($this->linkDb)); + $this->assertEquals( + '20160225_205548', + $this->linkDb['20160225_205548']['linkdate'] + ); + $this->assertEquals( + '20160225_205549', + $this->linkDb['20160225_205549']['linkdate'] + ); + $this->assertEquals( + '20160225_205550', + $this->linkDb['20160225_205550']['linkdate'] + ); + } +} diff --git a/tests/NetscapeBookmarkUtils/input/empty.htm b/tests/NetscapeBookmarkUtils/input/empty.htm new file mode 100644 index 00000000..e69de29b diff --git a/tests/NetscapeBookmarkUtils/input/netscape_basic.htm b/tests/NetscapeBookmarkUtils/input/netscape_basic.htm new file mode 100644 index 00000000..affe0cf8 --- /dev/null +++ b/tests/NetscapeBookmarkUtils/input/netscape_basic.htm @@ -0,0 +1,11 @@ + + +Bookmarks +

Bookmarks

+

+

Secret stuff +
Super-secret stuff you're not supposed to know about +
Public stuff +

diff --git a/tests/NetscapeBookmarkUtils/input/netscape_nested.htm b/tests/NetscapeBookmarkUtils/input/netscape_nested.htm new file mode 100644 index 00000000..b486fe18 --- /dev/null +++ b/tests/NetscapeBookmarkUtils/input/netscape_nested.htm @@ -0,0 +1,31 @@ + + +Bookmarks +

Bookmarks

+

+

Nested 1 +

Folder1

+

+

Nested 1-1 +
Nested 1-2 +

+

Folder2

+
This second folder contains wonderful links! +

+

Nested 2-1 +
First link of the second section +
Nested 2-2 +
Second link of the second section +

+

Folder3

+

+

Folder3-1

+

+

Nested 3-1 +
Nested 3-2 +

+

+

Nested 2 +

diff --git a/tests/NetscapeBookmarkUtils/input/no_doctype.htm b/tests/NetscapeBookmarkUtils/input/no_doctype.htm new file mode 100644 index 00000000..766d398b --- /dev/null +++ b/tests/NetscapeBookmarkUtils/input/no_doctype.htm @@ -0,0 +1,7 @@ +Bookmarks +

Bookmarks

+

+

Secret stuff +
Super-secret stuff you're not supposed to know about +
Public stuff +

diff --git a/tests/NetscapeBookmarkUtils/input/same_date.htm b/tests/NetscapeBookmarkUtils/input/same_date.htm new file mode 100644 index 00000000..9d58a582 --- /dev/null +++ b/tests/NetscapeBookmarkUtils/input/same_date.htm @@ -0,0 +1,11 @@ + + +Bookmarks +

Bookmarks

+

+

Today's first link +
Today's second link +
Today's third link +

diff --git a/tests/NetscapeBookmarkUtilsTest.php b/tests/NetscapeBookmarkUtilsTest.php deleted file mode 100644 index 41e6d84c..00000000 --- a/tests/NetscapeBookmarkUtilsTest.php +++ /dev/null @@ -1,134 +0,0 @@ -write(self::$testDatastore); - self::$linkDb = new LinkDB(self::$testDatastore, true, false); - } - - /** - * Attempt to export an invalid link selection - * @expectedException Exception - * @expectedExceptionMessageRegExp /Invalid export selection/ - */ - public function testFilterAndFormatInvalid() - { - NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'derp', false, ''); - } - - /** - * Prepare all links for export - */ - public function testFilterAndFormatAll() - { - $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'all', false, ''); - $this->assertEquals(self::$refDb->countLinks(), sizeof($links)); - foreach ($links as $link) { - $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); - $this->assertEquals( - $date->getTimestamp(), - $link['timestamp'] - ); - $this->assertEquals( - str_replace(' ', ',', $link['tags']), - $link['taglist'] - ); - } - } - - /** - * Prepare private links for export - */ - public function testFilterAndFormatPrivate() - { - $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'private', false, ''); - $this->assertEquals(self::$refDb->countPrivateLinks(), sizeof($links)); - foreach ($links as $link) { - $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); - $this->assertEquals( - $date->getTimestamp(), - $link['timestamp'] - ); - $this->assertEquals( - str_replace(' ', ',', $link['tags']), - $link['taglist'] - ); - } - } - - /** - * Prepare public links for export - */ - public function testFilterAndFormatPublic() - { - $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'public', false, ''); - $this->assertEquals(self::$refDb->countPublicLinks(), sizeof($links)); - foreach ($links as $link) { - $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); - $this->assertEquals( - $date->getTimestamp(), - $link['timestamp'] - ); - $this->assertEquals( - str_replace(' ', ',', $link['tags']), - $link['taglist'] - ); - } - } - - /** - * Do not prepend notes with the Shaarli index's URL - */ - public function testFilterAndFormatDoNotPrependNoteUrl() - { - $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'public', false, ''); - $this->assertEquals( - '?WDWyig', - $links[0]['url'] - ); - } - - /** - * Prepend notes with the Shaarli index's URL - */ - public function testFilterAndFormatPrependNoteUrl() - { - $indexUrl = 'http://localhost:7469/shaarli/'; - $links = NetscapeBookmarkUtils::filterAndFormat( - self::$linkDb, - 'public', - true, - $indexUrl - ); - $this->assertEquals( - $indexUrl . '?WDWyig', - $links[0]['url'] - ); - } -} -- cgit v1.2.3 From f4ad7bde56b769cea0a2a26eb739e57500705555 Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Fri, 12 Aug 2016 23:22:15 +0200 Subject: Fix: ensure Internet Explorer bookmark dumps can be imported Relates to https://github.com/shaarli/Shaarli/issues/607 Modifications: - [application][tests] NetscapeBookmarkUtils: more permissive doctype detection The IE bookmark exports contain extra escape sequences, which can be observed by binary comparison of the reference input data used in tests: $ cmp -b -l -n 8 netscape_basic.htm internet_explorer_encoding.htm 1 74 < 357 M-o 2 41 ! 273 M-; 3 104 D 277 M-? 4 117 O 74 < 5 103 C 41 ! 6 124 T 104 D 7 131 Y 117 O 8 120 P 103 C Signed-off-by: VirtualTam --- tests/NetscapeBookmarkUtils/BookmarkImportTest.php | 28 ++++++++++++++++++++++ .../input/internet_explorer_encoding.htm | 9 +++++++ 2 files changed, 37 insertions(+) create mode 100644 tests/NetscapeBookmarkUtils/input/internet_explorer_encoding.htm (limited to 'tests') diff --git a/tests/NetscapeBookmarkUtils/BookmarkImportTest.php b/tests/NetscapeBookmarkUtils/BookmarkImportTest.php index 2d4e7557..f0ad500f 100644 --- a/tests/NetscapeBookmarkUtils/BookmarkImportTest.php +++ b/tests/NetscapeBookmarkUtils/BookmarkImportTest.php @@ -82,6 +82,34 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals(0, count($this->linkDb)); } + /** + * Ensure IE dumps are supported + */ + public function testImportInternetExplorerEncoding() + { + $files = file2array('internet_explorer_encoding.htm'); + $this->assertEquals( + 'File internet_explorer_encoding.htm (356 bytes) was successfully processed:' + .' 1 links imported, 0 links overwritten, 0 links skipped.', + NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->pagecache) + ); + $this->assertEquals(1, count($this->linkDb)); + $this->assertEquals(0, count_private($this->linkDb)); + + $this->assertEquals( + array( + 'linkdate' => '20160618_173944', + 'title' => 'Hg Init a Mercurial tutorial by Joel Spolsky', + 'url' => 'http://hginit.com/', + 'description' => '', + 'private' => 0, + 'tags' => '' + ), + $this->linkDb->getLinkFromUrl('http://hginit.com/') + ); + } + + /** * Import bookmarks nested in a folder hierarchy */ diff --git a/tests/NetscapeBookmarkUtils/input/internet_explorer_encoding.htm b/tests/NetscapeBookmarkUtils/input/internet_explorer_encoding.htm new file mode 100644 index 00000000..18703cf6 --- /dev/null +++ b/tests/NetscapeBookmarkUtils/input/internet_explorer_encoding.htm @@ -0,0 +1,9 @@ + + +Bookmarks +

Bookmarks

+

+

Hg Init a Mercurial tutorial by Joel Spolsky +

-- cgit v1.2.3 From db6dec0de10d0ee3b35658ae9f431935d17e8a7c Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Sat, 13 Aug 2016 14:22:22 +0200 Subject: Fix: add missing final newlines, untabify text Signed-off-by: VirtualTam --- tests/PluginManagerTest.php | 2 +- tests/utils/config/configInvalid.json.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/PluginManagerTest.php b/tests/PluginManagerTest.php index 61efce68..c7511051 100644 --- a/tests/PluginManagerTest.php +++ b/tests/PluginManagerTest.php @@ -86,4 +86,4 @@ class PluginManagerTest extends PHPUnit_Framework_TestCase $this->assertEquals('test plugin', $meta[self::$pluginName]['description']); $this->assertEquals($expectedParameters, $meta[self::$pluginName]['parameters']); } -} \ No newline at end of file +} diff --git a/tests/utils/config/configInvalid.json.php b/tests/utils/config/configInvalid.json.php index 167f2168..e39d640a 100644 --- a/tests/utils/config/configInvalid.json.php +++ b/tests/utils/config/configInvalid.json.php @@ -2,4 +2,4 @@ { bad: bad, } -*/ ?> \ No newline at end of file +*/ ?> -- cgit v1.2.3 From 5e148f8a529abf72256ef74ed149d56a5b27bd72 Mon Sep 17 00:00:00 2001 From: Teromene Date: Tue, 11 Oct 2016 16:37:42 +0100 Subject: Archive.org plugin: do not propose archival of private notes Fixes #637 --- tests/plugins/PluginArchiveorgTest.php | 102 +++++++++++++++++++++++++++++++-- 1 file changed, 98 insertions(+), 4 deletions(-) (limited to 'tests') diff --git a/tests/plugins/PluginArchiveorgTest.php b/tests/plugins/PluginArchiveorgTest.php index dbc52bc8..4daa4c9d 100644 --- a/tests/plugins/PluginArchiveorgTest.php +++ b/tests/plugins/PluginArchiveorgTest.php @@ -7,8 +7,8 @@ require_once 'plugins/archiveorg/archiveorg.php'; /** - * Class PlugQrcodeTest - * Unit test for the QR-Code plugin + * Class PluginArchiveorgTest + * Unit test for the archiveorg plugin */ class PluginArchiveorgTest extends PHPUnit_Framework_TestCase { @@ -21,21 +21,25 @@ class PluginArchiveorgTest extends PHPUnit_Framework_TestCase } /** - * Test render_linklist hook. + * Test render_linklist hook on external links. */ - function testArchiveorgLinklist() + function testArchiveorgLinklistOnExternalLinks() { $str = 'http://randomstr.com/test'; + $data = array( 'title' => $str, 'links' => array( array( 'url' => $str, + 'private' => 0, + 'real_url' => $str ) ) ); $data = hook_archiveorg_render_linklist($data); + $link = $data['links'][0]; // data shouldn't be altered $this->assertEquals($str, $data['title']); @@ -44,5 +48,95 @@ class PluginArchiveorgTest extends PHPUnit_Framework_TestCase // plugin data $this->assertEquals(1, count($link['link_plugin'])); $this->assertNotFalse(strpos($link['link_plugin'][0], $str)); + + } + + /** + * Test render_linklist hook on internal links. + */ + function testArchiveorgLinklistOnInternalLinks() + { + $internalLink1 = 'http://shaarli.shaarli/?qvMAqg'; + $internalLinkRealURL1 = '?qvMAqg'; + + $internalLink2 = 'http://shaarli.shaarli/?2_7zww'; + $internalLinkRealURL2 = '?2_7zww'; + + $internalLink3 = 'http://shaarli.shaarli/?z7u-_Q'; + $internalLinkRealURL3 = '?z7u-_Q'; + + $data = array( + 'title' => $internalLink1, + 'links' => array( + array( + 'url' => $internalLink1, + 'private' => 0, + 'real_url' => $internalLinkRealURL1 + ), + array( + 'url' => $internalLink1, + 'private' => 1, + 'real_url' => $internalLinkRealURL1 + ), + array( + 'url' => $internalLink2, + 'private' => 0, + 'real_url' => $internalLinkRealURL2 + ), + array( + 'url' => $internalLink2, + 'private' => 1, + 'real_url' => $internalLinkRealURL2 + ), + array( + 'url' => $internalLink3, + 'private' => 0, + 'real_url' => $internalLinkRealURL3 + ), + array( + 'url' => $internalLink3, + 'private' => 1, + 'real_url' => $internalLinkRealURL3 + ) + ) + ); + + + $data = hook_archiveorg_render_linklist($data); + + // Case n°1: first link type, public + $link = $data['links'][0]; + + $this->assertEquals(1, count($link['link_plugin'])); + $this->assertNotFalse(strpos($link['link_plugin'][0], $internalLink1)); + + // Case n°2: first link type, private + $link = $data['links'][1]; + + $this->assertArrayNotHasKey('link_plugin', $link); + + // Case n°3: second link type, public + $link = $data['links'][2]; + + $this->assertEquals(1, count($link['link_plugin'])); + $this->assertNotFalse(strpos($link['link_plugin'][0], $internalLink2)); + + // Case n°4: second link type, private + $link = $data['links'][3]; + + $this->assertArrayNotHasKey('link_plugin', $link); + + // Case n°5: third link type, public + $link = $data['links'][4]; + + $this->assertEquals(1, count($link['link_plugin'])); + $this->assertNotFalse(strpos($link['link_plugin'][0], $internalLink3)); + + // Case n°6: third link type, private + $link = $data['links'][5]; + + $this->assertArrayNotHasKey('link_plugin', $link); + } + } -- cgit v1.2.3 From 7fde6de1212323418401c15efba06026c704ca87 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Fri, 14 Oct 2016 13:22:58 +0200 Subject: New init function for plugins, supports errors reporting All plugins can optionally add an init function named `pluginname_init()` which is called when the plugin is loaded. This function is aware of the config, and can return initialization errors, which are displayed in the header template. Note that the previous error system hack no longer work. --- tests/plugins/PluginReadityourselfTest.php | 23 +++++++++++++++++++++-- tests/plugins/PluginWallabagTest.php | 23 +++++++++++++++++++++-- 2 files changed, 42 insertions(+), 4 deletions(-) (limited to 'tests') diff --git a/tests/plugins/PluginReadityourselfTest.php b/tests/plugins/PluginReadityourselfTest.php index d73e666a..532db146 100644 --- a/tests/plugins/PluginReadityourselfTest.php +++ b/tests/plugins/PluginReadityourselfTest.php @@ -4,8 +4,6 @@ * PluginReadityourselfTest.php.php */ -// FIXME! add an init method. -$conf = new ConfigManager(''); require_once 'plugins/readityourself/readityourself.php'; /** @@ -22,6 +20,27 @@ class PluginReadityourselfTest extends PHPUnit_Framework_TestCase PluginManager::$PLUGINS_PATH = 'plugins'; } + /** + * Test Readityourself init without errors. + */ + function testReadityourselfInitNoError() + { + $conf = new ConfigManager(''); + $conf->set('plugins.READITYOUSELF_URL', 'value'); + $errors = readityourself_init($conf); + $this->assertEmpty($errors); + } + + /** + * Test Readityourself init with errors. + */ + function testReadityourselfInitError() + { + $conf = new ConfigManager(''); + $errors = readityourself_init($conf); + $this->assertNotEmpty($errors); + } + /** * Test render_linklist hook. */ diff --git a/tests/plugins/PluginWallabagTest.php b/tests/plugins/PluginWallabagTest.php index 302ee296..2c268cbd 100644 --- a/tests/plugins/PluginWallabagTest.php +++ b/tests/plugins/PluginWallabagTest.php @@ -4,8 +4,6 @@ * PluginWallabagTest.php.php */ -// FIXME! add an init method. -$conf = new ConfigManager(''); require_once 'plugins/wallabag/wallabag.php'; /** @@ -22,6 +20,27 @@ class PluginWallabagTest extends PHPUnit_Framework_TestCase PluginManager::$PLUGINS_PATH = 'plugins'; } + /** + * Test wallabag init without errors. + */ + function testWallabagInitNoError() + { + $conf = new ConfigManager(''); + $conf->set('plugins.WALLABAG_URL', 'value'); + $errors = wallabag_init($conf); + $this->assertEmpty($errors); + } + + /** + * Test wallabag init with errors. + */ + function testWallabagInitError() + { + $conf = new ConfigManager(''); + $errors = wallabag_init($conf); + $this->assertNotEmpty($errors); + } + /** * Test render_linklist hook. */ -- cgit v1.2.3 From 44a718090d716e53c0e78bf3a0225fd2fb30071e Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Sun, 16 Oct 2016 20:52:35 +0200 Subject: Fix: return the proper value for the "self" feed attribute Fixes https://github.com/shaarli/Shaarli/issues/629 Closes https://github.com/shaarli/Shaarli/pull/630 Note: you might need to empty the "pagecache" directory for the fix to be taken into account Signed-off-by: VirtualTam --- tests/FeedBuilderTest.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'tests') diff --git a/tests/FeedBuilderTest.php b/tests/FeedBuilderTest.php index c9ff397d..aa57f44e 100644 --- a/tests/FeedBuilderTest.php +++ b/tests/FeedBuilderTest.php @@ -217,4 +217,37 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); $this->assertEquals('http://pubsubhub.io', $data['pubsubhub_url']); } + + /** + * Test buildData when Shaarli is served from a subdirectory + */ + public function testBuildDataServerSubdir() + { + $serverInfo = array( + 'HTTPS' => 'Off', + 'SERVER_NAME' => 'host.tld', + 'SERVER_PORT' => '8080', + 'SCRIPT_NAME' => '/~user/shaarli/index.php', + 'REQUEST_URI' => '/~user/shaarli/index.php?do=feed', + ); + $feedBuilder = new FeedBuilder( + self::$linkDB, + FeedBuilder::$FEED_ATOM, + $serverInfo, + null, + false + ); + $feedBuilder->setLocale(self::$LOCALE); + $data = $feedBuilder->buildData(); + + $this->assertEquals( + 'http://host.tld:8080/~user/shaarli/index.php?do=feed', + $data['self_link'] + ); + + // Test first link (note link) + $link = array_shift($data['links']); + $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['guid']); + $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['url']); + } } -- cgit v1.2.3 From bf26e7ebcb5ee69523b93a5ec7b7a65b39acb916 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Mon, 3 Oct 2016 09:43:49 +0200 Subject: Isso comments plugin Use Isso client to let visitors comments on permalinks --- tests/plugins/PluginIssoTest.php | 136 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 tests/plugins/PluginIssoTest.php (limited to 'tests') diff --git a/tests/plugins/PluginIssoTest.php b/tests/plugins/PluginIssoTest.php new file mode 100644 index 00000000..1f545c7d --- /dev/null +++ b/tests/plugins/PluginIssoTest.php @@ -0,0 +1,136 @@ +set('plugins.ISSO_SERVER', 'value'); + $errors = isso_init($conf); + $this->assertEmpty($errors); + } + + /** + * Test Isso init with errors. + */ + function testWallabagInitError() + { + $conf = new ConfigManager(''); + $errors = isso_init($conf); + $this->assertNotEmpty($errors); + } + + /** + * Test render_linklist hook with valid settings to display the comment form. + */ + function testIssoDisplayed() + { + $conf = new ConfigManager(''); + $conf->set('plugins.ISSO_SERVER', 'value'); + + $str = 'http://randomstr.com/test'; + $data = array( + 'title' => $str, + 'links' => array( + array( + 'url' => $str, + 'linkdate' => 'abc', + ) + ) + ); + + $data = hook_isso_render_linklist($data, $conf); + + // data shouldn't be altered + $this->assertEquals($str, $data['title']); + $this->assertEquals($str, $data['links'][0]['url']); + + // plugin data + $this->assertEquals(1, count($data['plugin_end_zone'])); + $this->assertNotFalse(strpos($data['plugin_end_zone'][0], 'abc')); + $this->assertNotFalse(strpos($data['plugin_end_zone'][0], 'embed.min.js')); + } + + /** + * Test isso plugin when multiple links are displayed (shouldn't be displayed). + */ + function testIssoMultipleLinks() + { + $conf = new ConfigManager(''); + $conf->set('plugins.ISSO_SERVER', 'value'); + + $str = 'http://randomstr.com/test'; + $data = array( + 'title' => $str, + 'links' => array( + array( + 'url' => $str, + 'linkdate' => 'abc', + ), + array( + 'url' => $str . '2', + 'linkdate' => 'abc2', + ), + ) + ); + + $processed = hook_isso_render_linklist($data, $conf); + // data shouldn't be altered + $this->assertEquals($data, $processed); + } + + /** + * Test isso plugin when using search (shouldn't be displayed). + */ + function testIssoNotDisplayedWhenSearch() + { + $conf = new ConfigManager(''); + $conf->set('plugins.ISSO_SERVER', 'value'); + + $str = 'http://randomstr.com/test'; + $data = array( + 'title' => $str, + 'links' => array( + array( + 'url' => $str, + 'linkdate' => 'abc', + ) + ), + 'search_term' => $str + ); + + $processed = hook_isso_render_linklist($data, $conf); + + // data shouldn't be altered + $this->assertEquals($data, $processed); + } + + /** + * Test isso plugin without server configuration (shouldn't be displayed). + */ + function testIssoWithoutConf() + { + $data = 'abc'; + $conf = new ConfigManager(''); + $processed = hook_isso_render_linklist($data, $conf); + $this->assertEquals($data, $processed); + } +} -- cgit v1.2.3 From 7af9a41881ed0b9d44d18a0ce03a123a8441adf5 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 20 Oct 2016 11:31:52 +0200 Subject: Minor code cleanup: PHPDoc, spelling, unused variables, etc. --- tests/config/ConfigJsonTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/config/ConfigJsonTest.php b/tests/config/ConfigJsonTest.php index 99c88820..07f6ab49 100644 --- a/tests/config/ConfigJsonTest.php +++ b/tests/config/ConfigJsonTest.php @@ -41,7 +41,7 @@ class ConfigJsonTest extends PHPUnit_Framework_TestCase * Read a non existent config file -> empty array. * * @expectedException Exception - * @expectedExceptionMessage An error occured while parsing JSON file: error code #4 + * @expectedExceptionMessage An error occurred while parsing JSON file: error code #4 */ public function testReadInvalidJson() { -- cgit v1.2.3 From fbc28ff1c892460e90e87a1be6dd3e28a4cd58b8 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Wed, 19 Oct 2016 11:05:20 +0200 Subject: Fix hashtags links in Feeds Make the hashtag link absolute in feeds to work properly in RSS syndication tools. --- tests/FeedBuilderTest.php | 1 + 1 file changed, 1 insertion(+) (limited to 'tests') diff --git a/tests/FeedBuilderTest.php b/tests/FeedBuilderTest.php index aa57f44e..d7839402 100644 --- a/tests/FeedBuilderTest.php +++ b/tests/FeedBuilderTest.php @@ -249,5 +249,6 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase $link = array_shift($data['links']); $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['guid']); $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['url']); + $this->assertContains('http://host.tld:8080/~user/shaarli/?addtag=hashtag', $link['description']); } } -- cgit v1.2.3 From 628b97cbdf276785eb9ff4f7a124e81e67d2f76c Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Thu, 20 Oct 2016 21:10:56 +0200 Subject: LinkDB: do not prefix privates with an underscore Relates to #95 Signed-off-by: VirtualTam --- tests/LinkDBTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/LinkDBTest.php b/tests/LinkDBTest.php index 31306069..43652e72 100644 --- a/tests/LinkDBTest.php +++ b/tests/LinkDBTest.php @@ -117,7 +117,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase unlink(self::$testDatastore); $this->assertFileNotExists(self::$testDatastore); - $checkDB = self::getMethod('_checkDB'); + $checkDB = self::getMethod('checkDB'); $checkDB->invokeArgs($linkDB, array()); $this->assertFileExists(self::$testDatastore); @@ -134,7 +134,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase $datastoreSize = filesize(self::$testDatastore); $this->assertGreaterThan(0, $datastoreSize); - $checkDB = self::getMethod('_checkDB'); + $checkDB = self::getMethod('checkDB'); $checkDB->invokeArgs($linkDB, array()); // ensure the datastore is left unmodified -- cgit v1.2.3 From f21abf329234ae4d5a1d56c5a9dd0bc11f80bac8 Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Thu, 20 Oct 2016 21:19:51 +0200 Subject: LinkDB: update datastore method names Relates to https://github.com/shaarli/Shaarli/issues/95 Signed-off-by: VirtualTam --- tests/LinkDBTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'tests') diff --git a/tests/LinkDBTest.php b/tests/LinkDBTest.php index 43652e72..9d79386c 100644 --- a/tests/LinkDBTest.php +++ b/tests/LinkDBTest.php @@ -117,7 +117,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase unlink(self::$testDatastore); $this->assertFileNotExists(self::$testDatastore); - $checkDB = self::getMethod('checkDB'); + $checkDB = self::getMethod('check'); $checkDB->invokeArgs($linkDB, array()); $this->assertFileExists(self::$testDatastore); @@ -134,7 +134,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase $datastoreSize = filesize(self::$testDatastore); $this->assertGreaterThan(0, $datastoreSize); - $checkDB = self::getMethod('checkDB'); + $checkDB = self::getMethod('check'); $checkDB->invokeArgs($linkDB, array()); // ensure the datastore is left unmodified @@ -180,7 +180,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase /** * Save the links to the DB */ - public function testSaveDB() + public function testSave() { $testDB = new LinkDB(self::$testDatastore, true, false); $dbSize = sizeof($testDB); @@ -194,7 +194,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase 'tags'=>'unit test' ); $testDB[$link['linkdate']] = $link; - $testDB->savedb('tests'); + $testDB->save('tests'); $testDB = new LinkDB(self::$testDatastore, true, false); $this->assertEquals($dbSize + 1, sizeof($testDB)); -- cgit v1.2.3 From 735ed4a94e1da5874195ac47c00612043a193edf Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Thu, 20 Oct 2016 21:24:39 +0200 Subject: LinkDB: explicit method visibility Relates to https://github.com/shaarli/Shaarli/issues/95 Signed-off-by: VirtualTam --- tests/utils/ReferenceLinkDB.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/utils/ReferenceLinkDB.php b/tests/utils/ReferenceLinkDB.php index 937961c8..abca4656 100644 --- a/tests/utils/ReferenceLinkDB.php +++ b/tests/utils/ReferenceLinkDB.php @@ -13,7 +13,7 @@ class ReferenceLinkDB /** * Populates the test DB with reference data */ - function __construct() + public function __construct() { $this->addLink( 'Link title: @website', -- cgit v1.2.3 From c5941f316a49c94eff354b63e75b3add98ac4aea Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Fri, 21 Oct 2016 12:38:38 +0200 Subject: Fix an issue with links not being reversed in code blocks Fixes #672 + Markdown to HTML unit test --- tests/plugins/PluginMarkdownTest.php | 13 +++++++++++++ tests/plugins/resources/markdown.html | 24 ++++++++++++++++++++++++ tests/plugins/resources/markdown.md | 24 ++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 tests/plugins/resources/markdown.html create mode 100644 tests/plugins/resources/markdown.md (limited to 'tests') diff --git a/tests/plugins/PluginMarkdownTest.php b/tests/plugins/PluginMarkdownTest.php index 3593a556..12bdda24 100644 --- a/tests/plugins/PluginMarkdownTest.php +++ b/tests/plugins/PluginMarkdownTest.php @@ -151,4 +151,17 @@ class PluginMarkdownTest extends PHPUnit_Framework_TestCase $data = hook_markdown_render_daily($data); $this->assertEquals($str, $data['cols'][0][0]['formatedDescription']); } + + /** + * Test hashtag links processed with markdown. + */ + function testMarkdownHashtagLinks() + { + $md = file_get_contents('tests/plugins/resources/markdown.md'); + $md = format_description($md); + $html = file_get_contents('tests/plugins/resources/markdown.html'); + + $data = process_markdown($md); + $this->assertEquals($html, $data); + } } diff --git a/tests/plugins/resources/markdown.html b/tests/plugins/resources/markdown.html new file mode 100644 index 00000000..c0fbe7f4 --- /dev/null +++ b/tests/plugins/resources/markdown.html @@ -0,0 +1,24 @@ +

+
    +
  1. zero +
      +
    1. two
    2. +
    3. three
    4. +
    5. four
    6. +
    7. foo #foobar
    8. +
  2. +
+

#foobar foo lol #foo #bar

+

fsdfs http://link.tld #foobar http://link.tld

+
http://link.tld #foobar
+next #foo
+

Block:

+
lorem ipsum #foobar http://link.tld
+#foobar http://link.tld
\ No newline at end of file diff --git a/tests/plugins/resources/markdown.md b/tests/plugins/resources/markdown.md new file mode 100644 index 00000000..0b8be7c5 --- /dev/null +++ b/tests/plugins/resources/markdown.md @@ -0,0 +1,24 @@ +* test: + * [zero](http://link.tld) + + [two](http://link.tld) + - [three](http://link.tld) + +1. [zero](http://link.tld) + 2. [two](http://link.tld) + 3. [three](http://link.tld) + 4. [four](http://link.tld) + 5. foo #foobar + +#foobar foo `lol #foo` #bar + +fsdfs http://link.tld #foobar `http://link.tld` + + http://link.tld #foobar + next #foo + +Block: + +``` +lorem ipsum #foobar http://link.tld +#foobar http://link.tld +``` \ No newline at end of file -- cgit v1.2.3 From 5ebc1d504bc8a8f29f49a8a4fc1c421f78677b2a Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 3 Nov 2016 13:59:18 +0100 Subject: .htaccess files: support Apache 2.4+ syntax If `mod_version` is enabled, the previous syntax will apply for Apache <2.4. If not, the new syntax is used by default. Fixes #676 `mod_version` identifier is `version_module` across all Apache versions. See: * https://httpd.apache.org/docs/current/mod/mod_version.html * https://httpd.apache.org/docs/2.2/mod/mod_version.html * https://serverfault.com/questions/733910/how-do-i-load-mod-version-only-if-it-isnt-built-in-to-apache Note that version_module comes built-in with Debian (and derivatives) Apache2 packages, see https://wiki.debian.org/Apache/PackagingFor24 --- tests/.htaccess | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/.htaccess b/tests/.htaccess index b584d98c..f601c1ee 100644 --- a/tests/.htaccess +++ b/tests/.htaccess @@ -1,2 +1,13 @@ -Allow from none -Deny from all + + = 2.4> + Require all denied + + + Allow from none + Deny from all + + + + + Require all denied + -- cgit v1.2.3 From 266e3fe5c8961aaf089bad16b9e4c54de1aaff40 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sun, 13 Nov 2016 16:51:21 +0100 Subject: Markdown: fixes feed rendering with nomarkdown tag * make sure we match exactly `nomarkdown` tag * pass the whole link data to stripNoMarkdownTag() to: * strip the noMD tag in taglist (array) * strip the tag in tags (string) Fixes #689 tmp --- tests/plugins/PluginMarkdownTest.php | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'tests') diff --git a/tests/plugins/PluginMarkdownTest.php b/tests/plugins/PluginMarkdownTest.php index 12bdda24..17ef2280 100644 --- a/tests/plugins/PluginMarkdownTest.php +++ b/tests/plugins/PluginMarkdownTest.php @@ -8,8 +8,8 @@ require_once 'application/Utils.php'; require_once 'plugins/markdown/markdown.php'; /** - * Class PlugQrcodeTest - * Unit test for the QR-Code plugin + * Class PluginMarkdownTest + * Unit test for the Markdown plugin */ class PluginMarkdownTest extends PHPUnit_Framework_TestCase { @@ -130,8 +130,11 @@ class PluginMarkdownTest extends PHPUnit_Framework_TestCase )) ); - $data = hook_markdown_render_linklist($data); - $this->assertEquals($str, $data['links'][0]['description']); + $processed = hook_markdown_render_linklist($data); + $this->assertEquals($str, $processed['links'][0]['description']); + + $processed = hook_markdown_render_feed($data); + $this->assertEquals($str, $processed['links'][0]['description']); $data = array( // Columns data @@ -152,6 +155,24 @@ class PluginMarkdownTest extends PHPUnit_Framework_TestCase $this->assertEquals($str, $data['cols'][0][0]['formatedDescription']); } + /** + * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`). + */ + function testNoMarkdownNotExcactlyMatching() + { + $str = 'All _work_ and `no play` makes Jack a *dull* boy.'; + $data = array( + 'links' => array(array( + 'description' => $str, + 'tags' => '.' . NO_MD_TAG, + 'taglist' => array('.'. NO_MD_TAG), + )) + ); + + $data = hook_markdown_render_feed($data); + $this->assertContains('', $data['links'][0]['description']); + } + /** * Test hashtag links processed with markdown. */ -- cgit v1.2.3 From c3dfd8995921083ff7250c25d0b6ab1184b91aff Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Mon, 28 Nov 2016 16:17:25 +0100 Subject: Unit Test for the new ID system --- tests/FeedBuilderTest.php | 31 +++-- tests/LinkDBTest.php | 39 +++++- tests/LinkFilterTest.php | 6 +- tests/NetscapeBookmarkUtils/BookmarkExportTest.php | 6 +- tests/NetscapeBookmarkUtils/BookmarkImportTest.php | 146 ++++++++++++++------- tests/Updater/UpdaterTest.php | 98 ++++++++++++++ tests/plugins/PluginIssoTest.php | 14 +- tests/utils/ReferenceLinkDB.php | 61 +++++++-- 8 files changed, 306 insertions(+), 95 deletions(-) (limited to 'tests') diff --git a/tests/FeedBuilderTest.php b/tests/FeedBuilderTest.php index d7839402..ea1dde25 100644 --- a/tests/FeedBuilderTest.php +++ b/tests/FeedBuilderTest.php @@ -84,8 +84,9 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); // Test first link (note link) - $link = array_shift($data['links']); - $this->assertEquals('20150310_114651', $link['linkdate']); + $link = reset($data['links']); + $this->assertEquals(41, $link['id']); + $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114651'), $link['created']); $this->assertEquals('http://host.tld/?WDWyig', $link['guid']); $this->assertEquals('http://host.tld/?WDWyig', $link['url']); $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['pub_iso_date']); @@ -99,14 +100,14 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase $this->assertEquals('sTuff', $link['taglist'][0]); // Test URL with external link. - $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $data['links']['20150310_114633']['url']); + $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $data['links'][8]['url']); // Test multitags. - $this->assertEquals(5, count($data['links']['20141125_084734']['taglist'])); - $this->assertEquals('css', $data['links']['20141125_084734']['taglist'][0]); + $this->assertEquals(5, count($data['links'][6]['taglist'])); + $this->assertEquals('css', $data['links'][6]['taglist'][0]); // Test update date - $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links']['20150310_114633']['up_iso_date']); + $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']); } /** @@ -119,9 +120,9 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase $data = $feedBuilder->buildData(); $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']); - $link = array_shift($data['links']); + $link = reset($data['links']); $this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:\d{2}/', $link['pub_iso_date']); - $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links']['20150310_114633']['up_iso_date']); + $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']); } /** @@ -138,7 +139,8 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase $data = $feedBuilder->buildData(); $this->assertEquals(1, count($data['links'])); $link = array_shift($data['links']); - $this->assertEquals('20150310_114651', $link['linkdate']); + $this->assertEquals(41, $link['id']); + $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114651'), $link['created']); } /** @@ -154,7 +156,8 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase $data = $feedBuilder->buildData(); $this->assertEquals(1, count($data['links'])); $link = array_shift($data['links']); - $this->assertEquals('20150310_114651', $link['linkdate']); + $this->assertEquals(41, $link['id']); + $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114651'), $link['created']); } /** @@ -170,15 +173,17 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase $this->assertTrue($data['usepermalinks']); // First link is a permalink $link = array_shift($data['links']); - $this->assertEquals('20150310_114651', $link['linkdate']); + $this->assertEquals(41, $link['id']); + $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114651'), $link['created']); $this->assertEquals('http://host.tld/?WDWyig', $link['guid']); $this->assertEquals('http://host.tld/?WDWyig', $link['url']); $this->assertContains('Direct link', $link['description']); $this->assertContains('http://host.tld/?WDWyig', $link['description']); // Second link is a direct link $link = array_shift($data['links']); - $this->assertEquals('20150310_114633', $link['linkdate']); - $this->assertEquals('http://host.tld/?kLHmZg', $link['guid']); + $this->assertEquals(8, $link['id']); + $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114633'), $link['created']); + $this->assertEquals('http://host.tld/?RttfEw', $link['guid']); $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']); $this->assertContains('Direct link', $link['description']); $this->assertContains('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']); diff --git a/tests/LinkDBTest.php b/tests/LinkDBTest.php index 9d79386c..bedc680e 100644 --- a/tests/LinkDBTest.php +++ b/tests/LinkDBTest.php @@ -186,14 +186,15 @@ class LinkDBTest extends PHPUnit_Framework_TestCase $dbSize = sizeof($testDB); $link = array( + 'id' => 42, 'title'=>'an additional link', 'url'=>'http://dum.my', 'description'=>'One more', 'private'=>0, - 'linkdate'=>'20150518_190000', + 'created'=> DateTime::createFromFormat('Ymd_His', '20150518_190000'), 'tags'=>'unit test' ); - $testDB[$link['linkdate']] = $link; + $testDB[$link['id']] = $link; $testDB->save('tests'); $testDB = new LinkDB(self::$testDatastore, true, false); @@ -238,12 +239,12 @@ class LinkDBTest extends PHPUnit_Framework_TestCase public function testDays() { $this->assertEquals( - array('20121206', '20130614', '20150310'), + array('20100310', '20121206', '20130614', '20150310'), self::$publicLinkDB->days() ); $this->assertEquals( - array('20121206', '20130614', '20141125', '20150310'), + array('20100310', '20121206', '20130614', '20141125', '20150310'), self::$privateLinkDB->days() ); } @@ -290,10 +291,11 @@ class LinkDBTest extends PHPUnit_Framework_TestCase '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 (`sTuff`). + // They need to be grouped with the first case found - order by date DESC: `sTuff`. 'sTuff' => 2, - 'hashtag' => 2, + 'ut' => 1, ), self::$publicLinkDB->allTags() ); @@ -321,6 +323,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase 'tag2' => 1, 'tag3' => 1, 'tag4' => 1, + 'ut' => 1, ), self::$privateLinkDB->allTags() ); @@ -411,6 +414,11 @@ class LinkDBTest extends PHPUnit_Framework_TestCase 1, count(self::$publicLinkDB->filterHash($request)) ); + $request = smallHash('20150310_114633' . 8); + $this->assertEquals( + 1, + count(self::$publicLinkDB->filterHash($request)) + ); } /** @@ -433,4 +441,23 @@ class LinkDBTest extends PHPUnit_Framework_TestCase { self::$publicLinkDB->filterHash(''); } + + /** + * Test reorder with asc/desc parameter. + */ + public function testReorderLinksDesc() + { + self::$publicLinkDB->reorder('ASC'); + $linkIdToTest = 42; + foreach (self::$publicLinkDB as $key => $value) { + $this->assertEquals($linkIdToTest, $key); + break; + } + self::$publicLinkDB->reorder('DESC'); + $linkIdToTest = 41; + foreach (self::$publicLinkDB as $key => $value) { + $this->assertEquals($linkIdToTest, $key); + break; + } + } } diff --git a/tests/LinkFilterTest.php b/tests/LinkFilterTest.php index 7d45fc59..21d680a5 100644 --- a/tests/LinkFilterTest.php +++ b/tests/LinkFilterTest.php @@ -159,7 +159,7 @@ class LinkFilterTest extends PHPUnit_Framework_TestCase $this->assertEquals( 'MediaGoblin', - $links['20130614_184135']['title'] + $links[7]['title'] ); } @@ -286,7 +286,7 @@ class LinkFilterTest extends PHPUnit_Framework_TestCase ); $this->assertEquals( - 6, + 7, count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, '-revolution')) ); } @@ -346,7 +346,7 @@ class LinkFilterTest extends PHPUnit_Framework_TestCase ); $this->assertEquals( - 6, + 7, count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, '-free')) ); } diff --git a/tests/NetscapeBookmarkUtils/BookmarkExportTest.php b/tests/NetscapeBookmarkUtils/BookmarkExportTest.php index cc54ab9f..6a47bbb9 100644 --- a/tests/NetscapeBookmarkUtils/BookmarkExportTest.php +++ b/tests/NetscapeBookmarkUtils/BookmarkExportTest.php @@ -50,7 +50,7 @@ class BookmarkExportTest extends PHPUnit_Framework_TestCase $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'all', false, ''); $this->assertEquals(self::$refDb->countLinks(), sizeof($links)); foreach ($links as $link) { - $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); + $date = $link['created']; $this->assertEquals( $date->getTimestamp(), $link['timestamp'] @@ -70,7 +70,7 @@ class BookmarkExportTest extends PHPUnit_Framework_TestCase $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'private', false, ''); $this->assertEquals(self::$refDb->countPrivateLinks(), sizeof($links)); foreach ($links as $link) { - $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); + $date = $link['created']; $this->assertEquals( $date->getTimestamp(), $link['timestamp'] @@ -90,7 +90,7 @@ class BookmarkExportTest extends PHPUnit_Framework_TestCase $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'public', false, ''); $this->assertEquals(self::$refDb->countPublicLinks(), sizeof($links)); foreach ($links as $link) { - $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); + $date = $link['created']; $this->assertEquals( $date->getTimestamp(), $link['timestamp'] diff --git a/tests/NetscapeBookmarkUtils/BookmarkImportTest.php b/tests/NetscapeBookmarkUtils/BookmarkImportTest.php index f0ad500f..96895b00 100644 --- a/tests/NetscapeBookmarkUtils/BookmarkImportTest.php +++ b/tests/NetscapeBookmarkUtils/BookmarkImportTest.php @@ -42,6 +42,18 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase */ protected $pagecache = 'tests'; + /** + * @var string Save the current timezone. + */ + protected static $defaultTimeZone; + + public static function setUpBeforeClass() + { + self::$defaultTimeZone = date_default_timezone_get(); + // Timezone without DST for test consistency + date_default_timezone_set('Africa/Nairobi'); + } + /** * Resets test data before each test */ @@ -55,6 +67,11 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->linkDb = new LinkDB(self::$testDatastore, true, false); } + public static function tearDownAfterClass() + { + date_default_timezone_set(self::$defaultTimeZone); + } + /** * Attempt to import bookmarks from an empty file */ @@ -98,18 +115,19 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals( array( - 'linkdate' => '20160618_173944', + 'id' => 0, + 'created' => DateTime::createFromFormat('Ymd_His', '20160618_203944'), 'title' => 'Hg Init a Mercurial tutorial by Joel Spolsky', 'url' => 'http://hginit.com/', 'description' => '', 'private' => 0, - 'tags' => '' + 'tags' => '', + 'shorturl' => 'La37cg', ), $this->linkDb->getLinkFromUrl('http://hginit.com/') ); } - /** * Import bookmarks nested in a folder hierarchy */ @@ -126,89 +144,105 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals( array( - 'linkdate' => '20160225_205541', + 'id' => 0, + 'created' => DateTime::createFromFormat('Ymd_His', '20160225_235541'), 'title' => 'Nested 1', 'url' => 'http://nest.ed/1', 'description' => '', 'private' => 0, - 'tags' => 'tag1 tag2' + 'tags' => 'tag1 tag2', + 'shorturl' => 'KyDNKA', ), $this->linkDb->getLinkFromUrl('http://nest.ed/1') ); $this->assertEquals( array( - 'linkdate' => '20160225_205542', + 'id' => 1, + 'created' => DateTime::createFromFormat('Ymd_His', '20160225_235542'), 'title' => 'Nested 1-1', 'url' => 'http://nest.ed/1-1', 'description' => '', 'private' => 0, - 'tags' => 'folder1 tag1 tag2' + 'tags' => 'folder1 tag1 tag2', + 'shorturl' => 'T2LnXg', ), $this->linkDb->getLinkFromUrl('http://nest.ed/1-1') ); $this->assertEquals( array( - 'linkdate' => '20160225_205547', + 'id' => 2, + 'created' => DateTime::createFromFormat('Ymd_His', '20160225_235547'), 'title' => 'Nested 1-2', 'url' => 'http://nest.ed/1-2', 'description' => '', 'private' => 0, - 'tags' => 'folder1 tag3 tag4' + 'tags' => 'folder1 tag3 tag4', + 'shorturl' => '46SZxA', ), $this->linkDb->getLinkFromUrl('http://nest.ed/1-2') ); $this->assertEquals( array( - 'linkdate' => '20160202_172222', + 'id' => 3, + 'created' => DateTime::createFromFormat('Ymd_His', '20160202_202222'), 'title' => 'Nested 2-1', 'url' => 'http://nest.ed/2-1', 'description' => 'First link of the second section', 'private' => 1, - 'tags' => 'folder2' + 'tags' => 'folder2', + 'shorturl' => '4UHOSw', ), $this->linkDb->getLinkFromUrl('http://nest.ed/2-1') ); $this->assertEquals( array( - 'linkdate' => '20160119_200227', + 'id' => 4, + 'created' => DateTime::createFromFormat('Ymd_His', '20160119_230227'), 'title' => 'Nested 2-2', 'url' => 'http://nest.ed/2-2', 'description' => 'Second link of the second section', 'private' => 1, - 'tags' => 'folder2' + 'tags' => 'folder2', + 'shorturl' => 'yfzwbw', ), $this->linkDb->getLinkFromUrl('http://nest.ed/2-2') ); $this->assertEquals( array( - 'linkdate' => '20160202_172223', + 'id' => 5, + 'created' => DateTime::createFromFormat('Ymd_His', '20160202_202222'), 'title' => 'Nested 3-1', 'url' => 'http://nest.ed/3-1', 'description' => '', 'private' => 0, - 'tags' => 'folder3 folder3-1 tag3' + 'tags' => 'folder3 folder3-1 tag3', + 'shorturl' => 'UwxIUQ', ), $this->linkDb->getLinkFromUrl('http://nest.ed/3-1') ); $this->assertEquals( array( - 'linkdate' => '20160119_200228', + 'id' => 6, + 'created' => DateTime::createFromFormat('Ymd_His', '20160119_230227'), 'title' => 'Nested 3-2', 'url' => 'http://nest.ed/3-2', 'description' => '', 'private' => 0, - 'tags' => 'folder3 folder3-1' + 'tags' => 'folder3 folder3-1', + 'shorturl' => 'p8dyZg', ), $this->linkDb->getLinkFromUrl('http://nest.ed/3-2') ); $this->assertEquals( array( - 'linkdate' => '20160229_081541', + 'id' => 7, + 'created' => DateTime::createFromFormat('Ymd_His', '20160229_111541'), 'title' => 'Nested 2', 'url' => 'http://nest.ed/2', 'description' => '', 'private' => 0, - 'tags' => 'tag4' + 'tags' => 'tag4', + 'shorturl' => 'Gt3Uug', ), $this->linkDb->getLinkFromUrl('http://nest.ed/2') ); @@ -227,28 +261,34 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase .' 2 links imported, 0 links overwritten, 0 links skipped.', NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->pagecache) ); + $this->assertEquals(2, count($this->linkDb)); $this->assertEquals(1, count_private($this->linkDb)); $this->assertEquals( array( - 'linkdate' => '20001010_105536', + 'id' => 0, + // Old link - UTC+4 (note that TZ in the import file is ignored). + 'created' => DateTime::createFromFormat('Ymd_His', '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' + 'tags' => 'private secret', + 'shorturl' => 'EokDtA', ), $this->linkDb->getLinkFromUrl('https://private.tld') ); $this->assertEquals( array( - 'linkdate' => '20160225_205548', + 'id' => 1, + 'created' => DateTime::createFromFormat('Ymd_His', '20160225_235548'), 'title' => 'Public stuff', 'url' => 'http://public.tld', 'description' => '', 'private' => 0, - 'tags' => 'public hello world' + 'tags' => 'public hello world', + 'shorturl' => 'Er9ddA', ), $this->linkDb->getLinkFromUrl('http://public.tld') ); @@ -271,23 +311,28 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals( array( - 'linkdate' => '20001010_105536', + 'id' => 0, + // Note that TZ in the import file is ignored. + 'created' => DateTime::createFromFormat('Ymd_His', '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' + 'tags' => 'private secret', + 'shorturl' => 'EokDtA', ), $this->linkDb->getLinkFromUrl('https://private.tld') ); $this->assertEquals( array( - 'linkdate' => '20160225_205548', + 'id' => 1, + 'created' => DateTime::createFromFormat('Ymd_His', '20160225_235548'), 'title' => 'Public stuff', 'url' => 'http://public.tld', 'description' => '', 'private' => 0, - 'tags' => 'public hello world' + 'tags' => 'public hello world', + 'shorturl' => 'Er9ddA', ), $this->linkDb->getLinkFromUrl('http://public.tld') ); @@ -309,11 +354,11 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals(0, count_private($this->linkDb)); $this->assertEquals( 0, - $this->linkDb['20001010_105536']['private'] + $this->linkDb[0]['private'] ); $this->assertEquals( 0, - $this->linkDb['20160225_205548']['private'] + $this->linkDb[1]['private'] ); } @@ -333,11 +378,11 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals(2, count_private($this->linkDb)); $this->assertEquals( 1, - $this->linkDb['20001010_105536']['private'] + $this->linkDb['0']['private'] ); $this->assertEquals( 1, - $this->linkDb['20160225_205548']['private'] + $this->linkDb['1']['private'] ); } @@ -359,13 +404,12 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals(2, count_private($this->linkDb)); $this->assertEquals( 1, - $this->linkDb['20001010_105536']['private'] + $this->linkDb[0]['private'] ); $this->assertEquals( 1, - $this->linkDb['20160225_205548']['private'] + $this->linkDb[1]['private'] ); - // re-import as public, enable overwriting $post = array( 'privacy' => 'public', @@ -380,11 +424,11 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals(0, count_private($this->linkDb)); $this->assertEquals( 0, - $this->linkDb['20001010_105536']['private'] + $this->linkDb[0]['private'] ); $this->assertEquals( 0, - $this->linkDb['20160225_205548']['private'] + $this->linkDb[1]['private'] ); } @@ -406,11 +450,11 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals(0, count_private($this->linkDb)); $this->assertEquals( 0, - $this->linkDb['20001010_105536']['private'] + $this->linkDb['0']['private'] ); $this->assertEquals( 0, - $this->linkDb['20160225_205548']['private'] + $this->linkDb['1']['private'] ); // re-import as private, enable overwriting @@ -427,11 +471,11 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals(2, count_private($this->linkDb)); $this->assertEquals( 1, - $this->linkDb['20001010_105536']['private'] + $this->linkDb['0']['private'] ); $this->assertEquals( 1, - $this->linkDb['20160225_205548']['private'] + $this->linkDb['1']['private'] ); } @@ -480,11 +524,11 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals(0, count_private($this->linkDb)); $this->assertEquals( 'tag1 tag2 tag3 private secret', - $this->linkDb['20001010_105536']['tags'] + $this->linkDb['0']['tags'] ); $this->assertEquals( 'tag1 tag2 tag3 public hello world', - $this->linkDb['20160225_205548']['tags'] + $this->linkDb['1']['tags'] ); } @@ -507,16 +551,16 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals(0, count_private($this->linkDb)); $this->assertEquals( 'tag1& tag2 "tag3" private secret', - $this->linkDb['20001010_105536']['tags'] + $this->linkDb['0']['tags'] ); $this->assertEquals( 'tag1& tag2 "tag3" public hello world', - $this->linkDb['20160225_205548']['tags'] + $this->linkDb['1']['tags'] ); } /** - * Ensure each imported bookmark has a unique linkdate + * Ensure each imported bookmark has a unique id * * See https://github.com/shaarli/Shaarli/issues/351 */ @@ -531,16 +575,16 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals(3, count($this->linkDb)); $this->assertEquals(0, count_private($this->linkDb)); $this->assertEquals( - '20160225_205548', - $this->linkDb['20160225_205548']['linkdate'] + 0, + $this->linkDb[0]['id'] ); $this->assertEquals( - '20160225_205549', - $this->linkDb['20160225_205549']['linkdate'] + 1, + $this->linkDb[1]['id'] ); $this->assertEquals( - '20160225_205550', - $this->linkDb['20160225_205550']['linkdate'] + 2, + $this->linkDb[2]['id'] ); } } diff --git a/tests/Updater/UpdaterTest.php b/tests/Updater/UpdaterTest.php index 0d0ad922..b8a050b0 100644 --- a/tests/Updater/UpdaterTest.php +++ b/tests/Updater/UpdaterTest.php @@ -214,6 +214,7 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; $refDB = new ReferenceLinkDB(); $refDB->write(self::$testDatastore); $linkDB = new LinkDB(self::$testDatastore, true, false); + $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude'))); $updater = new Updater(array(), $linkDB, $this->conf, true); $updater->updateMethodRenameDashTags(); @@ -287,4 +288,101 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; $this->assertEquals(escape($redirectorUrl), $this->conf->get('redirector.url')); unlink($sandbox .'.json.php'); } + + /** + * Test updateMethodDatastoreIds(). + */ + public function testDatastoreIds() + { + $links = array( + '20121206_182539' => array( + 'linkdate' => '20121206_182539', + 'title' => 'Geek and Poke', + 'url' => 'http://geek-and-poke.com/', + 'description' => 'desc', + 'tags' => 'dev cartoon tag1 tag2 tag3 tag4 ', + 'updated' => '20121206_190301', + 'private' => false, + ), + '20121206_172539' => array( + 'linkdate' => '20121206_172539', + 'title' => 'UserFriendly - Samba', + 'url' => 'http://ars.userfriendly.org/cartoons/?id=20010306', + 'description' => '', + 'tags' => 'samba cartoon web', + 'private' => false, + ), + '20121206_142300' => array( + 'linkdate' => '20121206_142300', + 'title' => 'UserFriendly - Web Designer', + 'url' => 'http://ars.userfriendly.org/cartoons/?id=20121206', + 'description' => 'Naming conventions... #private', + 'tags' => 'samba cartoon web', + 'private' => true, + ), + ); + $refDB = new ReferenceLinkDB(); + $refDB->setLinks($links); + $refDB->write(self::$testDatastore); + $linkDB = new LinkDB(self::$testDatastore, true, false); + + $checksum = hash_file('sha1', self::$testDatastore); + + $this->conf->set('resource.data_dir', 'sandbox'); + $this->conf->set('resource.datastore', self::$testDatastore); + + $updater = new Updater(array(), $linkDB, $this->conf, true); + $this->assertTrue($updater->updateMethodDatastoreIds()); + + $linkDB = new LinkDB(self::$testDatastore, true, false); + + $backup = glob($this->conf->get('resource.data_dir') . '/datastore.'. date('YmdH') .'*.php'); + $backup = $backup[0]; + + $this->assertFileExists($backup); + $this->assertEquals($checksum, hash_file('sha1', $backup)); + unlink($backup); + + $this->assertEquals(3, count($linkDB)); + $this->assertTrue(isset($linkDB[0])); + $this->assertFalse(isset($linkDB[0]['linkdate'])); + $this->assertEquals(0, $linkDB[0]['id']); + $this->assertEquals('UserFriendly - Web Designer', $linkDB[0]['title']); + $this->assertEquals('http://ars.userfriendly.org/cartoons/?id=20121206', $linkDB[0]['url']); + $this->assertEquals('Naming conventions... #private', $linkDB[0]['description']); + $this->assertEquals('samba cartoon web', $linkDB[0]['tags']); + $this->assertTrue($linkDB[0]['private']); + $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_142300'), $linkDB[0]['created']); + + $this->assertTrue(isset($linkDB[1])); + $this->assertFalse(isset($linkDB[1]['linkdate'])); + $this->assertEquals(1, $linkDB[1]['id']); + $this->assertEquals('UserFriendly - Samba', $linkDB[1]['title']); + $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_172539'), $linkDB[1]['created']); + + $this->assertTrue(isset($linkDB[2])); + $this->assertFalse(isset($linkDB[2]['linkdate'])); + $this->assertEquals(2, $linkDB[2]['id']); + $this->assertEquals('Geek and Poke', $linkDB[2]['title']); + $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_182539'), $linkDB[2]['created']); + $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_190301'), $linkDB[2]['updated']); + } + + /** + * Test updateMethodDatastoreIds() with the update already applied: nothing to do. + */ + public function testDatastoreIdsNothingToDo() + { + $refDB = new ReferenceLinkDB(); + $refDB->write(self::$testDatastore); + $linkDB = new LinkDB(self::$testDatastore, true, false); + + $this->conf->set('resource.data_dir', 'sandbox'); + $this->conf->set('resource.datastore', self::$testDatastore); + + $checksum = hash_file('sha1', self::$testDatastore); + $updater = new Updater(array(), $linkDB, $this->conf, true); + $this->assertTrue($updater->updateMethodDatastoreIds()); + $this->assertEquals($checksum, hash_file('sha1', self::$testDatastore)); + } } diff --git a/tests/plugins/PluginIssoTest.php b/tests/plugins/PluginIssoTest.php index 1f545c7d..ea86a05c 100644 --- a/tests/plugins/PluginIssoTest.php +++ b/tests/plugins/PluginIssoTest.php @@ -47,12 +47,13 @@ class PluginIssoTest extends PHPUnit_Framework_TestCase $conf->set('plugins.ISSO_SERVER', 'value'); $str = 'http://randomstr.com/test'; + $date = '20161118_100001'; $data = array( 'title' => $str, 'links' => array( array( 'url' => $str, - 'linkdate' => 'abc', + 'created' => DateTime::createFromFormat('Ymd_His', $date), ) ) ); @@ -65,7 +66,7 @@ class PluginIssoTest extends PHPUnit_Framework_TestCase // plugin data $this->assertEquals(1, count($data['plugin_end_zone'])); - $this->assertNotFalse(strpos($data['plugin_end_zone'][0], 'abc')); + $this->assertNotFalse(strpos($data['plugin_end_zone'][0], $date)); $this->assertNotFalse(strpos($data['plugin_end_zone'][0], 'embed.min.js')); } @@ -78,16 +79,18 @@ class PluginIssoTest extends PHPUnit_Framework_TestCase $conf->set('plugins.ISSO_SERVER', 'value'); $str = 'http://randomstr.com/test'; + $date1 = '20161118_100001'; + $date2 = '20161118_100002'; $data = array( 'title' => $str, 'links' => array( array( 'url' => $str, - 'linkdate' => 'abc', + 'created' => DateTime::createFromFormat('Ymd_His', $date1), ), array( 'url' => $str . '2', - 'linkdate' => 'abc2', + 'created' => DateTime::createFromFormat('Ymd_His', $date2), ), ) ); @@ -106,12 +109,13 @@ class PluginIssoTest extends PHPUnit_Framework_TestCase $conf->set('plugins.ISSO_SERVER', 'value'); $str = 'http://randomstr.com/test'; + $date = '20161118_100001'; $data = array( 'title' => $str, 'links' => array( array( 'url' => $str, - 'linkdate' => 'abc', + 'created' => DateTime::createFromFormat('Ymd_His', $date), ) ), 'search_term' => $str diff --git a/tests/utils/ReferenceLinkDB.php b/tests/utils/ReferenceLinkDB.php index abca4656..6b16c9e8 100644 --- a/tests/utils/ReferenceLinkDB.php +++ b/tests/utils/ReferenceLinkDB.php @@ -4,7 +4,7 @@ */ class ReferenceLinkDB { - public static $NB_LINKS_TOTAL = 7; + public static $NB_LINKS_TOTAL = 8; private $_links = array(); private $_publicCount = 0; @@ -16,66 +16,87 @@ class ReferenceLinkDB public function __construct() { $this->addLink( + 41, 'Link title: @website', '?WDWyig', 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag', 0, - '20150310_114651', - 'sTuff' + DateTime::createFromFormat('Ymd_His', '20150310_114651'), + 'sTuff', + null, + 'WDWyig' ); $this->addLink( + 42, + 'Note: I have a big ID but an old date', + '?WDWyig', + 'Used to test links reordering.', + 0, + DateTime::createFromFormat('Ymd_His', '20100310_101010'), + 'ut' + ); + + $this->addLink( + 8, 'Free as in Freedom 2.0 @website', 'https://static.fsf.org/nosvn/faif-2.0.pdf', 'Richard Stallman and the Free Software Revolution. Read this. #hashtag', 0, - '20150310_114633', + DateTime::createFromFormat('Ymd_His', '20150310_114633'), 'free gnu software stallman -exclude stuff hashtag', - '20160803_093033' + DateTime::createFromFormat('Ymd_His', '20160803_093033') ); $this->addLink( + 7, 'MediaGoblin', 'http://mediagoblin.org/', 'A free software media publishing platform #hashtagOther', 0, - '20130614_184135', - 'gnu media web .hidden hashtag' + DateTime::createFromFormat('Ymd_His', '20130614_184135'), + 'gnu media web .hidden hashtag', + null, + 'IuWvgA' ); $this->addLink( + 6, 'w3c-markup-validator', 'https://dvcs.w3.org/hg/markup-validator/summary', 'Mercurial repository for the W3C Validator #private', 1, - '20141125_084734', + DateTime::createFromFormat('Ymd_His', '20141125_084734'), 'css html w3c web Mercurial' ); $this->addLink( + 4, 'UserFriendly - Web Designer', 'http://ars.userfriendly.org/cartoons/?id=20121206', 'Naming conventions... #private', 0, - '20121206_142300', + DateTime::createFromFormat('Ymd_His', '20121206_142300'), 'dev cartoon web' ); $this->addLink( + 1, 'UserFriendly - Samba', 'http://ars.userfriendly.org/cartoons/?id=20010306', 'Tropical printing', 0, - '20121206_172539', + DateTime::createFromFormat('Ymd_His', '20121206_172539'), 'samba cartoon web' ); $this->addLink( + 0, 'Geek and Poke', 'http://geek-and-poke.com/', '', 1, - '20121206_182539', + DateTime::createFromFormat('Ymd_His', '20121206_182539'), 'dev cartoon tag1 tag2 tag3 tag4 ' ); } @@ -83,18 +104,20 @@ class ReferenceLinkDB /** * Adds a new link */ - protected function addLink($title, $url, $description, $private, $date, $tags, $updated = '') + protected function addLink($id, $title, $url, $description, $private, $date, $tags, $updated = '', $shorturl = '') { $link = array( + 'id' => $id, 'title' => $title, 'url' => $url, 'description' => $description, 'private' => $private, - 'linkdate' => $date, 'tags' => $tags, + 'created' => $date, 'updated' => $updated, + 'shorturl' => $shorturl ? $shorturl : smallHash($date->format('Ymd_His') . $id), ); - $this->_links[$date] = $link; + $this->_links[$id] = $link; if ($private) { $this->_privateCount++; @@ -142,4 +165,14 @@ class ReferenceLinkDB { return $this->_links; } + + /** + * Setter to override link creation. + * + * @param array $links List of links. + */ + public function setLinks($links) + { + $this->_links = $links; + } } -- cgit v1.2.3 From d592daea8343bb4dfecff5d97e93699581ccc58c Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Mon, 28 Nov 2016 18:24:15 +0100 Subject: Add a persistent 'shorturl' key to all links All existing link will keep their permalinks. New links will have smallhash generated with date+id. The purpose of this is to avoid collision between links due to their creation date. --- tests/FeedBuilderTest.php | 10 ++++----- tests/LinkDBTest.php | 22 +++++++++--------- tests/NetscapeBookmarkUtils/BookmarkImportTest.php | 26 +++++++++++----------- tests/Updater/UpdaterTest.php | 8 +++---- tests/plugins/PluginIssoTest.php | 21 ++++++++++++----- tests/utils/ReferenceLinkDB.php | 20 ++++++++--------- 6 files changed, 59 insertions(+), 48 deletions(-) (limited to 'tests') diff --git a/tests/FeedBuilderTest.php b/tests/FeedBuilderTest.php index ea1dde25..06a44506 100644 --- a/tests/FeedBuilderTest.php +++ b/tests/FeedBuilderTest.php @@ -86,7 +86,7 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase // Test first link (note link) $link = reset($data['links']); $this->assertEquals(41, $link['id']); - $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114651'), $link['created']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); $this->assertEquals('http://host.tld/?WDWyig', $link['guid']); $this->assertEquals('http://host.tld/?WDWyig', $link['url']); $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['pub_iso_date']); @@ -140,7 +140,7 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase $this->assertEquals(1, count($data['links'])); $link = array_shift($data['links']); $this->assertEquals(41, $link['id']); - $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114651'), $link['created']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); } /** @@ -157,7 +157,7 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase $this->assertEquals(1, count($data['links'])); $link = array_shift($data['links']); $this->assertEquals(41, $link['id']); - $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114651'), $link['created']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); } /** @@ -174,7 +174,7 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase // First link is a permalink $link = array_shift($data['links']); $this->assertEquals(41, $link['id']); - $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114651'), $link['created']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); $this->assertEquals('http://host.tld/?WDWyig', $link['guid']); $this->assertEquals('http://host.tld/?WDWyig', $link['url']); $this->assertContains('Direct link', $link['description']); @@ -182,7 +182,7 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase // Second link is a direct link $link = array_shift($data['links']); $this->assertEquals(8, $link['id']); - $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114633'), $link['created']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114633'), $link['created']); $this->assertEquals('http://host.tld/?RttfEw', $link['guid']); $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']); $this->assertContains('Direct link', $link['description']); diff --git a/tests/LinkDBTest.php b/tests/LinkDBTest.php index bedc680e..1f62a34a 100644 --- a/tests/LinkDBTest.php +++ b/tests/LinkDBTest.php @@ -191,7 +191,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase 'url'=>'http://dum.my', 'description'=>'One more', 'private'=>0, - 'created'=> DateTime::createFromFormat('Ymd_His', '20150518_190000'), + 'created'=> DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150518_190000'), 'tags'=>'unit test' ); $testDB[$link['id']] = $link; @@ -447,17 +447,17 @@ class LinkDBTest extends PHPUnit_Framework_TestCase */ public function testReorderLinksDesc() { - self::$publicLinkDB->reorder('ASC'); - $linkIdToTest = 42; - foreach (self::$publicLinkDB as $key => $value) { - $this->assertEquals($linkIdToTest, $key); - break; + self::$privateLinkDB->reorder('ASC'); + $linkIds = array(42, 4, 1, 0, 7, 6, 8, 41); + $cpt = 0; + foreach (self::$privateLinkDB as $key => $value) { + $this->assertEquals($linkIds[$cpt++], $key); } - self::$publicLinkDB->reorder('DESC'); - $linkIdToTest = 41; - foreach (self::$publicLinkDB as $key => $value) { - $this->assertEquals($linkIdToTest, $key); - break; + self::$privateLinkDB->reorder('DESC'); + $linkIds = array_reverse($linkIds); + $cpt = 0; + foreach (self::$privateLinkDB as $key => $value) { + $this->assertEquals($linkIds[$cpt++], $key); } } } diff --git a/tests/NetscapeBookmarkUtils/BookmarkImportTest.php b/tests/NetscapeBookmarkUtils/BookmarkImportTest.php index 96895b00..0ca07eac 100644 --- a/tests/NetscapeBookmarkUtils/BookmarkImportTest.php +++ b/tests/NetscapeBookmarkUtils/BookmarkImportTest.php @@ -116,7 +116,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals( array( 'id' => 0, - 'created' => DateTime::createFromFormat('Ymd_His', '20160618_203944'), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160618_203944'), 'title' => 'Hg Init a Mercurial tutorial by Joel Spolsky', 'url' => 'http://hginit.com/', 'description' => '', @@ -145,7 +145,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals( array( 'id' => 0, - 'created' => DateTime::createFromFormat('Ymd_His', '20160225_235541'), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235541'), 'title' => 'Nested 1', 'url' => 'http://nest.ed/1', 'description' => '', @@ -158,7 +158,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals( array( 'id' => 1, - 'created' => DateTime::createFromFormat('Ymd_His', '20160225_235542'), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235542'), 'title' => 'Nested 1-1', 'url' => 'http://nest.ed/1-1', 'description' => '', @@ -171,7 +171,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals( array( 'id' => 2, - 'created' => DateTime::createFromFormat('Ymd_His', '20160225_235547'), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235547'), 'title' => 'Nested 1-2', 'url' => 'http://nest.ed/1-2', 'description' => '', @@ -184,7 +184,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals( array( 'id' => 3, - 'created' => DateTime::createFromFormat('Ymd_His', '20160202_202222'), + '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', @@ -197,7 +197,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals( array( 'id' => 4, - 'created' => DateTime::createFromFormat('Ymd_His', '20160119_230227'), + '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', @@ -210,7 +210,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals( array( 'id' => 5, - 'created' => DateTime::createFromFormat('Ymd_His', '20160202_202222'), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160202_202222'), 'title' => 'Nested 3-1', 'url' => 'http://nest.ed/3-1', 'description' => '', @@ -223,7 +223,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals( array( 'id' => 6, - 'created' => DateTime::createFromFormat('Ymd_His', '20160119_230227'), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160119_230227'), 'title' => 'Nested 3-2', 'url' => 'http://nest.ed/3-2', 'description' => '', @@ -236,7 +236,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals( array( 'id' => 7, - 'created' => DateTime::createFromFormat('Ymd_His', '20160229_111541'), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160229_111541'), 'title' => 'Nested 2', 'url' => 'http://nest.ed/2', 'description' => '', @@ -269,7 +269,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase array( 'id' => 0, // Old link - UTC+4 (note that TZ in the import file is ignored). - 'created' => DateTime::createFromFormat('Ymd_His', '20001010_135536'), + '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", @@ -282,7 +282,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals( array( 'id' => 1, - 'created' => DateTime::createFromFormat('Ymd_His', '20160225_235548'), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235548'), 'title' => 'Public stuff', 'url' => 'http://public.tld', 'description' => '', @@ -313,7 +313,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase array( 'id' => 0, // Note that TZ in the import file is ignored. - 'created' => DateTime::createFromFormat('Ymd_His', '20001010_135536'), + '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", @@ -326,7 +326,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase $this->assertEquals( array( 'id' => 1, - 'created' => DateTime::createFromFormat('Ymd_His', '20160225_235548'), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235548'), 'title' => 'Public stuff', 'url' => 'http://public.tld', 'description' => '', diff --git a/tests/Updater/UpdaterTest.php b/tests/Updater/UpdaterTest.php index b8a050b0..4948fe52 100644 --- a/tests/Updater/UpdaterTest.php +++ b/tests/Updater/UpdaterTest.php @@ -352,20 +352,20 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; $this->assertEquals('Naming conventions... #private', $linkDB[0]['description']); $this->assertEquals('samba cartoon web', $linkDB[0]['tags']); $this->assertTrue($linkDB[0]['private']); - $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_142300'), $linkDB[0]['created']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_142300'), $linkDB[0]['created']); $this->assertTrue(isset($linkDB[1])); $this->assertFalse(isset($linkDB[1]['linkdate'])); $this->assertEquals(1, $linkDB[1]['id']); $this->assertEquals('UserFriendly - Samba', $linkDB[1]['title']); - $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_172539'), $linkDB[1]['created']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_172539'), $linkDB[1]['created']); $this->assertTrue(isset($linkDB[2])); $this->assertFalse(isset($linkDB[2]['linkdate'])); $this->assertEquals(2, $linkDB[2]['id']); $this->assertEquals('Geek and Poke', $linkDB[2]['title']); - $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_182539'), $linkDB[2]['created']); - $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_190301'), $linkDB[2]['updated']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_182539'), $linkDB[2]['created']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_190301'), $linkDB[2]['updated']); } /** diff --git a/tests/plugins/PluginIssoTest.php b/tests/plugins/PluginIssoTest.php index ea86a05c..6b7904dd 100644 --- a/tests/plugins/PluginIssoTest.php +++ b/tests/plugins/PluginIssoTest.php @@ -52,8 +52,9 @@ class PluginIssoTest extends PHPUnit_Framework_TestCase 'title' => $str, 'links' => array( array( + 'id' => 12, 'url' => $str, - 'created' => DateTime::createFromFormat('Ymd_His', $date), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $date), ) ) ); @@ -66,7 +67,14 @@ class PluginIssoTest extends PHPUnit_Framework_TestCase // plugin data $this->assertEquals(1, count($data['plugin_end_zone'])); - $this->assertNotFalse(strpos($data['plugin_end_zone'][0], $date)); + $this->assertNotFalse(strpos( + $data['plugin_end_zone'][0], + 'data-isso-id="'. $data['links'][0]['id'] .'"' + )); + $this->assertNotFalse(strpos( + $data['plugin_end_zone'][0], + 'data-title="'. $data['links'][0]['id'] .'"' + )); $this->assertNotFalse(strpos($data['plugin_end_zone'][0], 'embed.min.js')); } @@ -85,12 +93,14 @@ class PluginIssoTest extends PHPUnit_Framework_TestCase 'title' => $str, 'links' => array( array( + 'id' => 12, 'url' => $str, - 'created' => DateTime::createFromFormat('Ymd_His', $date1), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $date1), ), array( + 'id' => 13, 'url' => $str . '2', - 'created' => DateTime::createFromFormat('Ymd_His', $date2), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $date2), ), ) ); @@ -114,8 +124,9 @@ class PluginIssoTest extends PHPUnit_Framework_TestCase 'title' => $str, 'links' => array( array( + 'id' => 12, 'url' => $str, - 'created' => DateTime::createFromFormat('Ymd_His', $date), + 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $date), ) ), 'search_term' => $str diff --git a/tests/utils/ReferenceLinkDB.php b/tests/utils/ReferenceLinkDB.php index 6b16c9e8..36d58c68 100644 --- a/tests/utils/ReferenceLinkDB.php +++ b/tests/utils/ReferenceLinkDB.php @@ -21,7 +21,7 @@ class ReferenceLinkDB '?WDWyig', 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag', 0, - DateTime::createFromFormat('Ymd_His', '20150310_114651'), + DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), 'sTuff', null, 'WDWyig' @@ -33,7 +33,7 @@ class ReferenceLinkDB '?WDWyig', 'Used to test links reordering.', 0, - DateTime::createFromFormat('Ymd_His', '20100310_101010'), + DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20100310_101010'), 'ut' ); @@ -43,9 +43,9 @@ class ReferenceLinkDB 'https://static.fsf.org/nosvn/faif-2.0.pdf', 'Richard Stallman and the Free Software Revolution. Read this. #hashtag', 0, - DateTime::createFromFormat('Ymd_His', '20150310_114633'), + DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114633'), 'free gnu software stallman -exclude stuff hashtag', - DateTime::createFromFormat('Ymd_His', '20160803_093033') + DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160803_093033') ); $this->addLink( @@ -54,7 +54,7 @@ class ReferenceLinkDB 'http://mediagoblin.org/', 'A free software media publishing platform #hashtagOther', 0, - DateTime::createFromFormat('Ymd_His', '20130614_184135'), + DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20130614_184135'), 'gnu media web .hidden hashtag', null, 'IuWvgA' @@ -66,7 +66,7 @@ class ReferenceLinkDB 'https://dvcs.w3.org/hg/markup-validator/summary', 'Mercurial repository for the W3C Validator #private', 1, - DateTime::createFromFormat('Ymd_His', '20141125_084734'), + DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20141125_084734'), 'css html w3c web Mercurial' ); @@ -76,7 +76,7 @@ class ReferenceLinkDB 'http://ars.userfriendly.org/cartoons/?id=20121206', 'Naming conventions... #private', 0, - DateTime::createFromFormat('Ymd_His', '20121206_142300'), + DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_142300'), 'dev cartoon web' ); @@ -86,7 +86,7 @@ class ReferenceLinkDB 'http://ars.userfriendly.org/cartoons/?id=20010306', 'Tropical printing', 0, - DateTime::createFromFormat('Ymd_His', '20121206_172539'), + DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_172539'), 'samba cartoon web' ); @@ -96,7 +96,7 @@ class ReferenceLinkDB 'http://geek-and-poke.com/', '', 1, - DateTime::createFromFormat('Ymd_His', '20121206_182539'), + DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_182539'), 'dev cartoon tag1 tag2 tag3 tag4 ' ); } @@ -115,7 +115,7 @@ class ReferenceLinkDB 'tags' => $tags, 'created' => $date, 'updated' => $updated, - 'shorturl' => $shorturl ? $shorturl : smallHash($date->format('Ymd_His') . $id), + 'shorturl' => $shorturl ? $shorturl : smallHash($date->format(LinkDB::LINK_DATE_FORMAT) . $id), ); $this->_links[$id] = $link; -- cgit v1.2.3