ControllerTest.php 23 KB

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