]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/netscape/BookmarkImportTest.php
Pin bookmarks through Slim controller
[github/shaarli/Shaarli.git] / tests / netscape / BookmarkImportTest.php
CommitLineData
a973afea 1<?php
349b0144 2namespace Shaarli\Netscape;
a973afea 3
349b0144 4use DateTime;
e26e2060
A
5use Shaarli\Bookmark\Bookmark;
6use Shaarli\Bookmark\BookmarkFilter;
7use Shaarli\Bookmark\BookmarkFileService;
f24896b2 8use Shaarli\Bookmark\LinkDB;
87e9631e 9use Shaarli\Config\ConfigManager;
bdc5152d 10use Shaarli\History;
a973afea
V
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 */
19function 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 */
349b0144 34class BookmarkImportTest extends \PHPUnit\Framework\TestCase
a973afea
V
35{
36 /**
37 * @var string datastore to test write operations
38 */
39 protected static $testDatastore = 'sandbox/datastore.php';
40
4306b184
A
41 /**
42 * @var string History file path
43 */
44 protected static $historyFilePath = 'sandbox/history.php';
45
a973afea 46 /**
e26e2060 47 * @var BookmarkFileService private LinkDB instance
a973afea 48 */
e26e2060 49 protected $bookmarkService = null;
a973afea
V
50
51 /**
52 * @var string Dummy page cache
53 */
54 protected $pagecache = 'tests';
55
48417aed
A
56 /**
57 * @var ConfigManager instance.
58 */
59 protected $conf;
60
4306b184
A
61 /**
62 * @var History instance.
63 */
64 protected $history;
65
c3dfd899
A
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
a973afea
V
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= */ ?>');
e26e2060 88
48417aed
A
89 $this->conf = new ConfigManager('tests/utils/config/configJson');
90 $this->conf->set('resource.page_cache', $this->pagecache);
e26e2060 91 $this->conf->set('resource.datastore', self::$testDatastore);
4306b184 92 $this->history = new History(self::$historyFilePath);
e26e2060 93 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
4306b184
A
94 }
95
96 /**
97 * Delete history file.
98 */
99 public function tearDown()
100 {
101 @unlink(self::$historyFilePath);
a973afea
V
102 }
103
c3dfd899
A
104 public static function tearDownAfterClass()
105 {
106 date_default_timezone_set(self::$defaultTimeZone);
107 }
108
a973afea
V
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.',
4306b184 118 NetscapeBookmarkUtils::import(null, $files, null, $this->conf, $this->history)
a973afea 119 );
e26e2060 120 $this->assertEquals(0, $this->bookmarkService->count());
a973afea
V
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.',
4306b184 131 NetscapeBookmarkUtils::import(null, $files, null, $this->conf, $this->history)
a973afea 132 );
e26e2060 133 $this->assertEquals(0, $this->bookmarkService->count());
a973afea
V
134 }
135
3ff1ce47
A
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:'
e26e2060
A
144 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
145 NetscapeBookmarkUtils::import(null, $files, $this->bookmarkService, $this->conf, $this->history)
3ff1ce47 146 );
e26e2060 147 $this->assertEquals(2, $this->bookmarkService->count());
3ff1ce47
A
148 }
149
150
f4ad7bde
V
151 /**
152 * Ensure IE dumps are supported
153 */
154 public function testImportInternetExplorerEncoding()
155 {
156 $files = file2array('internet_explorer_encoding.htm');
66e74d50
A
157 $this->assertStringMatchesFormat(
158 'File internet_explorer_encoding.htm (356 bytes) was successfully processed in %d seconds:'
e26e2060
A
159 .' 1 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
160 NetscapeBookmarkUtils::import([], $files, $this->bookmarkService, $this->conf, $this->history)
f4ad7bde 161 );
e26e2060
A
162 $this->assertEquals(1, $this->bookmarkService->count());
163 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
f4ad7bde 164
e26e2060
A
165 $bookmark = $this->bookmarkService->findByUrl('http://hginit.com/');
166 $this->assertEquals(0, $bookmark->getId());
f4ad7bde 167 $this->assertEquals(
e26e2060
A
168 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160618_203944'),
169 $bookmark->getCreated()
f4ad7bde 170 );
e26e2060
A
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());
f4ad7bde
V
177 }
178
a973afea
V
179 /**
180 * Import bookmarks nested in a folder hierarchy
181 */
182 public function testImportNested()
183 {
184 $files = file2array('netscape_nested.htm');
66e74d50
A
185 $this->assertStringMatchesFormat(
186 'File netscape_nested.htm (1337 bytes) was successfully processed in %d seconds:'
e26e2060
A
187 .' 8 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
188 NetscapeBookmarkUtils::import([], $files, $this->bookmarkService, $this->conf, $this->history)
a973afea 189 );
e26e2060
A
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());
a973afea
V
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');
66e74d50
A
306 $this->assertStringMatchesFormat(
307 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
e26e2060
A
308 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
309 NetscapeBookmarkUtils::import([], $files, $this->bookmarkService, $this->conf, $this->history)
a973afea 310 );
c3dfd899 311
e26e2060
A
312 $this->assertEquals(2, $this->bookmarkService->count());
313 $this->assertEquals(1, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
a973afea 314
e26e2060
A
315 $bookmark = $this->bookmarkService->findByUrl('https://private.tld');
316 $this->assertEquals(0, $bookmark->getId());
a973afea 317 $this->assertEquals(
e26e2060
A
318 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20001010_135536'),
319 $bookmark->getCreated()
a973afea 320 );
e26e2060
A
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());
a973afea 330 $this->assertEquals(
e26e2060
A
331 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160225_235548'),
332 $bookmark->getCreated()
a973afea 333 );
e26e2060
A
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());
a973afea
V
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');
66e74d50
A
349 $this->assertStringMatchesFormat(
350 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
e26e2060
A
351 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
352 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
a973afea 353 );
a973afea 354
e26e2060
A
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());
a973afea 360 $this->assertEquals(
e26e2060
A
361 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20001010_135536'),
362 $bookmark->getCreated()
a973afea 363 );
e26e2060
A
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());
a973afea 373 $this->assertEquals(
e26e2060
A
374 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160225_235548'),
375 $bookmark->getCreated()
a973afea 376 );
e26e2060
A
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());
a973afea
V
383 }
384
385 /**
e26e2060 386 * Import bookmarks as public
a973afea
V
387 */
388 public function testImportAsPublic()
389 {
390 $post = array('privacy' => 'public');
391 $files = file2array('netscape_basic.htm');
66e74d50
A
392 $this->assertStringMatchesFormat(
393 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
e26e2060
A
394 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
395 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
a973afea 396 );
e26e2060
A
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());
a973afea
V
401 }
402
403 /**
e26e2060 404 * Import bookmarks as private
a973afea
V
405 */
406 public function testImportAsPrivate()
407 {
408 $post = array('privacy' => 'private');
409 $files = file2array('netscape_basic.htm');
66e74d50
A
410 $this->assertStringMatchesFormat(
411 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
e26e2060
A
412 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
413 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
a973afea 414 );
e26e2060
A
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());
a973afea
V
419 }
420
421 /**
e26e2060 422 * Overwrite private bookmarks so they become public
a973afea
V
423 */
424 public function testOverwriteAsPublic()
425 {
426 $files = file2array('netscape_basic.htm');
427
e26e2060 428 // import bookmarks as private
a973afea 429 $post = array('privacy' => 'private');
66e74d50
A
430 $this->assertStringMatchesFormat(
431 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
e26e2060
A
432 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
433 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
a973afea 434 );
e26e2060
A
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
a973afea
V
440 // re-import as public, enable overwriting
441 $post = array(
442 'privacy' => 'public',
443 'overwrite' => 'true'
444 );
66e74d50
A
445 $this->assertStringMatchesFormat(
446 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
e26e2060
A
447 .' 2 bookmarks imported, 2 bookmarks overwritten, 0 bookmarks skipped.',
448 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
a973afea 449 );
e26e2060
A
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());
a973afea
V
454 }
455
456 /**
e26e2060 457 * Overwrite public bookmarks so they become private
a973afea
V
458 */
459 public function testOverwriteAsPrivate()
460 {
461 $files = file2array('netscape_basic.htm');
462
e26e2060 463 // import bookmarks as public
a973afea 464 $post = array('privacy' => 'public');
66e74d50
A
465 $this->assertStringMatchesFormat(
466 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
e26e2060
A
467 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
468 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
a973afea 469 );
e26e2060
A
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());
a973afea
V
474
475 // re-import as private, enable overwriting
476 $post = array(
477 'privacy' => 'private',
478 'overwrite' => 'true'
479 );
66e74d50
A
480 $this->assertStringMatchesFormat(
481 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
e26e2060
A
482 .' 2 bookmarks imported, 2 bookmarks overwritten, 0 bookmarks skipped.',
483 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
a973afea 484 );
e26e2060
A
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());
a973afea
V
489 }
490
491 /**
e26e2060 492 * Attept to import the same bookmarks twice without enabling overwriting
a973afea
V
493 */
494 public function testSkipOverwrite()
495 {
496 $post = array('privacy' => 'public');
497 $files = file2array('netscape_basic.htm');
66e74d50
A
498 $this->assertStringMatchesFormat(
499 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
e26e2060
A
500 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
501 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
a973afea 502 );
e26e2060
A
503 $this->assertEquals(2, $this->bookmarkService->count());
504 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
a973afea
V
505
506 // re-import as private, DO NOT enable overwriting
507 $post = array('privacy' => 'private');
66e74d50
A
508 $this->assertStringMatchesFormat(
509 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
e26e2060
A
510 .' 0 bookmarks imported, 0 bookmarks overwritten, 2 bookmarks skipped.',
511 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
a973afea 512 );
e26e2060
A
513 $this->assertEquals(2, $this->bookmarkService->count());
514 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
a973afea
V
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');
66e74d50
A
527 $this->assertStringMatchesFormat(
528 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
e26e2060
A
529 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
530 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
a973afea 531 );
e26e2060
A
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());
a973afea
V
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');
66e74d50
A
548 $this->assertStringMatchesFormat(
549 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
e26e2060
A
550 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
551 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
a973afea 552 );
e26e2060
A
553 $this->assertEquals(2, $this->bookmarkService->count());
554 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
a973afea
V
555 $this->assertEquals(
556 'tag1&amp; tag2 &quot;tag3&quot; private secret',
e26e2060 557 $this->bookmarkService->get(0)->getTagsString()
a973afea
V
558 );
559 $this->assertEquals(
560 'tag1&amp; tag2 &quot;tag3&quot; public hello world',
e26e2060 561 $this->bookmarkService->get(1)->getTagsString()
a973afea
V
562 );
563 }
564
565 /**
c3dfd899 566 * Ensure each imported bookmark has a unique id
a973afea
V
567 *
568 * See https://github.com/shaarli/Shaarli/issues/351
569 */
570 public function testImportSameDate()
571 {
572 $files = file2array('same_date.htm');
66e74d50
A
573 $this->assertStringMatchesFormat(
574 'File same_date.htm (453 bytes) was successfully processed in %d seconds:'
e26e2060
A
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());
a973afea 583 }
4306b184
A
584
585 public function testImportCreateUpdateHistory()
586 {
587 $post = [
588 'privacy' => 'public',
589 'overwrite' => 'true',
590 ];
591 $files = file2array('netscape_basic.htm');
e26e2060 592 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history);
4306b184 593 $history = $this->history->getHistory();
66e74d50
A
594 $this->assertEquals(1, count($history));
595 $this->assertEquals(History::IMPORT, $history[0]['event']);
596 $this->assertTrue(new DateTime('-5 seconds') < $history[0]['datetime']);
4306b184
A
597
598 // re-import as private, enable overwriting
e26e2060 599 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history);
4306b184 600 $history = $this->history->getHistory();
66e74d50
A
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']);
4306b184 606 }
a973afea 607}