ControllerTest.php 22 KB

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