LinkDBTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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\nnull/
  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('20100310', '20121206', '20130614', '20150310'),
  214. self::$publicLinkDB->days()
  215. );
  216. $this->assertEquals(
  217. array('20100310', '20121206', '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->allTags()
  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->allTags()
  294. );
  295. }
  296. /**
  297. * Test real_url without redirector.
  298. */
  299. public function testLinkRealUrlWithoutRedirector()
  300. {
  301. $db = new LinkDB(self::$testDatastore, false, false);
  302. foreach($db as $link) {
  303. $this->assertEquals($link['url'], $link['real_url']);
  304. }
  305. }
  306. /**
  307. * Test real_url with redirector.
  308. */
  309. public function testLinkRealUrlWithRedirector()
  310. {
  311. $redirector = 'http://redirector.to?';
  312. $db = new LinkDB(self::$testDatastore, false, false, $redirector);
  313. foreach($db as $link) {
  314. $this->assertStringStartsWith($redirector, $link['real_url']);
  315. $this->assertNotFalse(strpos($link['real_url'], urlencode('://')));
  316. }
  317. $db = new LinkDB(self::$testDatastore, false, false, $redirector, false);
  318. foreach($db as $link) {
  319. $this->assertStringStartsWith($redirector, $link['real_url']);
  320. $this->assertFalse(strpos($link['real_url'], urlencode('://')));
  321. }
  322. }
  323. /**
  324. * Test filter with string.
  325. */
  326. public function testFilterString()
  327. {
  328. $tags = 'dev cartoon';
  329. $request = array('searchtags' => $tags);
  330. $this->assertEquals(
  331. 2,
  332. count(self::$privateLinkDB->filterSearch($request, true, false))
  333. );
  334. }
  335. /**
  336. * Test filter with string.
  337. */
  338. public function testFilterArray()
  339. {
  340. $tags = array('dev', 'cartoon');
  341. $request = array('searchtags' => $tags);
  342. $this->assertEquals(
  343. 2,
  344. count(self::$privateLinkDB->filterSearch($request, true, false))
  345. );
  346. }
  347. /**
  348. * Test hidden tags feature:
  349. * tags starting with a dot '.' are only visible when logged in.
  350. */
  351. public function testHiddenTags()
  352. {
  353. $tags = '.hidden';
  354. $request = array('searchtags' => $tags);
  355. $this->assertEquals(
  356. 1,
  357. count(self::$privateLinkDB->filterSearch($request, true, false))
  358. );
  359. $this->assertEquals(
  360. 0,
  361. count(self::$publicLinkDB->filterSearch($request, true, false))
  362. );
  363. }
  364. /**
  365. * Test filterHash() with a valid smallhash.
  366. */
  367. public function testFilterHashValid()
  368. {
  369. $request = smallHash('20150310_114651');
  370. $this->assertEquals(
  371. 1,
  372. count(self::$publicLinkDB->filterHash($request))
  373. );
  374. $request = smallHash('20150310_114633' . 8);
  375. $this->assertEquals(
  376. 1,
  377. count(self::$publicLinkDB->filterHash($request))
  378. );
  379. }
  380. /**
  381. * Test filterHash() with an invalid smallhash.
  382. *
  383. * @expectedException LinkNotFoundException
  384. */
  385. public function testFilterHashInValid1()
  386. {
  387. $request = 'blabla';
  388. self::$publicLinkDB->filterHash($request);
  389. }
  390. /**
  391. * Test filterHash() with an empty smallhash.
  392. *
  393. * @expectedException LinkNotFoundException
  394. */
  395. public function testFilterHashInValid()
  396. {
  397. self::$publicLinkDB->filterHash('');
  398. }
  399. /**
  400. * Test reorder with asc/desc parameter.
  401. */
  402. public function testReorderLinksDesc()
  403. {
  404. self::$privateLinkDB->reorder('ASC');
  405. $linkIds = array(42, 4, 1, 0, 7, 6, 8, 41);
  406. $cpt = 0;
  407. foreach (self::$privateLinkDB as $key => $value) {
  408. $this->assertEquals($linkIds[$cpt++], $key);
  409. }
  410. self::$privateLinkDB->reorder('DESC');
  411. $linkIds = array_reverse($linkIds);
  412. $cpt = 0;
  413. foreach (self::$privateLinkDB as $key => $value) {
  414. $this->assertEquals($linkIds[$cpt++], $key);
  415. }
  416. }
  417. }