ControllerTest.php 22 KB

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