cakephp2-php8/lib/Cake/Test/Case/View/XmlViewTest.php

267 lines
7.3 KiB
PHP
Raw Normal View History

2011-10-29 20:51:44 +00:00
<?php
/**
* XmlViewTest file
*
2017-06-10 22:15:34 +00:00
* CakePHP(tm) Tests <https://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
2011-10-29 20:51:44 +00:00
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
2011-10-29 20:51:44 +00:00
* Redistributions of files must retain the above copyright notice
*
* @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 20:51:44 +00:00
* @package Cake.Test.Case.View
* @since CakePHP(tm) v 2.1.0
* @license https://opensource.org/licenses/mit-license.php MIT License
2011-10-29 20:51:44 +00: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 {
public function setUp() : void {
parent::setUp();
Configure::write('debug', 0);
}
2011-10-29 20:51:44 +00:00
/**
* testRenderWithoutView method
*
* @return void
*/
public function testRenderWithoutView() {
$Request = new CakeRequest();
$Response = new CakeResponse();
$Controller = new Controller($Request, $Response);
2011-10-29 20:51:44 +00:00
$data = array('users' => array('user' => array('user1', 'user2')));
$Controller->set(array('users' => $data, '_serialize' => 'users'));
$View = new XmlView($Controller);
$output = $View->render(false);
2011-10-29 20:51:44 +00:00
$this->assertSame(Xml::build($data)->asXML(), $output);
$this->assertSame('application/xml', $Response->type());
$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-04 01:11:20 +00:00
$expected = Xml::build(array('response' => array('users' => $data)))->asXML();
$this->assertSame($expected, $output);
$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);
}
/**
* 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.');
}
2015-05-02 12:24:02 +00:00
/**
* Test that rendering with _serialize respects XML options.
*
* @return void
*/
public function testRenderSerializeWithOptions() {
$Request = new CakeRequest();
$Response = new CakeResponse();
$Controller = new Controller($Request, $Response);
2015-05-02 12:26:57 +00:00
$data = array(
'_serialize' => array('tags'),
'_xmlOptions' => array('format' => 'attributes', 'return' => 'domdocument'),
2015-05-02 12:26:57 +00:00
'tags' => array(
'tag' => array(
array(
2015-05-02 12:24:02 +00:00
'id' => '1',
'name' => 'defect'
2015-05-02 12:26:57 +00:00
),
array(
2015-05-02 12:24:02 +00:00
'id' => '2',
'name' => 'enhancement'
2015-05-02 12:26:57 +00:00
)
)
)
);
2015-05-02 12:24:02 +00:00
$Controller->set($data);
$Controller->viewClass = 'Xml';
$View = new XmlView($Controller);
$result = $View->render();
$expected = Xml::build(array('response' => array('tags' => $data['tags'])), $data['_xmlOptions'])->saveXML();
2015-05-02 12:24:02 +00:00
$this->assertSame($expected, $result);
}
/**
2015-05-02 12:24:02 +00:00
* Test that rendering with _serialize can work with string setting.
*
* @return void
*/
public function testRenderSerializeWithString() {
$Request = new CakeRequest();
$Response = new CakeResponse();
$Controller = new Controller($Request, $Response);
2015-05-02 12:26:57 +00:00
$data = array(
2015-05-02 12:24:02 +00:00
'_serialize' => 'tags',
2015-05-02 12:26:57 +00:00
'_xmlOptions' => array('format' => 'attributes'),
'tags' => array(
'tags' => array(
'tag' => array(
array(
2015-05-02 12:24:02 +00:00
'id' => '1',
'name' => 'defect'
2015-05-02 12:26:57 +00:00
),
array(
2015-05-02 12:24:02 +00:00
'id' => '2',
'name' => 'enhancement'
2015-05-02 12:26:57 +00:00
)
)
)
)
);
2015-05-02 12:24:02 +00:00
$Controller->set($data);
$Controller->viewClass = 'Xml';
$View = new XmlView($Controller);
$result = $View->render();
$expected = Xml::build($data['tags'], $data['_xmlOptions'])->asXML();
$this->assertSame($expected, $result);
}
2015-05-02 14:16:27 +00:00
/**
* Test render with an array in _serialize
*
* @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);
$Controller->set('_serialize', array('no', 'user'));
$View = new XmlView($Controller);
$this->assertSame('application/xml', $Response->type());
$output = $View->render(false);
$expected = array(
'response' => array('no' => $data['no'], '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('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'])
);
$this->assertSame(Xml::build($expected)->asXML(), $output);
2011-10-29 20:51:44 +00:00
}
/**
* testRenderWithView method
*
* @return void
*/
public function testRenderWithView() {
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';
2011-10-29 20:51:44 +00:00
$data = array(
array(
'User' => array(
'username' => 'user1'
)
),
array(
'User' => array(
'username' => 'user2'
)
)
);
$Controller->set('users', $data);
$View = new XmlView($Controller);
2011-11-23 01:52:02 +00:00
$output = $View->render('index');
2011-10-29 20:51:44 +00:00
$expected = array(
'users' => array('user' => array('user1', 'user2'))
);
$expected = Xml::build($expected)->asXML();
$this->assertSame($expected, $output);
$this->assertSame('application/xml', $Response->type());
$this->assertInstanceOf('HelperCollection', $View->Helpers);
2011-10-29 20:51:44 +00:00
}
}