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