3 require_once 'application/NetscapeBookmarkUtils.php';
7 * Utility function to load a file's metadata in a $_FILES-like array
9 * @param string $filename Basename of the file
11 * @return array A $_FILES-like array
13 function file2array($filename)
16 'filetoupload' => array(
18 'tmp_name' => __DIR__
. '/input/' . $filename,
19 'size' => filesize(__DIR__
. '/input/' . $filename)
26 * Netscape bookmark import
28 class BookmarkImportTest
extends PHPUnit_Framework_TestCase
31 * @var string datastore to test write operations
33 protected static $testDatastore = 'sandbox/datastore.php';
36 * @var LinkDB private LinkDB instance
38 protected $linkDb = null;
41 * @var string Dummy page cache
43 protected $pagecache = 'tests';
46 * Resets test data before each test
48 protected function setUp()
50 if (file_exists(self
::$testDatastore)) {
51 unlink(self
::$testDatastore);
53 // start with an empty datastore
54 file_put_contents(self
::$testDatastore, '<?php /* S7QysKquBQA= */ ?>');
55 $this->linkDb
= new LinkDB(self
::$testDatastore, true, false);
59 * Attempt to import bookmarks from an empty file
61 public function testImportEmptyData()
63 $files = file2array('empty.htm');
65 'File empty.htm (0 bytes) has an unknown file format.'
66 .' Nothing was imported.',
67 NetscapeBookmarkUtils
::import(NULL, $files, NULL, NULL)
69 $this->assertEquals(0, count($this->linkDb
));
73 * Attempt to import bookmarks from a file with no Doctype
75 public function testImportNoDoctype()
77 $files = file2array('no_doctype.htm');
79 'File no_doctype.htm (350 bytes) has an unknown file format. Nothing was imported.',
80 NetscapeBookmarkUtils
::import(NULL, $files, NULL, NULL)
82 $this->assertEquals(0, count($this->linkDb
));
86 * Ensure IE dumps are supported
88 public function testImportInternetExplorerEncoding()
90 $files = file2array('internet_explorer_encoding.htm');
92 'File internet_explorer_encoding.htm (356 bytes) was successfully processed:'
93 .' 1 links imported, 0 links overwritten, 0 links skipped.',
94 NetscapeBookmarkUtils
::import(array(), $files, $this->linkDb
, $this->pagecache
)
96 $this->assertEquals(1, count($this->linkDb
));
97 $this->assertEquals(0, count_private($this->linkDb
));
101 'linkdate' => '20160618_173944',
102 'title' => 'Hg Init a Mercurial tutorial by Joel Spolsky',
103 'url' => 'http://hginit.com/',
108 $this->linkDb
->getLinkFromUrl('http://hginit.com/')
114 * Import bookmarks nested in a folder hierarchy
116 public function testImportNested()
118 $files = file2array('netscape_nested.htm');
120 'File netscape_nested.htm (1337 bytes) was successfully processed:'
121 .' 8 links imported, 0 links overwritten, 0 links skipped.',
122 NetscapeBookmarkUtils
::import(array(), $files, $this->linkDb
, $this->pagecache
)
124 $this->assertEquals(8, count($this->linkDb
));
125 $this->assertEquals(2, count_private($this->linkDb
));
129 'linkdate' => '20160225_205541',
130 'title' => 'Nested 1',
131 'url' => 'http://nest.ed/1',
134 'tags' => 'tag1 tag2'
136 $this->linkDb
->getLinkFromUrl('http://nest.ed/1')
140 'linkdate' => '20160225_205542',
141 'title' => 'Nested 1-1',
142 'url' => 'http://nest.ed/1-1',
145 'tags' => 'folder1 tag1 tag2'
147 $this->linkDb
->getLinkFromUrl('http://nest.ed/1-1')
151 'linkdate' => '20160225_205547',
152 'title' => 'Nested 1-2',
153 'url' => 'http://nest.ed/1-2',
156 'tags' => 'folder1 tag3 tag4'
158 $this->linkDb
->getLinkFromUrl('http://nest.ed/1-2')
162 'linkdate' => '20160202_172222',
163 'title' => 'Nested 2-1',
164 'url' => 'http://nest.ed/2-1',
165 'description' => 'First link of the second section',
169 $this->linkDb
->getLinkFromUrl('http://nest.ed/2-1')
173 'linkdate' => '20160119_200227',
174 'title' => 'Nested 2-2',
175 'url' => 'http://nest.ed/2-2',
176 'description' => 'Second link of the second section',
180 $this->linkDb
->getLinkFromUrl('http://nest.ed/2-2')
184 'linkdate' => '20160202_172223',
185 'title' => 'Nested 3-1',
186 'url' => 'http://nest.ed/3-1',
189 'tags' => 'folder3 folder3-1 tag3'
191 $this->linkDb
->getLinkFromUrl('http://nest.ed/3-1')
195 'linkdate' => '20160119_200228',
196 'title' => 'Nested 3-2',
197 'url' => 'http://nest.ed/3-2',
200 'tags' => 'folder3 folder3-1'
202 $this->linkDb
->getLinkFromUrl('http://nest.ed/3-2')
206 'linkdate' => '20160229_081541',
207 'title' => 'Nested 2',
208 'url' => 'http://nest.ed/2',
213 $this->linkDb
->getLinkFromUrl('http://nest.ed/2')
218 * Import bookmarks with the default privacy setting (reuse from file)
220 * The $_POST array is not set.
222 public function testImportDefaultPrivacyNoPost()
224 $files = file2array('netscape_basic.htm');
226 'File netscape_basic.htm (482 bytes) was successfully processed:'
227 .' 2 links imported, 0 links overwritten, 0 links skipped.',
228 NetscapeBookmarkUtils
::import(array(), $files, $this->linkDb
, $this->pagecache
)
230 $this->assertEquals(2, count($this->linkDb
));
231 $this->assertEquals(1, count_private($this->linkDb
));
235 'linkdate' => '20001010_105536',
236 'title' => 'Secret stuff',
237 'url' => 'https://private.tld',
238 'description' => "Super-secret stuff you're not supposed to know about",
240 'tags' => 'private secret'
242 $this->linkDb
->getLinkFromUrl('https://private.tld')
246 'linkdate' => '20160225_205548',
247 'title' => 'Public stuff',
248 'url' => 'http://public.tld',
251 'tags' => 'public hello world'
253 $this->linkDb
->getLinkFromUrl('http://public.tld')
258 * Import bookmarks with the default privacy setting (reuse from file)
260 public function testImportKeepPrivacy()
262 $post = array('privacy' => 'default');
263 $files = file2array('netscape_basic.htm');
265 'File netscape_basic.htm (482 bytes) was successfully processed:'
266 .' 2 links imported, 0 links overwritten, 0 links skipped.',
267 NetscapeBookmarkUtils
::import($post, $files, $this->linkDb
, $this->pagecache
)
269 $this->assertEquals(2, count($this->linkDb
));
270 $this->assertEquals(1, count_private($this->linkDb
));
274 'linkdate' => '20001010_105536',
275 'title' => 'Secret stuff',
276 'url' => 'https://private.tld',
277 'description' => "Super-secret stuff you're not supposed to know about",
279 'tags' => 'private secret'
281 $this->linkDb
->getLinkFromUrl('https://private.tld')
285 'linkdate' => '20160225_205548',
286 'title' => 'Public stuff',
287 'url' => 'http://public.tld',
290 'tags' => 'public hello world'
292 $this->linkDb
->getLinkFromUrl('http://public.tld')
297 * Import links as public
299 public function testImportAsPublic()
301 $post = array('privacy' => 'public');
302 $files = file2array('netscape_basic.htm');
304 'File netscape_basic.htm (482 bytes) was successfully processed:'
305 .' 2 links imported, 0 links overwritten, 0 links skipped.',
306 NetscapeBookmarkUtils
::import($post, $files, $this->linkDb
, $this->pagecache
)
308 $this->assertEquals(2, count($this->linkDb
));
309 $this->assertEquals(0, count_private($this->linkDb
));
312 $this->linkDb
['20001010_105536']['private']
316 $this->linkDb
['20160225_205548']['private']
321 * Import links as private
323 public function testImportAsPrivate()
325 $post = array('privacy' => 'private');
326 $files = file2array('netscape_basic.htm');
328 'File netscape_basic.htm (482 bytes) was successfully processed:'
329 .' 2 links imported, 0 links overwritten, 0 links skipped.',
330 NetscapeBookmarkUtils
::import($post, $files, $this->linkDb
, $this->pagecache
)
332 $this->assertEquals(2, count($this->linkDb
));
333 $this->assertEquals(2, count_private($this->linkDb
));
336 $this->linkDb
['20001010_105536']['private']
340 $this->linkDb
['20160225_205548']['private']
345 * Overwrite private links so they become public
347 public function testOverwriteAsPublic()
349 $files = file2array('netscape_basic.htm');
351 // import links as private
352 $post = array('privacy' => 'private');
354 'File netscape_basic.htm (482 bytes) was successfully processed:'
355 .' 2 links imported, 0 links overwritten, 0 links skipped.',
356 NetscapeBookmarkUtils
::import($post, $files, $this->linkDb
, $this->pagecache
)
358 $this->assertEquals(2, count($this->linkDb
));
359 $this->assertEquals(2, count_private($this->linkDb
));
362 $this->linkDb
['20001010_105536']['private']
366 $this->linkDb
['20160225_205548']['private']
369 // re-import as public, enable overwriting
371 'privacy' => 'public',
372 'overwrite' => 'true'
375 'File netscape_basic.htm (482 bytes) was successfully processed:'
376 .' 2 links imported, 2 links overwritten, 0 links skipped.',
377 NetscapeBookmarkUtils
::import($post, $files, $this->linkDb
, $this->pagecache
)
379 $this->assertEquals(2, count($this->linkDb
));
380 $this->assertEquals(0, count_private($this->linkDb
));
383 $this->linkDb
['20001010_105536']['private']
387 $this->linkDb
['20160225_205548']['private']
392 * Overwrite public links so they become private
394 public function testOverwriteAsPrivate()
396 $files = file2array('netscape_basic.htm');
398 // import links as public
399 $post = array('privacy' => 'public');
401 'File netscape_basic.htm (482 bytes) was successfully processed:'
402 .' 2 links imported, 0 links overwritten, 0 links skipped.',
403 NetscapeBookmarkUtils
::import($post, $files, $this->linkDb
, $this->pagecache
)
405 $this->assertEquals(2, count($this->linkDb
));
406 $this->assertEquals(0, count_private($this->linkDb
));
409 $this->linkDb
['20001010_105536']['private']
413 $this->linkDb
['20160225_205548']['private']
416 // re-import as private, enable overwriting
418 'privacy' => 'private',
419 'overwrite' => 'true'
422 'File netscape_basic.htm (482 bytes) was successfully processed:'
423 .' 2 links imported, 2 links overwritten, 0 links skipped.',
424 NetscapeBookmarkUtils
::import($post, $files, $this->linkDb
, $this->pagecache
)
426 $this->assertEquals(2, count($this->linkDb
));
427 $this->assertEquals(2, count_private($this->linkDb
));
430 $this->linkDb
['20001010_105536']['private']
434 $this->linkDb
['20160225_205548']['private']
439 * Attept to import the same links twice without enabling overwriting
441 public function testSkipOverwrite()
443 $post = array('privacy' => 'public');
444 $files = file2array('netscape_basic.htm');
446 'File netscape_basic.htm (482 bytes) was successfully processed:'
447 .' 2 links imported, 0 links overwritten, 0 links skipped.',
448 NetscapeBookmarkUtils
::import($post, $files, $this->linkDb
, $this->pagecache
)
450 $this->assertEquals(2, count($this->linkDb
));
451 $this->assertEquals(0, count_private($this->linkDb
));
453 // re-import as private, DO NOT enable overwriting
454 $post = array('privacy' => 'private');
456 'File netscape_basic.htm (482 bytes) was successfully processed:'
457 .' 0 links imported, 0 links overwritten, 2 links skipped.',
458 NetscapeBookmarkUtils
::import($post, $files, $this->linkDb
, $this->pagecache
)
460 $this->assertEquals(2, count($this->linkDb
));
461 $this->assertEquals(0, count_private($this->linkDb
));
465 * Add user-specified tags to all imported bookmarks
467 public function testSetDefaultTags()
470 'privacy' => 'public',
471 'default_tags' => 'tag1,tag2 tag3'
473 $files = file2array('netscape_basic.htm');
475 'File netscape_basic.htm (482 bytes) was successfully processed:'
476 .' 2 links imported, 0 links overwritten, 0 links skipped.',
477 NetscapeBookmarkUtils
::import($post, $files, $this->linkDb
, $this->pagecache
)
479 $this->assertEquals(2, count($this->linkDb
));
480 $this->assertEquals(0, count_private($this->linkDb
));
482 'tag1 tag2 tag3 private secret',
483 $this->linkDb
['20001010_105536']['tags']
486 'tag1 tag2 tag3 public hello world',
487 $this->linkDb
['20160225_205548']['tags']
492 * The user-specified tags contain characters to be escaped
494 public function testSanitizeDefaultTags()
497 'privacy' => 'public',
498 'default_tags' => 'tag1&,tag2 "tag3"'
500 $files = file2array('netscape_basic.htm');
502 'File netscape_basic.htm (482 bytes) was successfully processed:'
503 .' 2 links imported, 0 links overwritten, 0 links skipped.',
504 NetscapeBookmarkUtils
::import($post, $files, $this->linkDb
, $this->pagecache
)
506 $this->assertEquals(2, count($this->linkDb
));
507 $this->assertEquals(0, count_private($this->linkDb
));
509 'tag1& tag2 "tag3" private secret',
510 $this->linkDb
['20001010_105536']['tags']
513 'tag1& tag2 "tag3" public hello world',
514 $this->linkDb
['20160225_205548']['tags']
519 * Ensure each imported bookmark has a unique linkdate
521 * See https://github.com/shaarli/Shaarli/issues/351
523 public function testImportSameDate()
525 $files = file2array('same_date.htm');
527 'File same_date.htm (453 bytes) was successfully processed:'
528 .' 3 links imported, 0 links overwritten, 0 links skipped.',
529 NetscapeBookmarkUtils
::import(array(), $files, $this->linkDb
, $this->pagecache
)
531 $this->assertEquals(3, count($this->linkDb
));
532 $this->assertEquals(0, count_private($this->linkDb
));
535 $this->linkDb
['20160225_205548']['linkdate']
539 $this->linkDb
['20160225_205549']['linkdate']
543 $this->linkDb
['20160225_205550']['linkdate']