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