6 require_once 'application/LinkDB.php';
7 require_once 'application/Utils.php';
8 require_once 'tests/utils/ReferenceLinkDB.php';
10 define('PHPPREFIX', '<?php /* ');
11 define('PHPSUFFIX', ' */ ?>');
15 * Unitary tests for LinkDB
17 class LinkDBTest
extends PHPUnit_Framework_TestCase
19 // datastore to test write operations
20 protected static $testDatastore = 'tests/datastore.php';
21 protected static $dummyDatastoreSHA1 = 'e3edea8ea7bb50be4bcb404df53fbb4546a7156e';
22 protected static $refDB = null;
23 protected static $publicLinkDB = null;
24 protected static $privateLinkDB = null;
27 * Instantiates public and private LinkDBs with test data
29 * The reference datastore contains public and private links that
30 * will be used to test LinkDB's methods:
31 * - access filtering (public/private),
38 public static function setUpBeforeClass()
40 self
::$refDB = new ReferenceLinkDB();
41 self
::$refDB->write(self
::$testDatastore, PHPPREFIX
, PHPSUFFIX
);
43 $GLOBALS['config']['DATASTORE'] = self
::$testDatastore;
44 self
::$publicLinkDB = new LinkDB(false);
45 self
::$privateLinkDB = new LinkDB(true);
49 * Resets test data for each test
51 protected function setUp()
53 $GLOBALS['config']['DATASTORE'] = self
::$testDatastore;
54 if (file_exists(self
::$testDatastore)) {
55 unlink(self
::$testDatastore);
60 * Allows to test LinkDB's private methods
63 * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html
64 * http://stackoverflow.com/a/2798203
66 protected static function getMethod($name)
68 $class = new ReflectionClass('LinkDB');
69 $method = $class->getMethod($name);
70 $method->setAccessible(true);
75 * Instantiate LinkDB objects - logged in user
77 public function testConstructLoggedIn()
80 $this->assertFileExists(self
::$testDatastore);
84 * Instantiate LinkDB objects - logged out or public instance
86 public function testConstructLoggedOut()
89 $this->assertFileExists(self
::$testDatastore);
93 * Attempt to instantiate a LinkDB whereas the datastore is not writable
95 * @expectedException PHPUnit_Framework_Error_Warning
96 * @expectedExceptionMessageRegExp /failed to open stream: No such file or directory/
98 public function testConstructDatastoreNotWriteable()
100 $GLOBALS['config']['DATASTORE'] = 'null/store.db';
105 * The DB doesn't exist, ensure it is created with dummy content
107 public function testCheckDBNew()
109 $linkDB = new LinkDB(false);
110 unlink(self
::$testDatastore);
111 $this->assertFileNotExists(self
::$testDatastore);
113 $checkDB = self
::getMethod('checkDB');
114 $checkDB->invokeArgs($linkDB, array());
115 $this->assertFileExists(self
::$testDatastore);
117 // ensure the correct data has been written
119 self
::$dummyDatastoreSHA1,
120 sha1_file(self
::$testDatastore)
125 * The DB exists, don't do anything
127 public function testCheckDBLoad()
129 $linkDB = new LinkDB(false);
131 self
::$dummyDatastoreSHA1,
132 sha1_file(self
::$testDatastore)
135 $checkDB = self
::getMethod('checkDB');
136 $checkDB->invokeArgs($linkDB, array());
138 // ensure the datastore is left unmodified
140 self
::$dummyDatastoreSHA1,
141 sha1_file(self
::$testDatastore)
148 public function testReadEmptyDB()
150 file_put_contents(self
::$testDatastore, PHPPREFIX
.'S7QysKquBQA='.PHPSUFFIX
);
151 $emptyDB = new LinkDB(false);
152 $this->assertEquals(0, sizeof($emptyDB));
153 $this->assertEquals(0, count($emptyDB));
157 * Load public links from the DB
159 public function testReadPublicDB()
162 self
::$refDB->countPublicLinks(),
163 sizeof(self
::$publicLinkDB)
168 * Load public and private links from the DB
170 public function testReadPrivateDB()
173 self
::$refDB->countLinks(),
174 sizeof(self
::$privateLinkDB)
179 * Save the links to the DB
181 public function testSaveDB()
183 $testDB = new LinkDB(true);
184 $dbSize = sizeof($testDB);
187 'title'=>'an additional link',
188 'url'=>'http://dum.my',
189 'description'=>'One more',
191 'linkdate'=>'20150518_190000',
194 $testDB[$link['linkdate']] = $link;
196 // TODO: move PageCache to a proper class/file
197 function invalidateCaches() {}
201 $testDB = new LinkDB(true);
202 $this->assertEquals($dbSize +
1, sizeof($testDB));
206 * Count existing links
208 public function testCount()
211 self
::$refDB->countPublicLinks(),
212 self
::$publicLinkDB->count()
215 self
::$refDB->countLinks(),
216 self
::$privateLinkDB->count()
221 * List the days for which links have been posted
223 public function testDays()
226 ['20121206', '20130614', '20150310'],
227 self
::$publicLinkDB->days()
231 ['20121206', '20130614', '20141125', '20150310'],
232 self
::$privateLinkDB->days()
237 * The URL corresponds to an existing entry in the DB
239 public function testGetKnownLinkFromURL()
241 $link = self
::$publicLinkDB->getLinkFromUrl('http://mediagoblin.org/');
243 $this->assertNotEquals(false, $link);
245 'A free software media publishing platform',
251 * The URL is not in the DB
253 public function testGetUnknownLinkFromURL()
257 self
::$publicLinkDB->getLinkFromUrl('http://dev.null')
264 public function testAllTags()
278 self
::$publicLinkDB->allTags()
297 self
::$privateLinkDB->allTags()
302 * Filter links using a tag
304 public function testFilterOneTag()
308 sizeof(self
::$publicLinkDB->filterTags('web', false))
313 sizeof(self
::$privateLinkDB->filterTags('web', false))
318 * Filter links using a tag - case-sensitive
320 public function testFilterCaseSensitiveTag()
324 sizeof(self
::$privateLinkDB->filterTags('mercurial', true))
329 sizeof(self
::$privateLinkDB->filterTags('Mercurial', true))
334 * Filter links using a tag combination
336 public function testFilterMultipleTags()
340 sizeof(self
::$publicLinkDB->filterTags('dev cartoon', false))
345 sizeof(self
::$privateLinkDB->filterTags('dev cartoon', false))
350 * Filter links using a non-existent tag
352 public function testFilterUnknownTag()
356 sizeof(self
::$publicLinkDB->filterTags('null', false))
361 * Return links for a given day
363 public function testFilterDay()
367 sizeof(self
::$publicLinkDB->filterDay('20121206'))
372 sizeof(self
::$privateLinkDB->filterDay('20121206'))
377 * 404 - day not found
379 public function testFilterUnknownDay()
383 sizeof(self
::$publicLinkDB->filterDay('19700101'))
388 sizeof(self
::$privateLinkDB->filterDay('19700101'))
393 * Use an invalid date format
395 public function testFilterInvalidDay()
399 sizeof(self
::$privateLinkDB->filterDay('Rainy day, dream away'))
402 // TODO: check input format
405 sizeof(self
::$privateLinkDB->filterDay('20'))
410 * Retrieve a link entry with its hash
412 public function testFilterSmallHash()
414 $links = self
::$privateLinkDB->filterSmallHash('IuWvgA');
423 $links['20130614_184135']['title']
429 * No link for this hash
431 public function testFilterUnknownSmallHash()
435 sizeof(self
::$privateLinkDB->filterSmallHash('Iblaah'))
440 * Full-text search - result from a link's URL
442 public function testFilterFullTextURL()
446 sizeof(self
::$publicLinkDB->filterFullText('ars.userfriendly.org'))
451 * Full-text search - result from a link's title only
453 public function testFilterFullTextTitle()
455 // use miscellaneous cases
458 sizeof(self
::$publicLinkDB->filterFullText('userfriendly -'))
462 sizeof(self
::$publicLinkDB->filterFullText('UserFriendly -'))
466 sizeof(self
::$publicLinkDB->filterFullText('uSeRFrIendlY -'))
469 // use miscellaneous case and offset
472 sizeof(self
::$publicLinkDB->filterFullText('RFrIendL'))
477 * Full-text search - result from the link's description only
479 public function testFilterFullTextDescription()
483 sizeof(self
::$publicLinkDB->filterFullText('media publishing'))
488 * Full-text search - result from the link's tags only
490 public function testFilterFullTextTags()
494 sizeof(self
::$publicLinkDB->filterFullText('gnu'))
499 * Full-text search - result set from mixed sources
501 public function testFilterFullTextMixed()
505 sizeof(self
::$publicLinkDB->filterFullText('free software'))