aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/NetscapeBookmarkUtils/BookmarkImportTest.php
diff options
context:
space:
mode:
authorVirtualTam <virtualtam@flibidi.net>2016-07-28 22:54:33 +0200
committerVirtualTam <virtualtam@flibidi.net>2016-08-10 01:42:44 +0200
commita973afeac7b7399d35b881920f0afc1947765ccd (patch)
tree4a13ddf32779cb8f02600d7cc20d23f692c9068c /tests/NetscapeBookmarkUtils/BookmarkImportTest.php
parent085157c5cb6bd0df928c7875fb7997683418f1fb (diff)
downloadShaarli-a973afeac7b7399d35b881920f0afc1947765ccd.tar.gz
Shaarli-a973afeac7b7399d35b881920f0afc1947765ccd.tar.zst
Shaarli-a973afeac7b7399d35b881920f0afc1947765ccd.zip
Refactor bookmark import using a generic Netscape parser
Relates to #607 Relates to #608 Relates to #493 (abandoned) Additions: - use Composer's autoload to load 3rd-party dependencies under vendor/ Modifications: - [import] replace the current parser with a generic, stable parser - move code to application/NetscapeBookmarkUtils - improve status report after parsing - [router] use the same endpoint for both bookmark upload and import dialog - [template] update bookmark import options - allow adding tags to all imported links - allow selecting the visibility (privacy) of imported links - [tests] ensure bookmarks are properly parsed and imported in the LinkDB - reuse reference input from the parser's test data See: - https://github.com/shaarli/netscape-bookmark-parser - https://getcomposer.org/doc/01-basic-usage.md#autoloading Signed-off-by: VirtualTam <virtualtam@flibidi.net>
Diffstat (limited to 'tests/NetscapeBookmarkUtils/BookmarkImportTest.php')
-rw-r--r--tests/NetscapeBookmarkUtils/BookmarkImportTest.php518
1 files changed, 518 insertions, 0 deletions
diff --git a/tests/NetscapeBookmarkUtils/BookmarkImportTest.php b/tests/NetscapeBookmarkUtils/BookmarkImportTest.php
new file mode 100644
index 00000000..2d4e7557
--- /dev/null
+++ b/tests/NetscapeBookmarkUtils/BookmarkImportTest.php
@@ -0,0 +1,518 @@
1<?php
2
3require_once 'application/NetscapeBookmarkUtils.php';
4
5
6/**
7 * Utility function to load a file's metadata in a $_FILES-like array
8 *
9 * @param string $filename Basename of the file
10 *
11 * @return array A $_FILES-like array
12 */
13function file2array($filename)
14{
15 return array(
16 'filetoupload' => array(
17 'name' => $filename,
18 'tmp_name' => __DIR__ . '/input/' . $filename,
19 'size' => filesize(__DIR__ . '/input/' . $filename)
20 )
21 );
22}
23
24
25/**
26 * Netscape bookmark import
27 */
28class BookmarkImportTest extends PHPUnit_Framework_TestCase
29{
30 /**
31 * @var string datastore to test write operations
32 */
33 protected static $testDatastore = 'sandbox/datastore.php';
34
35 /**
36 * @var LinkDB private LinkDB instance
37 */
38 protected $linkDb = null;
39
40 /**
41 * @var string Dummy page cache
42 */
43 protected $pagecache = 'tests';
44
45 /**
46 * Resets test data before each test
47 */
48 protected function setUp()
49 {
50 if (file_exists(self::$testDatastore)) {
51 unlink(self::$testDatastore);
52 }
53 // start with an empty datastore
54 file_put_contents(self::$testDatastore, '<?php /* S7QysKquBQA= */ ?>');
55 $this->linkDb = new LinkDB(self::$testDatastore, true, false);
56 }
57
58 /**
59 * Attempt to import bookmarks from an empty file
60 */
61 public function testImportEmptyData()
62 {
63 $files = file2array('empty.htm');
64 $this->assertEquals(
65 'File empty.htm (0 bytes) has an unknown file format.'
66 .' Nothing was imported.',
67 NetscapeBookmarkUtils::import(NULL, $files, NULL, NULL)
68 );
69 $this->assertEquals(0, count($this->linkDb));
70 }
71
72 /**
73 * Attempt to import bookmarks from a file with no Doctype
74 */
75 public function testImportNoDoctype()
76 {
77 $files = file2array('no_doctype.htm');
78 $this->assertEquals(
79 'File no_doctype.htm (350 bytes) has an unknown file format. Nothing was imported.',
80 NetscapeBookmarkUtils::import(NULL, $files, NULL, NULL)
81 );
82 $this->assertEquals(0, count($this->linkDb));
83 }
84
85 /**
86 * Import bookmarks nested in a folder hierarchy
87 */
88 public function testImportNested()
89 {
90 $files = file2array('netscape_nested.htm');
91 $this->assertEquals(
92 'File netscape_nested.htm (1337 bytes) was successfully processed:'
93 .' 8 links imported, 0 links overwritten, 0 links skipped.',
94 NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->pagecache)
95 );
96 $this->assertEquals(8, count($this->linkDb));
97 $this->assertEquals(2, count_private($this->linkDb));
98
99 $this->assertEquals(
100 array(
101 'linkdate' => '20160225_205541',
102 'title' => 'Nested 1',
103 'url' => 'http://nest.ed/1',
104 'description' => '',
105 'private' => 0,
106 'tags' => 'tag1 tag2'
107 ),
108 $this->linkDb->getLinkFromUrl('http://nest.ed/1')
109 );
110 $this->assertEquals(
111 array(
112 'linkdate' => '20160225_205542',
113 'title' => 'Nested 1-1',
114 'url' => 'http://nest.ed/1-1',
115 'description' => '',
116 'private' => 0,
117 'tags' => 'folder1 tag1 tag2'
118 ),
119 $this->linkDb->getLinkFromUrl('http://nest.ed/1-1')
120 );
121 $this->assertEquals(
122 array(
123 'linkdate' => '20160225_205547',
124 'title' => 'Nested 1-2',
125 'url' => 'http://nest.ed/1-2',
126 'description' => '',
127 'private' => 0,
128 'tags' => 'folder1 tag3 tag4'
129 ),
130 $this->linkDb->getLinkFromUrl('http://nest.ed/1-2')
131 );
132 $this->assertEquals(
133 array(
134 'linkdate' => '20160202_172222',
135 'title' => 'Nested 2-1',
136 'url' => 'http://nest.ed/2-1',
137 'description' => 'First link of the second section',
138 'private' => 1,
139 'tags' => 'folder2'
140 ),
141 $this->linkDb->getLinkFromUrl('http://nest.ed/2-1')
142 );
143 $this->assertEquals(
144 array(
145 'linkdate' => '20160119_200227',
146 'title' => 'Nested 2-2',
147 'url' => 'http://nest.ed/2-2',
148 'description' => 'Second link of the second section',
149 'private' => 1,
150 'tags' => 'folder2'
151 ),
152 $this->linkDb->getLinkFromUrl('http://nest.ed/2-2')
153 );
154 $this->assertEquals(
155 array(
156 'linkdate' => '20160202_172223',
157 'title' => 'Nested 3-1',
158 'url' => 'http://nest.ed/3-1',
159 'description' => '',
160 'private' => 0,
161 'tags' => 'folder3 folder3-1 tag3'
162 ),
163 $this->linkDb->getLinkFromUrl('http://nest.ed/3-1')
164 );
165 $this->assertEquals(
166 array(
167 'linkdate' => '20160119_200228',
168 'title' => 'Nested 3-2',
169 'url' => 'http://nest.ed/3-2',
170 'description' => '',
171 'private' => 0,
172 'tags' => 'folder3 folder3-1'
173 ),
174 $this->linkDb->getLinkFromUrl('http://nest.ed/3-2')
175 );
176 $this->assertEquals(
177 array(
178 'linkdate' => '20160229_081541',
179 'title' => 'Nested 2',
180 'url' => 'http://nest.ed/2',
181 'description' => '',
182 'private' => 0,
183 'tags' => 'tag4'
184 ),
185 $this->linkDb->getLinkFromUrl('http://nest.ed/2')
186 );
187 }
188
189 /**
190 * Import bookmarks with the default privacy setting (reuse from file)
191 *
192 * The $_POST array is not set.
193 */
194 public function testImportDefaultPrivacyNoPost()
195 {
196 $files = file2array('netscape_basic.htm');
197 $this->assertEquals(
198 'File netscape_basic.htm (482 bytes) was successfully processed:'
199 .' 2 links imported, 0 links overwritten, 0 links skipped.',
200 NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->pagecache)
201 );
202 $this->assertEquals(2, count($this->linkDb));
203 $this->assertEquals(1, count_private($this->linkDb));
204
205 $this->assertEquals(
206 array(
207 'linkdate' => '20001010_105536',
208 'title' => 'Secret stuff',
209 'url' => 'https://private.tld',
210 'description' => "Super-secret stuff you're not supposed to know about",
211 'private' => 1,
212 'tags' => 'private secret'
213 ),
214 $this->linkDb->getLinkFromUrl('https://private.tld')
215 );
216 $this->assertEquals(
217 array(
218 'linkdate' => '20160225_205548',
219 'title' => 'Public stuff',
220 'url' => 'http://public.tld',
221 'description' => '',
222 'private' => 0,
223 'tags' => 'public hello world'
224 ),
225 $this->linkDb->getLinkFromUrl('http://public.tld')
226 );
227 }
228
229 /**
230 * Import bookmarks with the default privacy setting (reuse from file)
231 */
232 public function testImportKeepPrivacy()
233 {
234 $post = array('privacy' => 'default');
235 $files = file2array('netscape_basic.htm');
236 $this->assertEquals(
237 'File netscape_basic.htm (482 bytes) was successfully processed:'
238 .' 2 links imported, 0 links overwritten, 0 links skipped.',
239 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
240 );
241 $this->assertEquals(2, count($this->linkDb));
242 $this->assertEquals(1, count_private($this->linkDb));
243
244 $this->assertEquals(
245 array(
246 'linkdate' => '20001010_105536',
247 'title' => 'Secret stuff',
248 'url' => 'https://private.tld',
249 'description' => "Super-secret stuff you're not supposed to know about",
250 'private' => 1,
251 'tags' => 'private secret'
252 ),
253 $this->linkDb->getLinkFromUrl('https://private.tld')
254 );
255 $this->assertEquals(
256 array(
257 'linkdate' => '20160225_205548',
258 'title' => 'Public stuff',
259 'url' => 'http://public.tld',
260 'description' => '',
261 'private' => 0,
262 'tags' => 'public hello world'
263 ),
264 $this->linkDb->getLinkFromUrl('http://public.tld')
265 );
266 }
267
268 /**
269 * Import links as public
270 */
271 public function testImportAsPublic()
272 {
273 $post = array('privacy' => 'public');
274 $files = file2array('netscape_basic.htm');
275 $this->assertEquals(
276 'File netscape_basic.htm (482 bytes) was successfully processed:'
277 .' 2 links imported, 0 links overwritten, 0 links skipped.',
278 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
279 );
280 $this->assertEquals(2, count($this->linkDb));
281 $this->assertEquals(0, count_private($this->linkDb));
282 $this->assertEquals(
283 0,
284 $this->linkDb['20001010_105536']['private']
285 );
286 $this->assertEquals(
287 0,
288 $this->linkDb['20160225_205548']['private']
289 );
290 }
291
292 /**
293 * Import links as private
294 */
295 public function testImportAsPrivate()
296 {
297 $post = array('privacy' => 'private');
298 $files = file2array('netscape_basic.htm');
299 $this->assertEquals(
300 'File netscape_basic.htm (482 bytes) was successfully processed:'
301 .' 2 links imported, 0 links overwritten, 0 links skipped.',
302 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
303 );
304 $this->assertEquals(2, count($this->linkDb));
305 $this->assertEquals(2, count_private($this->linkDb));
306 $this->assertEquals(
307 1,
308 $this->linkDb['20001010_105536']['private']
309 );
310 $this->assertEquals(
311 1,
312 $this->linkDb['20160225_205548']['private']
313 );
314 }
315
316 /**
317 * Overwrite private links so they become public
318 */
319 public function testOverwriteAsPublic()
320 {
321 $files = file2array('netscape_basic.htm');
322
323 // import links as private
324 $post = array('privacy' => 'private');
325 $this->assertEquals(
326 'File netscape_basic.htm (482 bytes) was successfully processed:'
327 .' 2 links imported, 0 links overwritten, 0 links skipped.',
328 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
329 );
330 $this->assertEquals(2, count($this->linkDb));
331 $this->assertEquals(2, count_private($this->linkDb));
332 $this->assertEquals(
333 1,
334 $this->linkDb['20001010_105536']['private']
335 );
336 $this->assertEquals(
337 1,
338 $this->linkDb['20160225_205548']['private']
339 );
340
341 // re-import as public, enable overwriting
342 $post = array(
343 'privacy' => 'public',
344 'overwrite' => 'true'
345 );
346 $this->assertEquals(
347 'File netscape_basic.htm (482 bytes) was successfully processed:'
348 .' 2 links imported, 2 links overwritten, 0 links skipped.',
349 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
350 );
351 $this->assertEquals(2, count($this->linkDb));
352 $this->assertEquals(0, count_private($this->linkDb));
353 $this->assertEquals(
354 0,
355 $this->linkDb['20001010_105536']['private']
356 );
357 $this->assertEquals(
358 0,
359 $this->linkDb['20160225_205548']['private']
360 );
361 }
362
363 /**
364 * Overwrite public links so they become private
365 */
366 public function testOverwriteAsPrivate()
367 {
368 $files = file2array('netscape_basic.htm');
369
370 // import links as public
371 $post = array('privacy' => 'public');
372 $this->assertEquals(
373 'File netscape_basic.htm (482 bytes) was successfully processed:'
374 .' 2 links imported, 0 links overwritten, 0 links skipped.',
375 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
376 );
377 $this->assertEquals(2, count($this->linkDb));
378 $this->assertEquals(0, count_private($this->linkDb));
379 $this->assertEquals(
380 0,
381 $this->linkDb['20001010_105536']['private']
382 );
383 $this->assertEquals(
384 0,
385 $this->linkDb['20160225_205548']['private']
386 );
387
388 // re-import as private, enable overwriting
389 $post = array(
390 'privacy' => 'private',
391 'overwrite' => 'true'
392 );
393 $this->assertEquals(
394 'File netscape_basic.htm (482 bytes) was successfully processed:'
395 .' 2 links imported, 2 links overwritten, 0 links skipped.',
396 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
397 );
398 $this->assertEquals(2, count($this->linkDb));
399 $this->assertEquals(2, count_private($this->linkDb));
400 $this->assertEquals(
401 1,
402 $this->linkDb['20001010_105536']['private']
403 );
404 $this->assertEquals(
405 1,
406 $this->linkDb['20160225_205548']['private']
407 );
408 }
409
410 /**
411 * Attept to import the same links twice without enabling overwriting
412 */
413 public function testSkipOverwrite()
414 {
415 $post = array('privacy' => 'public');
416 $files = file2array('netscape_basic.htm');
417 $this->assertEquals(
418 'File netscape_basic.htm (482 bytes) was successfully processed:'
419 .' 2 links imported, 0 links overwritten, 0 links skipped.',
420 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
421 );
422 $this->assertEquals(2, count($this->linkDb));
423 $this->assertEquals(0, count_private($this->linkDb));
424
425 // re-import as private, DO NOT enable overwriting
426 $post = array('privacy' => 'private');
427 $this->assertEquals(
428 'File netscape_basic.htm (482 bytes) was successfully processed:'
429 .' 0 links imported, 0 links overwritten, 2 links skipped.',
430 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
431 );
432 $this->assertEquals(2, count($this->linkDb));
433 $this->assertEquals(0, count_private($this->linkDb));
434 }
435
436 /**
437 * Add user-specified tags to all imported bookmarks
438 */
439 public function testSetDefaultTags()
440 {
441 $post = array(
442 'privacy' => 'public',
443 'default_tags' => 'tag1,tag2 tag3'
444 );
445 $files = file2array('netscape_basic.htm');
446 $this->assertEquals(
447 'File netscape_basic.htm (482 bytes) was successfully processed:'
448 .' 2 links imported, 0 links overwritten, 0 links skipped.',
449 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
450 );
451 $this->assertEquals(2, count($this->linkDb));
452 $this->assertEquals(0, count_private($this->linkDb));
453 $this->assertEquals(
454 'tag1 tag2 tag3 private secret',
455 $this->linkDb['20001010_105536']['tags']
456 );
457 $this->assertEquals(
458 'tag1 tag2 tag3 public hello world',
459 $this->linkDb['20160225_205548']['tags']
460 );
461 }
462
463 /**
464 * The user-specified tags contain characters to be escaped
465 */
466 public function testSanitizeDefaultTags()
467 {
468 $post = array(
469 'privacy' => 'public',
470 'default_tags' => 'tag1&,tag2 "tag3"'
471 );
472 $files = file2array('netscape_basic.htm');
473 $this->assertEquals(
474 'File netscape_basic.htm (482 bytes) was successfully processed:'
475 .' 2 links imported, 0 links overwritten, 0 links skipped.',
476 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
477 );
478 $this->assertEquals(2, count($this->linkDb));
479 $this->assertEquals(0, count_private($this->linkDb));
480 $this->assertEquals(
481 'tag1&amp; tag2 &quot;tag3&quot; private secret',
482 $this->linkDb['20001010_105536']['tags']
483 );
484 $this->assertEquals(
485 'tag1&amp; tag2 &quot;tag3&quot; public hello world',
486 $this->linkDb['20160225_205548']['tags']
487 );
488 }
489
490 /**
491 * Ensure each imported bookmark has a unique linkdate
492 *
493 * See https://github.com/shaarli/Shaarli/issues/351
494 */
495 public function testImportSameDate()
496 {
497 $files = file2array('same_date.htm');
498 $this->assertEquals(
499 'File same_date.htm (453 bytes) was successfully processed:'
500 .' 3 links imported, 0 links overwritten, 0 links skipped.',
501 NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->pagecache)
502 );
503 $this->assertEquals(3, count($this->linkDb));
504 $this->assertEquals(0, count_private($this->linkDb));
505 $this->assertEquals(
506 '20160225_205548',
507 $this->linkDb['20160225_205548']['linkdate']
508 );
509 $this->assertEquals(
510 '20160225_205549',
511 $this->linkDb['20160225_205549']['linkdate']
512 );
513 $this->assertEquals(
514 '20160225_205550',
515 $this->linkDb['20160225_205550']['linkdate']
516 );
517 }
518}