]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/LinkDBTest.php
namespacing: \Shaarli\Exceptions\IOException
[github/shaarli/Shaarli.git] / tests / LinkDBTest.php
1 <?php
2 /**
3 * Link datastore tests
4 */
5
6 use Shaarli\Exceptions\IOException;
7
8 require_once 'application/Cache.php';
9 require_once 'application/FileUtils.php';
10 require_once 'application/LinkDB.php';
11 require_once 'application/Utils.php';
12 require_once 'tests/utils/ReferenceLinkDB.php';
13
14
15 /**
16 * Unitary tests for LinkDB
17 */
18 class LinkDBTest extends PHPUnit_Framework_TestCase
19 {
20 // datastore to test write operations
21 protected static $testDatastore = 'sandbox/datastore.php';
22
23 /**
24 * @var ReferenceLinkDB instance.
25 */
26 protected static $refDB = null;
27
28 /**
29 * @var LinkDB public LinkDB instance.
30 */
31 protected static $publicLinkDB = null;
32
33 /**
34 * @var LinkDB private LinkDB instance.
35 */
36 protected static $privateLinkDB = null;
37
38 /**
39 * Instantiates public and private LinkDBs with test data
40 *
41 * The reference datastore contains public and private links that
42 * will be used to test LinkDB's methods:
43 * - access filtering (public/private),
44 * - link searches:
45 * - by day,
46 * - by tag,
47 * - by text,
48 * - etc.
49 */
50 public static function setUpBeforeClass()
51 {
52 self::$refDB = new ReferenceLinkDB();
53 self::$refDB->write(self::$testDatastore);
54
55 self::$publicLinkDB = new LinkDB(self::$testDatastore, false, false);
56 self::$privateLinkDB = new LinkDB(self::$testDatastore, true, false);
57 }
58
59 /**
60 * Resets test data for each test
61 */
62 protected function setUp()
63 {
64 if (file_exists(self::$testDatastore)) {
65 unlink(self::$testDatastore);
66 }
67 }
68
69 /**
70 * Allows to test LinkDB's private methods
71 *
72 * @see
73 * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html
74 * http://stackoverflow.com/a/2798203
75 */
76 protected static function getMethod($name)
77 {
78 $class = new ReflectionClass('LinkDB');
79 $method = $class->getMethod($name);
80 $method->setAccessible(true);
81 return $method;
82 }
83
84 /**
85 * Instantiate LinkDB objects - logged in user
86 */
87 public function testConstructLoggedIn()
88 {
89 new LinkDB(self::$testDatastore, true, false);
90 $this->assertFileExists(self::$testDatastore);
91 }
92
93 /**
94 * Instantiate LinkDB objects - logged out or public instance
95 */
96 public function testConstructLoggedOut()
97 {
98 new LinkDB(self::$testDatastore, false, false);
99 $this->assertFileExists(self::$testDatastore);
100 }
101
102 /**
103 * Attempt to instantiate a LinkDB whereas the datastore is not writable
104 *
105 * @expectedException Shaarli\Exceptions\IOException
106 * @expectedExceptionMessageRegExp /Error accessing "null"/
107 */
108 public function testConstructDatastoreNotWriteable()
109 {
110 new LinkDB('null/store.db', false, false);
111 }
112
113 /**
114 * The DB doesn't exist, ensure it is created with dummy content
115 */
116 public function testCheckDBNew()
117 {
118 $linkDB = new LinkDB(self::$testDatastore, false, false);
119 unlink(self::$testDatastore);
120 $this->assertFileNotExists(self::$testDatastore);
121
122 $checkDB = self::getMethod('check');
123 $checkDB->invokeArgs($linkDB, array());
124 $this->assertFileExists(self::$testDatastore);
125
126 // ensure the correct data has been written
127 $this->assertGreaterThan(0, filesize(self::$testDatastore));
128 }
129
130 /**
131 * The DB exists, don't do anything
132 */
133 public function testCheckDBLoad()
134 {
135 $linkDB = new LinkDB(self::$testDatastore, false, false);
136 $datastoreSize = filesize(self::$testDatastore);
137 $this->assertGreaterThan(0, $datastoreSize);
138
139 $checkDB = self::getMethod('check');
140 $checkDB->invokeArgs($linkDB, array());
141
142 // ensure the datastore is left unmodified
143 $this->assertEquals(
144 $datastoreSize,
145 filesize(self::$testDatastore)
146 );
147 }
148
149 /**
150 * Load an empty DB
151 */
152 public function testReadEmptyDB()
153 {
154 file_put_contents(self::$testDatastore, '<?php /* S7QysKquBQA= */ ?>');
155 $emptyDB = new LinkDB(self::$testDatastore, false, false);
156 $this->assertEquals(0, sizeof($emptyDB));
157 $this->assertEquals(0, count($emptyDB));
158 }
159
160 /**
161 * Load public links from the DB
162 */
163 public function testReadPublicDB()
164 {
165 $this->assertEquals(
166 self::$refDB->countPublicLinks(),
167 sizeof(self::$publicLinkDB)
168 );
169 }
170
171 /**
172 * Load public and private links from the DB
173 */
174 public function testReadPrivateDB()
175 {
176 $this->assertEquals(
177 self::$refDB->countLinks(),
178 sizeof(self::$privateLinkDB)
179 );
180 }
181
182 /**
183 * Save the links to the DB
184 */
185 public function testSave()
186 {
187 $testDB = new LinkDB(self::$testDatastore, true, false);
188 $dbSize = sizeof($testDB);
189
190 $link = array(
191 'id' => 42,
192 'title'=>'an additional link',
193 'url'=>'http://dum.my',
194 'description'=>'One more',
195 'private'=>0,
196 'created'=> DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150518_190000'),
197 'tags'=>'unit test'
198 );
199 $testDB[$link['id']] = $link;
200 $testDB->save('tests');
201
202 $testDB = new LinkDB(self::$testDatastore, true, false);
203 $this->assertEquals($dbSize + 1, sizeof($testDB));
204 }
205
206 /**
207 * Count existing links
208 */
209 public function testCount()
210 {
211 $this->assertEquals(
212 self::$refDB->countPublicLinks(),
213 self::$publicLinkDB->count()
214 );
215 $this->assertEquals(
216 self::$refDB->countLinks(),
217 self::$privateLinkDB->count()
218 );
219 }
220
221 /**
222 * Count existing links - public links hidden
223 */
224 public function testCountHiddenPublic()
225 {
226 $linkDB = new LinkDB(self::$testDatastore, false, true);
227
228 $this->assertEquals(
229 0,
230 $linkDB->count()
231 );
232 $this->assertEquals(
233 0,
234 $linkDB->count()
235 );
236 }
237
238 /**
239 * List the days for which links have been posted
240 */
241 public function testDays()
242 {
243 $this->assertEquals(
244 array('20100309', '20100310', '20121206', '20121207', '20130614', '20150310'),
245 self::$publicLinkDB->days()
246 );
247
248 $this->assertEquals(
249 array('20100309', '20100310', '20121206', '20121207', '20130614', '20141125', '20150310'),
250 self::$privateLinkDB->days()
251 );
252 }
253
254 /**
255 * The URL corresponds to an existing entry in the DB
256 */
257 public function testGetKnownLinkFromURL()
258 {
259 $link = self::$publicLinkDB->getLinkFromUrl('http://mediagoblin.org/');
260
261 $this->assertNotEquals(false, $link);
262 $this->assertContains(
263 'A free software media publishing platform',
264 $link['description']
265 );
266 }
267
268 /**
269 * The URL is not in the DB
270 */
271 public function testGetUnknownLinkFromURL()
272 {
273 $this->assertEquals(
274 false,
275 self::$publicLinkDB->getLinkFromUrl('http://dev.null')
276 );
277 }
278
279 /**
280 * Lists all tags
281 */
282 public function testAllTags()
283 {
284 $this->assertEquals(
285 array(
286 'web' => 3,
287 'cartoon' => 2,
288 'gnu' => 2,
289 'dev' => 1,
290 'samba' => 1,
291 'media' => 1,
292 'software' => 1,
293 'stallman' => 1,
294 'free' => 1,
295 '-exclude' => 1,
296 'hashtag' => 2,
297 // The DB contains a link with `sTuff` and another one with `stuff` tag.
298 // They need to be grouped with the first case found - order by date DESC: `sTuff`.
299 'sTuff' => 2,
300 'ut' => 1,
301 ),
302 self::$publicLinkDB->linksCountPerTag()
303 );
304
305 $this->assertEquals(
306 array(
307 'web' => 4,
308 'cartoon' => 3,
309 'gnu' => 2,
310 'dev' => 2,
311 'samba' => 1,
312 'media' => 1,
313 'software' => 1,
314 'stallman' => 1,
315 'free' => 1,
316 'html' => 1,
317 'w3c' => 1,
318 'css' => 1,
319 'Mercurial' => 1,
320 'sTuff' => 2,
321 '-exclude' => 1,
322 '.hidden' => 1,
323 'hashtag' => 2,
324 'tag1' => 1,
325 'tag2' => 1,
326 'tag3' => 1,
327 'tag4' => 1,
328 'ut' => 1,
329 ),
330 self::$privateLinkDB->linksCountPerTag()
331 );
332 $this->assertEquals(
333 array(
334 'web' => 4,
335 'cartoon' => 2,
336 'gnu' => 1,
337 'dev' => 1,
338 'samba' => 1,
339 'media' => 1,
340 'html' => 1,
341 'w3c' => 1,
342 'css' => 1,
343 'Mercurial' => 1,
344 '.hidden' => 1,
345 'hashtag' => 1,
346 ),
347 self::$privateLinkDB->linksCountPerTag(['web'])
348 );
349 $this->assertEquals(
350 array(
351 'web' => 1,
352 'html' => 1,
353 'w3c' => 1,
354 'css' => 1,
355 'Mercurial' => 1,
356 ),
357 self::$privateLinkDB->linksCountPerTag(['web'], 'private')
358 );
359 }
360
361 /**
362 * Test real_url without redirector.
363 */
364 public function testLinkRealUrlWithoutRedirector()
365 {
366 $db = new LinkDB(self::$testDatastore, false, false);
367 foreach ($db as $link) {
368 $this->assertEquals($link['url'], $link['real_url']);
369 }
370 }
371
372 /**
373 * Test real_url with redirector.
374 */
375 public function testLinkRealUrlWithRedirector()
376 {
377 $redirector = 'http://redirector.to?';
378 $db = new LinkDB(self::$testDatastore, false, false, $redirector);
379 foreach ($db as $link) {
380 $this->assertStringStartsWith($redirector, $link['real_url']);
381 $this->assertNotFalse(strpos($link['real_url'], urlencode('://')));
382 }
383
384 $db = new LinkDB(self::$testDatastore, false, false, $redirector, false);
385 foreach ($db as $link) {
386 $this->assertStringStartsWith($redirector, $link['real_url']);
387 $this->assertFalse(strpos($link['real_url'], urlencode('://')));
388 }
389 }
390
391 /**
392 * Test filter with string.
393 */
394 public function testFilterString()
395 {
396 $tags = 'dev cartoon';
397 $request = array('searchtags' => $tags);
398 $this->assertEquals(
399 2,
400 count(self::$privateLinkDB->filterSearch($request, true, false))
401 );
402 }
403
404 /**
405 * Test filter with string.
406 */
407 public function testFilterArray()
408 {
409 $tags = array('dev', 'cartoon');
410 $request = array('searchtags' => $tags);
411 $this->assertEquals(
412 2,
413 count(self::$privateLinkDB->filterSearch($request, true, false))
414 );
415 }
416
417 /**
418 * Test hidden tags feature:
419 * tags starting with a dot '.' are only visible when logged in.
420 */
421 public function testHiddenTags()
422 {
423 $tags = '.hidden';
424 $request = array('searchtags' => $tags);
425 $this->assertEquals(
426 1,
427 count(self::$privateLinkDB->filterSearch($request, true, false))
428 );
429
430 $this->assertEquals(
431 0,
432 count(self::$publicLinkDB->filterSearch($request, true, false))
433 );
434 }
435
436 /**
437 * Test filterHash() with a valid smallhash.
438 */
439 public function testFilterHashValid()
440 {
441 $request = smallHash('20150310_114651');
442 $this->assertEquals(
443 1,
444 count(self::$publicLinkDB->filterHash($request))
445 );
446 $request = smallHash('20150310_114633' . 8);
447 $this->assertEquals(
448 1,
449 count(self::$publicLinkDB->filterHash($request))
450 );
451 }
452
453 /**
454 * Test filterHash() with an invalid smallhash.
455 *
456 * @expectedException LinkNotFoundException
457 */
458 public function testFilterHashInValid1()
459 {
460 $request = 'blabla';
461 self::$publicLinkDB->filterHash($request);
462 }
463
464 /**
465 * Test filterHash() with an empty smallhash.
466 *
467 * @expectedException LinkNotFoundException
468 */
469 public function testFilterHashInValid()
470 {
471 self::$publicLinkDB->filterHash('');
472 }
473
474 /**
475 * Test reorder with asc/desc parameter.
476 */
477 public function testReorderLinksDesc()
478 {
479 self::$privateLinkDB->reorder('ASC');
480 $stickyIds = [11, 10];
481 $standardIds = [42, 4, 9, 1, 0, 7, 6, 8, 41];
482 $linkIds = array_merge($stickyIds, $standardIds);
483 $cpt = 0;
484 foreach (self::$privateLinkDB as $key => $value) {
485 $this->assertEquals($linkIds[$cpt++], $key);
486 }
487 self::$privateLinkDB->reorder('DESC');
488 $linkIds = array_merge(array_reverse($stickyIds), array_reverse($standardIds));
489 $cpt = 0;
490 foreach (self::$privateLinkDB as $key => $value) {
491 $this->assertEquals($linkIds[$cpt++], $key);
492 }
493 }
494
495 /**
496 * Test rename tag with a valid value present in multiple links
497 */
498 public function testRenameTagMultiple()
499 {
500 self::$refDB->write(self::$testDatastore);
501 $linkDB = new LinkDB(self::$testDatastore, true, false);
502
503 $res = $linkDB->renameTag('cartoon', 'Taz');
504 $this->assertEquals(3, count($res));
505 $this->assertContains(' Taz ', $linkDB[4]['tags']);
506 $this->assertContains(' Taz ', $linkDB[1]['tags']);
507 $this->assertContains(' Taz ', $linkDB[0]['tags']);
508 }
509
510 /**
511 * Test rename tag with a valid value
512 */
513 public function testRenameTagCaseSensitive()
514 {
515 self::$refDB->write(self::$testDatastore);
516 $linkDB = new LinkDB(self::$testDatastore, true, false, '');
517
518 $res = $linkDB->renameTag('sTuff', 'Taz');
519 $this->assertEquals(1, count($res));
520 $this->assertEquals('Taz', $linkDB[41]['tags']);
521 }
522
523 /**
524 * Test rename tag with invalid values
525 */
526 public function testRenameTagInvalid()
527 {
528 $linkDB = new LinkDB(self::$testDatastore, false, false);
529
530 $this->assertFalse($linkDB->renameTag('', 'test'));
531 $this->assertFalse($linkDB->renameTag('', ''));
532 // tag non existent
533 $this->assertEquals([], $linkDB->renameTag('test', ''));
534 $this->assertEquals([], $linkDB->renameTag('test', 'retest'));
535 }
536
537 /**
538 * Test delete tag with a valid value
539 */
540 public function testDeleteTag()
541 {
542 self::$refDB->write(self::$testDatastore);
543 $linkDB = new LinkDB(self::$testDatastore, true, false);
544
545 $res = $linkDB->renameTag('cartoon', null);
546 $this->assertEquals(3, count($res));
547 $this->assertNotContains('cartoon', $linkDB[4]['tags']);
548 }
549
550 /**
551 * Test linksCountPerTag all tags without filter.
552 * Equal occurrences should be sorted alphabetically.
553 */
554 public function testCountLinkPerTagAllNoFilter()
555 {
556 $expected = [
557 'web' => 4,
558 'cartoon' => 3,
559 'dev' => 2,
560 'gnu' => 2,
561 'hashtag' => 2,
562 'sTuff' => 2,
563 '-exclude' => 1,
564 '.hidden' => 1,
565 'Mercurial' => 1,
566 'css' => 1,
567 'free' => 1,
568 'html' => 1,
569 'media' => 1,
570 'samba' => 1,
571 'software' => 1,
572 'stallman' => 1,
573 'tag1' => 1,
574 'tag2' => 1,
575 'tag3' => 1,
576 'tag4' => 1,
577 'ut' => 1,
578 'w3c' => 1,
579 ];
580 $tags = self::$privateLinkDB->linksCountPerTag();
581
582 $this->assertEquals($expected, $tags, var_export($tags, true));
583 }
584
585 /**
586 * Test linksCountPerTag all tags with filter.
587 * Equal occurrences should be sorted alphabetically.
588 */
589 public function testCountLinkPerTagAllWithFilter()
590 {
591 $expected = [
592 'gnu' => 2,
593 'hashtag' => 2,
594 '-exclude' => 1,
595 '.hidden' => 1,
596 'free' => 1,
597 'media' => 1,
598 'software' => 1,
599 'stallman' => 1,
600 'stuff' => 1,
601 'web' => 1,
602 ];
603 $tags = self::$privateLinkDB->linksCountPerTag(['gnu']);
604
605 $this->assertEquals($expected, $tags, var_export($tags, true));
606 }
607
608 /**
609 * Test linksCountPerTag public tags with filter.
610 * Equal occurrences should be sorted alphabetically.
611 */
612 public function testCountLinkPerTagPublicWithFilter()
613 {
614 $expected = [
615 'gnu' => 2,
616 'hashtag' => 2,
617 '-exclude' => 1,
618 '.hidden' => 1,
619 'free' => 1,
620 'media' => 1,
621 'software' => 1,
622 'stallman' => 1,
623 'stuff' => 1,
624 'web' => 1,
625 ];
626 $tags = self::$privateLinkDB->linksCountPerTag(['gnu'], 'public');
627
628 $this->assertEquals($expected, $tags, var_export($tags, true));
629 }
630
631 /**
632 * Test linksCountPerTag public tags with filter.
633 * Equal occurrences should be sorted alphabetically.
634 */
635 public function testCountLinkPerTagPrivateWithFilter()
636 {
637 $expected = [
638 'cartoon' => 1,
639 'dev' => 1,
640 'tag1' => 1,
641 'tag2' => 1,
642 'tag3' => 1,
643 'tag4' => 1,
644 ];
645 $tags = self::$privateLinkDB->linksCountPerTag(['dev'], 'private');
646
647 $this->assertEquals($expected, $tags, var_export($tags, true));
648 }
649 }