BUGFIX Fixed double up of <span> highlight tags around keywords in Text::ContextSummary()

MINOR Added test for Text::ContextSummary()



git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@82035 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sean Harvey 2009-07-16 06:09:43 +00:00 committed by Sam Minnee
parent 6b35a34f91
commit 5ffd79cb79
2 changed files with 22 additions and 8 deletions

View File

@ -274,18 +274,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, "<span class=\"highlight\">$string</span>", $summary);
// Add a span around all key words from the search term as well
if($stringPieces) {
foreach($stringPieces as $stringPiece) {
$summary = str_ireplace($stringPiece, "<span class=\"highlight\">$stringPiece</span>", $summary);
}
if($stringPieces) foreach($stringPieces as $stringPiece) {
$summary = str_ireplace($stringPiece, "<span class=\"highlight\">$stringPiece</span>", $summary);
}
}

View File

@ -66,5 +66,25 @@ class TextTest extends SapphireTest {
}
}
function testContextSummary() {
$testString1 = '<p>This is &quot;some text&quot;. It is a test</p>';
$testKeywords1 = 'test';
$testString2 = '<p>This is some test text. Test test what if you have multiple keywords.</p>';
$testKeywords2 = 'some test';
$textObj = DBField::create('Text', $testString1, 'Text');
$this->assertEquals(
'text. It is a <span class="highlight">test</span>',
$textObj->ContextSummary(20, $testKeywords1)
);
$textObj->setValue($testString2);
$this->assertEquals(
'This is <span class="highlight">some</span> <span class="highlight">test</span> text. <span class="highlight">test</span> <span class="highlight">test</span> what if you have',
$textObj->ContextSummary(50, $testKeywords2)
);
}
}
?>