6 require_once 'application/Cache.php';
7 require_once 'application/FileUtils.php';
8 require_once 'application/LinkDB.php';
9 require_once 'application/Utils.php';
10 require_once 'tests/utils/ReferenceLinkDB.php';
14 * Unitary tests for LinkDB
16 class LinkDBTest
extends PHPUnit_Framework_TestCase
18 // datastore to test write operations
19 protected static $testDatastore = 'sandbox/datastore.php';
22 * @var ReferenceLinkDB instance.
24 protected static $refDB = null;
27 * @var LinkDB public LinkDB instance.
29 protected static $publicLinkDB = null;
32 * @var LinkDB private LinkDB instance.
34 protected static $privateLinkDB = null;
37 * Instantiates public and private LinkDBs with test data
39 * The reference datastore contains public and private links that
40 * will be used to test LinkDB's methods:
41 * - access filtering (public/private),
48 public static function setUpBeforeClass()
50 self
::$refDB = new ReferenceLinkDB();
51 self
::$refDB->write(self
::$testDatastore);
53 self
::$publicLinkDB = new LinkDB(self
::$testDatastore, false, false);
54 self
::$privateLinkDB = new LinkDB(self
::$testDatastore, true, false);
58 * Resets test data for each test
60 protected function setUp()
62 if (file_exists(self
::$testDatastore)) {
63 unlink(self
::$testDatastore);
68 * Allows to test LinkDB's private methods
71 * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html
72 * http://stackoverflow.com/a/2798203
74 protected static function getMethod($name)
76 $class = new ReflectionClass('LinkDB');
77 $method = $class->getMethod($name);
78 $method->setAccessible(true);
83 * Instantiate LinkDB objects - logged in user
85 public function testConstructLoggedIn()
87 new LinkDB(self
::$testDatastore, true, false);
88 $this->assertFileExists(self
::$testDatastore);
92 * Instantiate LinkDB objects - logged out or public instance
94 public function testConstructLoggedOut()
96 new LinkDB(self
::$testDatastore, false, false);
97 $this->assertFileExists(self
::$testDatastore);
101 * Attempt to instantiate a LinkDB whereas the datastore is not writable
103 * @expectedException IOException
104 * @expectedExceptionMessageRegExp /Error accessing\nnull/
106 public function testConstructDatastoreNotWriteable()
108 new LinkDB('null/store.db', false, false);
112 * The DB doesn't exist, ensure it is created with dummy content
114 public function testCheckDBNew()
116 $linkDB = new LinkDB(self
::$testDatastore, false, false);
117 unlink(self
::$testDatastore);
118 $this->assertFileNotExists(self
::$testDatastore);
120 $checkDB = self
::getMethod('check');
121 $checkDB->invokeArgs($linkDB, array());
122 $this->assertFileExists(self
::$testDatastore);
124 // ensure the correct data has been written
125 $this->assertGreaterThan(0, filesize(self
::$testDatastore));
129 * The DB exists, don't do anything
131 public function testCheckDBLoad()
133 $linkDB = new LinkDB(self
::$testDatastore, false, false);
134 $datastoreSize = filesize(self
::$testDatastore);
135 $this->assertGreaterThan(0, $datastoreSize);
137 $checkDB = self
::getMethod('check');
138 $checkDB->invokeArgs($linkDB, array());
140 // ensure the datastore is left unmodified
143 filesize(self
::$testDatastore)
150 public function testReadEmptyDB()
152 file_put_contents(self
::$testDatastore, '<?php /* S7QysKquBQA= */ ?>');
153 $emptyDB = new LinkDB(self
::$testDatastore, false, false);
154 $this->assertEquals(0, sizeof($emptyDB));
155 $this->assertEquals(0, count($emptyDB));
159 * Load public links from the DB
161 public function testReadPublicDB()
164 self
::$refDB->countPublicLinks(),
165 sizeof(self
::$publicLinkDB)
170 * Load public and private links from the DB
172 public function testReadPrivateDB()
175 self
::$refDB->countLinks(),
176 sizeof(self
::$privateLinkDB)
181 * Save the links to the DB
183 public function testSave()
185 $testDB = new LinkDB(self
::$testDatastore, true, false);
186 $dbSize = sizeof($testDB);
189 'title'=>'an additional link',
190 'url'=>'http://dum.my',
191 'description'=>'One more',
193 'linkdate'=>'20150518_190000',
196 $testDB[$link['linkdate']] = $link;
197 $testDB->save('tests');
199 $testDB = new LinkDB(self
::$testDatastore, true, false);
200 $this->assertEquals($dbSize +
1, sizeof($testDB));
204 * Count existing links
206 public function testCount()
209 self
::$refDB->countPublicLinks(),
210 self
::$publicLinkDB->count()
213 self
::$refDB->countLinks(),
214 self
::$privateLinkDB->count()
219 * Count existing links - public links hidden
221 public function testCountHiddenPublic()
223 $linkDB = new LinkDB(self
::$testDatastore, false, true);
236 * List the days for which links have been posted
238 public function testDays()
241 array('20121206', '20130614', '20150310'),
242 self
::$publicLinkDB->days()
246 array('20121206', '20130614', '20141125', '20150310'),
247 self
::$privateLinkDB->days()
252 * The URL corresponds to an existing entry in the DB
254 public function testGetKnownLinkFromURL()
256 $link = self
::$publicLinkDB->getLinkFromUrl('http://mediagoblin.org/');
258 $this->assertNotEquals(false, $link);
259 $this->assertContains(
260 'A free software media publishing platform',
266 * The URL is not in the DB
268 public function testGetUnknownLinkFromURL()
272 self
::$publicLinkDB->getLinkFromUrl('http://dev.null')
279 public function testAllTags()
293 // The DB contains a link with `sTuff` and another one with `stuff` tag.
294 // They need to be grouped with the first case found (`sTuff`).
298 self
::$publicLinkDB->allTags()
325 self
::$privateLinkDB->allTags()
330 * Test real_url without redirector.
332 public function testLinkRealUrlWithoutRedirector()
334 $db = new LinkDB(self
::$testDatastore, false, false);
335 foreach($db as $link) {
336 $this->assertEquals($link['url'], $link['real_url']);
341 * Test real_url with redirector.
343 public function testLinkRealUrlWithRedirector()
345 $redirector = 'http://redirector.to?';
346 $db = new LinkDB(self
::$testDatastore, false, false, $redirector);
347 foreach($db as $link) {
348 $this->assertStringStartsWith($redirector, $link['real_url']);
349 $this->assertNotFalse(strpos($link['real_url'], urlencode('://')));
352 $db = new LinkDB(self
::$testDatastore, false, false, $redirector, false);
353 foreach($db as $link) {
354 $this->assertStringStartsWith($redirector, $link['real_url']);
355 $this->assertFalse(strpos($link['real_url'], urlencode('://')));
360 * Test filter with string.
362 public function testFilterString()
364 $tags = 'dev cartoon';
365 $request = array('searchtags' => $tags);
368 count(self
::$privateLinkDB->filterSearch($request, true, false))
373 * Test filter with string.
375 public function testFilterArray()
377 $tags = array('dev', 'cartoon');
378 $request = array('searchtags' => $tags);
381 count(self
::$privateLinkDB->filterSearch($request, true, false))
386 * Test hidden tags feature:
387 * tags starting with a dot '.' are only visible when logged in.
389 public function testHiddenTags()
392 $request = array('searchtags' => $tags);
395 count(self
::$privateLinkDB->filterSearch($request, true, false))
400 count(self
::$publicLinkDB->filterSearch($request, true, false))
405 * Test filterHash() with a valid smallhash.
407 public function testFilterHashValid()
409 $request = smallHash('20150310_114651');
412 count(self
::$publicLinkDB->filterHash($request))
417 * Test filterHash() with an invalid smallhash.
419 * @expectedException LinkNotFoundException
421 public function testFilterHashInValid1()
424 self
::$publicLinkDB->filterHash($request);
428 * Test filterHash() with an empty smallhash.
430 * @expectedException LinkNotFoundException
432 public function testFilterHashInValid()
434 self
::$publicLinkDB->filterHash('');