From 2fdb54859405868a8406ecfbc5d9ddd423f757eb Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Fri, 18 Sep 2009 02:53:46 +0000 Subject: [PATCH] Merged Text::ContextSummary() changes from branches/2.3 - r82035 and r82036 git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@86669 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/fieldtypes/Text.php | 10 ++-------- tests/fieldtypes/TextTest.php | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/core/model/fieldtypes/Text.php b/core/model/fieldtypes/Text.php index 8f93f8162..0790d9cc1 100644 --- a/core/model/fieldtypes/Text.php +++ b/core/model/fieldtypes/Text.php @@ -262,18 +262,12 @@ class Text extends DBField { } $summary = substr($text, $position, $characters); - $stringPieces = explode(' ', $string); if($highlight) { - // Add a span around all occurences of the search term - $summary = str_ireplace($string, "$string", $summary); - // Add a span around all key words from the search term as well - if($stringPieces) { - foreach($stringPieces as $stringPiece) { - $summary = str_ireplace($stringPiece, "$stringPiece", $summary); - } + if($stringPieces) foreach($stringPieces as $stringPiece) { + $summary = str_ireplace($stringPiece, "$stringPiece", $summary); } } diff --git a/tests/fieldtypes/TextTest.php b/tests/fieldtypes/TextTest.php index 2f6490e89..233d6c77c 100644 --- a/tests/fieldtypes/TextTest.php +++ b/tests/fieldtypes/TextTest.php @@ -104,5 +104,25 @@ class TextTest extends SapphireTest { } } + function testContextSummary() { + $testString1 = '

This is some text. It is a test

'; + $testKeywords1 = 'test'; + $testString2 = '

This is some test text. Test test what if you have multiple keywords.

'; + $testKeywords2 = 'some test'; + $textObj = DBField::create('Text', $testString1, 'Text'); + + $this->assertEquals( + 'text. It is a test', + $textObj->ContextSummary(20, $testKeywords1) + ); + + $textObj->setValue($testString2); + + $this->assertEquals( + 'This is some test text. test test what if you have', + $textObj->ContextSummary(50, $testKeywords2) + ); + } + } -?> \ No newline at end of file +?>