From 88a2f0936edaa524426d0780d24b70d289296c8b Mon Sep 17 00:00:00 2001 From: DarkAngelBGE Date: Tue, 13 May 2008 07:56:33 +0000 Subject: [PATCH] fixes #4559 bringing xmlhelper to 100% code coverage fixing small bugs in XmlHelper::__parseContent git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6838 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/view/helpers/xml.php | 18 ++- cake/libs/xml.php | 1 + .../cases/libs/view/helpers/xml.test.php | 124 ++++++++++++++++++ 3 files changed, 136 insertions(+), 7 deletions(-) diff --git a/cake/libs/view/helpers/xml.php b/cake/libs/view/helpers/xml.php index 592b9026c..9398fa176 100644 --- a/cake/libs/view/helpers/xml.php +++ b/cake/libs/view/helpers/xml.php @@ -74,7 +74,11 @@ class XmlHelper extends AppHelper { if (Configure::read('App.encoding') !== null) { $this->encoding = Configure::read('App.encoding'); } - $attrib = array_merge(array('version' => '1.0', 'encoding' => $this->encoding), $attrib); + + if (is_array($attrib)) { + $attrib = array_merge(array('version' => '1.0', 'encoding' => $this->encoding), $attrib); + } + return $this->output('<' . '?xml' . $this->__composeAttributes($attrib) . ' ?' . '>'); } /** @@ -178,17 +182,19 @@ class XmlHelper extends AppHelper { $out = ''; $keys = array_keys($content); $count = count($keys); + for ($i = 0; $i < $count; $i++) { - if (is_numeric($keys[$i])) { + if (is_numeric($content[$keys[$i]])) { $out .= $this->__composeContent($content[$keys[$i]]); } elseif (is_array($content[$keys[$i]])) { $attr = $child = array(); if (Set::countDim($content[$keys[$i]]) >= 2) { - + trigger_error(__('Dimension for XmlHelper::__composeContent is too high (>= 2). Please use an array with less dimension.', true), E_USER_WARNING); } else { - + $out .= $this->__composeContent($content[$keys[$i]]); } - //$out .= $this->elem($keys[$i] + } elseif (is_string($content[$keys[$i]])) { + $out .= $this->elem($content[$keys[$i]]); } } return $out; @@ -196,8 +202,6 @@ class XmlHelper extends AppHelper { return $content->toString(); } elseif (is_object($content) && method_exists($content, 'toString')) { return $content->toString(); - } elseif (is_object($content) && method_exists($content, 'toString')) { - return $content->toString(); } else { return $content; } diff --git a/cake/libs/xml.php b/cake/libs/xml.php index 923abc854..126842df6 100644 --- a/cake/libs/xml.php +++ b/cake/libs/xml.php @@ -556,6 +556,7 @@ class XmlNode extends Object { $d .= '<' . $this->name(); if (is_array($this->attributes) && count($this->attributes) > 0) { foreach ($this->attributes as $key => $val) { + $val = str_replace('"', '\"', $val); $d .= " $key=\"$val\""; } } diff --git a/cake/tests/cases/libs/view/helpers/xml.test.php b/cake/tests/cases/libs/view/helpers/xml.test.php index a65b55613..7830a013d 100644 --- a/cake/tests/cases/libs/view/helpers/xml.test.php +++ b/cake/tests/cases/libs/view/helpers/xml.test.php @@ -32,6 +32,17 @@ if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) { uses('view'.DS.'helpers'.DS.'app_helper', 'controller'.DS.'controller', 'model'.DS.'model', 'view'.DS.'helper', 'view'.DS.'helpers'.DS.'xml'); +class TestXml extends Object { + var $content = ''; + + function __construct($content) { + $this->content = $content; + } + + function toString() { + return $this->content; + } +} /** * Short description for class. * @@ -71,6 +82,119 @@ class XmlHelperTest extends UnitTestCase { $this->assertEqual($result, $expected); } + function testRenderElementWithNamespace() { + $result = $this->Xml->elem('count', array('namespace' => 'myNameSpace'), 'content'); + $expected = 'content'; + $this->assertEqual($result, $expected); + + $result = $this->Xml->elem('count', array('namespace' => 'myNameSpace'), 'content', false); + $expected = 'content'; + $this->assertEqual($result, $expected); + } + + function testSerialize() { + $data = array( + 'test1' => 'test with no quotes', + 'test2' => 'test with "double quotes"' + ); + $result = $this->Xml->serialize($data); + $expected = ''; + $this->assertIdentical($result, $expected); + + $data = array( + 'test1' => 'test with no quotes', + 'test2' => 'test without double quotes' + ); + $result = $this->Xml->serialize($data); + $expected = ''; + $this->assertIdentical($result, $expected); + } + + function testHeader() { + $attrib = array(); + $result = $this->Xml->header($attrib); + $expected = ''; + $this->assertIdentical($result, $expected); + + $attrib = array( + 'encoding' => 'UTF-8', + 'version' => '1.1' + ); + $result = $this->Xml->header($attrib); + $expected = ''; + $this->assertIdentical($result, $expected); + + $attrib = array( + 'encoding' => 'UTF-8', + 'version' => '1.2', + 'additional' => 'attribute' + ); + $result = $this->Xml->header($attrib); + $expected = ''; + $this->assertIdentical($result, $expected); + + $attrib = 'encoding="UTF-8" someOther="value"'; + $result = $this->Xml->header($attrib); + $expected = ''; + $this->assertIdentical($result, $expected); + } + + function test__ComposeContent() { + $content = 'some String'; + $result = $this->Xml->__composeContent($content); + $expected = 'some String'; + $this->assertIdentical($result, $expected); + + $content = array('some String', 'some Other String'); + $result = $this->Xml->__composeContent($content); + $expected = ''; + $this->assertIdentical($result, $expected); + + $content = array(1, 'some Other String'); + $result = $this->Xml->__composeContent($content); + $expected = '1'; + $this->assertIdentical($result, $expected); + + $content = array( + array('some String'), + array('some Other String') + ); + $result = $this->Xml->__composeContent($content); + $expected = ''; + $this->assertIdentical($result, $expected); + + $content = array( + array(array('some String')), + array(('some Other String')) + ); + $result = $this->Xml->__composeContent($content); + $this->assertError(); + + $xml = new Xml(null, array()); + $result = $xml->load('simple note'); + $result = $this->Xml->__composeContent($xml); + $expected = ''; + $this->assertIdentical($result, $expected); + + $xml = new TestXml('simple note'); + $result = $this->Xml->__composeContent($xml); + $expected = 'simple note'; + $this->assertIdentical($result, $expected); + } + + function test__prepareNamespaces() { + $this->Xml->__namespaces = array('namespace1', 'namespace2'); + $result = $this->Xml->__prepareNamespaces(); + $expected = array('xmlns:0' => 'namespace1', 'xmlns:1' => 'namespace2'); + $this->assertIdentical($result, $expected); + + $this->Xml->__namespaces = array(); + $result = $this->Xml->__prepareNamespaces(); + $expected = array(); + $this->assertIdentical($result, $expected); + + } + function tearDown() { unset($this->Xml); }