ControllerTest.php 21 KB

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