From f1a148ab92c061ac129b5b2976de02d45b6a71e7 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Mon, 12 Oct 2020 12:23:57 +0200 Subject: add search highlight unit tests --- tests/api/controllers/links/GetLinksTest.php | 2 +- tests/bookmark/BookmarkFileServiceTest.php | 16 ++++ tests/bookmark/BookmarkFilterTest.php | 40 +++++++- tests/formatter/BookmarkDefaultFormatterTest.php | 115 +++++++++++++++++++++++ tests/legacy/LegacyLinkDBTest.php | 12 +++ tests/utils/ReferenceLinkDB.php | 2 +- 6 files changed, 184 insertions(+), 3 deletions(-) (limited to 'tests') 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 $response = $this->controller->getLinks($request, new Response()); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode((string) $response->getBody(), true); - $this->assertEquals(4, count($data)); + $this->assertEquals(5, count($data)); $this->assertEquals(6, $data[0]['id']); // 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 // They need to be grouped with the first case found - order by date DESC: `sTuff`. 'sTuff' => 2, 'ut' => 1, + 'assurance' => 1, + 'coding-style' => 1, + 'quality' => 1, + 'standards' => 1, ], $this->publicLinkDB->bookmarksCountPerTag() ); @@ -776,6 +780,10 @@ class BookmarkFileServiceTest extends TestCase 'tag3' => 1, 'tag4' => 1, 'ut' => 1, + 'assurance' => 1, + 'coding-style' => 1, + 'quality' => 1, + 'standards' => 1, ], $this->privateLinkDB->bookmarksCountPerTag() ); @@ -918,6 +926,10 @@ class BookmarkFileServiceTest extends TestCase 'tag4' => 1, 'ut' => 1, 'w3c' => 1, + 'assurance' => 1, + 'coding-style' => 1, + 'quality' => 1, + 'standards' => 1, ]; $tags = $this->privateLinkDB->bookmarksCountPerTag(); @@ -1016,6 +1028,10 @@ class BookmarkFileServiceTest extends TestCase 'stallman' => 1, 'ut' => 1, 'w3c' => 1, + 'assurance' => 1, + 'coding-style' => 1, + 'quality' => 1, + 'standards' => 1, ]; $bookmark = new Bookmark(); $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 @@ namespace Shaarli\Bookmark; -use Exception; use malkusch\lock\mutex\NoMutex; use ReferenceLinkDB; use Shaarli\Config\ConfigManager; @@ -525,4 +524,43 @@ class BookmarkFilterTest extends TestCase )) ); } + + /** + * Test search result highlights in every field of bookmark reference #9. + */ + public function testFullTextSearchHighlight(): void + { + $bookmarks = self::$linkFilter->filter( + BookmarkFilter::$FILTER_TEXT, + '"psr-2" coding guide http fig "psr-2/" "This guide" basic standard. coding-style quality assurance' + ); + + static::assertCount(1, $bookmarks); + static::assertArrayHasKey(9, $bookmarks); + + $bookmark = $bookmarks[9]; + $expectedHighlights = [ + 'title' => [ + ['start' => 0, 'end' => 5], // "psr-2" + ['start' => 7, 'end' => 13], // coding + ['start' => 20, 'end' => 25], // guide + ], + 'description' => [ + ['start' => 0, 'end' => 10], // "This guide" + ['start' => 45, 'end' => 50], // basic + ['start' => 58, 'end' => 67], // standard. + ], + 'url' => [ + ['start' => 0, 'end' => 4], // http + ['start' => 15, 'end' => 18], // fig + ['start' => 27, 'end' => 33], // "psr-2/" + ], + 'tags' => [ + ['start' => 0, 'end' => 12], // coding-style + ['start' => 23, 'end' => 30], // quality + ['start' => 31, 'end' => 40], // assurance + ], + ]; + static::assertSame($expectedHighlights, $bookmark->getAdditionalContentEntry('search_highlight')); + } } 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 $this->assertSame($tags, $link['taglist']); $this->assertSame(implode(' ', $tags), $link['tags']); } + + /** + * Test formatTitleHtml with search result highlight. + */ + public function testFormatTitleHtmlWithSearchHighlight(): void + { + $this->formatter = new BookmarkDefaultFormatter($this->conf, false); + + $bookmark = new Bookmark(); + $bookmark->setTitle('PSR-2: Coding Style Guide'); + $bookmark->addAdditionalContentEntry( + 'search_highlight', + ['title' => [ + ['start' => 0, 'end' => 5], // "psr-2" + ['start' => 7, 'end' => 13], // coding + ['start' => 20, 'end' => 25], // guide + ]] + ); + + $link = $this->formatter->format($bookmark); + + $this->assertSame( + 'PSR-2: ' . + 'Coding Style ' . + 'Guide', + $link['title_html'] + ); + } + + /** + * Test formatDescription with search result highlight. + */ + public function testFormatDescriptionWithSearchHighlight(): void + { + $this->formatter = new BookmarkDefaultFormatter($this->conf, false); + + $bookmark = new Bookmark(); + $bookmark->setDescription('This guide extends and expands on PSR-1, the basic coding standard.'); + $bookmark->addAdditionalContentEntry( + 'search_highlight', + ['description' => [ + ['start' => 0, 'end' => 10], // "This guide" + ['start' => 45, 'end' => 50], // basic + ['start' => 58, 'end' => 67], // standard. + ]] + ); + + $link = $this->formatter->format($bookmark); + + $this->assertSame( + 'This guide extends and expands on PSR-1, the ' . + 'basic coding ' . + 'standard.', + $link['description'] + ); + } + + /** + * Test formatUrlHtml with search result highlight. + */ + public function testFormatUrlHtmlWithSearchHighlight(): void + { + $this->formatter = new BookmarkDefaultFormatter($this->conf, false); + + $bookmark = new Bookmark(); + $bookmark->setUrl('http://www.php-fig.org/psr/psr-2/'); + $bookmark->addAdditionalContentEntry( + 'search_highlight', + ['url' => [ + ['start' => 0, 'end' => 4], // http + ['start' => 15, 'end' => 18], // fig + ['start' => 27, 'end' => 33], // "psr-2/" + ]] + ); + + $link = $this->formatter->format($bookmark); + + $this->assertSame( + 'http://www.php-' . + 'fig.org/psr/' . + 'psr-2/', + $link['url_html'] + ); + } + + /** + * Test formatTagListHtml with search result highlight. + */ + public function testFormatTagListHtmlWithSearchHighlight(): void + { + $this->formatter = new BookmarkDefaultFormatter($this->conf, false); + + $bookmark = new Bookmark(); + $bookmark->setTagsString('coding-style standards quality assurance'); + $bookmark->addAdditionalContentEntry( + 'search_highlight', + ['tags' => [ + ['start' => 0, 'end' => 12], // coding-style + ['start' => 23, 'end' => 30], // quality + ['start' => 31, 'end' => 40], // assurance + ],] + ); + + $link = $this->formatter->format($bookmark); + + $this->assertSame( + [ + 'coding-style', + 'standards', + 'quality', + 'assurance', + ], + $link['taglist_html'] + ); + } } 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 // They need to be grouped with the first case found - order by date DESC: `sTuff`. 'sTuff' => 2, 'ut' => 1, + 'assurance' => 1, + 'coding-style' => 1, + 'quality' => 1, + 'standards' => 1, ), self::$publicLinkDB->linksCountPerTag() ); @@ -324,6 +328,10 @@ class LegacyLinkDBTest extends \Shaarli\TestCase 'tag3' => 1, 'tag4' => 1, 'ut' => 1, + 'assurance' => 1, + 'coding-style' => 1, + 'quality' => 1, + 'standards' => 1, ), self::$privateLinkDB->linksCountPerTag() ); @@ -544,6 +552,10 @@ class LegacyLinkDBTest extends \Shaarli\TestCase 'tag4' => 1, 'ut' => 1, 'w3c' => 1, + 'assurance' => 1, + 'coding-style' => 1, + 'quality' => 1, + 'standards' => 1, ]; $tags = self::$privateLinkDB->linksCountPerTag(); 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 'This guide extends and expands on PSR-1, the basic coding standard.', 0, DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_152312'), - '' + 'coding-style standards quality assurance' ); $this->addLink( -- cgit v1.2.3