]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
add search highlight unit tests 1588/head
authorArthurHoaro <arthur@hoa.ro>
Mon, 12 Oct 2020 10:23:57 +0000 (12:23 +0200)
committerArthurHoaro <arthur@hoa.ro>
Fri, 16 Oct 2020 18:31:49 +0000 (20:31 +0200)
tests/api/controllers/links/GetLinksTest.php
tests/bookmark/BookmarkFileServiceTest.php
tests/bookmark/BookmarkFilterTest.php
tests/formatter/BookmarkDefaultFormatterTest.php
tests/legacy/LegacyLinkDBTest.php
tests/utils/ReferenceLinkDB.php

index 0f5073b47197470491982856b167529b86310bec..b1c46ee2816853b45a5a8abfcac28f8c71f4b973 100644 (file)
@@ -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
index 42485c99b3fb0a829c384494854b6ba87e4579ad..daafd2503369169500923895a1f5c6d625e5875a 100644 (file)
@@ -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]);
index 644abbc8429f18b51c64c6a64a6cac7d50fc7598..574d8e3f270e5a4f80638dfc78db4779860eb39d 100644 (file)
@@ -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'));
+    }
 }
index 9534436e3f7e7150c19a50022be476a36754f6b2..3fc6f8dc58f4c8f38e877f1c49c18d2d083cbcde 100644 (file)
@@ -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(
+            '<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']
+        );
+    }
 }
index df2cad62295594145843eac725bff46bec8068e3..5c3fd425f968c856b31d1e76dc96c47666be55d7 100644 (file)
@@ -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();
 
index fc3cb1094930eb28f8c0aefa3f6b17e75654b3c3..1f53dc3cd60337d2c6cbda01616ef88e7fad014b 100644 (file)
@@ -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(