ControllerTest.php 22 KB

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