X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=tests%2Fformatter%2FBookmarkDefaultFormatterTest.php;h=983960b6ad77979c8b22ac5ca9a32d4559946a29;hb=9ef8555ad298668bcb8537ccdd2ab6560f44177f;hp=382a560efcb2785ecbf357d26c466fa6ff18ccf1;hpb=a39acb2518f272df8a601af72c13eabe2719dcb8;p=github%2Fshaarli%2FShaarli.git diff --git a/tests/formatter/BookmarkDefaultFormatterTest.php b/tests/formatter/BookmarkDefaultFormatterTest.php index 382a560e..983960b6 100644 --- a/tests/formatter/BookmarkDefaultFormatterTest.php +++ b/tests/formatter/BookmarkDefaultFormatterTest.php @@ -3,9 +3,9 @@ namespace Shaarli\Formatter; use DateTime; -use PHPUnit\Framework\TestCase; use Shaarli\Bookmark\Bookmark; use Shaarli\Config\ConfigManager; +use Shaarli\TestCase; /** * Class BookmarkDefaultFormatterTest @@ -25,7 +25,7 @@ class BookmarkDefaultFormatterTest extends TestCase /** * Initialize formatter instance. */ - public function setUp() + protected function setUp(): void { copy('tests/utils/config/configJson.json.php', self::$testConf .'.json.php'); $this->conf = new ConfigManager(self::$testConf); @@ -123,7 +123,7 @@ class BookmarkDefaultFormatterTest extends TestCase $description[0] = 'This a <strong>description</strong>
'; $url = 'https://sub.domain.tld?query=here&for=real#hash'; $description[1] = 'text '. $url .' more text
'; - $description[2] = 'Also, there is an #hashtag added
'; $description[3] = '    A  N  D KEEP     '. 'SPACES    !  
'; @@ -148,7 +148,7 @@ class BookmarkDefaultFormatterTest extends TestCase $this->assertEquals($root . $short, $link['url']); $this->assertEquals($root . $short, $link['real_url']); $this->assertEquals( - 'Text '. + 'Text '. '#hashtag more text', $link['description'] ); @@ -174,4 +174,146 @@ 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.' . PHP_EOL . + 'https://www.php-fig.org/psr/psr-1/' + ); + $bookmark->addAdditionalContentEntry( + 'search_highlight', + ['description' => [ + ['start' => 0, 'end' => 10], // "This guide" + ['start' => 45, 'end' => 50], // basic + ['start' => 58, 'end' => 67], // standard. + ['start' => 84, 'end' => 87], // fig + ]] + ); + + $link = $this->formatter->format($bookmark); + + $this->assertSame( + 'This guide extends and expands on PSR-1, the ' . + 'basic coding ' . + 'standard.
' . PHP_EOL . + '' . + 'https://www.php-fig.org/psr/psr-1/' . + '', + $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'] + ); + } + + /** + * 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'] + ); + } }