2011-10-29 16:51:44 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* XmlViewTest file
|
|
|
|
*
|
|
|
|
* PHP 5
|
|
|
|
*
|
2012-04-26 19:49:18 -07:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
2013-02-08 20:59:49 +09:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-10-29 16:51:44 -04:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 21:22:51 +09:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2011-10-29 16:51:44 -04:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
|
|
|
*
|
2013-02-08 20:59:49 +09:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2012-04-26 19:49:18 -07:00
|
|
|
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2011-10-29 16:51:44 -04:00
|
|
|
* @package Cake.Test.Case.View
|
|
|
|
* @since CakePHP(tm) v 2.1.0
|
2013-05-31 00:11:14 +02:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2011-10-29 16:51:44 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
App::uses('Controller', 'Controller');
|
|
|
|
App::uses('CakeRequest', 'Network');
|
|
|
|
App::uses('CakeResponse', 'Network');
|
|
|
|
App::uses('XmlView', 'View');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* XmlViewTest
|
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.View
|
|
|
|
*/
|
|
|
|
class XmlViewTest extends CakeTestCase {
|
|
|
|
|
2013-08-02 13:39:54 +00:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
Configure::write('debug', 0);
|
|
|
|
}
|
|
|
|
|
2011-10-29 16:51:44 -04:00
|
|
|
/**
|
|
|
|
* testRenderWithoutView method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRenderWithoutView() {
|
2011-11-07 22:40:39 -05:00
|
|
|
$Request = new CakeRequest();
|
|
|
|
$Response = new CakeResponse();
|
|
|
|
$Controller = new Controller($Request, $Response);
|
2011-10-29 16:51:44 -04:00
|
|
|
$data = array('users' => array('user' => array('user1', 'user2')));
|
2011-11-22 20:56:25 -05:00
|
|
|
$Controller->set(array('users' => $data, '_serialize' => 'users'));
|
2011-11-07 22:40:39 -05:00
|
|
|
$View = new XmlView($Controller);
|
|
|
|
$output = $View->render(false);
|
2011-10-29 16:51:44 -04:00
|
|
|
|
2012-05-23 11:39:02 +02:00
|
|
|
$this->assertSame(Xml::build($data)->asXML(), $output);
|
2012-04-21 15:02:35 +02:00
|
|
|
$this->assertSame('application/xml', $Response->type());
|
2012-05-23 11:39:02 +02:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
array(
|
|
|
|
'User' => array(
|
|
|
|
'username' => 'user1'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'User' => array(
|
|
|
|
'username' => 'user2'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$Controller->set(array('users' => $data, '_serialize' => 'users'));
|
|
|
|
$View = new XmlView($Controller);
|
|
|
|
$output = $View->render(false);
|
|
|
|
|
2012-06-03 21:11:20 -04:00
|
|
|
$expected = Xml::build(array('response' => array('users' => $data)))->asXML();
|
2012-05-23 11:39:02 +02:00
|
|
|
$this->assertSame($expected, $output);
|
2012-11-02 14:14:49 -04:00
|
|
|
|
|
|
|
$Controller->set('_rootNode', 'custom_name');
|
|
|
|
$View = new XmlView($Controller);
|
|
|
|
$output = $View->render(false);
|
|
|
|
|
|
|
|
$expected = Xml::build(array('custom_name' => array('users' => $data)))->asXML();
|
|
|
|
$this->assertSame($expected, $output);
|
2011-11-17 22:24:49 -05:00
|
|
|
}
|
|
|
|
|
2013-08-27 10:00:29 -04:00
|
|
|
/**
|
|
|
|
* Test that rendering with _serialize does not load helpers
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRenderSerializeNoHelpers() {
|
|
|
|
$Request = new CakeRequest();
|
|
|
|
$Response = new CakeResponse();
|
|
|
|
$Controller = new Controller($Request, $Response);
|
|
|
|
$Controller->helpers = array('Html');
|
|
|
|
$Controller->set(array(
|
|
|
|
'_serialize' => 'tags',
|
|
|
|
'tags' => array('cakephp', 'framework')
|
|
|
|
));
|
|
|
|
$View = new XmlView($Controller);
|
|
|
|
$View->render();
|
|
|
|
$this->assertFalse(isset($View->Html), 'No helper loaded.');
|
|
|
|
}
|
|
|
|
|
2011-11-17 22:24:49 -05:00
|
|
|
/**
|
2011-11-22 20:56:25 -05:00
|
|
|
* Test render with an array in _serialize
|
2011-11-17 22:24:49 -05: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-22 20:56:25 -05:00
|
|
|
$Controller->set('_serialize', array('no', 'user'));
|
2011-11-17 22:24:49 -05:00
|
|
|
$View = new XmlView($Controller);
|
2012-11-02 14:14:49 -04:00
|
|
|
$this->assertSame('application/xml', $Response->type());
|
2011-11-17 22:24:49 -05:00
|
|
|
$output = $View->render(false);
|
|
|
|
$expected = array(
|
2012-03-15 22:50:05 -04:00
|
|
|
'response' => array('no' => $data['no'], 'user' => $data['user'])
|
2011-11-17 22:24:49 -05:00
|
|
|
);
|
2012-04-21 15:02:35 +02:00
|
|
|
$this->assertSame(Xml::build($expected)->asXML(), $output);
|
2012-11-02 14:14:49 -04:00
|
|
|
|
|
|
|
$Controller->set('_rootNode', 'custom_name');
|
|
|
|
$View = new XmlView($Controller);
|
|
|
|
$output = $View->render(false);
|
|
|
|
$expected = array(
|
|
|
|
'custom_name' => array('no' => $data['no'], 'user' => $data['user'])
|
2013-07-06 16:57:46 +00:00
|
|
|
);
|
|
|
|
$this->assertSame(Xml::build($expected)->asXML(), $output);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 XmlView($Controller);
|
|
|
|
$this->assertSame('application/xml', $Response->type());
|
|
|
|
$output = $View->render(false);
|
|
|
|
$expected = array(
|
|
|
|
'response' => array('new_name' => $data['original_name'], 'user' => $data['user'])
|
|
|
|
);
|
|
|
|
$this->assertSame(Xml::build($expected)->asXML(), $output);
|
|
|
|
|
|
|
|
$Controller->set('_rootNode', 'custom_name');
|
|
|
|
$View = new XmlView($Controller);
|
|
|
|
$output = $View->render(false);
|
|
|
|
$expected = array(
|
|
|
|
'custom_name' => array('new_name' => $data['original_name'], 'user' => $data['user'])
|
2012-11-02 14:14:49 -04:00
|
|
|
);
|
|
|
|
$this->assertSame(Xml::build($expected)->asXML(), $output);
|
2011-10-29 16:51:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testRenderWithView method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRenderWithView() {
|
|
|
|
App::build(array('View' => array(
|
|
|
|
CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS
|
|
|
|
)));
|
2011-11-07 22:40:39 -05:00
|
|
|
$Request = new CakeRequest();
|
|
|
|
$Response = new CakeResponse();
|
|
|
|
$Controller = new Controller($Request, $Response);
|
|
|
|
$Controller->name = $Controller->viewPath = 'Posts';
|
|
|
|
|
2011-10-29 16:51:44 -04:00
|
|
|
$data = array(
|
|
|
|
array(
|
|
|
|
'User' => array(
|
|
|
|
'username' => 'user1'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'User' => array(
|
|
|
|
'username' => 'user2'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2011-11-07 22:40:39 -05:00
|
|
|
$Controller->set('users', $data);
|
|
|
|
$View = new XmlView($Controller);
|
2011-11-22 20:52:02 -05:00
|
|
|
$output = $View->render('index');
|
2011-10-29 16:51:44 -04:00
|
|
|
|
2012-05-23 11:39:02 +02:00
|
|
|
$expected = array(
|
2012-05-31 21:14:34 -04:00
|
|
|
'users' => array('user' => array('user1', 'user2'))
|
2012-05-23 11:39:02 +02:00
|
|
|
);
|
|
|
|
$expected = Xml::build($expected)->asXML();
|
|
|
|
$this->assertSame($expected, $output);
|
2012-04-21 15:02:35 +02:00
|
|
|
$this->assertSame('application/xml', $Response->type());
|
2011-11-07 22:40:39 -05:00
|
|
|
$this->assertInstanceOf('HelperCollection', $View->Helpers);
|
2011-10-29 16:51:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|