Skip to content

Commit

Permalink
fixing XmlView, XmlException: Invalid input was raised when _serializ…
Browse files Browse the repository at this point in the history
…e is string and data is numerically indexed.
  • Loading branch information
ceeram committed May 29, 2012
1 parent 0bfcd49 commit 25c7a27
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
33 changes: 27 additions & 6 deletions lib/Cake/Test/Case/View/XmlViewTest.php
Expand Up @@ -43,9 +43,27 @@ public function testRenderWithoutView() {
$View = new XmlView($Controller);
$output = $View->render(false);

$expected = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . '<users><user>user1</user><user>user2</user></users>';
$this->assertTextEquals($expected, trim($output));
$this->assertIdentical('application/xml', $Response->type());
$this->assertSame(Xml::build($data)->asXML(), $output);
$this->assertSame('application/xml', $Response->type());

$data = array(
array(
'User' => array(
'username' => 'user1'
)
),
array(
'User' => array(
'username' => 'user2'
)
)
);
$Controller->set(array('users' => $data, '_serialize' => 'users'));
$View = new XmlView($Controller);
$output = $View->render(false);

$expected = Xml::build(array('response'=> array('users'=> $data)))->asXML();
$this->assertSame($expected, $output);
}

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

$expected = '<?xml version="1.0" encoding="UTF-8"?><users><user>user1</user><user>user2</user></users>';
$this->assertIdentical($expected, str_replace(array("\r", "\n"), '', $output));
$this->assertIdentical('application/xml', $Response->type());
$expected = array(
'users'=> array('user'=> array('user1', 'user2'))
);
$expected = Xml::build($expected)->asXML();
$this->assertSame($expected, $output);
$this->assertSame('application/xml', $Response->type());
$this->assertInstanceOf('HelperCollection', $View->Helpers);
}

Expand Down
11 changes: 7 additions & 4 deletions lib/Cake/View/XmlView.php
Expand Up @@ -26,7 +26,7 @@
*
* `$this->set(array('posts' => $posts, '_serialize' => 'posts'));`
*
* When the view is rendered, the `$posts` view variable will be serialized
* When the view is rendered, the `$posts` view variable will be serialized
* into XML.
*
* **Note** The view variable you specify must be compatible with Xml::fromArray().
Expand All @@ -38,7 +38,7 @@
* $this->set(compact('posts', 'users', 'stuff'));
* $this->set('_serialize', array('posts', 'users'));
* }}}
*
*
* The above would generate a XML object that looks like:
*
* `<response><posts>...</posts><users>...</users></response>`
Expand Down Expand Up @@ -75,8 +75,8 @@ public function __construct(Controller $controller = null) {
* Render a XML view.
*
* Uses the special '_serialize' parameter to convert a set of
* view variables into a XML response. Makes generating simple
* XML responses very easy. You can omit the '_serialize' parameter,
* view variables into a XML response. Makes generating simple
* XML responses very easy. You can omit the '_serialize' parameter,
* and use a normal view + layout as well.
*
* @param string $view The view being rendered.
Expand All @@ -93,6 +93,9 @@ public function render($view = null, $layout = null) {
}
} else {
$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
if (is_array($data) && Set::numeric(array_keys($data))) {
$data = array('response' => array($serialize => $data));
}
}
$content = Xml::fromArray($data)->asXML();
$this->Blocks->set('content', $content);
Expand Down

0 comments on commit 25c7a27

Please sign in to comment.