Skip to content

Commit 25c7a27

Browse files
committed
fixing XmlView, XmlException: Invalid input was raised when _serialize is string and data is numerically indexed.
1 parent 0bfcd49 commit 25c7a27

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

lib/Cake/Test/Case/View/XmlViewTest.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,27 @@ public function testRenderWithoutView() {
4343
$View = new XmlView($Controller);
4444
$output = $View->render(false);
4545

46-
$expected = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . '<users><user>user1</user><user>user2</user></users>';
47-
$this->assertTextEquals($expected, trim($output));
48-
$this->assertIdentical('application/xml', $Response->type());
46+
$this->assertSame(Xml::build($data)->asXML(), $output);
47+
$this->assertSame('application/xml', $Response->type());
48+
49+
$data = array(
50+
array(
51+
'User' => array(
52+
'username' => 'user1'
53+
)
54+
),
55+
array(
56+
'User' => array(
57+
'username' => 'user2'
58+
)
59+
)
60+
);
61+
$Controller->set(array('users' => $data, '_serialize' => 'users'));
62+
$View = new XmlView($Controller);
63+
$output = $View->render(false);
64+
65+
$expected = Xml::build(array('response'=> array('users'=> $data)))->asXML();
66+
$this->assertSame($expected, $output);
4967
}
5068

5169
/**
@@ -100,9 +118,12 @@ public function testRenderWithView() {
100118
$View = new XmlView($Controller);
101119
$output = $View->render('index');
102120

103-
$expected = '<?xml version="1.0" encoding="UTF-8"?><users><user>user1</user><user>user2</user></users>';
104-
$this->assertIdentical($expected, str_replace(array("\r", "\n"), '', $output));
105-
$this->assertIdentical('application/xml', $Response->type());
121+
$expected = array(
122+
'users'=> array('user'=> array('user1', 'user2'))
123+
);
124+
$expected = Xml::build($expected)->asXML();
125+
$this->assertSame($expected, $output);
126+
$this->assertSame('application/xml', $Response->type());
106127
$this->assertInstanceOf('HelperCollection', $View->Helpers);
107128
}
108129

lib/Cake/View/XmlView.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
* `$this->set(array('posts' => $posts, '_serialize' => 'posts'));`
2828
*
29-
* When the view is rendered, the `$posts` view variable will be serialized
29+
* When the view is rendered, the `$posts` view variable will be serialized
3030
* into XML.
3131
*
3232
* **Note** The view variable you specify must be compatible with Xml::fromArray().
@@ -38,7 +38,7 @@
3838
* $this->set(compact('posts', 'users', 'stuff'));
3939
* $this->set('_serialize', array('posts', 'users'));
4040
* }}}
41-
*
41+
*
4242
* The above would generate a XML object that looks like:
4343
*
4444
* `<response><posts>...</posts><users>...</users></response>`
@@ -75,8 +75,8 @@ public function __construct(Controller $controller = null) {
7575
* Render a XML view.
7676
*
7777
* Uses the special '_serialize' parameter to convert a set of
78-
* view variables into a XML response. Makes generating simple
79-
* XML responses very easy. You can omit the '_serialize' parameter,
78+
* view variables into a XML response. Makes generating simple
79+
* XML responses very easy. You can omit the '_serialize' parameter,
8080
* and use a normal view + layout as well.
8181
*
8282
* @param string $view The view being rendered.
@@ -93,6 +93,9 @@ public function render($view = null, $layout = null) {
9393
}
9494
} else {
9595
$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
96+
if (is_array($data) && Set::numeric(array_keys($data))) {
97+
$data = array('response' => array($serialize => $data));
98+
}
9699
}
97100
$content = Xml::fromArray($data)->asXML();
98101
$this->Blocks->set('content', $content);

0 commit comments

Comments
 (0)