Skip to content
Permalink
Browse files

Fixing Xml::toArray() Where camelcased data was collapsed and corrupt…

…ed. Causing unpredictable data structures with empty tags. Minor changes to existing behavior. See modified test cases. Fixes #8
  • Loading branch information...
markstory committed Aug 14, 2009
1 parent ab50bbe commit 5a971ddbf04810948d36c3df5873da87766a735a
Showing with 58 additions and 5 deletions.
  1. +3 −4 cake/libs/xml.php
  2. +55 −1 cake/tests/cases/libs/xml.test.php
@@ -676,7 +676,6 @@ function toArray($camelize = true) {
if ($child->attributes) {
$value = array_merge(array('value' => $value), $child->attributes);
}
if (isset($out[$child->name]) || isset($multi[$key])) {
if (!isset($multi[$key])) {
$multi[$key] = array($out[$child->name]);
@@ -689,15 +688,16 @@ function toArray($camelize = true) {
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 {
} elseif (!empty($value)) {
$out[$key] = $value;
} else {
$out[$child->name] = $value;
}
continue;
} else {
@@ -909,7 +909,6 @@ function parse() {
for ($i = 0; $i < $count; $i++) {
$data = $vals[$i];
$data += array('tag' => null, 'value' => null, 'attributes' => array());
switch ($data['type']) {
case "open" :
$xml =& $xml->createElement($data['tag'], $data['value'], $data['attributes']);
@@ -759,6 +759,60 @@ function testElementCollapsing() {
$this->assertEqual($result, $expected);
}
/**
* test that empty values do not casefold collapse
*
* @see http://code.cakephp.org/tickets/view/8
* @return void
**/
function testCaseFoldingWithEmptyValues() {
$filledValue = '<method name="set_user_settings">
<title>update user information</title>
<user>1</user>
<User>
<id>1</id>
<name>varchar(45)</name>
</User>
</method>';
$xml =& new XML($filledValue);
$expected = array(
'Method' => array(
'name' => 'set_user_settings',
'title' => 'update user information',
'user' => '1',
'User' => array(
'id' => 1,
'name' => 'varchar(45)',
),
)
);
$result = $xml->toArray();
$this->assertEqual($result, $expected);
$emptyValue ='<method name="set_user_settings">
<title>update user information</title>
<user></user>
<User>
<id>1</id>
<name>varchar(45)</name>
</User>
</method>';
$xml =& new XML($emptyValue);
$expected = array(
'Method' => array(
'name' => 'set_user_settings',
'title' => 'update user information',
'user' => array(),
'User' => array(
'id' => 1,
'name' => 'varchar(45)',
),
)
);
$result = $xml->toArray();
$this->assertEqual($result, $expected);
}
/**
* testMixedParsing method
*
@@ -921,7 +975,7 @@ function testToArray() {
'Example' => array(
'Item' => array(
'title' => 'An example of a correctly reversed XMLNode',
'Desc' => array(),
'desc' => array(),
)
)
);

0 comments on commit 5a971dd

Please sign in to comment.
You can’t perform that action at this time.