Fixing Xml parsing where multiple child elements of the same name, with 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
This commit is contained in:
mark_story 2009-07-01 18:58:05 +00:00
parent ff5d9ed6e0
commit 975e4452d2
2 changed files with 74 additions and 0 deletions

View file

@ -685,6 +685,19 @@ class XmlNode extends Object {
$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 file

@ -679,6 +679,67 @@ class XmlTest extends CakeTestCase {
$result = $xml->toString();
$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
*