Skip to content

Commit

Permalink
BUGFIX Casting return values on text helper methods in StringField, T…
Browse files Browse the repository at this point in the history
…ext, Varchar
  • Loading branch information
chillu committed Jan 31, 2012
1 parent 252e187 commit 0085876
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 3 deletions.
21 changes: 20 additions & 1 deletion core/model/fieldtypes/HTMLText.php
Expand Up @@ -28,6 +28,25 @@ function LimitCharacters($limit = 20, $add = "...") {
return (strlen($value) > $limit) ? substr($value, 0, $limit) . $add : $value;
}

static $casting = array(
"AbsoluteLinks" => "HTMLText",
"BigSummary" => "HTMLText",
"ContextSummary" => "HTMLText",
"FirstParagraph" => "HTMLText",
"FirstSentence" => "HTMLText",
"LimitCharacters" => "HTMLText",
"LimitSentences" => "HTMLText",
"Lower" => "HTMLText",
"LowerCase" => "HTMLText",
"Summary" => "HTMLText",
"Upper" => "HTMLText",
"UpperCase" => "HTMLText",
'EscapeXML' => 'HTMLText',
'LimitWordCount' => 'HTMLText',
'LimitWordCountXML' => 'HTMLText',
'NoHTML' => 'Text',
);

/**
* Create a summary of the content. This will be some section of the first paragraph, limited by
* $maxWords. All internal tags are stripped out - the return value is a string
Expand Down Expand Up @@ -133,4 +152,4 @@ public function scaffoldSearchField($title = null) {

}

?>
?>
6 changes: 6 additions & 0 deletions core/model/fieldtypes/StringField.php
Expand Up @@ -9,6 +9,12 @@
abstract class StringField extends DBField {
protected $nullifyEmpty = true;

static $casting = array(
"LimitCharacters" => "Text",
"Lower" => "Text",
"Upper" => "Text",
);

/**
* Construct a string type field with a set of optional parameters
* @param $name string The name of the field
Expand Down
14 changes: 13 additions & 1 deletion core/model/fieldtypes/Text.php
Expand Up @@ -17,8 +17,20 @@
* @subpackage model
*/
class Text extends StringField {

static $casting = array(
"AbsoluteLinks" => "HTMLText",
"AbsoluteLinks" => "Text",
"BigSummary" => "Text",
"ContextSummary" => "Text",
"FirstParagraph" => "Text",
"FirstSentence" => "Text",
"LimitCharacters" => "Text",
"LimitSentences" => "Text",
"Summary" => "Text",
'EscapeXML' => 'Text',
'LimitWordCount' => 'Text',
'LimitWordCountXML' => 'HTMLText',
'NoHTML' => 'Text',
);

/**
Expand Down
5 changes: 5 additions & 0 deletions core/model/fieldtypes/Varchar.php
Expand Up @@ -10,6 +10,11 @@
* @subpackage model
*/
class Varchar extends StringField {

static $casting = array(
"Initial" => "Text",
"URL" => "Text",
);

protected $size;

Expand Down
28 changes: 28 additions & 0 deletions tests/fieldtypes/HTMLTextTest.php
Expand Up @@ -102,5 +102,33 @@ function testFirstSentence() {
$this->assertEquals($match, $textObj->FirstSentence());
}
}

public function testRAW() {
$data = DBField::create('HTMLText', 'This & This');
$this->assertEquals($data->RAW(), 'This & This');

$data = DBField::create('HTMLText', 'This & This');
$this->assertEquals($data->RAW(), 'This & This');
}

public function testXML() {
$data = DBField::create('HTMLText', 'This & This');
$this->assertEquals($data->XML(), 'This & This');
}

public function testHTML() {
$data = DBField::create('HTMLText', 'This & This');
$this->assertEquals($data->HTML(), 'This & This');
}

public function testJS() {
$data = DBField::create('HTMLText', '"this is a test"');
$this->assertEquals($data->JS(), '\"this is a test\"');
}

public function testATT() {
$data = DBField::create('HTMLText', '"this is a test"');
$this->assertEquals($data->ATT(), '"this is a test"');
}
}
?>
26 changes: 25 additions & 1 deletion tests/fieldtypes/TextTest.php
Expand Up @@ -142,6 +142,30 @@ function testContextSummary() {
'A dog <span class="highlight">ate</span> a cat while looking at a Foobar',
$textObj->ContextSummary(100, $testKeyword3a)
);

}

public function testRAW() {
$data = DBField::create('Text', 'This &amp; This');
$this->assertEquals($data->RAW(), 'This &amp; This');
}

public function testXML() {
$data = DBField::create('Text', 'This & This');
$this->assertEquals($data->XML(), 'This &amp; This');
}

public function testHTML() {
$data = DBField::create('Text', 'This & This');
$this->assertEquals($data->HTML(), 'This &amp; This');
}

public function testJS() {
$data = DBField::create('Text', '"this is a test"');
$this->assertEquals($data->JS(), '\"this is a test\"');
}

public function testATT() {
$data = DBField::create('Text', '"this is a test"');
$this->assertEquals($data->ATT(), '&quot;this is a test&quot;');
}
}

1 comment on commit 0085876

@sminnee
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding casting is benign, so this is benign. The increased exec time should be okay, and if not, site devs should use partial caching! ;-)

Please sign in to comment.