]>
Commit | Line | Data |
---|---|---|
e26e2060 A |
1 | <?php |
2 | /** | |
3 | * Link datastore tests | |
4 | */ | |
5 | ||
6 | namespace Shaarli\Bookmark; | |
7 | ||
8 | use DateTime; | |
9 | use PHPUnit\Framework\TestCase; | |
10 | use ReferenceLinkDB; | |
11 | use ReflectionClass; | |
12 | use Shaarli; | |
13 | use Shaarli\Bookmark\Exception\BookmarkNotFoundException; | |
14 | use Shaarli\Config\ConfigManager; | |
a39acb25 | 15 | use Shaarli\Formatter\BookmarkMarkdownFormatter; |
e26e2060 A |
16 | use Shaarli\History; |
17 | ||
18 | /** | |
19 | * Unitary tests for LegacyLinkDBTest | |
20 | */ | |
21 | class BookmarkFileServiceTest extends TestCase | |
22 | { | |
23 | // datastore to test write operations | |
24 | protected static $testDatastore = 'sandbox/datastore.php'; | |
25 | ||
26 | protected static $testConf = 'sandbox/config'; | |
27 | ||
28 | protected static $testUpdates = 'sandbox/updates.txt'; | |
29 | ||
30 | /** | |
31 | * @var ConfigManager instance. | |
32 | */ | |
33 | protected $conf; | |
34 | ||
35 | /** | |
36 | * @var History instance. | |
37 | */ | |
38 | protected $history; | |
39 | ||
40 | /** | |
41 | * @var ReferenceLinkDB instance. | |
42 | */ | |
43 | protected $refDB = null; | |
44 | ||
45 | /** | |
46 | * @var BookmarkFileService public LinkDB instance. | |
47 | */ | |
48 | protected $publicLinkDB = null; | |
49 | ||
50 | /** | |
51 | * @var BookmarkFileService private LinkDB instance. | |
52 | */ | |
53 | protected $privateLinkDB = null; | |
54 | ||
55 | /** | |
56 | * Instantiates public and private LinkDBs with test data | |
57 | * | |
58 | * The reference datastore contains public and private bookmarks that | |
59 | * will be used to test LinkDB's methods: | |
60 | * - access filtering (public/private), | |
61 | * - link searches: | |
62 | * - by day, | |
63 | * - by tag, | |
64 | * - by text, | |
65 | * - etc. | |
66 | * | |
67 | * Resets test data for each test | |
68 | */ | |
69 | protected function setUp() | |
70 | { | |
71 | if (file_exists(self::$testDatastore)) { | |
72 | unlink(self::$testDatastore); | |
73 | } | |
74 | ||
75 | if (file_exists(self::$testConf .'.json.php')) { | |
76 | unlink(self::$testConf .'.json.php'); | |
77 | } | |
78 | ||
79 | if (file_exists(self::$testUpdates)) { | |
80 | unlink(self::$testUpdates); | |
81 | } | |
82 | ||
83 | copy('tests/utils/config/configJson.json.php', self::$testConf .'.json.php'); | |
84 | $this->conf = new ConfigManager(self::$testConf); | |
85 | $this->conf->set('resource.datastore', self::$testDatastore); | |
86 | $this->conf->set('resource.updates', self::$testUpdates); | |
87 | $this->refDB = new \ReferenceLinkDB(); | |
88 | $this->refDB->write(self::$testDatastore); | |
89 | $this->history = new History('sandbox/history.php'); | |
90 | $this->publicLinkDB = new BookmarkFileService($this->conf, $this->history, false); | |
91 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | |
92 | } | |
93 | ||
94 | /** | |
95 | * Test migrate() method with a legacy datastore. | |
96 | */ | |
97 | public function testDatabaseMigration() | |
98 | { | |
99 | if (!defined('SHAARLI_VERSION')) { | |
100 | define('SHAARLI_VERSION', 'dev'); | |
101 | } | |
102 | ||
103 | $this->refDB = new \ReferenceLinkDB(true); | |
104 | $this->refDB->write(self::$testDatastore); | |
105 | $db = self::getMethod('migrate'); | |
106 | $db->invokeArgs($this->privateLinkDB, []); | |
107 | ||
108 | $db = new \FakeBookmarkService($this->conf, $this->history, true); | |
109 | $this->assertInstanceOf(BookmarkArray::class, $db->getBookmarks()); | |
110 | $this->assertEquals($this->refDB->countLinks(), $db->count()); | |
111 | } | |
112 | ||
113 | /** | |
114 | * Test get() method for a defined and saved bookmark | |
115 | */ | |
116 | public function testGetDefinedSaved() | |
117 | { | |
118 | $bookmark = $this->privateLinkDB->get(42); | |
119 | $this->assertEquals(42, $bookmark->getId()); | |
120 | $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle()); | |
121 | } | |
122 | ||
123 | /** | |
124 | * Test get() method for a defined and not saved bookmark | |
125 | */ | |
126 | public function testGetDefinedNotSaved() | |
127 | { | |
128 | $bookmark = new Bookmark(); | |
129 | $this->privateLinkDB->add($bookmark); | |
130 | $createdBookmark = $this->privateLinkDB->get(43); | |
131 | $this->assertEquals(43, $createdBookmark->getId()); | |
132 | $this->assertEmpty($createdBookmark->getDescription()); | |
133 | } | |
134 | ||
135 | /** | |
136 | * Test get() method for an undefined bookmark | |
137 | * | |
138 | * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException | |
139 | */ | |
140 | public function testGetUndefined() | |
141 | { | |
142 | $this->privateLinkDB->get(666); | |
143 | } | |
144 | ||
145 | /** | |
146 | * Test add() method for a bookmark fully built | |
147 | */ | |
148 | public function testAddFull() | |
149 | { | |
150 | $bookmark = new Bookmark(); | |
151 | $bookmark->setUrl($url = 'https://domain.tld/index.php'); | |
152 | $bookmark->setShortUrl('abc'); | |
153 | $bookmark->setTitle($title = 'This a brand new bookmark'); | |
154 | $bookmark->setDescription($desc = 'It should be created and written'); | |
155 | $bookmark->setTags($tags = ['tag1', 'tagssss']); | |
156 | $bookmark->setThumbnail($thumb = 'http://thumb.tld/dle.png'); | |
157 | $bookmark->setPrivate(true); | |
158 | $bookmark->setSticky(true); | |
159 | $bookmark->setCreated($created = DateTime::createFromFormat('Ymd_His', '20190518_140354')); | |
160 | $bookmark->setUpdated($updated = DateTime::createFromFormat('Ymd_His', '20190518_150354')); | |
161 | ||
162 | $this->privateLinkDB->add($bookmark); | |
163 | $bookmark = $this->privateLinkDB->get(43); | |
164 | $this->assertEquals(43, $bookmark->getId()); | |
165 | $this->assertEquals($url, $bookmark->getUrl()); | |
166 | $this->assertEquals('abc', $bookmark->getShortUrl()); | |
167 | $this->assertEquals($title, $bookmark->getTitle()); | |
168 | $this->assertEquals($desc, $bookmark->getDescription()); | |
169 | $this->assertEquals($tags, $bookmark->getTags()); | |
170 | $this->assertEquals($thumb, $bookmark->getThumbnail()); | |
171 | $this->assertTrue($bookmark->isPrivate()); | |
172 | $this->assertTrue($bookmark->isSticky()); | |
173 | $this->assertEquals($created, $bookmark->getCreated()); | |
174 | $this->assertEquals($updated, $bookmark->getUpdated()); | |
175 | ||
176 | // reload from file | |
177 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | |
178 | ||
179 | $bookmark = $this->privateLinkDB->get(43); | |
180 | $this->assertEquals(43, $bookmark->getId()); | |
181 | $this->assertEquals($url, $bookmark->getUrl()); | |
182 | $this->assertEquals('abc', $bookmark->getShortUrl()); | |
183 | $this->assertEquals($title, $bookmark->getTitle()); | |
184 | $this->assertEquals($desc, $bookmark->getDescription()); | |
185 | $this->assertEquals($tags, $bookmark->getTags()); | |
186 | $this->assertEquals($thumb, $bookmark->getThumbnail()); | |
187 | $this->assertTrue($bookmark->isPrivate()); | |
188 | $this->assertTrue($bookmark->isSticky()); | |
189 | $this->assertEquals($created, $bookmark->getCreated()); | |
190 | $this->assertEquals($updated, $bookmark->getUpdated()); | |
191 | } | |
192 | ||
193 | /** | |
194 | * Test add() method for a bookmark without any field set | |
195 | */ | |
196 | public function testAddMinimal() | |
197 | { | |
198 | $bookmark = new Bookmark(); | |
199 | $this->privateLinkDB->add($bookmark); | |
200 | ||
201 | $bookmark = $this->privateLinkDB->get(43); | |
202 | $this->assertEquals(43, $bookmark->getId()); | |
301c7ab1 | 203 | $this->assertRegExp('#/shaare/[\w\-]{6}#', $bookmark->getUrl()); |
e26e2060 A |
204 | $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl()); |
205 | $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle()); | |
206 | $this->assertEmpty($bookmark->getDescription()); | |
207 | $this->assertEmpty($bookmark->getTags()); | |
208 | $this->assertEmpty($bookmark->getThumbnail()); | |
209 | $this->assertFalse($bookmark->isPrivate()); | |
210 | $this->assertFalse($bookmark->isSticky()); | |
211 | $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getCreated()); | |
212 | $this->assertNull($bookmark->getUpdated()); | |
213 | ||
214 | // reload from file | |
215 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | |
216 | ||
217 | $bookmark = $this->privateLinkDB->get(43); | |
218 | $this->assertEquals(43, $bookmark->getId()); | |
301c7ab1 | 219 | $this->assertRegExp('#/shaare/[\w\-]{6}#', $bookmark->getUrl()); |
e26e2060 A |
220 | $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl()); |
221 | $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle()); | |
222 | $this->assertEmpty($bookmark->getDescription()); | |
223 | $this->assertEmpty($bookmark->getTags()); | |
224 | $this->assertEmpty($bookmark->getThumbnail()); | |
225 | $this->assertFalse($bookmark->isPrivate()); | |
226 | $this->assertFalse($bookmark->isSticky()); | |
227 | $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getCreated()); | |
228 | $this->assertNull($bookmark->getUpdated()); | |
229 | } | |
230 | ||
231 | /** | |
232 | * Test add() method for a bookmark without any field set and without writing the data store | |
233 | * | |
234 | * @expectedExceptionMessage Shaarli\Bookmark\Exception\BookmarkNotFoundException | |
235 | */ | |
236 | public function testAddMinimalNoWrite() | |
237 | { | |
238 | $bookmark = new Bookmark(); | |
239 | $this->privateLinkDB->add($bookmark); | |
240 | ||
241 | $bookmark = $this->privateLinkDB->get(43); | |
242 | $this->assertEquals(43, $bookmark->getId()); | |
243 | ||
244 | // reload from file | |
245 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | |
246 | ||
247 | $this->privateLinkDB->get(43); | |
248 | } | |
249 | ||
250 | /** | |
251 | * Test add() method while logged out | |
252 | * | |
253 | * @expectedException \Exception | |
254 | * @expectedExceptionMessage You're not authorized to alter the datastore | |
255 | */ | |
256 | public function testAddLoggedOut() | |
257 | { | |
258 | $this->publicLinkDB->add(new Bookmark()); | |
259 | } | |
260 | ||
261 | /** | |
262 | * Test add() method with an entry which is not a bookmark instance | |
263 | * | |
264 | * @expectedException \Exception | |
265 | * @expectedExceptionMessage Provided data is invalid | |
266 | */ | |
267 | public function testAddNotABookmark() | |
268 | { | |
269 | $this->privateLinkDB->add(['title' => 'hi!']); | |
270 | } | |
271 | ||
272 | /** | |
273 | * Test add() method with a Bookmark already containing an ID | |
274 | * | |
275 | * @expectedException \Exception | |
276 | * @expectedExceptionMessage This bookmarks already exists | |
277 | */ | |
278 | public function testAddWithId() | |
279 | { | |
280 | $bookmark = new Bookmark(); | |
281 | $bookmark->setId(43); | |
282 | $this->privateLinkDB->add($bookmark); | |
283 | } | |
284 | ||
285 | /** | |
286 | * Test set() method for a bookmark fully built | |
287 | */ | |
288 | public function testSetFull() | |
289 | { | |
290 | $bookmark = $this->privateLinkDB->get(42); | |
291 | $bookmark->setUrl($url = 'https://domain.tld/index.php'); | |
292 | $bookmark->setShortUrl('abc'); | |
293 | $bookmark->setTitle($title = 'This a brand new bookmark'); | |
294 | $bookmark->setDescription($desc = 'It should be created and written'); | |
295 | $bookmark->setTags($tags = ['tag1', 'tagssss']); | |
296 | $bookmark->setThumbnail($thumb = 'http://thumb.tld/dle.png'); | |
297 | $bookmark->setPrivate(true); | |
298 | $bookmark->setSticky(true); | |
299 | $bookmark->setCreated($created = DateTime::createFromFormat('Ymd_His', '20190518_140354')); | |
300 | $bookmark->setUpdated($updated = DateTime::createFromFormat('Ymd_His', '20190518_150354')); | |
301 | ||
302 | $this->privateLinkDB->set($bookmark); | |
303 | $bookmark = $this->privateLinkDB->get(42); | |
304 | $this->assertEquals(42, $bookmark->getId()); | |
305 | $this->assertEquals($url, $bookmark->getUrl()); | |
306 | $this->assertEquals('abc', $bookmark->getShortUrl()); | |
307 | $this->assertEquals($title, $bookmark->getTitle()); | |
308 | $this->assertEquals($desc, $bookmark->getDescription()); | |
309 | $this->assertEquals($tags, $bookmark->getTags()); | |
310 | $this->assertEquals($thumb, $bookmark->getThumbnail()); | |
311 | $this->assertTrue($bookmark->isPrivate()); | |
312 | $this->assertTrue($bookmark->isSticky()); | |
313 | $this->assertEquals($created, $bookmark->getCreated()); | |
314 | $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated()); | |
315 | ||
316 | // reload from file | |
317 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | |
318 | ||
319 | $bookmark = $this->privateLinkDB->get(42); | |
320 | $this->assertEquals(42, $bookmark->getId()); | |
321 | $this->assertEquals($url, $bookmark->getUrl()); | |
322 | $this->assertEquals('abc', $bookmark->getShortUrl()); | |
323 | $this->assertEquals($title, $bookmark->getTitle()); | |
324 | $this->assertEquals($desc, $bookmark->getDescription()); | |
325 | $this->assertEquals($tags, $bookmark->getTags()); | |
326 | $this->assertEquals($thumb, $bookmark->getThumbnail()); | |
327 | $this->assertTrue($bookmark->isPrivate()); | |
328 | $this->assertTrue($bookmark->isSticky()); | |
329 | $this->assertEquals($created, $bookmark->getCreated()); | |
330 | $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated()); | |
331 | } | |
332 | ||
333 | /** | |
334 | * Test set() method for a bookmark without any field set | |
335 | */ | |
336 | public function testSetMinimal() | |
337 | { | |
338 | $bookmark = $this->privateLinkDB->get(42); | |
339 | $this->privateLinkDB->set($bookmark); | |
340 | ||
341 | $bookmark = $this->privateLinkDB->get(42); | |
342 | $this->assertEquals(42, $bookmark->getId()); | |
f7f08cee | 343 | $this->assertEquals('/shaare/WDWyig', $bookmark->getUrl()); |
e26e2060 A |
344 | $this->assertEquals('1eYJ1Q', $bookmark->getShortUrl()); |
345 | $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle()); | |
346 | $this->assertEquals('Used to test bookmarks reordering.', $bookmark->getDescription()); | |
347 | $this->assertEquals(['ut'], $bookmark->getTags()); | |
348 | $this->assertFalse($bookmark->getThumbnail()); | |
349 | $this->assertFalse($bookmark->isPrivate()); | |
350 | $this->assertFalse($bookmark->isSticky()); | |
351 | $this->assertEquals( | |
352 | DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20100310_101010'), | |
353 | $bookmark->getCreated() | |
354 | ); | |
355 | $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated()); | |
356 | ||
357 | // reload from file | |
358 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | |
359 | ||
360 | $bookmark = $this->privateLinkDB->get(42); | |
361 | $this->assertEquals(42, $bookmark->getId()); | |
f7f08cee | 362 | $this->assertEquals('/shaare/WDWyig', $bookmark->getUrl()); |
e26e2060 A |
363 | $this->assertEquals('1eYJ1Q', $bookmark->getShortUrl()); |
364 | $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle()); | |
365 | $this->assertEquals('Used to test bookmarks reordering.', $bookmark->getDescription()); | |
366 | $this->assertEquals(['ut'], $bookmark->getTags()); | |
367 | $this->assertFalse($bookmark->getThumbnail()); | |
368 | $this->assertFalse($bookmark->isPrivate()); | |
369 | $this->assertFalse($bookmark->isSticky()); | |
370 | $this->assertEquals( | |
371 | DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20100310_101010'), | |
372 | $bookmark->getCreated() | |
373 | ); | |
374 | $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated()); | |
375 | } | |
376 | ||
377 | /** | |
378 | * Test set() method for a bookmark without any field set and without writing the data store | |
379 | */ | |
380 | public function testSetMinimalNoWrite() | |
381 | { | |
382 | $bookmark = $this->privateLinkDB->get(42); | |
383 | $bookmark->setTitle($title = 'hi!'); | |
384 | $this->privateLinkDB->set($bookmark, false); | |
385 | ||
386 | $bookmark = $this->privateLinkDB->get(42); | |
387 | $this->assertEquals(42, $bookmark->getId()); | |
388 | $this->assertEquals($title, $bookmark->getTitle()); | |
389 | ||
390 | // reload from file | |
391 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | |
392 | ||
393 | $bookmark = $this->privateLinkDB->get(42); | |
394 | $this->assertEquals(42, $bookmark->getId()); | |
395 | $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle()); | |
396 | } | |
397 | ||
398 | /** | |
399 | * Test set() method while logged out | |
400 | * | |
401 | * @expectedException \Exception | |
402 | * @expectedExceptionMessage You're not authorized to alter the datastore | |
403 | */ | |
404 | public function testSetLoggedOut() | |
405 | { | |
406 | $this->publicLinkDB->set(new Bookmark()); | |
407 | } | |
408 | ||
409 | /** | |
410 | * Test set() method with an entry which is not a bookmark instance | |
411 | * | |
412 | * @expectedException \Exception | |
413 | * @expectedExceptionMessage Provided data is invalid | |
414 | */ | |
415 | public function testSetNotABookmark() | |
416 | { | |
417 | $this->privateLinkDB->set(['title' => 'hi!']); | |
418 | } | |
419 | ||
420 | /** | |
421 | * Test set() method with a Bookmark without an ID defined. | |
422 | * | |
423 | * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException | |
424 | */ | |
425 | public function testSetWithoutId() | |
426 | { | |
427 | $bookmark = new Bookmark(); | |
428 | $this->privateLinkDB->set($bookmark); | |
429 | } | |
430 | ||
431 | /** | |
432 | * Test set() method with a Bookmark with an unknow ID | |
433 | * | |
434 | * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException | |
435 | */ | |
436 | public function testSetWithUnknownId() | |
437 | { | |
438 | $bookmark = new Bookmark(); | |
439 | $bookmark->setId(666); | |
440 | $this->privateLinkDB->set($bookmark); | |
441 | } | |
442 | ||
443 | /** | |
444 | * Test addOrSet() method with a new ID | |
445 | */ | |
446 | public function testAddOrSetNew() | |
447 | { | |
448 | $bookmark = new Bookmark(); | |
449 | $this->privateLinkDB->addOrSet($bookmark); | |
450 | ||
451 | $bookmark = $this->privateLinkDB->get(43); | |
452 | $this->assertEquals(43, $bookmark->getId()); | |
453 | ||
454 | // reload from file | |
455 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | |
456 | ||
457 | $bookmark = $this->privateLinkDB->get(43); | |
458 | $this->assertEquals(43, $bookmark->getId()); | |
459 | } | |
460 | ||
461 | /** | |
462 | * Test addOrSet() method with an existing ID | |
463 | */ | |
464 | public function testAddOrSetExisting() | |
465 | { | |
466 | $bookmark = $this->privateLinkDB->get(42); | |
467 | $bookmark->setTitle($title = 'hi!'); | |
468 | $this->privateLinkDB->addOrSet($bookmark); | |
469 | ||
470 | $bookmark = $this->privateLinkDB->get(42); | |
471 | $this->assertEquals(42, $bookmark->getId()); | |
472 | $this->assertEquals($title, $bookmark->getTitle()); | |
473 | ||
474 | // reload from file | |
475 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | |
476 | ||
477 | $bookmark = $this->privateLinkDB->get(42); | |
478 | $this->assertEquals(42, $bookmark->getId()); | |
479 | $this->assertEquals($title, $bookmark->getTitle()); | |
480 | } | |
481 | ||
482 | /** | |
483 | * Test addOrSet() method while logged out | |
484 | * | |
485 | * @expectedException \Exception | |
486 | * @expectedExceptionMessage You're not authorized to alter the datastore | |
487 | */ | |
488 | public function testAddOrSetLoggedOut() | |
489 | { | |
490 | $this->publicLinkDB->addOrSet(new Bookmark()); | |
491 | } | |
492 | ||
493 | /** | |
494 | * Test addOrSet() method with an entry which is not a bookmark instance | |
495 | * | |
496 | * @expectedException \Exception | |
497 | * @expectedExceptionMessage Provided data is invalid | |
498 | */ | |
499 | public function testAddOrSetNotABookmark() | |
500 | { | |
501 | $this->privateLinkDB->addOrSet(['title' => 'hi!']); | |
502 | } | |
503 | ||
504 | /** | |
505 | * Test addOrSet() method for a bookmark without any field set and without writing the data store | |
506 | */ | |
507 | public function testAddOrSetMinimalNoWrite() | |
508 | { | |
509 | $bookmark = $this->privateLinkDB->get(42); | |
510 | $bookmark->setTitle($title = 'hi!'); | |
511 | $this->privateLinkDB->addOrSet($bookmark, false); | |
512 | ||
513 | $bookmark = $this->privateLinkDB->get(42); | |
514 | $this->assertEquals(42, $bookmark->getId()); | |
515 | $this->assertEquals($title, $bookmark->getTitle()); | |
516 | ||
517 | // reload from file | |
518 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | |
519 | ||
520 | $bookmark = $this->privateLinkDB->get(42); | |
521 | $this->assertEquals(42, $bookmark->getId()); | |
522 | $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle()); | |
523 | } | |
524 | ||
525 | /** | |
526 | * Test remove() method with an existing Bookmark | |
527 | * | |
528 | * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException | |
529 | */ | |
530 | public function testRemoveExisting() | |
531 | { | |
532 | $bookmark = $this->privateLinkDB->get(42); | |
533 | $this->privateLinkDB->remove($bookmark); | |
534 | ||
535 | $exception = null; | |
536 | try { | |
537 | $this->privateLinkDB->get(42); | |
538 | } catch (BookmarkNotFoundException $e) { | |
539 | $exception = $e; | |
540 | } | |
541 | $this->assertInstanceOf(BookmarkNotFoundException::class, $exception); | |
542 | ||
543 | // reload from file | |
544 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | |
545 | ||
546 | $this->privateLinkDB->get(42); | |
547 | } | |
548 | ||
549 | /** | |
550 | * Test remove() method while logged out | |
551 | * | |
552 | * @expectedException \Exception | |
553 | * @expectedExceptionMessage You're not authorized to alter the datastore | |
554 | */ | |
555 | public function testRemoveLoggedOut() | |
556 | { | |
557 | $bookmark = $this->privateLinkDB->get(42); | |
558 | $this->publicLinkDB->remove($bookmark); | |
559 | } | |
560 | ||
561 | /** | |
562 | * Test remove() method with an entry which is not a bookmark instance | |
563 | * | |
564 | * @expectedException \Exception | |
565 | * @expectedExceptionMessage Provided data is invalid | |
566 | */ | |
567 | public function testRemoveNotABookmark() | |
568 | { | |
569 | $this->privateLinkDB->remove(['title' => 'hi!']); | |
570 | } | |
571 | ||
572 | /** | |
573 | * Test remove() method with a Bookmark with an unknown ID | |
574 | * | |
575 | * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException | |
576 | */ | |
577 | public function testRemoveWithUnknownId() | |
578 | { | |
579 | $bookmark = new Bookmark(); | |
580 | $bookmark->setId(666); | |
581 | $this->privateLinkDB->remove($bookmark); | |
582 | } | |
583 | ||
584 | /** | |
585 | * Test exists() method | |
586 | */ | |
587 | public function testExists() | |
588 | { | |
589 | $this->assertTrue($this->privateLinkDB->exists(42)); // public | |
590 | $this->assertTrue($this->privateLinkDB->exists(6)); // private | |
591 | ||
592 | $this->assertTrue($this->privateLinkDB->exists(42, BookmarkFilter::$ALL)); | |
593 | $this->assertTrue($this->privateLinkDB->exists(6, BookmarkFilter::$ALL)); | |
594 | ||
595 | $this->assertTrue($this->privateLinkDB->exists(42, BookmarkFilter::$PUBLIC)); | |
596 | $this->assertFalse($this->privateLinkDB->exists(6, BookmarkFilter::$PUBLIC)); | |
597 | ||
598 | $this->assertFalse($this->privateLinkDB->exists(42, BookmarkFilter::$PRIVATE)); | |
599 | $this->assertTrue($this->privateLinkDB->exists(6, BookmarkFilter::$PRIVATE)); | |
600 | ||
601 | $this->assertTrue($this->publicLinkDB->exists(42)); | |
602 | $this->assertFalse($this->publicLinkDB->exists(6)); | |
603 | ||
604 | $this->assertTrue($this->publicLinkDB->exists(42, BookmarkFilter::$PUBLIC)); | |
605 | $this->assertFalse($this->publicLinkDB->exists(6, BookmarkFilter::$PUBLIC)); | |
606 | ||
607 | $this->assertFalse($this->publicLinkDB->exists(42, BookmarkFilter::$PRIVATE)); | |
608 | $this->assertTrue($this->publicLinkDB->exists(6, BookmarkFilter::$PRIVATE)); | |
609 | } | |
610 | ||
611 | /** | |
612 | * Test initialize() method | |
613 | */ | |
614 | public function testInitialize() | |
615 | { | |
616 | $dbSize = $this->privateLinkDB->count(); | |
617 | $this->privateLinkDB->initialize(); | |
618 | $this->assertEquals($dbSize + 2, $this->privateLinkDB->count()); | |
619 | $this->assertEquals( | |
620 | 'My secret stuff... - Pastebin.com', | |
621 | $this->privateLinkDB->get(43)->getTitle() | |
622 | ); | |
623 | $this->assertEquals( | |
624 | 'The personal, minimalist, super-fast, database free, bookmarking service', | |
625 | $this->privateLinkDB->get(44)->getTitle() | |
626 | ); | |
627 | } | |
628 | ||
629 | /* | |
630 | * The following tests have been taken from the legacy LinkDB test and adapted | |
631 | * to make sure that nothing have been broken in the migration process. | |
632 | * They mostly cover search/filters. Some of them might be redundant with the previous ones. | |
633 | */ | |
634 | ||
635 | /** | |
636 | * Attempt to instantiate a LinkDB whereas the datastore is not writable | |
637 | * | |
638 | * @expectedException Shaarli\Bookmark\Exception\NotWritableDataStoreException | |
639 | * @expectedExceptionMessageRegExp #Couldn't load data from the data store file "null".*# | |
640 | */ | |
641 | public function testConstructDatastoreNotWriteable() | |
642 | { | |
643 | $conf = new ConfigManager('tests/utils/config/configJson'); | |
644 | $conf->set('resource.datastore', 'null/store.db'); | |
645 | new BookmarkFileService($conf, $this->history, true); | |
646 | } | |
647 | ||
648 | /** | |
649 | * The DB doesn't exist, ensure it is created with an empty datastore | |
650 | */ | |
651 | public function testCheckDBNewLoggedIn() | |
652 | { | |
653 | unlink(self::$testDatastore); | |
654 | $this->assertFileNotExists(self::$testDatastore); | |
655 | new BookmarkFileService($this->conf, $this->history, true); | |
656 | $this->assertFileExists(self::$testDatastore); | |
657 | ||
658 | // ensure the correct data has been written | |
659 | $this->assertGreaterThan(0, filesize(self::$testDatastore)); | |
660 | } | |
661 | ||
662 | /** | |
663 | * The DB doesn't exist, but not logged in, ensure it initialized, but the file is not written | |
664 | */ | |
665 | public function testCheckDBNewLoggedOut() | |
666 | { | |
667 | unlink(self::$testDatastore); | |
668 | $this->assertFileNotExists(self::$testDatastore); | |
669 | $db = new \FakeBookmarkService($this->conf, $this->history, false); | |
670 | $this->assertFileNotExists(self::$testDatastore); | |
671 | $this->assertInstanceOf(BookmarkArray::class, $db->getBookmarks()); | |
672 | $this->assertCount(0, $db->getBookmarks()); | |
673 | } | |
674 | ||
675 | /** | |
676 | * Load public bookmarks from the DB | |
677 | */ | |
678 | public function testReadPublicDB() | |
679 | { | |
680 | $this->assertEquals( | |
681 | $this->refDB->countPublicLinks(), | |
682 | $this->publicLinkDB->count() | |
683 | ); | |
684 | } | |
685 | ||
686 | /** | |
687 | * Load public and private bookmarks from the DB | |
688 | */ | |
689 | public function testReadPrivateDB() | |
690 | { | |
691 | $this->assertEquals( | |
692 | $this->refDB->countLinks(), | |
693 | $this->privateLinkDB->count() | |
694 | ); | |
695 | } | |
696 | ||
697 | /** | |
698 | * Save the bookmarks to the DB | |
699 | */ | |
700 | public function testSave() | |
701 | { | |
702 | $testDB = new BookmarkFileService($this->conf, $this->history, true); | |
703 | $dbSize = $testDB->count(); | |
704 | ||
705 | $bookmark = new Bookmark(); | |
706 | $testDB->add($bookmark); | |
707 | ||
708 | $testDB = new BookmarkFileService($this->conf, $this->history, true); | |
709 | $this->assertEquals($dbSize + 1, $testDB->count()); | |
710 | } | |
711 | ||
712 | /** | |
713 | * Count existing bookmarks - public bookmarks hidden | |
714 | */ | |
715 | public function testCountHiddenPublic() | |
716 | { | |
717 | $this->conf->set('privacy.hide_public_links', true); | |
718 | $linkDB = new BookmarkFileService($this->conf, $this->history, false); | |
719 | ||
720 | $this->assertEquals(0, $linkDB->count()); | |
721 | } | |
722 | ||
723 | /** | |
724 | * List the days for which bookmarks have been posted | |
725 | */ | |
726 | public function testDays() | |
727 | { | |
728 | $this->assertEquals( | |
729 | ['20100309', '20100310', '20121206', '20121207', '20130614', '20150310'], | |
730 | $this->publicLinkDB->days() | |
731 | ); | |
732 | ||
733 | $this->assertEquals( | |
734 | ['20100309', '20100310', '20121206', '20121207', '20130614', '20141125', '20150310'], | |
735 | $this->privateLinkDB->days() | |
736 | ); | |
737 | } | |
738 | ||
739 | /** | |
740 | * The URL corresponds to an existing entry in the DB | |
741 | */ | |
742 | public function testGetKnownLinkFromURL() | |
743 | { | |
744 | $link = $this->publicLinkDB->findByUrl('http://mediagoblin.org/'); | |
745 | ||
746 | $this->assertNotEquals(false, $link); | |
747 | $this->assertContains( | |
748 | 'A free software media publishing platform', | |
749 | $link->getDescription() | |
750 | ); | |
751 | } | |
752 | ||
753 | /** | |
754 | * The URL is not in the DB | |
755 | */ | |
756 | public function testGetUnknownLinkFromURL() | |
757 | { | |
758 | $this->assertEquals( | |
759 | false, | |
760 | $this->publicLinkDB->findByUrl('http://dev.null') | |
761 | ); | |
762 | } | |
763 | ||
764 | /** | |
765 | * Lists all tags | |
766 | */ | |
767 | public function testAllTags() | |
768 | { | |
769 | $this->assertEquals( | |
770 | [ | |
771 | 'web' => 3, | |
772 | 'cartoon' => 2, | |
773 | 'gnu' => 2, | |
774 | 'dev' => 1, | |
775 | 'samba' => 1, | |
776 | 'media' => 1, | |
777 | 'software' => 1, | |
778 | 'stallman' => 1, | |
779 | 'free' => 1, | |
780 | '-exclude' => 1, | |
781 | 'hashtag' => 2, | |
782 | // The DB contains a link with `sTuff` and another one with `stuff` tag. | |
783 | // They need to be grouped with the first case found - order by date DESC: `sTuff`. | |
784 | 'sTuff' => 2, | |
785 | 'ut' => 1, | |
786 | ], | |
787 | $this->publicLinkDB->bookmarksCountPerTag() | |
788 | ); | |
789 | ||
790 | $this->assertEquals( | |
791 | [ | |
792 | 'web' => 4, | |
793 | 'cartoon' => 3, | |
794 | 'gnu' => 2, | |
795 | 'dev' => 2, | |
796 | 'samba' => 1, | |
797 | 'media' => 1, | |
798 | 'software' => 1, | |
799 | 'stallman' => 1, | |
800 | 'free' => 1, | |
801 | 'html' => 1, | |
802 | 'w3c' => 1, | |
803 | 'css' => 1, | |
804 | 'Mercurial' => 1, | |
805 | 'sTuff' => 2, | |
806 | '-exclude' => 1, | |
807 | '.hidden' => 1, | |
808 | 'hashtag' => 2, | |
809 | 'tag1' => 1, | |
810 | 'tag2' => 1, | |
811 | 'tag3' => 1, | |
812 | 'tag4' => 1, | |
813 | 'ut' => 1, | |
814 | ], | |
815 | $this->privateLinkDB->bookmarksCountPerTag() | |
816 | ); | |
817 | $this->assertEquals( | |
818 | [ | |
e26e2060 A |
819 | 'cartoon' => 2, |
820 | 'gnu' => 1, | |
821 | 'dev' => 1, | |
822 | 'samba' => 1, | |
823 | 'media' => 1, | |
824 | 'html' => 1, | |
825 | 'w3c' => 1, | |
826 | 'css' => 1, | |
827 | 'Mercurial' => 1, | |
828 | '.hidden' => 1, | |
829 | 'hashtag' => 1, | |
830 | ], | |
831 | $this->privateLinkDB->bookmarksCountPerTag(['web']) | |
832 | ); | |
833 | $this->assertEquals( | |
834 | [ | |
e26e2060 A |
835 | 'html' => 1, |
836 | 'w3c' => 1, | |
837 | 'css' => 1, | |
838 | 'Mercurial' => 1, | |
839 | ], | |
840 | $this->privateLinkDB->bookmarksCountPerTag(['web'], 'private') | |
841 | ); | |
842 | } | |
843 | ||
844 | /** | |
845 | * Test filter with string. | |
846 | */ | |
847 | public function testFilterString() | |
848 | { | |
849 | $tags = 'dev cartoon'; | |
850 | $request = ['searchtags' => $tags]; | |
851 | $this->assertEquals( | |
852 | 2, | |
853 | count($this->privateLinkDB->search($request, null, true)) | |
854 | ); | |
855 | } | |
856 | ||
857 | /** | |
858 | * Test filter with array. | |
859 | */ | |
860 | public function testFilterArray() | |
861 | { | |
862 | $tags = ['dev', 'cartoon']; | |
863 | $request = ['searchtags' => $tags]; | |
864 | $this->assertEquals( | |
865 | 2, | |
866 | count($this->privateLinkDB->search($request, null, true)) | |
867 | ); | |
868 | } | |
869 | ||
870 | /** | |
871 | * Test hidden tags feature: | |
872 | * tags starting with a dot '.' are only visible when logged in. | |
873 | */ | |
874 | public function testHiddenTags() | |
875 | { | |
876 | $tags = '.hidden'; | |
877 | $request = ['searchtags' => $tags]; | |
878 | $this->assertEquals( | |
879 | 1, | |
880 | count($this->privateLinkDB->search($request, 'all', true)) | |
881 | ); | |
882 | ||
883 | $this->assertEquals( | |
884 | 0, | |
885 | count($this->publicLinkDB->search($request, 'public', true)) | |
886 | ); | |
887 | } | |
888 | ||
889 | /** | |
890 | * Test filterHash() with a valid smallhash. | |
891 | */ | |
892 | public function testFilterHashValid() | |
893 | { | |
894 | $request = smallHash('20150310_114651'); | |
1a8ac737 A |
895 | $this->assertSame( |
896 | $request, | |
897 | $this->publicLinkDB->findByHash($request)->getShortUrl() | |
e26e2060 A |
898 | ); |
899 | $request = smallHash('20150310_114633' . 8); | |
1a8ac737 A |
900 | $this->assertSame( |
901 | $request, | |
902 | $this->publicLinkDB->findByHash($request)->getShortUrl() | |
e26e2060 A |
903 | ); |
904 | } | |
905 | ||
906 | /** | |
907 | * Test filterHash() with an invalid smallhash. | |
e26e2060 A |
908 | */ |
909 | public function testFilterHashInValid1() | |
910 | { | |
1a8ac737 A |
911 | $this->expectException(BookmarkNotFoundException::class); |
912 | ||
e26e2060 A |
913 | $request = 'blabla'; |
914 | $this->publicLinkDB->findByHash($request); | |
915 | } | |
916 | ||
917 | /** | |
918 | * Test filterHash() with an empty smallhash. | |
e26e2060 A |
919 | */ |
920 | public function testFilterHashInValid() | |
921 | { | |
1a8ac737 A |
922 | $this->expectException(BookmarkNotFoundException::class); |
923 | ||
e26e2060 A |
924 | $this->publicLinkDB->findByHash(''); |
925 | } | |
926 | ||
927 | /** | |
928 | * Test linksCountPerTag all tags without filter. | |
929 | * Equal occurrences should be sorted alphabetically. | |
930 | */ | |
931 | public function testCountLinkPerTagAllNoFilter() | |
932 | { | |
933 | $expected = [ | |
934 | 'web' => 4, | |
935 | 'cartoon' => 3, | |
936 | 'dev' => 2, | |
937 | 'gnu' => 2, | |
938 | 'hashtag' => 2, | |
939 | 'sTuff' => 2, | |
940 | '-exclude' => 1, | |
941 | '.hidden' => 1, | |
942 | 'Mercurial' => 1, | |
943 | 'css' => 1, | |
944 | 'free' => 1, | |
945 | 'html' => 1, | |
946 | 'media' => 1, | |
947 | 'samba' => 1, | |
948 | 'software' => 1, | |
949 | 'stallman' => 1, | |
950 | 'tag1' => 1, | |
951 | 'tag2' => 1, | |
952 | 'tag3' => 1, | |
953 | 'tag4' => 1, | |
954 | 'ut' => 1, | |
955 | 'w3c' => 1, | |
956 | ]; | |
957 | $tags = $this->privateLinkDB->bookmarksCountPerTag(); | |
958 | ||
959 | $this->assertEquals($expected, $tags, var_export($tags, true)); | |
960 | } | |
961 | ||
962 | /** | |
963 | * Test linksCountPerTag all tags with filter. | |
964 | * Equal occurrences should be sorted alphabetically. | |
965 | */ | |
966 | public function testCountLinkPerTagAllWithFilter() | |
967 | { | |
968 | $expected = [ | |
e26e2060 A |
969 | 'hashtag' => 2, |
970 | '-exclude' => 1, | |
971 | '.hidden' => 1, | |
972 | 'free' => 1, | |
973 | 'media' => 1, | |
974 | 'software' => 1, | |
975 | 'stallman' => 1, | |
976 | 'stuff' => 1, | |
977 | 'web' => 1, | |
978 | ]; | |
979 | $tags = $this->privateLinkDB->bookmarksCountPerTag(['gnu']); | |
980 | ||
981 | $this->assertEquals($expected, $tags, var_export($tags, true)); | |
982 | } | |
983 | ||
984 | /** | |
985 | * Test linksCountPerTag public tags with filter. | |
986 | * Equal occurrences should be sorted alphabetically. | |
987 | */ | |
988 | public function testCountLinkPerTagPublicWithFilter() | |
989 | { | |
990 | $expected = [ | |
e26e2060 A |
991 | 'hashtag' => 2, |
992 | '-exclude' => 1, | |
993 | '.hidden' => 1, | |
994 | 'free' => 1, | |
995 | 'media' => 1, | |
996 | 'software' => 1, | |
997 | 'stallman' => 1, | |
998 | 'stuff' => 1, | |
999 | 'web' => 1, | |
1000 | ]; | |
1001 | $tags = $this->privateLinkDB->bookmarksCountPerTag(['gnu'], 'public'); | |
1002 | ||
1003 | $this->assertEquals($expected, $tags, var_export($tags, true)); | |
1004 | } | |
1005 | ||
1006 | /** | |
1007 | * Test linksCountPerTag public tags with filter. | |
1008 | * Equal occurrences should be sorted alphabetically. | |
1009 | */ | |
1010 | public function testCountLinkPerTagPrivateWithFilter() | |
1011 | { | |
1012 | $expected = [ | |
1013 | 'cartoon' => 1, | |
e26e2060 A |
1014 | 'tag1' => 1, |
1015 | 'tag2' => 1, | |
1016 | 'tag3' => 1, | |
1017 | 'tag4' => 1, | |
1018 | ]; | |
1019 | $tags = $this->privateLinkDB->bookmarksCountPerTag(['dev'], 'private'); | |
1020 | ||
1021 | $this->assertEquals($expected, $tags, var_export($tags, true)); | |
1022 | } | |
1023 | ||
a39acb25 A |
1024 | /** |
1025 | * Test linksCountPerTag public tags with filter. | |
1026 | * Equal occurrences should be sorted alphabetically. | |
1027 | */ | |
1028 | public function testCountTagsNoMarkdown() | |
1029 | { | |
1030 | $expected = [ | |
1031 | 'cartoon' => 3, | |
1032 | 'dev' => 2, | |
1033 | 'tag1' => 1, | |
1034 | 'tag2' => 1, | |
1035 | 'tag3' => 1, | |
1036 | 'tag4' => 1, | |
1037 | 'web' => 4, | |
1038 | 'gnu' => 2, | |
1039 | 'hashtag' => 2, | |
1040 | 'sTuff' => 2, | |
1041 | '-exclude' => 1, | |
1042 | '.hidden' => 1, | |
1043 | 'Mercurial' => 1, | |
1044 | 'css' => 1, | |
1045 | 'free' => 1, | |
1046 | 'html' => 1, | |
1047 | 'media' => 1, | |
1048 | 'newTagToCount' => 1, | |
1049 | 'samba' => 1, | |
1050 | 'software' => 1, | |
1051 | 'stallman' => 1, | |
1052 | 'ut' => 1, | |
1053 | 'w3c' => 1, | |
1054 | ]; | |
1055 | $bookmark = new Bookmark(); | |
1056 | $bookmark->setTags(['newTagToCount', BookmarkMarkdownFormatter::NO_MD_TAG]); | |
1057 | $this->privateLinkDB->add($bookmark); | |
1058 | ||
1059 | $tags = $this->privateLinkDB->bookmarksCountPerTag(); | |
1060 | ||
1061 | $this->assertEquals($expected, $tags, var_export($tags, true)); | |
1062 | } | |
1063 | ||
e26e2060 A |
1064 | /** |
1065 | * Allows to test LinkDB's private methods | |
1066 | * | |
1067 | * @see | |
1068 | * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html | |
1069 | * http://stackoverflow.com/a/2798203 | |
1070 | */ | |
1071 | protected static function getMethod($name) | |
1072 | { | |
1073 | $class = new ReflectionClass('Shaarli\Bookmark\BookmarkFileService'); | |
1074 | $method = $class->getMethod($name); | |
1075 | $method->setAccessible(true); | |
1076 | return $method; | |
1077 | } | |
1078 | } |