Permalink
Browse files

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
  • Loading branch information...
1 parent bd7bd5d commit bbfee7ba179a07e1f544d436ab8fa70d3fc599d1 @davidpersson davidpersson committed Apr 17, 2009
Showing with 29 additions and 3 deletions.
  1. +9 −3 cake/libs/xml.php
  2. +20 −0 cake/tests/cases/libs/xml.test.php
View
@@ -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) {
@@ -257,6 +257,26 @@ function testNestedArraySerialization() {
$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
*
* @access public

0 comments on commit bbfee7b

Please sign in to comment.