diff options
author | ArthurHoaro <arthur@hoa.ro> | 2015-12-27 10:08:20 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2016-01-06 19:53:04 +0100 |
commit | 822bffced8212e7f34bcb2ad063b31a78bd57bdb (patch) | |
tree | 04cd1efe9d2f015669a451fc7c853c23e38a44cb /tests | |
parent | ba83317573a1477d731cbd3d974b601cf9afdba3 (diff) | |
download | Shaarli-822bffced8212e7f34bcb2ad063b31a78bd57bdb.tar.gz Shaarli-822bffced8212e7f34bcb2ad063b31a78bd57bdb.tar.zst Shaarli-822bffced8212e7f34bcb2ad063b31a78bd57bdb.zip |
Link filter refactoring
* introduce class LinkFilter to handle link filter operation (and lighten LinkDB).
* handle 'private only' in filtering.
* update template to prefill search fields with current search terms.
* coding style.
* unit test (mostly move from LinkDB to LinkFilter).
PS: preparation for #358 #315 and 'AND' search.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/LinkDBTest.php | 229 | ||||
-rw-r--r-- | tests/LinkFilterTest.php | 242 | ||||
-rw-r--r-- | tests/utils/ReferenceLinkDB.php | 5 |
3 files changed, 268 insertions, 208 deletions
diff --git a/tests/LinkDBTest.php b/tests/LinkDBTest.php index 7b22b270..3b1a2057 100644 --- a/tests/LinkDBTest.php +++ b/tests/LinkDBTest.php | |||
@@ -302,236 +302,49 @@ class LinkDBTest extends PHPUnit_Framework_TestCase | |||
302 | } | 302 | } |
303 | 303 | ||
304 | /** | 304 | /** |
305 | * Filter links using a tag | 305 | * Test real_url without redirector. |
306 | */ | ||
307 | public function testFilterOneTag() | ||
308 | { | ||
309 | $this->assertEquals( | ||
310 | 3, | ||
311 | sizeof(self::$publicLinkDB->filterTags('web', false)) | ||
312 | ); | ||
313 | |||
314 | $this->assertEquals( | ||
315 | 4, | ||
316 | sizeof(self::$privateLinkDB->filterTags('web', false)) | ||
317 | ); | ||
318 | } | ||
319 | |||
320 | /** | ||
321 | * Filter links using a tag - case-sensitive | ||
322 | */ | ||
323 | public function testFilterCaseSensitiveTag() | ||
324 | { | ||
325 | $this->assertEquals( | ||
326 | 0, | ||
327 | sizeof(self::$privateLinkDB->filterTags('mercurial', true)) | ||
328 | ); | ||
329 | |||
330 | $this->assertEquals( | ||
331 | 1, | ||
332 | sizeof(self::$privateLinkDB->filterTags('Mercurial', true)) | ||
333 | ); | ||
334 | } | ||
335 | |||
336 | /** | ||
337 | * Filter links using a tag combination | ||
338 | */ | ||
339 | public function testFilterMultipleTags() | ||
340 | { | ||
341 | $this->assertEquals( | ||
342 | 1, | ||
343 | sizeof(self::$publicLinkDB->filterTags('dev cartoon', false)) | ||
344 | ); | ||
345 | |||
346 | $this->assertEquals( | ||
347 | 2, | ||
348 | sizeof(self::$privateLinkDB->filterTags('dev cartoon', false)) | ||
349 | ); | ||
350 | } | ||
351 | |||
352 | /** | ||
353 | * Filter links using a non-existent tag | ||
354 | */ | ||
355 | public function testFilterUnknownTag() | ||
356 | { | ||
357 | $this->assertEquals( | ||
358 | 0, | ||
359 | sizeof(self::$publicLinkDB->filterTags('null', false)) | ||
360 | ); | ||
361 | } | ||
362 | |||
363 | /** | ||
364 | * Return links for a given day | ||
365 | */ | ||
366 | public function testFilterDay() | ||
367 | { | ||
368 | $this->assertEquals( | ||
369 | 2, | ||
370 | sizeof(self::$publicLinkDB->filterDay('20121206')) | ||
371 | ); | ||
372 | |||
373 | $this->assertEquals( | ||
374 | 3, | ||
375 | sizeof(self::$privateLinkDB->filterDay('20121206')) | ||
376 | ); | ||
377 | } | ||
378 | |||
379 | /** | ||
380 | * 404 - day not found | ||
381 | */ | ||
382 | public function testFilterUnknownDay() | ||
383 | { | ||
384 | $this->assertEquals( | ||
385 | 0, | ||
386 | sizeof(self::$publicLinkDB->filterDay('19700101')) | ||
387 | ); | ||
388 | |||
389 | $this->assertEquals( | ||
390 | 0, | ||
391 | sizeof(self::$privateLinkDB->filterDay('19700101')) | ||
392 | ); | ||
393 | } | ||
394 | |||
395 | /** | ||
396 | * Use an invalid date format | ||
397 | * @expectedException Exception | ||
398 | * @expectedExceptionMessageRegExp /Invalid date format/ | ||
399 | */ | ||
400 | public function testFilterInvalidDayWithChars() | ||
401 | { | ||
402 | self::$privateLinkDB->filterDay('Rainy day, dream away'); | ||
403 | } | ||
404 | |||
405 | /** | ||
406 | * Use an invalid date format | ||
407 | * @expectedException Exception | ||
408 | * @expectedExceptionMessageRegExp /Invalid date format/ | ||
409 | */ | ||
410 | public function testFilterInvalidDayDigits() | ||
411 | { | ||
412 | self::$privateLinkDB->filterDay('20'); | ||
413 | } | ||
414 | |||
415 | /** | ||
416 | * Retrieve a link entry with its hash | ||
417 | */ | ||
418 | public function testFilterSmallHash() | ||
419 | { | ||
420 | $links = self::$privateLinkDB->filterSmallHash('IuWvgA'); | ||
421 | |||
422 | $this->assertEquals( | ||
423 | 1, | ||
424 | sizeof($links) | ||
425 | ); | ||
426 | |||
427 | $this->assertEquals( | ||
428 | 'MediaGoblin', | ||
429 | $links['20130614_184135']['title'] | ||
430 | ); | ||
431 | |||
432 | } | ||
433 | |||
434 | /** | ||
435 | * No link for this hash | ||
436 | */ | ||
437 | public function testFilterUnknownSmallHash() | ||
438 | { | ||
439 | $this->assertEquals( | ||
440 | 0, | ||
441 | sizeof(self::$privateLinkDB->filterSmallHash('Iblaah')) | ||
442 | ); | ||
443 | } | ||
444 | |||
445 | /** | ||
446 | * Full-text search - result from a link's URL | ||
447 | */ | ||
448 | public function testFilterFullTextURL() | ||
449 | { | ||
450 | $this->assertEquals( | ||
451 | 2, | ||
452 | sizeof(self::$publicLinkDB->filterFullText('ars.userfriendly.org')) | ||
453 | ); | ||
454 | } | ||
455 | |||
456 | /** | ||
457 | * Full-text search - result from a link's title only | ||
458 | */ | 306 | */ |
459 | public function testFilterFullTextTitle() | 307 | public function testLinkRealUrlWithoutRedirector() |
460 | { | 308 | { |
461 | // use miscellaneous cases | 309 | $db = new LinkDB(self::$testDatastore, false, false); |
462 | $this->assertEquals( | 310 | foreach($db as $link) { |
463 | 2, | 311 | $this->assertEquals($link['url'], $link['real_url']); |
464 | sizeof(self::$publicLinkDB->filterFullText('userfriendly -')) | 312 | } |
465 | ); | ||
466 | $this->assertEquals( | ||
467 | 2, | ||
468 | sizeof(self::$publicLinkDB->filterFullText('UserFriendly -')) | ||
469 | ); | ||
470 | $this->assertEquals( | ||
471 | 2, | ||
472 | sizeof(self::$publicLinkDB->filterFullText('uSeRFrIendlY -')) | ||
473 | ); | ||
474 | |||
475 | // use miscellaneous case and offset | ||
476 | $this->assertEquals( | ||
477 | 2, | ||
478 | sizeof(self::$publicLinkDB->filterFullText('RFrIendL')) | ||
479 | ); | ||
480 | } | 313 | } |
481 | 314 | ||
482 | /** | 315 | /** |
483 | * Full-text search - result from the link's description only | 316 | * Test real_url with redirector. |
484 | */ | 317 | */ |
485 | public function testFilterFullTextDescription() | 318 | public function testLinkRealUrlWithRedirector() |
486 | { | 319 | { |
487 | $this->assertEquals( | 320 | $redirector = 'http://redirector.to?'; |
488 | 1, | 321 | $db = new LinkDB(self::$testDatastore, false, false, $redirector); |
489 | sizeof(self::$publicLinkDB->filterFullText('media publishing')) | 322 | foreach($db as $link) { |
490 | ); | 323 | $this->assertStringStartsWith($redirector, $link['real_url']); |
324 | } | ||
491 | } | 325 | } |
492 | 326 | ||
493 | /** | 327 | /** |
494 | * Full-text search - result from the link's tags only | 328 | * Test filter with string. |
495 | */ | 329 | */ |
496 | public function testFilterFullTextTags() | 330 | public function testFilterString() |
497 | { | 331 | { |
332 | $tags = 'dev cartoon'; | ||
498 | $this->assertEquals( | 333 | $this->assertEquals( |
499 | 2, | 334 | 2, |
500 | sizeof(self::$publicLinkDB->filterFullText('gnu')) | 335 | count(self::$privateLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false)) |
501 | ); | 336 | ); |
502 | } | 337 | } |
503 | 338 | ||
504 | /** | 339 | /** |
505 | * Full-text search - result set from mixed sources | 340 | * Test filter with string. |
506 | */ | 341 | */ |
507 | public function testFilterFullTextMixed() | 342 | public function testFilterArray() |
508 | { | 343 | { |
344 | $tags = array('dev', 'cartoon'); | ||
509 | $this->assertEquals( | 345 | $this->assertEquals( |
510 | 2, | 346 | 2, |
511 | sizeof(self::$publicLinkDB->filterFullText('free software')) | 347 | count(self::$privateLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false)) |
512 | ); | 348 | ); |
513 | } | 349 | } |
514 | |||
515 | /** | ||
516 | * Test real_url without redirector. | ||
517 | */ | ||
518 | public function testLinkRealUrlWithoutRedirector() | ||
519 | { | ||
520 | $db = new LinkDB(self::$testDatastore, false, false); | ||
521 | foreach($db as $link) { | ||
522 | $this->assertEquals($link['url'], $link['real_url']); | ||
523 | } | ||
524 | } | ||
525 | |||
526 | /** | ||
527 | * Test real_url with redirector. | ||
528 | */ | ||
529 | public function testLinkRealUrlWithRedirector() | ||
530 | { | ||
531 | $redirector = 'http://redirector.to?'; | ||
532 | $db = new LinkDB(self::$testDatastore, false, false, $redirector); | ||
533 | foreach($db as $link) { | ||
534 | $this->assertStringStartsWith($redirector, $link['real_url']); | ||
535 | } | ||
536 | } | ||
537 | } | 350 | } |
diff --git a/tests/LinkFilterTest.php b/tests/LinkFilterTest.php new file mode 100644 index 00000000..5107ab72 --- /dev/null +++ b/tests/LinkFilterTest.php | |||
@@ -0,0 +1,242 @@ | |||
1 | <?php | ||
2 | |||
3 | require_once 'application/LinkFilter.php'; | ||
4 | |||
5 | /** | ||
6 | * Class LinkFilterTest. | ||
7 | */ | ||
8 | class LinkFilterTest extends PHPUnit_Framework_TestCase | ||
9 | { | ||
10 | /** | ||
11 | * @var LinkFilter instance. | ||
12 | */ | ||
13 | protected static $linkFilter; | ||
14 | |||
15 | /** | ||
16 | * Instanciate linkFilter with ReferenceLinkDB data. | ||
17 | */ | ||
18 | public static function setUpBeforeClass() | ||
19 | { | ||
20 | $refDB = new ReferenceLinkDB(); | ||
21 | self::$linkFilter = new LinkFilter($refDB->getLinks()); | ||
22 | } | ||
23 | |||
24 | /** | ||
25 | * Blank filter. | ||
26 | */ | ||
27 | public function testFilter() | ||
28 | { | ||
29 | $this->assertEquals( | ||
30 | 6, | ||
31 | count(self::$linkFilter->filter('', '')) | ||
32 | ); | ||
33 | |||
34 | // Private only. | ||
35 | $this->assertEquals( | ||
36 | 2, | ||
37 | count(self::$linkFilter->filter('', '', false, true)) | ||
38 | ); | ||
39 | } | ||
40 | |||
41 | /** | ||
42 | * Filter links using a tag | ||
43 | */ | ||
44 | public function testFilterOneTag() | ||
45 | { | ||
46 | $this->assertEquals( | ||
47 | 4, | ||
48 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'web', false)) | ||
49 | ); | ||
50 | |||
51 | // Private only. | ||
52 | $this->assertEquals( | ||
53 | 1, | ||
54 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'web', false, true)) | ||
55 | ); | ||
56 | } | ||
57 | |||
58 | /** | ||
59 | * Filter links using a tag - case-sensitive | ||
60 | */ | ||
61 | public function testFilterCaseSensitiveTag() | ||
62 | { | ||
63 | $this->assertEquals( | ||
64 | 0, | ||
65 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'mercurial', true)) | ||
66 | ); | ||
67 | |||
68 | $this->assertEquals( | ||
69 | 1, | ||
70 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'Mercurial', true)) | ||
71 | ); | ||
72 | } | ||
73 | |||
74 | /** | ||
75 | * Filter links using a tag combination | ||
76 | */ | ||
77 | public function testFilterMultipleTags() | ||
78 | { | ||
79 | $this->assertEquals( | ||
80 | 2, | ||
81 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'dev cartoon', false)) | ||
82 | ); | ||
83 | } | ||
84 | |||
85 | /** | ||
86 | * Filter links using a non-existent tag | ||
87 | */ | ||
88 | public function testFilterUnknownTag() | ||
89 | { | ||
90 | $this->assertEquals( | ||
91 | 0, | ||
92 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'null', false)) | ||
93 | ); | ||
94 | } | ||
95 | |||
96 | /** | ||
97 | * Return links for a given day | ||
98 | */ | ||
99 | public function testFilterDay() | ||
100 | { | ||
101 | $this->assertEquals( | ||
102 | 3, | ||
103 | count(self::$linkFilter->filter(LinkFilter::$FILTER_DAY, '20121206')) | ||
104 | ); | ||
105 | } | ||
106 | |||
107 | /** | ||
108 | * 404 - day not found | ||
109 | */ | ||
110 | public function testFilterUnknownDay() | ||
111 | { | ||
112 | $this->assertEquals( | ||
113 | 0, | ||
114 | count(self::$linkFilter->filter(LinkFilter::$FILTER_DAY, '19700101')) | ||
115 | ); | ||
116 | } | ||
117 | |||
118 | /** | ||
119 | * Use an invalid date format | ||
120 | * @expectedException Exception | ||
121 | * @expectedExceptionMessageRegExp /Invalid date format/ | ||
122 | */ | ||
123 | public function testFilterInvalidDayWithChars() | ||
124 | { | ||
125 | self::$linkFilter->filter(LinkFilter::$FILTER_DAY, 'Rainy day, dream away'); | ||
126 | } | ||
127 | |||
128 | /** | ||
129 | * Use an invalid date format | ||
130 | * @expectedException Exception | ||
131 | * @expectedExceptionMessageRegExp /Invalid date format/ | ||
132 | */ | ||
133 | public function testFilterInvalidDayDigits() | ||
134 | { | ||
135 | self::$linkFilter->filter(LinkFilter::$FILTER_DAY, '20'); | ||
136 | } | ||
137 | |||
138 | /** | ||
139 | * Retrieve a link entry with its hash | ||
140 | */ | ||
141 | public function testFilterSmallHash() | ||
142 | { | ||
143 | $links = self::$linkFilter->filter(LinkFilter::$FILTER_HASH, 'IuWvgA'); | ||
144 | |||
145 | $this->assertEquals( | ||
146 | 1, | ||
147 | count($links) | ||
148 | ); | ||
149 | |||
150 | $this->assertEquals( | ||
151 | 'MediaGoblin', | ||
152 | $links['20130614_184135']['title'] | ||
153 | ); | ||
154 | } | ||
155 | |||
156 | /** | ||
157 | * No link for this hash | ||
158 | */ | ||
159 | public function testFilterUnknownSmallHash() | ||
160 | { | ||
161 | $this->assertEquals( | ||
162 | 0, | ||
163 | count(self::$linkFilter->filter(LinkFilter::$FILTER_HASH, 'Iblaah')) | ||
164 | ); | ||
165 | } | ||
166 | |||
167 | /** | ||
168 | * Full-text search - result from a link's URL | ||
169 | */ | ||
170 | public function testFilterFullTextURL() | ||
171 | { | ||
172 | $this->assertEquals( | ||
173 | 2, | ||
174 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'ars.userfriendly.org')) | ||
175 | ); | ||
176 | } | ||
177 | |||
178 | /** | ||
179 | * Full-text search - result from a link's title only | ||
180 | */ | ||
181 | public function testFilterFullTextTitle() | ||
182 | { | ||
183 | // use miscellaneous cases | ||
184 | $this->assertEquals( | ||
185 | 2, | ||
186 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'userfriendly -')) | ||
187 | ); | ||
188 | $this->assertEquals( | ||
189 | 2, | ||
190 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'UserFriendly -')) | ||
191 | ); | ||
192 | $this->assertEquals( | ||
193 | 2, | ||
194 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'uSeRFrIendlY -')) | ||
195 | ); | ||
196 | |||
197 | // use miscellaneous case and offset | ||
198 | $this->assertEquals( | ||
199 | 2, | ||
200 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'RFrIendL')) | ||
201 | ); | ||
202 | } | ||
203 | |||
204 | /** | ||
205 | * Full-text search - result from the link's description only | ||
206 | */ | ||
207 | public function testFilterFullTextDescription() | ||
208 | { | ||
209 | $this->assertEquals( | ||
210 | 1, | ||
211 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'media publishing')) | ||
212 | ); | ||
213 | } | ||
214 | |||
215 | /** | ||
216 | * Full-text search - result from the link's tags only | ||
217 | */ | ||
218 | public function testFilterFullTextTags() | ||
219 | { | ||
220 | $this->assertEquals( | ||
221 | 2, | ||
222 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'gnu')) | ||
223 | ); | ||
224 | |||
225 | // Private only. | ||
226 | $this->assertEquals( | ||
227 | 1, | ||
228 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'web', false, true)) | ||
229 | ); | ||
230 | } | ||
231 | |||
232 | /** | ||
233 | * Full-text search - result set from mixed sources | ||
234 | */ | ||
235 | public function testFilterFullTextMixed() | ||
236 | { | ||
237 | $this->assertEquals( | ||
238 | 2, | ||
239 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'free software')) | ||
240 | ); | ||
241 | } | ||
242 | } | ||
diff --git a/tests/utils/ReferenceLinkDB.php b/tests/utils/ReferenceLinkDB.php index 47b51829..011317ef 100644 --- a/tests/utils/ReferenceLinkDB.php +++ b/tests/utils/ReferenceLinkDB.php | |||
@@ -124,4 +124,9 @@ class ReferenceLinkDB | |||
124 | { | 124 | { |
125 | return $this->_privateCount; | 125 | return $this->_privateCount; |
126 | } | 126 | } |
127 | |||
128 | public function getLinks() | ||
129 | { | ||
130 | return $this->_links; | ||
131 | } | ||
127 | } | 132 | } |