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