2016-07-28 22:54:33 +02:00
|
|
|
<?php
|
2018-12-04 00:13:42 +01:00
|
|
|
namespace Shaarli\Netscape;
|
2016-07-28 22:54:33 +02:00
|
|
|
|
2018-12-04 00:13:42 +01:00
|
|
|
use DateTime;
|
2018-12-03 01:10:39 +01:00
|
|
|
use Shaarli\Bookmark\LinkDB;
|
2017-03-10 18:49:53 +01:00
|
|
|
use Shaarli\Config\ConfigManager;
|
2018-12-02 23:24:58 +01:00
|
|
|
use Shaarli\History;
|
2016-07-28 22:54:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2018-12-04 00:13:42 +01:00
|
|
|
class BookmarkImportTest extends \PHPUnit\Framework\TestCase
|
2016-07-28 22:54:33 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string datastore to test write operations
|
|
|
|
*/
|
|
|
|
protected static $testDatastore = 'sandbox/datastore.php';
|
|
|
|
|
2017-01-16 12:31:08 +01:00
|
|
|
/**
|
|
|
|
* @var string History file path
|
|
|
|
*/
|
|
|
|
protected static $historyFilePath = 'sandbox/history.php';
|
|
|
|
|
2016-07-28 22:54:33 +02:00
|
|
|
/**
|
|
|
|
* @var LinkDB private LinkDB instance
|
|
|
|
*/
|
|
|
|
protected $linkDb = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Dummy page cache
|
|
|
|
*/
|
|
|
|
protected $pagecache = 'tests';
|
|
|
|
|
2017-02-09 20:54:56 +01:00
|
|
|
/**
|
|
|
|
* @var ConfigManager instance.
|
|
|
|
*/
|
|
|
|
protected $conf;
|
|
|
|
|
2017-01-16 12:31:08 +01:00
|
|
|
/**
|
|
|
|
* @var History instance.
|
|
|
|
*/
|
|
|
|
protected $history;
|
|
|
|
|
2016-11-28 16:17:25 +01:00
|
|
|
/**
|
|
|
|
* @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');
|
|
|
|
}
|
|
|
|
|
2016-07-28 22:54:33 +02:00
|
|
|
/**
|
|
|
|
* 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);
|
2017-02-09 20:54:56 +01:00
|
|
|
$this->conf = new ConfigManager('tests/utils/config/configJson');
|
|
|
|
$this->conf->set('resource.page_cache', $this->pagecache);
|
2017-01-16 12:31:08 +01:00
|
|
|
$this->history = new History(self::$historyFilePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete history file.
|
|
|
|
*/
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
@unlink(self::$historyFilePath);
|
2016-07-28 22:54:33 +02:00
|
|
|
}
|
|
|
|
|
2016-11-28 16:17:25 +01:00
|
|
|
public static function tearDownAfterClass()
|
|
|
|
{
|
|
|
|
date_default_timezone_set(self::$defaultTimeZone);
|
|
|
|
}
|
|
|
|
|
2016-07-28 22:54:33 +02:00
|
|
|
/**
|
|
|
|
* 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.',
|
2017-01-16 12:31:08 +01:00
|
|
|
NetscapeBookmarkUtils::import(null, $files, null, $this->conf, $this->history)
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
$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.',
|
2017-01-16 12:31:08 +01:00
|
|
|
NetscapeBookmarkUtils::import(null, $files, null, $this->conf, $this->history)
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(0, count($this->linkDb));
|
|
|
|
}
|
|
|
|
|
2018-02-23 20:34:06 +01:00
|
|
|
/**
|
|
|
|
* Attempt to import bookmarks from a file with a lowercase Doctype
|
|
|
|
*/
|
|
|
|
public function testImportLowecaseDoctype()
|
|
|
|
{
|
|
|
|
$files = file2array('lowercase_doctype.htm');
|
|
|
|
$this->assertStringMatchesFormat(
|
|
|
|
'File lowercase_doctype.htm (386 bytes) was successfully processed in %d seconds:'
|
|
|
|
.' 2 links imported, 0 links overwritten, 0 links skipped.',
|
|
|
|
NetscapeBookmarkUtils::import(null, $files, $this->linkDb, $this->conf, $this->history)
|
|
|
|
);
|
|
|
|
$this->assertEquals(2, count($this->linkDb));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-12 23:22:15 +02:00
|
|
|
/**
|
|
|
|
* Ensure IE dumps are supported
|
|
|
|
*/
|
|
|
|
public function testImportInternetExplorerEncoding()
|
|
|
|
{
|
|
|
|
$files = file2array('internet_explorer_encoding.htm');
|
2017-10-07 16:40:16 +02:00
|
|
|
$this->assertStringMatchesFormat(
|
|
|
|
'File internet_explorer_encoding.htm (356 bytes) was successfully processed in %d seconds:'
|
2016-08-12 23:22:15 +02:00
|
|
|
.' 1 links imported, 0 links overwritten, 0 links skipped.',
|
2017-01-16 12:31:08 +01:00
|
|
|
NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history)
|
2016-08-12 23:22:15 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(1, count($this->linkDb));
|
|
|
|
$this->assertEquals(0, count_private($this->linkDb));
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
2016-11-28 16:17:25 +01:00
|
|
|
'id' => 0,
|
2016-11-28 18:24:15 +01:00
|
|
|
'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160618_203944'),
|
2016-08-12 23:22:15 +02:00
|
|
|
'title' => 'Hg Init a Mercurial tutorial by Joel Spolsky',
|
|
|
|
'url' => 'http://hginit.com/',
|
|
|
|
'description' => '',
|
|
|
|
'private' => 0,
|
2016-11-28 16:17:25 +01:00
|
|
|
'tags' => '',
|
|
|
|
'shorturl' => 'La37cg',
|
2016-08-12 23:22:15 +02:00
|
|
|
),
|
|
|
|
$this->linkDb->getLinkFromUrl('http://hginit.com/')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-07-28 22:54:33 +02:00
|
|
|
/**
|
|
|
|
* Import bookmarks nested in a folder hierarchy
|
|
|
|
*/
|
|
|
|
public function testImportNested()
|
|
|
|
{
|
|
|
|
$files = file2array('netscape_nested.htm');
|
2017-10-07 16:40:16 +02:00
|
|
|
$this->assertStringMatchesFormat(
|
|
|
|
'File netscape_nested.htm (1337 bytes) was successfully processed in %d seconds:'
|
2016-07-28 22:54:33 +02:00
|
|
|
.' 8 links imported, 0 links overwritten, 0 links skipped.',
|
2017-01-16 12:31:08 +01:00
|
|
|
NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history)
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(8, count($this->linkDb));
|
|
|
|
$this->assertEquals(2, count_private($this->linkDb));
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
2016-11-28 16:17:25 +01:00
|
|
|
'id' => 0,
|
2016-11-28 18:24:15 +01:00
|
|
|
'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235541'),
|
2016-07-28 22:54:33 +02:00
|
|
|
'title' => 'Nested 1',
|
|
|
|
'url' => 'http://nest.ed/1',
|
|
|
|
'description' => '',
|
|
|
|
'private' => 0,
|
2016-11-28 16:17:25 +01:00
|
|
|
'tags' => 'tag1 tag2',
|
|
|
|
'shorturl' => 'KyDNKA',
|
2016-07-28 22:54:33 +02:00
|
|
|
),
|
|
|
|
$this->linkDb->getLinkFromUrl('http://nest.ed/1')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
2016-11-28 16:17:25 +01:00
|
|
|
'id' => 1,
|
2016-11-28 18:24:15 +01:00
|
|
|
'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235542'),
|
2016-07-28 22:54:33 +02:00
|
|
|
'title' => 'Nested 1-1',
|
|
|
|
'url' => 'http://nest.ed/1-1',
|
|
|
|
'description' => '',
|
|
|
|
'private' => 0,
|
2016-11-28 16:17:25 +01:00
|
|
|
'tags' => 'folder1 tag1 tag2',
|
|
|
|
'shorturl' => 'T2LnXg',
|
2016-07-28 22:54:33 +02:00
|
|
|
),
|
|
|
|
$this->linkDb->getLinkFromUrl('http://nest.ed/1-1')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
2016-11-28 16:17:25 +01:00
|
|
|
'id' => 2,
|
2016-11-28 18:24:15 +01:00
|
|
|
'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235547'),
|
2016-07-28 22:54:33 +02:00
|
|
|
'title' => 'Nested 1-2',
|
|
|
|
'url' => 'http://nest.ed/1-2',
|
|
|
|
'description' => '',
|
|
|
|
'private' => 0,
|
2016-11-28 16:17:25 +01:00
|
|
|
'tags' => 'folder1 tag3 tag4',
|
|
|
|
'shorturl' => '46SZxA',
|
2016-07-28 22:54:33 +02:00
|
|
|
),
|
|
|
|
$this->linkDb->getLinkFromUrl('http://nest.ed/1-2')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
2016-11-28 16:17:25 +01:00
|
|
|
'id' => 3,
|
2016-11-28 18:24:15 +01:00
|
|
|
'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160202_202222'),
|
2016-07-28 22:54:33 +02:00
|
|
|
'title' => 'Nested 2-1',
|
|
|
|
'url' => 'http://nest.ed/2-1',
|
|
|
|
'description' => 'First link of the second section',
|
|
|
|
'private' => 1,
|
2016-11-28 16:17:25 +01:00
|
|
|
'tags' => 'folder2',
|
|
|
|
'shorturl' => '4UHOSw',
|
2016-07-28 22:54:33 +02:00
|
|
|
),
|
|
|
|
$this->linkDb->getLinkFromUrl('http://nest.ed/2-1')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
2016-11-28 16:17:25 +01:00
|
|
|
'id' => 4,
|
2016-11-28 18:24:15 +01:00
|
|
|
'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160119_230227'),
|
2016-07-28 22:54:33 +02:00
|
|
|
'title' => 'Nested 2-2',
|
|
|
|
'url' => 'http://nest.ed/2-2',
|
|
|
|
'description' => 'Second link of the second section',
|
|
|
|
'private' => 1,
|
2016-11-28 16:17:25 +01:00
|
|
|
'tags' => 'folder2',
|
|
|
|
'shorturl' => 'yfzwbw',
|
2016-07-28 22:54:33 +02:00
|
|
|
),
|
|
|
|
$this->linkDb->getLinkFromUrl('http://nest.ed/2-2')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
2016-11-28 16:17:25 +01:00
|
|
|
'id' => 5,
|
2016-11-28 18:24:15 +01:00
|
|
|
'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160202_202222'),
|
2016-07-28 22:54:33 +02:00
|
|
|
'title' => 'Nested 3-1',
|
|
|
|
'url' => 'http://nest.ed/3-1',
|
|
|
|
'description' => '',
|
|
|
|
'private' => 0,
|
2016-11-28 16:17:25 +01:00
|
|
|
'tags' => 'folder3 folder3-1 tag3',
|
|
|
|
'shorturl' => 'UwxIUQ',
|
2016-07-28 22:54:33 +02:00
|
|
|
),
|
|
|
|
$this->linkDb->getLinkFromUrl('http://nest.ed/3-1')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
2016-11-28 16:17:25 +01:00
|
|
|
'id' => 6,
|
2016-11-28 18:24:15 +01:00
|
|
|
'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160119_230227'),
|
2016-07-28 22:54:33 +02:00
|
|
|
'title' => 'Nested 3-2',
|
|
|
|
'url' => 'http://nest.ed/3-2',
|
|
|
|
'description' => '',
|
|
|
|
'private' => 0,
|
2016-11-28 16:17:25 +01:00
|
|
|
'tags' => 'folder3 folder3-1',
|
|
|
|
'shorturl' => 'p8dyZg',
|
2016-07-28 22:54:33 +02:00
|
|
|
),
|
|
|
|
$this->linkDb->getLinkFromUrl('http://nest.ed/3-2')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
2016-11-28 16:17:25 +01:00
|
|
|
'id' => 7,
|
2016-11-28 18:24:15 +01:00
|
|
|
'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160229_111541'),
|
2016-07-28 22:54:33 +02:00
|
|
|
'title' => 'Nested 2',
|
|
|
|
'url' => 'http://nest.ed/2',
|
|
|
|
'description' => '',
|
|
|
|
'private' => 0,
|
2016-11-28 16:17:25 +01:00
|
|
|
'tags' => 'tag4',
|
|
|
|
'shorturl' => 'Gt3Uug',
|
2016-07-28 22:54:33 +02:00
|
|
|
),
|
|
|
|
$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');
|
2017-10-07 16:40:16 +02:00
|
|
|
$this->assertStringMatchesFormat(
|
|
|
|
'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
|
2016-07-28 22:54:33 +02:00
|
|
|
.' 2 links imported, 0 links overwritten, 0 links skipped.',
|
2017-01-16 12:31:08 +01:00
|
|
|
NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history)
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
2016-11-28 16:17:25 +01:00
|
|
|
|
2016-07-28 22:54:33 +02:00
|
|
|
$this->assertEquals(2, count($this->linkDb));
|
|
|
|
$this->assertEquals(1, count_private($this->linkDb));
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
2016-11-28 16:17:25 +01:00
|
|
|
'id' => 0,
|
|
|
|
// Old link - UTC+4 (note that TZ in the import file is ignored).
|
2016-11-28 18:24:15 +01:00
|
|
|
'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20001010_135536'),
|
2016-07-28 22:54:33 +02:00
|
|
|
'title' => 'Secret stuff',
|
|
|
|
'url' => 'https://private.tld',
|
|
|
|
'description' => "Super-secret stuff you're not supposed to know about",
|
|
|
|
'private' => 1,
|
2016-11-28 16:17:25 +01:00
|
|
|
'tags' => 'private secret',
|
|
|
|
'shorturl' => 'EokDtA',
|
2016-07-28 22:54:33 +02:00
|
|
|
),
|
|
|
|
$this->linkDb->getLinkFromUrl('https://private.tld')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
2016-11-28 16:17:25 +01:00
|
|
|
'id' => 1,
|
2016-11-28 18:24:15 +01:00
|
|
|
'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235548'),
|
2016-07-28 22:54:33 +02:00
|
|
|
'title' => 'Public stuff',
|
|
|
|
'url' => 'http://public.tld',
|
|
|
|
'description' => '',
|
|
|
|
'private' => 0,
|
2016-11-28 16:17:25 +01:00
|
|
|
'tags' => 'public hello world',
|
|
|
|
'shorturl' => 'Er9ddA',
|
2016-07-28 22:54:33 +02:00
|
|
|
),
|
|
|
|
$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');
|
2017-10-07 16:40:16 +02:00
|
|
|
$this->assertStringMatchesFormat(
|
|
|
|
'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
|
2016-07-28 22:54:33 +02:00
|
|
|
.' 2 links imported, 0 links overwritten, 0 links skipped.',
|
2017-01-16 12:31:08 +01:00
|
|
|
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(2, count($this->linkDb));
|
|
|
|
$this->assertEquals(1, count_private($this->linkDb));
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
2016-11-28 16:17:25 +01:00
|
|
|
'id' => 0,
|
|
|
|
// Note that TZ in the import file is ignored.
|
2016-11-28 18:24:15 +01:00
|
|
|
'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20001010_135536'),
|
2016-07-28 22:54:33 +02:00
|
|
|
'title' => 'Secret stuff',
|
|
|
|
'url' => 'https://private.tld',
|
|
|
|
'description' => "Super-secret stuff you're not supposed to know about",
|
|
|
|
'private' => 1,
|
2016-11-28 16:17:25 +01:00
|
|
|
'tags' => 'private secret',
|
|
|
|
'shorturl' => 'EokDtA',
|
2016-07-28 22:54:33 +02:00
|
|
|
),
|
|
|
|
$this->linkDb->getLinkFromUrl('https://private.tld')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
2016-11-28 16:17:25 +01:00
|
|
|
'id' => 1,
|
2016-11-28 18:24:15 +01:00
|
|
|
'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235548'),
|
2016-07-28 22:54:33 +02:00
|
|
|
'title' => 'Public stuff',
|
|
|
|
'url' => 'http://public.tld',
|
|
|
|
'description' => '',
|
|
|
|
'private' => 0,
|
2016-11-28 16:17:25 +01:00
|
|
|
'tags' => 'public hello world',
|
|
|
|
'shorturl' => 'Er9ddA',
|
2016-07-28 22:54:33 +02:00
|
|
|
),
|
|
|
|
$this->linkDb->getLinkFromUrl('http://public.tld')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import links as public
|
|
|
|
*/
|
|
|
|
public function testImportAsPublic()
|
|
|
|
{
|
|
|
|
$post = array('privacy' => 'public');
|
|
|
|
$files = file2array('netscape_basic.htm');
|
2017-10-07 16:40:16 +02:00
|
|
|
$this->assertStringMatchesFormat(
|
|
|
|
'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
|
2016-07-28 22:54:33 +02:00
|
|
|
.' 2 links imported, 0 links overwritten, 0 links skipped.',
|
2017-01-16 12:31:08 +01:00
|
|
|
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(2, count($this->linkDb));
|
|
|
|
$this->assertEquals(0, count_private($this->linkDb));
|
|
|
|
$this->assertEquals(
|
|
|
|
0,
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->linkDb[0]['private']
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
0,
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->linkDb[1]['private']
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import links as private
|
|
|
|
*/
|
|
|
|
public function testImportAsPrivate()
|
|
|
|
{
|
|
|
|
$post = array('privacy' => 'private');
|
|
|
|
$files = file2array('netscape_basic.htm');
|
2017-10-07 16:40:16 +02:00
|
|
|
$this->assertStringMatchesFormat(
|
|
|
|
'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
|
2016-07-28 22:54:33 +02:00
|
|
|
.' 2 links imported, 0 links overwritten, 0 links skipped.',
|
2017-01-16 12:31:08 +01:00
|
|
|
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(2, count($this->linkDb));
|
|
|
|
$this->assertEquals(2, count_private($this->linkDb));
|
|
|
|
$this->assertEquals(
|
|
|
|
1,
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->linkDb['0']['private']
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
1,
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->linkDb['1']['private']
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overwrite private links so they become public
|
|
|
|
*/
|
|
|
|
public function testOverwriteAsPublic()
|
|
|
|
{
|
|
|
|
$files = file2array('netscape_basic.htm');
|
|
|
|
|
|
|
|
// import links as private
|
|
|
|
$post = array('privacy' => 'private');
|
2017-10-07 16:40:16 +02:00
|
|
|
$this->assertStringMatchesFormat(
|
|
|
|
'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
|
2016-07-28 22:54:33 +02:00
|
|
|
.' 2 links imported, 0 links overwritten, 0 links skipped.',
|
2017-01-16 12:31:08 +01:00
|
|
|
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(2, count($this->linkDb));
|
|
|
|
$this->assertEquals(2, count_private($this->linkDb));
|
|
|
|
$this->assertEquals(
|
|
|
|
1,
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->linkDb[0]['private']
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
1,
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->linkDb[1]['private']
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
// re-import as public, enable overwriting
|
|
|
|
$post = array(
|
|
|
|
'privacy' => 'public',
|
|
|
|
'overwrite' => 'true'
|
|
|
|
);
|
2017-10-07 16:40:16 +02:00
|
|
|
$this->assertStringMatchesFormat(
|
|
|
|
'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
|
2016-07-28 22:54:33 +02:00
|
|
|
.' 2 links imported, 2 links overwritten, 0 links skipped.',
|
2017-01-16 12:31:08 +01:00
|
|
|
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(2, count($this->linkDb));
|
|
|
|
$this->assertEquals(0, count_private($this->linkDb));
|
|
|
|
$this->assertEquals(
|
|
|
|
0,
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->linkDb[0]['private']
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
0,
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->linkDb[1]['private']
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overwrite public links so they become private
|
|
|
|
*/
|
|
|
|
public function testOverwriteAsPrivate()
|
|
|
|
{
|
|
|
|
$files = file2array('netscape_basic.htm');
|
|
|
|
|
|
|
|
// import links as public
|
|
|
|
$post = array('privacy' => 'public');
|
2017-10-07 16:40:16 +02:00
|
|
|
$this->assertStringMatchesFormat(
|
|
|
|
'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
|
2016-07-28 22:54:33 +02:00
|
|
|
.' 2 links imported, 0 links overwritten, 0 links skipped.',
|
2017-01-16 12:31:08 +01:00
|
|
|
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(2, count($this->linkDb));
|
|
|
|
$this->assertEquals(0, count_private($this->linkDb));
|
|
|
|
$this->assertEquals(
|
|
|
|
0,
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->linkDb['0']['private']
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
0,
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->linkDb['1']['private']
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// re-import as private, enable overwriting
|
|
|
|
$post = array(
|
|
|
|
'privacy' => 'private',
|
|
|
|
'overwrite' => 'true'
|
|
|
|
);
|
2017-10-07 16:40:16 +02:00
|
|
|
$this->assertStringMatchesFormat(
|
|
|
|
'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
|
2016-07-28 22:54:33 +02:00
|
|
|
.' 2 links imported, 2 links overwritten, 0 links skipped.',
|
2017-01-16 12:31:08 +01:00
|
|
|
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(2, count($this->linkDb));
|
|
|
|
$this->assertEquals(2, count_private($this->linkDb));
|
|
|
|
$this->assertEquals(
|
|
|
|
1,
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->linkDb['0']['private']
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
1,
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->linkDb['1']['private']
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attept to import the same links twice without enabling overwriting
|
|
|
|
*/
|
|
|
|
public function testSkipOverwrite()
|
|
|
|
{
|
|
|
|
$post = array('privacy' => 'public');
|
|
|
|
$files = file2array('netscape_basic.htm');
|
2017-10-07 16:40:16 +02:00
|
|
|
$this->assertStringMatchesFormat(
|
|
|
|
'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
|
2016-07-28 22:54:33 +02:00
|
|
|
.' 2 links imported, 0 links overwritten, 0 links skipped.',
|
2017-01-16 12:31:08 +01:00
|
|
|
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
$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');
|
2017-10-07 16:40:16 +02:00
|
|
|
$this->assertStringMatchesFormat(
|
|
|
|
'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
|
2016-07-28 22:54:33 +02:00
|
|
|
.' 0 links imported, 0 links overwritten, 2 links skipped.',
|
2017-01-16 12:31:08 +01:00
|
|
|
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
$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');
|
2017-10-07 16:40:16 +02:00
|
|
|
$this->assertStringMatchesFormat(
|
|
|
|
'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
|
2016-07-28 22:54:33 +02:00
|
|
|
.' 2 links imported, 0 links overwritten, 0 links skipped.',
|
2017-01-16 12:31:08 +01:00
|
|
|
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(2, count($this->linkDb));
|
|
|
|
$this->assertEquals(0, count_private($this->linkDb));
|
|
|
|
$this->assertEquals(
|
|
|
|
'tag1 tag2 tag3 private secret',
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->linkDb['0']['tags']
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'tag1 tag2 tag3 public hello world',
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->linkDb['1']['tags']
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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');
|
2017-10-07 16:40:16 +02:00
|
|
|
$this->assertStringMatchesFormat(
|
|
|
|
'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
|
2016-07-28 22:54:33 +02:00
|
|
|
.' 2 links imported, 0 links overwritten, 0 links skipped.',
|
2017-01-16 12:31:08 +01:00
|
|
|
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(2, count($this->linkDb));
|
|
|
|
$this->assertEquals(0, count_private($this->linkDb));
|
|
|
|
$this->assertEquals(
|
|
|
|
'tag1& tag2 "tag3" private secret',
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->linkDb['0']['tags']
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'tag1& tag2 "tag3" public hello world',
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->linkDb['1']['tags']
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-28 16:17:25 +01:00
|
|
|
* Ensure each imported bookmark has a unique id
|
2016-07-28 22:54:33 +02:00
|
|
|
*
|
|
|
|
* See https://github.com/shaarli/Shaarli/issues/351
|
|
|
|
*/
|
|
|
|
public function testImportSameDate()
|
|
|
|
{
|
|
|
|
$files = file2array('same_date.htm');
|
2017-10-07 16:40:16 +02:00
|
|
|
$this->assertStringMatchesFormat(
|
|
|
|
'File same_date.htm (453 bytes) was successfully processed in %d seconds:'
|
2016-07-28 22:54:33 +02:00
|
|
|
.' 3 links imported, 0 links overwritten, 0 links skipped.',
|
2017-01-16 12:31:08 +01:00
|
|
|
NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->conf, $this->history)
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(3, count($this->linkDb));
|
|
|
|
$this->assertEquals(0, count_private($this->linkDb));
|
|
|
|
$this->assertEquals(
|
2016-11-28 16:17:25 +01:00
|
|
|
0,
|
|
|
|
$this->linkDb[0]['id']
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2016-11-28 16:17:25 +01:00
|
|
|
1,
|
|
|
|
$this->linkDb[1]['id']
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2016-11-28 16:17:25 +01:00
|
|
|
2,
|
|
|
|
$this->linkDb[2]['id']
|
2016-07-28 22:54:33 +02:00
|
|
|
);
|
|
|
|
}
|
2017-01-16 12:31:08 +01:00
|
|
|
|
|
|
|
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();
|
2017-10-07 16:40:16 +02:00
|
|
|
$this->assertEquals(1, count($history));
|
|
|
|
$this->assertEquals(History::IMPORT, $history[0]['event']);
|
|
|
|
$this->assertTrue(new DateTime('-5 seconds') < $history[0]['datetime']);
|
2017-01-16 12:31:08 +01:00
|
|
|
|
|
|
|
// re-import as private, enable overwriting
|
|
|
|
NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history);
|
|
|
|
$history = $this->history->getHistory();
|
2017-10-07 16:40:16 +02:00
|
|
|
$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']);
|
2017-01-16 12:31:08 +01:00
|
|
|
}
|
2016-07-28 22:54:33 +02:00
|
|
|
}
|