| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP Project
- * @since 1.2.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Controller;
- use Cake\Controller\Component;
- use Cake\Controller\Controller;
- use Cake\Core\Configure;
- use Cake\Core\Plugin;
- use Cake\Event\Event;
- use Cake\Http\Response;
- use Cake\Http\ServerRequest;
- use Cake\ORM\TableRegistry;
- use Cake\Routing\Router;
- use Cake\TestSuite\TestCase;
- use TestApp\Controller\Admin\PostsController;
- use TestPlugin\Controller\TestPluginController;
- /**
- * AppController class
- */
- class ControllerTestAppController extends Controller
- {
- /**
- * helpers property
- *
- * @var array
- */
- public $helpers = ['Html'];
- /**
- * modelClass property
- *
- * @var string
- */
- public $modelClass = 'Posts';
- /**
- * components property
- *
- * @var array
- */
- public $components = ['Cookie'];
- }
- /**
- * TestController class
- */
- class TestController extends ControllerTestAppController
- {
- /**
- * Theme property
- *
- * @var string
- */
- public $theme = 'Foo';
- /**
- * helpers property
- *
- * @var array
- */
- public $helpers = ['Html'];
- /**
- * components property
- *
- * @var array
- */
- public $components = ['Security'];
- /**
- * modelClass property
- *
- * @var string
- */
- public $modelClass = 'Comments';
- /**
- * beforeFilter handler
- *
- * @param \Cake\Event\Event $event
- * @return void
- */
- public function beforeFilter(Event $event)
- {
- }
- /**
- * index method
- *
- * @param mixed $testId
- * @param mixed $testTwoId
- * @return void
- */
- public function index($testId, $testTwoId)
- {
- $this->request->data = [
- 'testId' => $testId,
- 'test2Id' => $testTwoId
- ];
- }
- /**
- * view method
- *
- * @param mixed $testId
- * @param mixed $testTwoId
- * @return void
- */
- public function view($testId, $testTwoId)
- {
- $this->request->data = [
- 'testId' => $testId,
- 'test2Id' => $testTwoId
- ];
- }
- public function returner()
- {
- return 'I am from the controller.';
- }
- //@codingStandardsIgnoreStart
- protected function protected_m()
- {
- }
- private function private_m()
- {
- }
- public function _hidden()
- {
- }
- //@codingStandardsIgnoreEnd
- public function admin_add()
- {
- }
- }
- /**
- * TestComponent class
- */
- class TestComponent extends Component
- {
- /**
- * beforeRedirect method
- *
- * @return void
- */
- public function beforeRedirect()
- {
- }
- /**
- * initialize method
- *
- * @param array $config
- * @return void
- */
- public function initialize(array $config)
- {
- }
- /**
- * startup method
- *
- * @param Event $event
- * @return void
- */
- public function startup(Event $event)
- {
- }
- /**
- * shutdown method
- *
- * @param Event $event
- * @return void
- */
- public function shutdown(Event $event)
- {
- }
- /**
- * beforeRender callback
- *
- * @param Event $event
- * @return void
- */
- public function beforeRender(Event $event)
- {
- $controller = $event->subject();
- if ($this->viewclass) {
- $controller->viewClass = $this->viewclass;
- }
- }
- }
- /**
- * AnotherTestController class
- */
- class AnotherTestController extends ControllerTestAppController
- {
- }
- /**
- * ControllerTest class
- */
- class ControllerTest extends TestCase
- {
- /**
- * fixtures property
- *
- * @var array
- */
- public $fixtures = [
- 'core.comments',
- 'core.posts'
- ];
- /**
- * reset environment.
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- static::setAppNamespace();
- Router::reload();
- }
- /**
- * tearDown
- *
- * @return void
- */
- public function tearDown()
- {
- parent::tearDown();
- Plugin::unload();
- }
- /**
- * test autoload modelClass
- *
- * @return void
- */
- public function testTableAutoload()
- {
- $request = new ServerRequest('controller_posts/index');
- $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
- $Controller = new Controller($request, $response);
- $Controller->modelClass = 'SiteArticles';
- $this->assertFalse($Controller->Articles);
- $this->assertInstanceOf(
- 'Cake\ORM\Table',
- $Controller->SiteArticles
- );
- unset($Controller->SiteArticles);
- $Controller->modelClass = 'Articles';
- $this->assertFalse($Controller->SiteArticles);
- $this->assertInstanceOf(
- 'TestApp\Model\Table\ArticlesTable',
- $Controller->Articles
- );
- }
- /**
- * testLoadModel method
- *
- * @return void
- */
- public function testLoadModel()
- {
- $request = new ServerRequest('controller_posts/index');
- $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
- $Controller = new Controller($request, $response);
- $this->assertFalse(isset($Controller->Articles));
- $result = $Controller->loadModel('Articles');
- $this->assertInstanceOf(
- 'TestApp\Model\Table\ArticlesTable',
- $result
- );
- $this->assertInstanceOf(
- 'TestApp\Model\Table\ArticlesTable',
- $Controller->Articles
- );
- }
- /**
- * testLoadModel method from a plugin controller
- *
- * @return void
- */
- public function testLoadModelInPlugins()
- {
- Plugin::load('TestPlugin');
- $Controller = new TestPluginController();
- $Controller->plugin = 'TestPlugin';
- $this->assertFalse(isset($Controller->TestPluginComments));
- $result = $Controller->loadModel('TestPlugin.TestPluginComments');
- $this->assertInstanceOf(
- 'TestPlugin\Model\Table\TestPluginCommentsTable',
- $result
- );
- $this->assertInstanceOf(
- 'TestPlugin\Model\Table\TestPluginCommentsTable',
- $Controller->TestPluginComments
- );
- }
- /**
- * Test that the constructor sets modelClass properly.
- *
- * @return void
- */
- public function testConstructSetModelClass()
- {
- Plugin::load('TestPlugin');
- $request = new ServerRequest();
- $response = new Response();
- $controller = new \TestApp\Controller\PostsController($request, $response);
- $this->assertEquals('Posts', $controller->modelClass);
- $this->assertInstanceOf('Cake\ORM\Table', $controller->Posts);
- $controller = new \TestApp\Controller\Admin\PostsController($request, $response);
- $this->assertEquals('Posts', $controller->modelClass);
- $this->assertInstanceOf('Cake\ORM\Table', $controller->Posts);
- $request->params['plugin'] = 'TestPlugin';
- $controller = new \TestPlugin\Controller\Admin\CommentsController($request, $response);
- $this->assertEquals('TestPlugin.Comments', $controller->modelClass);
- $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $controller->Comments);
- }
- /**
- * testConstructClassesWithComponents method
- *
- * @return void
- */
- public function testConstructClassesWithComponents()
- {
- Plugin::load('TestPlugin');
- $Controller = new TestPluginController(new ServerRequest(), new Response());
- $Controller->loadComponent('TestPlugin.Other');
- $this->assertInstanceOf('TestPlugin\Controller\Component\OtherComponent', $Controller->Other);
- }
- /**
- * testRender method
- *
- * @return void
- */
- public function testRender()
- {
- Plugin::load('TestPlugin');
- $request = new ServerRequest('controller_posts/index');
- $request->params['action'] = 'index';
- $Controller = new Controller($request, new Response());
- $Controller->viewBuilder()->templatePath('Posts');
- $result = $Controller->render('index');
- $this->assertRegExp('/posts index/', (string)$result);
- $Controller->viewBuilder()->template('index');
- $result = $Controller->render();
- $this->assertRegExp('/posts index/', (string)$result);
- $result = $Controller->render('/Element/test_element');
- $this->assertRegExp('/this is the test element/', (string)$result);
- }
- /**
- * test view rendering changing response
- *
- * @return void
- */
- public function testRenderViewChangesResponse()
- {
- $request = new ServerRequest('controller_posts/index');
- $request->params['action'] = 'header';
- $controller = new Controller($request, new Response());
- $controller->viewBuilder()->templatePath('Posts');
- $result = $controller->render('header');
- $this->assertContains('header template', (string)$result);
- $this->assertTrue($controller->response->hasHeader('X-view-template'));
- $this->assertSame('yes', $controller->response->getHeaderLine('X-view-template'));
- }
- /**
- * test that a component beforeRender can change the controller view class.
- *
- * @return void
- */
- public function testBeforeRenderCallbackChangingViewClass()
- {
- $Controller = new Controller(new ServerRequest, new Response());
- $Controller->getEventManager()->on('Controller.beforeRender', function (Event $event) {
- $controller = $event->subject();
- $controller->viewClass = 'Json';
- });
- $Controller->set([
- 'test' => 'value',
- '_serialize' => ['test']
- ]);
- $debug = Configure::read('debug');
- Configure::write('debug', false);
- $result = $Controller->render('index');
- $this->assertEquals('{"test":"value"}', $result->body());
- Configure::write('debug', $debug);
- }
- /**
- * test that a component beforeRender can change the controller view class.
- *
- * @return void
- */
- public function testBeforeRenderEventCancelsRender()
- {
- $Controller = new Controller(new ServerRequest, new Response());
- $Controller->getEventManager()->on('Controller.beforeRender', function (Event $event) {
- return false;
- });
- $result = $Controller->render('index');
- $this->assertInstanceOf('Cake\Http\Response', $result);
- }
- /**
- * Generates status codes for redirect test.
- *
- * @return void
- */
- public static function statusCodeProvider()
- {
- return [
- [300, 'Multiple Choices'],
- [301, 'Moved Permanently'],
- [302, 'Found'],
- [303, 'See Other'],
- [304, 'Not Modified'],
- [305, 'Use Proxy'],
- [307, 'Temporary Redirect'],
- [403, 'Forbidden'],
- ];
- }
- /**
- * testRedirect method
- *
- * @dataProvider statusCodeProvider
- * @return void
- */
- public function testRedirectByCode($code, $msg)
- {
- $Controller = new Controller(null, new Response());
- $response = $Controller->redirect('http://cakephp.org', (int)$code);
- $this->assertSame($response, $Controller->response);
- $this->assertEquals($code, $response->statusCode());
- $this->assertEquals('http://cakephp.org', $response->header()['Location']);
- $this->assertFalse($Controller->autoRender);
- }
- /**
- * test that beforeRedirect callbacks can set the URL that is being redirected to.
- *
- * @return void
- */
- public function testRedirectBeforeRedirectModifyingUrl()
- {
- $Controller = new Controller(null, new Response());
- $Controller->getEventManager()->on('Controller.beforeRedirect', function (Event $event, $url, Response $response) {
- $response->location('https://book.cakephp.org');
- });
- $response = $Controller->redirect('http://cakephp.org', 301);
- $this->assertEquals('https://book.cakephp.org', $response->header()['Location']);
- $this->assertEquals(301, $response->statusCode());
- }
- /**
- * test that beforeRedirect callback returning null doesn't affect things.
- *
- * @return void
- */
- public function testRedirectBeforeRedirectModifyingStatusCode()
- {
- $Response = $this->getMockBuilder('Cake\Http\Response')
- ->setMethods(['stop'])
- ->getMock();
- $Controller = new Controller(null, $Response);
- $Controller->getEventManager()->on('Controller.beforeRedirect', function (Event $event, $url, Response $response) {
- $response->statusCode(302);
- });
- $response = $Controller->redirect('http://cakephp.org', 301);
- $this->assertEquals('http://cakephp.org', $response->header()['Location']);
- $this->assertEquals(302, $response->statusCode());
- }
- public function testRedirectBeforeRedirectListenerReturnResponse()
- {
- $Response = $this->getMockBuilder('Cake\Http\Response')
- ->setMethods(['stop', 'header', 'statusCode'])
- ->getMock();
- $Controller = new Controller(null, $Response);
- $newResponse = new Response;
- $Controller->getEventManager()->on('Controller.beforeRedirect', function (Event $event, $url, Response $response) use ($newResponse) {
- return $newResponse;
- });
- $result = $Controller->redirect('http://cakephp.org');
- $this->assertSame($newResponse, $result);
- $this->assertSame($newResponse, $Controller->response);
- }
- /**
- * testMergeVars method
- *
- * @return void
- */
- public function testMergeVars()
- {
- $request = new ServerRequest();
- $TestController = new TestController($request);
- $expected = [
- 'Html' => null,
- ];
- $this->assertEquals($expected, $TestController->helpers);
- $expected = [
- 'Security' => null,
- 'Cookie' => null,
- ];
- $this->assertEquals($expected, $TestController->components);
- $TestController = new AnotherTestController($request);
- $this->assertEquals(
- 'Posts',
- $TestController->modelClass,
- 'modelClass should not be overwritten when defined.'
- );
- }
- /**
- * testReferer method
- *
- * @return void
- */
- public function testReferer()
- {
- $request = $this->getMockBuilder('Cake\Http\ServerRequest')
- ->setMethods(['referer'])
- ->getMock();
- $request->expects($this->any())->method('referer')
- ->with(true)
- ->will($this->returnValue('/posts/index'));
- $Controller = new Controller($request);
- $result = $Controller->referer(null, true);
- $this->assertEquals('/posts/index', $result);
- $request = $this->getMockBuilder('Cake\Http\ServerRequest')
- ->setMethods(['referer'])
- ->getMock();
- $request->expects($this->any())->method('referer')
- ->with(true)
- ->will($this->returnValue('/posts/index'));
- $Controller = new Controller($request);
- $result = $Controller->referer(['controller' => 'posts', 'action' => 'index'], true);
- $this->assertEquals('/posts/index', $result);
- $request = $this->getMockBuilder('Cake\Http\ServerRequest')
- ->setMethods(['referer'])
- ->getMock();
- $request->expects($this->any())->method('referer')
- ->with(false)
- ->will($this->returnValue('http://localhost/posts/index'));
- $Controller = new Controller($request);
- $result = $Controller->referer();
- $this->assertEquals('http://localhost/posts/index', $result);
- $Controller = new Controller(null);
- $result = $Controller->referer();
- $this->assertEquals('/', $result);
- }
- /**
- * Test that the referer is not absolute if it is '/'.
- *
- * This avoids the base path being applied twice on string urls.
- *
- * @return void
- */
- public function testRefererSlash()
- {
- $request = $this->getMockBuilder('Cake\Http\ServerRequest')
- ->setMethods(['referer'])
- ->getMock();
- $request->base = '/base';
- Router::pushRequest($request);
- $request->expects($this->any())->method('referer')
- ->will($this->returnValue('/'));
- $controller = new Controller($request);
- $result = $controller->referer('/', true);
- $this->assertEquals('/', $result);
- $controller = new Controller($request);
- $result = $controller->referer('/some/path', true);
- $this->assertEquals('/some/path', $result);
- }
- /**
- * testSetAction method
- *
- * @return void
- */
- public function testSetAction()
- {
- $request = new ServerRequest('controller_posts/index');
- $TestController = new TestController($request);
- $TestController->setAction('view', 1, 2);
- $expected = ['testId' => 1, 'test2Id' => 2];
- $this->assertSame($expected, $TestController->request->data);
- $this->assertSame('view', $TestController->request->params['action']);
- }
- /**
- * Tests that the startup process calls the correct functions
- *
- * @return void
- */
- public function testStartupProcess()
- {
- $eventManager = $this->getMockBuilder('Cake\Event\EventManager')->getMock();
- $controller = new Controller(null, null, null, $eventManager);
- $eventManager->expects($this->at(0))->method('dispatch')
- ->with(
- $this->logicalAnd(
- $this->isInstanceOf('Cake\Event\Event'),
- $this->attributeEqualTo('_name', 'Controller.initialize'),
- $this->attributeEqualTo('_subject', $controller)
- )
- )
- ->will($this->returnValue($this->getMockBuilder('Cake\Event\Event')->disableOriginalConstructor()->getMock()));
- $eventManager->expects($this->at(1))->method('dispatch')
- ->with(
- $this->logicalAnd(
- $this->isInstanceOf('Cake\Event\Event'),
- $this->attributeEqualTo('_name', 'Controller.startup'),
- $this->attributeEqualTo('_subject', $controller)
- )
- )
- ->will($this->returnValue($this->getMockBuilder('Cake\Event\Event')->disableOriginalConstructor()->getMock()));
- $controller->startupProcess();
- }
- /**
- * Tests that the shutdown process calls the correct functions
- *
- * @return void
- */
- public function testShutdownProcess()
- {
- $eventManager = $this->getMockBuilder('Cake\Event\EventManager')->getMock();
- $controller = new Controller(null, null, null, $eventManager);
- $eventManager->expects($this->once())->method('dispatch')
- ->with(
- $this->logicalAnd(
- $this->isInstanceOf('Cake\Event\Event'),
- $this->attributeEqualTo('_name', 'Controller.shutdown'),
- $this->attributeEqualTo('_subject', $controller)
- )
- )
- ->will($this->returnValue($this->getMockBuilder('Cake\Event\Event')->disableOriginalConstructor()->getMock()));
- $controller->shutdownProcess();
- }
- /**
- * test using Controller::paginate()
- *
- * @return void
- */
- public function testPaginate()
- {
- $request = new ServerRequest('controller_posts/index');
- $request->params['pass'] = [];
- $response = $this->getMockBuilder('Cake\Http\Response')
- ->setMethods(['httpCodes'])
- ->getMock();
- $Controller = new Controller($request, $response);
- $Controller->request->query = [
- 'url' => [],
- 'posts' => [
- 'page' => 2,
- 'limit' => 2,
- ]
- ];
- $this->assertEquals([], $Controller->paginate);
- $this->assertNotContains('Paginator', $Controller->helpers);
- $this->assertArrayNotHasKey('Paginator', $Controller->helpers);
- $results = $Controller->paginate('Posts');
- $this->assertInstanceOf('Cake\Datasource\ResultSetInterface', $results);
- $this->assertCount(3, $results);
- $results = $Controller->paginate(TableRegistry::get('Posts'));
- $this->assertInstanceOf('Cake\Datasource\ResultSetInterface', $results);
- $this->assertCount(3, $results);
- $this->assertSame($Controller->request->params['paging']['Posts']['page'], 1);
- $this->assertSame($Controller->request->params['paging']['Posts']['pageCount'], 1);
- $this->assertSame($Controller->request->params['paging']['Posts']['prevPage'], false);
- $this->assertSame($Controller->request->params['paging']['Posts']['nextPage'], false);
- $this->assertNull($Controller->request->params['paging']['Posts']['scope']);
- $results = $Controller->paginate(TableRegistry::get('Posts'), ['scope' => 'posts']);
- $this->assertInstanceOf('Cake\Datasource\ResultSetInterface', $results);
- $this->assertCount(1, $results);
- $this->assertSame($Controller->request->params['paging']['Posts']['page'], 2);
- $this->assertSame($Controller->request->params['paging']['Posts']['pageCount'], 2);
- $this->assertSame($Controller->request->params['paging']['Posts']['prevPage'], true);
- $this->assertSame($Controller->request->params['paging']['Posts']['nextPage'], false);
- $this->assertSame($Controller->request->params['paging']['Posts']['scope'], 'posts');
- }
- /**
- * test that paginate uses modelClass property.
- *
- * @return void
- */
- public function testPaginateUsesModelClass()
- {
- $request = new ServerRequest('controller_posts/index');
- $request->params['pass'] = [];
- $response = $this->getMockBuilder('Cake\Http\Response')
- ->setMethods(['httpCodes'])
- ->getMock();
- $Controller = new Controller($request, $response);
- $Controller->request->query['url'] = [];
- $Controller->modelClass = 'Posts';
- $results = $Controller->paginate();
- $this->assertInstanceOf('Cake\Datasource\ResultSetInterface', $results);
- }
- /**
- * testMissingAction method
- *
- * @expectedException \Cake\Controller\Exception\MissingActionException
- * @expectedExceptionMessage Action TestController::missing() could not be found, or is not accessible.
- * @return void
- */
- public function testInvokeActionMissingAction()
- {
- $url = new ServerRequest('test/missing');
- $url->addParams(['controller' => 'Test', 'action' => 'missing']);
- $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
- $Controller = new TestController($url, $response);
- $Controller->invokeAction();
- }
- /**
- * test invoking private methods.
- *
- * @expectedException \Cake\Controller\Exception\MissingActionException
- * @expectedExceptionMessage Action TestController::private_m() could not be found, or is not accessible.
- * @return void
- */
- public function testInvokeActionPrivate()
- {
- $url = new ServerRequest('test/private_m/');
- $url->addParams(['controller' => 'Test', 'action' => 'private_m']);
- $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
- $Controller = new TestController($url, $response);
- $Controller->invokeAction();
- }
- /**
- * test invoking protected methods.
- *
- * @expectedException \Cake\Controller\Exception\MissingActionException
- * @expectedExceptionMessage Action TestController::protected_m() could not be found, or is not accessible.
- * @return void
- */
- public function testInvokeActionProtected()
- {
- $url = new ServerRequest('test/protected_m/');
- $url->addParams(['controller' => 'Test', 'action' => 'protected_m']);
- $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
- $Controller = new TestController($url, $response);
- $Controller->invokeAction();
- }
- /**
- * test invoking controller methods.
- *
- * @expectedException \Cake\Controller\Exception\MissingActionException
- * @expectedExceptionMessage Action TestController::redirect() could not be found, or is not accessible.
- * @return void
- */
- public function testInvokeActionBaseMethods()
- {
- $url = new ServerRequest('test/redirect/');
- $url->addParams(['controller' => 'Test', 'action' => 'redirect']);
- $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
- $Controller = new TestController($url, $response);
- $Controller->invokeAction();
- }
- /**
- * test invoking controller methods.
- *
- * @return void
- */
- public function testInvokeActionReturnValue()
- {
- $url = new ServerRequest('test/returner/');
- $url->addParams([
- 'controller' => 'Test',
- 'action' => 'returner',
- 'pass' => []
- ]);
- $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
- $Controller = new TestController($url, $response);
- $result = $Controller->invokeAction();
- $this->assertEquals('I am from the controller.', $result);
- }
- /**
- * test invoking controller methods with passed params
- *
- * @return void
- */
- public function testInvokeActionWithPassedParams()
- {
- $url = new ServerRequest('test/index/1/2');
- $url->addParams([
- 'controller' => 'Test',
- 'action' => 'index',
- 'pass' => ['param1' => '1', 'param2' => '2']
- ]);
- $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
- $Controller = new TestController($url, $response);
- $result = $Controller->invokeAction();
- $this->assertEquals(
- ['testId' => '1', 'test2Id' => '2'],
- $Controller->request->data
- );
- }
- /**
- * test that a classes namespace is used in the viewPath.
- *
- * @return void
- */
- public function testViewPathConventions()
- {
- $request = new ServerRequest('admin/posts');
- $request->addParams([
- 'prefix' => 'admin'
- ]);
- $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
- $Controller = new \TestApp\Controller\Admin\PostsController($request, $response);
- $Controller->getEventManager()->on('Controller.beforeRender', function (Event $e) {
- return $e->subject()->response;
- });
- $Controller->render();
- $this->assertEquals('Admin' . DS . 'Posts', $Controller->viewBuilder()->templatePath());
- $request->addParams([
- 'prefix' => 'admin/super'
- ]);
- $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
- $Controller = new \TestApp\Controller\Admin\PostsController($request, $response);
- $Controller->getEventManager()->on('Controller.beforeRender', function (Event $e) {
- return $e->subject()->response;
- });
- $Controller->render();
- $this->assertEquals('Admin' . DS . 'Super' . DS . 'Posts', $Controller->viewBuilder()->templatePath());
- $request = new ServerRequest('pages/home');
- $request->addParams([
- 'prefix' => false
- ]);
- $Controller = new \TestApp\Controller\PagesController($request, $response);
- $Controller->getEventManager()->on('Controller.beforeRender', function (Event $e) {
- return $e->subject()->response;
- });
- $Controller->render();
- $this->assertEquals('Pages', $Controller->viewBuilder()->templatePath());
- }
- /**
- * Test the components() method.
- *
- * @return void
- */
- public function testComponents()
- {
- $request = new ServerRequest('/');
- $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
- $controller = new TestController($request, $response);
- $this->assertInstanceOf('Cake\Controller\ComponentRegistry', $controller->components());
- $result = $controller->components();
- $this->assertSame($result, $controller->components());
- }
- /**
- * Test the components() method with the custom ObjectRegistry.
- *
- * @return void
- */
- public function testComponentsWithCustomRegistry()
- {
- $request = new ServerRequest('/');
- $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
- $componentRegistry = $this->getMockBuilder('Cake\Controller\ComponentRegistry')
- ->setMethods(['offsetGet'])
- ->getMock();
- $controller = new TestController($request, $response, null, null, $componentRegistry);
- $this->assertInstanceOf(get_class($componentRegistry), $controller->components());
- $result = $controller->components();
- $this->assertSame($result, $controller->components());
- }
- /**
- * Test adding a component
- *
- * @return void
- */
- public function testLoadComponent()
- {
- $request = new ServerRequest('/');
- $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
- $controller = new TestController($request, $response);
- $result = $controller->loadComponent('Paginator');
- $this->assertInstanceOf('Cake\Controller\Component\PaginatorComponent', $result);
- $this->assertSame($result, $controller->Paginator);
- $registry = $controller->components();
- $this->assertTrue(isset($registry->Paginator));
- }
- /**
- * Test adding a component that is a duplicate.
- *
- * @return void
- */
- public function testLoadComponentDuplicate()
- {
- $request = new ServerRequest('/');
- $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
- $controller = new TestController($request, $response);
- $this->assertNotEmpty($controller->loadComponent('Paginator'));
- $this->assertNotEmpty($controller->loadComponent('Paginator'));
- try {
- $controller->loadComponent('Paginator', ['bad' => 'settings']);
- $this->fail('No exception');
- } catch (\RuntimeException $e) {
- $this->assertContains('The "Paginator" alias has already been loaded', $e->getMessage());
- }
- }
- /**
- * Test the isAction method.
- *
- * @return void
- */
- public function testIsAction()
- {
- $request = new ServerRequest('/');
- $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
- $controller = new TestController($request, $response);
- $this->assertFalse($controller->isAction('redirect'));
- $this->assertFalse($controller->isAction('beforeFilter'));
- $this->assertTrue($controller->isAction('index'));
- }
- /**
- * Test declared deprecated properties like $theme are properly passed to view.
- *
- * @return void
- */
- public function testDeclaredDeprecatedProperty()
- {
- $controller = new TestController(new ServerRequest(), new Response());
- $theme = $controller->theme;
- // @codingStandardsIgnoreStart
- $this->assertEquals($theme, @$controller->createView()->theme);
- // @codingStandardsIgnoreEnd
- }
- /**
- * Test that view variables are being set after the beforeRender event gets dispatched
- *
- * @return void
- */
- public function testBeforeRenderViewVariables()
- {
- $controller = new PostsController();
- $controller->getEventManager()->on('Controller.beforeRender', function (Event $event) {
- /* @var Controller $controller */
- $controller = $event->subject();
- $controller->set('testVariable', 'test');
- });
- $controller->render('index');
- $this->assertArrayHasKey('testVariable', $controller->View->viewVars);
- }
- /**
- * Tests deprecated view propertiyes work
- *
- * @param $property Deprecated property name
- * @param $getter Getter name
- * @param $setter Setter name
- * @param mixed $value Value to be set
- * @return void
- * @dataProvider deprecatedViewPropertyProvider
- */
- public function testDeprecatedViewProperty($property, $getter, $setter, $value)
- {
- $controller = new AnotherTestController();
- $message = false;
- set_error_handler(function ($errno, $errstr) use (&$message) {
- $message = ($errno === E_USER_DEPRECATED ? $errstr : false);
- });
- try {
- $controller->$property = $value;
- $this->assertSame(sprintf('Controller::$%s is deprecated. Use $this->viewBuilder()->%s() instead.', $property, $setter), $message);
- $this->assertSame($value, $controller->$property);
- $this->assertSame(sprintf('Controller::$%s is deprecated. Use $this->viewBuilder()->%s() instead.', $property, $getter), $message);
- $this->assertSame($value, $controller->viewBuilder()->{$getter}());
- } finally {
- restore_error_handler();
- }
- }
- /**
- * Data provider for testing deprecated view properties
- *
- * @return array
- */
- public function deprecatedViewPropertyProvider()
- {
- return [
- ['layout', 'getLayout', 'setLayout', 'custom'],
- ['view', 'getTemplate', 'setTemplate', 'view'],
- ['theme', 'getTheme', 'setTheme', 'Modern'],
- ['autoLayout', 'isAutoLayoutEnabled', 'enableAutoLayout', false],
- ['viewPath', 'getTemplatePath', 'setTemplatePath', 'Templates'],
- ['layoutPath', 'getLayoutPath', 'setLayoutPath', 'Layouts'],
- ];
- }
- }
|