]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/bookmark/BookmarkFilterTest.php
Compatibility with PHPUnit 9
[github/shaarli/Shaarli.git] / tests / bookmark / BookmarkFilterTest.php
CommitLineData
822bffce
A
1<?php
2
6696729b 3namespace Shaarli\Bookmark;
f24896b2 4
6696729b
V
5use Exception;
6use ReferenceLinkDB;
e26e2060 7use Shaarli\Config\ConfigManager;
e26e2060 8use Shaarli\History;
a5a9cf23 9use Shaarli\TestCase;
822bffce
A
10
11/**
e26e2060 12 * Class BookmarkFilterTest.
822bffce 13 */
e26e2060 14class BookmarkFilterTest extends TestCase
822bffce 15{
9ec0a611
A
16 /**
17 * @var string Test datastore path.
18 */
19 protected static $testDatastore = 'sandbox/datastore.php';
822bffce 20 /**
e26e2060 21 * @var BookmarkFilter instance.
822bffce
A
22 */
23 protected static $linkFilter;
24
7f96d9ec
A
25 /**
26 * @var ReferenceLinkDB instance
27 */
28 protected static $refDB;
29
9ec0a611 30 /**
e26e2060 31 * @var BookmarkFileService instance
9ec0a611 32 */
e26e2060 33 protected static $bookmarkService;
9ec0a611 34
822bffce 35 /**
6696729b 36 * Instantiate linkFilter with ReferenceLinkDB data.
822bffce 37 */
27ddfec3 38 public static function setUpBeforeClass(): void
822bffce 39 {
e26e2060
A
40 $conf = new ConfigManager('tests/utils/config/configJson');
41 $conf->set('resource.datastore', self::$testDatastore);
42 self::$refDB = new \ReferenceLinkDB();
9ec0a611 43 self::$refDB->write(self::$testDatastore);
e26e2060
A
44 $history = new History('sandbox/history.php');
45 self::$bookmarkService = new \FakeBookmarkService($conf, $history, true);
46 self::$linkFilter = new BookmarkFilter(self::$bookmarkService->getBookmarks());
822bffce
A
47 }
48
49 /**
50 * Blank filter.
51 */
52 public function testFilter()
53 {
54 $this->assertEquals(
7f96d9ec 55 self::$refDB->countLinks(),
822bffce
A
56 count(self::$linkFilter->filter('', ''))
57 );
58
7f96d9ec
A
59 $this->assertEquals(
60 self::$refDB->countLinks(),
61 count(self::$linkFilter->filter('', '', 'all'))
62 );
63
64 $this->assertEquals(
65 self::$refDB->countLinks(),
66 count(self::$linkFilter->filter('', '', 'randomstr'))
67 );
68
822bffce
A
69 // Private only.
70 $this->assertEquals(
7f96d9ec
A
71 self::$refDB->countPrivateLinks(),
72 count(self::$linkFilter->filter('', '', false, 'private'))
73 );
74
75 // Public only.
76 $this->assertEquals(
77 self::$refDB->countPublicLinks(),
78 count(self::$linkFilter->filter('', '', false, 'public'))
822bffce 79 );
c51fae92
A
80
81 $this->assertEquals(
89baf23d 82 ReferenceLinkDB::$NB_LINKS_TOTAL,
e26e2060 83 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, ''))
c51fae92
A
84 );
85
7d86f40b
A
86 $this->assertEquals(
87 self::$refDB->countUntaggedLinks(),
9d9f6d75
V
88 count(
89 self::$linkFilter->filter(
e26e2060 90 BookmarkFilter::$FILTER_TAG,
6696729b
V
91 /*$request=*/
92 '',
93 /*$casesensitive=*/
94 false,
95 /*$visibility=*/
96 'all',
97 /*$untaggedonly=*/
98 true
9d9f6d75
V
99 )
100 )
7d86f40b
A
101 );
102
c51fae92 103 $this->assertEquals(
89baf23d 104 ReferenceLinkDB::$NB_LINKS_TOTAL,
e26e2060 105 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, ''))
c51fae92 106 );
822bffce
A
107 }
108
109 /**
e26e2060 110 * Filter bookmarks using a tag
822bffce
A
111 */
112 public function testFilterOneTag()
113 {
114 $this->assertEquals(
115 4,
e26e2060 116 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'web', false))
822bffce
A
117 );
118
7f96d9ec
A
119 $this->assertEquals(
120 4,
e26e2060 121 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'web', false, 'all'))
7f96d9ec
A
122 );
123
124 $this->assertEquals(
125 4,
e26e2060 126 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'web', false, 'default-blabla'))
7f96d9ec
A
127 );
128
822bffce
A
129 // Private only.
130 $this->assertEquals(
131 1,
e26e2060 132 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'web', false, 'private'))
7f96d9ec
A
133 );
134
135 // Public only.
136 $this->assertEquals(
137 3,
e26e2060 138 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'web', false, 'public'))
822bffce
A
139 );
140 }
141
142 /**
e26e2060 143 * Filter bookmarks using a tag - case-sensitive
822bffce
A
144 */
145 public function testFilterCaseSensitiveTag()
146 {
147 $this->assertEquals(
148 0,
e26e2060 149 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'mercurial', true))
822bffce
A
150 );
151
152 $this->assertEquals(
153 1,
e26e2060 154 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'Mercurial', true))
822bffce
A
155 );
156 }
157
158 /**
e26e2060 159 * Filter bookmarks using a tag combination
822bffce
A
160 */
161 public function testFilterMultipleTags()
162 {
163 $this->assertEquals(
164 2,
e26e2060 165 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'dev cartoon', false))
822bffce
A
166 );
167 }
168
169 /**
e26e2060 170 * Filter bookmarks using a non-existent tag
822bffce
A
171 */
172 public function testFilterUnknownTag()
173 {
174 $this->assertEquals(
175 0,
e26e2060 176 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'null', false))
822bffce
A
177 );
178 }
179
180 /**
e26e2060 181 * Return bookmarks for a given day
822bffce
A
182 */
183 public function testFilterDay()
184 {
185 $this->assertEquals(
7d86f40b 186 4,
e26e2060 187 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_DAY, '20121206'))
822bffce
A
188 );
189 }
190
27ddfec3
A
191 /**
192 * Return bookmarks for a given day
193 */
194 public function testFilterDayRestrictedVisibility(): void
195 {
196 $this->assertEquals(
197 3,
198 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_DAY, '20121206', false, BookmarkFilter::$PUBLIC))
199 );
200 }
201
822bffce
A
202 /**
203 * 404 - day not found
204 */
205 public function testFilterUnknownDay()
206 {
207 $this->assertEquals(
208 0,
e26e2060 209 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_DAY, '19700101'))
822bffce
A
210 );
211 }
212
213 /**
214 * Use an invalid date format
215 * @expectedException Exception
822bffce
A
216 */
217 public function testFilterInvalidDayWithChars()
218 {
b1baca99
A
219 $this->expectExceptionMessageRegExp('/Invalid date format/');
220
e26e2060 221 self::$linkFilter->filter(BookmarkFilter::$FILTER_DAY, 'Rainy day, dream away');
822bffce
A
222 }
223
224 /**
225 * Use an invalid date format
226 * @expectedException Exception
822bffce
A
227 */
228 public function testFilterInvalidDayDigits()
229 {
b1baca99
A
230 $this->expectExceptionMessageRegExp('/Invalid date format/');
231
e26e2060 232 self::$linkFilter->filter(BookmarkFilter::$FILTER_DAY, '20');
822bffce
A
233 }
234
235 /**
236 * Retrieve a link entry with its hash
237 */
238 public function testFilterSmallHash()
239 {
e26e2060 240 $links = self::$linkFilter->filter(BookmarkFilter::$FILTER_HASH, 'IuWvgA');
822bffce
A
241
242 $this->assertEquals(
243 1,
244 count($links)
245 );
246
247 $this->assertEquals(
248 'MediaGoblin',
e26e2060 249 $links[7]->getTitle()
822bffce
A
250 );
251 }
252
253 /**
254 * No link for this hash
255 */
256 public function testFilterUnknownSmallHash()
257 {
b1baca99
A
258 $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class);
259
e26e2060 260 self::$linkFilter->filter(BookmarkFilter::$FILTER_HASH, 'Iblaah');
822bffce
A
261 }
262
522b278b
A
263 /**
264 * Full-text search - no result found.
265 */
266 public function testFilterFullTextNoResult()
267 {
268 $this->assertEquals(
269 0,
e26e2060 270 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'azertyuiop'))
522b278b
A
271 );
272 }
273
822bffce
A
274 /**
275 * Full-text search - result from a link's URL
276 */
277 public function testFilterFullTextURL()
278 {
279 $this->assertEquals(
280 2,
e26e2060 281 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'ars.userfriendly.org'))
822bffce 282 );
9d9f6d75 283
ebd8075a
FV
284 $this->assertEquals(
285 2,
e26e2060 286 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'ars org'))
ebd8075a 287 );
822bffce
A
288 }
289
290 /**
291 * Full-text search - result from a link's title only
292 */
293 public function testFilterFullTextTitle()
294 {
295 // use miscellaneous cases
296 $this->assertEquals(
297 2,
e26e2060 298 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'userfriendly -'))
822bffce
A
299 );
300 $this->assertEquals(
301 2,
e26e2060 302 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'UserFriendly -'))
822bffce
A
303 );
304 $this->assertEquals(
305 2,
e26e2060 306 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'uSeRFrIendlY -'))
822bffce
A
307 );
308
309 // use miscellaneous case and offset
310 $this->assertEquals(
311 2,
e26e2060 312 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'RFrIendL'))
822bffce
A
313 );
314 }
315
316 /**
317 * Full-text search - result from the link's description only
318 */
319 public function testFilterFullTextDescription()
320 {
321 $this->assertEquals(
322 1,
e26e2060 323 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'publishing media'))
ebd8075a 324 );
9d9f6d75 325
ebd8075a
FV
326 $this->assertEquals(
327 1,
e26e2060 328 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'mercurial w3c'))
822bffce 329 );
9d9f6d75 330
ebd8075a 331 $this->assertEquals(
bedd176a 332 3,
e26e2060 333 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, '"free software"'))
067c2dd8 334 );
822bffce
A
335 }
336
337 /**
338 * Full-text search - result from the link's tags only
339 */
340 public function testFilterFullTextTags()
341 {
342 $this->assertEquals(
7f96d9ec 343 6,
e26e2060 344 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'web'))
7f96d9ec
A
345 );
346
347 $this->assertEquals(
348 6,
e26e2060 349 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'web', 'all'))
7f96d9ec
A
350 );
351
352 $this->assertEquals(
353 6,
e26e2060 354 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'web', 'bla'))
822bffce
A
355 );
356
357 // Private only.
358 $this->assertEquals(
359 1,
e26e2060 360 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'web', false, 'private'))
7f96d9ec
A
361 );
362
363 // Public only.
364 $this->assertEquals(
365 5,
e26e2060 366 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'web', false, 'public'))
822bffce
A
367 );
368 }
369
370 /**
371 * Full-text search - result set from mixed sources
372 */
373 public function testFilterFullTextMixed()
374 {
375 $this->assertEquals(
bedd176a 376 3,
e26e2060 377 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'free software'))
822bffce
A
378 );
379 }
21979ff1 380
bedd176a
A
381 /**
382 * Full-text search - test exclusion with '-'.
383 */
384 public function testExcludeSearch()
385 {
386 $this->assertEquals(
387 1,
e26e2060 388 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'free -gnu'))
bedd176a
A
389 );
390
391 $this->assertEquals(
7d86f40b 392 ReferenceLinkDB::$NB_LINKS_TOTAL - 1,
e26e2060 393 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, '-revolution'))
bedd176a
A
394 );
395 }
396
397 /**
522b278b 398 * Full-text search - test AND, exact terms and exclusion combined, across fields.
bedd176a
A
399 */
400 public function testMultiSearch()
401 {
402 $this->assertEquals(
403 2,
522b278b 404 count(self::$linkFilter->filter(
e26e2060 405 BookmarkFilter::$FILTER_TEXT,
522b278b
A
406 '"Free Software " stallman "read this" @website stuff'
407 ))
bedd176a
A
408 );
409
410 $this->assertEquals(
411 1,
522b278b 412 count(self::$linkFilter->filter(
e26e2060 413 BookmarkFilter::$FILTER_TEXT,
522b278b
A
414 '"free software " stallman "read this" -beard @website stuff'
415 ))
416 );
417 }
418
419 /**
420 * Full-text search - make sure that exact search won't work across fields.
421 */
422 public function testSearchExactTermMultiFieldsKo()
423 {
424 $this->assertEquals(
425 0,
426 count(self::$linkFilter->filter(
e26e2060 427 BookmarkFilter::$FILTER_TEXT,
522b278b
A
428 '"designer naming"'
429 ))
430 );
431
432 $this->assertEquals(
433 0,
434 count(self::$linkFilter->filter(
e26e2060 435 BookmarkFilter::$FILTER_TEXT,
522b278b
A
436 '"designernaming"'
437 ))
bedd176a
A
438 );
439 }
440
21979ff1
A
441 /**
442 * Tag search with exclusion.
443 */
444 public function testTagFilterWithExclusion()
445 {
446 $this->assertEquals(
447 1,
e26e2060 448 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'gnu -free'))
21979ff1
A
449 );
450
451 $this->assertEquals(
7d86f40b 452 ReferenceLinkDB::$NB_LINKS_TOTAL - 1,
e26e2060 453 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, '-free'))
21979ff1
A
454 );
455 }
c51fae92
A
456
457 /**
458 * Test crossed search (terms + tags).
459 */
460 public function testFilterCrossedSearch()
461 {
462 $terms = '"Free Software " stallman "read this" @website stuff';
463 $tags = 'free';
464 $this->assertEquals(
465 1,
466 count(self::$linkFilter->filter(
e26e2060 467 BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT,
c51fae92
A
468 array($tags, $terms)
469 ))
470 );
471 $this->assertEquals(
472 2,
473 count(self::$linkFilter->filter(
e26e2060 474 BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT,
c51fae92
A
475 array('', $terms)
476 ))
477 );
478 $this->assertEquals(
7d86f40b
A
479 1,
480 count(self::$linkFilter->filter(
e26e2060 481 BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT,
7d86f40b
A
482 array(false, 'PSR-2')
483 ))
484 );
485 $this->assertEquals(
c51fae92
A
486 1,
487 count(self::$linkFilter->filter(
e26e2060 488 BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT,
c51fae92
A
489 array($tags, '')
490 ))
491 );
492 $this->assertEquals(
89baf23d 493 ReferenceLinkDB::$NB_LINKS_TOTAL,
c51fae92 494 count(self::$linkFilter->filter(
e26e2060 495 BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT,
c51fae92
A
496 ''
497 ))
498 );
499 }
9ccca401
A
500
501 /**
e26e2060 502 * Filter bookmarks by #hashtag.
9ccca401
A
503 */
504 public function testFilterByHashtag()
505 {
506 $hashtag = 'hashtag';
507 $this->assertEquals(
508 3,
509 count(self::$linkFilter->filter(
e26e2060 510 BookmarkFilter::$FILTER_TAG,
9ccca401
A
511 $hashtag
512 ))
513 );
514
515 $hashtag = 'private';
516 $this->assertEquals(
517 1,
518 count(self::$linkFilter->filter(
e26e2060 519 BookmarkFilter::$FILTER_TAG,
9ccca401
A
520 $hashtag,
521 false,
7f96d9ec 522 'private'
9ccca401
A
523 ))
524 );
525 }
822bffce 526}