]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/NetscapeBookmarkUtils/BookmarkImportTest.php
Ignore the case while checking DOCTYPE during the file import
[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 * Attempt to import bookmarks from a file with a lowercase Doctype
131 */
132 public function testImportLowecaseDoctype()
133 {
134 $files = file2array('lowercase_doctype.htm');
135 $this->assertStringMatchesFormat(
136 'File lowercase_doctype.htm (386 bytes) was successfully processed in %d seconds:'
137 .' 2 links imported, 0 links overwritten, 0 links skipped.',
138 NetscapeBookmarkUtils::import(null, $files, $this->linkDb, $this->conf, $this->history)
139 );
140 $this->assertEquals(2, count($this->linkDb));
141 }
142
143
144 /**
145 * Ensure IE dumps are supported
146 */
147 public function testImportInternetExplorerEncoding()
148 {
149 $files = file2array('internet_explorer_encoding.htm');
150 $this->assertStringMatchesFormat(
151 'File internet_explorer_encoding.htm (356 bytes) was successfully processed in %d seconds:'
152 .' 1 links imported, 0 links overwritten, 0 links skipped.',
153 NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history)
154 );
155 $this->assertEquals(1, count($this->linkDb));
156 $this->assertEquals(0, count_private($this->linkDb));
157
158 $this->assertEquals(
159 array(
160 'id' => 0,
161 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160618_203944'),
162 'title' => 'Hg Init a Mercurial tutorial by Joel Spolsky',
163 'url' => 'http://hginit.com/',
164 'description' => '',
165 'private' => 0,
166 'tags' => '',
167 'shorturl' => 'La37cg',
168 ),
169 $this->linkDb->getLinkFromUrl('http://hginit.com/')
170 );
171 }
172
173 /**
174 * Import bookmarks nested in a folder hierarchy
175 */
176 public function testImportNested()
177 {
178 $files = file2array('netscape_nested.htm');
179 $this->assertStringMatchesFormat(
180 'File netscape_nested.htm (1337 bytes) was successfully processed in %d seconds:'
181 .' 8 links imported, 0 links overwritten, 0 links skipped.',
182 NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history)
183 );
184 $this->assertEquals(8, count($this->linkDb));
185 $this->assertEquals(2, count_private($this->linkDb));
186
187 $this->assertEquals(
188 array(
189 'id' => 0,
190 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235541'),
191 'title' => 'Nested 1',
192 'url' => 'http://nest.ed/1',
193 'description' => '',
194 'private' => 0,
195 'tags' => 'tag1 tag2',
196 'shorturl' => 'KyDNKA',
197 ),
198 $this->linkDb->getLinkFromUrl('http://nest.ed/1')
199 );
200 $this->assertEquals(
201 array(
202 'id' => 1,
203 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235542'),
204 'title' => 'Nested 1-1',
205 'url' => 'http://nest.ed/1-1',
206 'description' => '',
207 'private' => 0,
208 'tags' => 'folder1 tag1 tag2',
209 'shorturl' => 'T2LnXg',
210 ),
211 $this->linkDb->getLinkFromUrl('http://nest.ed/1-1')
212 );
213 $this->assertEquals(
214 array(
215 'id' => 2,
216 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235547'),
217 'title' => 'Nested 1-2',
218 'url' => 'http://nest.ed/1-2',
219 'description' => '',
220 'private' => 0,
221 'tags' => 'folder1 tag3 tag4',
222 'shorturl' => '46SZxA',
223 ),
224 $this->linkDb->getLinkFromUrl('http://nest.ed/1-2')
225 );
226 $this->assertEquals(
227 array(
228 'id' => 3,
229 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160202_202222'),
230 'title' => 'Nested 2-1',
231 'url' => 'http://nest.ed/2-1',
232 'description' => 'First link of the second section',
233 'private' => 1,
234 'tags' => 'folder2',
235 'shorturl' => '4UHOSw',
236 ),
237 $this->linkDb->getLinkFromUrl('http://nest.ed/2-1')
238 );
239 $this->assertEquals(
240 array(
241 'id' => 4,
242 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160119_230227'),
243 'title' => 'Nested 2-2',
244 'url' => 'http://nest.ed/2-2',
245 'description' => 'Second link of the second section',
246 'private' => 1,
247 'tags' => 'folder2',
248 'shorturl' => 'yfzwbw',
249 ),
250 $this->linkDb->getLinkFromUrl('http://nest.ed/2-2')
251 );
252 $this->assertEquals(
253 array(
254 'id' => 5,
255 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160202_202222'),
256 'title' => 'Nested 3-1',
257 'url' => 'http://nest.ed/3-1',
258 'description' => '',
259 'private' => 0,
260 'tags' => 'folder3 folder3-1 tag3',
261 'shorturl' => 'UwxIUQ',
262 ),
263 $this->linkDb->getLinkFromUrl('http://nest.ed/3-1')
264 );
265 $this->assertEquals(
266 array(
267 'id' => 6,
268 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160119_230227'),
269 'title' => 'Nested 3-2',
270 'url' => 'http://nest.ed/3-2',
271 'description' => '',
272 'private' => 0,
273 'tags' => 'folder3 folder3-1',
274 'shorturl' => 'p8dyZg',
275 ),
276 $this->linkDb->getLinkFromUrl('http://nest.ed/3-2')
277 );
278 $this->assertEquals(
279 array(
280 'id' => 7,
281 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160229_111541'),
282 'title' => 'Nested 2',
283 'url' => 'http://nest.ed/2',
284 'description' => '',
285 'private' => 0,
286 'tags' => 'tag4',
287 'shorturl' => 'Gt3Uug',
288 ),
289 $this->linkDb->getLinkFromUrl('http://nest.ed/2')
290 );
291 }
292
293 /**
294 * Import bookmarks with the default privacy setting (reuse from file)
295 *
296 * The $_POST array is not set.
297 */
298 public function testImportDefaultPrivacyNoPost()
299 {
300 $files = file2array('netscape_basic.htm');
301 $this->assertStringMatchesFormat(
302 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
303 .' 2 links imported, 0 links overwritten, 0 links skipped.',
304 NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history)
305 );
306
307 $this->assertEquals(2, count($this->linkDb));
308 $this->assertEquals(1, count_private($this->linkDb));
309
310 $this->assertEquals(
311 array(
312 'id' => 0,
313 // Old link - UTC+4 (note that TZ in the import file is ignored).
314 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20001010_135536'),
315 'title' => 'Secret stuff',
316 'url' => 'https://private.tld',
317 'description' => "Super-secret stuff you're not supposed to know about",
318 'private' => 1,
319 'tags' => 'private secret',
320 'shorturl' => 'EokDtA',
321 ),
322 $this->linkDb->getLinkFromUrl('https://private.tld')
323 );
324 $this->assertEquals(
325 array(
326 'id' => 1,
327 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235548'),
328 'title' => 'Public stuff',
329 'url' => 'http://public.tld',
330 'description' => '',
331 'private' => 0,
332 'tags' => 'public hello world',
333 'shorturl' => 'Er9ddA',
334 ),
335 $this->linkDb->getLinkFromUrl('http://public.tld')
336 );
337 }
338
339 /**
340 * Import bookmarks with the default privacy setting (reuse from file)
341 */
342 public function testImportKeepPrivacy()
343 {
344 $post = array('privacy' => 'default');
345 $files = file2array('netscape_basic.htm');
346 $this->assertStringMatchesFormat(
347 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
348 .' 2 links imported, 0 links overwritten, 0 links skipped.',
349 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
350 );
351 $this->assertEquals(2, count($this->linkDb));
352 $this->assertEquals(1, count_private($this->linkDb));
353
354 $this->assertEquals(
355 array(
356 'id' => 0,
357 // Note that TZ in the import file is ignored.
358 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20001010_135536'),
359 'title' => 'Secret stuff',
360 'url' => 'https://private.tld',
361 'description' => "Super-secret stuff you're not supposed to know about",
362 'private' => 1,
363 'tags' => 'private secret',
364 'shorturl' => 'EokDtA',
365 ),
366 $this->linkDb->getLinkFromUrl('https://private.tld')
367 );
368 $this->assertEquals(
369 array(
370 'id' => 1,
371 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235548'),
372 'title' => 'Public stuff',
373 'url' => 'http://public.tld',
374 'description' => '',
375 'private' => 0,
376 'tags' => 'public hello world',
377 'shorturl' => 'Er9ddA',
378 ),
379 $this->linkDb->getLinkFromUrl('http://public.tld')
380 );
381 }
382
383 /**
384 * Import links as public
385 */
386 public function testImportAsPublic()
387 {
388 $post = array('privacy' => 'public');
389 $files = file2array('netscape_basic.htm');
390 $this->assertStringMatchesFormat(
391 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
392 .' 2 links imported, 0 links overwritten, 0 links skipped.',
393 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
394 );
395 $this->assertEquals(2, count($this->linkDb));
396 $this->assertEquals(0, count_private($this->linkDb));
397 $this->assertEquals(
398 0,
399 $this->linkDb[0]['private']
400 );
401 $this->assertEquals(
402 0,
403 $this->linkDb[1]['private']
404 );
405 }
406
407 /**
408 * Import links as private
409 */
410 public function testImportAsPrivate()
411 {
412 $post = array('privacy' => 'private');
413 $files = file2array('netscape_basic.htm');
414 $this->assertStringMatchesFormat(
415 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
416 .' 2 links imported, 0 links overwritten, 0 links skipped.',
417 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
418 );
419 $this->assertEquals(2, count($this->linkDb));
420 $this->assertEquals(2, count_private($this->linkDb));
421 $this->assertEquals(
422 1,
423 $this->linkDb['0']['private']
424 );
425 $this->assertEquals(
426 1,
427 $this->linkDb['1']['private']
428 );
429 }
430
431 /**
432 * Overwrite private links so they become public
433 */
434 public function testOverwriteAsPublic()
435 {
436 $files = file2array('netscape_basic.htm');
437
438 // import links as private
439 $post = array('privacy' => 'private');
440 $this->assertStringMatchesFormat(
441 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
442 .' 2 links imported, 0 links overwritten, 0 links skipped.',
443 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
444 );
445 $this->assertEquals(2, count($this->linkDb));
446 $this->assertEquals(2, count_private($this->linkDb));
447 $this->assertEquals(
448 1,
449 $this->linkDb[0]['private']
450 );
451 $this->assertEquals(
452 1,
453 $this->linkDb[1]['private']
454 );
455 // re-import as public, enable overwriting
456 $post = array(
457 'privacy' => 'public',
458 'overwrite' => 'true'
459 );
460 $this->assertStringMatchesFormat(
461 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
462 .' 2 links imported, 2 links overwritten, 0 links skipped.',
463 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
464 );
465 $this->assertEquals(2, count($this->linkDb));
466 $this->assertEquals(0, count_private($this->linkDb));
467 $this->assertEquals(
468 0,
469 $this->linkDb[0]['private']
470 );
471 $this->assertEquals(
472 0,
473 $this->linkDb[1]['private']
474 );
475 }
476
477 /**
478 * Overwrite public links so they become private
479 */
480 public function testOverwriteAsPrivate()
481 {
482 $files = file2array('netscape_basic.htm');
483
484 // import links as public
485 $post = array('privacy' => 'public');
486 $this->assertStringMatchesFormat(
487 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
488 .' 2 links imported, 0 links overwritten, 0 links skipped.',
489 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
490 );
491 $this->assertEquals(2, count($this->linkDb));
492 $this->assertEquals(0, count_private($this->linkDb));
493 $this->assertEquals(
494 0,
495 $this->linkDb['0']['private']
496 );
497 $this->assertEquals(
498 0,
499 $this->linkDb['1']['private']
500 );
501
502 // re-import as private, enable overwriting
503 $post = array(
504 'privacy' => 'private',
505 'overwrite' => 'true'
506 );
507 $this->assertStringMatchesFormat(
508 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
509 .' 2 links imported, 2 links overwritten, 0 links skipped.',
510 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
511 );
512 $this->assertEquals(2, count($this->linkDb));
513 $this->assertEquals(2, count_private($this->linkDb));
514 $this->assertEquals(
515 1,
516 $this->linkDb['0']['private']
517 );
518 $this->assertEquals(
519 1,
520 $this->linkDb['1']['private']
521 );
522 }
523
524 /**
525 * Attept to import the same links twice without enabling overwriting
526 */
527 public function testSkipOverwrite()
528 {
529 $post = array('privacy' => 'public');
530 $files = file2array('netscape_basic.htm');
531 $this->assertStringMatchesFormat(
532 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
533 .' 2 links imported, 0 links overwritten, 0 links skipped.',
534 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
535 );
536 $this->assertEquals(2, count($this->linkDb));
537 $this->assertEquals(0, count_private($this->linkDb));
538
539 // re-import as private, DO NOT enable overwriting
540 $post = array('privacy' => 'private');
541 $this->assertStringMatchesFormat(
542 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
543 .' 0 links imported, 0 links overwritten, 2 links skipped.',
544 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
545 );
546 $this->assertEquals(2, count($this->linkDb));
547 $this->assertEquals(0, count_private($this->linkDb));
548 }
549
550 /**
551 * Add user-specified tags to all imported bookmarks
552 */
553 public function testSetDefaultTags()
554 {
555 $post = array(
556 'privacy' => 'public',
557 'default_tags' => 'tag1,tag2 tag3'
558 );
559 $files = file2array('netscape_basic.htm');
560 $this->assertStringMatchesFormat(
561 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
562 .' 2 links imported, 0 links overwritten, 0 links skipped.',
563 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
564 );
565 $this->assertEquals(2, count($this->linkDb));
566 $this->assertEquals(0, count_private($this->linkDb));
567 $this->assertEquals(
568 'tag1 tag2 tag3 private secret',
569 $this->linkDb['0']['tags']
570 );
571 $this->assertEquals(
572 'tag1 tag2 tag3 public hello world',
573 $this->linkDb['1']['tags']
574 );
575 }
576
577 /**
578 * The user-specified tags contain characters to be escaped
579 */
580 public function testSanitizeDefaultTags()
581 {
582 $post = array(
583 'privacy' => 'public',
584 'default_tags' => 'tag1&,tag2 "tag3"'
585 );
586 $files = file2array('netscape_basic.htm');
587 $this->assertStringMatchesFormat(
588 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
589 .' 2 links imported, 0 links overwritten, 0 links skipped.',
590 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
591 );
592 $this->assertEquals(2, count($this->linkDb));
593 $this->assertEquals(0, count_private($this->linkDb));
594 $this->assertEquals(
595 'tag1&amp; tag2 &quot;tag3&quot; private secret',
596 $this->linkDb['0']['tags']
597 );
598 $this->assertEquals(
599 'tag1&amp; tag2 &quot;tag3&quot; public hello world',
600 $this->linkDb['1']['tags']
601 );
602 }
603
604 /**
605 * Ensure each imported bookmark has a unique id
606 *
607 * See https://github.com/shaarli/Shaarli/issues/351
608 */
609 public function testImportSameDate()
610 {
611 $files = file2array('same_date.htm');
612 $this->assertStringMatchesFormat(
613 'File same_date.htm (453 bytes) was successfully processed in %d seconds:'
614 .' 3 links imported, 0 links overwritten, 0 links skipped.',
615 NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->conf, $this->history)
616 );
617 $this->assertEquals(3, count($this->linkDb));
618 $this->assertEquals(0, count_private($this->linkDb));
619 $this->assertEquals(
620 0,
621 $this->linkDb[0]['id']
622 );
623 $this->assertEquals(
624 1,
625 $this->linkDb[1]['id']
626 );
627 $this->assertEquals(
628 2,
629 $this->linkDb[2]['id']
630 );
631 }
632
633 public function testImportCreateUpdateHistory()
634 {
635 $post = [
636 'privacy' => 'public',
637 'overwrite' => 'true',
638 ];
639 $files = file2array('netscape_basic.htm');
640 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history);
641 $history = $this->history->getHistory();
642 $this->assertEquals(1, count($history));
643 $this->assertEquals(History::IMPORT, $history[0]['event']);
644 $this->assertTrue(new DateTime('-5 seconds') < $history[0]['datetime']);
645
646 // re-import as private, enable overwriting
647 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history);
648 $history = $this->history->getHistory();
649 $this->assertEquals(2, count($history));
650 $this->assertEquals(History::IMPORT, $history[0]['event']);
651 $this->assertTrue(new DateTime('-5 seconds') < $history[0]['datetime']);
652 $this->assertEquals(History::IMPORT, $history[1]['event']);
653 $this->assertTrue(new DateTime('-5 seconds') < $history[1]['datetime']);
654 }
655 }