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