summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Seidl2018-01-20 10:34:04 (GMT)
committerThomas Seidl2018-01-20 10:34:04 (GMT)
commit31a4e8250bbbba465b908bf70f0a8c53f73953f8 (patch)
tree993b81053a7152d602fe4f8829339bc4a59cfe35
parent627d0f78f77777deda05f72148d21c1c52bc0437 (diff)
Issue #2932347 by drunken monkey, ghaya: Fixed case insensitive matching for highlighting non-ASCII text.
-rw-r--r--CHANGELOG.txt2
-rw-r--r--src/Plugin/search_api/processor/Highlight.php3
-rw-r--r--tests/src/Unit/Processor/HighlightTest.php44
3 files changed, 43 insertions, 6 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index 6081acd..e28ebe7 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -1,5 +1,7 @@
Search API 1.x, dev (xxxx-xx-xx):
---------------------------------
+- #2932347 by drunken monkey, ghaya: Fixed case insensitive matching for
+ highlighting non-ASCII text.
- #2933309 by drunken monkey, mkalkbrenner: Made some helper methods in the
CommandHelper class public.
- #2928279 by drunken monkey, borisson_: Added a test for the default tracker
diff --git a/src/Plugin/search_api/processor/Highlight.php b/src/Plugin/search_api/processor/Highlight.php
index 4346d64..9e23eb7 100644
--- a/src/Plugin/search_api/processor/Highlight.php
+++ b/src/Plugin/search_api/processor/Highlight.php
@@ -480,7 +480,8 @@ class Highlight extends ProcessorPluginBase implements PluginFormInterface {
}
}
else {
- $found_position = stripos($text, $key, $look_start[$key]);
+ $function = function_exists('mb_stripos') ? 'mb_stripos' : 'stripos';
+ $found_position = $function($text, $key, $look_start[$key]);
}
if ($found_position !== FALSE) {
$look_start[$key] = $found_position + 1;
diff --git a/tests/src/Unit/Processor/HighlightTest.php b/tests/src/Unit/Processor/HighlightTest.php
index 074fc67..b62be7a 100644
--- a/tests/src/Unit/Processor/HighlightTest.php
+++ b/tests/src/Unit/Processor/HighlightTest.php
@@ -295,8 +295,17 @@ class HighlightTest extends UnitTestCase {
/**
* Tests highlighting of partial matches.
+ *
+ * @param string $text
+ * The text that should be highlighted.
+ * @param string $keywords
+ * The search keywords.
+ * @param string $highlighted
+ * The expected highlighted text.
+ *
+ * @dataProvider postprocessSearchResultsHighlightPartialDataProvider
*/
- public function testPostprocessSearchResultsHighlightPartial() {
+ public function testPostprocessSearchResultsHighlightPartial($text, $keywords, $highlighted) {
$this->processor->setConfiguration(['highlight_partial' => TRUE]);
$query = $this->getMock(QueryInterface::class);
@@ -305,7 +314,7 @@ class HighlightTest extends UnitTestCase {
->willReturn(QueryInterface::PROCESSING_FULL);
$query->expects($this->atLeastOnce())
->method('getOriginalKeys')
- ->will($this->returnValue(['#conjunction' => 'AND', 'partial']));
+ ->will($this->returnValue(['#conjunction' => 'AND', $keywords]));
/** @var \Drupal\search_api\Query\QueryInterface $query */
$field = $this->createTestField('body', 'entity:node/body');
@@ -316,11 +325,10 @@ class HighlightTest extends UnitTestCase {
$this->processor->setIndex($this->index);
- $body_values = ['Some longwordtoshowpartialmatching value'];
$fields = [
'entity:node/body' => [
'type' => 'text',
- 'values' => $body_values,
+ 'values' => [$text],
],
];
@@ -333,7 +341,33 @@ class HighlightTest extends UnitTestCase {
$this->processor->postprocessSearchResults($results);
$fields = $items[$this->itemIds[0]]->getExtraData('highlighted_fields');
- $this->assertEquals('Some longwordtoshow<strong>partial</strong>matching value', $fields['body'][0], 'Highlighting is correctly applied to a partial match.');
+ $this->assertEquals($highlighted, $fields['body'][0], 'Highlighting is correctly applied to a partial match.');
+ $excerpt = $items[$this->itemIds[0]]->getExcerpt();
+ $this->assertEquals("… $highlighted …", $excerpt, 'Highlighting is correctly applied to a partial match.');
+ }
+
+ /**
+ * Provides test data sets for testPostprocessSearchResultsHighlightPartial().
+ *
+ * @return array[]
+ * An array of argument arrays for
+ * testPostprocessSearchResultsHighlightPartial().
+ *
+ * @see \Drupal\Tests\search_api\Unit\Processor\HighlightTest::testPostprocessSearchResultsHighlightPartial()
+ */
+ public function postprocessSearchResultsHighlightPartialDataProvider() {
+ return [
+ 'normal' => [
+ 'Some longwordtoshowpartialmatching value',
+ 'partial',
+ 'Some longwordtoshow<strong>partial</strong>matching value',
+ ],
+ 'multi-byte' => [
+ 'Alle Angaben ohne Gewähr.',
+ 'Ähr',
+ 'Alle Angaben ohne Gew<strong>ähr</strong>.',
+ ],
+ ];
}
/**