Skip to content

Commit

Permalink
Updating Xml class to always use #document as the very root node and …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
mariuswilms committed Apr 17, 2009
1 parent bd7bd5d commit bbfee7b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
12 changes: 9 additions & 3 deletions cake/libs/xml.php
Expand Up @@ -823,13 +823,19 @@ function __construct($input = null, $options = array()) {
$this->{$key} = $options[$key];
}
$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 (is_string($input)) {
$this->load($input);
$Root->load($input);
} elseif (is_array($input) || is_object($input)) {
$this->append($input, $options);
$Root->append($input, $options);
}
}
// if (Configure::read('App.encoding') !== null) {
Expand Down
20 changes: 20 additions & 0 deletions cake/tests/cases/libs/xml.test.php
Expand Up @@ -256,6 +256,26 @@ function testNestedArraySerialization() {
$result = $xml->toString(array('header' => false, 'cdata' => false));
$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
*
Expand Down

0 comments on commit bbfee7b

Please sign in to comment.