add search highlight unit tests

This commit is contained in:
ArthurHoaro 2020-10-12 12:23:57 +02:00
parent 4e3875c0ce
commit f1a148ab92
6 changed files with 184 additions and 3 deletions

View file

@ -398,7 +398,7 @@ public function testGetLinksSearchTags()
$response = $this->controller->getLinks($request, new Response()); $response = $this->controller->getLinks($request, new Response());
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
$data = json_decode((string) $response->getBody(), true); $data = json_decode((string) $response->getBody(), true);
$this->assertEquals(4, count($data)); $this->assertEquals(5, count($data));
$this->assertEquals(6, $data[0]['id']); $this->assertEquals(6, $data[0]['id']);
// wildcard: placeholder at the middle // wildcard: placeholder at the middle

View file

@ -748,6 +748,10 @@ public function testAllTags()
// They need to be grouped with the first case found - order by date DESC: `sTuff`. // They need to be grouped with the first case found - order by date DESC: `sTuff`.
'sTuff' => 2, 'sTuff' => 2,
'ut' => 1, 'ut' => 1,
'assurance' => 1,
'coding-style' => 1,
'quality' => 1,
'standards' => 1,
], ],
$this->publicLinkDB->bookmarksCountPerTag() $this->publicLinkDB->bookmarksCountPerTag()
); );
@ -776,6 +780,10 @@ public function testAllTags()
'tag3' => 1, 'tag3' => 1,
'tag4' => 1, 'tag4' => 1,
'ut' => 1, 'ut' => 1,
'assurance' => 1,
'coding-style' => 1,
'quality' => 1,
'standards' => 1,
], ],
$this->privateLinkDB->bookmarksCountPerTag() $this->privateLinkDB->bookmarksCountPerTag()
); );
@ -918,6 +926,10 @@ public function testCountLinkPerTagAllNoFilter()
'tag4' => 1, 'tag4' => 1,
'ut' => 1, 'ut' => 1,
'w3c' => 1, 'w3c' => 1,
'assurance' => 1,
'coding-style' => 1,
'quality' => 1,
'standards' => 1,
]; ];
$tags = $this->privateLinkDB->bookmarksCountPerTag(); $tags = $this->privateLinkDB->bookmarksCountPerTag();
@ -1016,6 +1028,10 @@ public function testCountTagsNoMarkdown()
'stallman' => 1, 'stallman' => 1,
'ut' => 1, 'ut' => 1,
'w3c' => 1, 'w3c' => 1,
'assurance' => 1,
'coding-style' => 1,
'quality' => 1,
'standards' => 1,
]; ];
$bookmark = new Bookmark(); $bookmark = new Bookmark();
$bookmark->setTags(['newTagToCount', BookmarkMarkdownFormatter::NO_MD_TAG]); $bookmark->setTags(['newTagToCount', BookmarkMarkdownFormatter::NO_MD_TAG]);

View file

@ -2,7 +2,6 @@
namespace Shaarli\Bookmark; namespace Shaarli\Bookmark;
use Exception;
use malkusch\lock\mutex\NoMutex; use malkusch\lock\mutex\NoMutex;
use ReferenceLinkDB; use ReferenceLinkDB;
use Shaarli\Config\ConfigManager; use Shaarli\Config\ConfigManager;
@ -525,4 +524,43 @@ public function testFilterByHashtag()
)) ))
); );
} }
/**
* 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'));
}
} }

View file

@ -174,4 +174,119 @@ public function testFormatTagListRemovePrivate(): void
$this->assertSame($tags, $link['taglist']); $this->assertSame($tags, $link['taglist']);
$this->assertSame(implode(' ', $tags), $link['tags']); $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(
'<span class="search-highlight">PSR-2</span>: ' .
'<span class="search-highlight">Coding</span> Style ' .
'<span class="search-highlight">Guide</span>',
$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(
'<span class="search-highlight">This guide</span> extends and expands on PSR-1, the ' .
'<span class="search-highlight">basic</span> coding ' .
'<span class="search-highlight">standard.</span>',
$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(
'<span class="search-highlight">http</span>://www.php-' .
'<span class="search-highlight">fig</span>.org/psr/' .
'<span class="search-highlight">psr-2/</span>',
$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(
[
'<span class="search-highlight">coding-style</span>',
'standards',
'<span class="search-highlight">quality</span>',
'<span class="search-highlight">assurance</span>',
],
$link['taglist_html']
);
}
} }

View file

@ -296,6 +296,10 @@ public function testAllTags()
// They need to be grouped with the first case found - order by date DESC: `sTuff`. // They need to be grouped with the first case found - order by date DESC: `sTuff`.
'sTuff' => 2, 'sTuff' => 2,
'ut' => 1, 'ut' => 1,
'assurance' => 1,
'coding-style' => 1,
'quality' => 1,
'standards' => 1,
), ),
self::$publicLinkDB->linksCountPerTag() self::$publicLinkDB->linksCountPerTag()
); );
@ -324,6 +328,10 @@ public function testAllTags()
'tag3' => 1, 'tag3' => 1,
'tag4' => 1, 'tag4' => 1,
'ut' => 1, 'ut' => 1,
'assurance' => 1,
'coding-style' => 1,
'quality' => 1,
'standards' => 1,
), ),
self::$privateLinkDB->linksCountPerTag() self::$privateLinkDB->linksCountPerTag()
); );
@ -544,6 +552,10 @@ public function testCountLinkPerTagAllNoFilter()
'tag4' => 1, 'tag4' => 1,
'ut' => 1, 'ut' => 1,
'w3c' => 1, 'w3c' => 1,
'assurance' => 1,
'coding-style' => 1,
'quality' => 1,
'standards' => 1,
]; ];
$tags = self::$privateLinkDB->linksCountPerTag(); $tags = self::$privateLinkDB->linksCountPerTag();

View file

@ -82,7 +82,7 @@ public function __construct($isLegacy = false)
'This guide extends and expands on PSR-1, the basic coding standard.', 'This guide extends and expands on PSR-1, the basic coding standard.',
0, 0,
DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_152312'), DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_152312'),
'' 'coding-style standards quality assurance'
); );
$this->addLink( $this->addLink(