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 * Test real_url without redirector.
307 public function testLinkRealUrlWithoutRedirector()
309 $db = new LinkDB(self
::$testDatastore, false, false);
310 foreach($db as $link) {
311 $this->assertEquals($link['url'], $link['real_url']);
316 * Test real_url with redirector.
318 public function testLinkRealUrlWithRedirector()
320 $redirector = 'http://redirector.to?';
321 $db = new LinkDB(self
::$testDatastore, false, false, $redirector);
322 foreach($db as $link) {
323 $this->assertStringStartsWith($redirector, $link['real_url']);
328 * Test filter with string.
330 public function testFilterString()
332 $tags = 'dev cartoon';
335 count(self
::$privateLinkDB->filter(LinkFilter
::$FILTER_TAG, $tags, true, false))
340 * Test filter with string.
342 public function testFilterArray()
344 $tags = array('dev', 'cartoon');
347 count(self
::$privateLinkDB->filter(LinkFilter
::$FILTER_TAG, $tags, true, false))