Updating Xml class to always use #document as the very root node and to create a node below it if the root option is set.

Adding test. 
Fixes #6294

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8147 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
davidpersson 2009-04-17 13:26:07 +00:00
parent bd7bd5d891
commit bbfee7ba17
2 changed files with 29 additions and 3 deletions

View file

@ -823,13 +823,19 @@ class Xml extends XmlNode {
$this->{$key} = $options[$key]; $this->{$key} = $options[$key];
} }
$this->__tags = $options['tags']; $this->__tags = $options['tags'];
parent::__construct($options['root']); parent::__construct('#document');
if ($options['root'] !== '#document') {
$Root = $this->createNode($options['root']);
} else {
$Root =& $this;
}
if (!empty($input)) { if (!empty($input)) {
if (is_string($input)) { if (is_string($input)) {
$this->load($input); $Root->load($input);
} elseif (is_array($input) || is_object($input)) { } elseif (is_array($input) || is_object($input)) {
$this->append($input, $options); $Root->append($input, $options);
} }
} }
// if (Configure::read('App.encoding') !== null) { // if (Configure::read('App.encoding') !== null) {

View file

@ -256,6 +256,26 @@ class XmlTest extends CakeTestCase {
$result = $xml->toString(array('header' => false, 'cdata' => false)); $result = $xml->toString(array('header' => false, 'cdata' => false));
$this->assertEqual($expected, $result); $this->assertEqual($expected, $result);
} }
/**
* Prove that serialization with a given root node works
* as expected.
*
* @access public
* @return void
* @link https://trac.cakephp.org/ticket/6294
*/
function testArraySerializationWithRoot() {
$input = array(
array('Shirt' => array('id' => 1, 'color' => 'green')),
array('Shirt' => array('id' => 2, 'color' => 'blue')),
);
$expected = '<collection><shirt id="1" color="green" />';
$expected .= '<shirt id="2" color="blue" /></collection>';
$Xml = new Xml($input, array('root' => 'collection'));
$result = $Xml->toString(array('header' => false));
$this->assertEqual($expected, $result);
}
/** /**
* testCloneNode * testCloneNode
* *