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