mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Backport _xmlOptions
This commit is contained in:
parent
bcb403078d
commit
ed21f84236
2 changed files with 78 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue