diff --git a/module/VuFind/src/VuFind/RecordDriver/SolrDefault.php b/module/VuFind/src/VuFind/RecordDriver/SolrDefault.php index 7e7c62f47ab..6a9949eb3a4 100644 --- a/module/VuFind/src/VuFind/RecordDriver/SolrDefault.php +++ b/module/VuFind/src/VuFind/RecordDriver/SolrDefault.php @@ -234,11 +234,13 @@ public function getHighlightedSnippet() if ($this->snippet) { // First check for preferred fields: foreach ($this->preferredSnippetFields as $current) { - if (isset($this->highlightDetails[$current][0])) { - return [ - 'snippet' => $this->highlightDetails[$current][0], - 'caption' => $this->getSnippetCaption($current), - ]; + foreach ($this->highlightDetails[$current] ?? [] as $hl) { + if (!empty($hl)) { + return [ + 'snippet' => $hl, + 'caption' => $this->getSnippetCaption($current), + ]; + } } } @@ -249,10 +251,14 @@ public function getHighlightedSnippet() ) { foreach ($this->highlightDetails as $key => $value) { if ($value && !in_array($key, $this->forbiddenSnippetFields)) { - return [ - 'snippet' => $value[0], - 'caption' => $this->getSnippetCaption($key), - ]; + foreach ($value as $hl) { + if (!empty($hl)) { + return [ + 'snippet' => $hl, + 'caption' => $this->getSnippetCaption($key), + ]; + } + } } } } diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/RecordDriver/SolrDefaultTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/RecordDriver/SolrDefaultTest.php index fc6aaafa330..40aa890fa9e 100644 --- a/module/VuFind/tests/unit-tests/src/VuFindTest/RecordDriver/SolrDefaultTest.php +++ b/module/VuFind/tests/unit-tests/src/VuFindTest/RecordDriver/SolrDefaultTest.php @@ -208,15 +208,97 @@ public function testGetHighlightedTitle() } /** - * Test getHighlightedSnippet for a record. + * Test getHighlightedSnippet for an empty record. * * @return void */ - public function testGetHighlightedSnippet() + public function testEmptyGetHighlightedSnippet() { $this->assertEquals(false, $this->getDriver()->getHighlightedSnippet()); } + /** + * Test getHighlightedSnippet for a record when empty snippet data is given. + * + * @return void + */ + public function testGetHighlightedSnippetAllEmpty() + { + $overrides = [ + 'General' => ['snippets' => true], + ]; + $driver = $this->getDriver([], $overrides); + $details = ['topic' => ['']]; + $driver->setHighlightDetails($details); + $this->assertEquals(false, $driver->getHighlightedSnippet()); + } + + /** + * Test getHighlightedSnippet for a record when the first snippet is empty. + * + * @return void + */ + public function testGetHighlightedSnippetFirstEmpty() + { + $overrides = [ + 'General' => ['snippets' => true], + ]; + $driver = $this->getDriver([], $overrides); + // Note that the first topic result is empty, it should return the first non-empty one + $details = ['topic' => ['', 'Testing {{{{START_HILITE}}}}Snippets{{{{END_HILITE}}}} highlighting']]; + $driver->setHighlightDetails($details); + $this->assertEquals( + ['snippet' => 'Testing {{{{START_HILITE}}}}Snippets{{{{END_HILITE}}}} highlighting', 'caption' => false], + $driver->getHighlightedSnippet() + ); + } + + /** + * Test getHighlightedSnippet for a record when multiple preferred snippet fields exist. + * + * @return void + */ + public function testGetHighlightedSnippetInPreferredFieldOrder() + { + $overrides = [ + 'General' => ['snippets' => true], + ]; + $driver = $this->getDriver([], $overrides); + $details = [ + 'topic' => ['', 'Testing topic {{{{START_HILITE}}}}snippet{{{{END_HILITE}}}}'], + 'contents' => ['Testing content {{{{START_HILITE}}}}snippet{{{{END_HILITE}}}}'], + ]; + $driver->setHighlightDetails($details); + // Should return the snippet from contents since that is the first item in preferredSnippetFields + $this->assertEquals( + ['snippet' => 'Testing content {{{{START_HILITE}}}}snippet{{{{END_HILITE}}}}', 'caption' => false], + $driver->getHighlightedSnippet() + ); + } + + /** + * Test getHighlightedSnippet for a record when no preferred snippet fields exist. + * + * @return void + */ + public function testGetHighlightedSnippetNonForbiddenField() + { + $overrides = [ + 'General' => ['snippets' => true], + ]; + $driver = $this->getDriver([], $overrides); + $details = [ + 'author' => ['', 'Testing author {{{{START_HILITE}}}}snippet{{{{END_HILITE}}}}'], + 'toast' => ['Testing toast {{{{START_HILITE}}}}snippet{{{{END_HILITE}}}}'], + ]; + $driver->setHighlightDetails($details); + // Should ignore the 'author' snippet since that is forbidden, and return 'toast' instead + $this->assertEquals( + ['snippet' => 'Testing toast {{{{START_HILITE}}}}snippet{{{{END_HILITE}}}}', 'caption' => false], + $driver->getHighlightedSnippet() + ); + } + /** * Test HighlightDetails for a record. *