]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/LinkDBTest.php
namespacing: \Shaarli\Exceptions\IOException
[github/shaarli/Shaarli.git] / tests / LinkDBTest.php
CommitLineData
ca74886f
V
1<?php
2/**
3 * Link datastore tests
4 */
5
f3d2f257
V
6use Shaarli\Exceptions\IOException;
7
01e48f26 8require_once 'application/Cache.php';
2e28269b 9require_once 'application/FileUtils.php';
ca74886f
V
10require_once 'application/LinkDB.php';
11require_once 'application/Utils.php';
12require_once 'tests/utils/ReferenceLinkDB.php';
13
ca74886f
V
14
15/**
16 * Unitary tests for LinkDB
17 */
18class LinkDBTest extends PHPUnit_Framework_TestCase
19{
20 // datastore to test write operations
4bf35ba5 21 protected static $testDatastore = 'sandbox/datastore.php';
528a6f8a
A
22
23 /**
24 * @var ReferenceLinkDB instance.
25 */
ca74886f 26 protected static $refDB = null;
528a6f8a
A
27
28 /**
29 * @var LinkDB public LinkDB instance.
30 */
ca74886f 31 protected static $publicLinkDB = null;
528a6f8a
A
32
33 /**
34 * @var LinkDB private LinkDB instance.
35 */
ca74886f
V
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();
9c8752a2 53 self::$refDB->write(self::$testDatastore);
ca74886f 54
9c8752a2
V
55 self::$publicLinkDB = new LinkDB(self::$testDatastore, false, false);
56 self::$privateLinkDB = new LinkDB(self::$testDatastore, true, false);
ca74886f
V
57 }
58
59 /**
60 * Resets test data for each test
61 */
62 protected function setUp()
63 {
ca74886f
V
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 {
9c8752a2 89 new LinkDB(self::$testDatastore, true, false);
ca74886f
V
90 $this->assertFileExists(self::$testDatastore);
91 }
92
93 /**
94 * Instantiate LinkDB objects - logged out or public instance
95 */
96 public function testConstructLoggedOut()
97 {
9c8752a2 98 new LinkDB(self::$testDatastore, false, false);
ca74886f
V
99 $this->assertFileExists(self::$testDatastore);
100 }
101
102 /**
103 * Attempt to instantiate a LinkDB whereas the datastore is not writable
104 *
f3d2f257 105 * @expectedException Shaarli\Exceptions\IOException
b2306b0c 106 * @expectedExceptionMessageRegExp /Error accessing "null"/
ca74886f
V
107 */
108 public function testConstructDatastoreNotWriteable()
109 {
9c8752a2 110 new LinkDB('null/store.db', false, false);
ca74886f
V
111 }
112
113 /**
114 * The DB doesn't exist, ensure it is created with dummy content
115 */
116 public function testCheckDBNew()
117 {
9c8752a2 118 $linkDB = new LinkDB(self::$testDatastore, false, false);
ca74886f
V
119 unlink(self::$testDatastore);
120 $this->assertFileNotExists(self::$testDatastore);
121
f21abf32 122 $checkDB = self::getMethod('check');
ca74886f
V
123 $checkDB->invokeArgs($linkDB, array());
124 $this->assertFileExists(self::$testDatastore);
125
126 // ensure the correct data has been written
0037fbe1 127 $this->assertGreaterThan(0, filesize(self::$testDatastore));
ca74886f
V
128 }
129
130 /**
131 * The DB exists, don't do anything
132 */
133 public function testCheckDBLoad()
134 {
9c8752a2 135 $linkDB = new LinkDB(self::$testDatastore, false, false);
0037fbe1
V
136 $datastoreSize = filesize(self::$testDatastore);
137 $this->assertGreaterThan(0, $datastoreSize);
ca74886f 138
f21abf32 139 $checkDB = self::getMethod('check');
ca74886f
V
140 $checkDB->invokeArgs($linkDB, array());
141
142 // ensure the datastore is left unmodified
143 $this->assertEquals(
0037fbe1 144 $datastoreSize,
30e6f1ca 145 filesize(self::$testDatastore)
ca74886f
V
146 );
147 }
148
149 /**
150 * Load an empty DB
151 */
152 public function testReadEmptyDB()
153 {
9c8752a2
V
154 file_put_contents(self::$testDatastore, '<?php /* S7QysKquBQA= */ ?>');
155 $emptyDB = new LinkDB(self::$testDatastore, false, false);
ca74886f
V
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 */
f21abf32 185 public function testSave()
ca74886f 186 {
9c8752a2 187 $testDB = new LinkDB(self::$testDatastore, true, false);
ca74886f
V
188 $dbSize = sizeof($testDB);
189
190 $link = array(
c3dfd899 191 'id' => 42,
ca74886f
V
192 'title'=>'an additional link',
193 'url'=>'http://dum.my',
194 'description'=>'One more',
195 'private'=>0,
d592daea 196 'created'=> DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150518_190000'),
ca74886f
V
197 'tags'=>'unit test'
198 );
c3dfd899 199 $testDB[$link['id']] = $link;
f21abf32 200 $testDB->save('tests');
ca74886f 201
9c8752a2 202 $testDB = new LinkDB(self::$testDatastore, true, false);
ca74886f
V
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
9f15ca9e
V
221 /**
222 * Count existing links - public links hidden
223 */
224 public function testCountHiddenPublic()
225 {
9c8752a2 226 $linkDB = new LinkDB(self::$testDatastore, false, true);
9f15ca9e
V
227
228 $this->assertEquals(
229 0,
230 $linkDB->count()
231 );
232 $this->assertEquals(
233 0,
234 $linkDB->count()
235 );
236 }
237
ca74886f
V
238 /**
239 * List the days for which links have been posted
240 */
241 public function testDays()
242 {
243 $this->assertEquals(
4154c25b 244 array('20100309', '20100310', '20121206', '20121207', '20130614', '20150310'),
ca74886f
V
245 self::$publicLinkDB->days()
246 );
247
248 $this->assertEquals(
4154c25b 249 array('20100309', '20100310', '20121206', '20121207', '20130614', '20141125', '20150310'),
ca74886f
V
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);
9ccca401 262 $this->assertContains(
ca74886f
V
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(
d1e2f8e5 285 array(
ca74886f
V
286 'web' => 3,
287 'cartoon' => 2,
288 'gnu' => 2,
289 'dev' => 1,
290 'samba' => 1,
291 'media' => 1,
292 'software' => 1,
293 'stallman' => 1,
21979ff1
A
294 'free' => 1,
295 '-exclude' => 1,
c3dfd899 296 'hashtag' => 2,
b1eb5d1d 297 // The DB contains a link with `sTuff` and another one with `stuff` tag.
c3dfd899 298 // They need to be grouped with the first case found - order by date DESC: `sTuff`.
b1eb5d1d 299 'sTuff' => 2,
c3dfd899 300 'ut' => 1,
d1e2f8e5 301 ),
6ccd0b21 302 self::$publicLinkDB->linksCountPerTag()
ca74886f
V
303 );
304
305 $this->assertEquals(
d1e2f8e5 306 array(
ca74886f
V
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,
21979ff1 319 'Mercurial' => 1,
b1eb5d1d 320 'sTuff' => 2,
21979ff1 321 '-exclude' => 1,
195acf9f 322 '.hidden' => 1,
9ccca401 323 'hashtag' => 2,
9866b408
A
324 'tag1' => 1,
325 'tag2' => 1,
326 'tag3' => 1,
327 'tag4' => 1,
c3dfd899 328 'ut' => 1,
d1e2f8e5 329 ),
6ccd0b21
LC
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')
ca74886f
V
358 );
359 }
360
361 /**
822bffce 362 * Test real_url without redirector.
ca74886f 363 */
822bffce 364 public function testLinkRealUrlWithoutRedirector()
ca74886f 365 {
822bffce 366 $db = new LinkDB(self::$testDatastore, false, false);
067c2dd8 367 foreach ($db as $link) {
822bffce
A
368 $this->assertEquals($link['url'], $link['real_url']);
369 }
ca74886f
V
370 }
371
372 /**
822bffce 373 * Test real_url with redirector.
ca74886f 374 */
822bffce 375 public function testLinkRealUrlWithRedirector()
ca74886f 376 {
822bffce
A
377 $redirector = 'http://redirector.to?';
378 $db = new LinkDB(self::$testDatastore, false, false, $redirector);
067c2dd8 379 foreach ($db as $link) {
822bffce 380 $this->assertStringStartsWith($redirector, $link['real_url']);
043eae70
A
381 $this->assertNotFalse(strpos($link['real_url'], urlencode('://')));
382 }
383
384 $db = new LinkDB(self::$testDatastore, false, false, $redirector, false);
067c2dd8 385 foreach ($db as $link) {
043eae70
A
386 $this->assertStringStartsWith($redirector, $link['real_url']);
387 $this->assertFalse(strpos($link['real_url'], urlencode('://')));
822bffce 388 }
ca74886f
V
389 }
390
391 /**
822bffce 392 * Test filter with string.
ca74886f 393 */
822bffce 394 public function testFilterString()
ca74886f 395 {
822bffce 396 $tags = 'dev cartoon';
528a6f8a 397 $request = array('searchtags' => $tags);
ca74886f
V
398 $this->assertEquals(
399 2,
528a6f8a 400 count(self::$privateLinkDB->filterSearch($request, true, false))
ca74886f
V
401 );
402 }
403
404 /**
822bffce 405 * Test filter with string.
ca74886f 406 */
822bffce 407 public function testFilterArray()
ca74886f 408 {
822bffce 409 $tags = array('dev', 'cartoon');
528a6f8a 410 $request = array('searchtags' => $tags);
ca74886f
V
411 $this->assertEquals(
412 2,
528a6f8a 413 count(self::$privateLinkDB->filterSearch($request, true, false))
ca74886f
V
414 );
415 }
195acf9f
A
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';
528a6f8a 424 $request = array('searchtags' => $tags);
195acf9f
A
425 $this->assertEquals(
426 1,
528a6f8a 427 count(self::$privateLinkDB->filterSearch($request, true, false))
195acf9f
A
428 );
429
430 $this->assertEquals(
431 0,
528a6f8a 432 count(self::$publicLinkDB->filterSearch($request, true, false))
195acf9f
A
433 );
434 }
528a6f8a
A
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 );
c3dfd899
A
446 $request = smallHash('20150310_114633' . 8);
447 $this->assertEquals(
448 1,
449 count(self::$publicLinkDB->filterHash($request))
450 );
528a6f8a
A
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 }
c3dfd899
A
473
474 /**
475 * Test reorder with asc/desc parameter.
476 */
477 public function testReorderLinksDesc()
478 {
d592daea 479 self::$privateLinkDB->reorder('ASC');
4154c25b
A
480 $stickyIds = [11, 10];
481 $standardIds = [42, 4, 9, 1, 0, 7, 6, 8, 41];
482 $linkIds = array_merge($stickyIds, $standardIds);
d592daea
A
483 $cpt = 0;
484 foreach (self::$privateLinkDB as $key => $value) {
485 $this->assertEquals($linkIds[$cpt++], $key);
c3dfd899 486 }
d592daea 487 self::$privateLinkDB->reorder('DESC');
4154c25b 488 $linkIds = array_merge(array_reverse($stickyIds), array_reverse($standardIds));
d592daea
A
489 $cpt = 0;
490 foreach (self::$privateLinkDB as $key => $value) {
491 $this->assertEquals($linkIds[$cpt++], $key);
c3dfd899
A
492 }
493 }
3b67b222
A
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 }
f8c5660d
A
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 }
ca74886f 649}