]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/bookmark/BookmarkFilterTest.php
Merge pull request #1547 from ArthurHoaro/fix/daily-visibility
[github/shaarli/Shaarli.git] / tests / bookmark / BookmarkFilterTest.php
CommitLineData
822bffce
A
1<?php
2
6696729b 3namespace Shaarli\Bookmark;
f24896b2 4
6696729b 5use Exception;
e26e2060 6use PHPUnit\Framework\TestCase;
6696729b 7use ReferenceLinkDB;
e26e2060 8use Shaarli\Config\ConfigManager;
e26e2060 9use Shaarli\History;
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
216 * @expectedExceptionMessageRegExp /Invalid date format/
217 */
218 public function testFilterInvalidDayWithChars()
219 {
e26e2060 220 self::$linkFilter->filter(BookmarkFilter::$FILTER_DAY, 'Rainy day, dream away');
822bffce
A
221 }
222
223 /**
224 * Use an invalid date format
225 * @expectedException Exception
226 * @expectedExceptionMessageRegExp /Invalid date format/
227 */
228 public function testFilterInvalidDayDigits()
229 {
e26e2060 230 self::$linkFilter->filter(BookmarkFilter::$FILTER_DAY, '20');
822bffce
A
231 }
232
233 /**
234 * Retrieve a link entry with its hash
235 */
236 public function testFilterSmallHash()
237 {
e26e2060 238 $links = self::$linkFilter->filter(BookmarkFilter::$FILTER_HASH, 'IuWvgA');
822bffce
A
239
240 $this->assertEquals(
241 1,
242 count($links)
243 );
244
245 $this->assertEquals(
246 'MediaGoblin',
e26e2060 247 $links[7]->getTitle()
822bffce
A
248 );
249 }
250
251 /**
252 * No link for this hash
528a6f8a 253 *
e26e2060 254 * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException
822bffce
A
255 */
256 public function testFilterUnknownSmallHash()
257 {
e26e2060 258 self::$linkFilter->filter(BookmarkFilter::$FILTER_HASH, 'Iblaah');
822bffce
A
259 }
260
522b278b
A
261 /**
262 * Full-text search - no result found.
263 */
264 public function testFilterFullTextNoResult()
265 {
266 $this->assertEquals(
267 0,
e26e2060 268 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'azertyuiop'))
522b278b
A
269 );
270 }
271
822bffce
A
272 /**
273 * Full-text search - result from a link's URL
274 */
275 public function testFilterFullTextURL()
276 {
277 $this->assertEquals(
278 2,
e26e2060 279 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'ars.userfriendly.org'))
822bffce 280 );
9d9f6d75 281
ebd8075a
FV
282 $this->assertEquals(
283 2,
e26e2060 284 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'ars org'))
ebd8075a 285 );
822bffce
A
286 }
287
288 /**
289 * Full-text search - result from a link's title only
290 */
291 public function testFilterFullTextTitle()
292 {
293 // use miscellaneous cases
294 $this->assertEquals(
295 2,
e26e2060 296 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'userfriendly -'))
822bffce
A
297 );
298 $this->assertEquals(
299 2,
e26e2060 300 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'UserFriendly -'))
822bffce
A
301 );
302 $this->assertEquals(
303 2,
e26e2060 304 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'uSeRFrIendlY -'))
822bffce
A
305 );
306
307 // use miscellaneous case and offset
308 $this->assertEquals(
309 2,
e26e2060 310 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'RFrIendL'))
822bffce
A
311 );
312 }
313
314 /**
315 * Full-text search - result from the link's description only
316 */
317 public function testFilterFullTextDescription()
318 {
319 $this->assertEquals(
320 1,
e26e2060 321 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'publishing media'))
ebd8075a 322 );
9d9f6d75 323
ebd8075a
FV
324 $this->assertEquals(
325 1,
e26e2060 326 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'mercurial w3c'))
822bffce 327 );
9d9f6d75 328
ebd8075a 329 $this->assertEquals(
bedd176a 330 3,
e26e2060 331 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, '"free software"'))
067c2dd8 332 );
822bffce
A
333 }
334
335 /**
336 * Full-text search - result from the link's tags only
337 */
338 public function testFilterFullTextTags()
339 {
340 $this->assertEquals(
7f96d9ec 341 6,
e26e2060 342 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'web'))
7f96d9ec
A
343 );
344
345 $this->assertEquals(
346 6,
e26e2060 347 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'web', 'all'))
7f96d9ec
A
348 );
349
350 $this->assertEquals(
351 6,
e26e2060 352 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'web', 'bla'))
822bffce
A
353 );
354
355 // Private only.
356 $this->assertEquals(
357 1,
e26e2060 358 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'web', false, 'private'))
7f96d9ec
A
359 );
360
361 // Public only.
362 $this->assertEquals(
363 5,
e26e2060 364 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'web', false, 'public'))
822bffce
A
365 );
366 }
367
368 /**
369 * Full-text search - result set from mixed sources
370 */
371 public function testFilterFullTextMixed()
372 {
373 $this->assertEquals(
bedd176a 374 3,
e26e2060 375 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'free software'))
822bffce
A
376 );
377 }
21979ff1 378
bedd176a
A
379 /**
380 * Full-text search - test exclusion with '-'.
381 */
382 public function testExcludeSearch()
383 {
384 $this->assertEquals(
385 1,
e26e2060 386 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'free -gnu'))
bedd176a
A
387 );
388
389 $this->assertEquals(
7d86f40b 390 ReferenceLinkDB::$NB_LINKS_TOTAL - 1,
e26e2060 391 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, '-revolution'))
bedd176a
A
392 );
393 }
394
395 /**
522b278b 396 * Full-text search - test AND, exact terms and exclusion combined, across fields.
bedd176a
A
397 */
398 public function testMultiSearch()
399 {
400 $this->assertEquals(
401 2,
522b278b 402 count(self::$linkFilter->filter(
e26e2060 403 BookmarkFilter::$FILTER_TEXT,
522b278b
A
404 '"Free Software " stallman "read this" @website stuff'
405 ))
bedd176a
A
406 );
407
408 $this->assertEquals(
409 1,
522b278b 410 count(self::$linkFilter->filter(
e26e2060 411 BookmarkFilter::$FILTER_TEXT,
522b278b
A
412 '"free software " stallman "read this" -beard @website stuff'
413 ))
414 );
415 }
416
417 /**
418 * Full-text search - make sure that exact search won't work across fields.
419 */
420 public function testSearchExactTermMultiFieldsKo()
421 {
422 $this->assertEquals(
423 0,
424 count(self::$linkFilter->filter(
e26e2060 425 BookmarkFilter::$FILTER_TEXT,
522b278b
A
426 '"designer naming"'
427 ))
428 );
429
430 $this->assertEquals(
431 0,
432 count(self::$linkFilter->filter(
e26e2060 433 BookmarkFilter::$FILTER_TEXT,
522b278b
A
434 '"designernaming"'
435 ))
bedd176a
A
436 );
437 }
438
21979ff1
A
439 /**
440 * Tag search with exclusion.
441 */
442 public function testTagFilterWithExclusion()
443 {
444 $this->assertEquals(
445 1,
e26e2060 446 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'gnu -free'))
21979ff1
A
447 );
448
449 $this->assertEquals(
7d86f40b 450 ReferenceLinkDB::$NB_LINKS_TOTAL - 1,
e26e2060 451 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, '-free'))
21979ff1
A
452 );
453 }
c51fae92
A
454
455 /**
456 * Test crossed search (terms + tags).
457 */
458 public function testFilterCrossedSearch()
459 {
460 $terms = '"Free Software " stallman "read this" @website stuff';
461 $tags = 'free';
462 $this->assertEquals(
463 1,
464 count(self::$linkFilter->filter(
e26e2060 465 BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT,
c51fae92
A
466 array($tags, $terms)
467 ))
468 );
469 $this->assertEquals(
470 2,
471 count(self::$linkFilter->filter(
e26e2060 472 BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT,
c51fae92
A
473 array('', $terms)
474 ))
475 );
476 $this->assertEquals(
7d86f40b
A
477 1,
478 count(self::$linkFilter->filter(
e26e2060 479 BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT,
7d86f40b
A
480 array(false, 'PSR-2')
481 ))
482 );
483 $this->assertEquals(
c51fae92
A
484 1,
485 count(self::$linkFilter->filter(
e26e2060 486 BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT,
c51fae92
A
487 array($tags, '')
488 ))
489 );
490 $this->assertEquals(
89baf23d 491 ReferenceLinkDB::$NB_LINKS_TOTAL,
c51fae92 492 count(self::$linkFilter->filter(
e26e2060 493 BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT,
c51fae92
A
494 ''
495 ))
496 );
497 }
9ccca401
A
498
499 /**
e26e2060 500 * Filter bookmarks by #hashtag.
9ccca401
A
501 */
502 public function testFilterByHashtag()
503 {
504 $hashtag = 'hashtag';
505 $this->assertEquals(
506 3,
507 count(self::$linkFilter->filter(
e26e2060 508 BookmarkFilter::$FILTER_TAG,
9ccca401
A
509 $hashtag
510 ))
511 );
512
513 $hashtag = 'private';
514 $this->assertEquals(
515 1,
516 count(self::$linkFilter->filter(
e26e2060 517 BookmarkFilter::$FILTER_TAG,
9ccca401
A
518 $hashtag,
519 false,
7f96d9ec 520 'private'
9ccca401
A
521 ))
522 );
523 }
822bffce 524}