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