]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/NetscapeBookmarkUtils/BookmarkImportTest.php
4961aa2c4f73166d069144776cb574db9a537f67
[github/shaarli/Shaarli.git] / tests / NetscapeBookmarkUtils / BookmarkImportTest.php
1 <?php
2
3 require_once 'application/NetscapeBookmarkUtils.php';
4
5 use Shaarli\Config\ConfigManager;
6
7 /**
8 * Utility function to load a file's metadata in a $_FILES-like array
9 *
10 * @param string $filename Basename of the file
11 *
12 * @return array A $_FILES-like array
13 */
14 function file2array($filename)
15 {
16 return array(
17 'filetoupload' => array(
18 'name' => $filename,
19 'tmp_name' => __DIR__ . '/input/' . $filename,
20 'size' => filesize(__DIR__ . '/input/' . $filename)
21 )
22 );
23 }
24
25
26 /**
27 * Netscape bookmark import
28 */
29 class BookmarkImportTest extends PHPUnit_Framework_TestCase
30 {
31 /**
32 * @var string datastore to test write operations
33 */
34 protected static $testDatastore = 'sandbox/datastore.php';
35
36 /**
37 * @var string History file path
38 */
39 protected static $historyFilePath = 'sandbox/history.php';
40
41 /**
42 * @var LinkDB private LinkDB instance
43 */
44 protected $linkDb = null;
45
46 /**
47 * @var string Dummy page cache
48 */
49 protected $pagecache = 'tests';
50
51 /**
52 * @var ConfigManager instance.
53 */
54 protected $conf;
55
56 /**
57 * @var History instance.
58 */
59 protected $history;
60
61 /**
62 * @var string Save the current timezone.
63 */
64 protected static $defaultTimeZone;
65
66 public static function setUpBeforeClass()
67 {
68 self::$defaultTimeZone = date_default_timezone_get();
69 // Timezone without DST for test consistency
70 date_default_timezone_set('Africa/Nairobi');
71 }
72
73 /**
74 * Resets test data before each test
75 */
76 protected function setUp()
77 {
78 if (file_exists(self::$testDatastore)) {
79 unlink(self::$testDatastore);
80 }
81 // start with an empty datastore
82 file_put_contents(self::$testDatastore, '<?php /* S7QysKquBQA= */ ?>');
83 $this->linkDb = new LinkDB(self::$testDatastore, true, false);
84 $this->conf = new ConfigManager('tests/utils/config/configJson');
85 $this->conf->set('resource.page_cache', $this->pagecache);
86 $this->history = new History(self::$historyFilePath);
87 }
88
89 /**
90 * Delete history file.
91 */
92 public function tearDown()
93 {
94 @unlink(self::$historyFilePath);
95 }
96
97 public static function tearDownAfterClass()
98 {
99 date_default_timezone_set(self::$defaultTimeZone);
100 }
101
102 /**
103 * Attempt to import bookmarks from an empty file
104 */
105 public function testImportEmptyData()
106 {
107 $files = file2array('empty.htm');
108 $this->assertEquals(
109 'File empty.htm (0 bytes) has an unknown file format.'
110 .' Nothing was imported.',
111 NetscapeBookmarkUtils::import(null, $files, null, $this->conf, $this->history)
112 );
113 $this->assertEquals(0, count($this->linkDb));
114 }
115
116 /**
117 * Attempt to import bookmarks from a file with no Doctype
118 */
119 public function testImportNoDoctype()
120 {
121 $files = file2array('no_doctype.htm');
122 $this->assertEquals(
123 'File no_doctype.htm (350 bytes) has an unknown file format. Nothing was imported.',
124 NetscapeBookmarkUtils::import(null, $files, null, $this->conf, $this->history)
125 );
126 $this->assertEquals(0, count($this->linkDb));
127 }
128
129 /**
130 * Ensure IE dumps are supported
131 */
132 public function testImportInternetExplorerEncoding()
133 {
134 $files = file2array('internet_explorer_encoding.htm');
135 $this->assertStringMatchesFormat(
136 'File internet_explorer_encoding.htm (356 bytes) was successfully processed in %d seconds:'
137 .' 1 links imported, 0 links overwritten, 0 links skipped.',
138 NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history)
139 );
140 $this->assertEquals(1, count($this->linkDb));
141 $this->assertEquals(0, count_private($this->linkDb));
142
143 $this->assertEquals(
144 array(
145 'id' => 0,
146 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160618_203944'),
147 'title' => 'Hg Init a Mercurial tutorial by Joel Spolsky',
148 'url' => 'http://hginit.com/',
149 'description' => '',
150 'private' => 0,
151 'tags' => '',
152 'shorturl' => 'La37cg',
153 ),
154 $this->linkDb->getLinkFromUrl('http://hginit.com/')
155 );
156 }
157
158 /**
159 * Import bookmarks nested in a folder hierarchy
160 */
161 public function testImportNested()
162 {
163 $files = file2array('netscape_nested.htm');
164 $this->assertStringMatchesFormat(
165 'File netscape_nested.htm (1337 bytes) was successfully processed in %d seconds:'
166 .' 8 links imported, 0 links overwritten, 0 links skipped.',
167 NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history)
168 );
169 $this->assertEquals(8, count($this->linkDb));
170 $this->assertEquals(2, count_private($this->linkDb));
171
172 $this->assertEquals(
173 array(
174 'id' => 0,
175 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235541'),
176 'title' => 'Nested 1',
177 'url' => 'http://nest.ed/1',
178 'description' => '',
179 'private' => 0,
180 'tags' => 'tag1 tag2',
181 'shorturl' => 'KyDNKA',
182 ),
183 $this->linkDb->getLinkFromUrl('http://nest.ed/1')
184 );
185 $this->assertEquals(
186 array(
187 'id' => 1,
188 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235542'),
189 'title' => 'Nested 1-1',
190 'url' => 'http://nest.ed/1-1',
191 'description' => '',
192 'private' => 0,
193 'tags' => 'folder1 tag1 tag2',
194 'shorturl' => 'T2LnXg',
195 ),
196 $this->linkDb->getLinkFromUrl('http://nest.ed/1-1')
197 );
198 $this->assertEquals(
199 array(
200 'id' => 2,
201 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235547'),
202 'title' => 'Nested 1-2',
203 'url' => 'http://nest.ed/1-2',
204 'description' => '',
205 'private' => 0,
206 'tags' => 'folder1 tag3 tag4',
207 'shorturl' => '46SZxA',
208 ),
209 $this->linkDb->getLinkFromUrl('http://nest.ed/1-2')
210 );
211 $this->assertEquals(
212 array(
213 'id' => 3,
214 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160202_202222'),
215 'title' => 'Nested 2-1',
216 'url' => 'http://nest.ed/2-1',
217 'description' => 'First link of the second section',
218 'private' => 1,
219 'tags' => 'folder2',
220 'shorturl' => '4UHOSw',
221 ),
222 $this->linkDb->getLinkFromUrl('http://nest.ed/2-1')
223 );
224 $this->assertEquals(
225 array(
226 'id' => 4,
227 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160119_230227'),
228 'title' => 'Nested 2-2',
229 'url' => 'http://nest.ed/2-2',
230 'description' => 'Second link of the second section',
231 'private' => 1,
232 'tags' => 'folder2',
233 'shorturl' => 'yfzwbw',
234 ),
235 $this->linkDb->getLinkFromUrl('http://nest.ed/2-2')
236 );
237 $this->assertEquals(
238 array(
239 'id' => 5,
240 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160202_202222'),
241 'title' => 'Nested 3-1',
242 'url' => 'http://nest.ed/3-1',
243 'description' => '',
244 'private' => 0,
245 'tags' => 'folder3 folder3-1 tag3',
246 'shorturl' => 'UwxIUQ',
247 ),
248 $this->linkDb->getLinkFromUrl('http://nest.ed/3-1')
249 );
250 $this->assertEquals(
251 array(
252 'id' => 6,
253 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160119_230227'),
254 'title' => 'Nested 3-2',
255 'url' => 'http://nest.ed/3-2',
256 'description' => '',
257 'private' => 0,
258 'tags' => 'folder3 folder3-1',
259 'shorturl' => 'p8dyZg',
260 ),
261 $this->linkDb->getLinkFromUrl('http://nest.ed/3-2')
262 );
263 $this->assertEquals(
264 array(
265 'id' => 7,
266 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160229_111541'),
267 'title' => 'Nested 2',
268 'url' => 'http://nest.ed/2',
269 'description' => '',
270 'private' => 0,
271 'tags' => 'tag4',
272 'shorturl' => 'Gt3Uug',
273 ),
274 $this->linkDb->getLinkFromUrl('http://nest.ed/2')
275 );
276 }
277
278 /**
279 * Import bookmarks with the default privacy setting (reuse from file)
280 *
281 * The $_POST array is not set.
282 */
283 public function testImportDefaultPrivacyNoPost()
284 {
285 $files = file2array('netscape_basic.htm');
286 $this->assertStringMatchesFormat(
287 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
288 .' 2 links imported, 0 links overwritten, 0 links skipped.',
289 NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history)
290 );
291
292 $this->assertEquals(2, count($this->linkDb));
293 $this->assertEquals(1, count_private($this->linkDb));
294
295 $this->assertEquals(
296 array(
297 'id' => 0,
298 // Old link - UTC+4 (note that TZ in the import file is ignored).
299 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20001010_135536'),
300 'title' => 'Secret stuff',
301 'url' => 'https://private.tld',
302 'description' => "Super-secret stuff you're not supposed to know about",
303 'private' => 1,
304 'tags' => 'private secret',
305 'shorturl' => 'EokDtA',
306 ),
307 $this->linkDb->getLinkFromUrl('https://private.tld')
308 );
309 $this->assertEquals(
310 array(
311 'id' => 1,
312 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235548'),
313 'title' => 'Public stuff',
314 'url' => 'http://public.tld',
315 'description' => '',
316 'private' => 0,
317 'tags' => 'public hello world',
318 'shorturl' => 'Er9ddA',
319 ),
320 $this->linkDb->getLinkFromUrl('http://public.tld')
321 );
322 }
323
324 /**
325 * Import bookmarks with the default privacy setting (reuse from file)
326 */
327 public function testImportKeepPrivacy()
328 {
329 $post = array('privacy' => 'default');
330 $files = file2array('netscape_basic.htm');
331 $this->assertStringMatchesFormat(
332 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
333 .' 2 links imported, 0 links overwritten, 0 links skipped.',
334 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
335 );
336 $this->assertEquals(2, count($this->linkDb));
337 $this->assertEquals(1, count_private($this->linkDb));
338
339 $this->assertEquals(
340 array(
341 'id' => 0,
342 // Note that TZ in the import file is ignored.
343 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20001010_135536'),
344 'title' => 'Secret stuff',
345 'url' => 'https://private.tld',
346 'description' => "Super-secret stuff you're not supposed to know about",
347 'private' => 1,
348 'tags' => 'private secret',
349 'shorturl' => 'EokDtA',
350 ),
351 $this->linkDb->getLinkFromUrl('https://private.tld')
352 );
353 $this->assertEquals(
354 array(
355 'id' => 1,
356 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235548'),
357 'title' => 'Public stuff',
358 'url' => 'http://public.tld',
359 'description' => '',
360 'private' => 0,
361 'tags' => 'public hello world',
362 'shorturl' => 'Er9ddA',
363 ),
364 $this->linkDb->getLinkFromUrl('http://public.tld')
365 );
366 }
367
368 /**
369 * Import links as public
370 */
371 public function testImportAsPublic()
372 {
373 $post = array('privacy' => 'public');
374 $files = file2array('netscape_basic.htm');
375 $this->assertStringMatchesFormat(
376 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
377 .' 2 links imported, 0 links overwritten, 0 links skipped.',
378 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
379 );
380 $this->assertEquals(2, count($this->linkDb));
381 $this->assertEquals(0, count_private($this->linkDb));
382 $this->assertEquals(
383 0,
384 $this->linkDb[0]['private']
385 );
386 $this->assertEquals(
387 0,
388 $this->linkDb[1]['private']
389 );
390 }
391
392 /**
393 * Import links as private
394 */
395 public function testImportAsPrivate()
396 {
397 $post = array('privacy' => 'private');
398 $files = file2array('netscape_basic.htm');
399 $this->assertStringMatchesFormat(
400 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
401 .' 2 links imported, 0 links overwritten, 0 links skipped.',
402 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
403 );
404 $this->assertEquals(2, count($this->linkDb));
405 $this->assertEquals(2, count_private($this->linkDb));
406 $this->assertEquals(
407 1,
408 $this->linkDb['0']['private']
409 );
410 $this->assertEquals(
411 1,
412 $this->linkDb['1']['private']
413 );
414 }
415
416 /**
417 * Overwrite private links so they become public
418 */
419 public function testOverwriteAsPublic()
420 {
421 $files = file2array('netscape_basic.htm');
422
423 // import links as private
424 $post = array('privacy' => 'private');
425 $this->assertStringMatchesFormat(
426 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
427 .' 2 links imported, 0 links overwritten, 0 links skipped.',
428 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
429 );
430 $this->assertEquals(2, count($this->linkDb));
431 $this->assertEquals(2, count_private($this->linkDb));
432 $this->assertEquals(
433 1,
434 $this->linkDb[0]['private']
435 );
436 $this->assertEquals(
437 1,
438 $this->linkDb[1]['private']
439 );
440 // re-import as public, enable overwriting
441 $post = array(
442 'privacy' => 'public',
443 'overwrite' => 'true'
444 );
445 $this->assertStringMatchesFormat(
446 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
447 .' 2 links imported, 2 links overwritten, 0 links skipped.',
448 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
449 );
450 $this->assertEquals(2, count($this->linkDb));
451 $this->assertEquals(0, count_private($this->linkDb));
452 $this->assertEquals(
453 0,
454 $this->linkDb[0]['private']
455 );
456 $this->assertEquals(
457 0,
458 $this->linkDb[1]['private']
459 );
460 }
461
462 /**
463 * Overwrite public links so they become private
464 */
465 public function testOverwriteAsPrivate()
466 {
467 $files = file2array('netscape_basic.htm');
468
469 // import links as public
470 $post = array('privacy' => 'public');
471 $this->assertStringMatchesFormat(
472 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
473 .' 2 links imported, 0 links overwritten, 0 links skipped.',
474 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
475 );
476 $this->assertEquals(2, count($this->linkDb));
477 $this->assertEquals(0, count_private($this->linkDb));
478 $this->assertEquals(
479 0,
480 $this->linkDb['0']['private']
481 );
482 $this->assertEquals(
483 0,
484 $this->linkDb['1']['private']
485 );
486
487 // re-import as private, enable overwriting
488 $post = array(
489 'privacy' => 'private',
490 'overwrite' => 'true'
491 );
492 $this->assertStringMatchesFormat(
493 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
494 .' 2 links imported, 2 links overwritten, 0 links skipped.',
495 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
496 );
497 $this->assertEquals(2, count($this->linkDb));
498 $this->assertEquals(2, count_private($this->linkDb));
499 $this->assertEquals(
500 1,
501 $this->linkDb['0']['private']
502 );
503 $this->assertEquals(
504 1,
505 $this->linkDb['1']['private']
506 );
507 }
508
509 /**
510 * Attept to import the same links twice without enabling overwriting
511 */
512 public function testSkipOverwrite()
513 {
514 $post = array('privacy' => 'public');
515 $files = file2array('netscape_basic.htm');
516 $this->assertStringMatchesFormat(
517 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
518 .' 2 links imported, 0 links overwritten, 0 links skipped.',
519 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
520 );
521 $this->assertEquals(2, count($this->linkDb));
522 $this->assertEquals(0, count_private($this->linkDb));
523
524 // re-import as private, DO NOT enable overwriting
525 $post = array('privacy' => 'private');
526 $this->assertStringMatchesFormat(
527 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
528 .' 0 links imported, 0 links overwritten, 2 links skipped.',
529 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
530 );
531 $this->assertEquals(2, count($this->linkDb));
532 $this->assertEquals(0, count_private($this->linkDb));
533 }
534
535 /**
536 * Add user-specified tags to all imported bookmarks
537 */
538 public function testSetDefaultTags()
539 {
540 $post = array(
541 'privacy' => 'public',
542 'default_tags' => 'tag1,tag2 tag3'
543 );
544 $files = file2array('netscape_basic.htm');
545 $this->assertStringMatchesFormat(
546 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
547 .' 2 links imported, 0 links overwritten, 0 links skipped.',
548 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
549 );
550 $this->assertEquals(2, count($this->linkDb));
551 $this->assertEquals(0, count_private($this->linkDb));
552 $this->assertEquals(
553 'tag1 tag2 tag3 private secret',
554 $this->linkDb['0']['tags']
555 );
556 $this->assertEquals(
557 'tag1 tag2 tag3 public hello world',
558 $this->linkDb['1']['tags']
559 );
560 }
561
562 /**
563 * The user-specified tags contain characters to be escaped
564 */
565 public function testSanitizeDefaultTags()
566 {
567 $post = array(
568 'privacy' => 'public',
569 'default_tags' => 'tag1&,tag2 "tag3"'
570 );
571 $files = file2array('netscape_basic.htm');
572 $this->assertStringMatchesFormat(
573 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
574 .' 2 links imported, 0 links overwritten, 0 links skipped.',
575 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
576 );
577 $this->assertEquals(2, count($this->linkDb));
578 $this->assertEquals(0, count_private($this->linkDb));
579 $this->assertEquals(
580 'tag1&amp; tag2 &quot;tag3&quot; private secret',
581 $this->linkDb['0']['tags']
582 );
583 $this->assertEquals(
584 'tag1&amp; tag2 &quot;tag3&quot; public hello world',
585 $this->linkDb['1']['tags']
586 );
587 }
588
589 /**
590 * Ensure each imported bookmark has a unique id
591 *
592 * See https://github.com/shaarli/Shaarli/issues/351
593 */
594 public function testImportSameDate()
595 {
596 $files = file2array('same_date.htm');
597 $this->assertStringMatchesFormat(
598 'File same_date.htm (453 bytes) was successfully processed in %d seconds:'
599 .' 3 links imported, 0 links overwritten, 0 links skipped.',
600 NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->conf, $this->history)
601 );
602 $this->assertEquals(3, count($this->linkDb));
603 $this->assertEquals(0, count_private($this->linkDb));
604 $this->assertEquals(
605 0,
606 $this->linkDb[0]['id']
607 );
608 $this->assertEquals(
609 1,
610 $this->linkDb[1]['id']
611 );
612 $this->assertEquals(
613 2,
614 $this->linkDb[2]['id']
615 );
616 }
617
618 public function testImportCreateUpdateHistory()
619 {
620 $post = [
621 'privacy' => 'public',
622 'overwrite' => 'true',
623 ];
624 $files = file2array('netscape_basic.htm');
625 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history);
626 $history = $this->history->getHistory();
627 $this->assertEquals(1, count($history));
628 $this->assertEquals(History::IMPORT, $history[0]['event']);
629 $this->assertTrue(new DateTime('-5 seconds') < $history[0]['datetime']);
630
631 // re-import as private, enable overwriting
632 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history);
633 $history = $this->history->getHistory();
634 $this->assertEquals(2, count($history));
635 $this->assertEquals(History::IMPORT, $history[0]['event']);
636 $this->assertTrue(new DateTime('-5 seconds') < $history[0]['datetime']);
637 $this->assertEquals(History::IMPORT, $history[1]['event']);
638 $this->assertTrue(new DateTime('-5 seconds') < $history[1]['datetime']);
639 }
640 }