LinkDBTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. 'title'=>'an additional link',
  166. 'url'=>'http://dum.my',
  167. 'description'=>'One more',
  168. 'private'=>0,
  169. 'linkdate'=>'20150518_190000',
  170. 'tags'=>'unit test'
  171. );
  172. $testDB[$link['linkdate']] = $link;
  173. $testDB->save('tests');
  174. $testDB = new LinkDB(self::$testDatastore, true, false);
  175. $this->assertEquals($dbSize + 1, sizeof($testDB));
  176. }
  177. /**
  178. * Count existing links
  179. */
  180. public function testCount()
  181. {
  182. $this->assertEquals(
  183. self::$refDB->countPublicLinks(),
  184. self::$publicLinkDB->count()
  185. );
  186. $this->assertEquals(
  187. self::$refDB->countLinks(),
  188. self::$privateLinkDB->count()
  189. );
  190. }
  191. /**
  192. * Count existing links - public links hidden
  193. */
  194. public function testCountHiddenPublic()
  195. {
  196. $linkDB = new LinkDB(self::$testDatastore, false, true);
  197. $this->assertEquals(
  198. 0,
  199. $linkDB->count()
  200. );
  201. $this->assertEquals(
  202. 0,
  203. $linkDB->count()
  204. );
  205. }
  206. /**
  207. * List the days for which links have been posted
  208. */
  209. public function testDays()
  210. {
  211. $this->assertEquals(
  212. array('20121206', '20130614', '20150310'),
  213. self::$publicLinkDB->days()
  214. );
  215. $this->assertEquals(
  216. array('20121206', '20130614', '20141125', '20150310'),
  217. self::$privateLinkDB->days()
  218. );
  219. }
  220. /**
  221. * The URL corresponds to an existing entry in the DB
  222. */
  223. public function testGetKnownLinkFromURL()
  224. {
  225. $link = self::$publicLinkDB->getLinkFromUrl('http://mediagoblin.org/');
  226. $this->assertNotEquals(false, $link);
  227. $this->assertContains(
  228. 'A free software media publishing platform',
  229. $link['description']
  230. );
  231. }
  232. /**
  233. * The URL is not in the DB
  234. */
  235. public function testGetUnknownLinkFromURL()
  236. {
  237. $this->assertEquals(
  238. false,
  239. self::$publicLinkDB->getLinkFromUrl('http://dev.null')
  240. );
  241. }
  242. /**
  243. * Lists all tags
  244. */
  245. public function testAllTags()
  246. {
  247. $this->assertEquals(
  248. array(
  249. 'web' => 3,
  250. 'cartoon' => 2,
  251. 'gnu' => 2,
  252. 'dev' => 1,
  253. 'samba' => 1,
  254. 'media' => 1,
  255. 'software' => 1,
  256. 'stallman' => 1,
  257. 'free' => 1,
  258. '-exclude' => 1,
  259. // The DB contains a link with `sTuff` and another one with `stuff` tag.
  260. // They need to be grouped with the first case found (`sTuff`).
  261. 'sTuff' => 2,
  262. 'hashtag' => 2,
  263. ),
  264. self::$publicLinkDB->allTags()
  265. );
  266. $this->assertEquals(
  267. array(
  268. 'web' => 4,
  269. 'cartoon' => 3,
  270. 'gnu' => 2,
  271. 'dev' => 2,
  272. 'samba' => 1,
  273. 'media' => 1,
  274. 'software' => 1,
  275. 'stallman' => 1,
  276. 'free' => 1,
  277. 'html' => 1,
  278. 'w3c' => 1,
  279. 'css' => 1,
  280. 'Mercurial' => 1,
  281. 'sTuff' => 2,
  282. '-exclude' => 1,
  283. '.hidden' => 1,
  284. 'hashtag' => 2,
  285. 'tag1' => 1,
  286. 'tag2' => 1,
  287. 'tag3' => 1,
  288. 'tag4' => 1,
  289. ),
  290. self::$privateLinkDB->allTags()
  291. );
  292. }
  293. /**
  294. * Test real_url without redirector.
  295. */
  296. public function testLinkRealUrlWithoutRedirector()
  297. {
  298. $db = new LinkDB(self::$testDatastore, false, false);
  299. foreach($db as $link) {
  300. $this->assertEquals($link['url'], $link['real_url']);
  301. }
  302. }
  303. /**
  304. * Test real_url with redirector.
  305. */
  306. public function testLinkRealUrlWithRedirector()
  307. {
  308. $redirector = 'http://redirector.to?';
  309. $db = new LinkDB(self::$testDatastore, false, false, $redirector);
  310. foreach($db as $link) {
  311. $this->assertStringStartsWith($redirector, $link['real_url']);
  312. $this->assertNotFalse(strpos($link['real_url'], urlencode('://')));
  313. }
  314. $db = new LinkDB(self::$testDatastore, false, false, $redirector, false);
  315. foreach($db as $link) {
  316. $this->assertStringStartsWith($redirector, $link['real_url']);
  317. $this->assertFalse(strpos($link['real_url'], urlencode('://')));
  318. }
  319. }
  320. /**
  321. * Test filter with string.
  322. */
  323. public function testFilterString()
  324. {
  325. $tags = 'dev cartoon';
  326. $request = array('searchtags' => $tags);
  327. $this->assertEquals(
  328. 2,
  329. count(self::$privateLinkDB->filterSearch($request, true, false))
  330. );
  331. }
  332. /**
  333. * Test filter with string.
  334. */
  335. public function testFilterArray()
  336. {
  337. $tags = array('dev', 'cartoon');
  338. $request = array('searchtags' => $tags);
  339. $this->assertEquals(
  340. 2,
  341. count(self::$privateLinkDB->filterSearch($request, true, false))
  342. );
  343. }
  344. /**
  345. * Test hidden tags feature:
  346. * tags starting with a dot '.' are only visible when logged in.
  347. */
  348. public function testHiddenTags()
  349. {
  350. $tags = '.hidden';
  351. $request = array('searchtags' => $tags);
  352. $this->assertEquals(
  353. 1,
  354. count(self::$privateLinkDB->filterSearch($request, true, false))
  355. );
  356. $this->assertEquals(
  357. 0,
  358. count(self::$publicLinkDB->filterSearch($request, true, false))
  359. );
  360. }
  361. /**
  362. * Test filterHash() with a valid smallhash.
  363. */
  364. public function testFilterHashValid()
  365. {
  366. $request = smallHash('20150310_114651');
  367. $this->assertEquals(
  368. 1,
  369. count(self::$publicLinkDB->filterHash($request))
  370. );
  371. }
  372. /**
  373. * Test filterHash() with an invalid smallhash.
  374. *
  375. * @expectedException LinkNotFoundException
  376. */
  377. public function testFilterHashInValid1()
  378. {
  379. $request = 'blabla';
  380. self::$publicLinkDB->filterHash($request);
  381. }
  382. /**
  383. * Test filterHash() with an empty smallhash.
  384. *
  385. * @expectedException LinkNotFoundException
  386. */
  387. public function testFilterHashInValid()
  388. {
  389. self::$publicLinkDB->filterHash('');
  390. }
  391. }