| 12345 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* XmlViewTest file
|
|
|
4 |
*
|
|
|
5 |
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
|
|
6 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
7 |
*
|
|
|
8 |
* Licensed under The MIT License
|
|
|
9 |
* For full copyright and license information, please see the LICENSE.txt
|
|
|
10 |
* Redistributions of files must retain the above copyright notice
|
|
|
11 |
*
|
|
|
12 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
13 |
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
|
|
14 |
* @package Cake.Test.Case.View
|
|
|
15 |
* @since CakePHP(tm) v 2.1.0
|
|
|
16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
App::uses('Controller', 'Controller');
|
|
|
20 |
App::uses('CakeRequest', 'Network');
|
|
|
21 |
App::uses('CakeResponse', 'Network');
|
|
|
22 |
App::uses('XmlView', 'View');
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* XmlViewTest
|
|
|
26 |
*
|
|
|
27 |
* @package Cake.Test.Case.View
|
|
|
28 |
*/
|
|
|
29 |
class XmlViewTest extends CakeTestCase {
|
|
|
30 |
|
|
|
31 |
public function setUp() {
|
|
|
32 |
parent::setUp();
|
|
|
33 |
Configure::write('debug', 0);
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* testRenderWithoutView method
|
|
|
38 |
*
|
|
|
39 |
* @return void
|
|
|
40 |
*/
|
|
|
41 |
public function testRenderWithoutView() {
|
|
|
42 |
$Request = new CakeRequest();
|
|
|
43 |
$Response = new CakeResponse();
|
|
|
44 |
$Controller = new Controller($Request, $Response);
|
|
|
45 |
$data = array('users' => array('user' => array('user1', 'user2')));
|
|
|
46 |
$Controller->set(array('users' => $data, '_serialize' => 'users'));
|
|
|
47 |
$View = new XmlView($Controller);
|
|
|
48 |
$output = $View->render(false);
|
|
|
49 |
|
|
|
50 |
$this->assertSame(Xml::build($data)->asXML(), $output);
|
|
|
51 |
$this->assertSame('application/xml', $Response->type());
|
|
|
52 |
|
|
|
53 |
$data = array(
|
|
|
54 |
array(
|
|
|
55 |
'User' => array(
|
|
|
56 |
'username' => 'user1'
|
|
|
57 |
)
|
|
|
58 |
),
|
|
|
59 |
array(
|
|
|
60 |
'User' => array(
|
|
|
61 |
'username' => 'user2'
|
|
|
62 |
)
|
|
|
63 |
)
|
|
|
64 |
);
|
|
|
65 |
$Controller->set(array('users' => $data, '_serialize' => 'users'));
|
|
|
66 |
$View = new XmlView($Controller);
|
|
|
67 |
$output = $View->render(false);
|
|
|
68 |
|
|
|
69 |
$expected = Xml::build(array('response' => array('users' => $data)))->asXML();
|
|
|
70 |
$this->assertSame($expected, $output);
|
|
|
71 |
|
|
|
72 |
$Controller->set('_rootNode', 'custom_name');
|
|
|
73 |
$View = new XmlView($Controller);
|
|
|
74 |
$output = $View->render(false);
|
|
|
75 |
|
|
|
76 |
$expected = Xml::build(array('custom_name' => array('users' => $data)))->asXML();
|
|
|
77 |
$this->assertSame($expected, $output);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Test that rendering with _serialize does not load helpers
|
|
|
82 |
*
|
|
|
83 |
* @return void
|
|
|
84 |
*/
|
|
|
85 |
public function testRenderSerializeNoHelpers() {
|
|
|
86 |
$Request = new CakeRequest();
|
|
|
87 |
$Response = new CakeResponse();
|
|
|
88 |
$Controller = new Controller($Request, $Response);
|
|
|
89 |
$Controller->helpers = array('Html');
|
|
|
90 |
$Controller->set(array(
|
|
|
91 |
'_serialize' => 'tags',
|
|
|
92 |
'tags' => array('cakephp', 'framework')
|
|
|
93 |
));
|
|
|
94 |
$View = new XmlView($Controller);
|
|
|
95 |
$View->render();
|
|
|
96 |
$this->assertFalse(isset($View->Html), 'No helper loaded.');
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Test render with an array in _serialize
|
|
|
101 |
*
|
|
|
102 |
* @return void
|
|
|
103 |
*/
|
|
|
104 |
public function testRenderWithoutViewMultiple() {
|
|
|
105 |
$Request = new CakeRequest();
|
|
|
106 |
$Response = new CakeResponse();
|
|
|
107 |
$Controller = new Controller($Request, $Response);
|
|
|
108 |
$data = array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2'));
|
|
|
109 |
$Controller->set($data);
|
|
|
110 |
$Controller->set('_serialize', array('no', 'user'));
|
|
|
111 |
$View = new XmlView($Controller);
|
|
|
112 |
$this->assertSame('application/xml', $Response->type());
|
|
|
113 |
$output = $View->render(false);
|
|
|
114 |
$expected = array(
|
|
|
115 |
'response' => array('no' => $data['no'], 'user' => $data['user'])
|
|
|
116 |
);
|
|
|
117 |
$this->assertSame(Xml::build($expected)->asXML(), $output);
|
|
|
118 |
|
|
|
119 |
$Controller->set('_rootNode', 'custom_name');
|
|
|
120 |
$View = new XmlView($Controller);
|
|
|
121 |
$output = $View->render(false);
|
|
|
122 |
$expected = array(
|
|
|
123 |
'custom_name' => array('no' => $data['no'], 'user' => $data['user'])
|
|
|
124 |
);
|
|
|
125 |
$this->assertSame(Xml::build($expected)->asXML(), $output);
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
/**
|
|
|
129 |
* Test render with an array in _serialize and alias
|
|
|
130 |
*
|
|
|
131 |
* @return void
|
|
|
132 |
*/
|
|
|
133 |
public function testRenderWithoutViewMultipleAndAlias() {
|
|
|
134 |
$Request = new CakeRequest();
|
|
|
135 |
$Response = new CakeResponse();
|
|
|
136 |
$Controller = new Controller($Request, $Response);
|
|
|
137 |
$data = array('original_name' => 'my epic name', 'user' => 'fake', 'list' => array('item1', 'item2'));
|
|
|
138 |
$Controller->set($data);
|
|
|
139 |
$Controller->set('_serialize', array('new_name' => 'original_name', 'user'));
|
|
|
140 |
$View = new XmlView($Controller);
|
|
|
141 |
$this->assertSame('application/xml', $Response->type());
|
|
|
142 |
$output = $View->render(false);
|
|
|
143 |
$expected = array(
|
|
|
144 |
'response' => array('new_name' => $data['original_name'], 'user' => $data['user'])
|
|
|
145 |
);
|
|
|
146 |
$this->assertSame(Xml::build($expected)->asXML(), $output);
|
|
|
147 |
|
|
|
148 |
$Controller->set('_rootNode', 'custom_name');
|
|
|
149 |
$View = new XmlView($Controller);
|
|
|
150 |
$output = $View->render(false);
|
|
|
151 |
$expected = array(
|
|
|
152 |
'custom_name' => array('new_name' => $data['original_name'], 'user' => $data['user'])
|
|
|
153 |
);
|
|
|
154 |
$this->assertSame(Xml::build($expected)->asXML(), $output);
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
/**
|
|
|
158 |
* testRenderWithView method
|
|
|
159 |
*
|
|
|
160 |
* @return void
|
|
|
161 |
*/
|
|
|
162 |
public function testRenderWithView() {
|
|
|
163 |
App::build(array('View' => array(
|
|
|
164 |
CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS
|
|
|
165 |
)));
|
|
|
166 |
$Request = new CakeRequest();
|
|
|
167 |
$Response = new CakeResponse();
|
|
|
168 |
$Controller = new Controller($Request, $Response);
|
|
|
169 |
$Controller->name = $Controller->viewPath = 'Posts';
|
|
|
170 |
|
|
|
171 |
$data = array(
|
|
|
172 |
array(
|
|
|
173 |
'User' => array(
|
|
|
174 |
'username' => 'user1'
|
|
|
175 |
)
|
|
|
176 |
),
|
|
|
177 |
array(
|
|
|
178 |
'User' => array(
|
|
|
179 |
'username' => 'user2'
|
|
|
180 |
)
|
|
|
181 |
)
|
|
|
182 |
);
|
|
|
183 |
$Controller->set('users', $data);
|
|
|
184 |
$View = new XmlView($Controller);
|
|
|
185 |
$output = $View->render('index');
|
|
|
186 |
|
|
|
187 |
$expected = array(
|
|
|
188 |
'users' => array('user' => array('user1', 'user2'))
|
|
|
189 |
);
|
|
|
190 |
$expected = Xml::build($expected)->asXML();
|
|
|
191 |
$this->assertSame($expected, $output);
|
|
|
192 |
$this->assertSame('application/xml', $Response->type());
|
|
|
193 |
$this->assertInstanceOf('HelperCollection', $View->Helpers);
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
}
|