ControllerTest.php 29 KB

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