ControllerTest.php 22 KB

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