6 namespace Shaarli\Bookmark
;
13 require_once 'application/feed/Cache.php';
14 require_once 'application/Utils.php';
15 require_once 'tests/utils/ReferenceLinkDB.php';
19 * Unitary tests for LinkDB
21 class LinkDBTest
extends \PHPUnit\Framework\TestCase
23 // datastore to test write operations
24 protected static $testDatastore = 'sandbox/datastore.php';
27 * @var ReferenceLinkDB instance.
29 protected static $refDB = null;
32 * @var LinkDB public LinkDB instance.
34 protected static $publicLinkDB = null;
37 * @var LinkDB private LinkDB instance.
39 protected static $privateLinkDB = null;
42 * Instantiates public and private LinkDBs with test data
44 * The reference datastore contains public and private links that
45 * will be used to test LinkDB's methods:
46 * - access filtering (public/private),
53 public static function setUpBeforeClass()
55 self
::$refDB = new ReferenceLinkDB();
56 self
::$refDB->write(self
::$testDatastore);
58 self
::$publicLinkDB = new LinkDB(self
::$testDatastore, false, false);
59 self
::$privateLinkDB = new LinkDB(self
::$testDatastore, true, false);
63 * Resets test data for each test
65 protected function setUp()
67 if (file_exists(self
::$testDatastore)) {
68 unlink(self
::$testDatastore);
73 * Allows to test LinkDB's private methods
76 * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html
77 * http://stackoverflow.com/a/2798203
79 protected static function getMethod($name)
81 $class = new ReflectionClass('Shaarli\Bookmark\LinkDB');
82 $method = $class->getMethod($name);
83 $method->setAccessible(true);
88 * Instantiate LinkDB objects - logged in user
90 public function testConstructLoggedIn()
92 new LinkDB(self
::$testDatastore, true, false);
93 $this->assertFileExists(self
::$testDatastore);
97 * Instantiate LinkDB objects - logged out or public instance
99 public function testConstructLoggedOut()
101 new LinkDB(self
::$testDatastore, false, false);
102 $this->assertFileExists(self
::$testDatastore);
106 * Attempt to instantiate a LinkDB whereas the datastore is not writable
108 * @expectedException Shaarli\Exceptions\IOException
109 * @expectedExceptionMessageRegExp /Error accessing "null"/
111 public function testConstructDatastoreNotWriteable()
113 new LinkDB('null/store.db', false, false);
117 * The DB doesn't exist, ensure it is created with dummy content
119 public function testCheckDBNew()
121 $linkDB = new LinkDB(self
::$testDatastore, false, false);
122 unlink(self
::$testDatastore);
123 $this->assertFileNotExists(self
::$testDatastore);
125 $checkDB = self
::getMethod('check');
126 $checkDB->invokeArgs($linkDB, array());
127 $this->assertFileExists(self
::$testDatastore);
129 // ensure the correct data has been written
130 $this->assertGreaterThan(0, filesize(self
::$testDatastore));
134 * The DB exists, don't do anything
136 public function testCheckDBLoad()
138 $linkDB = new LinkDB(self
::$testDatastore, false, false);
139 $datastoreSize = filesize(self
::$testDatastore);
140 $this->assertGreaterThan(0, $datastoreSize);
142 $checkDB = self
::getMethod('check');
143 $checkDB->invokeArgs($linkDB, array());
145 // ensure the datastore is left unmodified
148 filesize(self
::$testDatastore)
155 public function testReadEmptyDB()
157 file_put_contents(self
::$testDatastore, '<?php /* S7QysKquBQA= */ ?>');
158 $emptyDB = new LinkDB(self
::$testDatastore, false, false);
159 $this->assertEquals(0, sizeof($emptyDB));
160 $this->assertEquals(0, count($emptyDB));
164 * Load public links from the DB
166 public function testReadPublicDB()
169 self
::$refDB->countPublicLinks(),
170 sizeof(self
::$publicLinkDB)
175 * Load public and private links from the DB
177 public function testReadPrivateDB()
180 self
::$refDB->countLinks(),
181 sizeof(self
::$privateLinkDB)
186 * Save the links to the DB
188 public function testSave()
190 $testDB = new LinkDB(self
::$testDatastore, true, false);
191 $dbSize = sizeof($testDB);
195 'title' => 'an additional link',
196 'url' => 'http://dum.my',
197 'description' => 'One more',
199 'created' => DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20150518_190000'),
200 'tags' => 'unit test'
202 $testDB[$link['id']] = $link;
203 $testDB->save('tests');
205 $testDB = new LinkDB(self
::$testDatastore, true, false);
206 $this->assertEquals($dbSize +
1, sizeof($testDB));
210 * Count existing links
212 public function testCount()
215 self
::$refDB->countPublicLinks(),
216 self
::$publicLinkDB->count()
219 self
::$refDB->countLinks(),
220 self
::$privateLinkDB->count()
225 * Count existing links - public links hidden
227 public function testCountHiddenPublic()
229 $linkDB = new LinkDB(self
::$testDatastore, false, true);
242 * List the days for which links have been posted
244 public function testDays()
247 array('20100309', '20100310', '20121206', '20121207', '20130614', '20150310'),
248 self
::$publicLinkDB->days()
252 array('20100309', '20100310', '20121206', '20121207', '20130614', '20141125', '20150310'),
253 self
::$privateLinkDB->days()
258 * The URL corresponds to an existing entry in the DB
260 public function testGetKnownLinkFromURL()
262 $link = self
::$publicLinkDB->getLinkFromUrl('http://mediagoblin.org/');
264 $this->assertNotEquals(false, $link);
265 $this->assertContains(
266 'A free software media publishing platform',
272 * The URL is not in the DB
274 public function testGetUnknownLinkFromURL()
278 self
::$publicLinkDB->getLinkFromUrl('http://dev.null')
285 public function testAllTags()
300 // The DB contains a link with `sTuff` and another one with `stuff` tag.
301 // They need to be grouped with the first case found - order by date DESC: `sTuff`.
305 self
::$publicLinkDB->linksCountPerTag()
333 self
::$privateLinkDB->linksCountPerTag()
350 self
::$privateLinkDB->linksCountPerTag(['web'])
360 self
::$privateLinkDB->linksCountPerTag(['web'], 'private')
365 * Test filter with string.
367 public function testFilterString()
369 $tags = 'dev cartoon';
370 $request = array('searchtags' => $tags);
373 count(self
::$privateLinkDB->filterSearch($request, true, false))
378 * Test filter with string.
380 public function testFilterArray()
382 $tags = array('dev', 'cartoon');
383 $request = array('searchtags' => $tags);
386 count(self
::$privateLinkDB->filterSearch($request, true, false))
391 * Test hidden tags feature:
392 * tags starting with a dot '.' are only visible when logged in.
394 public function testHiddenTags()
397 $request = array('searchtags' => $tags);
400 count(self
::$privateLinkDB->filterSearch($request, true, false))
405 count(self
::$publicLinkDB->filterSearch($request, true, false))
410 * Test filterHash() with a valid smallhash.
412 public function testFilterHashValid()
414 $request = smallHash('20150310_114651');
417 count(self
::$publicLinkDB->filterHash($request))
419 $request = smallHash('20150310_114633' . 8);
422 count(self
::$publicLinkDB->filterHash($request))
427 * Test filterHash() with an invalid smallhash.
429 * @expectedException \Shaarli\Bookmark\Exception\LinkNotFoundException
431 public function testFilterHashInValid1()
434 self
::$publicLinkDB->filterHash($request);
438 * Test filterHash() with an empty smallhash.
440 * @expectedException \Shaarli\Bookmark\Exception\LinkNotFoundException
442 public function testFilterHashInValid()
444 self
::$publicLinkDB->filterHash('');
448 * Test reorder with asc/desc parameter.
450 public function testReorderLinksDesc()
452 self
::$privateLinkDB->reorder('ASC');
453 $stickyIds = [11, 10];
454 $standardIds = [42, 4, 9, 1, 0, 7, 6, 8, 41];
455 $linkIds = array_merge($stickyIds, $standardIds);
457 foreach (self
::$privateLinkDB as $key => $value) {
458 $this->assertEquals($linkIds[$cpt++
], $key);
460 self
::$privateLinkDB->reorder('DESC');
461 $linkIds = array_merge(array_reverse($stickyIds), array_reverse($standardIds));
463 foreach (self
::$privateLinkDB as $key => $value) {
464 $this->assertEquals($linkIds[$cpt++
], $key);
469 * Test rename tag with a valid value present in multiple links
471 public function testRenameTagMultiple()
473 self
::$refDB->write(self
::$testDatastore);
474 $linkDB = new LinkDB(self
::$testDatastore, true, false);
476 $res = $linkDB->renameTag('cartoon', 'Taz');
477 $this->assertEquals(3, count($res));
478 $this->assertContains(' Taz ', $linkDB[4]['tags']);
479 $this->assertContains(' Taz ', $linkDB[1]['tags']);
480 $this->assertContains(' Taz ', $linkDB[0]['tags']);
484 * Test rename tag with a valid value
486 public function testRenameTagCaseSensitive()
488 self
::$refDB->write(self
::$testDatastore);
489 $linkDB = new LinkDB(self
::$testDatastore, true, false);
491 $res = $linkDB->renameTag('sTuff', 'Taz');
492 $this->assertEquals(1, count($res));
493 $this->assertEquals('Taz', $linkDB[41]['tags']);
497 * Test rename tag with invalid values
499 public function testRenameTagInvalid()
501 $linkDB = new LinkDB(self
::$testDatastore, false, false);
503 $this->assertFalse($linkDB->renameTag('', 'test'));
504 $this->assertFalse($linkDB->renameTag('', ''));
506 $this->assertEquals([], $linkDB->renameTag('test', ''));
507 $this->assertEquals([], $linkDB->renameTag('test', 'retest'));
511 * Test delete tag with a valid value
513 public function testDeleteTag()
515 self
::$refDB->write(self
::$testDatastore);
516 $linkDB = new LinkDB(self
::$testDatastore, true, false);
518 $res = $linkDB->renameTag('cartoon', null);
519 $this->assertEquals(3, count($res));
520 $this->assertNotContains('cartoon', $linkDB[4]['tags']);
524 * Test linksCountPerTag all tags without filter.
525 * Equal occurrences should be sorted alphabetically.
527 public function testCountLinkPerTagAllNoFilter()
553 $tags = self
::$privateLinkDB->linksCountPerTag();
555 $this->assertEquals($expected, $tags, var_export($tags, true));
559 * Test linksCountPerTag all tags with filter.
560 * Equal occurrences should be sorted alphabetically.
562 public function testCountLinkPerTagAllWithFilter()
576 $tags = self
::$privateLinkDB->linksCountPerTag(['gnu']);
578 $this->assertEquals($expected, $tags, var_export($tags, true));
582 * Test linksCountPerTag public tags with filter.
583 * Equal occurrences should be sorted alphabetically.
585 public function testCountLinkPerTagPublicWithFilter()
599 $tags = self
::$privateLinkDB->linksCountPerTag(['gnu'], 'public');
601 $this->assertEquals($expected, $tags, var_export($tags, true));
605 * Test linksCountPerTag public tags with filter.
606 * Equal occurrences should be sorted alphabetically.
608 public function testCountLinkPerTagPrivateWithFilter()
618 $tags = self
::$privateLinkDB->linksCountPerTag(['dev'], 'private');
620 $this->assertEquals($expected, $tags, var_export($tags, true));