6 require_once 'application/Cache.php';
7 require_once 'application/FileUtils.php';
8 require_once 'application/LinkDB.php';
9 require_once 'application/Utils.php';
10 require_once 'tests/utils/ReferenceLinkDB.php';
14 * Unitary tests for LinkDB
16 class LinkDBTest
extends PHPUnit_Framework_TestCase
18 // datastore to test write operations
19 protected static $testDatastore = 'sandbox/datastore.php';
20 protected static $refDB = null;
21 protected static $publicLinkDB = null;
22 protected static $privateLinkDB = null;
25 * Instantiates public and private LinkDBs with test data
27 * The reference datastore contains public and private links that
28 * will be used to test LinkDB's methods:
29 * - access filtering (public/private),
36 public static function setUpBeforeClass()
38 self
::$refDB = new ReferenceLinkDB();
39 self
::$refDB->write(self
::$testDatastore);
41 self
::$publicLinkDB = new LinkDB(self
::$testDatastore, false, false);
42 self
::$privateLinkDB = new LinkDB(self
::$testDatastore, true, false);
46 * Resets test data for each test
48 protected function setUp()
50 if (file_exists(self
::$testDatastore)) {
51 unlink(self
::$testDatastore);
56 * Allows to test LinkDB's private methods
59 * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html
60 * http://stackoverflow.com/a/2798203
62 protected static function getMethod($name)
64 $class = new ReflectionClass('LinkDB');
65 $method = $class->getMethod($name);
66 $method->setAccessible(true);
71 * Instantiate LinkDB objects - logged in user
73 public function testConstructLoggedIn()
75 new LinkDB(self
::$testDatastore, true, false);
76 $this->assertFileExists(self
::$testDatastore);
80 * Instantiate LinkDB objects - logged out or public instance
82 public function testConstructLoggedOut()
84 new LinkDB(self
::$testDatastore, false, false);
85 $this->assertFileExists(self
::$testDatastore);
89 * Attempt to instantiate a LinkDB whereas the datastore is not writable
91 * @expectedException IOException
92 * @expectedExceptionMessageRegExp /Error accessing null/
94 public function testConstructDatastoreNotWriteable()
96 new LinkDB('null/store.db', false, false);
100 * The DB doesn't exist, ensure it is created with dummy content
102 public function testCheckDBNew()
104 $linkDB = new LinkDB(self
::$testDatastore, false, false);
105 unlink(self
::$testDatastore);
106 $this->assertFileNotExists(self
::$testDatastore);
108 $checkDB = self
::getMethod('_checkDB');
109 $checkDB->invokeArgs($linkDB, array());
110 $this->assertFileExists(self
::$testDatastore);
112 // ensure the correct data has been written
113 $this->assertGreaterThan(0, filesize(self
::$testDatastore));
117 * The DB exists, don't do anything
119 public function testCheckDBLoad()
121 $linkDB = new LinkDB(self
::$testDatastore, false, false);
122 $datastoreSize = filesize(self
::$testDatastore);
123 $this->assertGreaterThan(0, $datastoreSize);
125 $checkDB = self
::getMethod('_checkDB');
126 $checkDB->invokeArgs($linkDB, array());
128 // ensure the datastore is left unmodified
131 filesize(self
::$testDatastore)
138 public function testReadEmptyDB()
140 file_put_contents(self
::$testDatastore, '<?php /* S7QysKquBQA= */ ?>');
141 $emptyDB = new LinkDB(self
::$testDatastore, false, false);
142 $this->assertEquals(0, sizeof($emptyDB));
143 $this->assertEquals(0, count($emptyDB));
147 * Load public links from the DB
149 public function testReadPublicDB()
152 self
::$refDB->countPublicLinks(),
153 sizeof(self
::$publicLinkDB)
158 * Load public and private links from the DB
160 public function testReadPrivateDB()
163 self
::$refDB->countLinks(),
164 sizeof(self
::$privateLinkDB)
169 * Save the links to the DB
171 public function testSaveDB()
173 $testDB = new LinkDB(self
::$testDatastore, true, false);
174 $dbSize = sizeof($testDB);
177 'title'=>'an additional link',
178 'url'=>'http://dum.my',
179 'description'=>'One more',
181 'linkdate'=>'20150518_190000',
184 $testDB[$link['linkdate']] = $link;
185 $testDB->savedb('tests');
187 $testDB = new LinkDB(self
::$testDatastore, true, false);
188 $this->assertEquals($dbSize +
1, sizeof($testDB));
192 * Count existing links
194 public function testCount()
197 self
::$refDB->countPublicLinks(),
198 self
::$publicLinkDB->count()
201 self
::$refDB->countLinks(),
202 self
::$privateLinkDB->count()
207 * Count existing links - public links hidden
209 public function testCountHiddenPublic()
211 $linkDB = new LinkDB(self
::$testDatastore, false, true);
224 * List the days for which links have been posted
226 public function testDays()
229 array('20121206', '20130614', '20150310'),
230 self
::$publicLinkDB->days()
234 array('20121206', '20130614', '20141125', '20150310'),
235 self
::$privateLinkDB->days()
240 * The URL corresponds to an existing entry in the DB
242 public function testGetKnownLinkFromURL()
244 $link = self
::$publicLinkDB->getLinkFromUrl('http://mediagoblin.org/');
246 $this->assertNotEquals(false, $link);
248 'A free software media publishing platform',
254 * The URL is not in the DB
256 public function testGetUnknownLinkFromURL()
260 self
::$publicLinkDB->getLinkFromUrl('http://dev.null')
267 public function testAllTags()
281 self
::$publicLinkDB->allTags()
300 self
::$privateLinkDB->allTags()
305 * Filter links using a tag
307 public function testFilterOneTag()
311 sizeof(self
::$publicLinkDB->filterTags('web', false))
316 sizeof(self
::$privateLinkDB->filterTags('web', false))
321 * Filter links using a tag - case-sensitive
323 public function testFilterCaseSensitiveTag()
327 sizeof(self
::$privateLinkDB->filterTags('mercurial', true))
332 sizeof(self
::$privateLinkDB->filterTags('Mercurial', true))
337 * Filter links using a tag combination
339 public function testFilterMultipleTags()
343 sizeof(self
::$publicLinkDB->filterTags('dev cartoon', false))
348 sizeof(self
::$privateLinkDB->filterTags('dev cartoon', false))
353 * Filter links using a non-existent tag
355 public function testFilterUnknownTag()
359 sizeof(self
::$publicLinkDB->filterTags('null', false))
364 * Return links for a given day
366 public function testFilterDay()
370 sizeof(self
::$publicLinkDB->filterDay('20121206'))
375 sizeof(self
::$privateLinkDB->filterDay('20121206'))
380 * 404 - day not found
382 public function testFilterUnknownDay()
386 sizeof(self
::$publicLinkDB->filterDay('19700101'))
391 sizeof(self
::$privateLinkDB->filterDay('19700101'))
396 * Use an invalid date format
397 * @expectedException Exception
398 * @expectedExceptionMessageRegExp /Invalid date format/
400 public function testFilterInvalidDayWithChars()
402 self
::$privateLinkDB->filterDay('Rainy day, dream away');
406 * Use an invalid date format
407 * @expectedException Exception
408 * @expectedExceptionMessageRegExp /Invalid date format/
410 public function testFilterInvalidDayDigits()
412 self
::$privateLinkDB->filterDay('20');
416 * Retrieve a link entry with its hash
418 public function testFilterSmallHash()
420 $links = self
::$privateLinkDB->filterSmallHash('IuWvgA');
429 $links['20130614_184135']['title']
435 * No link for this hash
437 public function testFilterUnknownSmallHash()
441 sizeof(self
::$privateLinkDB->filterSmallHash('Iblaah'))
446 * Full-text search - result from a link's URL
448 public function testFilterFullTextURL()
452 sizeof(self
::$publicLinkDB->filterFullText('ars.userfriendly.org'))
457 * Full-text search - result from a link's title only
459 public function testFilterFullTextTitle()
461 // use miscellaneous cases
464 sizeof(self
::$publicLinkDB->filterFullText('userfriendly -'))
468 sizeof(self
::$publicLinkDB->filterFullText('UserFriendly -'))
472 sizeof(self
::$publicLinkDB->filterFullText('uSeRFrIendlY -'))
475 // use miscellaneous case and offset
478 sizeof(self
::$publicLinkDB->filterFullText('RFrIendL'))
483 * Full-text search - result from the link's description only
485 public function testFilterFullTextDescription()
489 sizeof(self
::$publicLinkDB->filterFullText('media publishing'))
494 * Full-text search - result from the link's tags only
496 public function testFilterFullTextTags()
500 sizeof(self
::$publicLinkDB->filterFullText('gnu'))
505 * Full-text search - result set from mixed sources
507 public function testFilterFullTextMixed()
511 sizeof(self
::$publicLinkDB->filterFullText('free software'))
516 * Test real_url without redirector.
518 public function testLinkRealUrlWithoutRedirector()
520 $db = new LinkDB(self
::$testDatastore, false, false);
521 foreach($db as $link) {
522 $this->assertEquals($link['url'], $link['real_url']);
527 * Test real_url with redirector.
529 public function testLinkRealUrlWithRedirector()
531 $redirector = 'http://redirector.to?';
532 $db = new LinkDB(self
::$testDatastore, false, false, $redirector);
533 foreach($db as $link) {
534 $this->assertStringStartsWith($redirector, $link['real_url']);