ControllerTest.php 28 KB

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