]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/netscape/BookmarkImportTest.php
Feature: support any tag separator
[github/shaarli/Shaarli.git] / tests / netscape / BookmarkImportTest.php
CommitLineData
a973afea 1<?php
e8a10f31 2
349b0144 3namespace Shaarli\Netscape;
a973afea 4
349b0144 5use DateTime;
fd1ddad9 6use malkusch\lock\mutex\NoMutex;
78657347 7use Psr\Http\Message\UploadedFileInterface;
e26e2060 8use Shaarli\Bookmark\Bookmark;
e26e2060 9use Shaarli\Bookmark\BookmarkFileService;
e8a10f31 10use Shaarli\Bookmark\BookmarkFilter;
87e9631e 11use Shaarli\Config\ConfigManager;
bdc5152d 12use Shaarli\History;
a5a9cf23 13use Shaarli\TestCase;
78657347 14use Slim\Http\UploadedFile;
a973afea
V
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 *
78657347 21 * @return UploadedFileInterface Upload file in PSR-7 compatible object
a973afea
V
22 */
23function file2array($filename)
24{
78657347
A
25 return new UploadedFile(
26 __DIR__ . '/input/' . $filename,
27 $filename,
28 null,
29 filesize(__DIR__ . '/input/' . $filename)
a973afea
V
30 );
31}
32
33
34/**
35 * Netscape bookmark import
36 */
e8a10f31 37class BookmarkImportTest extends TestCase
a973afea
V
38{
39 /**
40 * @var string datastore to test write operations
41 */
42 protected static $testDatastore = 'sandbox/datastore.php';
43
4306b184
A
44 /**
45 * @var string History file path
46 */
47 protected static $historyFilePath = 'sandbox/history.php';
48
a973afea 49 /**
e26e2060 50 * @var BookmarkFileService private LinkDB instance
a973afea 51 */
e26e2060 52 protected $bookmarkService = null;
a973afea
V
53
54 /**
55 * @var string Dummy page cache
56 */
57 protected $pagecache = 'tests';
58
48417aed
A
59 /**
60 * @var ConfigManager instance.
61 */
62 protected $conf;
63
4306b184
A
64 /**
65 * @var History instance.
66 */
67 protected $history;
68
e8a10f31
A
69 /**
70 * @var NetscapeBookmarkUtils
71 */
72 protected $netscapeBookmarkUtils;
73
c3dfd899
A
74 /**
75 * @var string Save the current timezone.
76 */
77 protected static $defaultTimeZone;
78
8f60e120 79 public static function setUpBeforeClass(): void
c3dfd899
A
80 {
81 self::$defaultTimeZone = date_default_timezone_get();
82 // Timezone without DST for test consistency
83 date_default_timezone_set('Africa/Nairobi');
84 }
85
a973afea
V
86 /**
87 * Resets test data before each test
88 */
8f60e120 89 protected function setUp(): void
a973afea 90 {
fd1ddad9 91 $mutex = new NoMutex();
a973afea
V
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= */ ?>');
e26e2060 97
48417aed
A
98 $this->conf = new ConfigManager('tests/utils/config/configJson');
99 $this->conf->set('resource.page_cache', $this->pagecache);
e26e2060 100 $this->conf->set('resource.datastore', self::$testDatastore);
4306b184 101 $this->history = new History(self::$historyFilePath);
fd1ddad9 102 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, $mutex, true);
e8a10f31 103 $this->netscapeBookmarkUtils = new NetscapeBookmarkUtils($this->bookmarkService, $this->conf, $this->history);
4306b184
A
104 }
105
106 /**
107 * Delete history file.
108 */
8f60e120 109 protected function tearDown(): void
4306b184
A
110 {
111 @unlink(self::$historyFilePath);
a973afea
V
112 }
113
8f60e120 114 public static function tearDownAfterClass(): void
c3dfd899
A
115 {
116 date_default_timezone_set(self::$defaultTimeZone);
117 }
118
a973afea
V
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.',
e8a10f31 128 $this->netscapeBookmarkUtils->import(null, $files)
a973afea 129 );
e26e2060 130 $this->assertEquals(0, $this->bookmarkService->count());
a973afea
V
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.',
e8a10f31 141 $this->netscapeBookmarkUtils->import(null, $files)
a973afea 142 );
e26e2060 143 $this->assertEquals(0, $this->bookmarkService->count());
a973afea
V
144 }
145
3ff1ce47
A
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:'
e26e2060 154 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
e8a10f31 155 $this->netscapeBookmarkUtils->import(null, $files)
3ff1ce47 156 );
e26e2060 157 $this->assertEquals(2, $this->bookmarkService->count());
3ff1ce47
A
158 }
159
160
f4ad7bde
V
161 /**
162 * Ensure IE dumps are supported
163 */
164 public function testImportInternetExplorerEncoding()
165 {
166 $files = file2array('internet_explorer_encoding.htm');
66e74d50
A
167 $this->assertStringMatchesFormat(
168 'File internet_explorer_encoding.htm (356 bytes) was successfully processed in %d seconds:'
e26e2060 169 .' 1 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
e8a10f31 170 $this->netscapeBookmarkUtils->import([], $files)
f4ad7bde 171 );
e26e2060
A
172 $this->assertEquals(1, $this->bookmarkService->count());
173 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
f4ad7bde 174
e26e2060
A
175 $bookmark = $this->bookmarkService->findByUrl('http://hginit.com/');
176 $this->assertEquals(0, $bookmark->getId());
f4ad7bde 177 $this->assertEquals(
e26e2060
A
178 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160618_203944'),
179 $bookmark->getCreated()
f4ad7bde 180 );
e26e2060
A
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());
f4ad7bde
V
187 }
188
a973afea
V
189 /**
190 * Import bookmarks nested in a folder hierarchy
191 */
192 public function testImportNested()
193 {
194 $files = file2array('netscape_nested.htm');
66e74d50
A
195 $this->assertStringMatchesFormat(
196 'File netscape_nested.htm (1337 bytes) was successfully processed in %d seconds:'
e26e2060 197 .' 8 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
e8a10f31 198 $this->netscapeBookmarkUtils->import([], $files)
a973afea 199 );
e26e2060
A
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());
a973afea
V
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');
66e74d50
A
316 $this->assertStringMatchesFormat(
317 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
e26e2060 318 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
e8a10f31 319 $this->netscapeBookmarkUtils->import([], $files)
a973afea 320 );
c3dfd899 321
e26e2060
A
322 $this->assertEquals(2, $this->bookmarkService->count());
323 $this->assertEquals(1, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
a973afea 324
e26e2060
A
325 $bookmark = $this->bookmarkService->findByUrl('https://private.tld');
326 $this->assertEquals(0, $bookmark->getId());
a973afea 327 $this->assertEquals(
e26e2060
A
328 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20001010_135536'),
329 $bookmark->getCreated()
a973afea 330 );
e26e2060
A
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());
a973afea 340 $this->assertEquals(
e26e2060
A
341 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160225_235548'),
342 $bookmark->getCreated()
a973afea 343 );
e26e2060
A
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());
a973afea
V
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');
66e74d50
A
359 $this->assertStringMatchesFormat(
360 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
e26e2060 361 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
e8a10f31 362 $this->netscapeBookmarkUtils->import($post, $files)
a973afea 363 );
a973afea 364
e26e2060
A
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());
a973afea 370 $this->assertEquals(
e26e2060
A
371 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20001010_135536'),
372 $bookmark->getCreated()
a973afea 373 );
e26e2060
A
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());
a973afea 383 $this->assertEquals(
e26e2060
A
384 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160225_235548'),
385 $bookmark->getCreated()
a973afea 386 );
e26e2060
A
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());
a973afea
V
393 }
394
395 /**
e26e2060 396 * Import bookmarks as public
a973afea
V
397 */
398 public function testImportAsPublic()
399 {
400 $post = array('privacy' => 'public');
401 $files = file2array('netscape_basic.htm');
66e74d50
A
402 $this->assertStringMatchesFormat(
403 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
e26e2060 404 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
e8a10f31 405 $this->netscapeBookmarkUtils->import($post, $files)
a973afea 406 );
e26e2060
A
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());
a973afea
V
411 }
412
413 /**
e26e2060 414 * Import bookmarks as private
a973afea
V
415 */
416 public function testImportAsPrivate()
417 {
418 $post = array('privacy' => 'private');
419 $files = file2array('netscape_basic.htm');
66e74d50
A
420 $this->assertStringMatchesFormat(
421 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
e26e2060 422 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
e8a10f31 423 $this->netscapeBookmarkUtils->import($post, $files)
a973afea 424 );
e26e2060
A
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());
a973afea
V
429 }
430
431 /**
e26e2060 432 * Overwrite private bookmarks so they become public
a973afea
V
433 */
434 public function testOverwriteAsPublic()
435 {
436 $files = file2array('netscape_basic.htm');
437
e26e2060 438 // import bookmarks as private
a973afea 439 $post = array('privacy' => 'private');
66e74d50
A
440 $this->assertStringMatchesFormat(
441 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
e26e2060 442 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
e8a10f31 443 $this->netscapeBookmarkUtils->import($post, $files)
a973afea 444 );
e26e2060
A
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
a973afea
V
450 // re-import as public, enable overwriting
451 $post = array(
452 'privacy' => 'public',
453 'overwrite' => 'true'
454 );
66e74d50
A
455 $this->assertStringMatchesFormat(
456 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
e26e2060 457 .' 2 bookmarks imported, 2 bookmarks overwritten, 0 bookmarks skipped.',
e8a10f31 458 $this->netscapeBookmarkUtils->import($post, $files)
a973afea 459 );
e26e2060
A
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());
a973afea
V
464 }
465
466 /**
e26e2060 467 * Overwrite public bookmarks so they become private
a973afea
V
468 */
469 public function testOverwriteAsPrivate()
470 {
471 $files = file2array('netscape_basic.htm');
472
e26e2060 473 // import bookmarks as public
a973afea 474 $post = array('privacy' => 'public');
66e74d50
A
475 $this->assertStringMatchesFormat(
476 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
e26e2060 477 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
e8a10f31 478 $this->netscapeBookmarkUtils->import($post, $files)
a973afea 479 );
e26e2060
A
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());
a973afea
V
484
485 // re-import as private, enable overwriting
486 $post = array(
487 'privacy' => 'private',
488 'overwrite' => 'true'
489 );
66e74d50
A
490 $this->assertStringMatchesFormat(
491 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
e26e2060 492 .' 2 bookmarks imported, 2 bookmarks overwritten, 0 bookmarks skipped.',
e8a10f31 493 $this->netscapeBookmarkUtils->import($post, $files)
a973afea 494 );
e26e2060
A
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());
a973afea
V
499 }
500
501 /**
e26e2060 502 * Attept to import the same bookmarks twice without enabling overwriting
a973afea
V
503 */
504 public function testSkipOverwrite()
505 {
506 $post = array('privacy' => 'public');
507 $files = file2array('netscape_basic.htm');
66e74d50
A
508 $this->assertStringMatchesFormat(
509 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
e26e2060 510 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
e8a10f31 511 $this->netscapeBookmarkUtils->import($post, $files)
a973afea 512 );
e26e2060
A
513 $this->assertEquals(2, $this->bookmarkService->count());
514 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
a973afea
V
515
516 // re-import as private, DO NOT enable overwriting
517 $post = array('privacy' => 'private');
66e74d50
A
518 $this->assertStringMatchesFormat(
519 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
e26e2060 520 .' 0 bookmarks imported, 0 bookmarks overwritten, 2 bookmarks skipped.',
e8a10f31 521 $this->netscapeBookmarkUtils->import($post, $files)
a973afea 522 );
e26e2060
A
523 $this->assertEquals(2, $this->bookmarkService->count());
524 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
a973afea
V
525 }
526
527 /**
528 * Add user-specified tags to all imported bookmarks
529 */
530 public function testSetDefaultTags()
531 {
532 $post = array(
533 'privacy' => 'public',
b3bd8c3e 534 'default_tags' => 'tag1 tag2 tag3'
a973afea
V
535 );
536 $files = file2array('netscape_basic.htm');
66e74d50
A
537 $this->assertStringMatchesFormat(
538 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
e26e2060 539 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
e8a10f31 540 $this->netscapeBookmarkUtils->import($post, $files)
a973afea 541 );
e26e2060
A
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());
a973afea
V
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',
b3bd8c3e 555 'default_tags' => 'tag1& tag2 "tag3"'
a973afea
V
556 );
557 $files = file2array('netscape_basic.htm');
66e74d50
A
558 $this->assertStringMatchesFormat(
559 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
e26e2060 560 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
e8a10f31 561 $this->netscapeBookmarkUtils->import($post, $files)
a973afea 562 );
e26e2060
A
563 $this->assertEquals(2, $this->bookmarkService->count());
564 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
a973afea
V
565 $this->assertEquals(
566 'tag1&amp; tag2 &quot;tag3&quot; private secret',
e26e2060 567 $this->bookmarkService->get(0)->getTagsString()
a973afea
V
568 );
569 $this->assertEquals(
570 'tag1&amp; tag2 &quot;tag3&quot; public hello world',
e26e2060 571 $this->bookmarkService->get(1)->getTagsString()
a973afea
V
572 );
573 }
574
b3bd8c3e
A
575 /**
576 * Add user-specified tags to all imported bookmarks
577 */
578 public function testSetDefaultTagsWithCustomSeparator()
579 {
580 $separator = '@';
581 $this->conf->set('general.tags_separator', $separator);
582 $post = [
583 'privacy' => 'public',
584 'default_tags' => 'tag1@tag2@tag3@multiple words tag'
585 ];
586 $files = file2array('netscape_basic.htm');
587 $this->assertStringMatchesFormat(
588 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
589 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
590 $this->netscapeBookmarkUtils->import($post, $files)
591 );
592 $this->assertEquals(2, $this->bookmarkService->count());
593 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
594 $this->assertEquals(
595 'tag1@tag2@tag3@multiple words tag@private@secret',
596 $this->bookmarkService->get(0)->getTagsString($separator)
597 );
598 $this->assertEquals(
599 ['tag1', 'tag2', 'tag3', 'multiple words tag', 'private', 'secret'],
600 $this->bookmarkService->get(0)->getTags()
601 );
602 $this->assertEquals(
603 'tag1@tag2@tag3@multiple words tag@public@hello@world',
604 $this->bookmarkService->get(1)->getTagsString($separator)
605 );
606 $this->assertEquals(
607 ['tag1', 'tag2', 'tag3', 'multiple words tag', 'public', 'hello', 'world'],
608 $this->bookmarkService->get(1)->getTags()
609 );
610 }
611
a973afea 612 /**
c3dfd899 613 * Ensure each imported bookmark has a unique id
a973afea
V
614 *
615 * See https://github.com/shaarli/Shaarli/issues/351
616 */
617 public function testImportSameDate()
618 {
619 $files = file2array('same_date.htm');
66e74d50
A
620 $this->assertStringMatchesFormat(
621 'File same_date.htm (453 bytes) was successfully processed in %d seconds:'
e26e2060 622 .' 3 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
e8a10f31 623 $this->netscapeBookmarkUtils->import(array(), $files)
e26e2060
A
624 );
625 $this->assertEquals(3, $this->bookmarkService->count());
626 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
627 $this->assertEquals(0, $this->bookmarkService->get(0)->getId());
628 $this->assertEquals(1, $this->bookmarkService->get(1)->getId());
629 $this->assertEquals(2, $this->bookmarkService->get(2)->getId());
a973afea 630 }
4306b184
A
631
632 public function testImportCreateUpdateHistory()
633 {
634 $post = [
635 'privacy' => 'public',
636 'overwrite' => 'true',
637 ];
638 $files = file2array('netscape_basic.htm');
e8a10f31 639 $this->netscapeBookmarkUtils->import($post, $files);
4306b184 640 $history = $this->history->getHistory();
66e74d50
A
641 $this->assertEquals(1, count($history));
642 $this->assertEquals(History::IMPORT, $history[0]['event']);
643 $this->assertTrue(new DateTime('-5 seconds') < $history[0]['datetime']);
4306b184
A
644
645 // re-import as private, enable overwriting
e8a10f31 646 $this->netscapeBookmarkUtils->import($post, $files);
4306b184 647 $history = $this->history->getHistory();
66e74d50
A
648 $this->assertEquals(2, count($history));
649 $this->assertEquals(History::IMPORT, $history[0]['event']);
650 $this->assertTrue(new DateTime('-5 seconds') < $history[0]['datetime']);
651 $this->assertEquals(History::IMPORT, $history[1]['event']);
652 $this->assertTrue(new DateTime('-5 seconds') < $history[1]['datetime']);
4306b184 653 }
a973afea 654}