]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/legacy/LegacyLinkDBTest.php
Fix missing @expectedException convertion
[github/shaarli/Shaarli.git] / tests / legacy / LegacyLinkDBTest.php
CommitLineData
ca74886f
V
1<?php
2/**
3 * Link datastore tests
4 */
5
e26e2060 6namespace Shaarli\Legacy;
f24896b2
V
7
8use DateTime;
f24896b2
V
9use ReferenceLinkDB;
10use ReflectionClass;
11use Shaarli;
e26e2060 12use Shaarli\Bookmark\Bookmark;
f24896b2 13
ca74886f
V
14require_once 'application/Utils.php';
15require_once 'tests/utils/ReferenceLinkDB.php';
16
ca74886f
V
17
18/**
e26e2060 19 * Unitary tests for LegacyLinkDBTest
ca74886f 20 */
a5a9cf23 21class LegacyLinkDBTest extends \Shaarli\TestCase
ca74886f
V
22{
23 // datastore to test write operations
4bf35ba5 24 protected static $testDatastore = 'sandbox/datastore.php';
528a6f8a
A
25
26 /**
27 * @var ReferenceLinkDB instance.
28 */
ca74886f 29 protected static $refDB = null;
528a6f8a
A
30
31 /**
e26e2060 32 * @var LegacyLinkDB public LinkDB instance.
528a6f8a 33 */
ca74886f 34 protected static $publicLinkDB = null;
528a6f8a
A
35
36 /**
e26e2060 37 * @var LegacyLinkDB private LinkDB instance.
528a6f8a 38 */
ca74886f
V
39 protected static $privateLinkDB = null;
40
41 /**
42 * Instantiates public and private LinkDBs with test data
43 *
e26e2060 44 * The reference datastore contains public and private bookmarks that
ca74886f
V
45 * will be used to test LinkDB's methods:
46 * - access filtering (public/private),
47 * - link searches:
48 * - by day,
49 * - by tag,
50 * - by text,
51 * - etc.
bd1adc8d 52 *
ca74886f
V
53 * Resets test data for each test
54 */
8f60e120 55 protected function setUp(): void
ca74886f 56 {
ca74886f
V
57 if (file_exists(self::$testDatastore)) {
58 unlink(self::$testDatastore);
59 }
bd1adc8d 60
e26e2060 61 self::$refDB = new ReferenceLinkDB(true);
bd1adc8d 62 self::$refDB->write(self::$testDatastore);
e26e2060
A
63 self::$publicLinkDB = new LegacyLinkDB(self::$testDatastore, false, false);
64 self::$privateLinkDB = new LegacyLinkDB(self::$testDatastore, true, false);
ca74886f
V
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 {
e26e2060 76 $class = new ReflectionClass('Shaarli\Legacy\LegacyLinkDB');
ca74886f
V
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 {
e26e2060 87 new LegacyLinkDB(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 {
e26e2060 96 new LegacyLinkDB(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
ca74886f
V
102 */
103 public function testConstructDatastoreNotWriteable()
104 {
f447edb7 105 $this->expectException(\Shaarli\Exceptions\IOException::class);
b1baca99
A
106 $this->expectExceptionMessageRegExp('/Error accessing "null"/');
107
e26e2060 108 new LegacyLinkDB('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 {
e26e2060 116 $linkDB = new LegacyLinkDB(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 {
e26e2060 133 $linkDB = new LegacyLinkDB(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 152 file_put_contents(self::$testDatastore, '<?php /* S7QysKquBQA= */ ?>');
e26e2060 153 $emptyDB = new LegacyLinkDB(self::$testDatastore, false, false);
ca74886f
V
154 $this->assertEquals(0, sizeof($emptyDB));
155 $this->assertEquals(0, count($emptyDB));
156 }
157
158 /**
e26e2060 159 * Load public bookmarks from the DB
ca74886f
V
160 */
161 public function testReadPublicDB()
162 {
163 $this->assertEquals(
164 self::$refDB->countPublicLinks(),
165 sizeof(self::$publicLinkDB)
166 );
167 }
168
169 /**
e26e2060 170 * Load public and private bookmarks from the DB
ca74886f
V
171 */
172 public function testReadPrivateDB()
173 {
174 $this->assertEquals(
175 self::$refDB->countLinks(),
176 sizeof(self::$privateLinkDB)
177 );
178 }
179
180 /**
e26e2060 181 * Save the bookmarks to the DB
ca74886f 182 */
f21abf32 183 public function testSave()
ca74886f 184 {
e26e2060 185 $testDB = new LegacyLinkDB(self::$testDatastore, true, false);
ca74886f
V
186 $dbSize = sizeof($testDB);
187
188 $link = array(
bd1adc8d 189 'id' => 43,
f24896b2
V
190 'title' => 'an additional link',
191 'url' => 'http://dum.my',
192 'description' => 'One more',
193 'private' => 0,
e26e2060 194 'created' => DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150518_190000'),
f24896b2 195 'tags' => 'unit test'
ca74886f 196 );
c3dfd899 197 $testDB[$link['id']] = $link;
f21abf32 198 $testDB->save('tests');
ca74886f 199
e26e2060 200 $testDB = new LegacyLinkDB(self::$testDatastore, true, false);
ca74886f
V
201 $this->assertEquals($dbSize + 1, sizeof($testDB));
202 }
203
204 /**
e26e2060 205 * Count existing bookmarks
ca74886f
V
206 */
207 public function testCount()
208 {
209 $this->assertEquals(
210 self::$refDB->countPublicLinks(),
211 self::$publicLinkDB->count()
212 );
213 $this->assertEquals(
214 self::$refDB->countLinks(),
215 self::$privateLinkDB->count()
216 );
217 }
218
9f15ca9e 219 /**
e26e2060 220 * Count existing bookmarks - public bookmarks hidden
9f15ca9e
V
221 */
222 public function testCountHiddenPublic()
223 {
e26e2060 224 $linkDB = new LegacyLinkDB(self::$testDatastore, false, true);
9f15ca9e
V
225
226 $this->assertEquals(
227 0,
228 $linkDB->count()
229 );
230 $this->assertEquals(
231 0,
232 $linkDB->count()
233 );
234 }
235
ca74886f 236 /**
e26e2060 237 * List the days for which bookmarks have been posted
ca74886f
V
238 */
239 public function testDays()
240 {
241 $this->assertEquals(
4154c25b 242 array('20100309', '20100310', '20121206', '20121207', '20130614', '20150310'),
ca74886f
V
243 self::$publicLinkDB->days()
244 );
245
246 $this->assertEquals(
4154c25b 247 array('20100309', '20100310', '20121206', '20121207', '20130614', '20141125', '20150310'),
ca74886f
V
248 self::$privateLinkDB->days()
249 );
250 }
251
252 /**
253 * The URL corresponds to an existing entry in the DB
254 */
255 public function testGetKnownLinkFromURL()
256 {
257 $link = self::$publicLinkDB->getLinkFromUrl('http://mediagoblin.org/');
258
259 $this->assertNotEquals(false, $link);
a5a9cf23 260 $this->assertContainsPolyfill(
ca74886f
V
261 'A free software media publishing platform',
262 $link['description']
263 );
264 }
265
266 /**
267 * The URL is not in the DB
268 */
269 public function testGetUnknownLinkFromURL()
270 {
271 $this->assertEquals(
272 false,
273 self::$publicLinkDB->getLinkFromUrl('http://dev.null')
274 );
275 }
276
277 /**
278 * Lists all tags
279 */
280 public function testAllTags()
281 {
282 $this->assertEquals(
d1e2f8e5 283 array(
ca74886f
V
284 'web' => 3,
285 'cartoon' => 2,
286 'gnu' => 2,
287 'dev' => 1,
288 'samba' => 1,
289 'media' => 1,
290 'software' => 1,
291 'stallman' => 1,
21979ff1
A
292 'free' => 1,
293 '-exclude' => 1,
c3dfd899 294 'hashtag' => 2,
b1eb5d1d 295 // The DB contains a link with `sTuff` and another one with `stuff` tag.
c3dfd899 296 // They need to be grouped with the first case found - order by date DESC: `sTuff`.
b1eb5d1d 297 'sTuff' => 2,
c3dfd899 298 'ut' => 1,
d1e2f8e5 299 ),
6ccd0b21 300 self::$publicLinkDB->linksCountPerTag()
ca74886f
V
301 );
302
303 $this->assertEquals(
d1e2f8e5 304 array(
ca74886f
V
305 'web' => 4,
306 'cartoon' => 3,
307 'gnu' => 2,
308 'dev' => 2,
309 'samba' => 1,
310 'media' => 1,
311 'software' => 1,
312 'stallman' => 1,
313 'free' => 1,
314 'html' => 1,
315 'w3c' => 1,
316 'css' => 1,
21979ff1 317 'Mercurial' => 1,
b1eb5d1d 318 'sTuff' => 2,
21979ff1 319 '-exclude' => 1,
195acf9f 320 '.hidden' => 1,
9ccca401 321 'hashtag' => 2,
9866b408
A
322 'tag1' => 1,
323 'tag2' => 1,
324 'tag3' => 1,
325 'tag4' => 1,
c3dfd899 326 'ut' => 1,
d1e2f8e5 327 ),
6ccd0b21
LC
328 self::$privateLinkDB->linksCountPerTag()
329 );
330 $this->assertEquals(
331 array(
332 'web' => 4,
333 'cartoon' => 2,
334 'gnu' => 1,
335 'dev' => 1,
336 'samba' => 1,
337 'media' => 1,
338 'html' => 1,
339 'w3c' => 1,
340 'css' => 1,
341 'Mercurial' => 1,
342 '.hidden' => 1,
343 'hashtag' => 1,
344 ),
345 self::$privateLinkDB->linksCountPerTag(['web'])
346 );
347 $this->assertEquals(
348 array(
349 'web' => 1,
350 'html' => 1,
351 'w3c' => 1,
352 'css' => 1,
353 'Mercurial' => 1,
354 ),
355 self::$privateLinkDB->linksCountPerTag(['web'], 'private')
ca74886f
V
356 );
357 }
358
ca74886f 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 );
c3dfd899
A
414 $request = smallHash('20150310_114633' . 8);
415 $this->assertEquals(
416 1,
417 count(self::$publicLinkDB->filterHash($request))
418 );
528a6f8a
A
419 }
420
421 /**
422 * Test filterHash() with an invalid smallhash.
528a6f8a
A
423 */
424 public function testFilterHashInValid1()
425 {
b1baca99
A
426 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class);
427
528a6f8a
A
428 $request = 'blabla';
429 self::$publicLinkDB->filterHash($request);
430 }
431
432 /**
433 * Test filterHash() with an empty smallhash.
528a6f8a
A
434 */
435 public function testFilterHashInValid()
436 {
b1baca99
A
437 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class);
438
528a6f8a
A
439 self::$publicLinkDB->filterHash('');
440 }
c3dfd899
A
441
442 /**
443 * Test reorder with asc/desc parameter.
444 */
445 public function testReorderLinksDesc()
446 {
d592daea 447 self::$privateLinkDB->reorder('ASC');
4154c25b
A
448 $stickyIds = [11, 10];
449 $standardIds = [42, 4, 9, 1, 0, 7, 6, 8, 41];
450 $linkIds = array_merge($stickyIds, $standardIds);
d592daea
A
451 $cpt = 0;
452 foreach (self::$privateLinkDB as $key => $value) {
453 $this->assertEquals($linkIds[$cpt++], $key);
c3dfd899 454 }
d592daea 455 self::$privateLinkDB->reorder('DESC');
4154c25b 456 $linkIds = array_merge(array_reverse($stickyIds), array_reverse($standardIds));
d592daea
A
457 $cpt = 0;
458 foreach (self::$privateLinkDB as $key => $value) {
459 $this->assertEquals($linkIds[$cpt++], $key);
c3dfd899
A
460 }
461 }
3b67b222
A
462
463 /**
e26e2060 464 * Test rename tag with a valid value present in multiple bookmarks
3b67b222
A
465 */
466 public function testRenameTagMultiple()
467 {
468 self::$refDB->write(self::$testDatastore);
e26e2060 469 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
3b67b222
A
470
471 $res = $linkDB->renameTag('cartoon', 'Taz');
472 $this->assertEquals(3, count($res));
a5a9cf23
A
473 $this->assertContainsPolyfill(' Taz ', $linkDB[4]['tags']);
474 $this->assertContainsPolyfill(' Taz ', $linkDB[1]['tags']);
475 $this->assertContainsPolyfill(' Taz ', $linkDB[0]['tags']);
3b67b222
A
476 }
477
478 /**
479 * Test rename tag with a valid value
480 */
481 public function testRenameTagCaseSensitive()
482 {
483 self::$refDB->write(self::$testDatastore);
e26e2060 484 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
3b67b222
A
485
486 $res = $linkDB->renameTag('sTuff', 'Taz');
487 $this->assertEquals(1, count($res));
488 $this->assertEquals('Taz', $linkDB[41]['tags']);
489 }
490
491 /**
492 * Test rename tag with invalid values
493 */
494 public function testRenameTagInvalid()
495 {
e26e2060 496 $linkDB = new LegacyLinkDB(self::$testDatastore, false, false);
3b67b222
A
497
498 $this->assertFalse($linkDB->renameTag('', 'test'));
499 $this->assertFalse($linkDB->renameTag('', ''));
500 // tag non existent
501 $this->assertEquals([], $linkDB->renameTag('test', ''));
502 $this->assertEquals([], $linkDB->renameTag('test', 'retest'));
503 }
504
505 /**
506 * Test delete tag with a valid value
507 */
508 public function testDeleteTag()
509 {
510 self::$refDB->write(self::$testDatastore);
e26e2060 511 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
3b67b222
A
512
513 $res = $linkDB->renameTag('cartoon', null);
514 $this->assertEquals(3, count($res));
a5a9cf23 515 $this->assertNotContainsPolyfill('cartoon', $linkDB[4]['tags']);
3b67b222 516 }
f8c5660d
A
517
518 /**
519 * Test linksCountPerTag all tags without filter.
520 * Equal occurrences should be sorted alphabetically.
521 */
522 public function testCountLinkPerTagAllNoFilter()
523 {
524 $expected = [
525 'web' => 4,
526 'cartoon' => 3,
527 'dev' => 2,
528 'gnu' => 2,
529 'hashtag' => 2,
530 'sTuff' => 2,
531 '-exclude' => 1,
532 '.hidden' => 1,
533 'Mercurial' => 1,
534 'css' => 1,
535 'free' => 1,
536 'html' => 1,
537 'media' => 1,
538 'samba' => 1,
539 'software' => 1,
540 'stallman' => 1,
541 'tag1' => 1,
542 'tag2' => 1,
543 'tag3' => 1,
544 'tag4' => 1,
545 'ut' => 1,
546 'w3c' => 1,
547 ];
548 $tags = self::$privateLinkDB->linksCountPerTag();
549
550 $this->assertEquals($expected, $tags, var_export($tags, true));
551 }
552
553 /**
554 * Test linksCountPerTag all tags with filter.
555 * Equal occurrences should be sorted alphabetically.
556 */
557 public function testCountLinkPerTagAllWithFilter()
558 {
559 $expected = [
560 'gnu' => 2,
561 'hashtag' => 2,
562 '-exclude' => 1,
563 '.hidden' => 1,
564 'free' => 1,
565 'media' => 1,
566 'software' => 1,
567 'stallman' => 1,
568 'stuff' => 1,
569 'web' => 1,
570 ];
571 $tags = self::$privateLinkDB->linksCountPerTag(['gnu']);
572
573 $this->assertEquals($expected, $tags, var_export($tags, true));
574 }
575
576 /**
577 * Test linksCountPerTag public tags with filter.
578 * Equal occurrences should be sorted alphabetically.
579 */
580 public function testCountLinkPerTagPublicWithFilter()
581 {
582 $expected = [
583 'gnu' => 2,
584 'hashtag' => 2,
585 '-exclude' => 1,
586 '.hidden' => 1,
587 'free' => 1,
588 'media' => 1,
589 'software' => 1,
590 'stallman' => 1,
591 'stuff' => 1,
592 'web' => 1,
593 ];
594 $tags = self::$privateLinkDB->linksCountPerTag(['gnu'], 'public');
595
596 $this->assertEquals($expected, $tags, var_export($tags, true));
597 }
598
599 /**
600 * Test linksCountPerTag public tags with filter.
601 * Equal occurrences should be sorted alphabetically.
602 */
603 public function testCountLinkPerTagPrivateWithFilter()
604 {
605 $expected = [
606 'cartoon' => 1,
607 'dev' => 1,
608 'tag1' => 1,
609 'tag2' => 1,
610 'tag3' => 1,
611 'tag4' => 1,
612 ];
613 $tags = self::$privateLinkDB->linksCountPerTag(['dev'], 'private');
614
615 $this->assertEquals($expected, $tags, var_export($tags, true));
616 }
9f962705
A
617
618 /**
619 * Make sure that bookmarks with the same timestamp have a consistent order:
620 * if their creation date is equal, bookmarks are sorted by ID DESC.
621 */
622 public function testConsistentOrder()
623 {
bd1adc8d 624 $nextId = 43;
9f962705 625 $creation = DateTime::createFromFormat('Ymd_His', '20190807_130444');
e26e2060 626 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
9f962705
A
627 for ($i = 0; $i < 4; ++$i) {
628 $linkDB[$nextId + $i] = [
629 'id' => $nextId + $i,
630 'url' => 'http://'. $i,
631 'created' => $creation,
632 'title' => true,
633 'description' => true,
634 'tags' => true,
635 ];
636 }
637
638 // Check 4 new links 4 times
639 for ($i = 0; $i < 4; ++$i) {
640 $linkDB->save('tests');
e26e2060 641 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
9f962705
A
642 $count = 3;
643 foreach ($linkDB as $link) {
bd1adc8d
A
644 if ($link['sticky'] === true) {
645 continue;
646 }
9f962705
A
647 $this->assertEquals($nextId + $count, $link['id']);
648 $this->assertEquals('http://'. $count, $link['url']);
649 if (--$count < 0) {
650 break;
651 }
652 }
653 }
654 }
ca74886f 655}