aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/NetscapeBookmarkUtils/BookmarkImportTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/NetscapeBookmarkUtils/BookmarkImportTest.php')
-rw-r--r--tests/NetscapeBookmarkUtils/BookmarkImportTest.php144
1 files changed, 97 insertions, 47 deletions
diff --git a/tests/NetscapeBookmarkUtils/BookmarkImportTest.php b/tests/NetscapeBookmarkUtils/BookmarkImportTest.php
index 0ca07eac..4961aa2c 100644
--- a/tests/NetscapeBookmarkUtils/BookmarkImportTest.php
+++ b/tests/NetscapeBookmarkUtils/BookmarkImportTest.php
@@ -2,6 +2,7 @@
2 2
3require_once 'application/NetscapeBookmarkUtils.php'; 3require_once 'application/NetscapeBookmarkUtils.php';
4 4
5use Shaarli\Config\ConfigManager;
5 6
6/** 7/**
7 * Utility function to load a file's metadata in a $_FILES-like array 8 * Utility function to load a file's metadata in a $_FILES-like array
@@ -33,6 +34,11 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
33 protected static $testDatastore = 'sandbox/datastore.php'; 34 protected static $testDatastore = 'sandbox/datastore.php';
34 35
35 /** 36 /**
37 * @var string History file path
38 */
39 protected static $historyFilePath = 'sandbox/history.php';
40
41 /**
36 * @var LinkDB private LinkDB instance 42 * @var LinkDB private LinkDB instance
37 */ 43 */
38 protected $linkDb = null; 44 protected $linkDb = null;
@@ -43,6 +49,16 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
43 protected $pagecache = 'tests'; 49 protected $pagecache = 'tests';
44 50
45 /** 51 /**
52 * @var ConfigManager instance.
53 */
54 protected $conf;
55
56 /**
57 * @var History instance.
58 */
59 protected $history;
60
61 /**
46 * @var string Save the current timezone. 62 * @var string Save the current timezone.
47 */ 63 */
48 protected static $defaultTimeZone; 64 protected static $defaultTimeZone;
@@ -65,6 +81,17 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
65 // start with an empty datastore 81 // start with an empty datastore
66 file_put_contents(self::$testDatastore, '<?php /* S7QysKquBQA= */ ?>'); 82 file_put_contents(self::$testDatastore, '<?php /* S7QysKquBQA= */ ?>');
67 $this->linkDb = new LinkDB(self::$testDatastore, true, false); 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);
68 } 95 }
69 96
70 public static function tearDownAfterClass() 97 public static function tearDownAfterClass()
@@ -81,7 +108,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
81 $this->assertEquals( 108 $this->assertEquals(
82 'File empty.htm (0 bytes) has an unknown file format.' 109 'File empty.htm (0 bytes) has an unknown file format.'
83 .' Nothing was imported.', 110 .' Nothing was imported.',
84 NetscapeBookmarkUtils::import(NULL, $files, NULL, NULL) 111 NetscapeBookmarkUtils::import(null, $files, null, $this->conf, $this->history)
85 ); 112 );
86 $this->assertEquals(0, count($this->linkDb)); 113 $this->assertEquals(0, count($this->linkDb));
87 } 114 }
@@ -94,7 +121,7 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
94 $files = file2array('no_doctype.htm'); 121 $files = file2array('no_doctype.htm');
95 $this->assertEquals( 122 $this->assertEquals(
96 'File no_doctype.htm (350 bytes) has an unknown file format. Nothing was imported.', 123 'File no_doctype.htm (350 bytes) has an unknown file format. Nothing was imported.',
97 NetscapeBookmarkUtils::import(NULL, $files, NULL, NULL) 124 NetscapeBookmarkUtils::import(null, $files, null, $this->conf, $this->history)
98 ); 125 );
99 $this->assertEquals(0, count($this->linkDb)); 126 $this->assertEquals(0, count($this->linkDb));
100 } 127 }
@@ -105,10 +132,10 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
105 public function testImportInternetExplorerEncoding() 132 public function testImportInternetExplorerEncoding()
106 { 133 {
107 $files = file2array('internet_explorer_encoding.htm'); 134 $files = file2array('internet_explorer_encoding.htm');
108 $this->assertEquals( 135 $this->assertStringMatchesFormat(
109 'File internet_explorer_encoding.htm (356 bytes) was successfully processed:' 136 'File internet_explorer_encoding.htm (356 bytes) was successfully processed in %d seconds:'
110 .' 1 links imported, 0 links overwritten, 0 links skipped.', 137 .' 1 links imported, 0 links overwritten, 0 links skipped.',
111 NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->pagecache) 138 NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history)
112 ); 139 );
113 $this->assertEquals(1, count($this->linkDb)); 140 $this->assertEquals(1, count($this->linkDb));
114 $this->assertEquals(0, count_private($this->linkDb)); 141 $this->assertEquals(0, count_private($this->linkDb));
@@ -134,10 +161,10 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
134 public function testImportNested() 161 public function testImportNested()
135 { 162 {
136 $files = file2array('netscape_nested.htm'); 163 $files = file2array('netscape_nested.htm');
137 $this->assertEquals( 164 $this->assertStringMatchesFormat(
138 'File netscape_nested.htm (1337 bytes) was successfully processed:' 165 'File netscape_nested.htm (1337 bytes) was successfully processed in %d seconds:'
139 .' 8 links imported, 0 links overwritten, 0 links skipped.', 166 .' 8 links imported, 0 links overwritten, 0 links skipped.',
140 NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->pagecache) 167 NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history)
141 ); 168 );
142 $this->assertEquals(8, count($this->linkDb)); 169 $this->assertEquals(8, count($this->linkDb));
143 $this->assertEquals(2, count_private($this->linkDb)); 170 $this->assertEquals(2, count_private($this->linkDb));
@@ -256,10 +283,10 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
256 public function testImportDefaultPrivacyNoPost() 283 public function testImportDefaultPrivacyNoPost()
257 { 284 {
258 $files = file2array('netscape_basic.htm'); 285 $files = file2array('netscape_basic.htm');
259 $this->assertEquals( 286 $this->assertStringMatchesFormat(
260 'File netscape_basic.htm (482 bytes) was successfully processed:' 287 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
261 .' 2 links imported, 0 links overwritten, 0 links skipped.', 288 .' 2 links imported, 0 links overwritten, 0 links skipped.',
262 NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->pagecache) 289 NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history)
263 ); 290 );
264 291
265 $this->assertEquals(2, count($this->linkDb)); 292 $this->assertEquals(2, count($this->linkDb));
@@ -301,10 +328,10 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
301 { 328 {
302 $post = array('privacy' => 'default'); 329 $post = array('privacy' => 'default');
303 $files = file2array('netscape_basic.htm'); 330 $files = file2array('netscape_basic.htm');
304 $this->assertEquals( 331 $this->assertStringMatchesFormat(
305 'File netscape_basic.htm (482 bytes) was successfully processed:' 332 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
306 .' 2 links imported, 0 links overwritten, 0 links skipped.', 333 .' 2 links imported, 0 links overwritten, 0 links skipped.',
307 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache) 334 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
308 ); 335 );
309 $this->assertEquals(2, count($this->linkDb)); 336 $this->assertEquals(2, count($this->linkDb));
310 $this->assertEquals(1, count_private($this->linkDb)); 337 $this->assertEquals(1, count_private($this->linkDb));
@@ -345,10 +372,10 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
345 { 372 {
346 $post = array('privacy' => 'public'); 373 $post = array('privacy' => 'public');
347 $files = file2array('netscape_basic.htm'); 374 $files = file2array('netscape_basic.htm');
348 $this->assertEquals( 375 $this->assertStringMatchesFormat(
349 'File netscape_basic.htm (482 bytes) was successfully processed:' 376 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
350 .' 2 links imported, 0 links overwritten, 0 links skipped.', 377 .' 2 links imported, 0 links overwritten, 0 links skipped.',
351 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache) 378 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
352 ); 379 );
353 $this->assertEquals(2, count($this->linkDb)); 380 $this->assertEquals(2, count($this->linkDb));
354 $this->assertEquals(0, count_private($this->linkDb)); 381 $this->assertEquals(0, count_private($this->linkDb));
@@ -369,10 +396,10 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
369 { 396 {
370 $post = array('privacy' => 'private'); 397 $post = array('privacy' => 'private');
371 $files = file2array('netscape_basic.htm'); 398 $files = file2array('netscape_basic.htm');
372 $this->assertEquals( 399 $this->assertStringMatchesFormat(
373 'File netscape_basic.htm (482 bytes) was successfully processed:' 400 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
374 .' 2 links imported, 0 links overwritten, 0 links skipped.', 401 .' 2 links imported, 0 links overwritten, 0 links skipped.',
375 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache) 402 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
376 ); 403 );
377 $this->assertEquals(2, count($this->linkDb)); 404 $this->assertEquals(2, count($this->linkDb));
378 $this->assertEquals(2, count_private($this->linkDb)); 405 $this->assertEquals(2, count_private($this->linkDb));
@@ -395,10 +422,10 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
395 422
396 // import links as private 423 // import links as private
397 $post = array('privacy' => 'private'); 424 $post = array('privacy' => 'private');
398 $this->assertEquals( 425 $this->assertStringMatchesFormat(
399 'File netscape_basic.htm (482 bytes) was successfully processed:' 426 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
400 .' 2 links imported, 0 links overwritten, 0 links skipped.', 427 .' 2 links imported, 0 links overwritten, 0 links skipped.',
401 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache) 428 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
402 ); 429 );
403 $this->assertEquals(2, count($this->linkDb)); 430 $this->assertEquals(2, count($this->linkDb));
404 $this->assertEquals(2, count_private($this->linkDb)); 431 $this->assertEquals(2, count_private($this->linkDb));
@@ -415,10 +442,10 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
415 'privacy' => 'public', 442 'privacy' => 'public',
416 'overwrite' => 'true' 443 'overwrite' => 'true'
417 ); 444 );
418 $this->assertEquals( 445 $this->assertStringMatchesFormat(
419 'File netscape_basic.htm (482 bytes) was successfully processed:' 446 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
420 .' 2 links imported, 2 links overwritten, 0 links skipped.', 447 .' 2 links imported, 2 links overwritten, 0 links skipped.',
421 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache) 448 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
422 ); 449 );
423 $this->assertEquals(2, count($this->linkDb)); 450 $this->assertEquals(2, count($this->linkDb));
424 $this->assertEquals(0, count_private($this->linkDb)); 451 $this->assertEquals(0, count_private($this->linkDb));
@@ -441,10 +468,10 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
441 468
442 // import links as public 469 // import links as public
443 $post = array('privacy' => 'public'); 470 $post = array('privacy' => 'public');
444 $this->assertEquals( 471 $this->assertStringMatchesFormat(
445 'File netscape_basic.htm (482 bytes) was successfully processed:' 472 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
446 .' 2 links imported, 0 links overwritten, 0 links skipped.', 473 .' 2 links imported, 0 links overwritten, 0 links skipped.',
447 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache) 474 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
448 ); 475 );
449 $this->assertEquals(2, count($this->linkDb)); 476 $this->assertEquals(2, count($this->linkDb));
450 $this->assertEquals(0, count_private($this->linkDb)); 477 $this->assertEquals(0, count_private($this->linkDb));
@@ -462,10 +489,10 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
462 'privacy' => 'private', 489 'privacy' => 'private',
463 'overwrite' => 'true' 490 'overwrite' => 'true'
464 ); 491 );
465 $this->assertEquals( 492 $this->assertStringMatchesFormat(
466 'File netscape_basic.htm (482 bytes) was successfully processed:' 493 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
467 .' 2 links imported, 2 links overwritten, 0 links skipped.', 494 .' 2 links imported, 2 links overwritten, 0 links skipped.',
468 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache) 495 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
469 ); 496 );
470 $this->assertEquals(2, count($this->linkDb)); 497 $this->assertEquals(2, count($this->linkDb));
471 $this->assertEquals(2, count_private($this->linkDb)); 498 $this->assertEquals(2, count_private($this->linkDb));
@@ -486,20 +513,20 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
486 { 513 {
487 $post = array('privacy' => 'public'); 514 $post = array('privacy' => 'public');
488 $files = file2array('netscape_basic.htm'); 515 $files = file2array('netscape_basic.htm');
489 $this->assertEquals( 516 $this->assertStringMatchesFormat(
490 'File netscape_basic.htm (482 bytes) was successfully processed:' 517 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
491 .' 2 links imported, 0 links overwritten, 0 links skipped.', 518 .' 2 links imported, 0 links overwritten, 0 links skipped.',
492 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache) 519 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
493 ); 520 );
494 $this->assertEquals(2, count($this->linkDb)); 521 $this->assertEquals(2, count($this->linkDb));
495 $this->assertEquals(0, count_private($this->linkDb)); 522 $this->assertEquals(0, count_private($this->linkDb));
496 523
497 // re-import as private, DO NOT enable overwriting 524 // re-import as private, DO NOT enable overwriting
498 $post = array('privacy' => 'private'); 525 $post = array('privacy' => 'private');
499 $this->assertEquals( 526 $this->assertStringMatchesFormat(
500 'File netscape_basic.htm (482 bytes) was successfully processed:' 527 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
501 .' 0 links imported, 0 links overwritten, 2 links skipped.', 528 .' 0 links imported, 0 links overwritten, 2 links skipped.',
502 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache) 529 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
503 ); 530 );
504 $this->assertEquals(2, count($this->linkDb)); 531 $this->assertEquals(2, count($this->linkDb));
505 $this->assertEquals(0, count_private($this->linkDb)); 532 $this->assertEquals(0, count_private($this->linkDb));
@@ -515,10 +542,10 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
515 'default_tags' => 'tag1,tag2 tag3' 542 'default_tags' => 'tag1,tag2 tag3'
516 ); 543 );
517 $files = file2array('netscape_basic.htm'); 544 $files = file2array('netscape_basic.htm');
518 $this->assertEquals( 545 $this->assertStringMatchesFormat(
519 'File netscape_basic.htm (482 bytes) was successfully processed:' 546 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
520 .' 2 links imported, 0 links overwritten, 0 links skipped.', 547 .' 2 links imported, 0 links overwritten, 0 links skipped.',
521 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache) 548 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
522 ); 549 );
523 $this->assertEquals(2, count($this->linkDb)); 550 $this->assertEquals(2, count($this->linkDb));
524 $this->assertEquals(0, count_private($this->linkDb)); 551 $this->assertEquals(0, count_private($this->linkDb));
@@ -542,10 +569,10 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
542 'default_tags' => 'tag1&,tag2 "tag3"' 569 'default_tags' => 'tag1&,tag2 "tag3"'
543 ); 570 );
544 $files = file2array('netscape_basic.htm'); 571 $files = file2array('netscape_basic.htm');
545 $this->assertEquals( 572 $this->assertStringMatchesFormat(
546 'File netscape_basic.htm (482 bytes) was successfully processed:' 573 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
547 .' 2 links imported, 0 links overwritten, 0 links skipped.', 574 .' 2 links imported, 0 links overwritten, 0 links skipped.',
548 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache) 575 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history)
549 ); 576 );
550 $this->assertEquals(2, count($this->linkDb)); 577 $this->assertEquals(2, count($this->linkDb));
551 $this->assertEquals(0, count_private($this->linkDb)); 578 $this->assertEquals(0, count_private($this->linkDb));
@@ -567,10 +594,10 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
567 public function testImportSameDate() 594 public function testImportSameDate()
568 { 595 {
569 $files = file2array('same_date.htm'); 596 $files = file2array('same_date.htm');
570 $this->assertEquals( 597 $this->assertStringMatchesFormat(
571 'File same_date.htm (453 bytes) was successfully processed:' 598 'File same_date.htm (453 bytes) was successfully processed in %d seconds:'
572 .' 3 links imported, 0 links overwritten, 0 links skipped.', 599 .' 3 links imported, 0 links overwritten, 0 links skipped.',
573 NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->pagecache) 600 NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->conf, $this->history)
574 ); 601 );
575 $this->assertEquals(3, count($this->linkDb)); 602 $this->assertEquals(3, count($this->linkDb));
576 $this->assertEquals(0, count_private($this->linkDb)); 603 $this->assertEquals(0, count_private($this->linkDb));
@@ -587,4 +614,27 @@ class BookmarkImportTest extends PHPUnit_Framework_TestCase
587 $this->linkDb[2]['id'] 614 $this->linkDb[2]['id']
588 ); 615 );
589 } 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 }
590} 640}