LinkDBTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. <?php
  2. /**
  3. * Link datastore tests
  4. */
  5. require_once 'application/Cache.php';
  6. require_once 'application/FileUtils.php';
  7. require_once 'application/LinkDB.php';
  8. require_once 'application/Utils.php';
  9. require_once 'tests/utils/ReferenceLinkDB.php';
  10. /**
  11. * Unitary tests for LinkDB
  12. */
  13. class LinkDBTest extends PHPUnit_Framework_TestCase
  14. {
  15. // datastore to test write operations
  16. protected static $testDatastore = 'sandbox/datastore.php';
  17. /**
  18. * @var ReferenceLinkDB instance.
  19. */
  20. protected static $refDB = null;
  21. /**
  22. * @var LinkDB public LinkDB instance.
  23. */
  24. protected static $publicLinkDB = null;
  25. /**
  26. * @var LinkDB private LinkDB instance.
  27. */
  28. protected static $privateLinkDB = null;
  29. /**
  30. * Instantiates public and private LinkDBs with test data
  31. *
  32. * The reference datastore contains public and private links that
  33. * will be used to test LinkDB's methods:
  34. * - access filtering (public/private),
  35. * - link searches:
  36. * - by day,
  37. * - by tag,
  38. * - by text,
  39. * - etc.
  40. */
  41. public static function setUpBeforeClass()
  42. {
  43. self::$refDB = new ReferenceLinkDB();
  44. self::$refDB->write(self::$testDatastore);
  45. self::$publicLinkDB = new LinkDB(self::$testDatastore, false, false);
  46. self::$privateLinkDB = new LinkDB(self::$testDatastore, true, false);
  47. }
  48. /**
  49. * Resets test data for each test
  50. */
  51. protected function setUp()
  52. {
  53. if (file_exists(self::$testDatastore)) {
  54. unlink(self::$testDatastore);
  55. }
  56. }
  57. /**
  58. * Allows to test LinkDB's private methods
  59. *
  60. * @see
  61. * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html
  62. * http://stackoverflow.com/a/2798203
  63. */
  64. protected static function getMethod($name)
  65. {
  66. $class = new ReflectionClass('LinkDB');
  67. $method = $class->getMethod($name);
  68. $method->setAccessible(true);
  69. return $method;
  70. }
  71. /**
  72. * Instantiate LinkDB objects - logged in user
  73. */
  74. public function testConstructLoggedIn()
  75. {
  76. new LinkDB(self::$testDatastore, true, false);
  77. $this->assertFileExists(self::$testDatastore);
  78. }
  79. /**
  80. * Instantiate LinkDB objects - logged out or public instance
  81. */
  82. public function testConstructLoggedOut()
  83. {
  84. new LinkDB(self::$testDatastore, false, false);
  85. $this->assertFileExists(self::$testDatastore);
  86. }
  87. /**
  88. * Attempt to instantiate a LinkDB whereas the datastore is not writable
  89. *
  90. * @expectedException IOException
  91. * @expectedExceptionMessageRegExp /Error accessing "null"/
  92. */
  93. public function testConstructDatastoreNotWriteable()
  94. {
  95. new LinkDB('null/store.db', false, false);
  96. }
  97. /**
  98. * The DB doesn't exist, ensure it is created with dummy content
  99. */
  100. public function testCheckDBNew()
  101. {
  102. $linkDB = new LinkDB(self::$testDatastore, false, false);
  103. unlink(self::$testDatastore);
  104. $this->assertFileNotExists(self::$testDatastore);
  105. $checkDB = self::getMethod('check');
  106. $checkDB->invokeArgs($linkDB, array());
  107. $this->assertFileExists(self::$testDatastore);
  108. // ensure the correct data has been written
  109. $this->assertGreaterThan(0, filesize(self::$testDatastore));
  110. }
  111. /**
  112. * The DB exists, don't do anything
  113. */
  114. public function testCheckDBLoad()
  115. {
  116. $linkDB = new LinkDB(self::$testDatastore, false, false);
  117. $datastoreSize = filesize(self::$testDatastore);
  118. $this->assertGreaterThan(0, $datastoreSize);
  119. $checkDB = self::getMethod('check');
  120. $checkDB->invokeArgs($linkDB, array());
  121. // ensure the datastore is left unmodified
  122. $this->assertEquals(
  123. $datastoreSize,
  124. filesize(self::$testDatastore)
  125. );
  126. }
  127. /**
  128. * Load an empty DB
  129. */
  130. public function testReadEmptyDB()
  131. {
  132. file_put_contents(self::$testDatastore, '<?php /* S7QysKquBQA= */ ?>');
  133. $emptyDB = new LinkDB(self::$testDatastore, false, false);
  134. $this->assertEquals(0, sizeof($emptyDB));
  135. $this->assertEquals(0, count($emptyDB));
  136. }
  137. /**
  138. * Load public links from the DB
  139. */
  140. public function testReadPublicDB()
  141. {
  142. $this->assertEquals(
  143. self::$refDB->countPublicLinks(),
  144. sizeof(self::$publicLinkDB)
  145. );
  146. }
  147. /**
  148. * Load public and private links from the DB
  149. */
  150. public function testReadPrivateDB()
  151. {
  152. $this->assertEquals(
  153. self::$refDB->countLinks(),
  154. sizeof(self::$privateLinkDB)
  155. );
  156. }
  157. /**
  158. * Save the links to the DB
  159. */
  160. public function testSave()
  161. {
  162. $testDB = new LinkDB(self::$testDatastore, true, false);
  163. $dbSize = sizeof($testDB);
  164. $link = array(
  165. 'id' => 42,
  166. 'title'=>'an additional link',
  167. 'url'=>'http://dum.my',
  168. 'description'=>'One more',
  169. 'private'=>0,
  170. 'created'=> DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150518_190000'),
  171. 'tags'=>'unit test'
  172. );
  173. $testDB[$link['id']] = $link;
  174. $testDB->save('tests');
  175. $testDB = new LinkDB(self::$testDatastore, true, false);
  176. $this->assertEquals($dbSize + 1, sizeof($testDB));
  177. }
  178. /**
  179. * Count existing links
  180. */
  181. public function testCount()
  182. {
  183. $this->assertEquals(
  184. self::$refDB->countPublicLinks(),
  185. self::$publicLinkDB->count()
  186. );
  187. $this->assertEquals(
  188. self::$refDB->countLinks(),
  189. self::$privateLinkDB->count()
  190. );
  191. }
  192. /**
  193. * Count existing links - public links hidden
  194. */
  195. public function testCountHiddenPublic()
  196. {
  197. $linkDB = new LinkDB(self::$testDatastore, false, true);
  198. $this->assertEquals(
  199. 0,
  200. $linkDB->count()
  201. );
  202. $this->assertEquals(
  203. 0,
  204. $linkDB->count()
  205. );
  206. }
  207. /**
  208. * List the days for which links have been posted
  209. */
  210. public function testDays()
  211. {
  212. $this->assertEquals(
  213. array('20100309', '20100310', '20121206', '20121207', '20130614', '20150310'),
  214. self::$publicLinkDB->days()
  215. );
  216. $this->assertEquals(
  217. array('20100309', '20100310', '20121206', '20121207', '20130614', '20141125', '20150310'),
  218. self::$privateLinkDB->days()
  219. );
  220. }
  221. /**
  222. * The URL corresponds to an existing entry in the DB
  223. */
  224. public function testGetKnownLinkFromURL()
  225. {
  226. $link = self::$publicLinkDB->getLinkFromUrl('http://mediagoblin.org/');
  227. $this->assertNotEquals(false, $link);
  228. $this->assertContains(
  229. 'A free software media publishing platform',
  230. $link['description']
  231. );
  232. }
  233. /**
  234. * The URL is not in the DB
  235. */
  236. public function testGetUnknownLinkFromURL()
  237. {
  238. $this->assertEquals(
  239. false,
  240. self::$publicLinkDB->getLinkFromUrl('http://dev.null')
  241. );
  242. }
  243. /**
  244. * Lists all tags
  245. */
  246. public function testAllTags()
  247. {
  248. $this->assertEquals(
  249. array(
  250. 'web' => 3,
  251. 'cartoon' => 2,
  252. 'gnu' => 2,
  253. 'dev' => 1,
  254. 'samba' => 1,
  255. 'media' => 1,
  256. 'software' => 1,
  257. 'stallman' => 1,
  258. 'free' => 1,
  259. '-exclude' => 1,
  260. 'hashtag' => 2,
  261. // The DB contains a link with `sTuff` and another one with `stuff` tag.
  262. // They need to be grouped with the first case found - order by date DESC: `sTuff`.
  263. 'sTuff' => 2,
  264. 'ut' => 1,
  265. ),
  266. self::$publicLinkDB->linksCountPerTag()
  267. );
  268. $this->assertEquals(
  269. array(
  270. 'web' => 4,
  271. 'cartoon' => 3,
  272. 'gnu' => 2,
  273. 'dev' => 2,
  274. 'samba' => 1,
  275. 'media' => 1,
  276. 'software' => 1,
  277. 'stallman' => 1,
  278. 'free' => 1,
  279. 'html' => 1,
  280. 'w3c' => 1,
  281. 'css' => 1,
  282. 'Mercurial' => 1,
  283. 'sTuff' => 2,
  284. '-exclude' => 1,
  285. '.hidden' => 1,
  286. 'hashtag' => 2,
  287. 'tag1' => 1,
  288. 'tag2' => 1,
  289. 'tag3' => 1,
  290. 'tag4' => 1,
  291. 'ut' => 1,
  292. ),
  293. self::$privateLinkDB->linksCountPerTag()
  294. );
  295. $this->assertEquals(
  296. array(
  297. 'web' => 4,
  298. 'cartoon' => 2,
  299. 'gnu' => 1,
  300. 'dev' => 1,
  301. 'samba' => 1,
  302. 'media' => 1,
  303. 'html' => 1,
  304. 'w3c' => 1,
  305. 'css' => 1,
  306. 'Mercurial' => 1,
  307. '.hidden' => 1,
  308. 'hashtag' => 1,
  309. ),
  310. self::$privateLinkDB->linksCountPerTag(['web'])
  311. );
  312. $this->assertEquals(
  313. array(
  314. 'web' => 1,
  315. 'html' => 1,
  316. 'w3c' => 1,
  317. 'css' => 1,
  318. 'Mercurial' => 1,
  319. ),
  320. self::$privateLinkDB->linksCountPerTag(['web'], 'private')
  321. );
  322. }
  323. /**
  324. * Test real_url without redirector.
  325. */
  326. public function testLinkRealUrlWithoutRedirector()
  327. {
  328. $db = new LinkDB(self::$testDatastore, false, false);
  329. foreach ($db as $link) {
  330. $this->assertEquals($link['url'], $link['real_url']);
  331. }
  332. }
  333. /**
  334. * Test real_url with redirector.
  335. */
  336. public function testLinkRealUrlWithRedirector()
  337. {
  338. $redirector = 'http://redirector.to?';
  339. $db = new LinkDB(self::$testDatastore, false, false, $redirector);
  340. foreach ($db as $link) {
  341. $this->assertStringStartsWith($redirector, $link['real_url']);
  342. $this->assertNotFalse(strpos($link['real_url'], urlencode('://')));
  343. }
  344. $db = new LinkDB(self::$testDatastore, false, false, $redirector, false);
  345. foreach ($db as $link) {
  346. $this->assertStringStartsWith($redirector, $link['real_url']);
  347. $this->assertFalse(strpos($link['real_url'], urlencode('://')));
  348. }
  349. }
  350. /**
  351. * Test filter with string.
  352. */
  353. public function testFilterString()
  354. {
  355. $tags = 'dev cartoon';
  356. $request = array('searchtags' => $tags);
  357. $this->assertEquals(
  358. 2,
  359. count(self::$privateLinkDB->filterSearch($request, true, false))
  360. );
  361. }
  362. /**
  363. * Test filter with string.
  364. */
  365. public function testFilterArray()
  366. {
  367. $tags = array('dev', 'cartoon');
  368. $request = array('searchtags' => $tags);
  369. $this->assertEquals(
  370. 2,
  371. count(self::$privateLinkDB->filterSearch($request, true, false))
  372. );
  373. }
  374. /**
  375. * Test hidden tags feature:
  376. * tags starting with a dot '.' are only visible when logged in.
  377. */
  378. public function testHiddenTags()
  379. {
  380. $tags = '.hidden';
  381. $request = array('searchtags' => $tags);
  382. $this->assertEquals(
  383. 1,
  384. count(self::$privateLinkDB->filterSearch($request, true, false))
  385. );
  386. $this->assertEquals(
  387. 0,
  388. count(self::$publicLinkDB->filterSearch($request, true, false))
  389. );
  390. }
  391. /**
  392. * Test filterHash() with a valid smallhash.
  393. */
  394. public function testFilterHashValid()
  395. {
  396. $request = smallHash('20150310_114651');
  397. $this->assertEquals(
  398. 1,
  399. count(self::$publicLinkDB->filterHash($request))
  400. );
  401. $request = smallHash('20150310_114633' . 8);
  402. $this->assertEquals(
  403. 1,
  404. count(self::$publicLinkDB->filterHash($request))
  405. );
  406. }
  407. /**
  408. * Test filterHash() with an invalid smallhash.
  409. *
  410. * @expectedException LinkNotFoundException
  411. */
  412. public function testFilterHashInValid1()
  413. {
  414. $request = 'blabla';
  415. self::$publicLinkDB->filterHash($request);
  416. }
  417. /**
  418. * Test filterHash() with an empty smallhash.
  419. *
  420. * @expectedException LinkNotFoundException
  421. */
  422. public function testFilterHashInValid()
  423. {
  424. self::$publicLinkDB->filterHash('');
  425. }
  426. /**
  427. * Test reorder with asc/desc parameter.
  428. */
  429. public function testReorderLinksDesc()
  430. {
  431. self::$privateLinkDB->reorder('ASC');
  432. $stickyIds = [11, 10];
  433. $standardIds = [42, 4, 9, 1, 0, 7, 6, 8, 41];
  434. $linkIds = array_merge($stickyIds, $standardIds);
  435. $cpt = 0;
  436. foreach (self::$privateLinkDB as $key => $value) {
  437. $this->assertEquals($linkIds[$cpt++], $key);
  438. }
  439. self::$privateLinkDB->reorder('DESC');
  440. $linkIds = array_merge(array_reverse($stickyIds), array_reverse($standardIds));
  441. $cpt = 0;
  442. foreach (self::$privateLinkDB as $key => $value) {
  443. $this->assertEquals($linkIds[$cpt++], $key);
  444. }
  445. }
  446. /**
  447. * Test rename tag with a valid value present in multiple links
  448. */
  449. public function testRenameTagMultiple()
  450. {
  451. self::$refDB->write(self::$testDatastore);
  452. $linkDB = new LinkDB(self::$testDatastore, true, false);
  453. $res = $linkDB->renameTag('cartoon', 'Taz');
  454. $this->assertEquals(3, count($res));
  455. $this->assertContains(' Taz ', $linkDB[4]['tags']);
  456. $this->assertContains(' Taz ', $linkDB[1]['tags']);
  457. $this->assertContains(' Taz ', $linkDB[0]['tags']);
  458. }
  459. /**
  460. * Test rename tag with a valid value
  461. */
  462. public function testRenameTagCaseSensitive()
  463. {
  464. self::$refDB->write(self::$testDatastore);
  465. $linkDB = new LinkDB(self::$testDatastore, true, false, '');
  466. $res = $linkDB->renameTag('sTuff', 'Taz');
  467. $this->assertEquals(1, count($res));
  468. $this->assertEquals('Taz', $linkDB[41]['tags']);
  469. }
  470. /**
  471. * Test rename tag with invalid values
  472. */
  473. public function testRenameTagInvalid()
  474. {
  475. $linkDB = new LinkDB(self::$testDatastore, false, false);
  476. $this->assertFalse($linkDB->renameTag('', 'test'));
  477. $this->assertFalse($linkDB->renameTag('', ''));
  478. // tag non existent
  479. $this->assertEquals([], $linkDB->renameTag('test', ''));
  480. $this->assertEquals([], $linkDB->renameTag('test', 'retest'));
  481. }
  482. /**
  483. * Test delete tag with a valid value
  484. */
  485. public function testDeleteTag()
  486. {
  487. self::$refDB->write(self::$testDatastore);
  488. $linkDB = new LinkDB(self::$testDatastore, true, false);
  489. $res = $linkDB->renameTag('cartoon', null);
  490. $this->assertEquals(3, count($res));
  491. $this->assertNotContains('cartoon', $linkDB[4]['tags']);
  492. }
  493. /**
  494. * Test linksCountPerTag all tags without filter.
  495. * Equal occurrences should be sorted alphabetically.
  496. */
  497. public function testCountLinkPerTagAllNoFilter()
  498. {
  499. $expected = [
  500. 'web' => 4,
  501. 'cartoon' => 3,
  502. 'dev' => 2,
  503. 'gnu' => 2,
  504. 'hashtag' => 2,
  505. 'sTuff' => 2,
  506. '-exclude' => 1,
  507. '.hidden' => 1,
  508. 'Mercurial' => 1,
  509. 'css' => 1,
  510. 'free' => 1,
  511. 'html' => 1,
  512. 'media' => 1,
  513. 'samba' => 1,
  514. 'software' => 1,
  515. 'stallman' => 1,
  516. 'tag1' => 1,
  517. 'tag2' => 1,
  518. 'tag3' => 1,
  519. 'tag4' => 1,
  520. 'ut' => 1,
  521. 'w3c' => 1,
  522. ];
  523. $tags = self::$privateLinkDB->linksCountPerTag();
  524. $this->assertEquals($expected, $tags, var_export($tags, true));
  525. }
  526. /**
  527. * Test linksCountPerTag all tags with filter.
  528. * Equal occurrences should be sorted alphabetically.
  529. */
  530. public function testCountLinkPerTagAllWithFilter()
  531. {
  532. $expected = [
  533. 'gnu' => 2,
  534. 'hashtag' => 2,
  535. '-exclude' => 1,
  536. '.hidden' => 1,
  537. 'free' => 1,
  538. 'media' => 1,
  539. 'software' => 1,
  540. 'stallman' => 1,
  541. 'stuff' => 1,
  542. 'web' => 1,
  543. ];
  544. $tags = self::$privateLinkDB->linksCountPerTag(['gnu']);
  545. $this->assertEquals($expected, $tags, var_export($tags, true));
  546. }
  547. /**
  548. * Test linksCountPerTag public tags with filter.
  549. * Equal occurrences should be sorted alphabetically.
  550. */
  551. public function testCountLinkPerTagPublicWithFilter()
  552. {
  553. $expected = [
  554. 'gnu' => 2,
  555. 'hashtag' => 2,
  556. '-exclude' => 1,
  557. '.hidden' => 1,
  558. 'free' => 1,
  559. 'media' => 1,
  560. 'software' => 1,
  561. 'stallman' => 1,
  562. 'stuff' => 1,
  563. 'web' => 1,
  564. ];
  565. $tags = self::$privateLinkDB->linksCountPerTag(['gnu'], 'public');
  566. $this->assertEquals($expected, $tags, var_export($tags, true));
  567. }
  568. /**
  569. * Test linksCountPerTag public tags with filter.
  570. * Equal occurrences should be sorted alphabetically.
  571. */
  572. public function testCountLinkPerTagPrivateWithFilter()
  573. {
  574. $expected = [
  575. 'cartoon' => 1,
  576. 'dev' => 1,
  577. 'tag1' => 1,
  578. 'tag2' => 1,
  579. 'tag3' => 1,
  580. 'tag4' => 1,
  581. ];
  582. $tags = self::$privateLinkDB->linksCountPerTag(['dev'], 'private');
  583. $this->assertEquals($expected, $tags, var_export($tags, true));
  584. }
  585. }