ControllerTest.php 29 KB

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