Skip to content
Browse files

Fixing Xml parsing where multiple child elements of the same name, wi…

…th no value are collapsed causing data errors when Xml object was converted to an array. Test cases from 'burzum' added.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8209 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information...
1 parent ff5d9ed commit 975e4452d2934168a9140b8744325cf265185316 @markstory markstory committed
Showing with 74 additions and 0 deletions.
  1. +13 −0 cake/libs/xml.php
  2. +61 −0 cake/tests/cases/libs/xml.test.php
View
13 cake/libs/xml.php
@@ -685,6 +685,19 @@ function toArray($camelize = true) {
$out[$child->name] = $value;
}
continue;
+ } elseif (count($child->children) === 0 && $child->value == '') {
+ $value = $child->attributes;
+
+ if (isset($out[$child->name]) || isset($multi[$key])) {
+ if (!isset($multi[$key])) {
+ $multi[$key] = array($out[$child->name]);
+ unset($out[$child->name]);
+ }
+ $multi[$key][] = $value;
+ } else {
+ $out[$key] = $value;
+ }
+ continue;
} else {
$value = $child->toArray($camelize);
}
View
61 cake/tests/cases/libs/xml.test.php
@@ -680,6 +680,67 @@ function testSimpleParsing() {
$this->assertEqual($source, $result);
}
/**
+ * test that elements with empty tag values do not collapse and corrupt data structures
+ *
+ * @access public
+ * @return void
+ **/
+ function testElementCollapsing() {
+ $xmlDataThatFails = '<resultpackage>
+ <result qid="46b1c46ed6208"><![CDATA[46b1c46ed3af9]]></result>
+ <result qid="46b1c46ed332a"><![CDATA[]]></result>
+ <result qid="46b1c46ed90e6"><![CDATA[46b1c46ed69d8]]></result>
+ <result qid="46b1c46ed71a7"><![CDATA[46b1c46ed5a38]]></result>
+ <result qid="46b1c46ed8146"><![CDATA[46b1c46ed98b6]]></result>
+ <result qid="46b1c46ed7978"><![CDATA[]]></result>
+ <result qid="46b1c46ed4a98"><![CDATA[]]></result>
+ <result qid="46b1c46ed42c8"><![CDATA[]]></result>
+ <result qid="46b1c46ed5268"><![CDATA[46b1c46ed8917]]></result>
+ </resultpackage>';
+
+ $Xml = new Xml();
+ $Xml->load('<?xml version="1.0" encoding="UTF-8" ?>' . $xmlDataThatFails);
+ $result = $Xml->toArray(false);
+
+ $this->assertTrue(is_array($result));
+ $expected = array(
+ 'resultpackage' => array(
+ 'result' => array(
+ 0 => array(
+ 'value' => '46b1c46ed3af9',
+ 'qid' => '46b1c46ed6208'),
+ 1 => array(
+ 'qid' => '46b1c46ed332a'),
+ 2 => array(
+ 'value' => '46b1c46ed69d8',
+ 'qid' => '46b1c46ed90e6'),
+ 3 => array(
+ 'value' => '46b1c46ed5a38',
+ 'qid' => '46b1c46ed71a7'),
+ 4 => array(
+ 'value' => '46b1c46ed98b6',
+ 'qid' => '46b1c46ed8146'),
+ 5 => array(
+ 'qid' => '46b1c46ed7978'),
+ 6 => array(
+ 'qid' => '46b1c46ed4a98'),
+ 7 => array(
+ 'qid' => '46b1c46ed42c8'),
+ 8 => array(
+ 'value' => '46b1c46ed8917',
+ 'qid' => '46b1c46ed5268'),
+ )
+ ));
+ $this->assertEqual(
+ count($result['resultpackage']['result']), count($expected['resultpackage']['result']),
+ 'Incorrect array length %s');
+
+ $this->assertFalse(
+ isset($result['resultpackage']['result'][0][0]['qid']), 'Nested array exists, data is corrupt. %s');
+
+ $this->assertEqual($result, $expected);
+ }
+/**
* testMixedParsing method
*
* @access public

0 comments on commit 975e445

Please sign in to comment.
Something went wrong with that request. Please try again.