From ed21f8423658eff738629b351e0896cfed89d1ff Mon Sep 17 00:00:00 2001 From: Mark Scherer Date: Sat, 2 May 2015 14:24:02 +0200 Subject: [PATCH] Backport _xmlOptions --- lib/Cake/Test/Case/View/XmlViewTest.php | 71 +++++++++++++++++++++++++ lib/Cake/View/XmlView.php | 7 +++ 2 files changed, 78 insertions(+) diff --git a/lib/Cake/Test/Case/View/XmlViewTest.php b/lib/Cake/Test/Case/View/XmlViewTest.php index 89399beef..d90c057ae 100644 --- a/lib/Cake/Test/Case/View/XmlViewTest.php +++ b/lib/Cake/Test/Case/View/XmlViewTest.php @@ -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 diff --git a/lib/Cake/View/XmlView.php b/lib/Cake/View/XmlView.php index 385543373..021854fc4 100644 --- a/lib/Cake/View/XmlView.php +++ b/lib/Cake/View/XmlView.php @@ -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; }