pictcode / lib / Cake / Test / Case / View / JsonViewTest.php @ 0b1b8047
履歴 | 表示 | アノテート | ダウンロード (11.096 KB)
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 |
/**
|
32 |
* setUp method
|
33 |
*
|
34 |
* @return void
|
35 |
**/
|
36 |
public function setUp() { |
37 |
parent::setUp();
|
38 |
Configure::write('debug', 0); |
39 |
} |
40 |
|
41 |
/**
|
42 |
* Generates testRenderWithoutView data.
|
43 |
*
|
44 |
* Note: array($data, $serialize, expected)
|
45 |
*
|
46 |
* @return void
|
47 |
*/
|
48 |
public static function renderWithoutViewProvider() { |
49 |
return array( |
50 |
// Test render with a valid string in _serialize.
|
51 |
array(
|
52 |
array('data' => array('user' => 'fake', 'list' => array('item1', 'item2'))), |
53 |
'data',
|
54 |
json_encode(array('user' => 'fake', 'list' => array('item1', 'item2'))) |
55 |
), |
56 |
|
57 |
// Test render with a string with an invalid key in _serialize.
|
58 |
array(
|
59 |
array('data' => array('user' => 'fake', 'list' => array('item1', 'item2'))), |
60 |
'no_key',
|
61 |
json_encode(null)
|
62 |
), |
63 |
|
64 |
// Test render with a valid array in _serialize.
|
65 |
array(
|
66 |
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')), |
67 |
array('no', 'user'), |
68 |
json_encode(array('no' => 'nope', 'user' => 'fake')) |
69 |
), |
70 |
|
71 |
// Test render with an empty array in _serialize.
|
72 |
array(
|
73 |
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')), |
74 |
array(),
|
75 |
json_encode(null)
|
76 |
), |
77 |
|
78 |
// Test render with a valid array with an invalid key in _serialize.
|
79 |
array(
|
80 |
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')), |
81 |
array('no', 'user', 'no_key'), |
82 |
json_encode(array('no' => 'nope', 'user' => 'fake')) |
83 |
), |
84 |
|
85 |
// Test render with a valid array with only an invalid key in _serialize.
|
86 |
array(
|
87 |
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')), |
88 |
array('no_key'), |
89 |
json_encode(null)
|
90 |
), |
91 |
|
92 |
// Test render with Null in _serialize (unset).
|
93 |
array(
|
94 |
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')), |
95 |
null,
|
96 |
null
|
97 |
), |
98 |
|
99 |
// Test render with False in _serialize.
|
100 |
array(
|
101 |
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')), |
102 |
false,
|
103 |
json_encode(null)
|
104 |
), |
105 |
|
106 |
// Test render with True in _serialize.
|
107 |
array(
|
108 |
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')), |
109 |
true,
|
110 |
json_encode(null)
|
111 |
), |
112 |
|
113 |
// Test render with empty string in _serialize.
|
114 |
array(
|
115 |
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')), |
116 |
'',
|
117 |
json_encode(null)
|
118 |
), |
119 |
|
120 |
// Test render with a valid array in _serialize and alias.
|
121 |
array(
|
122 |
array('original_name' => 'my epic name', 'user' => 'fake', 'list' => array('item1', 'item2')), |
123 |
array('new_name' => 'original_name', 'user'), |
124 |
json_encode(array('new_name' => 'my epic name', 'user' => 'fake')) |
125 |
), |
126 |
|
127 |
// Test render with an a valid array in _serialize and alias of a null value.
|
128 |
array(
|
129 |
array('null' => null), |
130 |
array('null'), |
131 |
json_encode(array('null' => null)) |
132 |
), |
133 |
|
134 |
// Test render with a False value to be serialized.
|
135 |
array(
|
136 |
array('false' => false), |
137 |
'false',
|
138 |
json_encode(false)
|
139 |
), |
140 |
|
141 |
// Test render with a True value to be serialized.
|
142 |
array(
|
143 |
array('true' => true), |
144 |
'true',
|
145 |
json_encode(true)
|
146 |
), |
147 |
|
148 |
// Test render with an empty string value to be serialized.
|
149 |
array(
|
150 |
array('empty' => ''), |
151 |
'empty',
|
152 |
json_encode('')
|
153 |
), |
154 |
|
155 |
// Test render with a zero value to be serialized.
|
156 |
array(
|
157 |
array('zero' => 0), |
158 |
'zero',
|
159 |
json_encode(0)
|
160 |
), |
161 |
); |
162 |
} |
163 |
|
164 |
/**
|
165 |
* Custom error handler for use while testing methods that use json_encode
|
166 |
* @param int $errno
|
167 |
* @param string $errstr
|
168 |
* @param string $errfile
|
169 |
* @param int $errline
|
170 |
* @param array $errcontext
|
171 |
* @return void
|
172 |
* @throws CakeException
|
173 |
**/
|
174 |
public function jsonEncodeErrorHandler($errno, $errstr, $errfile, $errline, $errcontext) { |
175 |
throw new CakeException($errstr, 0, $errno, $errfile, $errline); |
176 |
} |
177 |
|
178 |
/**
|
179 |
* Test render with a valid string in _serialize.
|
180 |
*
|
181 |
* @dataProvider renderWithoutViewProvider
|
182 |
* @return void
|
183 |
*/
|
184 |
public function testRenderWithoutView($data, $serialize, $expected) { |
185 |
$Request = new CakeRequest(); |
186 |
$Response = new CakeResponse(); |
187 |
$Controller = new Controller($Request, $Response); |
188 |
|
189 |
$Controller->set($data); |
190 |
$Controller->set('_serialize', $serialize); |
191 |
$View = new JsonView($Controller); |
192 |
$output = $View->render(false); |
193 |
|
194 |
$this->assertSame($expected, $output); |
195 |
} |
196 |
|
197 |
/**
|
198 |
* Test render with _jsonOptions setting.
|
199 |
*
|
200 |
* @return void
|
201 |
*/
|
202 |
public function testRenderWithoutViewJsonOptions() { |
203 |
$this->skipIf(!version_compare(PHP_VERSION, '5.3.0', '>='), 'Needs PHP5.3+ for these constants to be tested'); |
204 |
|
205 |
$Request = new CakeRequest(); |
206 |
$Response = new CakeResponse(); |
207 |
$Controller = new Controller($Request, $Response); |
208 |
|
209 |
// Test render with encode <, >, ', &, and " for RFC4627-compliant to be serialized.
|
210 |
$data = array('rfc4627_escape' => '<tag> \'quote\' "double-quote" &'); |
211 |
$serialize = 'rfc4627_escape'; |
212 |
$expected = json_encode('<tag> \'quote\' "double-quote" &', JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT); |
213 |
|
214 |
$Controller->set($data); |
215 |
$Controller->set('_serialize', $serialize); |
216 |
$Controller->set('_jsonOptions', JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT); |
217 |
$View = new JsonView($Controller); |
218 |
$output = $View->render(false); |
219 |
|
220 |
$this->assertSame($expected, $output); |
221 |
} |
222 |
/**
|
223 |
* Test that rendering with _serialize does not load helpers.
|
224 |
*
|
225 |
* @return void
|
226 |
*/
|
227 |
public function testRenderSerializeNoHelpers() { |
228 |
$Request = new CakeRequest(); |
229 |
$Response = new CakeResponse(); |
230 |
$Controller = new Controller($Request, $Response); |
231 |
|
232 |
$Controller->helpers = array('Html'); |
233 |
$Controller->set(array( |
234 |
'tags' => array('cakephp', 'framework'), |
235 |
'_serialize' => 'tags' |
236 |
)); |
237 |
$View = new JsonView($Controller); |
238 |
$View->render();
|
239 |
|
240 |
$this->assertFalse(isset($View->Html), 'No helper loaded.'); |
241 |
} |
242 |
|
243 |
/**
|
244 |
* testJsonpResponse method
|
245 |
*
|
246 |
* @return void
|
247 |
*/
|
248 |
public function testJsonpResponse() { |
249 |
$Request = new CakeRequest(); |
250 |
$Response = new CakeResponse(); |
251 |
$Controller = new Controller($Request, $Response); |
252 |
|
253 |
$data = array('user' => 'fake', 'list' => array('item1', 'item2')); |
254 |
$Controller->set(array( |
255 |
'data' => $data, |
256 |
'_serialize' => 'data', |
257 |
'_jsonp' => true |
258 |
)); |
259 |
$View = new JsonView($Controller); |
260 |
$output = $View->render(false); |
261 |
|
262 |
$this->assertSame(json_encode($data), $output); |
263 |
$this->assertSame('application/json', $Response->type()); |
264 |
|
265 |
$View->request->query = array('callback' => 'jfunc'); |
266 |
$output = $View->render(false); |
267 |
$expected = 'jfunc(' . json_encode($data) . ')'; |
268 |
$this->assertSame($expected, $output); |
269 |
$this->assertSame('application/javascript', $Response->type()); |
270 |
|
271 |
$View->request->query = array('jsonCallback' => 'jfunc'); |
272 |
$View->viewVars['_jsonp'] = 'jsonCallback'; |
273 |
$output = $View->render(false); |
274 |
$expected = 'jfunc(' . json_encode($data) . ')'; |
275 |
$this->assertSame($expected, $output); |
276 |
} |
277 |
|
278 |
/**
|
279 |
* Test render with a View file specified.
|
280 |
*
|
281 |
* @return void
|
282 |
*/
|
283 |
public function testRenderWithView() { |
284 |
App::build(array( |
285 |
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS) |
286 |
)); |
287 |
$Request = new CakeRequest(); |
288 |
$Response = new CakeResponse(); |
289 |
$Controller = new Controller($Request, $Response); |
290 |
$Controller->name = $Controller->viewPath = 'Posts'; |
291 |
|
292 |
$data = array( |
293 |
'User' => array( |
294 |
'username' => 'fake' |
295 |
), |
296 |
'Item' => array( |
297 |
array('name' => 'item1'), |
298 |
array('name' => 'item2') |
299 |
) |
300 |
); |
301 |
$Controller->set('user', $data); |
302 |
$View = new JsonView($Controller); |
303 |
$output = $View->render('index'); |
304 |
|
305 |
$expected = json_encode(array('user' => 'fake', 'list' => array('item1', 'item2'), 'paging' => null)); |
306 |
$this->assertSame($expected, $output); |
307 |
$this->assertSame('application/json', $Response->type()); |
308 |
} |
309 |
|
310 |
/**
|
311 |
* Test render with a View file specified and named parameters.
|
312 |
*
|
313 |
* @return void
|
314 |
*/
|
315 |
public function testRenderWithViewAndNamed() { |
316 |
App::build(array( |
317 |
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS) |
318 |
)); |
319 |
$Request = new CakeRequest(null, false); |
320 |
$Request->params['named'] = array('page' => 2); |
321 |
$Response = new CakeResponse(); |
322 |
$Controller = new Controller($Request, $Response); |
323 |
$Controller->name = $Controller->viewPath = 'Posts'; |
324 |
|
325 |
$data = array( |
326 |
'User' => array( |
327 |
'username' => 'fake' |
328 |
), |
329 |
'Item' => array( |
330 |
array('name' => 'item1'), |
331 |
array('name' => 'item2') |
332 |
) |
333 |
); |
334 |
$Controller->set('user', $data); |
335 |
$Controller->helpers = array('Paginator'); |
336 |
$View = new JsonView($Controller); |
337 |
$output = $View->render('index'); |
338 |
|
339 |
$expected = array('user' => 'fake', 'list' => array('item1', 'item2'), 'paging' => array('page' => 2)); |
340 |
$this->assertSame(json_encode($expected), $output); |
341 |
$this->assertSame('application/json', $Response->type()); |
342 |
|
343 |
$View->request->query = array('jsonCallback' => 'jfunc'); |
344 |
$Controller->set('_jsonp', 'jsonCallback'); |
345 |
$View = new JsonView($Controller); |
346 |
$View->helpers = array('Paginator'); |
347 |
$output = $View->render('index'); |
348 |
$expected['paging']['?']['jsonCallback'] = 'jfunc'; |
349 |
$expected = 'jfunc(' . json_encode($expected) . ')'; |
350 |
$this->assertSame($expected, $output); |
351 |
$this->assertSame('application/javascript', $Response->type()); |
352 |
} |
353 |
|
354 |
/**
|
355 |
* JsonViewTest::testRenderInvalidJSON()
|
356 |
*
|
357 |
* @return void
|
358 |
*/
|
359 |
public function testRenderInvalidJSON() { |
360 |
$Request = new CakeRequest(); |
361 |
$Response = new CakeResponse(); |
362 |
$Controller = new Controller($Request, $Response); |
363 |
|
364 |
// non utf-8 stuff
|
365 |
$data = array('data' => array('foo' => 'bar' . chr('0x97'))); |
366 |
|
367 |
$Controller->set($data); |
368 |
$Controller->set('_serialize', 'data'); |
369 |
$View = new JsonView($Controller); |
370 |
|
371 |
// Use a custom error handler
|
372 |
set_error_handler(array($this, 'jsonEncodeErrorHandler')); |
373 |
|
374 |
try {
|
375 |
$View->render();
|
376 |
restore_error_handler(); |
377 |
$this->fail('Failed asserting that exception of type "CakeException" is thrown.'); |
378 |
} catch (CakeException $e) { |
379 |
restore_error_handler(); |
380 |
$this->assertRegExp('/UTF-8/', $e->getMessage()); |
381 |
return;
|
382 |
} |
383 |
} |
384 |
|
385 |
/**
|
386 |
* JsonViewTest::testRenderJSONBoolFalse()
|
387 |
*
|
388 |
* @return void
|
389 |
*/
|
390 |
public function testRenderJSONBoolFalse() { |
391 |
$Request = new CakeRequest(); |
392 |
$Response = new CakeResponse(); |
393 |
$Controller = new Controller($Request, $Response); |
394 |
|
395 |
// encoding a false, ensure this doesn't trigger exception
|
396 |
$data = false; |
397 |
|
398 |
$Controller->set($data); |
399 |
$Controller->set('_serialize', 'data'); |
400 |
$View = new JsonView($Controller); |
401 |
$output = $View->render(); |
402 |
$this->assertSame('null', $output); |
403 |
} |
404 |
|
405 |
} |