Skip to content

Commit

Permalink
add test case and add case _jsonOption = false
Browse files Browse the repository at this point in the history
  • Loading branch information
slywalker committed Nov 20, 2013
1 parent 03aa24f commit 875035b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
43 changes: 42 additions & 1 deletion Cake/Test/TestCase/View/JsonViewTest.php
Expand Up @@ -46,113 +46,153 @@ public static function renderWithoutViewProvider() {
array(
array('data' => array('user' => 'fake', 'list' => array('item1', 'item2'))),
'data',
null,
json_encode(array('user' => 'fake', 'list' => array('item1', 'item2')))
),

// Test render with a string with an invalid key in _serialize.
array(
array('data' => array('user' => 'fake', 'list' => array('item1', 'item2'))),
'no_key',
null,
json_encode(null)
),

// Test render with a valid array in _serialize.
array(
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')),
array('no', 'user'),
null,
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(),
null,
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'),
null,
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'),
null,
json_encode(null)
),

// Test render with Null in _serialize (unset).
array(
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')),
null,
null,
null
),

// Test render with False in _serialize.
array(
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')),
false,
null,
json_encode(null)
),

// Test render with True in _serialize.
array(
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')),
true,
null,
json_encode(null)
),

// Test render with empty string in _serialize.
array(
array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2')),
'',
null,
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'),
null,
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'),
null,
json_encode(array('null' => null))
),

// Test render with a False value to be serialized.
array(
array('false' => false),
'false',
null,
json_encode(false)
),

// Test render with a True value to be serialized.
array(
array('true' => true),
'true',
null,
json_encode(true)
),

// Test render with an empty string value to be serialized.
array(
array('empty' => ''),
'empty',
null,
json_encode('')
),

// Test render with a zero value to be serialized.
array(
array('zero' => 0),
'zero',
null,
json_encode(0)
),

// Test render with encode <, >, ', &, and " for RFC4627-compliant to be serialized.
array(
array('rfc4627_escape' => '<tag> \'quote\' "double-quote" &'),
'rfc4627_escape',
null,
json_encode('<tag> \'quote\' "double-quote" &', JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT)
),

// Test render with _jsonOptions null to be serialized.
array(
array('noescape' => '<tag> \'quote\' "double-quote" &'),
'noescape',
false,
json_encode('<tag> \'quote\' "double-quote" &')
),

// Test render with setting _jsonOptions to be serialized.
array(
array('rfc4627_escape' => '<tag> \'quote\' "double-quote" &'),
'rfc4627_escape',
JSON_HEX_TAG | JSON_HEX_APOS,
json_encode('<tag> \'quote\' "double-quote" &', JSON_HEX_TAG | JSON_HEX_APOS)
),
);
}

Expand All @@ -162,13 +202,14 @@ public static function renderWithoutViewProvider() {
* @dataProvider renderWithoutViewProvider
* @return void
*/
public function testRenderWithoutView($data, $serialize, $expected) {
public function testRenderWithoutView($data, $serialize, $jsonOptions, $expected) {
$Request = new Request();
$Response = new Response();
$Controller = new Controller($Request, $Response);

$Controller->set($data);
$Controller->set('_serialize', $serialize);
$Controller->set('_jsonOptions', $jsonOptions);
$View = new JsonView($Controller);
$output = $View->render(false);

Expand Down
6 changes: 5 additions & 1 deletion Cake/View/JsonView.php
Expand Up @@ -148,7 +148,11 @@ protected function _serialize($serialize) {

$jsonOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
if (isset($this->viewVars['_jsonOptions'])) {
$jsonOptions = $this->viewVars['_jsonOptions'];
if ($this->viewVars['_jsonOptions'] === false) {
$jsonOptions = 0;
} else {
$jsonOptions = $this->viewVars['_jsonOptions'];
}
}

if (Configure::read('debug')) {
Expand Down

0 comments on commit 875035b

Please sign in to comment.