2011-10-29 02:01:33 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* JsonViewTest file
|
|
|
|
*
|
2017-06-10 22:15:34 +00:00
|
|
|
* CakePHP(tm) Tests <https://book.cakephp.org/2.0/en/development/testing.html>
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2011-10-29 02:01:33 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2011-10-29 02:01:33 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2011-10-29 02:01:33 +00:00
|
|
|
* @package Cake.Test.Case.View
|
|
|
|
* @since CakePHP(tm) v 2.1.0
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2011-10-29 02:01:33 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
App::uses('Controller', 'Controller');
|
|
|
|
App::uses('CakeRequest', 'Network');
|
|
|
|
App::uses('CakeResponse', 'Network');
|
|
|
|
App::uses('JsonView', 'View');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* JsonViewTest
|
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.View
|
|
|
|
*/
|
|
|
|
class JsonViewTest extends CakeTestCase {
|
|
|
|
|
2015-03-14 13:27:34 +00:00
|
|
|
/**
|
|
|
|
* setUp method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
2013-08-02 13:39:54 +00:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
Configure::write('debug', 0);
|
|
|
|
}
|
|
|
|
|
2011-10-29 02:01:33 +00:00
|
|
|
/**
|
2013-09-05 16:46:55 +00:00
|
|
|
* Generates testRenderWithoutView data.
|
|
|
|
*
|
|
|
|
* Note: array($data, $serialize, expected)
|
2011-10-29 02:01:33 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2013-09-05 16:46:55 +00:00
|
|
|
public static function renderWithoutViewProvider() {
|
|
|
|
return array(
|
|
|
|
// Test render with a valid string in _serialize.
|
|
|
|
array(
|
|
|
|
array('data' => array('user' => 'fake', 'list' => array('item1', 'item2'))),
|
|
|
|
'data',
|
|
|
|
json_encode(array('user' => 'fake', 'list' => array('item1', 'item2')))
|
|
|
|
),
|
2011-10-29 02:01:33 +00:00
|
|
|
|
2013-09-05 16:46:55 +00:00
|
|
|
// Test render with a string with an invalid key in _serialize.
|
|
|
|
array(
|
|
|
|
array('data' => array('user' => 'fake', 'list' => array('item1', 'item2'))),
|
|
|
|
'no_key',
|
|
|
|
json_encode(null)
|
|
|
|
),
|
2011-10-29 02:01:33 +00:00
|
|
|
|
2013-09-05 16:46:55 +00:00
|
|
|
// Test render with a valid array in _serialize.
|
|
|
|
array(
|
|
|
|
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')),
|
|
|
|
array('no', 'user'),
|
|
|
|
json_encode(array('no' => 'nope', 'user' => 'fake'))
|
|
|
|
),
|
|
|
|
|
|
|
|
// Test render with an empty array in _serialize.
|
|
|
|
array(
|
|
|
|
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')),
|
|
|
|
array(),
|
|
|
|
json_encode(null)
|
|
|
|
),
|
|
|
|
|
|
|
|
// Test render with a valid array with an invalid key in _serialize.
|
|
|
|
array(
|
|
|
|
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')),
|
|
|
|
array('no', 'user', 'no_key'),
|
|
|
|
json_encode(array('no' => 'nope', 'user' => 'fake'))
|
|
|
|
),
|
|
|
|
|
|
|
|
// Test render with a valid array with only an invalid key in _serialize.
|
|
|
|
array(
|
|
|
|
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')),
|
|
|
|
array('no_key'),
|
|
|
|
json_encode(null)
|
|
|
|
),
|
|
|
|
|
|
|
|
// Test render with Null in _serialize (unset).
|
|
|
|
array(
|
|
|
|
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')),
|
|
|
|
null,
|
|
|
|
null
|
|
|
|
),
|
|
|
|
|
|
|
|
// Test render with False in _serialize.
|
|
|
|
array(
|
|
|
|
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')),
|
|
|
|
false,
|
|
|
|
json_encode(null)
|
|
|
|
),
|
|
|
|
|
|
|
|
// Test render with True in _serialize.
|
|
|
|
array(
|
|
|
|
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')),
|
|
|
|
true,
|
|
|
|
json_encode(null)
|
|
|
|
),
|
|
|
|
|
|
|
|
// Test render with empty string in _serialize.
|
|
|
|
array(
|
|
|
|
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')),
|
|
|
|
'',
|
|
|
|
json_encode(null)
|
|
|
|
),
|
|
|
|
|
|
|
|
// Test render with a valid array in _serialize and alias.
|
|
|
|
array(
|
|
|
|
array('original_name' => 'my epic name', 'user' => 'fake', 'list' => array('item1', 'item2')),
|
|
|
|
array('new_name' => 'original_name', 'user'),
|
|
|
|
json_encode(array('new_name' => 'my epic name', 'user' => 'fake'))
|
|
|
|
),
|
|
|
|
|
|
|
|
// Test render with an a valid array in _serialize and alias of a null value.
|
|
|
|
array(
|
|
|
|
array('null' => null),
|
|
|
|
array('null'),
|
|
|
|
json_encode(array('null' => null))
|
|
|
|
),
|
|
|
|
|
|
|
|
// Test render with a False value to be serialized.
|
|
|
|
array(
|
|
|
|
array('false' => false),
|
|
|
|
'false',
|
|
|
|
json_encode(false)
|
|
|
|
),
|
|
|
|
|
|
|
|
// Test render with a True value to be serialized.
|
|
|
|
array(
|
|
|
|
array('true' => true),
|
|
|
|
'true',
|
|
|
|
json_encode(true)
|
|
|
|
),
|
|
|
|
|
|
|
|
// Test render with an empty string value to be serialized.
|
|
|
|
array(
|
|
|
|
array('empty' => ''),
|
|
|
|
'empty',
|
|
|
|
json_encode('')
|
|
|
|
),
|
|
|
|
|
|
|
|
// Test render with a zero value to be serialized.
|
|
|
|
array(
|
|
|
|
array('zero' => 0),
|
|
|
|
'zero',
|
|
|
|
json_encode(0)
|
|
|
|
),
|
|
|
|
);
|
2013-08-27 14:00:29 +00:00
|
|
|
}
|
|
|
|
|
2015-03-14 13:12:09 +00:00
|
|
|
/**
|
|
|
|
* Custom error handler for use while testing methods that use json_encode
|
|
|
|
* @param int $errno
|
|
|
|
* @param string $errstr
|
|
|
|
* @param string $errfile
|
|
|
|
* @param int $errline
|
|
|
|
* @param array $errcontext
|
|
|
|
* @return void
|
|
|
|
* @throws CakeException
|
|
|
|
**/
|
|
|
|
public function jsonEncodeErrorHandler($errno, $errstr, $errfile, $errline, $errcontext) {
|
|
|
|
throw new CakeException($errstr, 0, $errno, $errfile, $errline);
|
|
|
|
}
|
|
|
|
|
2011-11-18 03:24:49 +00:00
|
|
|
/**
|
2013-09-05 16:46:55 +00:00
|
|
|
* Test render with a valid string in _serialize.
|
2011-11-18 03:24:49 +00:00
|
|
|
*
|
2013-09-05 16:46:55 +00:00
|
|
|
* @dataProvider renderWithoutViewProvider
|
2011-11-18 03:24:49 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2013-09-05 16:46:55 +00:00
|
|
|
public function testRenderWithoutView($data, $serialize, $expected) {
|
2011-11-18 03:24:49 +00:00
|
|
|
$Request = new CakeRequest();
|
|
|
|
$Response = new CakeResponse();
|
|
|
|
$Controller = new Controller($Request, $Response);
|
2013-09-05 16:46:55 +00:00
|
|
|
|
2011-11-18 03:24:49 +00:00
|
|
|
$Controller->set($data);
|
2013-09-05 16:46:55 +00:00
|
|
|
$Controller->set('_serialize', $serialize);
|
2011-11-18 03:24:49 +00:00
|
|
|
$View = new JsonView($Controller);
|
|
|
|
$output = $View->render(false);
|
|
|
|
|
2013-09-05 16:46:55 +00:00
|
|
|
$this->assertSame($expected, $output);
|
2011-11-18 03:24:49 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 13:51:13 +00:00
|
|
|
/**
|
|
|
|
* Test render with _jsonOptions setting.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRenderWithoutViewJsonOptions() {
|
|
|
|
$this->skipIf(!version_compare(PHP_VERSION, '5.3.0', '>='), 'Needs PHP5.3+ for these constants to be tested');
|
|
|
|
|
|
|
|
$Request = new CakeRequest();
|
|
|
|
$Response = new CakeResponse();
|
|
|
|
$Controller = new Controller($Request, $Response);
|
|
|
|
|
|
|
|
// Test render with encode <, >, ', &, and " for RFC4627-compliant to be serialized.
|
|
|
|
$data = array('rfc4627_escape' => '<tag> \'quote\' "double-quote" &');
|
|
|
|
$serialize = 'rfc4627_escape';
|
|
|
|
$expected = json_encode('<tag> \'quote\' "double-quote" &', JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
|
|
|
|
|
|
|
|
$Controller->set($data);
|
|
|
|
$Controller->set('_serialize', $serialize);
|
|
|
|
$Controller->set('_jsonOptions', JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
|
|
|
|
$View = new JsonView($Controller);
|
|
|
|
$output = $View->render(false);
|
|
|
|
|
|
|
|
$this->assertSame($expected, $output);
|
|
|
|
}
|
2013-07-06 16:57:46 +00:00
|
|
|
/**
|
2013-09-05 16:46:55 +00:00
|
|
|
* Test that rendering with _serialize does not load helpers.
|
2013-07-06 16:57:46 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2013-09-05 16:46:55 +00:00
|
|
|
public function testRenderSerializeNoHelpers() {
|
2013-07-06 16:57:46 +00:00
|
|
|
$Request = new CakeRequest();
|
|
|
|
$Response = new CakeResponse();
|
|
|
|
$Controller = new Controller($Request, $Response);
|
2013-09-05 16:46:55 +00:00
|
|
|
|
|
|
|
$Controller->helpers = array('Html');
|
|
|
|
$Controller->set(array(
|
|
|
|
'tags' => array('cakephp', 'framework'),
|
|
|
|
'_serialize' => 'tags'
|
|
|
|
));
|
2013-07-06 16:57:46 +00:00
|
|
|
$View = new JsonView($Controller);
|
2013-09-05 16:46:55 +00:00
|
|
|
$View->render();
|
2013-07-06 16:57:46 +00:00
|
|
|
|
2013-09-05 16:46:55 +00:00
|
|
|
$this->assertFalse(isset($View->Html), 'No helper loaded.');
|
2013-07-06 16:57:46 +00:00
|
|
|
}
|
|
|
|
|
2013-02-02 07:55:27 +00:00
|
|
|
/**
|
|
|
|
* testJsonpResponse method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testJsonpResponse() {
|
|
|
|
$Request = new CakeRequest();
|
|
|
|
$Response = new CakeResponse();
|
|
|
|
$Controller = new Controller($Request, $Response);
|
2013-09-05 16:46:55 +00:00
|
|
|
|
2013-02-02 07:55:27 +00:00
|
|
|
$data = array('user' => 'fake', 'list' => array('item1', 'item2'));
|
2013-09-05 16:46:55 +00:00
|
|
|
$Controller->set(array(
|
|
|
|
'data' => $data,
|
|
|
|
'_serialize' => 'data',
|
|
|
|
'_jsonp' => true
|
|
|
|
));
|
2013-02-02 07:55:27 +00:00
|
|
|
$View = new JsonView($Controller);
|
|
|
|
$output = $View->render(false);
|
|
|
|
|
|
|
|
$this->assertSame(json_encode($data), $output);
|
|
|
|
$this->assertSame('application/json', $Response->type());
|
|
|
|
|
|
|
|
$View->request->query = array('callback' => 'jfunc');
|
|
|
|
$output = $View->render(false);
|
|
|
|
$expected = 'jfunc(' . json_encode($data) . ')';
|
|
|
|
$this->assertSame($expected, $output);
|
|
|
|
$this->assertSame('application/javascript', $Response->type());
|
|
|
|
|
|
|
|
$View->request->query = array('jsonCallback' => 'jfunc');
|
|
|
|
$View->viewVars['_jsonp'] = 'jsonCallback';
|
|
|
|
$output = $View->render(false);
|
|
|
|
$expected = 'jfunc(' . json_encode($data) . ')';
|
|
|
|
$this->assertSame($expected, $output);
|
|
|
|
}
|
|
|
|
|
2011-10-29 02:01:33 +00:00
|
|
|
/**
|
2013-09-05 16:46:55 +00:00
|
|
|
* Test render with a View file specified.
|
2011-10-29 02:01:33 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRenderWithView() {
|
2013-09-05 16:46:55 +00:00
|
|
|
App::build(array(
|
|
|
|
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
|
|
|
|
));
|
|
|
|
$Request = new CakeRequest();
|
|
|
|
$Response = new CakeResponse();
|
|
|
|
$Controller = new Controller($Request, $Response);
|
|
|
|
$Controller->name = $Controller->viewPath = 'Posts';
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'User' => array(
|
|
|
|
'username' => 'fake'
|
|
|
|
),
|
|
|
|
'Item' => array(
|
|
|
|
array('name' => 'item1'),
|
|
|
|
array('name' => 'item2')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$Controller->set('user', $data);
|
|
|
|
$View = new JsonView($Controller);
|
|
|
|
$output = $View->render('index');
|
|
|
|
|
|
|
|
$expected = json_encode(array('user' => 'fake', 'list' => array('item1', 'item2'), 'paging' => null));
|
|
|
|
$this->assertSame($expected, $output);
|
|
|
|
$this->assertSame('application/json', $Response->type());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test render with a View file specified and named parameters.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRenderWithViewAndNamed() {
|
2011-11-08 02:46:52 +00:00
|
|
|
App::build(array(
|
|
|
|
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
|
|
|
|
));
|
2013-03-14 11:44:11 +00:00
|
|
|
$Request = new CakeRequest(null, false);
|
2012-08-22 11:13:49 +00:00
|
|
|
$Request->params['named'] = array('page' => 2);
|
2011-11-07 03:12:57 +00:00
|
|
|
$Response = new CakeResponse();
|
|
|
|
$Controller = new Controller($Request, $Response);
|
2011-11-08 02:46:52 +00:00
|
|
|
$Controller->name = $Controller->viewPath = 'Posts';
|
|
|
|
|
2011-10-29 02:01:33 +00:00
|
|
|
$data = array(
|
|
|
|
'User' => array(
|
|
|
|
'username' => 'fake'
|
|
|
|
),
|
|
|
|
'Item' => array(
|
|
|
|
array('name' => 'item1'),
|
|
|
|
array('name' => 'item2')
|
|
|
|
)
|
|
|
|
);
|
2011-11-07 03:12:57 +00:00
|
|
|
$Controller->set('user', $data);
|
2013-08-27 02:43:28 +00:00
|
|
|
$Controller->helpers = array('Paginator');
|
2011-11-07 03:12:57 +00:00
|
|
|
$View = new JsonView($Controller);
|
|
|
|
$output = $View->render('index');
|
2011-10-29 02:01:33 +00:00
|
|
|
|
2013-02-02 07:55:27 +00:00
|
|
|
$expected = array('user' => 'fake', 'list' => array('item1', 'item2'), 'paging' => array('page' => 2));
|
|
|
|
$this->assertSame(json_encode($expected), $output);
|
2012-04-21 13:02:35 +00:00
|
|
|
$this->assertSame('application/json', $Response->type());
|
2013-02-02 07:55:27 +00:00
|
|
|
|
|
|
|
$View->request->query = array('jsonCallback' => 'jfunc');
|
|
|
|
$Controller->set('_jsonp', 'jsonCallback');
|
|
|
|
$View = new JsonView($Controller);
|
|
|
|
$View->helpers = array('Paginator');
|
|
|
|
$output = $View->render('index');
|
|
|
|
$expected['paging']['?']['jsonCallback'] = 'jfunc';
|
|
|
|
$expected = 'jfunc(' . json_encode($expected) . ')';
|
|
|
|
$this->assertSame($expected, $output);
|
|
|
|
$this->assertSame('application/javascript', $Response->type());
|
2011-10-29 02:01:33 +00:00
|
|
|
}
|
2015-03-13 15:52:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* JsonViewTest::testRenderInvalidJSON()
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRenderInvalidJSON() {
|
|
|
|
$Request = new CakeRequest();
|
|
|
|
$Response = new CakeResponse();
|
|
|
|
$Controller = new Controller($Request, $Response);
|
|
|
|
|
|
|
|
// non utf-8 stuff
|
2015-12-28 22:42:56 +00:00
|
|
|
$bar = 'bar';
|
|
|
|
$bar .= chr(0x97);
|
|
|
|
$data = array('data' => array('foo' => $bar));
|
2015-03-14 13:16:05 +00:00
|
|
|
|
2015-03-13 15:52:22 +00:00
|
|
|
$Controller->set($data);
|
|
|
|
$Controller->set('_serialize', 'data');
|
|
|
|
$View = new JsonView($Controller);
|
2015-03-15 10:36:06 +00:00
|
|
|
|
|
|
|
// Use a custom error handler
|
|
|
|
set_error_handler(array($this, 'jsonEncodeErrorHandler'));
|
|
|
|
|
|
|
|
try {
|
|
|
|
$View->render();
|
|
|
|
restore_error_handler();
|
|
|
|
$this->fail('Failed asserting that exception of type "CakeException" is thrown.');
|
|
|
|
} catch (CakeException $e) {
|
|
|
|
restore_error_handler();
|
2015-03-15 11:01:51 +00:00
|
|
|
$this->assertRegExp('/UTF-8/', $e->getMessage());
|
2015-03-15 10:36:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-03-13 15:52:22 +00:00
|
|
|
}
|
2015-03-13 16:12:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* JsonViewTest::testRenderJSONBoolFalse()
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRenderJSONBoolFalse() {
|
|
|
|
$Request = new CakeRequest();
|
|
|
|
$Response = new CakeResponse();
|
|
|
|
$Controller = new Controller($Request, $Response);
|
|
|
|
|
2015-03-13 16:21:54 +00:00
|
|
|
// encoding a false, ensure this doesn't trigger exception
|
2015-03-13 16:12:21 +00:00
|
|
|
$data = false;
|
|
|
|
|
|
|
|
$Controller->set($data);
|
|
|
|
$Controller->set('_serialize', 'data');
|
|
|
|
$View = new JsonView($Controller);
|
|
|
|
$output = $View->render();
|
|
|
|
$this->assertSame('null', $output);
|
2015-03-13 16:13:29 +00:00
|
|
|
}
|
|
|
|
|
2011-10-29 02:01:33 +00:00
|
|
|
}
|