ControllerTest.php 23 KB

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