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