From 358fbd418eeec0923e91eff3500c7aa0dc998276 Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Tue, 16 Dec 2008 10:51:09 +0000 Subject: [PATCH] BUGFIX Fixed Text->LimitWordCount() not returning the correct number of words in the truncated text. MINOR Added TextTest for testing Text class methods git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@69220 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/fieldtypes/Text.php | 7 ++--- tests/fieldtypes/TextTest.php | 49 ++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 tests/fieldtypes/TextTest.php diff --git a/core/model/fieldtypes/Text.php b/core/model/fieldtypes/Text.php index f2db5f8b7..782e43f11 100644 --- a/core/model/fieldtypes/Text.php +++ b/core/model/fieldtypes/Text.php @@ -23,13 +23,14 @@ class Text extends DBField { * {@link LimitWordCountXML()} instead. * * @param int $numWords Number of words to limit by + * @param string $add Ellipsis to add to the end of truncated string * @return string */ function LimitWordCount($numWords = 26, $add = '...') { $this->value = Convert::xml2raw($this->value); - $ret = explode(' ', $this->value, $numWords); + $ret = explode(' ', $this->value, $numWords + 1); - if(count($ret) < $numWords - 1) { + if(count($ret) <= $numWords - 1) { $ret = $this->value; } else { array_pop($ret); @@ -56,7 +57,7 @@ class Text extends DBField { * has the potential to return malformed HTML. * * @param int $limit Number of characters to limit by - * @param string $add Ellipsis to add to the end of limited string + * @param string $add Ellipsis to add to the end of truncated string * @return string */ function LimitCharacters($limit = 20, $add = "...") { diff --git a/tests/fieldtypes/TextTest.php b/tests/fieldtypes/TextTest.php new file mode 100644 index 000000000..73f91c4e0 --- /dev/null +++ b/tests/fieldtypes/TextTest.php @@ -0,0 +1,49 @@ +LimitWordCount()} + */ + function testLimitWordCount() { + $cases = array( + /* Standard words limited, ellipsis added if truncated */ + 'The little brown fox jumped over the lazy cow.' => 'The little brown...', + + /* Words less than the limt word count don't get truncated, ellipsis not added */ + 'Two words' => 'Two words', // Two words shouldn't have an ellipsis + 'One' => 'One', // Neither should one word + '' => '', // No words produces nothing! + + /* HTML tags get stripped out, leaving the raw text */ + '

Text inside a paragraph tag should also work

' => 'Text inside a...', + '

Text nested inside another tag should also work

' => 'Text nested inside...' + ); + + foreach($cases as $originalValue => $expectedValue) { + $textObj = new Text('Test'); + $textObj->setValue($originalValue); + $this->assertEquals($expectedValue, $textObj->LimitWordCount(3)); + } + } + + /** + * Test {@link Text->LimitWordCountXML()} + */ + /*function testLimitWordCountXML() { + $cases = array( + // @todo add cases to test by + ); + + foreach($cases as $originalValue => $expectedValue) { + $textObj = new Text('Test'); + $textObj->setValue($originalValue); + $this->assertEquals($expectedValue, $textObj->LimitWordCountXML(3)); + } + }*/ + +} +?> \ No newline at end of file