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