From 4d77cb059dfd4db72179dde9d9da8c17dfad2d54 Mon Sep 17 00:00:00 2001 From: domingues Date: Tue, 13 Dec 2016 16:16:12 +0000 Subject: [PATCH 1/2] Fix a bug in Xml::fromArray() When creating from an array with elements like this: `[ "a" => [ 0 ] ]` or `[ "a" => [ '0' ] ]` it fails and produces XML like this `` instant of `0`. The problem is that in PHP `empty('0')` is true, so an exception to this case is needed. --- lib/Cake/Utility/Xml.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cake/Utility/Xml.php b/lib/Cake/Utility/Xml.php index 95bb4659e..f27fc1497 100644 --- a/lib/Cake/Utility/Xml.php +++ b/lib/Cake/Utility/Xml.php @@ -312,7 +312,7 @@ class Xml { $childNS = $value['xmlns:']; unset($value['xmlns:']); } - } elseif (!empty($value) || $value === 0) { + } elseif (!empty($value) || $value === 0 || $value === '0') { $childValue = (string)$value; } From 3c44ddd10b057ea1095583987495a7724223c178 Mon Sep 17 00:00:00 2001 From: mark_story Date: Tue, 13 Dec 2016 22:47:57 -0500 Subject: [PATCH 2/2] Add tests for #9870 --- lib/Cake/Test/Case/Utility/XmlTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/Cake/Test/Case/Utility/XmlTest.php b/lib/Cake/Test/Case/Utility/XmlTest.php index 5cb62d0f8..d0fa9d1d5 100644 --- a/lib/Cake/Test/Case/Utility/XmlTest.php +++ b/lib/Cake/Test/Case/Utility/XmlTest.php @@ -395,7 +395,15 @@ XML; $obj = Xml::fromArray($xml, 'attributes'); $xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?>defect'; $this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML()); + } +/** + * Test fromArray() with zero values. + * + * @return void + */ + public function testFromArrayZeroValue() + { $xml = array( 'tag' => array( '@' => 0, @@ -406,6 +414,16 @@ XML; $xmlText = << 0 +XML; + $this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML()); + + $xml = array( + 'tag' => array('0') + ); + $obj = Xml::fromArray($xml); + $xmlText = << +0 XML; $this->assertXmlStringEqualsXmlString($xmlText, $obj->asXML()); }