Skip to content

Commit

Permalink
Fixing Xml::toString() not slugging elements with no children.
Browse files Browse the repository at this point in the history
Thanks to jeremyharris for the patch.
Fixes #903
  • Loading branch information
markstory committed May 28, 2011
1 parent b76901e commit 3f58509
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cake/libs/xml.php
Expand Up @@ -275,6 +275,9 @@ function normalize($object, $keyName = null, $options = array()) {

$node->normalize($val, $n, $options);
} elseif ($options['format'] == 'tags' && $this->__tagOptions($key) !== false) {
if ($options['slug'] == true) {
$key = Inflector::slug(Inflector::underscore($key));
}
$tmp =& $node->createElement($key);
if (!empty($val) || $val === 0 || $val === '0') {
$tmp->createTextNode($val);
Expand Down
13 changes: 13 additions & 0 deletions cake/tests/cases/libs/xml.test.php
Expand Up @@ -1624,4 +1624,17 @@ function testToArrayAlternate() {
$this->assertIdentical($result, $expected);
}


function testToStringSlugging() {
$array = array(
'Response' => array(
'OneKey' => 'foo',
'TwoKey' => array('bar', 'baz')
)
);
$xml = new Xml($array, array('format' => 'tags'));
$result = $xml->toString(array('cdata' => false));
$expected = '<response><one_key>foo</one_key><two_key>bar</two_key><two_key>baz</two_key></response>';
$this->assertEqual($result, $expected);
}
}

0 comments on commit 3f58509

Please sign in to comment.