Skip to content
This repository
Browse code

Adding test cases for boolean and boolean-ish values. Fixes false bei…

…ng converted to '' when using Xml::toString().

Fixes #6478

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8206 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information...
commit fd7cf5e5e601b17bedf7e74b30905cd286fa3671 1 parent 8c7883f
Mark Story authored
3  cake/libs/xml.php
@@ -607,6 +607,9 @@ function toString($options = array(), $depth = 0) {
607 607
 
608 608
 			if (is_array($this->attributes) && count($this->attributes) > 0) {
609 609
 				foreach ($this->attributes as $key => $val) {
  610
+					if (is_bool($val) && $val === false) {
  611
+						$val = 0;
  612
+					}
610 613
 					$d .= ' ' . $key . '="' . htmlspecialchars($val, ENT_QUOTES, Configure::read('App.encoding')) . '"';
611 614
 				}
612 615
 			}
32  cake/tests/cases/libs/xml.test.php
@@ -97,6 +97,38 @@ function testSerialization() {
97 97
 		$result = preg_replace("/\n/",'', $xml->toString(false));
98 98
 		$this->assertEqual($result, $expected);
99 99
 	}
  100
+
  101
+/**
  102
+ * test serialization of boolean and null values.  false = 0, true = 1, null = ''
  103
+ *
  104
+ * @return void
  105
+ **/
  106
+	function testSerializationOfBooleanAndBooleanishValues() {
  107
+		$xml =& new Xml(array('data' => array('example' => false)));
  108
+		$result = $xml->toString(false);
  109
+		$expected = '<data example="0" />';
  110
+		$this->assertEqual($result, $expected, 'Boolean values incorrectly handled. %s');
  111
+
  112
+		$xml =& new Xml(array('data' => array('example' => true)));
  113
+		$result = $xml->toString(false);
  114
+		$expected = '<data example="1" />';
  115
+		$this->assertEqual($result, $expected, 'Boolean values incorrectly handled. %s');
  116
+
  117
+		$xml =& new Xml(array('data' => array('example' => null)));
  118
+		$result = $xml->toString(false);
  119
+		$expected = '<data example="" />';
  120
+		$this->assertEqual($result, $expected, 'Boolean values incorrectly handled. %s');
  121
+		
  122
+		$xml =& new Xml(array('data' => array('example' => 0)));
  123
+		$result = $xml->toString(false);
  124
+		$expected = '<data example="0" />';
  125
+		$this->assertEqual($result, $expected, 'Boolean-ish values incorrectly handled. %s');
  126
+
  127
+		$xml =& new Xml(array('data' => array('example' => 1)));
  128
+		$result = $xml->toString(false);
  129
+		$expected = '<data example="1" />';
  130
+		$this->assertEqual($result, $expected, 'Boolean-ish values incorrectly handled. %s');
  131
+	}
100 132
 /**
101 133
  * testSimpleArray method
102 134
  *

0 notes on commit fd7cf5e

Please sign in to comment.
Something went wrong with that request. Please try again.