diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-10-12 12:23:57 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-10-16 20:31:49 +0200 |
commit | f1a148ab92c061ac129b5b2976de02d45b6a71e7 (patch) | |
tree | 89b085ac6c4cd7608526f855f452d1a797dbb333 /tests | |
parent | 4e3875c0ce7f3b17e3d358dc5ecb1f8bed64546b (diff) | |
download | Shaarli-f1a148ab92c061ac129b5b2976de02d45b6a71e7.tar.gz Shaarli-f1a148ab92c061ac129b5b2976de02d45b6a71e7.tar.zst Shaarli-f1a148ab92c061ac129b5b2976de02d45b6a71e7.zip |
add search highlight unit tests
Diffstat (limited to 'tests')
-rw-r--r-- | tests/api/controllers/links/GetLinksTest.php | 2 | ||||
-rw-r--r-- | tests/bookmark/BookmarkFileServiceTest.php | 16 | ||||
-rw-r--r-- | tests/bookmark/BookmarkFilterTest.php | 40 | ||||
-rw-r--r-- | tests/formatter/BookmarkDefaultFormatterTest.php | 115 | ||||
-rw-r--r-- | tests/legacy/LegacyLinkDBTest.php | 12 | ||||
-rw-r--r-- | tests/utils/ReferenceLinkDB.php | 2 |
6 files changed, 184 insertions, 3 deletions
diff --git a/tests/api/controllers/links/GetLinksTest.php b/tests/api/controllers/links/GetLinksTest.php index 0f5073b4..b1c46ee2 100644 --- a/tests/api/controllers/links/GetLinksTest.php +++ b/tests/api/controllers/links/GetLinksTest.php | |||
@@ -398,7 +398,7 @@ class GetLinksTest extends \Shaarli\TestCase | |||
398 | $response = $this->controller->getLinks($request, new Response()); | 398 | $response = $this->controller->getLinks($request, new Response()); |
399 | $this->assertEquals(200, $response->getStatusCode()); | 399 | $this->assertEquals(200, $response->getStatusCode()); |
400 | $data = json_decode((string) $response->getBody(), true); | 400 | $data = json_decode((string) $response->getBody(), true); |
401 | $this->assertEquals(4, count($data)); | 401 | $this->assertEquals(5, count($data)); |
402 | $this->assertEquals(6, $data[0]['id']); | 402 | $this->assertEquals(6, $data[0]['id']); |
403 | 403 | ||
404 | // wildcard: placeholder at the middle | 404 | // wildcard: placeholder at the middle |
diff --git a/tests/bookmark/BookmarkFileServiceTest.php b/tests/bookmark/BookmarkFileServiceTest.php index 42485c99..daafd250 100644 --- a/tests/bookmark/BookmarkFileServiceTest.php +++ b/tests/bookmark/BookmarkFileServiceTest.php | |||
@@ -748,6 +748,10 @@ class BookmarkFileServiceTest extends TestCase | |||
748 | // They need to be grouped with the first case found - order by date DESC: `sTuff`. | 748 | // They need to be grouped with the first case found - order by date DESC: `sTuff`. |
749 | 'sTuff' => 2, | 749 | 'sTuff' => 2, |
750 | 'ut' => 1, | 750 | 'ut' => 1, |
751 | 'assurance' => 1, | ||
752 | 'coding-style' => 1, | ||
753 | 'quality' => 1, | ||
754 | 'standards' => 1, | ||
751 | ], | 755 | ], |
752 | $this->publicLinkDB->bookmarksCountPerTag() | 756 | $this->publicLinkDB->bookmarksCountPerTag() |
753 | ); | 757 | ); |
@@ -776,6 +780,10 @@ class BookmarkFileServiceTest extends TestCase | |||
776 | 'tag3' => 1, | 780 | 'tag3' => 1, |
777 | 'tag4' => 1, | 781 | 'tag4' => 1, |
778 | 'ut' => 1, | 782 | 'ut' => 1, |
783 | 'assurance' => 1, | ||
784 | 'coding-style' => 1, | ||
785 | 'quality' => 1, | ||
786 | 'standards' => 1, | ||
779 | ], | 787 | ], |
780 | $this->privateLinkDB->bookmarksCountPerTag() | 788 | $this->privateLinkDB->bookmarksCountPerTag() |
781 | ); | 789 | ); |
@@ -918,6 +926,10 @@ class BookmarkFileServiceTest extends TestCase | |||
918 | 'tag4' => 1, | 926 | 'tag4' => 1, |
919 | 'ut' => 1, | 927 | 'ut' => 1, |
920 | 'w3c' => 1, | 928 | 'w3c' => 1, |
929 | 'assurance' => 1, | ||
930 | 'coding-style' => 1, | ||
931 | 'quality' => 1, | ||
932 | 'standards' => 1, | ||
921 | ]; | 933 | ]; |
922 | $tags = $this->privateLinkDB->bookmarksCountPerTag(); | 934 | $tags = $this->privateLinkDB->bookmarksCountPerTag(); |
923 | 935 | ||
@@ -1016,6 +1028,10 @@ class BookmarkFileServiceTest extends TestCase | |||
1016 | 'stallman' => 1, | 1028 | 'stallman' => 1, |
1017 | 'ut' => 1, | 1029 | 'ut' => 1, |
1018 | 'w3c' => 1, | 1030 | 'w3c' => 1, |
1031 | 'assurance' => 1, | ||
1032 | 'coding-style' => 1, | ||
1033 | 'quality' => 1, | ||
1034 | 'standards' => 1, | ||
1019 | ]; | 1035 | ]; |
1020 | $bookmark = new Bookmark(); | 1036 | $bookmark = new Bookmark(); |
1021 | $bookmark->setTags(['newTagToCount', BookmarkMarkdownFormatter::NO_MD_TAG]); | 1037 | $bookmark->setTags(['newTagToCount', BookmarkMarkdownFormatter::NO_MD_TAG]); |
diff --git a/tests/bookmark/BookmarkFilterTest.php b/tests/bookmark/BookmarkFilterTest.php index 644abbc8..574d8e3f 100644 --- a/tests/bookmark/BookmarkFilterTest.php +++ b/tests/bookmark/BookmarkFilterTest.php | |||
@@ -2,7 +2,6 @@ | |||
2 | 2 | ||
3 | namespace Shaarli\Bookmark; | 3 | namespace Shaarli\Bookmark; |
4 | 4 | ||
5 | use Exception; | ||
6 | use malkusch\lock\mutex\NoMutex; | 5 | use malkusch\lock\mutex\NoMutex; |
7 | use ReferenceLinkDB; | 6 | use ReferenceLinkDB; |
8 | use Shaarli\Config\ConfigManager; | 7 | use Shaarli\Config\ConfigManager; |
@@ -525,4 +524,43 @@ class BookmarkFilterTest extends TestCase | |||
525 | )) | 524 | )) |
526 | ); | 525 | ); |
527 | } | 526 | } |
527 | |||
528 | /** | ||
529 | * Test search result highlights in every field of bookmark reference #9. | ||
530 | */ | ||
531 | public function testFullTextSearchHighlight(): void | ||
532 | { | ||
533 | $bookmarks = self::$linkFilter->filter( | ||
534 | BookmarkFilter::$FILTER_TEXT, | ||
535 | '"psr-2" coding guide http fig "psr-2/" "This guide" basic standard. coding-style quality assurance' | ||
536 | ); | ||
537 | |||
538 | static::assertCount(1, $bookmarks); | ||
539 | static::assertArrayHasKey(9, $bookmarks); | ||
540 | |||
541 | $bookmark = $bookmarks[9]; | ||
542 | $expectedHighlights = [ | ||
543 | 'title' => [ | ||
544 | ['start' => 0, 'end' => 5], // "psr-2" | ||
545 | ['start' => 7, 'end' => 13], // coding | ||
546 | ['start' => 20, 'end' => 25], // guide | ||
547 | ], | ||
548 | 'description' => [ | ||
549 | ['start' => 0, 'end' => 10], // "This guide" | ||
550 | ['start' => 45, 'end' => 50], // basic | ||
551 | ['start' => 58, 'end' => 67], // standard. | ||
552 | ], | ||
553 | 'url' => [ | ||
554 | ['start' => 0, 'end' => 4], // http | ||
555 | ['start' => 15, 'end' => 18], // fig | ||
556 | ['start' => 27, 'end' => 33], // "psr-2/" | ||
557 | ], | ||
558 | 'tags' => [ | ||
559 | ['start' => 0, 'end' => 12], // coding-style | ||
560 | ['start' => 23, 'end' => 30], // quality | ||
561 | ['start' => 31, 'end' => 40], // assurance | ||
562 | ], | ||
563 | ]; | ||
564 | static::assertSame($expectedHighlights, $bookmark->getAdditionalContentEntry('search_highlight')); | ||
565 | } | ||
528 | } | 566 | } |
diff --git a/tests/formatter/BookmarkDefaultFormatterTest.php b/tests/formatter/BookmarkDefaultFormatterTest.php index 9534436e..3fc6f8dc 100644 --- a/tests/formatter/BookmarkDefaultFormatterTest.php +++ b/tests/formatter/BookmarkDefaultFormatterTest.php | |||
@@ -174,4 +174,119 @@ class BookmarkDefaultFormatterTest extends TestCase | |||
174 | $this->assertSame($tags, $link['taglist']); | 174 | $this->assertSame($tags, $link['taglist']); |
175 | $this->assertSame(implode(' ', $tags), $link['tags']); | 175 | $this->assertSame(implode(' ', $tags), $link['tags']); |
176 | } | 176 | } |
177 | |||
178 | /** | ||
179 | * Test formatTitleHtml with search result highlight. | ||
180 | */ | ||
181 | public function testFormatTitleHtmlWithSearchHighlight(): void | ||
182 | { | ||
183 | $this->formatter = new BookmarkDefaultFormatter($this->conf, false); | ||
184 | |||
185 | $bookmark = new Bookmark(); | ||
186 | $bookmark->setTitle('PSR-2: Coding Style Guide'); | ||
187 | $bookmark->addAdditionalContentEntry( | ||
188 | 'search_highlight', | ||
189 | ['title' => [ | ||
190 | ['start' => 0, 'end' => 5], // "psr-2" | ||
191 | ['start' => 7, 'end' => 13], // coding | ||
192 | ['start' => 20, 'end' => 25], // guide | ||
193 | ]] | ||
194 | ); | ||
195 | |||
196 | $link = $this->formatter->format($bookmark); | ||
197 | |||
198 | $this->assertSame( | ||
199 | '<span class="search-highlight">PSR-2</span>: ' . | ||
200 | '<span class="search-highlight">Coding</span> Style ' . | ||
201 | '<span class="search-highlight">Guide</span>', | ||
202 | $link['title_html'] | ||
203 | ); | ||
204 | } | ||
205 | |||
206 | /** | ||
207 | * Test formatDescription with search result highlight. | ||
208 | */ | ||
209 | public function testFormatDescriptionWithSearchHighlight(): void | ||
210 | { | ||
211 | $this->formatter = new BookmarkDefaultFormatter($this->conf, false); | ||
212 | |||
213 | $bookmark = new Bookmark(); | ||
214 | $bookmark->setDescription('This guide extends and expands on PSR-1, the basic coding standard.'); | ||
215 | $bookmark->addAdditionalContentEntry( | ||
216 | 'search_highlight', | ||
217 | ['description' => [ | ||
218 | ['start' => 0, 'end' => 10], // "This guide" | ||
219 | ['start' => 45, 'end' => 50], // basic | ||
220 | ['start' => 58, 'end' => 67], // standard. | ||
221 | ]] | ||
222 | ); | ||
223 | |||
224 | $link = $this->formatter->format($bookmark); | ||
225 | |||
226 | $this->assertSame( | ||
227 | '<span class="search-highlight">This guide</span> extends and expands on PSR-1, the ' . | ||
228 | '<span class="search-highlight">basic</span> coding ' . | ||
229 | '<span class="search-highlight">standard.</span>', | ||
230 | $link['description'] | ||
231 | ); | ||
232 | } | ||
233 | |||
234 | /** | ||
235 | * Test formatUrlHtml with search result highlight. | ||
236 | */ | ||
237 | public function testFormatUrlHtmlWithSearchHighlight(): void | ||
238 | { | ||
239 | $this->formatter = new BookmarkDefaultFormatter($this->conf, false); | ||
240 | |||
241 | $bookmark = new Bookmark(); | ||
242 | $bookmark->setUrl('http://www.php-fig.org/psr/psr-2/'); | ||
243 | $bookmark->addAdditionalContentEntry( | ||
244 | 'search_highlight', | ||
245 | ['url' => [ | ||
246 | ['start' => 0, 'end' => 4], // http | ||
247 | ['start' => 15, 'end' => 18], // fig | ||
248 | ['start' => 27, 'end' => 33], // "psr-2/" | ||
249 | ]] | ||
250 | ); | ||
251 | |||
252 | $link = $this->formatter->format($bookmark); | ||
253 | |||
254 | $this->assertSame( | ||
255 | '<span class="search-highlight">http</span>://www.php-' . | ||
256 | '<span class="search-highlight">fig</span>.org/psr/' . | ||
257 | '<span class="search-highlight">psr-2/</span>', | ||
258 | $link['url_html'] | ||
259 | ); | ||
260 | } | ||
261 | |||
262 | /** | ||
263 | * Test formatTagListHtml with search result highlight. | ||
264 | */ | ||
265 | public function testFormatTagListHtmlWithSearchHighlight(): void | ||
266 | { | ||
267 | $this->formatter = new BookmarkDefaultFormatter($this->conf, false); | ||
268 | |||
269 | $bookmark = new Bookmark(); | ||
270 | $bookmark->setTagsString('coding-style standards quality assurance'); | ||
271 | $bookmark->addAdditionalContentEntry( | ||
272 | 'search_highlight', | ||
273 | ['tags' => [ | ||
274 | ['start' => 0, 'end' => 12], // coding-style | ||
275 | ['start' => 23, 'end' => 30], // quality | ||
276 | ['start' => 31, 'end' => 40], // assurance | ||
277 | ],] | ||
278 | ); | ||
279 | |||
280 | $link = $this->formatter->format($bookmark); | ||
281 | |||
282 | $this->assertSame( | ||
283 | [ | ||
284 | '<span class="search-highlight">coding-style</span>', | ||
285 | 'standards', | ||
286 | '<span class="search-highlight">quality</span>', | ||
287 | '<span class="search-highlight">assurance</span>', | ||
288 | ], | ||
289 | $link['taglist_html'] | ||
290 | ); | ||
291 | } | ||
177 | } | 292 | } |
diff --git a/tests/legacy/LegacyLinkDBTest.php b/tests/legacy/LegacyLinkDBTest.php index df2cad62..5c3fd425 100644 --- a/tests/legacy/LegacyLinkDBTest.php +++ b/tests/legacy/LegacyLinkDBTest.php | |||
@@ -296,6 +296,10 @@ class LegacyLinkDBTest extends \Shaarli\TestCase | |||
296 | // They need to be grouped with the first case found - order by date DESC: `sTuff`. | 296 | // They need to be grouped with the first case found - order by date DESC: `sTuff`. |
297 | 'sTuff' => 2, | 297 | 'sTuff' => 2, |
298 | 'ut' => 1, | 298 | 'ut' => 1, |
299 | 'assurance' => 1, | ||
300 | 'coding-style' => 1, | ||
301 | 'quality' => 1, | ||
302 | 'standards' => 1, | ||
299 | ), | 303 | ), |
300 | self::$publicLinkDB->linksCountPerTag() | 304 | self::$publicLinkDB->linksCountPerTag() |
301 | ); | 305 | ); |
@@ -324,6 +328,10 @@ class LegacyLinkDBTest extends \Shaarli\TestCase | |||
324 | 'tag3' => 1, | 328 | 'tag3' => 1, |
325 | 'tag4' => 1, | 329 | 'tag4' => 1, |
326 | 'ut' => 1, | 330 | 'ut' => 1, |
331 | 'assurance' => 1, | ||
332 | 'coding-style' => 1, | ||
333 | 'quality' => 1, | ||
334 | 'standards' => 1, | ||
327 | ), | 335 | ), |
328 | self::$privateLinkDB->linksCountPerTag() | 336 | self::$privateLinkDB->linksCountPerTag() |
329 | ); | 337 | ); |
@@ -544,6 +552,10 @@ class LegacyLinkDBTest extends \Shaarli\TestCase | |||
544 | 'tag4' => 1, | 552 | 'tag4' => 1, |
545 | 'ut' => 1, | 553 | 'ut' => 1, |
546 | 'w3c' => 1, | 554 | 'w3c' => 1, |
555 | 'assurance' => 1, | ||
556 | 'coding-style' => 1, | ||
557 | 'quality' => 1, | ||
558 | 'standards' => 1, | ||
547 | ]; | 559 | ]; |
548 | $tags = self::$privateLinkDB->linksCountPerTag(); | 560 | $tags = self::$privateLinkDB->linksCountPerTag(); |
549 | 561 | ||
diff --git a/tests/utils/ReferenceLinkDB.php b/tests/utils/ReferenceLinkDB.php index fc3cb109..1f53dc3c 100644 --- a/tests/utils/ReferenceLinkDB.php +++ b/tests/utils/ReferenceLinkDB.php | |||
@@ -82,7 +82,7 @@ class ReferenceLinkDB | |||
82 | 'This guide extends and expands on PSR-1, the basic coding standard.', | 82 | 'This guide extends and expands on PSR-1, the basic coding standard.', |
83 | 0, | 83 | 0, |
84 | DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_152312'), | 84 | DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_152312'), |
85 | '' | 85 | 'coding-style standards quality assurance' |
86 | ); | 86 | ); |
87 | 87 | ||
88 | $this->addLink( | 88 | $this->addLink( |