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 $dummyDatastoreFilesize = 759;
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
113 self
::$dummyDatastoreFilesize,
114 filesize(self
::$testDatastore)
119 * The DB exists, don't do anything
121 public function testCheckDBLoad()
123 $linkDB = new LinkDB(self
::$testDatastore, false, false);
125 self
::$dummyDatastoreFilesize,
126 filesize(self
::$testDatastore)
129 $checkDB = self
::getMethod('checkDB');
130 $checkDB->invokeArgs($linkDB, array());
132 // ensure the datastore is left unmodified
134 self
::$dummyDatastoreFilesize,
135 filesize(self
::$testDatastore)
142 public function testReadEmptyDB()
144 file_put_contents(self
::$testDatastore, '<?php /* S7QysKquBQA= */ ?>');
145 $emptyDB = new LinkDB(self
::$testDatastore, false, false);
146 $this->assertEquals(0, sizeof($emptyDB));
147 $this->assertEquals(0, count($emptyDB));
151 * Load public links from the DB
153 public function testReadPublicDB()
156 self
::$refDB->countPublicLinks(),
157 sizeof(self
::$publicLinkDB)
162 * Load public and private links from the DB
164 public function testReadPrivateDB()
167 self
::$refDB->countLinks(),
168 sizeof(self
::$privateLinkDB)
173 * Save the links to the DB
175 public function testSaveDB()
177 $testDB = new LinkDB(self
::$testDatastore, true, false);
178 $dbSize = sizeof($testDB);
181 'title'=>'an additional link',
182 'url'=>'http://dum.my',
183 'description'=>'One more',
185 'linkdate'=>'20150518_190000',
188 $testDB[$link['linkdate']] = $link;
190 // TODO: move PageCache to a proper class/file
191 function invalidateCaches() {}
195 $testDB = new LinkDB(self
::$testDatastore, true, false);
196 $this->assertEquals($dbSize +
1, sizeof($testDB));
200 * Count existing links
202 public function testCount()
205 self
::$refDB->countPublicLinks(),
206 self
::$publicLinkDB->count()
209 self
::$refDB->countLinks(),
210 self
::$privateLinkDB->count()
215 * Count existing links - public links hidden
217 public function testCountHiddenPublic()
219 $linkDB = new LinkDB(self
::$testDatastore, false, true);
232 * List the days for which links have been posted
234 public function testDays()
237 ['20121206', '20130614', '20150310'],
238 self
::$publicLinkDB->days()
242 ['20121206', '20130614', '20141125', '20150310'],
243 self
::$privateLinkDB->days()
248 * The URL corresponds to an existing entry in the DB
250 public function testGetKnownLinkFromURL()
252 $link = self
::$publicLinkDB->getLinkFromUrl('http://mediagoblin.org/');
254 $this->assertNotEquals(false, $link);
256 'A free software media publishing platform',
262 * The URL is not in the DB
264 public function testGetUnknownLinkFromURL()
268 self
::$publicLinkDB->getLinkFromUrl('http://dev.null')
275 public function testAllTags()
289 self
::$publicLinkDB->allTags()
308 self
::$privateLinkDB->allTags()
313 * Filter links using a tag
315 public function testFilterOneTag()
319 sizeof(self
::$publicLinkDB->filterTags('web', false))
324 sizeof(self
::$privateLinkDB->filterTags('web', false))
329 * Filter links using a tag - case-sensitive
331 public function testFilterCaseSensitiveTag()
335 sizeof(self
::$privateLinkDB->filterTags('mercurial', true))
340 sizeof(self
::$privateLinkDB->filterTags('Mercurial', true))
345 * Filter links using a tag combination
347 public function testFilterMultipleTags()
351 sizeof(self
::$publicLinkDB->filterTags('dev cartoon', false))
356 sizeof(self
::$privateLinkDB->filterTags('dev cartoon', false))
361 * Filter links using a non-existent tag
363 public function testFilterUnknownTag()
367 sizeof(self
::$publicLinkDB->filterTags('null', false))
372 * Return links for a given day
374 public function testFilterDay()
378 sizeof(self
::$publicLinkDB->filterDay('20121206'))
383 sizeof(self
::$privateLinkDB->filterDay('20121206'))
388 * 404 - day not found
390 public function testFilterUnknownDay()
394 sizeof(self
::$publicLinkDB->filterDay('19700101'))
399 sizeof(self
::$privateLinkDB->filterDay('19700101'))
404 * Use an invalid date format
406 public function testFilterInvalidDay()
410 sizeof(self
::$privateLinkDB->filterDay('Rainy day, dream away'))
413 // TODO: check input format
416 sizeof(self
::$privateLinkDB->filterDay('20'))
421 * Retrieve a link entry with its hash
423 public function testFilterSmallHash()
425 $links = self
::$privateLinkDB->filterSmallHash('IuWvgA');
434 $links['20130614_184135']['title']
440 * No link for this hash
442 public function testFilterUnknownSmallHash()
446 sizeof(self
::$privateLinkDB->filterSmallHash('Iblaah'))
451 * Full-text search - result from a link's URL
453 public function testFilterFullTextURL()
457 sizeof(self
::$publicLinkDB->filterFullText('ars.userfriendly.org'))
462 * Full-text search - result from a link's title only
464 public function testFilterFullTextTitle()
466 // use miscellaneous cases
469 sizeof(self
::$publicLinkDB->filterFullText('userfriendly -'))
473 sizeof(self
::$publicLinkDB->filterFullText('UserFriendly -'))
477 sizeof(self
::$publicLinkDB->filterFullText('uSeRFrIendlY -'))
480 // use miscellaneous case and offset
483 sizeof(self
::$publicLinkDB->filterFullText('RFrIendL'))
488 * Full-text search - result from the link's description only
490 public function testFilterFullTextDescription()
494 sizeof(self
::$publicLinkDB->filterFullText('media publishing'))
499 * Full-text search - result from the link's tags only
501 public function testFilterFullTextTags()
505 sizeof(self
::$publicLinkDB->filterFullText('gnu'))
510 * Full-text search - result set from mixed sources
512 public function testFilterFullTextMixed()
516 sizeof(self
::$publicLinkDB->filterFullText('free software'))