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