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 * Import bookmarks nested in a folder hierarchy
88 public function testImportNested()
90 $files = file2array('netscape_nested.htm');
92 'File netscape_nested.htm (1337 bytes) was successfully processed:'
93 .' 8 links imported, 0 links overwritten, 0 links skipped.',
94 NetscapeBookmarkUtils
::import(array(), $files, $this->linkDb
, $this->pagecache
)
96 $this->assertEquals(8, count($this->linkDb
));
97 $this->assertEquals(2, count_private($this->linkDb
));
101 'linkdate' => '20160225_205541',
102 'title' => 'Nested 1',
103 'url' => 'http://nest.ed/1',
106 'tags' => 'tag1 tag2'
108 $this->linkDb
->getLinkFromUrl('http://nest.ed/1')
112 'linkdate' => '20160225_205542',
113 'title' => 'Nested 1-1',
114 'url' => 'http://nest.ed/1-1',
117 'tags' => 'folder1 tag1 tag2'
119 $this->linkDb
->getLinkFromUrl('http://nest.ed/1-1')
123 'linkdate' => '20160225_205547',
124 'title' => 'Nested 1-2',
125 'url' => 'http://nest.ed/1-2',
128 'tags' => 'folder1 tag3 tag4'
130 $this->linkDb
->getLinkFromUrl('http://nest.ed/1-2')
134 'linkdate' => '20160202_172222',
135 'title' => 'Nested 2-1',
136 'url' => 'http://nest.ed/2-1',
137 'description' => 'First link of the second section',
141 $this->linkDb
->getLinkFromUrl('http://nest.ed/2-1')
145 'linkdate' => '20160119_200227',
146 'title' => 'Nested 2-2',
147 'url' => 'http://nest.ed/2-2',
148 'description' => 'Second link of the second section',
152 $this->linkDb
->getLinkFromUrl('http://nest.ed/2-2')
156 'linkdate' => '20160202_172223',
157 'title' => 'Nested 3-1',
158 'url' => 'http://nest.ed/3-1',
161 'tags' => 'folder3 folder3-1 tag3'
163 $this->linkDb
->getLinkFromUrl('http://nest.ed/3-1')
167 'linkdate' => '20160119_200228',
168 'title' => 'Nested 3-2',
169 'url' => 'http://nest.ed/3-2',
172 'tags' => 'folder3 folder3-1'
174 $this->linkDb
->getLinkFromUrl('http://nest.ed/3-2')
178 'linkdate' => '20160229_081541',
179 'title' => 'Nested 2',
180 'url' => 'http://nest.ed/2',
185 $this->linkDb
->getLinkFromUrl('http://nest.ed/2')
190 * Import bookmarks with the default privacy setting (reuse from file)
192 * The $_POST array is not set.
194 public function testImportDefaultPrivacyNoPost()
196 $files = file2array('netscape_basic.htm');
198 'File netscape_basic.htm (482 bytes) was successfully processed:'
199 .' 2 links imported, 0 links overwritten, 0 links skipped.',
200 NetscapeBookmarkUtils
::import(array(), $files, $this->linkDb
, $this->pagecache
)
202 $this->assertEquals(2, count($this->linkDb
));
203 $this->assertEquals(1, count_private($this->linkDb
));
207 'linkdate' => '20001010_105536',
208 'title' => 'Secret stuff',
209 'url' => 'https://private.tld',
210 'description' => "Super-secret stuff you're not supposed to know about",
212 'tags' => 'private secret'
214 $this->linkDb
->getLinkFromUrl('https://private.tld')
218 'linkdate' => '20160225_205548',
219 'title' => 'Public stuff',
220 'url' => 'http://public.tld',
223 'tags' => 'public hello world'
225 $this->linkDb
->getLinkFromUrl('http://public.tld')
230 * Import bookmarks with the default privacy setting (reuse from file)
232 public function testImportKeepPrivacy()
234 $post = array('privacy' => 'default');
235 $files = file2array('netscape_basic.htm');
237 'File netscape_basic.htm (482 bytes) was successfully processed:'
238 .' 2 links imported, 0 links overwritten, 0 links skipped.',
239 NetscapeBookmarkUtils
::import($post, $files, $this->linkDb
, $this->pagecache
)
241 $this->assertEquals(2, count($this->linkDb
));
242 $this->assertEquals(1, count_private($this->linkDb
));
246 'linkdate' => '20001010_105536',
247 'title' => 'Secret stuff',
248 'url' => 'https://private.tld',
249 'description' => "Super-secret stuff you're not supposed to know about",
251 'tags' => 'private secret'
253 $this->linkDb
->getLinkFromUrl('https://private.tld')
257 'linkdate' => '20160225_205548',
258 'title' => 'Public stuff',
259 'url' => 'http://public.tld',
262 'tags' => 'public hello world'
264 $this->linkDb
->getLinkFromUrl('http://public.tld')
269 * Import links as public
271 public function testImportAsPublic()
273 $post = array('privacy' => 'public');
274 $files = file2array('netscape_basic.htm');
276 'File netscape_basic.htm (482 bytes) was successfully processed:'
277 .' 2 links imported, 0 links overwritten, 0 links skipped.',
278 NetscapeBookmarkUtils
::import($post, $files, $this->linkDb
, $this->pagecache
)
280 $this->assertEquals(2, count($this->linkDb
));
281 $this->assertEquals(0, count_private($this->linkDb
));
284 $this->linkDb
['20001010_105536']['private']
288 $this->linkDb
['20160225_205548']['private']
293 * Import links as private
295 public function testImportAsPrivate()
297 $post = array('privacy' => 'private');
298 $files = file2array('netscape_basic.htm');
300 'File netscape_basic.htm (482 bytes) was successfully processed:'
301 .' 2 links imported, 0 links overwritten, 0 links skipped.',
302 NetscapeBookmarkUtils
::import($post, $files, $this->linkDb
, $this->pagecache
)
304 $this->assertEquals(2, count($this->linkDb
));
305 $this->assertEquals(2, count_private($this->linkDb
));
308 $this->linkDb
['20001010_105536']['private']
312 $this->linkDb
['20160225_205548']['private']
317 * Overwrite private links so they become public
319 public function testOverwriteAsPublic()
321 $files = file2array('netscape_basic.htm');
323 // import links as private
324 $post = array('privacy' => 'private');
326 'File netscape_basic.htm (482 bytes) was successfully processed:'
327 .' 2 links imported, 0 links overwritten, 0 links skipped.',
328 NetscapeBookmarkUtils
::import($post, $files, $this->linkDb
, $this->pagecache
)
330 $this->assertEquals(2, count($this->linkDb
));
331 $this->assertEquals(2, count_private($this->linkDb
));
334 $this->linkDb
['20001010_105536']['private']
338 $this->linkDb
['20160225_205548']['private']
341 // re-import as public, enable overwriting
343 'privacy' => 'public',
344 'overwrite' => 'true'
347 'File netscape_basic.htm (482 bytes) was successfully processed:'
348 .' 2 links imported, 2 links overwritten, 0 links skipped.',
349 NetscapeBookmarkUtils
::import($post, $files, $this->linkDb
, $this->pagecache
)
351 $this->assertEquals(2, count($this->linkDb
));
352 $this->assertEquals(0, count_private($this->linkDb
));
355 $this->linkDb
['20001010_105536']['private']
359 $this->linkDb
['20160225_205548']['private']
364 * Overwrite public links so they become private
366 public function testOverwriteAsPrivate()
368 $files = file2array('netscape_basic.htm');
370 // import links as public
371 $post = array('privacy' => 'public');
373 'File netscape_basic.htm (482 bytes) was successfully processed:'
374 .' 2 links imported, 0 links overwritten, 0 links skipped.',
375 NetscapeBookmarkUtils
::import($post, $files, $this->linkDb
, $this->pagecache
)
377 $this->assertEquals(2, count($this->linkDb
));
378 $this->assertEquals(0, count_private($this->linkDb
));
381 $this->linkDb
['20001010_105536']['private']
385 $this->linkDb
['20160225_205548']['private']
388 // re-import as private, enable overwriting
390 'privacy' => 'private',
391 'overwrite' => 'true'
394 'File netscape_basic.htm (482 bytes) was successfully processed:'
395 .' 2 links imported, 2 links overwritten, 0 links skipped.',
396 NetscapeBookmarkUtils
::import($post, $files, $this->linkDb
, $this->pagecache
)
398 $this->assertEquals(2, count($this->linkDb
));
399 $this->assertEquals(2, count_private($this->linkDb
));
402 $this->linkDb
['20001010_105536']['private']
406 $this->linkDb
['20160225_205548']['private']
411 * Attept to import the same links twice without enabling overwriting
413 public function testSkipOverwrite()
415 $post = array('privacy' => 'public');
416 $files = file2array('netscape_basic.htm');
418 'File netscape_basic.htm (482 bytes) was successfully processed:'
419 .' 2 links imported, 0 links overwritten, 0 links skipped.',
420 NetscapeBookmarkUtils
::import($post, $files, $this->linkDb
, $this->pagecache
)
422 $this->assertEquals(2, count($this->linkDb
));
423 $this->assertEquals(0, count_private($this->linkDb
));
425 // re-import as private, DO NOT enable overwriting
426 $post = array('privacy' => 'private');
428 'File netscape_basic.htm (482 bytes) was successfully processed:'
429 .' 0 links imported, 0 links overwritten, 2 links skipped.',
430 NetscapeBookmarkUtils
::import($post, $files, $this->linkDb
, $this->pagecache
)
432 $this->assertEquals(2, count($this->linkDb
));
433 $this->assertEquals(0, count_private($this->linkDb
));
437 * Add user-specified tags to all imported bookmarks
439 public function testSetDefaultTags()
442 'privacy' => 'public',
443 'default_tags' => 'tag1,tag2 tag3'
445 $files = file2array('netscape_basic.htm');
447 'File netscape_basic.htm (482 bytes) was successfully processed:'
448 .' 2 links imported, 0 links overwritten, 0 links skipped.',
449 NetscapeBookmarkUtils
::import($post, $files, $this->linkDb
, $this->pagecache
)
451 $this->assertEquals(2, count($this->linkDb
));
452 $this->assertEquals(0, count_private($this->linkDb
));
454 'tag1 tag2 tag3 private secret',
455 $this->linkDb
['20001010_105536']['tags']
458 'tag1 tag2 tag3 public hello world',
459 $this->linkDb
['20160225_205548']['tags']
464 * The user-specified tags contain characters to be escaped
466 public function testSanitizeDefaultTags()
469 'privacy' => 'public',
470 'default_tags' => 'tag1&,tag2 "tag3"'
472 $files = file2array('netscape_basic.htm');
474 'File netscape_basic.htm (482 bytes) was successfully processed:'
475 .' 2 links imported, 0 links overwritten, 0 links skipped.',
476 NetscapeBookmarkUtils
::import($post, $files, $this->linkDb
, $this->pagecache
)
478 $this->assertEquals(2, count($this->linkDb
));
479 $this->assertEquals(0, count_private($this->linkDb
));
481 'tag1& tag2 "tag3" private secret',
482 $this->linkDb
['20001010_105536']['tags']
485 'tag1& tag2 "tag3" public hello world',
486 $this->linkDb
['20160225_205548']['tags']
491 * Ensure each imported bookmark has a unique linkdate
493 * See https://github.com/shaarli/Shaarli/issues/351
495 public function testImportSameDate()
497 $files = file2array('same_date.htm');
499 'File same_date.htm (453 bytes) was successfully processed:'
500 .' 3 links imported, 0 links overwritten, 0 links skipped.',
501 NetscapeBookmarkUtils
::import(array(), $files, $this->linkDb
, $this->pagecache
)
503 $this->assertEquals(3, count($this->linkDb
));
504 $this->assertEquals(0, count_private($this->linkDb
));
507 $this->linkDb
['20160225_205548']['linkdate']
511 $this->linkDb
['20160225_205549']['linkdate']
515 $this->linkDb
['20160225_205550']['linkdate']