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/formatter/BookmarkDefaultFormatterTest.php | 115 +++++++++++++++++++++++ 1 file changed, 115 insertions(+) (limited to 'tests/formatter/BookmarkDefaultFormatterTest.php') 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'] + ); + } } -- cgit v1.2.3 From 740b32b520e6b1723512c6f9b78cef6575b1725b Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 3 Nov 2020 12:38:38 +0100 Subject: Default formatter: add a setting to disable auto-linkification + update documentation + single parameter for both URL and hashtags Fixes #1094 --- tests/formatter/BookmarkDefaultFormatterTest.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'tests/formatter/BookmarkDefaultFormatterTest.php') diff --git a/tests/formatter/BookmarkDefaultFormatterTest.php b/tests/formatter/BookmarkDefaultFormatterTest.php index 3fc6f8dc..4fcc5dd1 100644 --- a/tests/formatter/BookmarkDefaultFormatterTest.php +++ b/tests/formatter/BookmarkDefaultFormatterTest.php @@ -289,4 +289,24 @@ class BookmarkDefaultFormatterTest extends TestCase $link['taglist_html'] ); } + + /** + * Test default formatting with formatter_settings.autolink set to false: + * URLs and hashtags should not be transformed + */ + public function testFormatDescriptionWithoutLinkification(): void + { + $this->conf->set('formatter_settings.autolink', false); + $this->formatter = new BookmarkDefaultFormatter($this->conf, false); + + $bookmark = new Bookmark(); + $bookmark->setDescription('Hi!' . PHP_EOL . 'https://thisisaurl.tld #hashtag'); + + $link = $this->formatter->format($bookmark); + + static::assertSame( + 'Hi!
' . PHP_EOL . 'https://thisisaurl.tld  #hashtag', + $link['description'] + ); + } } -- cgit v1.2.3