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