Skip to content

Commit

Permalink
Update getHighlightedSnippet to return first non-empty snippet (vufin…
Browse files Browse the repository at this point in the history
  • Loading branch information
meganschanz authored Jul 23, 2024
1 parent b7fa59c commit 99dec40
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 11 deletions.
24 changes: 15 additions & 9 deletions module/VuFind/src/VuFind/RecordDriver/SolrDefault.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
];
}
}
}

Expand All @@ -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),
];
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 99dec40

Please sign in to comment.