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/BookmarkImportTest.php | 518 +++++++++++++++++++++ 1 file changed, 518 insertions(+) create mode 100644 tests/NetscapeBookmarkUtils/BookmarkImportTest.php (limited to 'tests/NetscapeBookmarkUtils/BookmarkImportTest.php') 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'] + ); + } +} -- 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 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'tests/NetscapeBookmarkUtils/BookmarkImportTest.php') 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 */ -- 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/NetscapeBookmarkUtils/BookmarkImportTest.php | 146 ++++++++++++++------- 1 file changed, 95 insertions(+), 51 deletions(-) (limited to 'tests/NetscapeBookmarkUtils/BookmarkImportTest.php') 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'] ); } } -- 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/NetscapeBookmarkUtils/BookmarkImportTest.php | 26 +++++++++++----------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'tests/NetscapeBookmarkUtils/BookmarkImportTest.php') 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' => '', -- cgit v1.2.3