ControllerTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP Project
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Controller;
  16. use Cake\Controller\Component;
  17. use Cake\Controller\Controller;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Plugin;
  20. use Cake\Event\Event;
  21. use Cake\Network\Request;
  22. use Cake\Network\Response;
  23. use Cake\ORM\TableRegistry;
  24. use Cake\Routing\Router;
  25. use Cake\TestSuite\Fixture\TestModel;
  26. use Cake\TestSuite\TestCase;
  27. use TestApp\Controller\Admin\PostsController;
  28. use TestPlugin\Controller\TestPluginController;
  29. /**
  30. * AppController class
  31. *
  32. */
  33. class ControllerTestAppController extends Controller
  34. {
  35. /**
  36. * helpers property
  37. *
  38. * @var array
  39. */
  40. public $helpers = ['Html'];
  41. /**
  42. * modelClass property
  43. *
  44. * @var string
  45. */
  46. public $modelClass = 'Posts';
  47. /**
  48. * components property
  49. *
  50. * @var array
  51. */
  52. public $components = ['Cookie'];
  53. }
  54. /**
  55. * TestController class
  56. */
  57. class TestController extends ControllerTestAppController
  58. {
  59. /**
  60. * Theme property
  61. *
  62. * @var string
  63. */
  64. public $theme = 'Foo';
  65. /**
  66. * helpers property
  67. *
  68. * @var array
  69. */
  70. public $helpers = ['Html'];
  71. /**
  72. * components property
  73. *
  74. * @var array
  75. */
  76. public $components = ['Security'];
  77. /**
  78. * modelClass property
  79. *
  80. * @var string
  81. */
  82. public $modelClass = 'Comments';
  83. /**
  84. * beforeFilter handler
  85. *
  86. * @param \Cake\Event\Event $event
  87. * @retun void
  88. */
  89. public function beforeFilter(Event $event)
  90. {
  91. }
  92. /**
  93. * index method
  94. *
  95. * @param mixed $testId
  96. * @param mixed $testTwoId
  97. * @return void
  98. */
  99. public function index($testId, $testTwoId)
  100. {
  101. $this->request->data = [
  102. 'testId' => $testId,
  103. 'test2Id' => $testTwoId
  104. ];
  105. }
  106. /**
  107. * view method
  108. *
  109. * @param mixed $testId
  110. * @param mixed $testTwoId
  111. * @return void
  112. */
  113. public function view($testId, $testTwoId)
  114. {
  115. $this->request->data = [
  116. 'testId' => $testId,
  117. 'test2Id' => $testTwoId
  118. ];
  119. }
  120. public function returner()
  121. {
  122. return 'I am from the controller.';
  123. }
  124. //@codingStandardsIgnoreStart
  125. protected function protected_m()
  126. {
  127. }
  128. private function private_m()
  129. {
  130. }
  131. public function _hidden()
  132. {
  133. }
  134. //@codingStandardsIgnoreEnd
  135. public function admin_add()
  136. {
  137. }
  138. }
  139. /**
  140. * TestComponent class
  141. */
  142. class TestComponent extends Component
  143. {
  144. /**
  145. * beforeRedirect method
  146. *
  147. * @return void
  148. */
  149. public function beforeRedirect()
  150. {
  151. }
  152. /**
  153. * initialize method
  154. *
  155. * @param array $config
  156. * @return void
  157. */
  158. public function initialize(array $config)
  159. {
  160. }
  161. /**
  162. * startup method
  163. *
  164. * @param Event $event
  165. * @return void
  166. */
  167. public function startup(Event $event)
  168. {
  169. }
  170. /**
  171. * shutdown method
  172. *
  173. * @param Event $event
  174. * @return void
  175. */
  176. public function shutdown(Event $event)
  177. {
  178. }
  179. /**
  180. * beforeRender callback
  181. *
  182. * @param Event $event
  183. * @return void
  184. */
  185. public function beforeRender(Event $event)
  186. {
  187. $controller = $event->subject();
  188. if ($this->viewclass) {
  189. $controller->viewClass = $this->viewclass;
  190. }
  191. }
  192. }
  193. /**
  194. * AnotherTestController class
  195. *
  196. */
  197. class AnotherTestController extends ControllerTestAppController
  198. {
  199. }
  200. /**
  201. * ControllerTest class
  202. *
  203. */
  204. class ControllerTest extends TestCase
  205. {
  206. /**
  207. * fixtures property
  208. *
  209. * @var array
  210. */
  211. public $fixtures = [
  212. 'core.comments',
  213. 'core.posts'
  214. ];
  215. /**
  216. * reset environment.
  217. *
  218. * @return void
  219. */
  220. public function setUp()
  221. {
  222. parent::setUp();
  223. Configure::write('App.namespace', 'TestApp');
  224. Router::reload();
  225. }
  226. /**
  227. * tearDown
  228. *
  229. * @return void
  230. */
  231. public function tearDown()
  232. {
  233. parent::tearDown();
  234. Plugin::unload();
  235. }
  236. /**
  237. * test autoload modelClass
  238. *
  239. * @return void
  240. */
  241. public function testTableAutoload()
  242. {
  243. $request = new Request('controller_posts/index');
  244. $response = $this->getMock('Cake\Network\Response');
  245. $Controller = new Controller($request, $response);
  246. $Controller->modelClass = 'SiteArticles';
  247. $this->assertFalse($Controller->Articles);
  248. $this->assertInstanceOf(
  249. 'Cake\ORM\Table',
  250. $Controller->SiteArticles
  251. );
  252. unset($Controller->SiteArticles);
  253. $Controller->modelClass = 'Articles';
  254. $this->assertFalse($Controller->SiteArticles);
  255. $this->assertInstanceOf(
  256. 'TestApp\Model\Table\ArticlesTable',
  257. $Controller->Articles
  258. );
  259. }
  260. /**
  261. * testLoadModel method
  262. *
  263. * @return void
  264. */
  265. public function testLoadModel()
  266. {
  267. $request = new Request('controller_posts/index');
  268. $response = $this->getMock('Cake\Network\Response');
  269. $Controller = new Controller($request, $response);
  270. $this->assertFalse(isset($Controller->Articles));
  271. $result = $Controller->loadModel('Articles');
  272. $this->assertInstanceOf(
  273. 'TestApp\Model\Table\ArticlesTable',
  274. $result
  275. );
  276. $this->assertInstanceOf(
  277. 'TestApp\Model\Table\ArticlesTable',
  278. $Controller->Articles
  279. );
  280. }
  281. /**
  282. * testLoadModel method from a plugin controller
  283. *
  284. * @return void
  285. */
  286. public function testLoadModelInPlugins()
  287. {
  288. Plugin::load('TestPlugin');
  289. $Controller = new TestPluginController();
  290. $Controller->plugin = 'TestPlugin';
  291. $this->assertFalse(isset($Controller->TestPluginComments));
  292. $result = $Controller->loadModel('TestPlugin.TestPluginComments');
  293. $this->assertInstanceOf(
  294. 'TestPlugin\Model\Table\TestPluginCommentsTable',
  295. $result
  296. );
  297. $this->assertInstanceOf(
  298. 'TestPlugin\Model\Table\TestPluginCommentsTable',
  299. $Controller->TestPluginComments
  300. );
  301. }
  302. /**
  303. * Test that the constructor sets modelClass properly.
  304. *
  305. * @return void
  306. */
  307. public function testConstructSetModelClass()
  308. {
  309. Plugin::load('TestPlugin');
  310. $request = new Request();
  311. $response = new Response();
  312. $controller = new \TestApp\Controller\PostsController($request, $response);
  313. $this->assertEquals('Posts', $controller->modelClass);
  314. $this->assertInstanceOf('Cake\ORM\Table', $controller->Posts);
  315. $controller = new \TestApp\Controller\Admin\PostsController($request, $response);
  316. $this->assertEquals('Posts', $controller->modelClass);
  317. $this->assertInstanceOf('Cake\ORM\Table', $controller->Posts);
  318. $request->params['plugin'] = 'TestPlugin';
  319. $controller = new \TestPlugin\Controller\Admin\CommentsController($request, $response);
  320. $this->assertEquals('TestPlugin.Comments', $controller->modelClass);
  321. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $controller->Comments);
  322. }
  323. /**
  324. * testConstructClassesWithComponents method
  325. *
  326. * @return void
  327. */
  328. public function testConstructClassesWithComponents()
  329. {
  330. Plugin::load('TestPlugin');
  331. $Controller = new TestPluginController(new Request(), new Response());
  332. $Controller->loadComponent('TestPlugin.Other');
  333. $this->assertInstanceOf('TestPlugin\Controller\Component\OtherComponent', $Controller->Other);
  334. }
  335. /**
  336. * testRender method
  337. *
  338. * @return void
  339. */
  340. public function testRender()
  341. {
  342. Plugin::load('TestPlugin');
  343. $request = new Request('controller_posts/index');
  344. $request->params['action'] = 'index';
  345. $Controller = new Controller($request, new Response());
  346. $Controller->viewBuilder()->templatePath('Posts');
  347. $result = $Controller->render('index');
  348. $this->assertRegExp('/posts index/', (string)$result);
  349. $Controller->viewBuilder()->template('index');
  350. $result = $Controller->render();
  351. $this->assertRegExp('/posts index/', (string)$result);
  352. $result = $Controller->render('/Element/test_element');
  353. $this->assertRegExp('/this is the test element/', (string)$result);
  354. }
  355. /**
  356. * test that a component beforeRender can change the controller view class.
  357. *
  358. * @return void
  359. */
  360. public function testBeforeRenderCallbackChangingViewClass()
  361. {
  362. $Controller = new Controller(new Request, new Response());
  363. $Controller->eventManager()->on('Controller.beforeRender', function ($event) {
  364. $controller = $event->subject();
  365. $controller->viewClass = 'Json';
  366. });
  367. $Controller->set([
  368. 'test' => 'value',
  369. '_serialize' => ['test']
  370. ]);
  371. $debug = Configure::read('debug');
  372. Configure::write('debug', false);
  373. $result = $Controller->render('index');
  374. $this->assertEquals('{"test":"value"}', $result->body());
  375. Configure::write('debug', $debug);
  376. }
  377. /**
  378. * test that a component beforeRender can change the controller view class.
  379. *
  380. * @return void
  381. */
  382. public function testBeforeRenderEventCancelsRender()
  383. {
  384. $Controller = new Controller(new Request, new Response());
  385. $Controller->eventManager()->attach(function ($event) {
  386. return false;
  387. }, 'Controller.beforeRender');
  388. $result = $Controller->render('index');
  389. $this->assertInstanceOf('Cake\Network\Response', $result);
  390. }
  391. /**
  392. * Generates status codes for redirect test.
  393. *
  394. * @return void
  395. */
  396. public static function statusCodeProvider()
  397. {
  398. return [
  399. [300, "Multiple Choices"],
  400. [301, "Moved Permanently"],
  401. [302, "Found"],
  402. [303, "See Other"],
  403. [304, "Not Modified"],
  404. [305, "Use Proxy"],
  405. [307, "Temporary Redirect"],
  406. [403, "Forbidden"],
  407. ];
  408. }
  409. /**
  410. * testRedirect method
  411. *
  412. * @dataProvider statusCodeProvider
  413. * @return void
  414. */
  415. public function testRedirectByCode($code, $msg)
  416. {
  417. $Controller = new Controller(null, new Response());
  418. $response = $Controller->redirect('http://cakephp.org', (int)$code);
  419. $this->assertEquals($code, $response->statusCode());
  420. $this->assertEquals('http://cakephp.org', $response->header()['Location']);
  421. $this->assertFalse($Controller->autoRender);
  422. }
  423. /**
  424. * test that beforeRedirect callbacks can set the URL that is being redirected to.
  425. *
  426. * @return void
  427. */
  428. public function testRedirectBeforeRedirectModifyingUrl()
  429. {
  430. $Controller = new Controller(null, new Response());
  431. $Controller->eventManager()->attach(function ($event, $url, $response) {
  432. $response->location('http://book.cakephp.org');
  433. }, 'Controller.beforeRedirect');
  434. $response = $Controller->redirect('http://cakephp.org', 301);
  435. $this->assertEquals('http://book.cakephp.org', $response->header()['Location']);
  436. $this->assertEquals(301, $response->statusCode());
  437. }
  438. /**
  439. * test that beforeRedirect callback returning null doesn't affect things.
  440. *
  441. * @return void
  442. */
  443. public function testRedirectBeforeRedirectModifyingStatusCode()
  444. {
  445. $Response = $this->getMock('Cake\Network\Response', ['stop']);
  446. $Controller = new Controller(null, $Response);
  447. $Controller->eventManager()->attach(function ($event, $url, $response) {
  448. $response->statusCode(302);
  449. }, 'Controller.beforeRedirect');
  450. $response = $Controller->redirect('http://cakephp.org', 301);
  451. $this->assertEquals('http://cakephp.org', $response->header()['Location']);
  452. $this->assertEquals(302, $response->statusCode());
  453. }
  454. public function testRedirectBeforeRedirectListenerReturnResponse()
  455. {
  456. $Response = $this->getMock('Cake\Network\Response', ['stop', 'header', 'statusCode']);
  457. $Controller = new Controller(null, $Response);
  458. $Controller->eventManager()->on('Controller.beforeRedirect', function ($event, $url, $response) {
  459. return new Response;
  460. });
  461. $result = $Controller->redirect('http://cakephp.org');
  462. $this->assertInstanceOf(Response::class, $result);
  463. }
  464. /**
  465. * testMergeVars method
  466. *
  467. * @return void
  468. */
  469. public function testMergeVars()
  470. {
  471. $request = new Request();
  472. $TestController = new TestController($request);
  473. $expected = [
  474. 'Html' => null,
  475. ];
  476. $this->assertEquals($expected, $TestController->helpers);
  477. $expected = [
  478. 'Security' => null,
  479. 'Cookie' => null,
  480. ];
  481. $this->assertEquals($expected, $TestController->components);
  482. $TestController = new AnotherTestController($request);
  483. $this->assertEquals(
  484. 'Posts',
  485. $TestController->modelClass,
  486. 'modelClass should not be overwritten when defined.'
  487. );
  488. }
  489. /**
  490. * testReferer method
  491. *
  492. * @return void
  493. */
  494. public function testReferer()
  495. {
  496. $request = $this->getMock('Cake\Network\Request', ['referer']);
  497. $request->expects($this->any())->method('referer')
  498. ->with(true)
  499. ->will($this->returnValue('/posts/index'));
  500. $Controller = new Controller($request);
  501. $result = $Controller->referer(null, true);
  502. $this->assertEquals('/posts/index', $result);
  503. $request = $this->getMock('Cake\Network\Request', ['referer']);
  504. $request->expects($this->any())->method('referer')
  505. ->with(true)
  506. ->will($this->returnValue('/posts/index'));
  507. $Controller = new Controller($request);
  508. $result = $Controller->referer(['controller' => 'posts', 'action' => 'index'], true);
  509. $this->assertEquals('/posts/index', $result);
  510. $request = $this->getMock('Cake\Network\Request', ['referer']);
  511. $request->expects($this->any())->method('referer')
  512. ->with(false)
  513. ->will($this->returnValue('http://localhost/posts/index'));
  514. $Controller = new Controller($request);
  515. $result = $Controller->referer();
  516. $this->assertEquals('http://localhost/posts/index', $result);
  517. $Controller = new Controller(null);
  518. $result = $Controller->referer();
  519. $this->assertEquals('/', $result);
  520. }
  521. /**
  522. * Test that the referer is not absolute if it is '/'.
  523. *
  524. * This avoids the base path being applied twice on string urls.
  525. *
  526. * @return void
  527. */
  528. public function testRefererSlash()
  529. {
  530. $request = $this->getMock('Cake\Network\Request', ['referer']);
  531. $request->base = '/base';
  532. Router::pushRequest($request);
  533. $request->expects($this->any())->method('referer')
  534. ->will($this->returnValue('/'));
  535. $controller = new Controller($request);
  536. $result = $controller->referer('/', true);
  537. $this->assertEquals('/', $result);
  538. $controller = new Controller($request);
  539. $result = $controller->referer('/some/path', true);
  540. $this->assertEquals('/base/some/path', $result);
  541. }
  542. /**
  543. * testSetAction method
  544. *
  545. * @return void
  546. */
  547. public function testSetAction()
  548. {
  549. $request = new Request('controller_posts/index');
  550. $TestController = new TestController($request);
  551. $TestController->setAction('view', 1, 2);
  552. $expected = ['testId' => 1, 'test2Id' => 2];
  553. $this->assertSame($expected, $TestController->request->data);
  554. $this->assertSame('view', $TestController->request->params['action']);
  555. }
  556. /**
  557. * Tests that the startup process calls the correct functions
  558. *
  559. * @return void
  560. */
  561. public function testStartupProcess()
  562. {
  563. $eventManager = $this->getMock('Cake\Event\EventManager');
  564. $controller = new Controller(null, null, null, $eventManager);
  565. $eventManager->expects($this->at(0))->method('dispatch')
  566. ->with(
  567. $this->logicalAnd(
  568. $this->isInstanceOf('Cake\Event\Event'),
  569. $this->attributeEqualTo('_name', 'Controller.initialize'),
  570. $this->attributeEqualTo('_subject', $controller)
  571. )
  572. )
  573. ->will($this->returnValue($this->getMock('Cake\Event\Event', null, [], '', false)));
  574. $eventManager->expects($this->at(1))->method('dispatch')
  575. ->with(
  576. $this->logicalAnd(
  577. $this->isInstanceOf('Cake\Event\Event'),
  578. $this->attributeEqualTo('_name', 'Controller.startup'),
  579. $this->attributeEqualTo('_subject', $controller)
  580. )
  581. )
  582. ->will($this->returnValue($this->getMock('Cake\Event\Event', null, [], '', false)));
  583. $controller->startupProcess();
  584. }
  585. /**
  586. * Tests that the shutdown process calls the correct functions
  587. *
  588. * @return void
  589. */
  590. public function testShutdownProcess()
  591. {
  592. $eventManager = $this->getMock('Cake\Event\EventManager');
  593. $controller = new Controller(null, null, null, $eventManager);
  594. $eventManager->expects($this->once())->method('dispatch')
  595. ->with(
  596. $this->logicalAnd(
  597. $this->isInstanceOf('Cake\Event\Event'),
  598. $this->attributeEqualTo('_name', 'Controller.shutdown'),
  599. $this->attributeEqualTo('_subject', $controller)
  600. )
  601. )
  602. ->will($this->returnValue($this->getMock('Cake\Event\Event', null, [], '', false)));
  603. $controller->shutdownProcess();
  604. }
  605. /**
  606. * test using Controller::paginate()
  607. *
  608. * @return void
  609. */
  610. public function testPaginate()
  611. {
  612. $request = new Request('controller_posts/index');
  613. $request->params['pass'] = [];
  614. $response = $this->getMock('Cake\Network\Response', ['httpCodes']);
  615. $Controller = new Controller($request, $response);
  616. $Controller->request->query['url'] = [];
  617. $this->assertEquals([], $Controller->paginate);
  618. $this->assertNotContains('Paginator', $Controller->helpers);
  619. $this->assertArrayNotHasKey('Paginator', $Controller->helpers);
  620. $results = $Controller->paginate('Posts');
  621. $this->assertInstanceOf('Cake\Datasource\ResultSetInterface', $results);
  622. $results = $Controller->paginate(TableRegistry::get('Posts'));
  623. $this->assertInstanceOf('Cake\Datasource\ResultSetInterface', $results);
  624. $this->assertSame($Controller->request->params['paging']['Posts']['page'], 1);
  625. $this->assertSame($Controller->request->params['paging']['Posts']['pageCount'], 1);
  626. $this->assertSame($Controller->request->params['paging']['Posts']['prevPage'], false);
  627. $this->assertSame($Controller->request->params['paging']['Posts']['nextPage'], false);
  628. }
  629. /**
  630. * test that paginate uses modelClass property.
  631. *
  632. * @return void
  633. */
  634. public function testPaginateUsesModelClass()
  635. {
  636. $request = new Request('controller_posts/index');
  637. $request->params['pass'] = [];
  638. $response = $this->getMock('Cake\Network\Response', ['httpCodes']);
  639. $Controller = new Controller($request, $response);
  640. $Controller->request->query['url'] = [];
  641. $Controller->modelClass = 'Posts';
  642. $results = $Controller->paginate();
  643. $this->assertInstanceOf('Cake\Datasource\ResultSetInterface', $results);
  644. }
  645. /**
  646. * testMissingAction method
  647. *
  648. * @expectedException \Cake\Controller\Exception\MissingActionException
  649. * @expectedExceptionMessage Action TestController::missing() could not be found, or is not accessible.
  650. * @return void
  651. */
  652. public function testInvokeActionMissingAction()
  653. {
  654. $url = new Request('test/missing');
  655. $url->addParams(['controller' => 'Test', 'action' => 'missing']);
  656. $response = $this->getMock('Cake\Network\Response');
  657. $Controller = new TestController($url, $response);
  658. $Controller->invokeAction();
  659. }
  660. /**
  661. * test invoking private methods.
  662. *
  663. * @expectedException \Cake\Controller\Exception\MissingActionException
  664. * @expectedExceptionMessage Action TestController::private_m() could not be found, or is not accessible.
  665. * @return void
  666. */
  667. public function testInvokeActionPrivate()
  668. {
  669. $url = new Request('test/private_m/');
  670. $url->addParams(['controller' => 'Test', 'action' => 'private_m']);
  671. $response = $this->getMock('Cake\Network\Response');
  672. $Controller = new TestController($url, $response);
  673. $Controller->invokeAction();
  674. }
  675. /**
  676. * test invoking protected methods.
  677. *
  678. * @expectedException \Cake\Controller\Exception\MissingActionException
  679. * @expectedExceptionMessage Action TestController::protected_m() could not be found, or is not accessible.
  680. * @return void
  681. */
  682. public function testInvokeActionProtected()
  683. {
  684. $url = new Request('test/protected_m/');
  685. $url->addParams(['controller' => 'Test', 'action' => 'protected_m']);
  686. $response = $this->getMock('Cake\Network\Response');
  687. $Controller = new TestController($url, $response);
  688. $Controller->invokeAction();
  689. }
  690. /**
  691. * test invoking controller methods.
  692. *
  693. * @expectedException \Cake\Controller\Exception\MissingActionException
  694. * @expectedExceptionMessage Action TestController::redirect() could not be found, or is not accessible.
  695. * @return void
  696. */
  697. public function testInvokeActionBaseMethods()
  698. {
  699. $url = new Request('test/redirect/');
  700. $url->addParams(['controller' => 'Test', 'action' => 'redirect']);
  701. $response = $this->getMock('Cake\Network\Response');
  702. $Controller = new TestController($url, $response);
  703. $Controller->invokeAction();
  704. }
  705. /**
  706. * test invoking controller methods.
  707. *
  708. * @return void
  709. */
  710. public function testInvokeActionReturnValue()
  711. {
  712. $url = new Request('test/returner/');
  713. $url->addParams([
  714. 'controller' => 'Test',
  715. 'action' => 'returner',
  716. 'pass' => []
  717. ]);
  718. $response = $this->getMock('Cake\Network\Response');
  719. $Controller = new TestController($url, $response);
  720. $result = $Controller->invokeAction();
  721. $this->assertEquals('I am from the controller.', $result);
  722. }
  723. /**
  724. * test that a classes namespace is used in the viewPath.
  725. *
  726. * @return void
  727. */
  728. public function testViewPathConventions()
  729. {
  730. $request = new Request('admin/posts');
  731. $request->addParams([
  732. 'prefix' => 'admin'
  733. ]);
  734. $response = $this->getMock('Cake\Network\Response');
  735. $Controller = new \TestApp\Controller\Admin\PostsController($request, $response);
  736. $Controller->eventManager()->on('Controller.beforeRender', function (Event $e) {
  737. return $e->subject()->response;
  738. });
  739. $Controller->render();
  740. $this->assertEquals('Admin' . DS . 'Posts', $Controller->viewBuilder()->templatePath());
  741. $request->addParams([
  742. 'prefix' => 'admin/super'
  743. ]);
  744. $response = $this->getMock('Cake\Network\Response');
  745. $Controller = new \TestApp\Controller\Admin\PostsController($request, $response);
  746. $Controller->eventManager()->on('Controller.beforeRender', function (Event $e) {
  747. return $e->subject()->response;
  748. });
  749. $Controller->render();
  750. $this->assertEquals('Admin' . DS . 'Super' . DS . 'Posts', $Controller->viewBuilder()->templatePath());
  751. $request = new Request('pages/home');
  752. $request->addParams([
  753. 'prefix' => false
  754. ]);
  755. $Controller = new \TestApp\Controller\PagesController($request, $response);
  756. $Controller->eventManager()->on('Controller.beforeRender', function (Event $e) {
  757. return $e->subject()->response;
  758. });
  759. $Controller->render();
  760. $this->assertEquals('Pages', $Controller->viewBuilder()->templatePath());
  761. }
  762. /**
  763. * Test the components() method.
  764. *
  765. * @return void
  766. */
  767. public function testComponents()
  768. {
  769. $request = new Request('/');
  770. $response = $this->getMock('Cake\Network\Response');
  771. $controller = new TestController($request, $response);
  772. $this->assertInstanceOf('Cake\Controller\ComponentRegistry', $controller->components());
  773. $result = $controller->components();
  774. $this->assertSame($result, $controller->components());
  775. }
  776. /**
  777. * Test the components() method with the custom ObjectRegistry.
  778. *
  779. * @return void
  780. */
  781. public function testComponentsWithCustomRegistry()
  782. {
  783. $request = new Request('/');
  784. $response = $this->getMock('Cake\Network\Response');
  785. $componentRegistry = $this->getMock('Cake\Controller\ComponentRegistry', ['offsetGet']);
  786. $controller = new TestController($request, $response, null, null, $componentRegistry);
  787. $this->assertInstanceOf(get_class($componentRegistry), $controller->components());
  788. $result = $controller->components();
  789. $this->assertSame($result, $controller->components());
  790. }
  791. /**
  792. * Test adding a component
  793. *
  794. * @return void
  795. */
  796. public function testLoadComponent()
  797. {
  798. $request = new Request('/');
  799. $response = $this->getMock('Cake\Network\Response');
  800. $controller = new TestController($request, $response);
  801. $result = $controller->loadComponent('Paginator');
  802. $this->assertInstanceOf('Cake\Controller\Component\PaginatorComponent', $result);
  803. $this->assertSame($result, $controller->Paginator);
  804. $registry = $controller->components();
  805. $this->assertTrue(isset($registry->Paginator));
  806. }
  807. /**
  808. * Test adding a component that is a duplicate.
  809. *
  810. * @return void
  811. */
  812. public function testLoadComponentDuplicate()
  813. {
  814. $request = new Request('/');
  815. $response = $this->getMock('Cake\Network\Response');
  816. $controller = new TestController($request, $response);
  817. $this->assertNotEmpty($controller->loadComponent('Paginator'));
  818. $this->assertNotEmpty($controller->loadComponent('Paginator'));
  819. try {
  820. $controller->loadComponent('Paginator', ['bad' => 'settings']);
  821. $this->fail('No exception');
  822. } catch (\RuntimeException $e) {
  823. $this->assertContains('The "Paginator" alias has already been loaded', $e->getMessage());
  824. }
  825. }
  826. /**
  827. * Test the isAction method.
  828. *
  829. * @return void
  830. */
  831. public function testIsAction()
  832. {
  833. $request = new Request('/');
  834. $response = $this->getMock('Cake\Network\Response');
  835. $controller = new TestController($request, $response);
  836. $this->assertFalse($controller->isAction('redirect'));
  837. $this->assertFalse($controller->isAction('beforeFilter'));
  838. $this->assertTrue($controller->isAction('index'));
  839. }
  840. /**
  841. * Test declared deprecated properties like $theme are properly passed to view.
  842. *
  843. * @return void
  844. */
  845. public function testDeclaredDeprecatedProperty()
  846. {
  847. $controller = new TestController(new Request(), new Response());
  848. $theme = $controller->theme;
  849. // @codingStandardsIgnoreStart
  850. $this->assertEquals($theme, @$controller->createView()->theme);
  851. // @codingStandardsIgnoreEnd
  852. }
  853. /**
  854. * Test that view variables are being set after the beforeRender event gets dispatched
  855. *
  856. * @return void
  857. */
  858. public function testBeforeRenderViewVariables()
  859. {
  860. $controller = new PostsController();
  861. $controller->eventManager()->on('Controller.beforeRender', function (Event $event) {
  862. /* @var Controller $controller */
  863. $controller = $event->subject();
  864. $controller->set('testVariable', 'test');
  865. });
  866. $controller->render('index');
  867. $this->assertArrayHasKey('testVariable', $controller->View->viewVars);
  868. }
  869. }