BookmarkImportTest.php 23 KB

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