BUGFIX #3228 Fixed undefined offset error in Text::BigSummary() if trying to summarise text that is smaller than the requested word limit

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@85076 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sean Harvey 2009-08-24 06:16:56 +00:00
parent 19769e3841
commit a1d20b8ae9
2 changed files with 22 additions and 4 deletions

View File

@ -181,7 +181,6 @@ class Text extends DBField {
return ""; return "";
$sentences = explode( '.', $data ); $sentences = explode( '.', $data );
$count = count( explode( ' ', $sentences[0] ) ); $count = count( explode( ' ', $sentences[0] ) );
// if the first sentence is too long, show only the first $maxWords words // if the first sentence is too long, show only the first $maxWords words
@ -190,13 +189,15 @@ class Text extends DBField {
} }
// add each sentence while there are enough words to do so // add each sentence while there are enough words to do so
do { do {
$result .= trim(array_shift( $sentences )).'. ' ; $result .= trim(array_shift($sentences));
if($sentences) {
$result .= '. ';
$count += count(explode(' ', $sentences[0])); $count += count(explode(' ', $sentences[0]));
}
// Ensure that we don't trim half way through a tag or a link // Ensure that we don't trim half way through a tag or a link
$brokenLink = (substr_count($result,'<') != substr_count($result,'>')) || $brokenLink = (substr_count($result,'<') != substr_count($result,'>')) ||
(substr_count($result,'<a') != substr_count($result,'</a')); (substr_count($result,'<a') != substr_count($result,'</a'));
} while( ($count < $maxWords || $brokenLink) && $sentences && trim( $sentences[0] ) ); } while( ($count < $maxWords || $brokenLink) && $sentences && trim( $sentences[0] ) );
if( preg_match( '/<a[^>]*>/', $result ) && !preg_match( '/<\/a>/', $result ) ) if( preg_match( '/<a[^>]*>/', $result ) && !preg_match( '/<\/a>/', $result ) )

View File

@ -87,5 +87,22 @@ class TextTest extends SapphireTest {
} }
} }
/**
* Test {@link Text->BigSummary()}
*/
function testBigSummary() {
$cases = array(
'This text has multiple sentences. Big Summary uses this to split sentences up.' => 'This text has multiple...',
'This text does not have multiple sentences' => 'This text does not...',
'Very short' => 'Very short',
'' => ''
);
foreach($cases as $originalValue => $expectedValue) {
$textObj = DBField::create('Text', $originalValue);
$this->assertEquals($expectedValue, $textObj->BigSummary(4));
}
}
} }
?> ?>