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