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