ControllerTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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 array $config
  132. * @return void
  133. */
  134. public function initialize(array $config) {
  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.posts',
  183. 'core.comments'
  184. );
  185. /**
  186. * reset environment.
  187. *
  188. * @return void
  189. */
  190. public function setUp() {
  191. parent::setUp();
  192. Configure::write('App.namespace', 'TestApp');
  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. $request = new Request('controller_posts/index');
  211. $response = $this->getMock('Cake\Network\Response');
  212. $Controller = new Controller($request, $response);
  213. $Controller->modelClass = 'SiteArticles';
  214. $this->assertFalse($Controller->Articles);
  215. $this->assertInstanceOf(
  216. 'Cake\ORM\Table',
  217. $Controller->SiteArticles
  218. );
  219. unset($Controller->SiteArticles);
  220. $Controller->modelClass = 'Articles';
  221. $this->assertFalse($Controller->SiteArticles);
  222. $this->assertInstanceOf(
  223. 'TestApp\Model\Table\ArticlesTable',
  224. $Controller->Articles
  225. );
  226. }
  227. /**
  228. * testLoadModel method
  229. *
  230. * @return void
  231. */
  232. public function testLoadModel() {
  233. $request = new Request('controller_posts/index');
  234. $response = $this->getMock('Cake\Network\Response');
  235. $Controller = new Controller($request, $response);
  236. $this->assertFalse(isset($Controller->Articles));
  237. $result = $Controller->loadModel('Articles');
  238. $this->assertInstanceOf(
  239. 'TestApp\Model\Table\ArticlesTable',
  240. $result
  241. );
  242. $this->assertInstanceOf(
  243. 'TestApp\Model\Table\ArticlesTable',
  244. $Controller->Articles
  245. );
  246. }
  247. /**
  248. * testLoadModel method from a plugin controller
  249. *
  250. * @return void
  251. */
  252. public function testLoadModelInPlugins() {
  253. Plugin::load('TestPlugin');
  254. $Controller = new TestPluginController();
  255. $Controller->plugin = 'TestPlugin';
  256. $this->assertFalse(isset($Controller->TestPluginComments));
  257. $result = $Controller->loadModel('TestPlugin.TestPluginComments');
  258. $this->assertInstanceOf(
  259. 'TestPlugin\Model\Table\TestPluginCommentsTable',
  260. $result
  261. );
  262. $this->assertInstanceOf(
  263. 'TestPlugin\Model\Table\TestPluginCommentsTable',
  264. $Controller->TestPluginComments
  265. );
  266. }
  267. /**
  268. * Test that the constructor sets modelClass properly.
  269. *
  270. * @return void
  271. */
  272. public function testConstructSetModelClass() {
  273. Plugin::load('TestPlugin');
  274. $request = new Request();
  275. $response = new Response();
  276. $controller = new \TestApp\Controller\PostsController($request, $response);
  277. $this->assertEquals('Posts', $controller->modelClass);
  278. $this->assertInstanceOf('Cake\ORM\Table', $controller->Posts);
  279. $controller = new \TestApp\Controller\Admin\PostsController($request, $response);
  280. $this->assertEquals('Posts', $controller->modelClass);
  281. $this->assertInstanceOf('Cake\ORM\Table', $controller->Posts);
  282. $request->params['plugin'] = 'TestPlugin';
  283. $controller = new \TestPlugin\Controller\Admin\CommentsController($request, $response);
  284. $this->assertEquals('TestPlugin.Comments', $controller->modelClass);
  285. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $controller->Comments);
  286. }
  287. /**
  288. * testConstructClassesWithComponents method
  289. *
  290. * @return void
  291. */
  292. public function testConstructClassesWithComponents() {
  293. Plugin::load('TestPlugin');
  294. $Controller = new TestPluginController(new Request(), new Response());
  295. $Controller->loadComponent('TestPlugin.Other');
  296. $this->assertInstanceOf('TestPlugin\Controller\Component\OtherComponent', $Controller->Other);
  297. }
  298. /**
  299. * testRender method
  300. *
  301. * @return void
  302. */
  303. public function testRender() {
  304. Plugin::load('TestPlugin');
  305. $request = new Request('controller_posts/index');
  306. $request->params['action'] = 'index';
  307. $Controller = new Controller($request, new Response());
  308. $Controller->viewPath = 'Posts';
  309. $result = $Controller->render('index');
  310. $this->assertRegExp('/posts index/', (string)$result);
  311. $Controller->view = 'index';
  312. $Controller->getView()->hasRendered = false;
  313. $result = $Controller->render();
  314. $this->assertRegExp('/posts index/', (string)$result);
  315. $Controller->getView()->hasRendered = false;
  316. $result = $Controller->render('/Element/test_element');
  317. $this->assertRegExp('/this is the test element/', (string)$result);
  318. $Controller->view = null;
  319. }
  320. /**
  321. * test that a component beforeRender can change the controller view class.
  322. *
  323. * @return void
  324. */
  325. public function testBeforeRenderCallbackChangingViewClass() {
  326. $Controller = new Controller(new Request, new Response());
  327. $Controller->eventManager()->attach(function ($event) {
  328. $controller = $event->subject();
  329. $controller->viewClass = 'Json';
  330. }, 'Controller.beforeRender');
  331. $Controller->set([
  332. 'test' => 'value',
  333. '_serialize' => ['test']
  334. ]);
  335. $debug = Configure::read('debug');
  336. Configure::write('debug', false);
  337. $result = $Controller->render('index');
  338. $this->assertEquals('{"test":"value"}', $result->body());
  339. Configure::write('debug', $debug);
  340. }
  341. /**
  342. * test that a component beforeRender can change the controller view class.
  343. *
  344. * @return void
  345. */
  346. public function testBeforeRenderEventCancelsRender() {
  347. $Controller = new Controller(new Request, new Response());
  348. $Controller->eventManager()->attach(function ($event) {
  349. return false;
  350. }, 'Controller.beforeRender');
  351. $result = $Controller->render('index');
  352. $this->assertInstanceOf('Cake\Network\Response', $result);
  353. }
  354. /**
  355. * Generates status codes for redirect test.
  356. *
  357. * @return void
  358. */
  359. public static function statusCodeProvider() {
  360. return array(
  361. array(300, "Multiple Choices"),
  362. array(301, "Moved Permanently"),
  363. array(302, "Found"),
  364. array(303, "See Other"),
  365. array(304, "Not Modified"),
  366. array(305, "Use Proxy"),
  367. array(307, "Temporary Redirect"),
  368. array(403, "Forbidden"),
  369. );
  370. }
  371. /**
  372. * testRedirect method
  373. *
  374. * @dataProvider statusCodeProvider
  375. * @return void
  376. */
  377. public function testRedirectByCode($code, $msg) {
  378. $Controller = new Controller(null);
  379. $Controller->response = new Response();
  380. $response = $Controller->redirect('http://cakephp.org', (int)$code, false);
  381. $this->assertEquals($code, $response->statusCode());
  382. $this->assertEquals('http://cakephp.org', $response->header()['Location']);
  383. $this->assertFalse($Controller->autoRender);
  384. }
  385. /**
  386. * test that beforeRedirect callbacks can set the URL that is being redirected to.
  387. *
  388. * @return void
  389. */
  390. public function testRedirectBeforeRedirectModifyingUrl() {
  391. $Controller = new Controller(null);
  392. $Controller->response = new Response();
  393. $Controller->eventManager()->attach(function ($event, $url, $response) {
  394. $response->location('http://book.cakephp.org');
  395. }, 'Controller.beforeRedirect');
  396. $response = $Controller->redirect('http://cakephp.org', 301);
  397. $this->assertEquals('http://book.cakephp.org', $response->header()['Location']);
  398. $this->assertEquals(301, $response->statusCode());
  399. }
  400. /**
  401. * test that beforeRedirect callback returning null doesn't affect things.
  402. *
  403. * @return void
  404. */
  405. public function testRedirectBeforeRedirectModifyingStatusCode() {
  406. $Response = $this->getMock('Cake\Network\Response', array('stop'));
  407. $Controller = new Controller(null, $Response);
  408. $Controller->eventManager()->attach(function ($event, $url, $response) {
  409. $response->statusCode(302);
  410. }, 'Controller.beforeRedirect');
  411. $response = $Controller->redirect('http://cakephp.org', 301, false);
  412. $this->assertEquals('http://cakephp.org', $response->header()['Location']);
  413. $this->assertEquals(302, $response->statusCode());
  414. }
  415. /**
  416. * test that beforeRedirect callback returning false in controller
  417. *
  418. * @return void
  419. */
  420. public function testRedirectBeforeRedirectListenerReturnFalse() {
  421. $Response = $this->getMock('Cake\Network\Response', array('stop', 'header'));
  422. $Controller = new Controller(null, $Response);
  423. $Controller->eventManager()->attach(function ($event, $url, $response) {
  424. return false;
  425. }, 'Controller.beforeRedirect');
  426. $Controller->response->expects($this->never())
  427. ->method('stop');
  428. $Controller->response->expects($this->never())
  429. ->method('header');
  430. $Controller->response->expects($this->never())
  431. ->method('statusCode');
  432. $result = $Controller->redirect('http://cakephp.org');
  433. $this->assertNull($result);
  434. }
  435. /**
  436. * testMergeVars method
  437. *
  438. * @return void
  439. */
  440. public function testMergeVars() {
  441. $request = new Request();
  442. $TestController = new TestController($request);
  443. $expected = [
  444. 'Html' => null,
  445. 'Session' => null
  446. ];
  447. $this->assertEquals($expected, $TestController->helpers);
  448. $expected = [
  449. 'Security' => null,
  450. 'Cookie' => null,
  451. ];
  452. $this->assertEquals($expected, $TestController->components);
  453. $TestController = new AnotherTestController($request);
  454. $this->assertEquals(
  455. 'Posts',
  456. $TestController->modelClass,
  457. 'modelClass should not be overwritten when defined.'
  458. );
  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. * Test that the referer is not absolute if it is '/'.
  493. *
  494. * This avoids the base path being applied twice on string urls.
  495. *
  496. * @return void
  497. */
  498. public function testRefererSlash() {
  499. $request = $this->getMock('Cake\Network\Request', ['referer']);
  500. $request->base = '/base';
  501. Router::pushRequest($request);
  502. $request->expects($this->any())->method('referer')
  503. ->will($this->returnValue('/'));
  504. $controller = new Controller($request);
  505. $result = $controller->referer('/', true);
  506. $this->assertEquals('/', $result);
  507. $controller = new Controller($request);
  508. $result = $controller->referer('/some/path', true);
  509. $this->assertEquals('/base/some/path', $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. $eventManager = $this->getMock('Cake\Event\EventManager');
  532. $controller = new Controller(null, null, null, $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. ->will($this->returnValue($this->getMock('Cake\Event\Event', null, [], '', false)));
  542. $eventManager->expects($this->at(1))->method('dispatch')
  543. ->with(
  544. $this->logicalAnd(
  545. $this->isInstanceOf('Cake\Event\Event'),
  546. $this->attributeEqualTo('_name', 'Controller.startup'),
  547. $this->attributeEqualTo('_subject', $controller)
  548. )
  549. )
  550. ->will($this->returnValue($this->getMock('Cake\Event\Event', null, [], '', false)));
  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. $eventManager = $this->getMock('Cake\Event\EventManager');
  560. $controller = new Controller(null, null, null, $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. ->will($this->returnValue($this->getMock('Cake\Event\Event', null, [], '', false)));
  570. $controller->shutdownProcess();
  571. }
  572. /**
  573. * test using Controller::paginate()
  574. *
  575. * @return void
  576. */
  577. public function testPaginate() {
  578. $request = new Request('controller_posts/index');
  579. $request->params['pass'] = array();
  580. $response = $this->getMock('Cake\Network\Response', ['httpCodes']);
  581. $Controller = new Controller($request, $response);
  582. $Controller->request->query['url'] = [];
  583. $this->assertEquals([], $Controller->paginate);
  584. $this->assertNotContains('Paginator', $Controller->helpers);
  585. $this->assertArrayNotHasKey('Paginator', $Controller->helpers);
  586. $results = $Controller->paginate('Posts');
  587. $this->assertInstanceOf('Cake\Datasource\ResultSetInterface', $results);
  588. $results = $Controller->paginate(TableRegistry::get('Posts'));
  589. $this->assertInstanceOf('Cake\Datasource\ResultSetInterface', $results);
  590. $this->assertSame($Controller->request->params['paging']['Posts']['page'], 1);
  591. $this->assertSame($Controller->request->params['paging']['Posts']['pageCount'], 1);
  592. $this->assertSame($Controller->request->params['paging']['Posts']['prevPage'], false);
  593. $this->assertSame($Controller->request->params['paging']['Posts']['nextPage'], false);
  594. }
  595. /**
  596. * test that paginate uses modelClass property.
  597. *
  598. * @return void
  599. */
  600. public function testPaginateUsesModelClass() {
  601. $request = new Request('controller_posts/index');
  602. $request->params['pass'] = array();
  603. $response = $this->getMock('Cake\Network\Response', ['httpCodes']);
  604. $Controller = new Controller($request, $response);
  605. $Controller->request->query['url'] = [];
  606. $Controller->modelClass = 'Posts';
  607. $results = $Controller->paginate();
  608. $this->assertInstanceOf('Cake\Datasource\ResultSetInterface', $results);
  609. }
  610. /**
  611. * testMissingAction method
  612. *
  613. * @expectedException \Cake\Controller\Exception\MissingActionException
  614. * @expectedExceptionMessage Action TestController::missing() could not be found, or is not accessible.
  615. * @return void
  616. */
  617. public function testInvokeActionMissingAction() {
  618. $url = new Request('test/missing');
  619. $url->addParams(array('controller' => 'test_controller', 'action' => 'missing'));
  620. $response = $this->getMock('Cake\Network\Response');
  621. $Controller = new TestController($url, $response);
  622. $Controller->invokeAction();
  623. }
  624. /**
  625. * test invoking private methods.
  626. *
  627. * @expectedException \Cake\Controller\Exception\MissingActionException
  628. * @expectedExceptionMessage Action TestController::private_m() could not be found, or is not accessible.
  629. * @return void
  630. */
  631. public function testInvokeActionPrivate() {
  632. $url = new Request('test/private_m/');
  633. $url->addParams(array('controller' => 'test_controller', 'action' => 'private_m'));
  634. $response = $this->getMock('Cake\Network\Response');
  635. $Controller = new TestController($url, $response);
  636. $Controller->invokeAction();
  637. }
  638. /**
  639. * test invoking protected methods.
  640. *
  641. * @expectedException \Cake\Controller\Exception\MissingActionException
  642. * @expectedExceptionMessage Action TestController::protected_m() could not be found, or is not accessible.
  643. * @return void
  644. */
  645. public function testInvokeActionProtected() {
  646. $url = new Request('test/protected_m/');
  647. $url->addParams(array('controller' => 'test_controller', 'action' => 'protected_m'));
  648. $response = $this->getMock('Cake\Network\Response');
  649. $Controller = new TestController($url, $response);
  650. $Controller->invokeAction();
  651. }
  652. /**
  653. * test invoking controller methods.
  654. *
  655. * @expectedException \Cake\Controller\Exception\MissingActionException
  656. * @expectedExceptionMessage Action TestController::redirect() could not be found, or is not accessible.
  657. * @return void
  658. */
  659. public function testInvokeActionBaseMethods() {
  660. $url = new Request('test/redirect/');
  661. $url->addParams(array('controller' => 'test_controller', 'action' => 'redirect'));
  662. $response = $this->getMock('Cake\Network\Response');
  663. $Controller = new TestController($url, $response);
  664. $Controller->invokeAction();
  665. }
  666. /**
  667. * test invoking controller methods.
  668. *
  669. * @return void
  670. */
  671. public function testInvokeActionReturnValue() {
  672. $url = new Request('test/returner/');
  673. $url->addParams(array(
  674. 'controller' => 'test_controller',
  675. 'action' => 'returner',
  676. 'pass' => array()
  677. ));
  678. $response = $this->getMock('Cake\Network\Response');
  679. $Controller = new TestController($url, $response);
  680. $result = $Controller->invokeAction();
  681. $this->assertEquals('I am from the controller.', $result);
  682. }
  683. /**
  684. * test that a classes namespace is used in the viewPath.
  685. *
  686. * @return void
  687. */
  688. public function testViewPathConventions() {
  689. $request = new Request('admin/posts');
  690. $request->addParams(array(
  691. 'prefix' => 'admin'
  692. ));
  693. $response = $this->getMock('Cake\Network\Response');
  694. $Controller = new \TestApp\Controller\Admin\PostsController($request, $response);
  695. $this->assertEquals('Admin' . DS . 'Posts', $Controller->viewPath);
  696. $request->addParams(array(
  697. 'prefix' => 'admin/super'
  698. ));
  699. $response = $this->getMock('Cake\Network\Response');
  700. $Controller = new \TestApp\Controller\Admin\PostsController($request, $response);
  701. $this->assertEquals('Admin' . DS . 'Super' . DS . 'Posts', $Controller->viewPath);
  702. $request = new Request('pages/home');
  703. $Controller = new \TestApp\Controller\PagesController($request, $response);
  704. $this->assertEquals('Pages', $Controller->viewPath);
  705. }
  706. /**
  707. * Test the components() method.
  708. *
  709. * @return void
  710. */
  711. public function testComponents() {
  712. $request = new Request('/');
  713. $response = $this->getMock('Cake\Network\Response');
  714. $controller = new TestController($request, $response);
  715. $this->assertInstanceOf('Cake\Controller\ComponentRegistry', $controller->components());
  716. $result = $controller->components();
  717. $this->assertSame($result, $controller->components());
  718. }
  719. /**
  720. * Test adding a component
  721. *
  722. * @return void
  723. */
  724. public function testLoadComponent() {
  725. $request = new Request('/');
  726. $response = $this->getMock('Cake\Network\Response');
  727. $controller = new TestController($request, $response);
  728. $result = $controller->loadComponent('Paginator');
  729. $this->assertInstanceOf('Cake\Controller\Component\PaginatorComponent', $result);
  730. $this->assertSame($result, $controller->Paginator);
  731. $registry = $controller->components();
  732. $this->assertTrue(isset($registry->Paginator));
  733. }
  734. /**
  735. * Test adding a component that is a duplicate.
  736. *
  737. * @return void
  738. */
  739. public function testLoadComponentDuplicate() {
  740. $request = new Request('/');
  741. $response = $this->getMock('Cake\Network\Response');
  742. $controller = new TestController($request, $response);
  743. $this->assertNotEmpty($controller->loadComponent('Paginator'));
  744. $this->assertNotEmpty($controller->loadComponent('Paginator'));
  745. try {
  746. $controller->loadComponent('Paginator', ['bad' => 'settings']);
  747. $this->fail('No exception');
  748. } catch (\RuntimeException $e) {
  749. $this->assertContains('The "Paginator" alias has already been loaded', $e->getMessage());
  750. }
  751. }
  752. /**
  753. * Test the isAction method.
  754. *
  755. * @return void
  756. */
  757. public function testIsAction() {
  758. $request = new Request('/');
  759. $response = $this->getMock('Cake\Network\Response');
  760. $controller = new TestController($request, $response);
  761. $this->assertFalse($controller->isAction('redirect'));
  762. $this->assertFalse($controller->isAction('beforeFilter'));
  763. $this->assertTrue($controller->isAction('index'));
  764. }
  765. }