aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/NetscapeBookmarkUtils/BookmarkImportTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/NetscapeBookmarkUtils/BookmarkImportTest.php')
-rw-r--r--tests/NetscapeBookmarkUtils/BookmarkImportTest.php546
1 files changed, 546 insertions, 0 deletions
diff --git a/tests/NetscapeBookmarkUtils/BookmarkImportTest.php b/tests/NetscapeBookmarkUtils/BookmarkImportTest.php
new file mode 100644
index 00000000..f0ad500f
--- /dev/null
+++ b/tests/NetscapeBookmarkUtils/BookmarkImportTest.php
@@ -0,0 +1,546 @@
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 * Ensure IE dumps are supported
87 */
88 public function testImportInternetExplorerEncoding()
89 {
90 $files = file2array('internet_explorer_encoding.htm');
91 $this->assertEquals(
92 'File internet_explorer_encoding.htm (356 bytes) was successfully processed:'
93 .' 1 links imported, 0 links overwritten, 0 links skipped.',
94 NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->pagecache)
95 );
96 $this->assertEquals(1, count($this->linkDb));
97 $this->assertEquals(0, count_private($this->linkDb));
98
99 $this->assertEquals(
100 array(
101 'linkdate' => '20160618_173944',
102 'title' => 'Hg Init a Mercurial tutorial by Joel Spolsky',
103 'url' => 'http://hginit.com/',
104 'description' => '',
105 'private' => 0,
106 'tags' => ''
107 ),
108 $this->linkDb->getLinkFromUrl('http://hginit.com/')
109 );
110 }
111
112
113 /**
114 * Import bookmarks nested in a folder hierarchy
115 */
116 public function testImportNested()
117 {
118 $files = file2array('netscape_nested.htm');
119 $this->assertEquals(
120 'File netscape_nested.htm (1337 bytes) was successfully processed:'
121 .' 8 links imported, 0 links overwritten, 0 links skipped.',
122 NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->pagecache)
123 );
124 $this->assertEquals(8, count($this->linkDb));
125 $this->assertEquals(2, count_private($this->linkDb));
126
127 $this->assertEquals(
128 array(
129 'linkdate' => '20160225_205541',
130 'title' => 'Nested 1',
131 'url' => 'http://nest.ed/1',
132 'description' => '',
133 'private' => 0,
134 'tags' => 'tag1 tag2'
135 ),
136 $this->linkDb->getLinkFromUrl('http://nest.ed/1')
137 );
138 $this->assertEquals(
139 array(
140 'linkdate' => '20160225_205542',
141 'title' => 'Nested 1-1',
142 'url' => 'http://nest.ed/1-1',
143 'description' => '',
144 'private' => 0,
145 'tags' => 'folder1 tag1 tag2'
146 ),
147 $this->linkDb->getLinkFromUrl('http://nest.ed/1-1')
148 );
149 $this->assertEquals(
150 array(
151 'linkdate' => '20160225_205547',
152 'title' => 'Nested 1-2',
153 'url' => 'http://nest.ed/1-2',
154 'description' => '',
155 'private' => 0,
156 'tags' => 'folder1 tag3 tag4'
157 ),
158 $this->linkDb->getLinkFromUrl('http://nest.ed/1-2')
159 );
160 $this->assertEquals(
161 array(
162 'linkdate' => '20160202_172222',
163 'title' => 'Nested 2-1',
164 'url' => 'http://nest.ed/2-1',
165 'description' => 'First link of the second section',
166 'private' => 1,
167 'tags' => 'folder2'
168 ),
169 $this->linkDb->getLinkFromUrl('http://nest.ed/2-1')
170 );
171 $this->assertEquals(
172 array(
173 'linkdate' => '20160119_200227',
174 'title' => 'Nested 2-2',
175 'url' => 'http://nest.ed/2-2',
176 'description' => 'Second link of the second section',
177 'private' => 1,
178 'tags' => 'folder2'
179 ),
180 $this->linkDb->getLinkFromUrl('http://nest.ed/2-2')
181 );
182 $this->assertEquals(
183 array(
184 'linkdate' => '20160202_172223',
185 'title' => 'Nested 3-1',
186 'url' => 'http://nest.ed/3-1',
187 'description' => '',
188 'private' => 0,
189 'tags' => 'folder3 folder3-1 tag3'
190 ),
191 $this->linkDb->getLinkFromUrl('http://nest.ed/3-1')
192 );
193 $this->assertEquals(
194 array(
195 'linkdate' => '20160119_200228',
196 'title' => 'Nested 3-2',
197 'url' => 'http://nest.ed/3-2',
198 'description' => '',
199 'private' => 0,
200 'tags' => 'folder3 folder3-1'
201 ),
202 $this->linkDb->getLinkFromUrl('http://nest.ed/3-2')
203 );
204 $this->assertEquals(
205 array(
206 'linkdate' => '20160229_081541',
207 'title' => 'Nested 2',
208 'url' => 'http://nest.ed/2',
209 'description' => '',
210 'private' => 0,
211 'tags' => 'tag4'
212 ),
213 $this->linkDb->getLinkFromUrl('http://nest.ed/2')
214 );
215 }
216
217 /**
218 * Import bookmarks with the default privacy setting (reuse from file)
219 *
220 * The $_POST array is not set.
221 */
222 public function testImportDefaultPrivacyNoPost()
223 {
224 $files = file2array('netscape_basic.htm');
225 $this->assertEquals(
226 'File netscape_basic.htm (482 bytes) was successfully processed:'
227 .' 2 links imported, 0 links overwritten, 0 links skipped.',
228 NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->pagecache)
229 );
230 $this->assertEquals(2, count($this->linkDb));
231 $this->assertEquals(1, count_private($this->linkDb));
232
233 $this->assertEquals(
234 array(
235 'linkdate' => '20001010_105536',
236 'title' => 'Secret stuff',
237 'url' => 'https://private.tld',
238 'description' => "Super-secret stuff you're not supposed to know about",
239 'private' => 1,
240 'tags' => 'private secret'
241 ),
242 $this->linkDb->getLinkFromUrl('https://private.tld')
243 );
244 $this->assertEquals(
245 array(
246 'linkdate' => '20160225_205548',
247 'title' => 'Public stuff',
248 'url' => 'http://public.tld',
249 'description' => '',
250 'private' => 0,
251 'tags' => 'public hello world'
252 ),
253 $this->linkDb->getLinkFromUrl('http://public.tld')
254 );
255 }
256
257 /**
258 * Import bookmarks with the default privacy setting (reuse from file)
259 */
260 public function testImportKeepPrivacy()
261 {
262 $post = array('privacy' => 'default');
263 $files = file2array('netscape_basic.htm');
264 $this->assertEquals(
265 'File netscape_basic.htm (482 bytes) was successfully processed:'
266 .' 2 links imported, 0 links overwritten, 0 links skipped.',
267 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
268 );
269 $this->assertEquals(2, count($this->linkDb));
270 $this->assertEquals(1, count_private($this->linkDb));
271
272 $this->assertEquals(
273 array(
274 'linkdate' => '20001010_105536',
275 'title' => 'Secret stuff',
276 'url' => 'https://private.tld',
277 'description' => "Super-secret stuff you're not supposed to know about",
278 'private' => 1,
279 'tags' => 'private secret'
280 ),
281 $this->linkDb->getLinkFromUrl('https://private.tld')
282 );
283 $this->assertEquals(
284 array(
285 'linkdate' => '20160225_205548',
286 'title' => 'Public stuff',
287 'url' => 'http://public.tld',
288 'description' => '',
289 'private' => 0,
290 'tags' => 'public hello world'
291 ),
292 $this->linkDb->getLinkFromUrl('http://public.tld')
293 );
294 }
295
296 /**
297 * Import links as public
298 */
299 public function testImportAsPublic()
300 {
301 $post = array('privacy' => 'public');
302 $files = file2array('netscape_basic.htm');
303 $this->assertEquals(
304 'File netscape_basic.htm (482 bytes) was successfully processed:'
305 .' 2 links imported, 0 links overwritten, 0 links skipped.',
306 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
307 );
308 $this->assertEquals(2, count($this->linkDb));
309 $this->assertEquals(0, count_private($this->linkDb));
310 $this->assertEquals(
311 0,
312 $this->linkDb['20001010_105536']['private']
313 );
314 $this->assertEquals(
315 0,
316 $this->linkDb['20160225_205548']['private']
317 );
318 }
319
320 /**
321 * Import links as private
322 */
323 public function testImportAsPrivate()
324 {
325 $post = array('privacy' => 'private');
326 $files = file2array('netscape_basic.htm');
327 $this->assertEquals(
328 'File netscape_basic.htm (482 bytes) was successfully processed:'
329 .' 2 links imported, 0 links overwritten, 0 links skipped.',
330 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
331 );
332 $this->assertEquals(2, count($this->linkDb));
333 $this->assertEquals(2, count_private($this->linkDb));
334 $this->assertEquals(
335 1,
336 $this->linkDb['20001010_105536']['private']
337 );
338 $this->assertEquals(
339 1,
340 $this->linkDb['20160225_205548']['private']
341 );
342 }
343
344 /**
345 * Overwrite private links so they become public
346 */
347 public function testOverwriteAsPublic()
348 {
349 $files = file2array('netscape_basic.htm');
350
351 // import links as private
352 $post = array('privacy' => 'private');
353 $this->assertEquals(
354 'File netscape_basic.htm (482 bytes) was successfully processed:'
355 .' 2 links imported, 0 links overwritten, 0 links skipped.',
356 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
357 );
358 $this->assertEquals(2, count($this->linkDb));
359 $this->assertEquals(2, count_private($this->linkDb));
360 $this->assertEquals(
361 1,
362 $this->linkDb['20001010_105536']['private']
363 );
364 $this->assertEquals(
365 1,
366 $this->linkDb['20160225_205548']['private']
367 );
368
369 // re-import as public, enable overwriting
370 $post = array(
371 'privacy' => 'public',
372 'overwrite' => 'true'
373 );
374 $this->assertEquals(
375 'File netscape_basic.htm (482 bytes) was successfully processed:'
376 .' 2 links imported, 2 links overwritten, 0 links skipped.',
377 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
378 );
379 $this->assertEquals(2, count($this->linkDb));
380 $this->assertEquals(0, count_private($this->linkDb));
381 $this->assertEquals(
382 0,
383 $this->linkDb['20001010_105536']['private']
384 );
385 $this->assertEquals(
386 0,
387 $this->linkDb['20160225_205548']['private']
388 );
389 }
390
391 /**
392 * Overwrite public links so they become private
393 */
394 public function testOverwriteAsPrivate()
395 {
396 $files = file2array('netscape_basic.htm');
397
398 // import links as public
399 $post = array('privacy' => 'public');
400 $this->assertEquals(
401 'File netscape_basic.htm (482 bytes) was successfully processed:'
402 .' 2 links imported, 0 links overwritten, 0 links skipped.',
403 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
404 );
405 $this->assertEquals(2, count($this->linkDb));
406 $this->assertEquals(0, count_private($this->linkDb));
407 $this->assertEquals(
408 0,
409 $this->linkDb['20001010_105536']['private']
410 );
411 $this->assertEquals(
412 0,
413 $this->linkDb['20160225_205548']['private']
414 );
415
416 // re-import as private, enable overwriting
417 $post = array(
418 'privacy' => 'private',
419 'overwrite' => 'true'
420 );
421 $this->assertEquals(
422 'File netscape_basic.htm (482 bytes) was successfully processed:'
423 .' 2 links imported, 2 links overwritten, 0 links skipped.',
424 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
425 );
426 $this->assertEquals(2, count($this->linkDb));
427 $this->assertEquals(2, count_private($this->linkDb));
428 $this->assertEquals(
429 1,
430 $this->linkDb['20001010_105536']['private']
431 );
432 $this->assertEquals(
433 1,
434 $this->linkDb['20160225_205548']['private']
435 );
436 }
437
438 /**
439 * Attept to import the same links twice without enabling overwriting
440 */
441 public function testSkipOverwrite()
442 {
443 $post = array('privacy' => 'public');
444 $files = file2array('netscape_basic.htm');
445 $this->assertEquals(
446 'File netscape_basic.htm (482 bytes) was successfully processed:'
447 .' 2 links imported, 0 links overwritten, 0 links skipped.',
448 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
449 );
450 $this->assertEquals(2, count($this->linkDb));
451 $this->assertEquals(0, count_private($this->linkDb));
452
453 // re-import as private, DO NOT enable overwriting
454 $post = array('privacy' => 'private');
455 $this->assertEquals(
456 'File netscape_basic.htm (482 bytes) was successfully processed:'
457 .' 0 links imported, 0 links overwritten, 2 links skipped.',
458 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
459 );
460 $this->assertEquals(2, count($this->linkDb));
461 $this->assertEquals(0, count_private($this->linkDb));
462 }
463
464 /**
465 * Add user-specified tags to all imported bookmarks
466 */
467 public function testSetDefaultTags()
468 {
469 $post = array(
470 'privacy' => 'public',
471 'default_tags' => 'tag1,tag2 tag3'
472 );
473 $files = file2array('netscape_basic.htm');
474 $this->assertEquals(
475 'File netscape_basic.htm (482 bytes) was successfully processed:'
476 .' 2 links imported, 0 links overwritten, 0 links skipped.',
477 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
478 );
479 $this->assertEquals(2, count($this->linkDb));
480 $this->assertEquals(0, count_private($this->linkDb));
481 $this->assertEquals(
482 'tag1 tag2 tag3 private secret',
483 $this->linkDb['20001010_105536']['tags']
484 );
485 $this->assertEquals(
486 'tag1 tag2 tag3 public hello world',
487 $this->linkDb['20160225_205548']['tags']
488 );
489 }
490
491 /**
492 * The user-specified tags contain characters to be escaped
493 */
494 public function testSanitizeDefaultTags()
495 {
496 $post = array(
497 'privacy' => 'public',
498 'default_tags' => 'tag1&,tag2 "tag3"'
499 );
500 $files = file2array('netscape_basic.htm');
501 $this->assertEquals(
502 'File netscape_basic.htm (482 bytes) was successfully processed:'
503 .' 2 links imported, 0 links overwritten, 0 links skipped.',
504 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
505 );
506 $this->assertEquals(2, count($this->linkDb));
507 $this->assertEquals(0, count_private($this->linkDb));
508 $this->assertEquals(
509 'tag1&amp; tag2 &quot;tag3&quot; private secret',
510 $this->linkDb['20001010_105536']['tags']
511 );
512 $this->assertEquals(
513 'tag1&amp; tag2 &quot;tag3&quot; public hello world',
514 $this->linkDb['20160225_205548']['tags']
515 );
516 }
517
518 /**
519 * Ensure each imported bookmark has a unique linkdate
520 *
521 * See https://github.com/shaarli/Shaarli/issues/351
522 */
523 public function testImportSameDate()
524 {
525 $files = file2array('same_date.htm');
526 $this->assertEquals(
527 'File same_date.htm (453 bytes) was successfully processed:'
528 .' 3 links imported, 0 links overwritten, 0 links skipped.',
529 NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->pagecache)
530 );
531 $this->assertEquals(3, count($this->linkDb));
532 $this->assertEquals(0, count_private($this->linkDb));
533 $this->assertEquals(
534 '20160225_205548',
535 $this->linkDb['20160225_205548']['linkdate']
536 );
537 $this->assertEquals(
538 '20160225_205549',
539 $this->linkDb['20160225_205549']['linkdate']
540 );
541 $this->assertEquals(
542 '20160225_205550',
543 $this->linkDb['20160225_205550']['linkdate']
544 );
545 }
546}