Skip to content
This repository
Browse code

fixing XmlView, XmlException: Invalid input was raised when _serializ…

…e is string and data is numerically indexed.
  • Loading branch information...
commit 25c7a274955a3bd2b165881451c6e0168e73e931 1 parent 0bfcd49
ceeram authored
33  lib/Cake/Test/Case/View/XmlViewTest.php
@@ -43,9 +43,27 @@ public function testRenderWithoutView() {
43 43
 		$View = new XmlView($Controller);
44 44
 		$output = $View->render(false);
45 45
 
46  
-		$expected = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . '<users><user>user1</user><user>user2</user></users>';
47  
-		$this->assertTextEquals($expected, trim($output));
48  
-		$this->assertIdentical('application/xml', $Response->type());
  46
+		$this->assertSame(Xml::build($data)->asXML(), $output);
  47
+		$this->assertSame('application/xml', $Response->type());
  48
+
  49
+		$data = array(
  50
+			array(
  51
+				'User' => array(
  52
+					'username' => 'user1'
  53
+				)
  54
+			),
  55
+			array(
  56
+				'User' => array(
  57
+					'username' => 'user2'
  58
+				)
  59
+			)
  60
+		);
  61
+		$Controller->set(array('users' => $data, '_serialize' => 'users'));
  62
+		$View = new XmlView($Controller);
  63
+		$output = $View->render(false);
  64
+
  65
+		$expected = Xml::build(array('response'=> array('users'=> $data)))->asXML();
  66
+		$this->assertSame($expected, $output);
49 67
 	}
50 68
 
51 69
 /**
@@ -100,9 +118,12 @@ public function testRenderWithView() {
100 118
 		$View = new XmlView($Controller);
101 119
 		$output = $View->render('index');
102 120
 
103  
-		$expected = '<?xml version="1.0" encoding="UTF-8"?><users><user>user1</user><user>user2</user></users>';
104  
-		$this->assertIdentical($expected, str_replace(array("\r", "\n"), '', $output));
105  
-		$this->assertIdentical('application/xml', $Response->type());
  121
+		$expected = array(
  122
+			'users'=> array('user'=> array('user1', 'user2'))
  123
+		);
  124
+		$expected = Xml::build($expected)->asXML();
  125
+		$this->assertSame($expected, $output);
  126
+		$this->assertSame('application/xml', $Response->type());
106 127
 		$this->assertInstanceOf('HelperCollection', $View->Helpers);
107 128
 	}
108 129
 
11  lib/Cake/View/XmlView.php
@@ -26,7 +26,7 @@
26 26
  *
27 27
  * `$this->set(array('posts' => $posts, '_serialize' => 'posts'));`
28 28
  *
29  
- * When the view is rendered, the `$posts` view variable will be serialized 
  29
+ * When the view is rendered, the `$posts` view variable will be serialized
30 30
  * into XML.
31 31
  *
32 32
  * **Note** The view variable you specify must be compatible with Xml::fromArray().
@@ -38,7 +38,7 @@
38 38
  * $this->set(compact('posts', 'users', 'stuff'));
39 39
  * $this->set('_serialize', array('posts', 'users'));
40 40
  * }}}
41  
- * 
  41
+ *
42 42
  * The above would generate a XML object that looks like:
43 43
  *
44 44
  * `<response><posts>...</posts><users>...</users></response>`
@@ -75,8 +75,8 @@ public function __construct(Controller $controller = null) {
75 75
  * Render a XML view.
76 76
  *
77 77
  * Uses the special '_serialize' parameter to convert a set of
78  
- * view variables into a XML response.  Makes generating simple 
79  
- * XML responses very easy.  You can omit the '_serialize' parameter, 
  78
+ * view variables into a XML response.  Makes generating simple
  79
+ * XML responses very easy.  You can omit the '_serialize' parameter,
80 80
  * and use a normal view + layout as well.
81 81
  *
82 82
  * @param string $view The view being rendered.
@@ -93,6 +93,9 @@ public function render($view = null, $layout = null) {
93 93
 				}
94 94
 			} else {
95 95
 				$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
  96
+				if (is_array($data) && Set::numeric(array_keys($data))) {
  97
+					$data = array('response' => array($serialize => $data));
  98
+				}
96 99
 			}
97 100
 			$content = Xml::fromArray($data)->asXML();
98 101
 			$this->Blocks->set('content', $content);

0 notes on commit 25c7a27

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