ControllerTest.php 27 KB

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