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, false);
45 self
::$privateLinkDB = new LinkDB(true, false);
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()
79 new LinkDB(true, false);
80 $this->assertFileExists(self
::$testDatastore);
84 * Instantiate LinkDB objects - logged out or public instance
86 public function testConstructLoggedOut()
88 new LinkDB(false, false);
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';
101 new LinkDB(false, false);
105 * The DB doesn't exist, ensure it is created with dummy content
107 public function testCheckDBNew()
109 $linkDB = new LinkDB(false, 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, 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, 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, false);
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, false);
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 * Count existing links - public links hidden
223 public function testCountHiddenPublic()
225 $linkDB = new LinkDB(false, true);
238 * List the days for which links have been posted
240 public function testDays()
243 ['20121206', '20130614', '20150310'],
244 self
::$publicLinkDB->days()
248 ['20121206', '20130614', '20141125', '20150310'],
249 self
::$privateLinkDB->days()
254 * The URL corresponds to an existing entry in the DB
256 public function testGetKnownLinkFromURL()
258 $link = self
::$publicLinkDB->getLinkFromUrl('http://mediagoblin.org/');
260 $this->assertNotEquals(false, $link);
262 'A free software media publishing platform',
268 * The URL is not in the DB
270 public function testGetUnknownLinkFromURL()
274 self
::$publicLinkDB->getLinkFromUrl('http://dev.null')
281 public function testAllTags()
295 self
::$publicLinkDB->allTags()
314 self
::$privateLinkDB->allTags()
319 * Filter links using a tag
321 public function testFilterOneTag()
325 sizeof(self
::$publicLinkDB->filterTags('web', false))
330 sizeof(self
::$privateLinkDB->filterTags('web', false))
335 * Filter links using a tag - case-sensitive
337 public function testFilterCaseSensitiveTag()
341 sizeof(self
::$privateLinkDB->filterTags('mercurial', true))
346 sizeof(self
::$privateLinkDB->filterTags('Mercurial', true))
351 * Filter links using a tag combination
353 public function testFilterMultipleTags()
357 sizeof(self
::$publicLinkDB->filterTags('dev cartoon', false))
362 sizeof(self
::$privateLinkDB->filterTags('dev cartoon', false))
367 * Filter links using a non-existent tag
369 public function testFilterUnknownTag()
373 sizeof(self
::$publicLinkDB->filterTags('null', false))
378 * Return links for a given day
380 public function testFilterDay()
384 sizeof(self
::$publicLinkDB->filterDay('20121206'))
389 sizeof(self
::$privateLinkDB->filterDay('20121206'))
394 * 404 - day not found
396 public function testFilterUnknownDay()
400 sizeof(self
::$publicLinkDB->filterDay('19700101'))
405 sizeof(self
::$privateLinkDB->filterDay('19700101'))
410 * Use an invalid date format
412 public function testFilterInvalidDay()
416 sizeof(self
::$privateLinkDB->filterDay('Rainy day, dream away'))
419 // TODO: check input format
422 sizeof(self
::$privateLinkDB->filterDay('20'))
427 * Retrieve a link entry with its hash
429 public function testFilterSmallHash()
431 $links = self
::$privateLinkDB->filterSmallHash('IuWvgA');
440 $links['20130614_184135']['title']
446 * No link for this hash
448 public function testFilterUnknownSmallHash()
452 sizeof(self
::$privateLinkDB->filterSmallHash('Iblaah'))
457 * Full-text search - result from a link's URL
459 public function testFilterFullTextURL()
463 sizeof(self
::$publicLinkDB->filterFullText('ars.userfriendly.org'))
468 * Full-text search - result from a link's title only
470 public function testFilterFullTextTitle()
472 // use miscellaneous cases
475 sizeof(self
::$publicLinkDB->filterFullText('userfriendly -'))
479 sizeof(self
::$publicLinkDB->filterFullText('UserFriendly -'))
483 sizeof(self
::$publicLinkDB->filterFullText('uSeRFrIendlY -'))
486 // use miscellaneous case and offset
489 sizeof(self
::$publicLinkDB->filterFullText('RFrIendL'))
494 * Full-text search - result from the link's description only
496 public function testFilterFullTextDescription()
500 sizeof(self
::$publicLinkDB->filterFullText('media publishing'))
505 * Full-text search - result from the link's tags only
507 public function testFilterFullTextTags()
511 sizeof(self
::$publicLinkDB->filterFullText('gnu'))
516 * Full-text search - result set from mixed sources
518 public function testFilterFullTextMixed()
522 sizeof(self
::$publicLinkDB->filterFullText('free software'))