Merge pull request #3521 from halkyon/bigsummary_plain_fix

BUG Text::BigSummary() fails with undefined $data when $plain = false
This commit is contained in:
Loz Calver 2014-10-25 23:30:36 +01:00
commit a77ca1995a
2 changed files with 29 additions and 11 deletions

View File

@ -159,14 +159,14 @@ class Text extends StringField {
* Performs the same function as the big summary, but doesn't trim new paragraphs off data. * Performs the same function as the big summary, but doesn't trim new paragraphs off data.
* Caution: Not XML/HTML-safe - does not respect closing tags. * Caution: Not XML/HTML-safe - does not respect closing tags.
*/ */
public function BigSummary($maxWords = 50, $plain = 1) { public function BigSummary($maxWords = 50, $plain = true) {
$result = ""; $result = '';
// get first sentence? // get first sentence?
// this needs to be more robust // this needs to be more robust
if($plain) $data = Convert::xml2raw($this->value, true); $data = $plain ? Convert::xml2raw($this->value, true) : $this->value;
if(!$data) return ""; if(!$data) return '';
$sentences = explode('.', $data); $sentences = explode('.', $data);
$count = count(explode(' ', $sentences[0])); $count = count(explode(' ', $sentences[0]));

View File

@ -112,21 +112,39 @@ class TextTest extends SapphireTest {
/** /**
* Test {@link Text->BigSummary()} * Test {@link Text->BigSummary()}
*/ */
public function testBigSummary() { public function testBigSummaryPlain() {
$cases = array( $cases = array(
'This text has multiple sentences. Big Summary uses this to split sentences up.' '<p>This text has multiple sentences. Big Summary uses this to split sentences up.</p>'
=> 'This text has multiple...', => 'This text has multiple...',
'This text does not have multiple sentences' => 'This text does not...', 'This text does not have multiple sentences' => 'This text does not...',
'Very short' => 'Very short', 'Very short' => 'Very short',
'' => '' '' => ''
); );
foreach($cases as $originalValue => $expectedValue) { foreach($cases as $originalValue => $expectedValue) {
$textObj = DBField::create_field('Text', $originalValue); $textObj = DBField::create_field('Text', $originalValue);
$this->assertEquals($expectedValue, $textObj->BigSummary(4)); $this->assertEquals($expectedValue, $textObj->BigSummary(4, true));
} }
} }
/**
* Test {@link Text->BigSummary()}
*/
public function testBigSummary() {
$cases = array(
'<strong>This</strong> text has multiple sentences. Big Summary uses this to split sentences up.</p>'
=> '<strong>This</strong> 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_field('Text', $originalValue);
$this->assertEquals($expectedValue, $textObj->BigSummary(4, false));
}
}
public function testContextSummary() { public function testContextSummary() {
$testString1 = '<p>This is some text. It is a test</p>'; $testString1 = '<p>This is some text. It is a test</p>';
$testKeywords1 = 'test'; $testKeywords1 = 'test';