diff --git a/api/JSONDataFormatter.php b/api/JSONDataFormatter.php index eb2eed9d3..bbc54119d 100644 --- a/api/JSONDataFormatter.php +++ b/api/JSONDataFormatter.php @@ -56,7 +56,7 @@ class JSONDataFormatter extends DataFormatter { // Field filtering if($fields && !in_array($fieldName, $fields)) continue; - $fieldValue = $obj->$fieldName; + $fieldValue = $obj->obj($fieldName)->forTemplate(); $serobj->$fieldName = $fieldValue; } diff --git a/api/XMLDataFormatter.php b/api/XMLDataFormatter.php index 1b6a338aa..29584613d 100644 --- a/api/XMLDataFormatter.php +++ b/api/XMLDataFormatter.php @@ -49,8 +49,7 @@ class XMLDataFormatter extends DataFormatter { foreach($this->getFieldsForObj($obj) as $fieldName => $fieldType) { // Field filtering if($fields && !in_array($fieldName, $fields)) continue; - - $fieldValue = $obj->$fieldName; + $fieldValue = $obj->obj($fieldName)->forTemplate(); if(!mb_check_encoding($fieldValue,'utf-8')) $fieldValue = "(data is badly encoded)"; if(is_object($fieldValue) && is_subclass_of($fieldValue, 'Object') && $fieldValue->hasMethod('toXML')) { diff --git a/tests/api/XMLDataFormatterTest.php b/tests/api/XMLDataFormatterTest.php index 4aa8329de..f1921abc9 100644 --- a/tests/api/XMLDataFormatterTest.php +++ b/tests/api/XMLDataFormatterTest.php @@ -1,8 +1,21 @@ register('test_shortcode', array($this, 'shortcodeSaver')); + + parent::setUp(); + } + + public function tearDown() { + ShortcodeParser::get_active()->unregister('test_shortcode'); + + parent::tearDown(); + } + protected $extraDataObjects = array( 'XMLDataFormatterTest_DataObject' ); @@ -28,6 +41,36 @@ class XMLDataFormatterTest extends SapphireTest { ); } + public function testShortcodesInDataObject() { + $formatter = new XMLDataFormatter(); + + $page = new XMLDataFormatterTest_DataObject(); + $page->Content = 'This is some test content [test_shortcode]test[/test_shortcode]'; + + $xml = new SimpleXMLElement('' . $formatter->convertDataObjectWithoutHeader($page)); + $this->assertEquals('This is some test content test', $xml->Content); + + $page->Content = '[test_shortcode,id=-1]'; + $xml = new SimpleXMLElement('' . $formatter->convertDataObjectWithoutHeader($page)); + $this->assertEmpty('', $xml->Content); + + $page->Content = '[bad_code,id=1]'; + + $xml = new SimpleXMLElement('' . $formatter->convertDataObjectWithoutHeader($page)); + $this->assertContains('[bad_code,id=1]', $xml->Content); + } + + /** + * Stores the result of a shortcode parse in object properties for easy testing access. + */ + public function shortcodeSaver($arguments, $content = null, $parser, $tagName = null) { + $this->arguments = $arguments; + $this->contents = $content; + $this->tagName = $tagName; + + return $content; + } + } class XMLDataFormatterTest_DataObject extends DataObject implements TestOnly {