Backport _xmlOptions

This commit is contained in:
Mark Scherer 2015-05-02 14:24:02 +02:00
parent bcb403078d
commit ed21f84236
2 changed files with 78 additions and 0 deletions

View file

@ -96,7 +96,78 @@ class XmlViewTest extends CakeTestCase {
$this->assertFalse(isset($View->Html), 'No helper loaded.');
}
/**
* Test that rendering with _serialize respects XML options.
*
* @return void
*/
public function testRenderSerializeWithOptions() {
$Request = new CakeRequest();
$Response = new CakeResponse();
$Controller = new Controller($Request, $Response);
$data = [
'_serialize' => ['tags'],
'_xmlOptions' => ['format' => 'attributes'],
'tags' => [
'tag' => [
[
'id' => '1',
'name' => 'defect'
],
[
'id' => '2',
'name' => 'enhancement'
]
]
]
];
$Controller->set($data);
$Controller->viewClass = 'Xml';
$View = new XmlView($Controller);
$result = $View->render();
$expected = Xml::build(['response' => ['tags' => $data['tags']]], $data['_xmlOptions'])->asXML();
$this->assertSame($expected, $result);
}
/**
* Test that rendering with _serialize can work with string setting.
*
* @return void
*/
public function testRenderSerializeWithString() {
$Request = new CakeRequest();
$Response = new CakeResponse();
$Controller = new Controller($Request, $Response);
$data = [
'_serialize' => 'tags',
'_xmlOptions' => ['format' => 'attributes'],
'tags' => [
'tags' => [
'tag' => [
[
'id' => '1',
'name' => 'defect'
],
[
'id' => '2',
'name' => 'enhancement'
]
]
]
]
];
$Controller->set($data);
$Controller->viewClass = 'Xml';
$View = new XmlView($Controller);
$result = $View->render();
$expected = Xml::build($data['tags'], $data['_xmlOptions'])->asXML();
$this->assertSame($expected, $result);
}
/**
* Test render with an array in _serialize
*
* @return void

View file

@ -109,6 +109,10 @@ class XmlView extends View {
/**
* Serialize view vars.
*
* ### Special parameters
* `_xmlOptions` You can set an array of custom options for Xml::fromArray() this way, e.g.
* 'format' as 'attributes' instead of 'tags'.
*
* @param array $serialize The viewVars that need to be serialized.
* @return string The serialized data
*/
@ -131,6 +135,9 @@ class XmlView extends View {
}
$options = array();
if (isset($this->viewVars['_xmlOptions'])) {
$options = $this->viewVars['_xmlOptions'];
}
if (Configure::read('debug')) {
$options['pretty'] = true;
}