ControllerTest.php 27 KB

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