fixing XmlView, XmlException: Invalid input was raised when _serialize is string and data is numerically indexed.

This commit is contained in:
Ceeram 2012-05-23 11:39:02 +02:00
parent 0bfcd49249
commit 25c7a27495
2 changed files with 34 additions and 10 deletions

View file

@ -43,9 +43,27 @@ class XmlViewTest extends CakeTestCase {
$View = new XmlView($Controller); $View = new XmlView($Controller);
$output = $View->render(false); $output = $View->render(false);
$expected = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . '<users><user>user1</user><user>user2</user></users>'; $this->assertSame(Xml::build($data)->asXML(), $output);
$this->assertTextEquals($expected, trim($output)); $this->assertSame('application/xml', $Response->type());
$this->assertIdentical('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);
} }
/** /**
@ -100,9 +118,12 @@ class XmlViewTest extends CakeTestCase {
$View = new XmlView($Controller); $View = new XmlView($Controller);
$output = $View->render('index'); $output = $View->render('index');
$expected = '<?xml version="1.0" encoding="UTF-8"?><users><user>user1</user><user>user2</user></users>'; $expected = array(
$this->assertIdentical($expected, str_replace(array("\r", "\n"), '', $output)); 'users'=> array('user'=> array('user1', 'user2'))
$this->assertIdentical('application/xml', $Response->type()); );
$expected = Xml::build($expected)->asXML();
$this->assertSame($expected, $output);
$this->assertSame('application/xml', $Response->type());
$this->assertInstanceOf('HelperCollection', $View->Helpers); $this->assertInstanceOf('HelperCollection', $View->Helpers);
} }

View file

@ -93,6 +93,9 @@ class XmlView extends View {
} }
} else { } else {
$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null; $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(); $content = Xml::fromArray($data)->asXML();
$this->Blocks->set('content', $content); $this->Blocks->set('content', $content);