BookmarkImportTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. <?php
  2. require_once 'application/NetscapeBookmarkUtils.php';
  3. /**
  4. * Utility function to load a file's metadata in a $_FILES-like array
  5. *
  6. * @param string $filename Basename of the file
  7. *
  8. * @return array A $_FILES-like array
  9. */
  10. function file2array($filename)
  11. {
  12. return array(
  13. 'filetoupload' => array(
  14. 'name' => $filename,
  15. 'tmp_name' => __DIR__ . '/input/' . $filename,
  16. 'size' => filesize(__DIR__ . '/input/' . $filename)
  17. )
  18. );
  19. }
  20. /**
  21. * Netscape bookmark import
  22. */
  23. class BookmarkImportTest extends PHPUnit_Framework_TestCase
  24. {
  25. /**
  26. * @var string datastore to test write operations
  27. */
  28. protected static $testDatastore = 'sandbox/datastore.php';
  29. /**
  30. * @var LinkDB private LinkDB instance
  31. */
  32. protected $linkDb = null;
  33. /**
  34. * @var string Dummy page cache
  35. */
  36. protected $pagecache = 'tests';
  37. /**
  38. * Resets test data before each test
  39. */
  40. protected function setUp()
  41. {
  42. if (file_exists(self::$testDatastore)) {
  43. unlink(self::$testDatastore);
  44. }
  45. // start with an empty datastore
  46. file_put_contents(self::$testDatastore, '<?php /* S7QysKquBQA= */ ?>');
  47. $this->linkDb = new LinkDB(self::$testDatastore, true, false);
  48. }
  49. /**
  50. * Attempt to import bookmarks from an empty file
  51. */
  52. public function testImportEmptyData()
  53. {
  54. $files = file2array('empty.htm');
  55. $this->assertEquals(
  56. 'File empty.htm (0 bytes) has an unknown file format.'
  57. .' Nothing was imported.',
  58. NetscapeBookmarkUtils::import(NULL, $files, NULL, NULL)
  59. );
  60. $this->assertEquals(0, count($this->linkDb));
  61. }
  62. /**
  63. * Attempt to import bookmarks from a file with no Doctype
  64. */
  65. public function testImportNoDoctype()
  66. {
  67. $files = file2array('no_doctype.htm');
  68. $this->assertEquals(
  69. 'File no_doctype.htm (350 bytes) has an unknown file format. Nothing was imported.',
  70. NetscapeBookmarkUtils::import(NULL, $files, NULL, NULL)
  71. );
  72. $this->assertEquals(0, count($this->linkDb));
  73. }
  74. /**
  75. * Ensure IE dumps are supported
  76. */
  77. public function testImportInternetExplorerEncoding()
  78. {
  79. $files = file2array('internet_explorer_encoding.htm');
  80. $this->assertEquals(
  81. 'File internet_explorer_encoding.htm (356 bytes) was successfully processed:'
  82. .' 1 links imported, 0 links overwritten, 0 links skipped.',
  83. NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->pagecache)
  84. );
  85. $this->assertEquals(1, count($this->linkDb));
  86. $this->assertEquals(0, count_private($this->linkDb));
  87. $this->assertEquals(
  88. array(
  89. 'linkdate' => '20160618_173944',
  90. 'title' => 'Hg Init a Mercurial tutorial by Joel Spolsky',
  91. 'url' => 'http://hginit.com/',
  92. 'description' => '',
  93. 'private' => 0,
  94. 'tags' => ''
  95. ),
  96. $this->linkDb->getLinkFromUrl('http://hginit.com/')
  97. );
  98. }
  99. /**
  100. * Import bookmarks nested in a folder hierarchy
  101. */
  102. public function testImportNested()
  103. {
  104. $files = file2array('netscape_nested.htm');
  105. $this->assertEquals(
  106. 'File netscape_nested.htm (1337 bytes) was successfully processed:'
  107. .' 8 links imported, 0 links overwritten, 0 links skipped.',
  108. NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->pagecache)
  109. );
  110. $this->assertEquals(8, count($this->linkDb));
  111. $this->assertEquals(2, count_private($this->linkDb));
  112. $this->assertEquals(
  113. array(
  114. 'linkdate' => '20160225_205541',
  115. 'title' => 'Nested 1',
  116. 'url' => 'http://nest.ed/1',
  117. 'description' => '',
  118. 'private' => 0,
  119. 'tags' => 'tag1 tag2'
  120. ),
  121. $this->linkDb->getLinkFromUrl('http://nest.ed/1')
  122. );
  123. $this->assertEquals(
  124. array(
  125. 'linkdate' => '20160225_205542',
  126. 'title' => 'Nested 1-1',
  127. 'url' => 'http://nest.ed/1-1',
  128. 'description' => '',
  129. 'private' => 0,
  130. 'tags' => 'folder1 tag1 tag2'
  131. ),
  132. $this->linkDb->getLinkFromUrl('http://nest.ed/1-1')
  133. );
  134. $this->assertEquals(
  135. array(
  136. 'linkdate' => '20160225_205547',
  137. 'title' => 'Nested 1-2',
  138. 'url' => 'http://nest.ed/1-2',
  139. 'description' => '',
  140. 'private' => 0,
  141. 'tags' => 'folder1 tag3 tag4'
  142. ),
  143. $this->linkDb->getLinkFromUrl('http://nest.ed/1-2')
  144. );
  145. $this->assertEquals(
  146. array(
  147. 'linkdate' => '20160202_172222',
  148. 'title' => 'Nested 2-1',
  149. 'url' => 'http://nest.ed/2-1',
  150. 'description' => 'First link of the second section',
  151. 'private' => 1,
  152. 'tags' => 'folder2'
  153. ),
  154. $this->linkDb->getLinkFromUrl('http://nest.ed/2-1')
  155. );
  156. $this->assertEquals(
  157. array(
  158. 'linkdate' => '20160119_200227',
  159. 'title' => 'Nested 2-2',
  160. 'url' => 'http://nest.ed/2-2',
  161. 'description' => 'Second link of the second section',
  162. 'private' => 1,
  163. 'tags' => 'folder2'
  164. ),
  165. $this->linkDb->getLinkFromUrl('http://nest.ed/2-2')
  166. );
  167. $this->assertEquals(
  168. array(
  169. 'linkdate' => '20160202_172223',
  170. 'title' => 'Nested 3-1',
  171. 'url' => 'http://nest.ed/3-1',
  172. 'description' => '',
  173. 'private' => 0,
  174. 'tags' => 'folder3 folder3-1 tag3'
  175. ),
  176. $this->linkDb->getLinkFromUrl('http://nest.ed/3-1')
  177. );
  178. $this->assertEquals(
  179. array(
  180. 'linkdate' => '20160119_200228',
  181. 'title' => 'Nested 3-2',
  182. 'url' => 'http://nest.ed/3-2',
  183. 'description' => '',
  184. 'private' => 0,
  185. 'tags' => 'folder3 folder3-1'
  186. ),
  187. $this->linkDb->getLinkFromUrl('http://nest.ed/3-2')
  188. );
  189. $this->assertEquals(
  190. array(
  191. 'linkdate' => '20160229_081541',
  192. 'title' => 'Nested 2',
  193. 'url' => 'http://nest.ed/2',
  194. 'description' => '',
  195. 'private' => 0,
  196. 'tags' => 'tag4'
  197. ),
  198. $this->linkDb->getLinkFromUrl('http://nest.ed/2')
  199. );
  200. }
  201. /**
  202. * Import bookmarks with the default privacy setting (reuse from file)
  203. *
  204. * The $_POST array is not set.
  205. */
  206. public function testImportDefaultPrivacyNoPost()
  207. {
  208. $files = file2array('netscape_basic.htm');
  209. $this->assertEquals(
  210. 'File netscape_basic.htm (482 bytes) was successfully processed:'
  211. .' 2 links imported, 0 links overwritten, 0 links skipped.',
  212. NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->pagecache)
  213. );
  214. $this->assertEquals(2, count($this->linkDb));
  215. $this->assertEquals(1, count_private($this->linkDb));
  216. $this->assertEquals(
  217. array(
  218. 'linkdate' => '20001010_105536',
  219. 'title' => 'Secret stuff',
  220. 'url' => 'https://private.tld',
  221. 'description' => "Super-secret stuff you're not supposed to know about",
  222. 'private' => 1,
  223. 'tags' => 'private secret'
  224. ),
  225. $this->linkDb->getLinkFromUrl('https://private.tld')
  226. );
  227. $this->assertEquals(
  228. array(
  229. 'linkdate' => '20160225_205548',
  230. 'title' => 'Public stuff',
  231. 'url' => 'http://public.tld',
  232. 'description' => '',
  233. 'private' => 0,
  234. 'tags' => 'public hello world'
  235. ),
  236. $this->linkDb->getLinkFromUrl('http://public.tld')
  237. );
  238. }
  239. /**
  240. * Import bookmarks with the default privacy setting (reuse from file)
  241. */
  242. public function testImportKeepPrivacy()
  243. {
  244. $post = array('privacy' => 'default');
  245. $files = file2array('netscape_basic.htm');
  246. $this->assertEquals(
  247. 'File netscape_basic.htm (482 bytes) was successfully processed:'
  248. .' 2 links imported, 0 links overwritten, 0 links skipped.',
  249. NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
  250. );
  251. $this->assertEquals(2, count($this->linkDb));
  252. $this->assertEquals(1, count_private($this->linkDb));
  253. $this->assertEquals(
  254. array(
  255. 'linkdate' => '20001010_105536',
  256. 'title' => 'Secret stuff',
  257. 'url' => 'https://private.tld',
  258. 'description' => "Super-secret stuff you're not supposed to know about",
  259. 'private' => 1,
  260. 'tags' => 'private secret'
  261. ),
  262. $this->linkDb->getLinkFromUrl('https://private.tld')
  263. );
  264. $this->assertEquals(
  265. array(
  266. 'linkdate' => '20160225_205548',
  267. 'title' => 'Public stuff',
  268. 'url' => 'http://public.tld',
  269. 'description' => '',
  270. 'private' => 0,
  271. 'tags' => 'public hello world'
  272. ),
  273. $this->linkDb->getLinkFromUrl('http://public.tld')
  274. );
  275. }
  276. /**
  277. * Import links as public
  278. */
  279. public function testImportAsPublic()
  280. {
  281. $post = array('privacy' => 'public');
  282. $files = file2array('netscape_basic.htm');
  283. $this->assertEquals(
  284. 'File netscape_basic.htm (482 bytes) was successfully processed:'
  285. .' 2 links imported, 0 links overwritten, 0 links skipped.',
  286. NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
  287. );
  288. $this->assertEquals(2, count($this->linkDb));
  289. $this->assertEquals(0, count_private($this->linkDb));
  290. $this->assertEquals(
  291. 0,
  292. $this->linkDb['20001010_105536']['private']
  293. );
  294. $this->assertEquals(
  295. 0,
  296. $this->linkDb['20160225_205548']['private']
  297. );
  298. }
  299. /**
  300. * Import links as private
  301. */
  302. public function testImportAsPrivate()
  303. {
  304. $post = array('privacy' => 'private');
  305. $files = file2array('netscape_basic.htm');
  306. $this->assertEquals(
  307. 'File netscape_basic.htm (482 bytes) was successfully processed:'
  308. .' 2 links imported, 0 links overwritten, 0 links skipped.',
  309. NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
  310. );
  311. $this->assertEquals(2, count($this->linkDb));
  312. $this->assertEquals(2, count_private($this->linkDb));
  313. $this->assertEquals(
  314. 1,
  315. $this->linkDb['20001010_105536']['private']
  316. );
  317. $this->assertEquals(
  318. 1,
  319. $this->linkDb['20160225_205548']['private']
  320. );
  321. }
  322. /**
  323. * Overwrite private links so they become public
  324. */
  325. public function testOverwriteAsPublic()
  326. {
  327. $files = file2array('netscape_basic.htm');
  328. // import links as private
  329. $post = array('privacy' => 'private');
  330. $this->assertEquals(
  331. 'File netscape_basic.htm (482 bytes) was successfully processed:'
  332. .' 2 links imported, 0 links overwritten, 0 links skipped.',
  333. NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
  334. );
  335. $this->assertEquals(2, count($this->linkDb));
  336. $this->assertEquals(2, count_private($this->linkDb));
  337. $this->assertEquals(
  338. 1,
  339. $this->linkDb['20001010_105536']['private']
  340. );
  341. $this->assertEquals(
  342. 1,
  343. $this->linkDb['20160225_205548']['private']
  344. );
  345. // re-import as public, enable overwriting
  346. $post = array(
  347. 'privacy' => 'public',
  348. 'overwrite' => 'true'
  349. );
  350. $this->assertEquals(
  351. 'File netscape_basic.htm (482 bytes) was successfully processed:'
  352. .' 2 links imported, 2 links overwritten, 0 links skipped.',
  353. NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
  354. );
  355. $this->assertEquals(2, count($this->linkDb));
  356. $this->assertEquals(0, count_private($this->linkDb));
  357. $this->assertEquals(
  358. 0,
  359. $this->linkDb['20001010_105536']['private']
  360. );
  361. $this->assertEquals(
  362. 0,
  363. $this->linkDb['20160225_205548']['private']
  364. );
  365. }
  366. /**
  367. * Overwrite public links so they become private
  368. */
  369. public function testOverwriteAsPrivate()
  370. {
  371. $files = file2array('netscape_basic.htm');
  372. // import links as public
  373. $post = array('privacy' => 'public');
  374. $this->assertEquals(
  375. 'File netscape_basic.htm (482 bytes) was successfully processed:'
  376. .' 2 links imported, 0 links overwritten, 0 links skipped.',
  377. NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
  378. );
  379. $this->assertEquals(2, count($this->linkDb));
  380. $this->assertEquals(0, count_private($this->linkDb));
  381. $this->assertEquals(
  382. 0,
  383. $this->linkDb['20001010_105536']['private']
  384. );
  385. $this->assertEquals(
  386. 0,
  387. $this->linkDb['20160225_205548']['private']
  388. );
  389. // re-import as private, enable overwriting
  390. $post = array(
  391. 'privacy' => 'private',
  392. 'overwrite' => 'true'
  393. );
  394. $this->assertEquals(
  395. 'File netscape_basic.htm (482 bytes) was successfully processed:'
  396. .' 2 links imported, 2 links overwritten, 0 links skipped.',
  397. NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
  398. );
  399. $this->assertEquals(2, count($this->linkDb));
  400. $this->assertEquals(2, count_private($this->linkDb));
  401. $this->assertEquals(
  402. 1,
  403. $this->linkDb['20001010_105536']['private']
  404. );
  405. $this->assertEquals(
  406. 1,
  407. $this->linkDb['20160225_205548']['private']
  408. );
  409. }
  410. /**
  411. * Attept to import the same links twice without enabling overwriting
  412. */
  413. public function testSkipOverwrite()
  414. {
  415. $post = array('privacy' => 'public');
  416. $files = file2array('netscape_basic.htm');
  417. $this->assertEquals(
  418. 'File netscape_basic.htm (482 bytes) was successfully processed:'
  419. .' 2 links imported, 0 links overwritten, 0 links skipped.',
  420. NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
  421. );
  422. $this->assertEquals(2, count($this->linkDb));
  423. $this->assertEquals(0, count_private($this->linkDb));
  424. // re-import as private, DO NOT enable overwriting
  425. $post = array('privacy' => 'private');
  426. $this->assertEquals(
  427. 'File netscape_basic.htm (482 bytes) was successfully processed:'
  428. .' 0 links imported, 0 links overwritten, 2 links skipped.',
  429. NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
  430. );
  431. $this->assertEquals(2, count($this->linkDb));
  432. $this->assertEquals(0, count_private($this->linkDb));
  433. }
  434. /**
  435. * Add user-specified tags to all imported bookmarks
  436. */
  437. public function testSetDefaultTags()
  438. {
  439. $post = array(
  440. 'privacy' => 'public',
  441. 'default_tags' => 'tag1,tag2 tag3'
  442. );
  443. $files = file2array('netscape_basic.htm');
  444. $this->assertEquals(
  445. 'File netscape_basic.htm (482 bytes) was successfully processed:'
  446. .' 2 links imported, 0 links overwritten, 0 links skipped.',
  447. NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
  448. );
  449. $this->assertEquals(2, count($this->linkDb));
  450. $this->assertEquals(0, count_private($this->linkDb));
  451. $this->assertEquals(
  452. 'tag1 tag2 tag3 private secret',
  453. $this->linkDb['20001010_105536']['tags']
  454. );
  455. $this->assertEquals(
  456. 'tag1 tag2 tag3 public hello world',
  457. $this->linkDb['20160225_205548']['tags']
  458. );
  459. }
  460. /**
  461. * The user-specified tags contain characters to be escaped
  462. */
  463. public function testSanitizeDefaultTags()
  464. {
  465. $post = array(
  466. 'privacy' => 'public',
  467. 'default_tags' => 'tag1&,tag2 "tag3"'
  468. );
  469. $files = file2array('netscape_basic.htm');
  470. $this->assertEquals(
  471. 'File netscape_basic.htm (482 bytes) was successfully processed:'
  472. .' 2 links imported, 0 links overwritten, 0 links skipped.',
  473. NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->pagecache)
  474. );
  475. $this->assertEquals(2, count($this->linkDb));
  476. $this->assertEquals(0, count_private($this->linkDb));
  477. $this->assertEquals(
  478. 'tag1&amp; tag2 &quot;tag3&quot; private secret',
  479. $this->linkDb['20001010_105536']['tags']
  480. );
  481. $this->assertEquals(
  482. 'tag1&amp; tag2 &quot;tag3&quot; public hello world',
  483. $this->linkDb['20160225_205548']['tags']
  484. );
  485. }
  486. /**
  487. * Ensure each imported bookmark has a unique linkdate
  488. *
  489. * See https://github.com/shaarli/Shaarli/issues/351
  490. */
  491. public function testImportSameDate()
  492. {
  493. $files = file2array('same_date.htm');
  494. $this->assertEquals(
  495. 'File same_date.htm (453 bytes) was successfully processed:'
  496. .' 3 links imported, 0 links overwritten, 0 links skipped.',
  497. NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->pagecache)
  498. );
  499. $this->assertEquals(3, count($this->linkDb));
  500. $this->assertEquals(0, count_private($this->linkDb));
  501. $this->assertEquals(
  502. '20160225_205548',
  503. $this->linkDb['20160225_205548']['linkdate']
  504. );
  505. $this->assertEquals(
  506. '20160225_205549',
  507. $this->linkDb['20160225_205549']['linkdate']
  508. );
  509. $this->assertEquals(
  510. '20160225_205550',
  511. $this->linkDb['20160225_205550']['linkdate']
  512. );
  513. }
  514. }