From 4c8374d8309482d3eb323181a31802493769a0f9 Mon Sep 17 00:00:00 2001 From: thinkingmedia Date: Sat, 22 Oct 2016 12:59:28 -0400 Subject: [PATCH] adds support for NAN and INF rendered as zero --- src/View/JsonView.php | 4 +++- src/View/View.php | 2 ++ tests/TestCase/View/JsonViewTest.php | 31 ++++++++++++++++++++++------ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/View/JsonView.php b/src/View/JsonView.php index d210e61d781..c2960e943ae 100644 --- a/src/View/JsonView.php +++ b/src/View/JsonView.php @@ -135,7 +135,9 @@ protected function _serialize($serialize) { $data = $this->_dataToSerialize($serialize); - $jsonOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT; + $jsonOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | + JSON_ERROR_INF_OR_NAN | JSON_PARTIAL_OUTPUT_ON_ERROR; + if (isset($this->viewVars['_jsonOptions'])) { if ($this->viewVars['_jsonOptions'] === false) { $jsonOptions = 0; diff --git a/src/View/View.php b/src/View/View.php index 053e29d4033..d01b2f286ba 100644 --- a/src/View/View.php +++ b/src/View/View.php @@ -67,6 +67,8 @@ * @property \Cake\View\Helper\TimeHelper $Time * @property \Cake\View\Helper\UrlHelper $Url * @property \Cake\View\ViewBlock $Blocks + * @property string $view + * @property string $viewPath */ class View implements EventDispatcherInterface { diff --git a/tests/TestCase/View/JsonViewTest.php b/tests/TestCase/View/JsonViewTest.php index 94e8277914d..a2bd8373671 100644 --- a/tests/TestCase/View/JsonViewTest.php +++ b/tests/TestCase/View/JsonViewTest.php @@ -39,7 +39,7 @@ public function setUp() * * Note: array($data, $serialize, expected) * - * @return void + * @return array */ public static function renderWithoutViewProvider() { @@ -203,14 +203,33 @@ public static function renderWithoutViewProvider() JSON_HEX_TAG | JSON_HEX_APOS, json_encode(' \'quote\' "double-quote" &', JSON_HEX_TAG | JSON_HEX_APOS) ], + + // Test render of NAN + [ + ['value' => NAN], + true, + null, + '{"value":0}' + ], + + // Test render of INF + [ + ['value' => INF], + true, + null, + '{"value":0}' + ], ]; } /** * Test render with a valid string in _serialize. * + * @param array $data + * @param string|null $serialize + * @param int|bool|null $jsonOptions + * @param string $expected * @dataProvider renderWithoutViewProvider - * @return void */ public function testRenderWithoutView($data, $serialize, $jsonOptions, $expected) { @@ -221,7 +240,7 @@ public function testRenderWithoutView($data, $serialize, $jsonOptions, $expected $Controller->set($data); $Controller->set('_serialize', $serialize); $Controller->set('_jsonOptions', $jsonOptions); - $Controller->viewClass = 'Json'; + $Controller->viewBuilder()->className('Json'); $View = $Controller->createView(); $output = $View->render(false); @@ -244,7 +263,7 @@ public function testRenderSerializeNoHelpers() 'tags' => ['cakephp', 'framework'], '_serialize' => 'tags' ]); - $Controller->viewClass = 'Json'; + $Controller->viewBuilder()->className('Json'); $View = $Controller->createView(); $View->render(); @@ -268,7 +287,7 @@ public function testJsonpResponse() '_serialize' => 'data', '_jsonp' => true ]); - $Controller->viewClass = 'Json'; + $Controller->viewBuilder()->className('Json'); $View = $Controller->createView(); $output = $View->render(false); @@ -310,7 +329,7 @@ public function testRenderWithView() ] ]; $Controller->set('user', $data); - $Controller->viewClass = 'Json'; + $Controller->viewBuilder()->className('Json'); $View = $Controller->createView(); $View->viewPath = $Controller->name; $output = $View->render('index');