From d8b4ca91d90bc8ee85786558ff4fbe669a73872c Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Mon, 6 Nov 2017 12:08:29 +1300 Subject: [PATCH] FIX: Fix ContextSummary behaviour with UTF8 chars Fixes https://github.com/silverstripe/silverstripe-framework/issues/1396 Ensure that DBTextTest has UTF8 test cases --- src/ORM/FieldType/DBText.php | 10 +++++----- tests/php/ORM/DBTextTest.php | 24 +++++++++++++++++++++++- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/ORM/FieldType/DBText.php b/src/ORM/FieldType/DBText.php index 1e6066727..494d21986 100644 --- a/src/ORM/FieldType/DBText.php +++ b/src/ORM/FieldType/DBText.php @@ -198,7 +198,7 @@ class DBText extends DBString $keywords = Convert::raw2xml($keywords); // Find the search string - $position = (int) stripos($text, $keywords); + $position = (int) mb_stripos($text, $keywords); // We want to search string to be in the middle of our block to give it some context $position = max(0, $position - ($characters / 2)); @@ -206,19 +206,19 @@ class DBText extends DBString if ($position > 0) { // We don't want to start mid-word $position = max( - (int) strrpos(substr($text, 0, $position), ' '), - (int) strrpos(substr($text, 0, $position), "\n") + (int) mb_strrpos(substr($text, 0, $position), ' '), + (int) mb_strrpos(substr($text, 0, $position), "\n") ); } - $summary = substr($text, $position, $characters); + $summary = mb_substr($text, $position, $characters); $stringPieces = explode(' ', $keywords); if ($highlight) { // Add a span around all key words from the search term as well if ($stringPieces) { foreach ($stringPieces as $stringPiece) { - if (strlen($stringPiece) > 2) { + if (mb_strlen($stringPiece) > 2) { // Maintain case of original string $summary = preg_replace( '/' . preg_quote($stringPiece, '/') . '/i', diff --git a/tests/php/ORM/DBTextTest.php b/tests/php/ORM/DBTextTest.php index e5985cc33..2fde108a0 100644 --- a/tests/php/ORM/DBTextTest.php +++ b/tests/php/ORM/DBTextTest.php @@ -41,6 +41,7 @@ class DBTextTest extends SapphireTest ['The little brown fox jumped over the lazy cow.', 'The little brown fox...'], ['

Short & Sweet

', '<p>Short & Sweet</p>'], ['This text contains & in it', 'This text contains &...'], + ['Is an umault in schön?', 'Is an umault in schö...'], ]; } @@ -80,7 +81,10 @@ class DBTextTest extends SapphireTest ['

Lorem ipsum dolor sit amet

', 24, '<p>Lorem ipsum dolor...'], ['

Lorem ipsum dolor sit amet

', 24, '<p><span>Lorem ipsum...'], ['

Lorem ipsum

', 24, '<p>Lorem ipsum</p>'], - ['Lorem & ipsum dolor sit amet', 24, 'Lorem &amp; ipsum dolor...'] + ['Lorem & ipsum dolor sit amet', 24, 'Lorem &amp; ipsum dolor...'], + + ['Is an umault in schön or not?', 22, 'Is an umault in schön...'], + ]; } @@ -124,6 +128,9 @@ class DBTextTest extends SapphireTest // If storing HTML you should use DBHTMLText instead ['

Text inside a paragraph tag should also work

', 3, '<p>Text inside a...'], ['

Two words

', 3, '<p>Two words</p>'], + + // Check UTF8 + ['Is an umault in schön or not?', 5, 'Is an umault in schön...'], ]; } @@ -156,6 +163,9 @@ class DBTextTest extends SapphireTest // If storing HTML you should use DBHTMLText instead ['

First sentence.

', 2, '<p>First sentence.</p>'], ['

First sentence. Second sentence. Third sentence

', 2, '<p>First sentence. Second sentence.'], + + // Check UTF8 + ['Is schön. Isn\'t schön.', 1, 'Is schön.'], ]; } @@ -187,6 +197,9 @@ class DBTextTest extends SapphireTest // If storing HTML you should use DBHTMLText instead ['
First sentence.', '<br />First sentence.'], ['

First sentence. Second sentence. Third sentence

', '<p>First sentence.'], + + // Check UTF8 + ['Is schön. Isn\'t schön.', 'Is schön.'], ]; } @@ -243,7 +256,16 @@ class DBTextTest extends SapphireTest 'ate', // it should highlight 3 letters or more. 'A dog ate a cat while looking at a Foobar', + ], + [ + 'both schön and können have umlauts', + 21, + 'schön', + // check UTF8 support + 'both schön and können...', ] + + ]; }