ControllerTest.php 23 KB

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