]>
Commit | Line | Data |
---|---|---|
ca74886f V |
1 | <?php |
2 | /** | |
3 | * Link datastore tests | |
4 | */ | |
5 | ||
01e48f26 | 6 | require_once 'application/Cache.php'; |
2e28269b | 7 | require_once 'application/FileUtils.php'; |
ca74886f V |
8 | require_once 'application/LinkDB.php'; |
9 | require_once 'application/Utils.php'; | |
10 | require_once 'tests/utils/ReferenceLinkDB.php'; | |
11 | ||
ca74886f V |
12 | |
13 | /** | |
14 | * Unitary tests for LinkDB | |
15 | */ | |
16 | class LinkDBTest extends PHPUnit_Framework_TestCase | |
17 | { | |
18 | // datastore to test write operations | |
4bf35ba5 | 19 | protected static $testDatastore = 'sandbox/datastore.php'; |
528a6f8a A |
20 | |
21 | /** | |
22 | * @var ReferenceLinkDB instance. | |
23 | */ | |
ca74886f | 24 | protected static $refDB = null; |
528a6f8a A |
25 | |
26 | /** | |
27 | * @var LinkDB public LinkDB instance. | |
28 | */ | |
ca74886f | 29 | protected static $publicLinkDB = null; |
528a6f8a A |
30 | |
31 | /** | |
32 | * @var LinkDB private LinkDB instance. | |
33 | */ | |
ca74886f V |
34 | protected static $privateLinkDB = null; |
35 | ||
36 | /** | |
37 | * Instantiates public and private LinkDBs with test data | |
38 | * | |
39 | * The reference datastore contains public and private links that | |
40 | * will be used to test LinkDB's methods: | |
41 | * - access filtering (public/private), | |
42 | * - link searches: | |
43 | * - by day, | |
44 | * - by tag, | |
45 | * - by text, | |
46 | * - etc. | |
47 | */ | |
48 | public static function setUpBeforeClass() | |
49 | { | |
50 | self::$refDB = new ReferenceLinkDB(); | |
9c8752a2 | 51 | self::$refDB->write(self::$testDatastore); |
ca74886f | 52 | |
9c8752a2 V |
53 | self::$publicLinkDB = new LinkDB(self::$testDatastore, false, false); |
54 | self::$privateLinkDB = new LinkDB(self::$testDatastore, true, false); | |
ca74886f V |
55 | } |
56 | ||
57 | /** | |
58 | * Resets test data for each test | |
59 | */ | |
60 | protected function setUp() | |
61 | { | |
ca74886f V |
62 | if (file_exists(self::$testDatastore)) { |
63 | unlink(self::$testDatastore); | |
64 | } | |
65 | } | |
66 | ||
67 | /** | |
68 | * Allows to test LinkDB's private methods | |
69 | * | |
70 | * @see | |
71 | * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html | |
72 | * http://stackoverflow.com/a/2798203 | |
73 | */ | |
74 | protected static function getMethod($name) | |
75 | { | |
76 | $class = new ReflectionClass('LinkDB'); | |
77 | $method = $class->getMethod($name); | |
78 | $method->setAccessible(true); | |
79 | return $method; | |
80 | } | |
81 | ||
82 | /** | |
83 | * Instantiate LinkDB objects - logged in user | |
84 | */ | |
85 | public function testConstructLoggedIn() | |
86 | { | |
9c8752a2 | 87 | new LinkDB(self::$testDatastore, true, false); |
ca74886f V |
88 | $this->assertFileExists(self::$testDatastore); |
89 | } | |
90 | ||
91 | /** | |
92 | * Instantiate LinkDB objects - logged out or public instance | |
93 | */ | |
94 | public function testConstructLoggedOut() | |
95 | { | |
9c8752a2 | 96 | new LinkDB(self::$testDatastore, false, false); |
ca74886f V |
97 | $this->assertFileExists(self::$testDatastore); |
98 | } | |
99 | ||
100 | /** | |
101 | * Attempt to instantiate a LinkDB whereas the datastore is not writable | |
102 | * | |
2e28269b | 103 | * @expectedException IOException |
684e662a | 104 | * @expectedExceptionMessageRegExp /Error accessing\nnull/ |
ca74886f V |
105 | */ |
106 | public function testConstructDatastoreNotWriteable() | |
107 | { | |
9c8752a2 | 108 | new LinkDB('null/store.db', false, false); |
ca74886f V |
109 | } |
110 | ||
111 | /** | |
112 | * The DB doesn't exist, ensure it is created with dummy content | |
113 | */ | |
114 | public function testCheckDBNew() | |
115 | { | |
9c8752a2 | 116 | $linkDB = new LinkDB(self::$testDatastore, false, false); |
ca74886f V |
117 | unlink(self::$testDatastore); |
118 | $this->assertFileNotExists(self::$testDatastore); | |
119 | ||
f21abf32 | 120 | $checkDB = self::getMethod('check'); |
ca74886f V |
121 | $checkDB->invokeArgs($linkDB, array()); |
122 | $this->assertFileExists(self::$testDatastore); | |
123 | ||
124 | // ensure the correct data has been written | |
0037fbe1 | 125 | $this->assertGreaterThan(0, filesize(self::$testDatastore)); |
ca74886f V |
126 | } |
127 | ||
128 | /** | |
129 | * The DB exists, don't do anything | |
130 | */ | |
131 | public function testCheckDBLoad() | |
132 | { | |
9c8752a2 | 133 | $linkDB = new LinkDB(self::$testDatastore, false, false); |
0037fbe1 V |
134 | $datastoreSize = filesize(self::$testDatastore); |
135 | $this->assertGreaterThan(0, $datastoreSize); | |
ca74886f | 136 | |
f21abf32 | 137 | $checkDB = self::getMethod('check'); |
ca74886f V |
138 | $checkDB->invokeArgs($linkDB, array()); |
139 | ||
140 | // ensure the datastore is left unmodified | |
141 | $this->assertEquals( | |
0037fbe1 | 142 | $datastoreSize, |
30e6f1ca | 143 | filesize(self::$testDatastore) |
ca74886f V |
144 | ); |
145 | } | |
146 | ||
147 | /** | |
148 | * Load an empty DB | |
149 | */ | |
150 | public function testReadEmptyDB() | |
151 | { | |
9c8752a2 V |
152 | file_put_contents(self::$testDatastore, '<?php /* S7QysKquBQA= */ ?>'); |
153 | $emptyDB = new LinkDB(self::$testDatastore, false, false); | |
ca74886f V |
154 | $this->assertEquals(0, sizeof($emptyDB)); |
155 | $this->assertEquals(0, count($emptyDB)); | |
156 | } | |
157 | ||
158 | /** | |
159 | * Load public links from the DB | |
160 | */ | |
161 | public function testReadPublicDB() | |
162 | { | |
163 | $this->assertEquals( | |
164 | self::$refDB->countPublicLinks(), | |
165 | sizeof(self::$publicLinkDB) | |
166 | ); | |
167 | } | |
168 | ||
169 | /** | |
170 | * Load public and private links from the DB | |
171 | */ | |
172 | public function testReadPrivateDB() | |
173 | { | |
174 | $this->assertEquals( | |
175 | self::$refDB->countLinks(), | |
176 | sizeof(self::$privateLinkDB) | |
177 | ); | |
178 | } | |
179 | ||
180 | /** | |
181 | * Save the links to the DB | |
182 | */ | |
f21abf32 | 183 | public function testSave() |
ca74886f | 184 | { |
9c8752a2 | 185 | $testDB = new LinkDB(self::$testDatastore, true, false); |
ca74886f V |
186 | $dbSize = sizeof($testDB); |
187 | ||
188 | $link = array( | |
189 | 'title'=>'an additional link', | |
190 | 'url'=>'http://dum.my', | |
191 | 'description'=>'One more', | |
192 | 'private'=>0, | |
193 | 'linkdate'=>'20150518_190000', | |
194 | 'tags'=>'unit test' | |
195 | ); | |
196 | $testDB[$link['linkdate']] = $link; | |
f21abf32 | 197 | $testDB->save('tests'); |
ca74886f | 198 | |
9c8752a2 | 199 | $testDB = new LinkDB(self::$testDatastore, true, false); |
ca74886f V |
200 | $this->assertEquals($dbSize + 1, sizeof($testDB)); |
201 | } | |
202 | ||
203 | /** | |
204 | * Count existing links | |
205 | */ | |
206 | public function testCount() | |
207 | { | |
208 | $this->assertEquals( | |
209 | self::$refDB->countPublicLinks(), | |
210 | self::$publicLinkDB->count() | |
211 | ); | |
212 | $this->assertEquals( | |
213 | self::$refDB->countLinks(), | |
214 | self::$privateLinkDB->count() | |
215 | ); | |
216 | } | |
217 | ||
9f15ca9e V |
218 | /** |
219 | * Count existing links - public links hidden | |
220 | */ | |
221 | public function testCountHiddenPublic() | |
222 | { | |
9c8752a2 | 223 | $linkDB = new LinkDB(self::$testDatastore, false, true); |
9f15ca9e V |
224 | |
225 | $this->assertEquals( | |
226 | 0, | |
227 | $linkDB->count() | |
228 | ); | |
229 | $this->assertEquals( | |
230 | 0, | |
231 | $linkDB->count() | |
232 | ); | |
233 | } | |
234 | ||
ca74886f V |
235 | /** |
236 | * List the days for which links have been posted | |
237 | */ | |
238 | public function testDays() | |
239 | { | |
240 | $this->assertEquals( | |
d1e2f8e5 | 241 | array('20121206', '20130614', '20150310'), |
ca74886f V |
242 | self::$publicLinkDB->days() |
243 | ); | |
244 | ||
245 | $this->assertEquals( | |
d1e2f8e5 | 246 | array('20121206', '20130614', '20141125', '20150310'), |
ca74886f V |
247 | self::$privateLinkDB->days() |
248 | ); | |
249 | } | |
250 | ||
251 | /** | |
252 | * The URL corresponds to an existing entry in the DB | |
253 | */ | |
254 | public function testGetKnownLinkFromURL() | |
255 | { | |
256 | $link = self::$publicLinkDB->getLinkFromUrl('http://mediagoblin.org/'); | |
257 | ||
258 | $this->assertNotEquals(false, $link); | |
9ccca401 | 259 | $this->assertContains( |
ca74886f V |
260 | 'A free software media publishing platform', |
261 | $link['description'] | |
262 | ); | |
263 | } | |
264 | ||
265 | /** | |
266 | * The URL is not in the DB | |
267 | */ | |
268 | public function testGetUnknownLinkFromURL() | |
269 | { | |
270 | $this->assertEquals( | |
271 | false, | |
272 | self::$publicLinkDB->getLinkFromUrl('http://dev.null') | |
273 | ); | |
274 | } | |
275 | ||
276 | /** | |
277 | * Lists all tags | |
278 | */ | |
279 | public function testAllTags() | |
280 | { | |
281 | $this->assertEquals( | |
d1e2f8e5 | 282 | array( |
ca74886f V |
283 | 'web' => 3, |
284 | 'cartoon' => 2, | |
285 | 'gnu' => 2, | |
286 | 'dev' => 1, | |
287 | 'samba' => 1, | |
288 | 'media' => 1, | |
289 | 'software' => 1, | |
290 | 'stallman' => 1, | |
21979ff1 A |
291 | 'free' => 1, |
292 | '-exclude' => 1, | |
b1eb5d1d A |
293 | // The DB contains a link with `sTuff` and another one with `stuff` tag. |
294 | // They need to be grouped with the first case found (`sTuff`). | |
295 | 'sTuff' => 2, | |
9ccca401 | 296 | 'hashtag' => 2, |
d1e2f8e5 | 297 | ), |
ca74886f V |
298 | self::$publicLinkDB->allTags() |
299 | ); | |
300 | ||
301 | $this->assertEquals( | |
d1e2f8e5 | 302 | array( |
ca74886f V |
303 | 'web' => 4, |
304 | 'cartoon' => 3, | |
305 | 'gnu' => 2, | |
306 | 'dev' => 2, | |
307 | 'samba' => 1, | |
308 | 'media' => 1, | |
309 | 'software' => 1, | |
310 | 'stallman' => 1, | |
311 | 'free' => 1, | |
312 | 'html' => 1, | |
313 | 'w3c' => 1, | |
314 | 'css' => 1, | |
21979ff1 | 315 | 'Mercurial' => 1, |
b1eb5d1d | 316 | 'sTuff' => 2, |
21979ff1 | 317 | '-exclude' => 1, |
195acf9f | 318 | '.hidden' => 1, |
9ccca401 | 319 | 'hashtag' => 2, |
9866b408 A |
320 | 'tag1' => 1, |
321 | 'tag2' => 1, | |
322 | 'tag3' => 1, | |
323 | 'tag4' => 1, | |
d1e2f8e5 | 324 | ), |
ca74886f V |
325 | self::$privateLinkDB->allTags() |
326 | ); | |
327 | } | |
328 | ||
329 | /** | |
822bffce | 330 | * Test real_url without redirector. |
ca74886f | 331 | */ |
822bffce | 332 | public function testLinkRealUrlWithoutRedirector() |
ca74886f | 333 | { |
822bffce A |
334 | $db = new LinkDB(self::$testDatastore, false, false); |
335 | foreach($db as $link) { | |
336 | $this->assertEquals($link['url'], $link['real_url']); | |
337 | } | |
ca74886f V |
338 | } |
339 | ||
340 | /** | |
822bffce | 341 | * Test real_url with redirector. |
ca74886f | 342 | */ |
822bffce | 343 | public function testLinkRealUrlWithRedirector() |
ca74886f | 344 | { |
822bffce A |
345 | $redirector = 'http://redirector.to?'; |
346 | $db = new LinkDB(self::$testDatastore, false, false, $redirector); | |
347 | foreach($db as $link) { | |
348 | $this->assertStringStartsWith($redirector, $link['real_url']); | |
043eae70 A |
349 | $this->assertNotFalse(strpos($link['real_url'], urlencode('://'))); |
350 | } | |
351 | ||
352 | $db = new LinkDB(self::$testDatastore, false, false, $redirector, false); | |
353 | foreach($db as $link) { | |
354 | $this->assertStringStartsWith($redirector, $link['real_url']); | |
355 | $this->assertFalse(strpos($link['real_url'], urlencode('://'))); | |
822bffce | 356 | } |
ca74886f V |
357 | } |
358 | ||
359 | /** | |
822bffce | 360 | * Test filter with string. |
ca74886f | 361 | */ |
822bffce | 362 | public function testFilterString() |
ca74886f | 363 | { |
822bffce | 364 | $tags = 'dev cartoon'; |
528a6f8a | 365 | $request = array('searchtags' => $tags); |
ca74886f V |
366 | $this->assertEquals( |
367 | 2, | |
528a6f8a | 368 | count(self::$privateLinkDB->filterSearch($request, true, false)) |
ca74886f V |
369 | ); |
370 | } | |
371 | ||
372 | /** | |
822bffce | 373 | * Test filter with string. |
ca74886f | 374 | */ |
822bffce | 375 | public function testFilterArray() |
ca74886f | 376 | { |
822bffce | 377 | $tags = array('dev', 'cartoon'); |
528a6f8a | 378 | $request = array('searchtags' => $tags); |
ca74886f V |
379 | $this->assertEquals( |
380 | 2, | |
528a6f8a | 381 | count(self::$privateLinkDB->filterSearch($request, true, false)) |
ca74886f V |
382 | ); |
383 | } | |
195acf9f A |
384 | |
385 | /** | |
386 | * Test hidden tags feature: | |
387 | * tags starting with a dot '.' are only visible when logged in. | |
388 | */ | |
389 | public function testHiddenTags() | |
390 | { | |
391 | $tags = '.hidden'; | |
528a6f8a | 392 | $request = array('searchtags' => $tags); |
195acf9f A |
393 | $this->assertEquals( |
394 | 1, | |
528a6f8a | 395 | count(self::$privateLinkDB->filterSearch($request, true, false)) |
195acf9f A |
396 | ); |
397 | ||
398 | $this->assertEquals( | |
399 | 0, | |
528a6f8a | 400 | count(self::$publicLinkDB->filterSearch($request, true, false)) |
195acf9f A |
401 | ); |
402 | } | |
528a6f8a A |
403 | |
404 | /** | |
405 | * Test filterHash() with a valid smallhash. | |
406 | */ | |
407 | public function testFilterHashValid() | |
408 | { | |
409 | $request = smallHash('20150310_114651'); | |
410 | $this->assertEquals( | |
411 | 1, | |
412 | count(self::$publicLinkDB->filterHash($request)) | |
413 | ); | |
414 | } | |
415 | ||
416 | /** | |
417 | * Test filterHash() with an invalid smallhash. | |
418 | * | |
419 | * @expectedException LinkNotFoundException | |
420 | */ | |
421 | public function testFilterHashInValid1() | |
422 | { | |
423 | $request = 'blabla'; | |
424 | self::$publicLinkDB->filterHash($request); | |
425 | } | |
426 | ||
427 | /** | |
428 | * Test filterHash() with an empty smallhash. | |
429 | * | |
430 | * @expectedException LinkNotFoundException | |
431 | */ | |
432 | public function testFilterHashInValid() | |
433 | { | |
434 | self::$publicLinkDB->filterHash(''); | |
435 | } | |
ca74886f | 436 | } |