6 require_once 'application/LinkDB.php';
7 require_once 'application/Utils.php';
8 require_once 'tests/utils/ReferenceLinkDB.php';
12 * Unitary tests for LinkDB
14 class LinkDBTest
extends PHPUnit_Framework_TestCase
16 // datastore to test write operations
17 protected static $testDatastore = 'tests/datastore.php';
18 protected static $refDB = null;
19 protected static $publicLinkDB = null;
20 protected static $privateLinkDB = null;
23 * Instantiates public and private LinkDBs with test data
25 * The reference datastore contains public and private links that
26 * will be used to test LinkDB's methods:
27 * - access filtering (public/private),
34 public static function setUpBeforeClass()
36 self
::$refDB = new ReferenceLinkDB();
37 self
::$refDB->write(self
::$testDatastore);
39 self
::$publicLinkDB = new LinkDB(self
::$testDatastore, false, false);
40 self
::$privateLinkDB = new LinkDB(self
::$testDatastore, true, false);
44 * Resets test data for each test
46 protected function setUp()
48 if (file_exists(self
::$testDatastore)) {
49 unlink(self
::$testDatastore);
54 * Allows to test LinkDB's private methods
57 * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html
58 * http://stackoverflow.com/a/2798203
60 protected static function getMethod($name)
62 $class = new ReflectionClass('LinkDB');
63 $method = $class->getMethod($name);
64 $method->setAccessible(true);
69 * Instantiate LinkDB objects - logged in user
71 public function testConstructLoggedIn()
73 new LinkDB(self
::$testDatastore, true, false);
74 $this->assertFileExists(self
::$testDatastore);
78 * Instantiate LinkDB objects - logged out or public instance
80 public function testConstructLoggedOut()
82 new LinkDB(self
::$testDatastore, false, false);
83 $this->assertFileExists(self
::$testDatastore);
87 * Attempt to instantiate a LinkDB whereas the datastore is not writable
89 * @expectedException PHPUnit_Framework_Error_Warning
90 * @expectedExceptionMessageRegExp /failed to open stream: No such file or directory/
92 public function testConstructDatastoreNotWriteable()
94 new LinkDB('null/store.db', false, false);
98 * The DB doesn't exist, ensure it is created with dummy content
100 public function testCheckDBNew()
102 $linkDB = new LinkDB(self
::$testDatastore, false, false);
103 unlink(self
::$testDatastore);
104 $this->assertFileNotExists(self
::$testDatastore);
106 $checkDB = self
::getMethod('_checkDB');
107 $checkDB->invokeArgs($linkDB, array());
108 $this->assertFileExists(self
::$testDatastore);
110 // ensure the correct data has been written
111 $this->assertGreaterThan(0, filesize(self
::$testDatastore));
115 * The DB exists, don't do anything
117 public function testCheckDBLoad()
119 $linkDB = new LinkDB(self
::$testDatastore, false, false);
120 $datastoreSize = filesize(self
::$testDatastore);
121 $this->assertGreaterThan(0, $datastoreSize);
123 $checkDB = self
::getMethod('_checkDB');
124 $checkDB->invokeArgs($linkDB, array());
126 // ensure the datastore is left unmodified
129 filesize(self
::$testDatastore)
136 public function testReadEmptyDB()
138 file_put_contents(self
::$testDatastore, '<?php /* S7QysKquBQA= */ ?>');
139 $emptyDB = new LinkDB(self
::$testDatastore, false, false);
140 $this->assertEquals(0, sizeof($emptyDB));
141 $this->assertEquals(0, count($emptyDB));
145 * Load public links from the DB
147 public function testReadPublicDB()
150 self
::$refDB->countPublicLinks(),
151 sizeof(self
::$publicLinkDB)
156 * Load public and private links from the DB
158 public function testReadPrivateDB()
161 self
::$refDB->countLinks(),
162 sizeof(self
::$privateLinkDB)
167 * Save the links to the DB
169 public function testSaveDB()
171 $testDB = new LinkDB(self
::$testDatastore, true, false);
172 $dbSize = sizeof($testDB);
175 'title'=>'an additional link',
176 'url'=>'http://dum.my',
177 'description'=>'One more',
179 'linkdate'=>'20150518_190000',
182 $testDB[$link['linkdate']] = $link;
184 // TODO: move PageCache to a proper class/file
185 function invalidateCaches() {}
189 $testDB = new LinkDB(self
::$testDatastore, true, false);
190 $this->assertEquals($dbSize +
1, sizeof($testDB));
194 * Count existing links
196 public function testCount()
199 self
::$refDB->countPublicLinks(),
200 self
::$publicLinkDB->count()
203 self
::$refDB->countLinks(),
204 self
::$privateLinkDB->count()
209 * Count existing links - public links hidden
211 public function testCountHiddenPublic()
213 $linkDB = new LinkDB(self
::$testDatastore, false, true);
226 * List the days for which links have been posted
228 public function testDays()
231 array('20121206', '20130614', '20150310'),
232 self
::$publicLinkDB->days()
236 array('20121206', '20130614', '20141125', '20150310'),
237 self
::$privateLinkDB->days()
242 * The URL corresponds to an existing entry in the DB
244 public function testGetKnownLinkFromURL()
246 $link = self
::$publicLinkDB->getLinkFromUrl('http://mediagoblin.org/');
248 $this->assertNotEquals(false, $link);
250 'A free software media publishing platform',
256 * The URL is not in the DB
258 public function testGetUnknownLinkFromURL()
262 self
::$publicLinkDB->getLinkFromUrl('http://dev.null')
269 public function testAllTags()
283 self
::$publicLinkDB->allTags()
302 self
::$privateLinkDB->allTags()
307 * Filter links using a tag
309 public function testFilterOneTag()
313 sizeof(self
::$publicLinkDB->filterTags('web', false))
318 sizeof(self
::$privateLinkDB->filterTags('web', false))
323 * Filter links using a tag - case-sensitive
325 public function testFilterCaseSensitiveTag()
329 sizeof(self
::$privateLinkDB->filterTags('mercurial', true))
334 sizeof(self
::$privateLinkDB->filterTags('Mercurial', true))
339 * Filter links using a tag combination
341 public function testFilterMultipleTags()
345 sizeof(self
::$publicLinkDB->filterTags('dev cartoon', false))
350 sizeof(self
::$privateLinkDB->filterTags('dev cartoon', false))
355 * Filter links using a non-existent tag
357 public function testFilterUnknownTag()
361 sizeof(self
::$publicLinkDB->filterTags('null', false))
366 * Return links for a given day
368 public function testFilterDay()
372 sizeof(self
::$publicLinkDB->filterDay('20121206'))
377 sizeof(self
::$privateLinkDB->filterDay('20121206'))
382 * 404 - day not found
384 public function testFilterUnknownDay()
388 sizeof(self
::$publicLinkDB->filterDay('19700101'))
393 sizeof(self
::$privateLinkDB->filterDay('19700101'))
398 * Use an invalid date format
399 * @expectedException Exception
400 * @expectedExceptionMessageRegExp /Invalid date format/
402 public function testFilterInvalidDayWithChars()
404 self
::$privateLinkDB->filterDay('Rainy day, dream away');
408 * Use an invalid date format
409 * @expectedException Exception
410 * @expectedExceptionMessageRegExp /Invalid date format/
412 public function testFilterInvalidDayDigits()
414 self
::$privateLinkDB->filterDay('20');
418 * Retrieve a link entry with its hash
420 public function testFilterSmallHash()
422 $links = self
::$privateLinkDB->filterSmallHash('IuWvgA');
431 $links['20130614_184135']['title']
437 * No link for this hash
439 public function testFilterUnknownSmallHash()
443 sizeof(self
::$privateLinkDB->filterSmallHash('Iblaah'))
448 * Full-text search - result from a link's URL
450 public function testFilterFullTextURL()
454 sizeof(self
::$publicLinkDB->filterFullText('ars.userfriendly.org'))
459 * Full-text search - result from a link's title only
461 public function testFilterFullTextTitle()
463 // use miscellaneous cases
466 sizeof(self
::$publicLinkDB->filterFullText('userfriendly -'))
470 sizeof(self
::$publicLinkDB->filterFullText('UserFriendly -'))
474 sizeof(self
::$publicLinkDB->filterFullText('uSeRFrIendlY -'))
477 // use miscellaneous case and offset
480 sizeof(self
::$publicLinkDB->filterFullText('RFrIendL'))
485 * Full-text search - result from the link's description only
487 public function testFilterFullTextDescription()
491 sizeof(self
::$publicLinkDB->filterFullText('media publishing'))
496 * Full-text search - result from the link's tags only
498 public function testFilterFullTextTags()
502 sizeof(self
::$publicLinkDB->filterFullText('gnu'))
507 * Full-text search - result set from mixed sources
509 public function testFilterFullTextMixed()
513 sizeof(self
::$publicLinkDB->filterFullText('free software'))