| 13532 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* JsonViewTest 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('JsonView', 'View');
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* JsonViewTest
|
|
|
26 |
*
|
|
|
27 |
* @package Cake.Test.Case.View
|
|
|
28 |
*/
|
|
|
29 |
class JsonViewTest extends CakeTestCase {
|
|
|
30 |
|
|
|
31 |
public function setUp() {
|
|
|
32 |
parent::setUp();
|
|
|
33 |
Configure::write('debug', 0);
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Generates testRenderWithoutView data.
|
|
|
38 |
*
|
|
|
39 |
* Note: array($data, $serialize, expected)
|
|
|
40 |
*
|
|
|
41 |
* @return void
|
|
|
42 |
*/
|
|
|
43 |
public static function renderWithoutViewProvider() {
|
|
|
44 |
return array(
|
|
|
45 |
// Test render with a valid string in _serialize.
|
|
|
46 |
array(
|
|
|
47 |
array('data' => array('user' => 'fake', 'list' => array('item1', 'item2'))),
|
|
|
48 |
'data',
|
|
|
49 |
json_encode(array('user' => 'fake', 'list' => array('item1', 'item2')))
|
|
|
50 |
),
|
|
|
51 |
|
|
|
52 |
// Test render with a string with an invalid key in _serialize.
|
|
|
53 |
array(
|
|
|
54 |
array('data' => array('user' => 'fake', 'list' => array('item1', 'item2'))),
|
|
|
55 |
'no_key',
|
|
|
56 |
json_encode(null)
|
|
|
57 |
),
|
|
|
58 |
|
|
|
59 |
// Test render with a valid array in _serialize.
|
|
|
60 |
array(
|
|
|
61 |
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')),
|
|
|
62 |
array('no', 'user'),
|
|
|
63 |
json_encode(array('no' => 'nope', 'user' => 'fake'))
|
|
|
64 |
),
|
|
|
65 |
|
|
|
66 |
// Test render with an empty array in _serialize.
|
|
|
67 |
array(
|
|
|
68 |
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')),
|
|
|
69 |
array(),
|
|
|
70 |
json_encode(null)
|
|
|
71 |
),
|
|
|
72 |
|
|
|
73 |
// Test render with a valid array with an invalid key in _serialize.
|
|
|
74 |
array(
|
|
|
75 |
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')),
|
|
|
76 |
array('no', 'user', 'no_key'),
|
|
|
77 |
json_encode(array('no' => 'nope', 'user' => 'fake'))
|
|
|
78 |
),
|
|
|
79 |
|
|
|
80 |
// Test render with a valid array with only an invalid key in _serialize.
|
|
|
81 |
array(
|
|
|
82 |
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')),
|
|
|
83 |
array('no_key'),
|
|
|
84 |
json_encode(null)
|
|
|
85 |
),
|
|
|
86 |
|
|
|
87 |
// Test render with Null in _serialize (unset).
|
|
|
88 |
array(
|
|
|
89 |
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')),
|
|
|
90 |
null,
|
|
|
91 |
null
|
|
|
92 |
),
|
|
|
93 |
|
|
|
94 |
// Test render with False in _serialize.
|
|
|
95 |
array(
|
|
|
96 |
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')),
|
|
|
97 |
false,
|
|
|
98 |
json_encode(null)
|
|
|
99 |
),
|
|
|
100 |
|
|
|
101 |
// Test render with True in _serialize.
|
|
|
102 |
array(
|
|
|
103 |
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')),
|
|
|
104 |
true,
|
|
|
105 |
json_encode(null)
|
|
|
106 |
),
|
|
|
107 |
|
|
|
108 |
// Test render with empty string in _serialize.
|
|
|
109 |
array(
|
|
|
110 |
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')),
|
|
|
111 |
'',
|
|
|
112 |
json_encode(null)
|
|
|
113 |
),
|
|
|
114 |
|
|
|
115 |
// Test render with a valid array in _serialize and alias.
|
|
|
116 |
array(
|
|
|
117 |
array('original_name' => 'my epic name', 'user' => 'fake', 'list' => array('item1', 'item2')),
|
|
|
118 |
array('new_name' => 'original_name', 'user'),
|
|
|
119 |
json_encode(array('new_name' => 'my epic name', 'user' => 'fake'))
|
|
|
120 |
),
|
|
|
121 |
|
|
|
122 |
// Test render with an a valid array in _serialize and alias of a null value.
|
|
|
123 |
array(
|
|
|
124 |
array('null' => null),
|
|
|
125 |
array('null'),
|
|
|
126 |
json_encode(array('null' => null))
|
|
|
127 |
),
|
|
|
128 |
|
|
|
129 |
// Test render with a False value to be serialized.
|
|
|
130 |
array(
|
|
|
131 |
array('false' => false),
|
|
|
132 |
'false',
|
|
|
133 |
json_encode(false)
|
|
|
134 |
),
|
|
|
135 |
|
|
|
136 |
// Test render with a True value to be serialized.
|
|
|
137 |
array(
|
|
|
138 |
array('true' => true),
|
|
|
139 |
'true',
|
|
|
140 |
json_encode(true)
|
|
|
141 |
),
|
|
|
142 |
|
|
|
143 |
// Test render with an empty string value to be serialized.
|
|
|
144 |
array(
|
|
|
145 |
array('empty' => ''),
|
|
|
146 |
'empty',
|
|
|
147 |
json_encode('')
|
|
|
148 |
),
|
|
|
149 |
|
|
|
150 |
// Test render with a zero value to be serialized.
|
|
|
151 |
array(
|
|
|
152 |
array('zero' => 0),
|
|
|
153 |
'zero',
|
|
|
154 |
json_encode(0)
|
|
|
155 |
),
|
|
|
156 |
);
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
/**
|
|
|
160 |
* Test render with a valid string in _serialize.
|
|
|
161 |
*
|
|
|
162 |
* @dataProvider renderWithoutViewProvider
|
|
|
163 |
* @return void
|
|
|
164 |
*/
|
|
|
165 |
public function testRenderWithoutView($data, $serialize, $expected) {
|
|
|
166 |
$Request = new CakeRequest();
|
|
|
167 |
$Response = new CakeResponse();
|
|
|
168 |
$Controller = new Controller($Request, $Response);
|
|
|
169 |
|
|
|
170 |
$Controller->set($data);
|
|
|
171 |
$Controller->set('_serialize', $serialize);
|
|
|
172 |
$View = new JsonView($Controller);
|
|
|
173 |
$output = $View->render(false);
|
|
|
174 |
|
|
|
175 |
$this->assertSame($expected, $output);
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
/**
|
|
|
179 |
* Test that rendering with _serialize does not load helpers.
|
|
|
180 |
*
|
|
|
181 |
* @return void
|
|
|
182 |
*/
|
|
|
183 |
public function testRenderSerializeNoHelpers() {
|
|
|
184 |
$Request = new CakeRequest();
|
|
|
185 |
$Response = new CakeResponse();
|
|
|
186 |
$Controller = new Controller($Request, $Response);
|
|
|
187 |
|
|
|
188 |
$Controller->helpers = array('Html');
|
|
|
189 |
$Controller->set(array(
|
|
|
190 |
'tags' => array('cakephp', 'framework'),
|
|
|
191 |
'_serialize' => 'tags'
|
|
|
192 |
));
|
|
|
193 |
$View = new JsonView($Controller);
|
|
|
194 |
$View->render();
|
|
|
195 |
|
|
|
196 |
$this->assertFalse(isset($View->Html), 'No helper loaded.');
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
/**
|
|
|
200 |
* testJsonpResponse method
|
|
|
201 |
*
|
|
|
202 |
* @return void
|
|
|
203 |
*/
|
|
|
204 |
public function testJsonpResponse() {
|
|
|
205 |
$Request = new CakeRequest();
|
|
|
206 |
$Response = new CakeResponse();
|
|
|
207 |
$Controller = new Controller($Request, $Response);
|
|
|
208 |
|
|
|
209 |
$data = array('user' => 'fake', 'list' => array('item1', 'item2'));
|
|
|
210 |
$Controller->set(array(
|
|
|
211 |
'data' => $data,
|
|
|
212 |
'_serialize' => 'data',
|
|
|
213 |
'_jsonp' => true
|
|
|
214 |
));
|
|
|
215 |
$View = new JsonView($Controller);
|
|
|
216 |
$output = $View->render(false);
|
|
|
217 |
|
|
|
218 |
$this->assertSame(json_encode($data), $output);
|
|
|
219 |
$this->assertSame('application/json', $Response->type());
|
|
|
220 |
|
|
|
221 |
$View->request->query = array('callback' => 'jfunc');
|
|
|
222 |
$output = $View->render(false);
|
|
|
223 |
$expected = 'jfunc(' . json_encode($data) . ')';
|
|
|
224 |
$this->assertSame($expected, $output);
|
|
|
225 |
$this->assertSame('application/javascript', $Response->type());
|
|
|
226 |
|
|
|
227 |
$View->request->query = array('jsonCallback' => 'jfunc');
|
|
|
228 |
$View->viewVars['_jsonp'] = 'jsonCallback';
|
|
|
229 |
$output = $View->render(false);
|
|
|
230 |
$expected = 'jfunc(' . json_encode($data) . ')';
|
|
|
231 |
$this->assertSame($expected, $output);
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
/**
|
|
|
235 |
* Test render with a View file specified.
|
|
|
236 |
*
|
|
|
237 |
* @return void
|
|
|
238 |
*/
|
|
|
239 |
public function testRenderWithView() {
|
|
|
240 |
App::build(array(
|
|
|
241 |
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
|
|
|
242 |
));
|
|
|
243 |
$Request = new CakeRequest();
|
|
|
244 |
$Response = new CakeResponse();
|
|
|
245 |
$Controller = new Controller($Request, $Response);
|
|
|
246 |
$Controller->name = $Controller->viewPath = 'Posts';
|
|
|
247 |
|
|
|
248 |
$data = array(
|
|
|
249 |
'User' => array(
|
|
|
250 |
'username' => 'fake'
|
|
|
251 |
),
|
|
|
252 |
'Item' => array(
|
|
|
253 |
array('name' => 'item1'),
|
|
|
254 |
array('name' => 'item2')
|
|
|
255 |
)
|
|
|
256 |
);
|
|
|
257 |
$Controller->set('user', $data);
|
|
|
258 |
$View = new JsonView($Controller);
|
|
|
259 |
$output = $View->render('index');
|
|
|
260 |
|
|
|
261 |
$expected = json_encode(array('user' => 'fake', 'list' => array('item1', 'item2'), 'paging' => null));
|
|
|
262 |
$this->assertSame($expected, $output);
|
|
|
263 |
$this->assertSame('application/json', $Response->type());
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
/**
|
|
|
267 |
* Test render with a View file specified and named parameters.
|
|
|
268 |
*
|
|
|
269 |
* @return void
|
|
|
270 |
*/
|
|
|
271 |
public function testRenderWithViewAndNamed() {
|
|
|
272 |
App::build(array(
|
|
|
273 |
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
|
|
|
274 |
));
|
|
|
275 |
$Request = new CakeRequest(null, false);
|
|
|
276 |
$Request->params['named'] = array('page' => 2);
|
|
|
277 |
$Response = new CakeResponse();
|
|
|
278 |
$Controller = new Controller($Request, $Response);
|
|
|
279 |
$Controller->name = $Controller->viewPath = 'Posts';
|
|
|
280 |
|
|
|
281 |
$data = array(
|
|
|
282 |
'User' => array(
|
|
|
283 |
'username' => 'fake'
|
|
|
284 |
),
|
|
|
285 |
'Item' => array(
|
|
|
286 |
array('name' => 'item1'),
|
|
|
287 |
array('name' => 'item2')
|
|
|
288 |
)
|
|
|
289 |
);
|
|
|
290 |
$Controller->set('user', $data);
|
|
|
291 |
$Controller->helpers = array('Paginator');
|
|
|
292 |
$View = new JsonView($Controller);
|
|
|
293 |
$output = $View->render('index');
|
|
|
294 |
|
|
|
295 |
$expected = array('user' => 'fake', 'list' => array('item1', 'item2'), 'paging' => array('page' => 2));
|
|
|
296 |
$this->assertSame(json_encode($expected), $output);
|
|
|
297 |
$this->assertSame('application/json', $Response->type());
|
|
|
298 |
|
|
|
299 |
$View->request->query = array('jsonCallback' => 'jfunc');
|
|
|
300 |
$Controller->set('_jsonp', 'jsonCallback');
|
|
|
301 |
$View = new JsonView($Controller);
|
|
|
302 |
$View->helpers = array('Paginator');
|
|
|
303 |
$output = $View->render('index');
|
|
|
304 |
$expected['paging']['?']['jsonCallback'] = 'jfunc';
|
|
|
305 |
$expected = 'jfunc(' . json_encode($expected) . ')';
|
|
|
306 |
$this->assertSame($expected, $output);
|
|
|
307 |
$this->assertSame('application/javascript', $Response->type());
|
|
|
308 |
}
|
|
|
309 |
}
|