2011-10-29 02:01:33 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* JsonViewTest file
|
|
|
|
*
|
|
|
|
* PHP 5
|
|
|
|
*
|
2012-04-27 02:49:18 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://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
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2012-04-27 02:49:18 +00:00
|
|
|
* @link http://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
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.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 {
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* testRenderWithoutView method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRenderWithoutView() {
|
2011-11-07 03:12:57 +00:00
|
|
|
$Request = new CakeRequest();
|
|
|
|
$Response = new CakeResponse();
|
|
|
|
$Controller = new Controller($Request, $Response);
|
2011-10-29 02:01:33 +00:00
|
|
|
$data = array('user' => 'fake', 'list' => array('item1', 'item2'));
|
2011-11-23 01:56:25 +00:00
|
|
|
$Controller->set(array('data' => $data, '_serialize' => 'data'));
|
2011-11-07 03:12:57 +00:00
|
|
|
$View = new JsonView($Controller);
|
|
|
|
$output = $View->render(false);
|
2011-10-29 02:01:33 +00:00
|
|
|
|
2012-04-21 13:02:35 +00:00
|
|
|
$this->assertSame(json_encode($data), $output);
|
|
|
|
$this->assertSame('application/json', $Response->type());
|
2011-10-29 02:01:33 +00:00
|
|
|
}
|
|
|
|
|
2011-11-18 03:24:49 +00:00
|
|
|
/**
|
2011-11-23 01:56:25 +00:00
|
|
|
* Test render with an array in _serialize
|
2011-11-18 03:24:49 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRenderWithoutViewMultiple() {
|
|
|
|
$Request = new CakeRequest();
|
|
|
|
$Response = new CakeResponse();
|
|
|
|
$Controller = new Controller($Request, $Response);
|
|
|
|
$data = array('no' => 'nope', 'user' => 'fake', 'list' => array('item1', 'item2'));
|
|
|
|
$Controller->set($data);
|
2011-11-23 01:56:25 +00:00
|
|
|
$Controller->set('_serialize', array('no', 'user'));
|
2011-11-18 03:24:49 +00:00
|
|
|
$View = new JsonView($Controller);
|
|
|
|
$output = $View->render(false);
|
|
|
|
|
2012-04-21 13:02:35 +00:00
|
|
|
$this->assertSame(json_encode(array('no' => $data['no'], 'user' => $data['user'])), $output);
|
|
|
|
$this->assertSame('application/json', $Response->type());
|
2011-11-18 03:24:49 +00:00
|
|
|
}
|
|
|
|
|
2013-07-06 16:57:46 +00:00
|
|
|
/**
|
|
|
|
* Test render with an array in _serialize and alias
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRenderWithoutViewMultipleAndAlias() {
|
|
|
|
$Request = new CakeRequest();
|
|
|
|
$Response = new CakeResponse();
|
|
|
|
$Controller = new Controller($Request, $Response);
|
|
|
|
$data = array('original_name' => 'my epic name', 'user' => 'fake', 'list' => array('item1', 'item2'));
|
|
|
|
$Controller->set($data);
|
|
|
|
$Controller->set('_serialize', array('new_name' => 'original_name', 'user'));
|
|
|
|
$View = new JsonView($Controller);
|
|
|
|
$output = $View->render(false);
|
|
|
|
|
|
|
|
$this->assertSame(json_encode(array('new_name' => $data['original_name'], 'user' => $data['user'])), $output);
|
|
|
|
$this->assertSame('application/json', $Response->type());
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
$data = array('user' => 'fake', 'list' => array('item1', 'item2'));
|
|
|
|
$Controller->set(array('data' => $data, '_serialize' => 'data', '_jsonp' => true));
|
|
|
|
$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
|
|
|
/**
|
|
|
|
* testRenderWithView method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRenderWithView() {
|
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);
|
|
|
|
$View = new JsonView($Controller);
|
2012-08-22 11:13:49 +00:00
|
|
|
$View->helpers = array('Paginator');
|
2011-11-07 03:12:57 +00:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
}
|