123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640 |
- <?php
- require_once 'application/NetscapeBookmarkUtils.php';
- use Shaarli\Config\ConfigManager;
- /**
- * Utility function to load a file's metadata in a $_FILES-like array
- *
- * @param string $filename Basename of the file
- *
- * @return array A $_FILES-like array
- */
- function file2array($filename)
- {
- return array(
- 'filetoupload' => 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 string History file path
- */
- protected static $historyFilePath = 'sandbox/history.php';
- /**
- * @var LinkDB private LinkDB instance
- */
- protected $linkDb = null;
- /**
- * @var string Dummy page cache
- */
- protected $pagecache = 'tests';
- /**
- * @var ConfigManager instance.
- */
- protected $conf;
- /**
- * @var History instance.
- */
- protected $history;
- /**
- * @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
- */
- protected function setUp()
- {
- if (file_exists(self::$testDatastore)) {
- unlink(self::$testDatastore);
- }
- // start with an empty datastore
- file_put_contents(self::$testDatastore, '<?php /* S7QysKquBQA= */ ?>');
- $this->linkDb = new LinkDB(self::$testDatastore, true, false);
- $this->conf = new ConfigManager('tests/utils/config/configJson');
- $this->conf->set('resource.page_cache', $this->pagecache);
- $this->history = new History(self::$historyFilePath);
- }
- /**
- * Delete history file.
- */
- public function tearDown()
- {
- @unlink(self::$historyFilePath);
- }
- public static function tearDownAfterClass()
- {
- date_default_timezone_set(self::$defaultTimeZone);
- }
- /**
- * 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, $this->conf, $this->history)
- );
- $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, $this->conf, $this->history)
- );
- $this->assertEquals(0, count($this->linkDb));
- }
- /**
- * Ensure IE dumps are supported
- */
- public function testImportInternetExplorerEncoding()
- {
- $files = file2array('internet_explorer_encoding.htm');
- $this->assertStringMatchesFormat(
- 'File internet_explorer_encoding.htm (356 bytes) was successfully processed in %d seconds:'
- .' 1 links imported, 0 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history)
- );
- $this->assertEquals(1, count($this->linkDb));
- $this->assertEquals(0, count_private($this->linkDb));
- $this->assertEquals(
- array(
- 'id' => 0,
- 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160618_203944'),
- 'title' => 'Hg Init a Mercurial tutorial by Joel Spolsky',
- 'url' => 'http://hginit.com/',
- 'description' => '',
- 'private' => 0,
- 'tags' => '',
- 'shorturl' => 'La37cg',
- ),
- $this->linkDb->getLinkFromUrl('http://hginit.com/')
- );
- }
- /**
- * Import bookmarks nested in a folder hierarchy
- */
- public function testImportNested()
- {
- $files = file2array('netscape_nested.htm');
- $this->assertStringMatchesFormat(
- 'File netscape_nested.htm (1337 bytes) was successfully processed in %d seconds:'
- .' 8 links imported, 0 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history)
- );
- $this->assertEquals(8, count($this->linkDb));
- $this->assertEquals(2, count_private($this->linkDb));
- $this->assertEquals(
- array(
- 'id' => 0,
- 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235541'),
- 'title' => 'Nested 1',
- 'url' => 'http://nest.ed/1',
- 'description' => '',
- 'private' => 0,
- 'tags' => 'tag1 tag2',
- 'shorturl' => 'KyDNKA',
- ),
- $this->linkDb->getLinkFromUrl('http://nest.ed/1')
- );
- $this->assertEquals(
- array(
- 'id' => 1,
- 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235542'),
- 'title' => 'Nested 1-1',
- 'url' => 'http://nest.ed/1-1',
- 'description' => '',
- 'private' => 0,
- 'tags' => 'folder1 tag1 tag2',
- 'shorturl' => 'T2LnXg',
- ),
- $this->linkDb->getLinkFromUrl('http://nest.ed/1-1')
- );
- $this->assertEquals(
- array(
- 'id' => 2,
- 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235547'),
- 'title' => 'Nested 1-2',
- 'url' => 'http://nest.ed/1-2',
- 'description' => '',
- 'private' => 0,
- 'tags' => 'folder1 tag3 tag4',
- 'shorturl' => '46SZxA',
- ),
- $this->linkDb->getLinkFromUrl('http://nest.ed/1-2')
- );
- $this->assertEquals(
- array(
- 'id' => 3,
- 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160202_202222'),
- 'title' => 'Nested 2-1',
- 'url' => 'http://nest.ed/2-1',
- 'description' => 'First link of the second section',
- 'private' => 1,
- 'tags' => 'folder2',
- 'shorturl' => '4UHOSw',
- ),
- $this->linkDb->getLinkFromUrl('http://nest.ed/2-1')
- );
- $this->assertEquals(
- array(
- 'id' => 4,
- 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160119_230227'),
- 'title' => 'Nested 2-2',
- 'url' => 'http://nest.ed/2-2',
- 'description' => 'Second link of the second section',
- 'private' => 1,
- 'tags' => 'folder2',
- 'shorturl' => 'yfzwbw',
- ),
- $this->linkDb->getLinkFromUrl('http://nest.ed/2-2')
- );
- $this->assertEquals(
- array(
- 'id' => 5,
- 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160202_202222'),
- 'title' => 'Nested 3-1',
- 'url' => 'http://nest.ed/3-1',
- 'description' => '',
- 'private' => 0,
- 'tags' => 'folder3 folder3-1 tag3',
- 'shorturl' => 'UwxIUQ',
- ),
- $this->linkDb->getLinkFromUrl('http://nest.ed/3-1')
- );
- $this->assertEquals(
- array(
- 'id' => 6,
- 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160119_230227'),
- 'title' => 'Nested 3-2',
- 'url' => 'http://nest.ed/3-2',
- 'description' => '',
- 'private' => 0,
- 'tags' => 'folder3 folder3-1',
- 'shorturl' => 'p8dyZg',
- ),
- $this->linkDb->getLinkFromUrl('http://nest.ed/3-2')
- );
- $this->assertEquals(
- array(
- 'id' => 7,
- 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160229_111541'),
- 'title' => 'Nested 2',
- 'url' => 'http://nest.ed/2',
- 'description' => '',
- 'private' => 0,
- 'tags' => 'tag4',
- 'shorturl' => 'Gt3Uug',
- ),
- $this->linkDb->getLinkFromUrl('http://nest.ed/2')
- );
- }
- /**
- * 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->assertStringMatchesFormat(
- 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
- .' 2 links imported, 0 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history)
- );
- $this->assertEquals(2, count($this->linkDb));
- $this->assertEquals(1, count_private($this->linkDb));
- $this->assertEquals(
- array(
- 'id' => 0,
- // Old link - UTC+4 (note that TZ in the import file is ignored).
- 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20001010_135536'),
- 'title' => 'Secret stuff',
- 'url' => 'https://private.tld',
- 'description' => "Super-secret stuff you're not supposed to know about",
- 'private' => 1,
- 'tags' => 'private secret',
- 'shorturl' => 'EokDtA',
- ),
- $this->linkDb->getLinkFromUrl('https://private.tld')
- );
- $this->assertEquals(
- array(
- 'id' => 1,
- 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235548'),
- 'title' => 'Public stuff',
- 'url' => 'http://public.tld',
- 'description' => '',
- 'private' => 0,
- 'tags' => 'public hello world',
- 'shorturl' => 'Er9ddA',
- ),
- $this->linkDb->getLinkFromUrl('http://public.tld')
- );
- }
- /**
- * Import bookmarks with the default privacy setting (reuse from file)
- */
- public function testImportKeepPrivacy()
- {
- $post = array('privacy' => 'default');
- $files = file2array('netscape_basic.htm');
- $this->assertStringMatchesFormat(
- 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
- .' 2 links imported, 0 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
- );
- $this->assertEquals(2, count($this->linkDb));
- $this->assertEquals(1, count_private($this->linkDb));
- $this->assertEquals(
- array(
- 'id' => 0,
- // Note that TZ in the import file is ignored.
- 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20001010_135536'),
- 'title' => 'Secret stuff',
- 'url' => 'https://private.tld',
- 'description' => "Super-secret stuff you're not supposed to know about",
- 'private' => 1,
- 'tags' => 'private secret',
- 'shorturl' => 'EokDtA',
- ),
- $this->linkDb->getLinkFromUrl('https://private.tld')
- );
- $this->assertEquals(
- array(
- 'id' => 1,
- 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235548'),
- 'title' => 'Public stuff',
- 'url' => 'http://public.tld',
- 'description' => '',
- 'private' => 0,
- 'tags' => 'public hello world',
- 'shorturl' => 'Er9ddA',
- ),
- $this->linkDb->getLinkFromUrl('http://public.tld')
- );
- }
- /**
- * Import links as public
- */
- public function testImportAsPublic()
- {
- $post = array('privacy' => 'public');
- $files = file2array('netscape_basic.htm');
- $this->assertStringMatchesFormat(
- 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
- .' 2 links imported, 0 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
- );
- $this->assertEquals(2, count($this->linkDb));
- $this->assertEquals(0, count_private($this->linkDb));
- $this->assertEquals(
- 0,
- $this->linkDb[0]['private']
- );
- $this->assertEquals(
- 0,
- $this->linkDb[1]['private']
- );
- }
- /**
- * Import links as private
- */
- public function testImportAsPrivate()
- {
- $post = array('privacy' => 'private');
- $files = file2array('netscape_basic.htm');
- $this->assertStringMatchesFormat(
- 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
- .' 2 links imported, 0 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
- );
- $this->assertEquals(2, count($this->linkDb));
- $this->assertEquals(2, count_private($this->linkDb));
- $this->assertEquals(
- 1,
- $this->linkDb['0']['private']
- );
- $this->assertEquals(
- 1,
- $this->linkDb['1']['private']
- );
- }
- /**
- * Overwrite private links so they become public
- */
- public function testOverwriteAsPublic()
- {
- $files = file2array('netscape_basic.htm');
- // import links as private
- $post = array('privacy' => 'private');
- $this->assertStringMatchesFormat(
- 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
- .' 2 links imported, 0 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
- );
- $this->assertEquals(2, count($this->linkDb));
- $this->assertEquals(2, count_private($this->linkDb));
- $this->assertEquals(
- 1,
- $this->linkDb[0]['private']
- );
- $this->assertEquals(
- 1,
- $this->linkDb[1]['private']
- );
- // re-import as public, enable overwriting
- $post = array(
- 'privacy' => 'public',
- 'overwrite' => 'true'
- );
- $this->assertStringMatchesFormat(
- 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
- .' 2 links imported, 2 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
- );
- $this->assertEquals(2, count($this->linkDb));
- $this->assertEquals(0, count_private($this->linkDb));
- $this->assertEquals(
- 0,
- $this->linkDb[0]['private']
- );
- $this->assertEquals(
- 0,
- $this->linkDb[1]['private']
- );
- }
- /**
- * Overwrite public links so they become private
- */
- public function testOverwriteAsPrivate()
- {
- $files = file2array('netscape_basic.htm');
- // import links as public
- $post = array('privacy' => 'public');
- $this->assertStringMatchesFormat(
- 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
- .' 2 links imported, 0 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
- );
- $this->assertEquals(2, count($this->linkDb));
- $this->assertEquals(0, count_private($this->linkDb));
- $this->assertEquals(
- 0,
- $this->linkDb['0']['private']
- );
- $this->assertEquals(
- 0,
- $this->linkDb['1']['private']
- );
- // re-import as private, enable overwriting
- $post = array(
- 'privacy' => 'private',
- 'overwrite' => 'true'
- );
- $this->assertStringMatchesFormat(
- 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
- .' 2 links imported, 2 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
- );
- $this->assertEquals(2, count($this->linkDb));
- $this->assertEquals(2, count_private($this->linkDb));
- $this->assertEquals(
- 1,
- $this->linkDb['0']['private']
- );
- $this->assertEquals(
- 1,
- $this->linkDb['1']['private']
- );
- }
- /**
- * Attept to import the same links twice without enabling overwriting
- */
- public function testSkipOverwrite()
- {
- $post = array('privacy' => 'public');
- $files = file2array('netscape_basic.htm');
- $this->assertStringMatchesFormat(
- 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
- .' 2 links imported, 0 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
- );
- $this->assertEquals(2, count($this->linkDb));
- $this->assertEquals(0, count_private($this->linkDb));
- // re-import as private, DO NOT enable overwriting
- $post = array('privacy' => 'private');
- $this->assertStringMatchesFormat(
- 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
- .' 0 links imported, 0 links overwritten, 2 links skipped.',
- NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
- );
- $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->assertStringMatchesFormat(
- 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
- .' 2 links imported, 0 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
- );
- $this->assertEquals(2, count($this->linkDb));
- $this->assertEquals(0, count_private($this->linkDb));
- $this->assertEquals(
- 'tag1 tag2 tag3 private secret',
- $this->linkDb['0']['tags']
- );
- $this->assertEquals(
- 'tag1 tag2 tag3 public hello world',
- $this->linkDb['1']['tags']
- );
- }
- /**
- * 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->assertStringMatchesFormat(
- 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
- .' 2 links imported, 0 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
- );
- $this->assertEquals(2, count($this->linkDb));
- $this->assertEquals(0, count_private($this->linkDb));
- $this->assertEquals(
- 'tag1& tag2 "tag3" private secret',
- $this->linkDb['0']['tags']
- );
- $this->assertEquals(
- 'tag1& tag2 "tag3" public hello world',
- $this->linkDb['1']['tags']
- );
- }
- /**
- * Ensure each imported bookmark has a unique id
- *
- * See https://github.com/shaarli/Shaarli/issues/351
- */
- public function testImportSameDate()
- {
- $files = file2array('same_date.htm');
- $this->assertStringMatchesFormat(
- 'File same_date.htm (453 bytes) was successfully processed in %d seconds:'
- .' 3 links imported, 0 links overwritten, 0 links skipped.',
- NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->conf, $this->history)
- );
- $this->assertEquals(3, count($this->linkDb));
- $this->assertEquals(0, count_private($this->linkDb));
- $this->assertEquals(
- 0,
- $this->linkDb[0]['id']
- );
- $this->assertEquals(
- 1,
- $this->linkDb[1]['id']
- );
- $this->assertEquals(
- 2,
- $this->linkDb[2]['id']
- );
- }
- public function testImportCreateUpdateHistory()
- {
- $post = [
- 'privacy' => 'public',
- 'overwrite' => 'true',
- ];
- $files = file2array('netscape_basic.htm');
- NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history);
- $history = $this->history->getHistory();
- $this->assertEquals(1, count($history));
- $this->assertEquals(History::IMPORT, $history[0]['event']);
- $this->assertTrue(new DateTime('-5 seconds') < $history[0]['datetime']);
- // re-import as private, enable overwriting
- NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history);
- $history = $this->history->getHistory();
- $this->assertEquals(2, count($history));
- $this->assertEquals(History::IMPORT, $history[0]['event']);
- $this->assertTrue(new DateTime('-5 seconds') < $history[0]['datetime']);
- $this->assertEquals(History::IMPORT, $history[1]['event']);
- $this->assertTrue(new DateTime('-5 seconds') < $history[1]['datetime']);
- }
- }
|