ControllerTest.php 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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. public function testRedirectBeforeRedirectListenerReturnResponse()
  455. {
  456. $Response = $this->getMock('Cake\Network\Response', ['stop', 'header', 'statusCode']);
  457. $Controller = new Controller(null, $Response);
  458. $newResponse = new Response;
  459. $Controller->eventManager()->on('Controller.beforeRedirect', function ($event, $url, $response) use ($newResponse) {
  460. return $newResponse;
  461. });
  462. $result = $Controller->redirect('http://cakephp.org');
  463. $this->assertSame($newResponse, $result);
  464. }
  465. /**
  466. * testMergeVars method
  467. *
  468. * @return void
  469. */
  470. public function testMergeVars()
  471. {
  472. $request = new Request();
  473. $TestController = new TestController($request);
  474. $expected = [
  475. 'Html' => null,
  476. ];
  477. $this->assertEquals($expected, $TestController->helpers);
  478. $expected = [
  479. 'Security' => null,
  480. 'Cookie' => null,
  481. ];
  482. $this->assertEquals($expected, $TestController->components);
  483. $TestController = new AnotherTestController($request);
  484. $this->assertEquals(
  485. 'Posts',
  486. $TestController->modelClass,
  487. 'modelClass should not be overwritten when defined.'
  488. );
  489. }
  490. /**
  491. * testReferer method
  492. *
  493. * @return void
  494. */
  495. public function testReferer()
  496. {
  497. $request = $this->getMock('Cake\Network\Request', ['referer']);
  498. $request->expects($this->any())->method('referer')
  499. ->with(true)
  500. ->will($this->returnValue('/posts/index'));
  501. $Controller = new Controller($request);
  502. $result = $Controller->referer(null, true);
  503. $this->assertEquals('/posts/index', $result);
  504. $request = $this->getMock('Cake\Network\Request', ['referer']);
  505. $request->expects($this->any())->method('referer')
  506. ->with(true)
  507. ->will($this->returnValue('/posts/index'));
  508. $Controller = new Controller($request);
  509. $result = $Controller->referer(['controller' => 'posts', 'action' => 'index'], true);
  510. $this->assertEquals('/posts/index', $result);
  511. $request = $this->getMock('Cake\Network\Request', ['referer']);
  512. $request->expects($this->any())->method('referer')
  513. ->with(false)
  514. ->will($this->returnValue('http://localhost/posts/index'));
  515. $Controller = new Controller($request);
  516. $result = $Controller->referer();
  517. $this->assertEquals('http://localhost/posts/index', $result);
  518. $Controller = new Controller(null);
  519. $result = $Controller->referer();
  520. $this->assertEquals('/', $result);
  521. }
  522. /**
  523. * Test that the referer is not absolute if it is '/'.
  524. *
  525. * This avoids the base path being applied twice on string urls.
  526. *
  527. * @return void
  528. */
  529. public function testRefererSlash()
  530. {
  531. $request = $this->getMock('Cake\Network\Request', ['referer']);
  532. $request->base = '/base';
  533. Router::pushRequest($request);
  534. $request->expects($this->any())->method('referer')
  535. ->will($this->returnValue('/'));
  536. $controller = new Controller($request);
  537. $result = $controller->referer('/', true);
  538. $this->assertEquals('/', $result);
  539. $controller = new Controller($request);
  540. $result = $controller->referer('/some/path', true);
  541. $this->assertEquals('/some/path', $result);
  542. }
  543. /**
  544. * testSetAction method
  545. *
  546. * @return void
  547. */
  548. public function testSetAction()
  549. {
  550. $request = new Request('controller_posts/index');
  551. $TestController = new TestController($request);
  552. $TestController->setAction('view', 1, 2);
  553. $expected = ['testId' => 1, 'test2Id' => 2];
  554. $this->assertSame($expected, $TestController->request->data);
  555. $this->assertSame('view', $TestController->request->params['action']);
  556. }
  557. /**
  558. * Tests that the startup process calls the correct functions
  559. *
  560. * @return void
  561. */
  562. public function testStartupProcess()
  563. {
  564. $eventManager = $this->getMock('Cake\Event\EventManager');
  565. $controller = new Controller(null, null, null, $eventManager);
  566. $eventManager->expects($this->at(0))->method('dispatch')
  567. ->with(
  568. $this->logicalAnd(
  569. $this->isInstanceOf('Cake\Event\Event'),
  570. $this->attributeEqualTo('_name', 'Controller.initialize'),
  571. $this->attributeEqualTo('_subject', $controller)
  572. )
  573. )
  574. ->will($this->returnValue($this->getMock('Cake\Event\Event', null, [], '', false)));
  575. $eventManager->expects($this->at(1))->method('dispatch')
  576. ->with(
  577. $this->logicalAnd(
  578. $this->isInstanceOf('Cake\Event\Event'),
  579. $this->attributeEqualTo('_name', 'Controller.startup'),
  580. $this->attributeEqualTo('_subject', $controller)
  581. )
  582. )
  583. ->will($this->returnValue($this->getMock('Cake\Event\Event', null, [], '', false)));
  584. $controller->startupProcess();
  585. }
  586. /**
  587. * Tests that the shutdown process calls the correct functions
  588. *
  589. * @return void
  590. */
  591. public function testShutdownProcess()
  592. {
  593. $eventManager = $this->getMock('Cake\Event\EventManager');
  594. $controller = new Controller(null, null, null, $eventManager);
  595. $eventManager->expects($this->once())->method('dispatch')
  596. ->with(
  597. $this->logicalAnd(
  598. $this->isInstanceOf('Cake\Event\Event'),
  599. $this->attributeEqualTo('_name', 'Controller.shutdown'),
  600. $this->attributeEqualTo('_subject', $controller)
  601. )
  602. )
  603. ->will($this->returnValue($this->getMock('Cake\Event\Event', null, [], '', false)));
  604. $controller->shutdownProcess();
  605. }
  606. /**
  607. * test using Controller::paginate()
  608. *
  609. * @return void
  610. */
  611. public function testPaginate()
  612. {
  613. $request = new Request('controller_posts/index');
  614. $request->params['pass'] = [];
  615. $response = $this->getMock('Cake\Network\Response', ['httpCodes']);
  616. $Controller = new Controller($request, $response);
  617. $Controller->request->query['url'] = [];
  618. $this->assertEquals([], $Controller->paginate);
  619. $this->assertNotContains('Paginator', $Controller->helpers);
  620. $this->assertArrayNotHasKey('Paginator', $Controller->helpers);
  621. $results = $Controller->paginate('Posts');
  622. $this->assertInstanceOf('Cake\Datasource\ResultSetInterface', $results);
  623. $results = $Controller->paginate(TableRegistry::get('Posts'));
  624. $this->assertInstanceOf('Cake\Datasource\ResultSetInterface', $results);
  625. $this->assertSame($Controller->request->params['paging']['Posts']['page'], 1);
  626. $this->assertSame($Controller->request->params['paging']['Posts']['pageCount'], 1);
  627. $this->assertSame($Controller->request->params['paging']['Posts']['prevPage'], false);
  628. $this->assertSame($Controller->request->params['paging']['Posts']['nextPage'], false);
  629. }
  630. /**
  631. * test that paginate uses modelClass property.
  632. *
  633. * @return void
  634. */
  635. public function testPaginateUsesModelClass()
  636. {
  637. $request = new Request('controller_posts/index');
  638. $request->params['pass'] = [];
  639. $response = $this->getMock('Cake\Network\Response', ['httpCodes']);
  640. $Controller = new Controller($request, $response);
  641. $Controller->request->query['url'] = [];
  642. $Controller->modelClass = 'Posts';
  643. $results = $Controller->paginate();
  644. $this->assertInstanceOf('Cake\Datasource\ResultSetInterface', $results);
  645. }
  646. /**
  647. * testMissingAction method
  648. *
  649. * @expectedException \Cake\Controller\Exception\MissingActionException
  650. * @expectedExceptionMessage Action TestController::missing() could not be found, or is not accessible.
  651. * @return void
  652. */
  653. public function testInvokeActionMissingAction()
  654. {
  655. $url = new Request('test/missing');
  656. $url->addParams(['controller' => 'Test', 'action' => 'missing']);
  657. $response = $this->getMock('Cake\Network\Response');
  658. $Controller = new TestController($url, $response);
  659. $Controller->invokeAction();
  660. }
  661. /**
  662. * test invoking private methods.
  663. *
  664. * @expectedException \Cake\Controller\Exception\MissingActionException
  665. * @expectedExceptionMessage Action TestController::private_m() could not be found, or is not accessible.
  666. * @return void
  667. */
  668. public function testInvokeActionPrivate()
  669. {
  670. $url = new Request('test/private_m/');
  671. $url->addParams(['controller' => 'Test', 'action' => 'private_m']);
  672. $response = $this->getMock('Cake\Network\Response');
  673. $Controller = new TestController($url, $response);
  674. $Controller->invokeAction();
  675. }
  676. /**
  677. * test invoking protected methods.
  678. *
  679. * @expectedException \Cake\Controller\Exception\MissingActionException
  680. * @expectedExceptionMessage Action TestController::protected_m() could not be found, or is not accessible.
  681. * @return void
  682. */
  683. public function testInvokeActionProtected()
  684. {
  685. $url = new Request('test/protected_m/');
  686. $url->addParams(['controller' => 'Test', 'action' => 'protected_m']);
  687. $response = $this->getMock('Cake\Network\Response');
  688. $Controller = new TestController($url, $response);
  689. $Controller->invokeAction();
  690. }
  691. /**
  692. * test invoking controller methods.
  693. *
  694. * @expectedException \Cake\Controller\Exception\MissingActionException
  695. * @expectedExceptionMessage Action TestController::redirect() could not be found, or is not accessible.
  696. * @return void
  697. */
  698. public function testInvokeActionBaseMethods()
  699. {
  700. $url = new Request('test/redirect/');
  701. $url->addParams(['controller' => 'Test', 'action' => 'redirect']);
  702. $response = $this->getMock('Cake\Network\Response');
  703. $Controller = new TestController($url, $response);
  704. $Controller->invokeAction();
  705. }
  706. /**
  707. * test invoking controller methods.
  708. *
  709. * @return void
  710. */
  711. public function testInvokeActionReturnValue()
  712. {
  713. $url = new Request('test/returner/');
  714. $url->addParams([
  715. 'controller' => 'Test',
  716. 'action' => 'returner',
  717. 'pass' => []
  718. ]);
  719. $response = $this->getMock('Cake\Network\Response');
  720. $Controller = new TestController($url, $response);
  721. $result = $Controller->invokeAction();
  722. $this->assertEquals('I am from the controller.', $result);
  723. }
  724. /**
  725. * test that a classes namespace is used in the viewPath.
  726. *
  727. * @return void
  728. */
  729. public function testViewPathConventions()
  730. {
  731. $request = new Request('admin/posts');
  732. $request->addParams([
  733. 'prefix' => 'admin'
  734. ]);
  735. $response = $this->getMock('Cake\Network\Response');
  736. $Controller = new \TestApp\Controller\Admin\PostsController($request, $response);
  737. $Controller->eventManager()->on('Controller.beforeRender', function (Event $e) {
  738. return $e->subject()->response;
  739. });
  740. $Controller->render();
  741. $this->assertEquals('Admin' . DS . 'Posts', $Controller->viewBuilder()->templatePath());
  742. $request->addParams([
  743. 'prefix' => 'admin/super'
  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 . 'Super' . DS . 'Posts', $Controller->viewBuilder()->templatePath());
  752. $request = new Request('pages/home');
  753. $request->addParams([
  754. 'prefix' => false
  755. ]);
  756. $Controller = new \TestApp\Controller\PagesController($request, $response);
  757. $Controller->eventManager()->on('Controller.beforeRender', function (Event $e) {
  758. return $e->subject()->response;
  759. });
  760. $Controller->render();
  761. $this->assertEquals('Pages', $Controller->viewBuilder()->templatePath());
  762. }
  763. /**
  764. * Test the components() method.
  765. *
  766. * @return void
  767. */
  768. public function testComponents()
  769. {
  770. $request = new Request('/');
  771. $response = $this->getMock('Cake\Network\Response');
  772. $controller = new TestController($request, $response);
  773. $this->assertInstanceOf('Cake\Controller\ComponentRegistry', $controller->components());
  774. $result = $controller->components();
  775. $this->assertSame($result, $controller->components());
  776. }
  777. /**
  778. * Test the components() method with the custom ObjectRegistry.
  779. *
  780. * @return void
  781. */
  782. public function testComponentsWithCustomRegistry()
  783. {
  784. $request = new Request('/');
  785. $response = $this->getMock('Cake\Network\Response');
  786. $componentRegistry = $this->getMock('Cake\Controller\ComponentRegistry', ['offsetGet']);
  787. $controller = new TestController($request, $response, null, null, $componentRegistry);
  788. $this->assertInstanceOf(get_class($componentRegistry), $controller->components());
  789. $result = $controller->components();
  790. $this->assertSame($result, $controller->components());
  791. }
  792. /**
  793. * Test adding a component
  794. *
  795. * @return void
  796. */
  797. public function testLoadComponent()
  798. {
  799. $request = new Request('/');
  800. $response = $this->getMock('Cake\Network\Response');
  801. $controller = new TestController($request, $response);
  802. $result = $controller->loadComponent('Paginator');
  803. $this->assertInstanceOf('Cake\Controller\Component\PaginatorComponent', $result);
  804. $this->assertSame($result, $controller->Paginator);
  805. $registry = $controller->components();
  806. $this->assertTrue(isset($registry->Paginator));
  807. }
  808. /**
  809. * Test adding a component that is a duplicate.
  810. *
  811. * @return void
  812. */
  813. public function testLoadComponentDuplicate()
  814. {
  815. $request = new Request('/');
  816. $response = $this->getMock('Cake\Network\Response');
  817. $controller = new TestController($request, $response);
  818. $this->assertNotEmpty($controller->loadComponent('Paginator'));
  819. $this->assertNotEmpty($controller->loadComponent('Paginator'));
  820. try {
  821. $controller->loadComponent('Paginator', ['bad' => 'settings']);
  822. $this->fail('No exception');
  823. } catch (\RuntimeException $e) {
  824. $this->assertContains('The "Paginator" alias has already been loaded', $e->getMessage());
  825. }
  826. }
  827. /**
  828. * Test the isAction method.
  829. *
  830. * @return void
  831. */
  832. public function testIsAction()
  833. {
  834. $request = new Request('/');
  835. $response = $this->getMock('Cake\Network\Response');
  836. $controller = new TestController($request, $response);
  837. $this->assertFalse($controller->isAction('redirect'));
  838. $this->assertFalse($controller->isAction('beforeFilter'));
  839. $this->assertTrue($controller->isAction('index'));
  840. }
  841. /**
  842. * Test declared deprecated properties like $theme are properly passed to view.
  843. *
  844. * @return void
  845. */
  846. public function testDeclaredDeprecatedProperty()
  847. {
  848. $controller = new TestController(new Request(), new Response());
  849. $theme = $controller->theme;
  850. // @codingStandardsIgnoreStart
  851. $this->assertEquals($theme, @$controller->createView()->theme);
  852. // @codingStandardsIgnoreEnd
  853. }
  854. /**
  855. * Test that view variables are being set after the beforeRender event gets dispatched
  856. *
  857. * @return void
  858. */
  859. public function testBeforeRenderViewVariables()
  860. {
  861. $controller = new PostsController();
  862. $controller->eventManager()->on('Controller.beforeRender', function (Event $event) {
  863. /* @var Controller $controller */
  864. $controller = $event->subject();
  865. $controller->set('testVariable', 'test');
  866. });
  867. $controller->render('index');
  868. $this->assertArrayHasKey('testVariable', $controller->View->viewVars);
  869. }
  870. }