From 3f5850913a2b75815531662bc9d5ca169a5a66a5 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 28 May 2011 16:19:54 -0400 Subject: [PATCH] Fixing Xml::toString() not slugging elements with no children. Thanks to jeremyharris for the patch. Fixes #903 --- cake/libs/xml.php | 3 +++ cake/tests/cases/libs/xml.test.php | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/cake/libs/xml.php b/cake/libs/xml.php index b6798ca2198..fd9f23faefa 100644 --- a/cake/libs/xml.php +++ b/cake/libs/xml.php @@ -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); diff --git a/cake/tests/cases/libs/xml.test.php b/cake/tests/cases/libs/xml.test.php index 5555178365e..0304e6e0028 100644 --- a/cake/tests/cases/libs/xml.test.php +++ b/cake/tests/cases/libs/xml.test.php @@ -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 = 'foobarbaz'; + $this->assertEqual($result, $expected); + } }