]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - tests/formatter/BookmarkDefaultFormatterTest.php
Default formatter: add a setting to disable auto-linkification
[github/shaarli/Shaarli.git] / tests / formatter / BookmarkDefaultFormatterTest.php
index fe42a2087e7615b196a37defafd67c6fdf5035d4..4fcc5dd19cc35356498f6acc7270b80ad4aef9bc 100644 (file)
@@ -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,11 +25,11 @@ 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);
-        $this->formatter = new BookmarkDefaultFormatter($this->conf);
+        $this->formatter = new BookmarkDefaultFormatter($this->conf, true);
     }
 
     /**
@@ -123,7 +123,7 @@ class BookmarkDefaultFormatterTest extends TestCase
         $description[0] = 'This a &lt;strong&gt;description&lt;/strong&gt;<br />';
         $url = 'https://sub.domain.tld?query=here&amp;for=real#hash';
         $description[1] = 'text <a href="'. $url .'">'. $url .'</a> more text<br />';
-        $description[2] = 'Also, there is an <a href="?addtag=hashtag" '.
+        $description[2] = 'Also, there is an <a href="./add-tag/hashtag" '.
             'title="Hashtag hashtag">#hashtag</a> added<br />';
         $description[3] = '&nbsp; &nbsp; A &nbsp;N &nbsp;D KEEP &nbsp; &nbsp; '.
             'SPACES &nbsp; &nbsp;! &nbsp; <br />';
@@ -148,9 +148,165 @@ class BookmarkDefaultFormatterTest extends TestCase
         $this->assertEquals($root . $short, $link['url']);
         $this->assertEquals($root . $short, $link['real_url']);
         $this->assertEquals(
-            'Text <a href="'. $root .'?addtag=hashtag" title="Hashtag hashtag">'.
+            'Text <a href="'. $root .'./add-tag/hashtag" title="Hashtag hashtag">'.
             '#hashtag</a> more text',
             $link['description']
         );
     }
+
+    /**
+     * Make sure that private tags are properly filtered out when the user is logged out.
+     */
+    public function testFormatTagListRemovePrivate(): void
+    {
+        $this->formatter = new BookmarkDefaultFormatter($this->conf, false);
+
+        $bookmark = new Bookmark();
+        $bookmark->setId($id = 11);
+        $bookmark->setTags($tags = ['bookmark', '.private', 'othertag']);
+
+        $link = $this->formatter->format($bookmark);
+
+        unset($tags[1]);
+        $tags = array_values($tags);
+
+        $this->assertSame(11, $link['id']);
+        $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(
+            '<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']
+        );
+    }
+
+    /**
+     * 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!<br />' . PHP_EOL . 'https://thisisaurl.tld &nbsp;#hashtag',
+            $link['description']
+        );
+    }
 }