ControllerTest.php 30 KB

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