]>
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 | ||
3ff1ce47 A |
129 | /** |
130 | * Attempt to import bookmarks from a file with a lowercase Doctype | |
131 | */ | |
132 | public function testImportLowecaseDoctype() | |
133 | { | |
134 | $files = file2array('lowercase_doctype.htm'); | |
135 | $this->assertStringMatchesFormat( | |
136 | 'File lowercase_doctype.htm (386 bytes) was successfully processed in %d seconds:' | |
137 | .' 2 links imported, 0 links overwritten, 0 links skipped.', | |
138 | NetscapeBookmarkUtils::import(null, $files, $this->linkDb, $this->conf, $this->history) | |
139 | ); | |
140 | $this->assertEquals(2, count($this->linkDb)); | |
141 | } | |
142 | ||
143 | ||
f4ad7bde V |
144 | /** |
145 | * Ensure IE dumps are supported | |
146 | */ | |
147 | public function testImportInternetExplorerEncoding() | |
148 | { | |
149 | $files = file2array('internet_explorer_encoding.htm'); | |
66e74d50 A |
150 | $this->assertStringMatchesFormat( |
151 | 'File internet_explorer_encoding.htm (356 bytes) was successfully processed in %d seconds:' | |
f4ad7bde | 152 | .' 1 links imported, 0 links overwritten, 0 links skipped.', |
4306b184 | 153 | NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history) |
f4ad7bde V |
154 | ); |
155 | $this->assertEquals(1, count($this->linkDb)); | |
156 | $this->assertEquals(0, count_private($this->linkDb)); | |
157 | ||
158 | $this->assertEquals( | |
159 | array( | |
c3dfd899 | 160 | 'id' => 0, |
d592daea | 161 | 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160618_203944'), |
f4ad7bde V |
162 | 'title' => 'Hg Init a Mercurial tutorial by Joel Spolsky', |
163 | 'url' => 'http://hginit.com/', | |
164 | 'description' => '', | |
165 | 'private' => 0, | |
c3dfd899 A |
166 | 'tags' => '', |
167 | 'shorturl' => 'La37cg', | |
f4ad7bde V |
168 | ), |
169 | $this->linkDb->getLinkFromUrl('http://hginit.com/') | |
170 | ); | |
171 | } | |
172 | ||
a973afea V |
173 | /** |
174 | * Import bookmarks nested in a folder hierarchy | |
175 | */ | |
176 | public function testImportNested() | |
177 | { | |
178 | $files = file2array('netscape_nested.htm'); | |
66e74d50 A |
179 | $this->assertStringMatchesFormat( |
180 | 'File netscape_nested.htm (1337 bytes) was successfully processed in %d seconds:' | |
a973afea | 181 | .' 8 links imported, 0 links overwritten, 0 links skipped.', |
4306b184 | 182 | NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history) |
a973afea V |
183 | ); |
184 | $this->assertEquals(8, count($this->linkDb)); | |
185 | $this->assertEquals(2, count_private($this->linkDb)); | |
186 | ||
187 | $this->assertEquals( | |
188 | array( | |
c3dfd899 | 189 | 'id' => 0, |
d592daea | 190 | 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235541'), |
a973afea V |
191 | 'title' => 'Nested 1', |
192 | 'url' => 'http://nest.ed/1', | |
193 | 'description' => '', | |
194 | 'private' => 0, | |
c3dfd899 A |
195 | 'tags' => 'tag1 tag2', |
196 | 'shorturl' => 'KyDNKA', | |
a973afea V |
197 | ), |
198 | $this->linkDb->getLinkFromUrl('http://nest.ed/1') | |
199 | ); | |
200 | $this->assertEquals( | |
201 | array( | |
c3dfd899 | 202 | 'id' => 1, |
d592daea | 203 | 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235542'), |
a973afea V |
204 | 'title' => 'Nested 1-1', |
205 | 'url' => 'http://nest.ed/1-1', | |
206 | 'description' => '', | |
207 | 'private' => 0, | |
c3dfd899 A |
208 | 'tags' => 'folder1 tag1 tag2', |
209 | 'shorturl' => 'T2LnXg', | |
a973afea V |
210 | ), |
211 | $this->linkDb->getLinkFromUrl('http://nest.ed/1-1') | |
212 | ); | |
213 | $this->assertEquals( | |
214 | array( | |
c3dfd899 | 215 | 'id' => 2, |
d592daea | 216 | 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235547'), |
a973afea V |
217 | 'title' => 'Nested 1-2', |
218 | 'url' => 'http://nest.ed/1-2', | |
219 | 'description' => '', | |
220 | 'private' => 0, | |
c3dfd899 A |
221 | 'tags' => 'folder1 tag3 tag4', |
222 | 'shorturl' => '46SZxA', | |
a973afea V |
223 | ), |
224 | $this->linkDb->getLinkFromUrl('http://nest.ed/1-2') | |
225 | ); | |
226 | $this->assertEquals( | |
227 | array( | |
c3dfd899 | 228 | 'id' => 3, |
d592daea | 229 | 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160202_202222'), |
a973afea V |
230 | 'title' => 'Nested 2-1', |
231 | 'url' => 'http://nest.ed/2-1', | |
232 | 'description' => 'First link of the second section', | |
233 | 'private' => 1, | |
c3dfd899 A |
234 | 'tags' => 'folder2', |
235 | 'shorturl' => '4UHOSw', | |
a973afea V |
236 | ), |
237 | $this->linkDb->getLinkFromUrl('http://nest.ed/2-1') | |
238 | ); | |
239 | $this->assertEquals( | |
240 | array( | |
c3dfd899 | 241 | 'id' => 4, |
d592daea | 242 | 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160119_230227'), |
a973afea V |
243 | 'title' => 'Nested 2-2', |
244 | 'url' => 'http://nest.ed/2-2', | |
245 | 'description' => 'Second link of the second section', | |
246 | 'private' => 1, | |
c3dfd899 A |
247 | 'tags' => 'folder2', |
248 | 'shorturl' => 'yfzwbw', | |
a973afea V |
249 | ), |
250 | $this->linkDb->getLinkFromUrl('http://nest.ed/2-2') | |
251 | ); | |
252 | $this->assertEquals( | |
253 | array( | |
c3dfd899 | 254 | 'id' => 5, |
d592daea | 255 | 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160202_202222'), |
a973afea V |
256 | 'title' => 'Nested 3-1', |
257 | 'url' => 'http://nest.ed/3-1', | |
258 | 'description' => '', | |
259 | 'private' => 0, | |
c3dfd899 A |
260 | 'tags' => 'folder3 folder3-1 tag3', |
261 | 'shorturl' => 'UwxIUQ', | |
a973afea V |
262 | ), |
263 | $this->linkDb->getLinkFromUrl('http://nest.ed/3-1') | |
264 | ); | |
265 | $this->assertEquals( | |
266 | array( | |
c3dfd899 | 267 | 'id' => 6, |
d592daea | 268 | 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160119_230227'), |
a973afea V |
269 | 'title' => 'Nested 3-2', |
270 | 'url' => 'http://nest.ed/3-2', | |
271 | 'description' => '', | |
272 | 'private' => 0, | |
c3dfd899 A |
273 | 'tags' => 'folder3 folder3-1', |
274 | 'shorturl' => 'p8dyZg', | |
a973afea V |
275 | ), |
276 | $this->linkDb->getLinkFromUrl('http://nest.ed/3-2') | |
277 | ); | |
278 | $this->assertEquals( | |
279 | array( | |
c3dfd899 | 280 | 'id' => 7, |
d592daea | 281 | 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160229_111541'), |
a973afea V |
282 | 'title' => 'Nested 2', |
283 | 'url' => 'http://nest.ed/2', | |
284 | 'description' => '', | |
285 | 'private' => 0, | |
c3dfd899 A |
286 | 'tags' => 'tag4', |
287 | 'shorturl' => 'Gt3Uug', | |
a973afea V |
288 | ), |
289 | $this->linkDb->getLinkFromUrl('http://nest.ed/2') | |
290 | ); | |
291 | } | |
292 | ||
293 | /** | |
294 | * Import bookmarks with the default privacy setting (reuse from file) | |
295 | * | |
296 | * The $_POST array is not set. | |
297 | */ | |
298 | public function testImportDefaultPrivacyNoPost() | |
299 | { | |
300 | $files = file2array('netscape_basic.htm'); | |
66e74d50 A |
301 | $this->assertStringMatchesFormat( |
302 | 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' | |
a973afea | 303 | .' 2 links imported, 0 links overwritten, 0 links skipped.', |
4306b184 | 304 | NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history) |
a973afea | 305 | ); |
c3dfd899 | 306 | |
a973afea V |
307 | $this->assertEquals(2, count($this->linkDb)); |
308 | $this->assertEquals(1, count_private($this->linkDb)); | |
309 | ||
310 | $this->assertEquals( | |
311 | array( | |
c3dfd899 A |
312 | 'id' => 0, |
313 | // Old link - UTC+4 (note that TZ in the import file is ignored). | |
d592daea | 314 | 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20001010_135536'), |
a973afea V |
315 | 'title' => 'Secret stuff', |
316 | 'url' => 'https://private.tld', | |
317 | 'description' => "Super-secret stuff you're not supposed to know about", | |
318 | 'private' => 1, | |
c3dfd899 A |
319 | 'tags' => 'private secret', |
320 | 'shorturl' => 'EokDtA', | |
a973afea V |
321 | ), |
322 | $this->linkDb->getLinkFromUrl('https://private.tld') | |
323 | ); | |
324 | $this->assertEquals( | |
325 | array( | |
c3dfd899 | 326 | 'id' => 1, |
d592daea | 327 | 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235548'), |
a973afea V |
328 | 'title' => 'Public stuff', |
329 | 'url' => 'http://public.tld', | |
330 | 'description' => '', | |
331 | 'private' => 0, | |
c3dfd899 A |
332 | 'tags' => 'public hello world', |
333 | 'shorturl' => 'Er9ddA', | |
a973afea V |
334 | ), |
335 | $this->linkDb->getLinkFromUrl('http://public.tld') | |
336 | ); | |
337 | } | |
338 | ||
339 | /** | |
340 | * Import bookmarks with the default privacy setting (reuse from file) | |
341 | */ | |
342 | public function testImportKeepPrivacy() | |
343 | { | |
344 | $post = array('privacy' => 'default'); | |
345 | $files = file2array('netscape_basic.htm'); | |
66e74d50 A |
346 | $this->assertStringMatchesFormat( |
347 | 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' | |
a973afea | 348 | .' 2 links imported, 0 links overwritten, 0 links skipped.', |
4306b184 | 349 | NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) |
a973afea V |
350 | ); |
351 | $this->assertEquals(2, count($this->linkDb)); | |
352 | $this->assertEquals(1, count_private($this->linkDb)); | |
353 | ||
354 | $this->assertEquals( | |
355 | array( | |
c3dfd899 A |
356 | 'id' => 0, |
357 | // Note that TZ in the import file is ignored. | |
d592daea | 358 | 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20001010_135536'), |
a973afea V |
359 | 'title' => 'Secret stuff', |
360 | 'url' => 'https://private.tld', | |
361 | 'description' => "Super-secret stuff you're not supposed to know about", | |
362 | 'private' => 1, | |
c3dfd899 A |
363 | 'tags' => 'private secret', |
364 | 'shorturl' => 'EokDtA', | |
a973afea V |
365 | ), |
366 | $this->linkDb->getLinkFromUrl('https://private.tld') | |
367 | ); | |
368 | $this->assertEquals( | |
369 | array( | |
c3dfd899 | 370 | 'id' => 1, |
d592daea | 371 | 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235548'), |
a973afea V |
372 | 'title' => 'Public stuff', |
373 | 'url' => 'http://public.tld', | |
374 | 'description' => '', | |
375 | 'private' => 0, | |
c3dfd899 A |
376 | 'tags' => 'public hello world', |
377 | 'shorturl' => 'Er9ddA', | |
a973afea V |
378 | ), |
379 | $this->linkDb->getLinkFromUrl('http://public.tld') | |
380 | ); | |
381 | } | |
382 | ||
383 | /** | |
384 | * Import links as public | |
385 | */ | |
386 | public function testImportAsPublic() | |
387 | { | |
388 | $post = array('privacy' => 'public'); | |
389 | $files = file2array('netscape_basic.htm'); | |
66e74d50 A |
390 | $this->assertStringMatchesFormat( |
391 | 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' | |
a973afea | 392 | .' 2 links imported, 0 links overwritten, 0 links skipped.', |
4306b184 | 393 | NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) |
a973afea V |
394 | ); |
395 | $this->assertEquals(2, count($this->linkDb)); | |
396 | $this->assertEquals(0, count_private($this->linkDb)); | |
397 | $this->assertEquals( | |
398 | 0, | |
c3dfd899 | 399 | $this->linkDb[0]['private'] |
a973afea V |
400 | ); |
401 | $this->assertEquals( | |
402 | 0, | |
c3dfd899 | 403 | $this->linkDb[1]['private'] |
a973afea V |
404 | ); |
405 | } | |
406 | ||
407 | /** | |
408 | * Import links as private | |
409 | */ | |
410 | public function testImportAsPrivate() | |
411 | { | |
412 | $post = array('privacy' => 'private'); | |
413 | $files = file2array('netscape_basic.htm'); | |
66e74d50 A |
414 | $this->assertStringMatchesFormat( |
415 | 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' | |
a973afea | 416 | .' 2 links imported, 0 links overwritten, 0 links skipped.', |
4306b184 | 417 | NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) |
a973afea V |
418 | ); |
419 | $this->assertEquals(2, count($this->linkDb)); | |
420 | $this->assertEquals(2, count_private($this->linkDb)); | |
421 | $this->assertEquals( | |
422 | 1, | |
c3dfd899 | 423 | $this->linkDb['0']['private'] |
a973afea V |
424 | ); |
425 | $this->assertEquals( | |
426 | 1, | |
c3dfd899 | 427 | $this->linkDb['1']['private'] |
a973afea V |
428 | ); |
429 | } | |
430 | ||
431 | /** | |
432 | * Overwrite private links so they become public | |
433 | */ | |
434 | public function testOverwriteAsPublic() | |
435 | { | |
436 | $files = file2array('netscape_basic.htm'); | |
437 | ||
438 | // import links as private | |
439 | $post = array('privacy' => 'private'); | |
66e74d50 A |
440 | $this->assertStringMatchesFormat( |
441 | 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' | |
a973afea | 442 | .' 2 links imported, 0 links overwritten, 0 links skipped.', |
4306b184 | 443 | NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) |
a973afea V |
444 | ); |
445 | $this->assertEquals(2, count($this->linkDb)); | |
446 | $this->assertEquals(2, count_private($this->linkDb)); | |
447 | $this->assertEquals( | |
448 | 1, | |
c3dfd899 | 449 | $this->linkDb[0]['private'] |
a973afea V |
450 | ); |
451 | $this->assertEquals( | |
452 | 1, | |
c3dfd899 | 453 | $this->linkDb[1]['private'] |
a973afea | 454 | ); |
a973afea V |
455 | // re-import as public, enable overwriting |
456 | $post = array( | |
457 | 'privacy' => 'public', | |
458 | 'overwrite' => 'true' | |
459 | ); | |
66e74d50 A |
460 | $this->assertStringMatchesFormat( |
461 | 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' | |
a973afea | 462 | .' 2 links imported, 2 links overwritten, 0 links skipped.', |
4306b184 | 463 | NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) |
a973afea V |
464 | ); |
465 | $this->assertEquals(2, count($this->linkDb)); | |
466 | $this->assertEquals(0, count_private($this->linkDb)); | |
467 | $this->assertEquals( | |
468 | 0, | |
c3dfd899 | 469 | $this->linkDb[0]['private'] |
a973afea V |
470 | ); |
471 | $this->assertEquals( | |
472 | 0, | |
c3dfd899 | 473 | $this->linkDb[1]['private'] |
a973afea V |
474 | ); |
475 | } | |
476 | ||
477 | /** | |
478 | * Overwrite public links so they become private | |
479 | */ | |
480 | public function testOverwriteAsPrivate() | |
481 | { | |
482 | $files = file2array('netscape_basic.htm'); | |
483 | ||
484 | // import links as public | |
485 | $post = array('privacy' => 'public'); | |
66e74d50 A |
486 | $this->assertStringMatchesFormat( |
487 | 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' | |
a973afea | 488 | .' 2 links imported, 0 links overwritten, 0 links skipped.', |
4306b184 | 489 | NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) |
a973afea V |
490 | ); |
491 | $this->assertEquals(2, count($this->linkDb)); | |
492 | $this->assertEquals(0, count_private($this->linkDb)); | |
493 | $this->assertEquals( | |
494 | 0, | |
c3dfd899 | 495 | $this->linkDb['0']['private'] |
a973afea V |
496 | ); |
497 | $this->assertEquals( | |
498 | 0, | |
c3dfd899 | 499 | $this->linkDb['1']['private'] |
a973afea V |
500 | ); |
501 | ||
502 | // re-import as private, enable overwriting | |
503 | $post = array( | |
504 | 'privacy' => 'private', | |
505 | 'overwrite' => 'true' | |
506 | ); | |
66e74d50 A |
507 | $this->assertStringMatchesFormat( |
508 | 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' | |
a973afea | 509 | .' 2 links imported, 2 links overwritten, 0 links skipped.', |
4306b184 | 510 | NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) |
a973afea V |
511 | ); |
512 | $this->assertEquals(2, count($this->linkDb)); | |
513 | $this->assertEquals(2, count_private($this->linkDb)); | |
514 | $this->assertEquals( | |
515 | 1, | |
c3dfd899 | 516 | $this->linkDb['0']['private'] |
a973afea V |
517 | ); |
518 | $this->assertEquals( | |
519 | 1, | |
c3dfd899 | 520 | $this->linkDb['1']['private'] |
a973afea V |
521 | ); |
522 | } | |
523 | ||
524 | /** | |
525 | * Attept to import the same links twice without enabling overwriting | |
526 | */ | |
527 | public function testSkipOverwrite() | |
528 | { | |
529 | $post = array('privacy' => 'public'); | |
530 | $files = file2array('netscape_basic.htm'); | |
66e74d50 A |
531 | $this->assertStringMatchesFormat( |
532 | 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' | |
a973afea | 533 | .' 2 links imported, 0 links overwritten, 0 links skipped.', |
4306b184 | 534 | NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) |
a973afea V |
535 | ); |
536 | $this->assertEquals(2, count($this->linkDb)); | |
537 | $this->assertEquals(0, count_private($this->linkDb)); | |
538 | ||
539 | // re-import as private, DO NOT enable overwriting | |
540 | $post = array('privacy' => 'private'); | |
66e74d50 A |
541 | $this->assertStringMatchesFormat( |
542 | 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' | |
a973afea | 543 | .' 0 links imported, 0 links overwritten, 2 links skipped.', |
4306b184 | 544 | NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) |
a973afea V |
545 | ); |
546 | $this->assertEquals(2, count($this->linkDb)); | |
547 | $this->assertEquals(0, count_private($this->linkDb)); | |
548 | } | |
549 | ||
550 | /** | |
551 | * Add user-specified tags to all imported bookmarks | |
552 | */ | |
553 | public function testSetDefaultTags() | |
554 | { | |
555 | $post = array( | |
556 | 'privacy' => 'public', | |
557 | 'default_tags' => 'tag1,tag2 tag3' | |
558 | ); | |
559 | $files = file2array('netscape_basic.htm'); | |
66e74d50 A |
560 | $this->assertStringMatchesFormat( |
561 | 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' | |
a973afea | 562 | .' 2 links imported, 0 links overwritten, 0 links skipped.', |
4306b184 | 563 | NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) |
a973afea V |
564 | ); |
565 | $this->assertEquals(2, count($this->linkDb)); | |
566 | $this->assertEquals(0, count_private($this->linkDb)); | |
567 | $this->assertEquals( | |
568 | 'tag1 tag2 tag3 private secret', | |
c3dfd899 | 569 | $this->linkDb['0']['tags'] |
a973afea V |
570 | ); |
571 | $this->assertEquals( | |
572 | 'tag1 tag2 tag3 public hello world', | |
c3dfd899 | 573 | $this->linkDb['1']['tags'] |
a973afea V |
574 | ); |
575 | } | |
576 | ||
577 | /** | |
578 | * The user-specified tags contain characters to be escaped | |
579 | */ | |
580 | public function testSanitizeDefaultTags() | |
581 | { | |
582 | $post = array( | |
583 | 'privacy' => 'public', | |
584 | 'default_tags' => 'tag1&,tag2 "tag3"' | |
585 | ); | |
586 | $files = file2array('netscape_basic.htm'); | |
66e74d50 A |
587 | $this->assertStringMatchesFormat( |
588 | 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' | |
a973afea | 589 | .' 2 links imported, 0 links overwritten, 0 links skipped.', |
4306b184 | 590 | NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) |
a973afea V |
591 | ); |
592 | $this->assertEquals(2, count($this->linkDb)); | |
593 | $this->assertEquals(0, count_private($this->linkDb)); | |
594 | $this->assertEquals( | |
595 | 'tag1& tag2 "tag3" private secret', | |
c3dfd899 | 596 | $this->linkDb['0']['tags'] |
a973afea V |
597 | ); |
598 | $this->assertEquals( | |
599 | 'tag1& tag2 "tag3" public hello world', | |
c3dfd899 | 600 | $this->linkDb['1']['tags'] |
a973afea V |
601 | ); |
602 | } | |
603 | ||
604 | /** | |
c3dfd899 | 605 | * Ensure each imported bookmark has a unique id |
a973afea V |
606 | * |
607 | * See https://github.com/shaarli/Shaarli/issues/351 | |
608 | */ | |
609 | public function testImportSameDate() | |
610 | { | |
611 | $files = file2array('same_date.htm'); | |
66e74d50 A |
612 | $this->assertStringMatchesFormat( |
613 | 'File same_date.htm (453 bytes) was successfully processed in %d seconds:' | |
a973afea | 614 | .' 3 links imported, 0 links overwritten, 0 links skipped.', |
4306b184 | 615 | NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->conf, $this->history) |
a973afea V |
616 | ); |
617 | $this->assertEquals(3, count($this->linkDb)); | |
618 | $this->assertEquals(0, count_private($this->linkDb)); | |
619 | $this->assertEquals( | |
c3dfd899 A |
620 | 0, |
621 | $this->linkDb[0]['id'] | |
a973afea V |
622 | ); |
623 | $this->assertEquals( | |
c3dfd899 A |
624 | 1, |
625 | $this->linkDb[1]['id'] | |
a973afea V |
626 | ); |
627 | $this->assertEquals( | |
c3dfd899 A |
628 | 2, |
629 | $this->linkDb[2]['id'] | |
a973afea V |
630 | ); |
631 | } | |
4306b184 A |
632 | |
633 | public function testImportCreateUpdateHistory() | |
634 | { | |
635 | $post = [ | |
636 | 'privacy' => 'public', | |
637 | 'overwrite' => 'true', | |
638 | ]; | |
639 | $files = file2array('netscape_basic.htm'); | |
4306b184 A |
640 | NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history); |
641 | $history = $this->history->getHistory(); | |
66e74d50 A |
642 | $this->assertEquals(1, count($history)); |
643 | $this->assertEquals(History::IMPORT, $history[0]['event']); | |
644 | $this->assertTrue(new DateTime('-5 seconds') < $history[0]['datetime']); | |
4306b184 A |
645 | |
646 | // re-import as private, enable overwriting | |
647 | NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history); | |
648 | $history = $this->history->getHistory(); | |
66e74d50 A |
649 | $this->assertEquals(2, count($history)); |
650 | $this->assertEquals(History::IMPORT, $history[0]['event']); | |
651 | $this->assertTrue(new DateTime('-5 seconds') < $history[0]['datetime']); | |
652 | $this->assertEquals(History::IMPORT, $history[1]['event']); | |
653 | $this->assertTrue(new DateTime('-5 seconds') < $history[1]['datetime']); | |
4306b184 | 654 | } |
a973afea | 655 | } |