ControllerTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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. /**
  38. * helpers property
  39. *
  40. * @var array
  41. */
  42. public $helpers = ['Html'];
  43. /**
  44. * modelClass property
  45. *
  46. * @var string
  47. */
  48. public $modelClass = 'Posts';
  49. /**
  50. * components property
  51. *
  52. * @var array
  53. */
  54. public $components = ['Cookie'];
  55. }
  56. /**
  57. * TestController class
  58. */
  59. class TestController extends ControllerTestAppController
  60. {
  61. /**
  62. * helpers property
  63. *
  64. * @var array
  65. */
  66. public $helpers = ['Html'];
  67. /**
  68. * components property
  69. *
  70. * @var array
  71. */
  72. public $components = ['Security'];
  73. /**
  74. * modelClass property
  75. *
  76. * @var string
  77. */
  78. public $modelClass = 'Comments';
  79. /**
  80. * beforeFilter handler
  81. *
  82. * @param \Cake\Event\Event $event
  83. * @retun void
  84. */
  85. public function beforeFilter(Event $event)
  86. {
  87. }
  88. /**
  89. * index method
  90. *
  91. * @param mixed $testId
  92. * @param mixed $testTwoId
  93. * @return void
  94. */
  95. public function index($testId, $testTwoId)
  96. {
  97. $this->request->data = [
  98. 'testId' => $testId,
  99. 'test2Id' => $testTwoId
  100. ];
  101. }
  102. /**
  103. * view method
  104. *
  105. * @param mixed $testId
  106. * @param mixed $testTwoId
  107. * @return void
  108. */
  109. public function view($testId, $testTwoId)
  110. {
  111. $this->request->data = [
  112. 'testId' => $testId,
  113. 'test2Id' => $testTwoId
  114. ];
  115. }
  116. public function returner()
  117. {
  118. return 'I am from the controller.';
  119. }
  120. //@codingStandardsIgnoreStart
  121. protected function protected_m()
  122. {
  123. }
  124. private function private_m()
  125. {
  126. }
  127. public function _hidden()
  128. {
  129. }
  130. //@codingStandardsIgnoreEnd
  131. public function admin_add()
  132. {
  133. }
  134. }
  135. /**
  136. * TestComponent class
  137. */
  138. class TestComponent extends Component
  139. {
  140. /**
  141. * beforeRedirect method
  142. *
  143. * @return void
  144. */
  145. public function beforeRedirect()
  146. {
  147. }
  148. /**
  149. * initialize method
  150. *
  151. * @param array $config
  152. * @return void
  153. */
  154. public function initialize(array $config)
  155. {
  156. }
  157. /**
  158. * startup method
  159. *
  160. * @param Event $event
  161. * @return void
  162. */
  163. public function startup(Event $event)
  164. {
  165. }
  166. /**
  167. * shutdown method
  168. *
  169. * @param Event $event
  170. * @return void
  171. */
  172. public function shutdown(Event $event)
  173. {
  174. }
  175. /**
  176. * beforeRender callback
  177. *
  178. * @param Event $event
  179. * @return void
  180. */
  181. public function beforeRender(Event $event)
  182. {
  183. $controller = $event->subject();
  184. if ($this->viewclass) {
  185. $controller->viewClass = $this->viewclass;
  186. }
  187. }
  188. }
  189. /**
  190. * AnotherTestController class
  191. *
  192. */
  193. class AnotherTestController extends ControllerTestAppController
  194. {
  195. }
  196. /**
  197. * ControllerTest class
  198. *
  199. */
  200. class ControllerTest extends TestCase
  201. {
  202. /**
  203. * fixtures property
  204. *
  205. * @var array
  206. */
  207. public $fixtures = [
  208. 'core.posts',
  209. 'core.comments'
  210. ];
  211. /**
  212. * reset environment.
  213. *
  214. * @return void
  215. */
  216. public function setUp()
  217. {
  218. parent::setUp();
  219. Configure::write('App.namespace', 'TestApp');
  220. Router::reload();
  221. }
  222. /**
  223. * tearDown
  224. *
  225. * @return void
  226. */
  227. public function tearDown()
  228. {
  229. parent::tearDown();
  230. Plugin::unload();
  231. }
  232. /**
  233. * test autoload modelClass
  234. *
  235. * @return void
  236. */
  237. public function testTableAutoload()
  238. {
  239. $request = new Request('controller_posts/index');
  240. $response = $this->getMock('Cake\Network\Response');
  241. $Controller = new Controller($request, $response);
  242. $Controller->modelClass = 'SiteArticles';
  243. $this->assertFalse($Controller->Articles);
  244. $this->assertInstanceOf(
  245. 'Cake\ORM\Table',
  246. $Controller->SiteArticles
  247. );
  248. unset($Controller->SiteArticles);
  249. $Controller->modelClass = 'Articles';
  250. $this->assertFalse($Controller->SiteArticles);
  251. $this->assertInstanceOf(
  252. 'TestApp\Model\Table\ArticlesTable',
  253. $Controller->Articles
  254. );
  255. }
  256. /**
  257. * testLoadModel method
  258. *
  259. * @return void
  260. */
  261. public function testLoadModel()
  262. {
  263. $request = new Request('controller_posts/index');
  264. $response = $this->getMock('Cake\Network\Response');
  265. $Controller = new Controller($request, $response);
  266. $this->assertFalse(isset($Controller->Articles));
  267. $result = $Controller->loadModel('Articles');
  268. $this->assertInstanceOf(
  269. 'TestApp\Model\Table\ArticlesTable',
  270. $result
  271. );
  272. $this->assertInstanceOf(
  273. 'TestApp\Model\Table\ArticlesTable',
  274. $Controller->Articles
  275. );
  276. }
  277. /**
  278. * testLoadModel method from a plugin controller
  279. *
  280. * @return void
  281. */
  282. public function testLoadModelInPlugins()
  283. {
  284. Plugin::load('TestPlugin');
  285. $Controller = new TestPluginController();
  286. $Controller->plugin = 'TestPlugin';
  287. $this->assertFalse(isset($Controller->TestPluginComments));
  288. $result = $Controller->loadModel('TestPlugin.TestPluginComments');
  289. $this->assertInstanceOf(
  290. 'TestPlugin\Model\Table\TestPluginCommentsTable',
  291. $result
  292. );
  293. $this->assertInstanceOf(
  294. 'TestPlugin\Model\Table\TestPluginCommentsTable',
  295. $Controller->TestPluginComments
  296. );
  297. }
  298. /**
  299. * Test that the constructor sets modelClass properly.
  300. *
  301. * @return void
  302. */
  303. public function testConstructSetModelClass()
  304. {
  305. Plugin::load('TestPlugin');
  306. $request = new Request();
  307. $response = new Response();
  308. $controller = new \TestApp\Controller\PostsController($request, $response);
  309. $this->assertEquals('Posts', $controller->modelClass);
  310. $this->assertInstanceOf('Cake\ORM\Table', $controller->Posts);
  311. $controller = new \TestApp\Controller\Admin\PostsController($request, $response);
  312. $this->assertEquals('Posts', $controller->modelClass);
  313. $this->assertInstanceOf('Cake\ORM\Table', $controller->Posts);
  314. $request->params['plugin'] = 'TestPlugin';
  315. $controller = new \TestPlugin\Controller\Admin\CommentsController($request, $response);
  316. $this->assertEquals('TestPlugin.Comments', $controller->modelClass);
  317. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $controller->Comments);
  318. }
  319. /**
  320. * testConstructClassesWithComponents method
  321. *
  322. * @return void
  323. */
  324. public function testConstructClassesWithComponents()
  325. {
  326. Plugin::load('TestPlugin');
  327. $Controller = new TestPluginController(new Request(), new Response());
  328. $Controller->loadComponent('TestPlugin.Other');
  329. $this->assertInstanceOf('TestPlugin\Controller\Component\OtherComponent', $Controller->Other);
  330. }
  331. /**
  332. * testRender method
  333. *
  334. * @return void
  335. */
  336. public function testRender()
  337. {
  338. Plugin::load('TestPlugin');
  339. $request = new Request('controller_posts/index');
  340. $request->params['action'] = 'index';
  341. $Controller = new Controller($request, new Response());
  342. $Controller->viewPath = 'Posts';
  343. $result = $Controller->render('index');
  344. $this->assertRegExp('/posts index/', (string)$result);
  345. $Controller->view = 'index';
  346. $Controller->getView()->hasRendered = false;
  347. $result = $Controller->render();
  348. $this->assertRegExp('/posts index/', (string)$result);
  349. $Controller->getView()->hasRendered = false;
  350. $result = $Controller->render('/Element/test_element');
  351. $this->assertRegExp('/this is the test element/', (string)$result);
  352. $Controller->view = null;
  353. }
  354. /**
  355. * test that a component beforeRender can change the controller view class.
  356. *
  357. * @return void
  358. */
  359. public function testBeforeRenderCallbackChangingViewClass()
  360. {
  361. $Controller = new Controller(new Request, new Response());
  362. $Controller->eventManager()->attach(function ($event) {
  363. $controller = $event->subject();
  364. $controller->viewClass = 'Json';
  365. }, 'Controller.beforeRender');
  366. $Controller->set([
  367. 'test' => 'value',
  368. '_serialize' => ['test']
  369. ]);
  370. $debug = Configure::read('debug');
  371. Configure::write('debug', false);
  372. $result = $Controller->render('index');
  373. $this->assertEquals('{"test":"value"}', $result->body());
  374. Configure::write('debug', $debug);
  375. }
  376. /**
  377. * test that a component beforeRender can change the controller view class.
  378. *
  379. * @return void
  380. */
  381. public function testBeforeRenderEventCancelsRender()
  382. {
  383. $Controller = new Controller(new Request, new Response());
  384. $Controller->eventManager()->attach(function ($event) {
  385. return false;
  386. }, 'Controller.beforeRender');
  387. $result = $Controller->render('index');
  388. $this->assertInstanceOf('Cake\Network\Response', $result);
  389. }
  390. /**
  391. * Generates status codes for redirect test.
  392. *
  393. * @return void
  394. */
  395. public static function statusCodeProvider()
  396. {
  397. return [
  398. [300, "Multiple Choices"],
  399. [301, "Moved Permanently"],
  400. [302, "Found"],
  401. [303, "See Other"],
  402. [304, "Not Modified"],
  403. [305, "Use Proxy"],
  404. [307, "Temporary Redirect"],
  405. [403, "Forbidden"],
  406. ];
  407. }
  408. /**
  409. * testRedirect method
  410. *
  411. * @dataProvider statusCodeProvider
  412. * @return void
  413. */
  414. public function testRedirectByCode($code, $msg)
  415. {
  416. $Controller = new Controller(null, new Response());
  417. $response = $Controller->redirect('http://cakephp.org', (int)$code, false);
  418. $this->assertEquals($code, $response->statusCode());
  419. $this->assertEquals('http://cakephp.org', $response->header()['Location']);
  420. $this->assertFalse($Controller->autoRender);
  421. }
  422. /**
  423. * test that beforeRedirect callbacks can set the URL that is being redirected to.
  424. *
  425. * @return void
  426. */
  427. public function testRedirectBeforeRedirectModifyingUrl()
  428. {
  429. $Controller = new Controller(null, new Response());
  430. $Controller->eventManager()->attach(function ($event, $url, $response) {
  431. $response->location('http://book.cakephp.org');
  432. }, 'Controller.beforeRedirect');
  433. $response = $Controller->redirect('http://cakephp.org', 301);
  434. $this->assertEquals('http://book.cakephp.org', $response->header()['Location']);
  435. $this->assertEquals(301, $response->statusCode());
  436. }
  437. /**
  438. * test that beforeRedirect callback returning null doesn't affect things.
  439. *
  440. * @return void
  441. */
  442. public function testRedirectBeforeRedirectModifyingStatusCode()
  443. {
  444. $Response = $this->getMock('Cake\Network\Response', ['stop']);
  445. $Controller = new Controller(null, $Response);
  446. $Controller->eventManager()->attach(function ($event, $url, $response) {
  447. $response->statusCode(302);
  448. }, 'Controller.beforeRedirect');
  449. $response = $Controller->redirect('http://cakephp.org', 301, false);
  450. $this->assertEquals('http://cakephp.org', $response->header()['Location']);
  451. $this->assertEquals(302, $response->statusCode());
  452. }
  453. /**
  454. * test that beforeRedirect callback returning false in controller
  455. *
  456. * @return void
  457. */
  458. public function testRedirectBeforeRedirectListenerReturnFalse()
  459. {
  460. $Response = $this->getMock('Cake\Network\Response', ['stop', 'header']);
  461. $Controller = new Controller(null, $Response);
  462. $Controller->eventManager()->attach(function ($event, $url, $response) {
  463. return false;
  464. }, 'Controller.beforeRedirect');
  465. $Controller->response->expects($this->never())
  466. ->method('stop');
  467. $Controller->response->expects($this->never())
  468. ->method('header');
  469. $Controller->response->expects($this->never())
  470. ->method('statusCode');
  471. $result = $Controller->redirect('http://cakephp.org');
  472. $this->assertNull($result);
  473. }
  474. /**
  475. * testMergeVars method
  476. *
  477. * @return void
  478. */
  479. public function testMergeVars()
  480. {
  481. $request = new Request();
  482. $TestController = new TestController($request);
  483. $expected = [
  484. 'Html' => null,
  485. ];
  486. $this->assertEquals($expected, $TestController->helpers);
  487. $expected = [
  488. 'Security' => null,
  489. 'Cookie' => null,
  490. ];
  491. $this->assertEquals($expected, $TestController->components);
  492. $TestController = new AnotherTestController($request);
  493. $this->assertEquals(
  494. 'Posts',
  495. $TestController->modelClass,
  496. 'modelClass should not be overwritten when defined.'
  497. );
  498. }
  499. /**
  500. * testReferer method
  501. *
  502. * @return void
  503. */
  504. public function testReferer()
  505. {
  506. $request = $this->getMock('Cake\Network\Request', ['referer']);
  507. $request->expects($this->any())->method('referer')
  508. ->with(true)
  509. ->will($this->returnValue('/posts/index'));
  510. $Controller = new Controller($request);
  511. $result = $Controller->referer(null, true);
  512. $this->assertEquals('/posts/index', $result);
  513. $request = $this->getMock('Cake\Network\Request', ['referer']);
  514. $request->expects($this->any())->method('referer')
  515. ->with(true)
  516. ->will($this->returnValue('/posts/index'));
  517. $Controller = new Controller($request);
  518. $result = $Controller->referer(['controller' => 'posts', 'action' => 'index'], true);
  519. $this->assertEquals('/posts/index', $result);
  520. $request = $this->getMock('Cake\Network\Request', ['referer']);
  521. $request->expects($this->any())->method('referer')
  522. ->with(false)
  523. ->will($this->returnValue('http://localhost/posts/index'));
  524. $Controller = new Controller($request);
  525. $result = $Controller->referer();
  526. $this->assertEquals('http://localhost/posts/index', $result);
  527. $Controller = new Controller(null);
  528. $result = $Controller->referer();
  529. $this->assertEquals('/', $result);
  530. }
  531. /**
  532. * Test that the referer is not absolute if it is '/'.
  533. *
  534. * This avoids the base path being applied twice on string urls.
  535. *
  536. * @return void
  537. */
  538. public function testRefererSlash()
  539. {
  540. $request = $this->getMock('Cake\Network\Request', ['referer']);
  541. $request->base = '/base';
  542. Router::pushRequest($request);
  543. $request->expects($this->any())->method('referer')
  544. ->will($this->returnValue('/'));
  545. $controller = new Controller($request);
  546. $result = $controller->referer('/', true);
  547. $this->assertEquals('/', $result);
  548. $controller = new Controller($request);
  549. $result = $controller->referer('/some/path', true);
  550. $this->assertEquals('/base/some/path', $result);
  551. }
  552. /**
  553. * testSetAction method
  554. *
  555. * @return void
  556. */
  557. public function testSetAction()
  558. {
  559. $request = new Request('controller_posts/index');
  560. $TestController = new TestController($request);
  561. $TestController->setAction('view', 1, 2);
  562. $expected = ['testId' => 1, 'test2Id' => 2];
  563. $this->assertSame($expected, $TestController->request->data);
  564. $this->assertSame('view', $TestController->request->params['action']);
  565. $this->assertSame('view', $TestController->view);
  566. }
  567. /**
  568. * Tests that the startup process calls the correct functions
  569. *
  570. * @return void
  571. */
  572. public function testStartupProcess()
  573. {
  574. $eventManager = $this->getMock('Cake\Event\EventManager');
  575. $controller = new Controller(null, null, null, $eventManager);
  576. $eventManager->expects($this->at(0))->method('dispatch')
  577. ->with(
  578. $this->logicalAnd(
  579. $this->isInstanceOf('Cake\Event\Event'),
  580. $this->attributeEqualTo('_name', 'Controller.initialize'),
  581. $this->attributeEqualTo('_subject', $controller)
  582. )
  583. )
  584. ->will($this->returnValue($this->getMock('Cake\Event\Event', null, [], '', false)));
  585. $eventManager->expects($this->at(1))->method('dispatch')
  586. ->with(
  587. $this->logicalAnd(
  588. $this->isInstanceOf('Cake\Event\Event'),
  589. $this->attributeEqualTo('_name', 'Controller.startup'),
  590. $this->attributeEqualTo('_subject', $controller)
  591. )
  592. )
  593. ->will($this->returnValue($this->getMock('Cake\Event\Event', null, [], '', false)));
  594. $controller->startupProcess();
  595. }
  596. /**
  597. * Tests that the shutdown process calls the correct functions
  598. *
  599. * @return void
  600. */
  601. public function testShutdownProcess()
  602. {
  603. $eventManager = $this->getMock('Cake\Event\EventManager');
  604. $controller = new Controller(null, null, null, $eventManager);
  605. $eventManager->expects($this->once())->method('dispatch')
  606. ->with(
  607. $this->logicalAnd(
  608. $this->isInstanceOf('Cake\Event\Event'),
  609. $this->attributeEqualTo('_name', 'Controller.shutdown'),
  610. $this->attributeEqualTo('_subject', $controller)
  611. )
  612. )
  613. ->will($this->returnValue($this->getMock('Cake\Event\Event', null, [], '', false)));
  614. $controller->shutdownProcess();
  615. }
  616. /**
  617. * test using Controller::paginate()
  618. *
  619. * @return void
  620. */
  621. public function testPaginate()
  622. {
  623. $request = new Request('controller_posts/index');
  624. $request->params['pass'] = [];
  625. $response = $this->getMock('Cake\Network\Response', ['httpCodes']);
  626. $Controller = new Controller($request, $response);
  627. $Controller->request->query['url'] = [];
  628. $this->assertEquals([], $Controller->paginate);
  629. $this->assertNotContains('Paginator', $Controller->helpers);
  630. $this->assertArrayNotHasKey('Paginator', $Controller->helpers);
  631. $results = $Controller->paginate('Posts');
  632. $this->assertInstanceOf('Cake\Datasource\ResultSetInterface', $results);
  633. $results = $Controller->paginate(TableRegistry::get('Posts'));
  634. $this->assertInstanceOf('Cake\Datasource\ResultSetInterface', $results);
  635. $this->assertSame($Controller->request->params['paging']['Posts']['page'], 1);
  636. $this->assertSame($Controller->request->params['paging']['Posts']['pageCount'], 1);
  637. $this->assertSame($Controller->request->params['paging']['Posts']['prevPage'], false);
  638. $this->assertSame($Controller->request->params['paging']['Posts']['nextPage'], false);
  639. }
  640. /**
  641. * test that paginate uses modelClass property.
  642. *
  643. * @return void
  644. */
  645. public function testPaginateUsesModelClass()
  646. {
  647. $request = new Request('controller_posts/index');
  648. $request->params['pass'] = [];
  649. $response = $this->getMock('Cake\Network\Response', ['httpCodes']);
  650. $Controller = new Controller($request, $response);
  651. $Controller->request->query['url'] = [];
  652. $Controller->modelClass = 'Posts';
  653. $results = $Controller->paginate();
  654. $this->assertInstanceOf('Cake\Datasource\ResultSetInterface', $results);
  655. }
  656. /**
  657. * testMissingAction method
  658. *
  659. * @expectedException \Cake\Controller\Exception\MissingActionException
  660. * @expectedExceptionMessage Action TestController::missing() could not be found, or is not accessible.
  661. * @return void
  662. */
  663. public function testInvokeActionMissingAction()
  664. {
  665. $url = new Request('test/missing');
  666. $url->addParams(['controller' => 'Test', 'action' => 'missing']);
  667. $response = $this->getMock('Cake\Network\Response');
  668. $Controller = new TestController($url, $response);
  669. $Controller->invokeAction();
  670. }
  671. /**
  672. * test invoking private methods.
  673. *
  674. * @expectedException \Cake\Controller\Exception\MissingActionException
  675. * @expectedExceptionMessage Action TestController::private_m() could not be found, or is not accessible.
  676. * @return void
  677. */
  678. public function testInvokeActionPrivate()
  679. {
  680. $url = new Request('test/private_m/');
  681. $url->addParams(['controller' => 'Test', 'action' => 'private_m']);
  682. $response = $this->getMock('Cake\Network\Response');
  683. $Controller = new TestController($url, $response);
  684. $Controller->invokeAction();
  685. }
  686. /**
  687. * test invoking protected methods.
  688. *
  689. * @expectedException \Cake\Controller\Exception\MissingActionException
  690. * @expectedExceptionMessage Action TestController::protected_m() could not be found, or is not accessible.
  691. * @return void
  692. */
  693. public function testInvokeActionProtected()
  694. {
  695. $url = new Request('test/protected_m/');
  696. $url->addParams(['controller' => 'Test', 'action' => 'protected_m']);
  697. $response = $this->getMock('Cake\Network\Response');
  698. $Controller = new TestController($url, $response);
  699. $Controller->invokeAction();
  700. }
  701. /**
  702. * test invoking controller methods.
  703. *
  704. * @expectedException \Cake\Controller\Exception\MissingActionException
  705. * @expectedExceptionMessage Action TestController::redirect() could not be found, or is not accessible.
  706. * @return void
  707. */
  708. public function testInvokeActionBaseMethods()
  709. {
  710. $url = new Request('test/redirect/');
  711. $url->addParams(['controller' => 'Test', 'action' => 'redirect']);
  712. $response = $this->getMock('Cake\Network\Response');
  713. $Controller = new TestController($url, $response);
  714. $Controller->invokeAction();
  715. }
  716. /**
  717. * test invoking controller methods.
  718. *
  719. * @return void
  720. */
  721. public function testInvokeActionReturnValue()
  722. {
  723. $url = new Request('test/returner/');
  724. $url->addParams([
  725. 'controller' => 'Test',
  726. 'action' => 'returner',
  727. 'pass' => []
  728. ]);
  729. $response = $this->getMock('Cake\Network\Response');
  730. $Controller = new TestController($url, $response);
  731. $result = $Controller->invokeAction();
  732. $this->assertEquals('I am from the controller.', $result);
  733. }
  734. /**
  735. * test that a classes namespace is used in the viewPath.
  736. *
  737. * @return void
  738. */
  739. public function testViewPathConventions()
  740. {
  741. $request = new Request('admin/posts');
  742. $request->addParams([
  743. 'prefix' => 'admin'
  744. ]);
  745. $response = $this->getMock('Cake\Network\Response');
  746. $Controller = new \TestApp\Controller\Admin\PostsController($request, $response);
  747. $this->assertEquals('Admin' . DS . 'Posts', $Controller->viewPath);
  748. $request->addParams([
  749. 'prefix' => 'admin/super'
  750. ]);
  751. $response = $this->getMock('Cake\Network\Response');
  752. $Controller = new \TestApp\Controller\Admin\PostsController($request, $response);
  753. $this->assertEquals('Admin' . DS . 'Super' . DS . 'Posts', $Controller->viewPath);
  754. $request = new Request('pages/home');
  755. $request->addParams([
  756. 'prefix' => false
  757. ]);
  758. $Controller = new \TestApp\Controller\PagesController($request, $response);
  759. $this->assertEquals('Pages', $Controller->viewPath);
  760. }
  761. /**
  762. * Test the components() method.
  763. *
  764. * @return void
  765. */
  766. public function testComponents()
  767. {
  768. $request = new Request('/');
  769. $response = $this->getMock('Cake\Network\Response');
  770. $controller = new TestController($request, $response);
  771. $this->assertInstanceOf('Cake\Controller\ComponentRegistry', $controller->components());
  772. $result = $controller->components();
  773. $this->assertSame($result, $controller->components());
  774. }
  775. /**
  776. * Test adding a component
  777. *
  778. * @return void
  779. */
  780. public function testLoadComponent()
  781. {
  782. $request = new Request('/');
  783. $response = $this->getMock('Cake\Network\Response');
  784. $controller = new TestController($request, $response);
  785. $result = $controller->loadComponent('Paginator');
  786. $this->assertInstanceOf('Cake\Controller\Component\PaginatorComponent', $result);
  787. $this->assertSame($result, $controller->Paginator);
  788. $registry = $controller->components();
  789. $this->assertTrue(isset($registry->Paginator));
  790. }
  791. /**
  792. * Test adding a component that is a duplicate.
  793. *
  794. * @return void
  795. */
  796. public function testLoadComponentDuplicate()
  797. {
  798. $request = new Request('/');
  799. $response = $this->getMock('Cake\Network\Response');
  800. $controller = new TestController($request, $response);
  801. $this->assertNotEmpty($controller->loadComponent('Paginator'));
  802. $this->assertNotEmpty($controller->loadComponent('Paginator'));
  803. try {
  804. $controller->loadComponent('Paginator', ['bad' => 'settings']);
  805. $this->fail('No exception');
  806. } catch (\RuntimeException $e) {
  807. $this->assertContains('The "Paginator" alias has already been loaded', $e->getMessage());
  808. }
  809. }
  810. /**
  811. * Test the isAction method.
  812. *
  813. * @return void
  814. */
  815. public function testIsAction()
  816. {
  817. $request = new Request('/');
  818. $response = $this->getMock('Cake\Network\Response');
  819. $controller = new TestController($request, $response);
  820. $this->assertFalse($controller->isAction('redirect'));
  821. $this->assertFalse($controller->isAction('beforeFilter'));
  822. $this->assertTrue($controller->isAction('index'));
  823. }
  824. }