6 require_once 'application/Cache.php';
7 require_once 'application/LinkDB.php';
8 require_once 'application/Utils.php';
9 require_once 'tests/utils/ReferenceLinkDB.php';
13 * Unitary tests for LinkDB
15 class LinkDBTest
extends PHPUnit_Framework_TestCase
17 // datastore to test write operations
18 protected static $testDatastore = 'tests/datastore.php';
19 protected static $refDB = null;
20 protected static $publicLinkDB = null;
21 protected static $privateLinkDB = null;
24 * Instantiates public and private LinkDBs with test data
26 * The reference datastore contains public and private links that
27 * will be used to test LinkDB's methods:
28 * - access filtering (public/private),
35 public static function setUpBeforeClass()
37 self
::$refDB = new ReferenceLinkDB();
38 self
::$refDB->write(self
::$testDatastore);
40 self
::$publicLinkDB = new LinkDB(self
::$testDatastore, false, false);
41 self
::$privateLinkDB = new LinkDB(self
::$testDatastore, true, false);
45 * Resets test data for each test
47 protected function setUp()
49 if (file_exists(self
::$testDatastore)) {
50 unlink(self
::$testDatastore);
55 * Allows to test LinkDB's private methods
58 * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html
59 * http://stackoverflow.com/a/2798203
61 protected static function getMethod($name)
63 $class = new ReflectionClass('LinkDB');
64 $method = $class->getMethod($name);
65 $method->setAccessible(true);
70 * Instantiate LinkDB objects - logged in user
72 public function testConstructLoggedIn()
74 new LinkDB(self
::$testDatastore, true, false);
75 $this->assertFileExists(self
::$testDatastore);
79 * Instantiate LinkDB objects - logged out or public instance
81 public function testConstructLoggedOut()
83 new LinkDB(self
::$testDatastore, false, false);
84 $this->assertFileExists(self
::$testDatastore);
88 * Attempt to instantiate a LinkDB whereas the datastore is not writable
90 * @expectedException PHPUnit_Framework_Error_Warning
91 * @expectedExceptionMessageRegExp /failed to open stream: No such file or directory/
93 public function testConstructDatastoreNotWriteable()
95 new LinkDB('null/store.db', false, false);
99 * The DB doesn't exist, ensure it is created with dummy content
101 public function testCheckDBNew()
103 $linkDB = new LinkDB(self
::$testDatastore, false, false);
104 unlink(self
::$testDatastore);
105 $this->assertFileNotExists(self
::$testDatastore);
107 $checkDB = self
::getMethod('_checkDB');
108 $checkDB->invokeArgs($linkDB, array());
109 $this->assertFileExists(self
::$testDatastore);
111 // ensure the correct data has been written
112 $this->assertGreaterThan(0, filesize(self
::$testDatastore));
116 * The DB exists, don't do anything
118 public function testCheckDBLoad()
120 $linkDB = new LinkDB(self
::$testDatastore, false, false);
121 $datastoreSize = filesize(self
::$testDatastore);
122 $this->assertGreaterThan(0, $datastoreSize);
124 $checkDB = self
::getMethod('_checkDB');
125 $checkDB->invokeArgs($linkDB, array());
127 // ensure the datastore is left unmodified
130 filesize(self
::$testDatastore)
137 public function testReadEmptyDB()
139 file_put_contents(self
::$testDatastore, '<?php /* S7QysKquBQA= */ ?>');
140 $emptyDB = new LinkDB(self
::$testDatastore, false, false);
141 $this->assertEquals(0, sizeof($emptyDB));
142 $this->assertEquals(0, count($emptyDB));
146 * Load public links from the DB
148 public function testReadPublicDB()
151 self
::$refDB->countPublicLinks(),
152 sizeof(self
::$publicLinkDB)
157 * Load public and private links from the DB
159 public function testReadPrivateDB()
162 self
::$refDB->countLinks(),
163 sizeof(self
::$privateLinkDB)
168 * Save the links to the DB
170 public function testSaveDB()
172 $testDB = new LinkDB(self
::$testDatastore, true, false);
173 $dbSize = sizeof($testDB);
176 'title'=>'an additional link',
177 'url'=>'http://dum.my',
178 'description'=>'One more',
180 'linkdate'=>'20150518_190000',
183 $testDB[$link['linkdate']] = $link;
184 $testDB->savedb('tests');
186 $testDB = new LinkDB(self
::$testDatastore, true, false);
187 $this->assertEquals($dbSize +
1, sizeof($testDB));
191 * Count existing links
193 public function testCount()
196 self
::$refDB->countPublicLinks(),
197 self
::$publicLinkDB->count()
200 self
::$refDB->countLinks(),
201 self
::$privateLinkDB->count()
206 * Count existing links - public links hidden
208 public function testCountHiddenPublic()
210 $linkDB = new LinkDB(self
::$testDatastore, false, true);
223 * List the days for which links have been posted
225 public function testDays()
228 array('20121206', '20130614', '20150310'),
229 self
::$publicLinkDB->days()
233 array('20121206', '20130614', '20141125', '20150310'),
234 self
::$privateLinkDB->days()
239 * The URL corresponds to an existing entry in the DB
241 public function testGetKnownLinkFromURL()
243 $link = self
::$publicLinkDB->getLinkFromUrl('http://mediagoblin.org/');
245 $this->assertNotEquals(false, $link);
247 'A free software media publishing platform',
253 * The URL is not in the DB
255 public function testGetUnknownLinkFromURL()
259 self
::$publicLinkDB->getLinkFromUrl('http://dev.null')
266 public function testAllTags()
280 self
::$publicLinkDB->allTags()
299 self
::$privateLinkDB->allTags()
304 * Filter links using a tag
306 public function testFilterOneTag()
310 sizeof(self
::$publicLinkDB->filterTags('web', false))
315 sizeof(self
::$privateLinkDB->filterTags('web', false))
320 * Filter links using a tag - case-sensitive
322 public function testFilterCaseSensitiveTag()
326 sizeof(self
::$privateLinkDB->filterTags('mercurial', true))
331 sizeof(self
::$privateLinkDB->filterTags('Mercurial', true))
336 * Filter links using a tag combination
338 public function testFilterMultipleTags()
342 sizeof(self
::$publicLinkDB->filterTags('dev cartoon', false))
347 sizeof(self
::$privateLinkDB->filterTags('dev cartoon', false))
352 * Filter links using a non-existent tag
354 public function testFilterUnknownTag()
358 sizeof(self
::$publicLinkDB->filterTags('null', false))
363 * Return links for a given day
365 public function testFilterDay()
369 sizeof(self
::$publicLinkDB->filterDay('20121206'))
374 sizeof(self
::$privateLinkDB->filterDay('20121206'))
379 * 404 - day not found
381 public function testFilterUnknownDay()
385 sizeof(self
::$publicLinkDB->filterDay('19700101'))
390 sizeof(self
::$privateLinkDB->filterDay('19700101'))
395 * Use an invalid date format
396 * @expectedException Exception
397 * @expectedExceptionMessageRegExp /Invalid date format/
399 public function testFilterInvalidDayWithChars()
401 self
::$privateLinkDB->filterDay('Rainy day, dream away');
405 * Use an invalid date format
406 * @expectedException Exception
407 * @expectedExceptionMessageRegExp /Invalid date format/
409 public function testFilterInvalidDayDigits()
411 self
::$privateLinkDB->filterDay('20');
415 * Retrieve a link entry with its hash
417 public function testFilterSmallHash()
419 $links = self
::$privateLinkDB->filterSmallHash('IuWvgA');
428 $links['20130614_184135']['title']
434 * No link for this hash
436 public function testFilterUnknownSmallHash()
440 sizeof(self
::$privateLinkDB->filterSmallHash('Iblaah'))
445 * Full-text search - result from a link's URL
447 public function testFilterFullTextURL()
451 sizeof(self
::$publicLinkDB->filterFullText('ars.userfriendly.org'))
456 * Full-text search - result from a link's title only
458 public function testFilterFullTextTitle()
460 // use miscellaneous cases
463 sizeof(self
::$publicLinkDB->filterFullText('userfriendly -'))
467 sizeof(self
::$publicLinkDB->filterFullText('UserFriendly -'))
471 sizeof(self
::$publicLinkDB->filterFullText('uSeRFrIendlY -'))
474 // use miscellaneous case and offset
477 sizeof(self
::$publicLinkDB->filterFullText('RFrIendL'))
482 * Full-text search - result from the link's description only
484 public function testFilterFullTextDescription()
488 sizeof(self
::$publicLinkDB->filterFullText('media publishing'))
493 * Full-text search - result from the link's tags only
495 public function testFilterFullTextTags()
499 sizeof(self
::$publicLinkDB->filterFullText('gnu'))
504 * Full-text search - result set from mixed sources
506 public function testFilterFullTextMixed()
510 sizeof(self
::$publicLinkDB->filterFullText('free software'))