ControllerTest.php 28 KB

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