ControllerTest.php 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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.posts',
  216. 'core.comments'
  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. $controller = new \TestApp\Controller\Admin\PostsController($request, $response);
  319. $this->assertEquals('Posts', $controller->modelClass);
  320. $this->assertInstanceOf('Cake\ORM\Table', $controller->Posts);
  321. $request->params['plugin'] = 'TestPlugin';
  322. $controller = new \TestPlugin\Controller\Admin\CommentsController($request, $response);
  323. $this->assertEquals('TestPlugin.Comments', $controller->modelClass);
  324. $this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $controller->Comments);
  325. }
  326. /**
  327. * testConstructClassesWithComponents method
  328. *
  329. * @return void
  330. */
  331. public function testConstructClassesWithComponents()
  332. {
  333. Plugin::load('TestPlugin');
  334. $Controller = new TestPluginController(new Request(), new Response());
  335. $Controller->loadComponent('TestPlugin.Other');
  336. $this->assertInstanceOf('TestPlugin\Controller\Component\OtherComponent', $Controller->Other);
  337. }
  338. /**
  339. * testRender method
  340. *
  341. * @return void
  342. */
  343. public function testRender()
  344. {
  345. Plugin::load('TestPlugin');
  346. $request = new Request('controller_posts/index');
  347. $request->params['action'] = 'index';
  348. $Controller = new Controller($request, new Response());
  349. $Controller->viewBuilder()->viewPath('Posts');
  350. $result = $Controller->render('index');
  351. $this->assertRegExp('/posts index/', (string)$result);
  352. $Controller->getView()->view = 'index';
  353. $Controller->getView()->hasRendered = false;
  354. $result = $Controller->render();
  355. $this->assertRegExp('/posts index/', (string)$result);
  356. $Controller->getView()->hasRendered = false;
  357. $result = $Controller->render('/Element/test_element');
  358. $this->assertRegExp('/this is the test element/', (string)$result);
  359. }
  360. /**
  361. * test that a component beforeRender can change the controller view class.
  362. *
  363. * @return void
  364. */
  365. public function testBeforeRenderCallbackChangingViewClass()
  366. {
  367. $Controller = new Controller(new Request, new Response());
  368. $Controller->eventManager()->on('Controller.beforeRender', function ($event) {
  369. $controller = $event->subject();
  370. $controller->viewClass = 'Json';
  371. });
  372. $Controller->set([
  373. 'test' => 'value',
  374. '_serialize' => ['test']
  375. ]);
  376. $debug = Configure::read('debug');
  377. Configure::write('debug', false);
  378. $result = $Controller->render('index');
  379. $this->assertEquals('{"test":"value"}', $result->body());
  380. Configure::write('debug', $debug);
  381. }
  382. /**
  383. * test that a component beforeRender can change the controller view class.
  384. *
  385. * @return void
  386. */
  387. public function testBeforeRenderEventCancelsRender()
  388. {
  389. $Controller = new Controller(new Request, new Response());
  390. $Controller->eventManager()->attach(function ($event) {
  391. return false;
  392. }, 'Controller.beforeRender');
  393. $result = $Controller->render('index');
  394. $this->assertInstanceOf('Cake\Network\Response', $result);
  395. }
  396. /**
  397. * Generates status codes for redirect test.
  398. *
  399. * @return void
  400. */
  401. public static function statusCodeProvider()
  402. {
  403. return [
  404. [300, "Multiple Choices"],
  405. [301, "Moved Permanently"],
  406. [302, "Found"],
  407. [303, "See Other"],
  408. [304, "Not Modified"],
  409. [305, "Use Proxy"],
  410. [307, "Temporary Redirect"],
  411. [403, "Forbidden"],
  412. ];
  413. }
  414. /**
  415. * testRedirect method
  416. *
  417. * @dataProvider statusCodeProvider
  418. * @return void
  419. */
  420. public function testRedirectByCode($code, $msg)
  421. {
  422. $Controller = new Controller(null, new Response());
  423. $response = $Controller->redirect('http://cakephp.org', (int)$code, false);
  424. $this->assertEquals($code, $response->statusCode());
  425. $this->assertEquals('http://cakephp.org', $response->header()['Location']);
  426. $this->assertFalse($Controller->autoRender);
  427. }
  428. /**
  429. * test that beforeRedirect callbacks can set the URL that is being redirected to.
  430. *
  431. * @return void
  432. */
  433. public function testRedirectBeforeRedirectModifyingUrl()
  434. {
  435. $Controller = new Controller(null, new Response());
  436. $Controller->eventManager()->attach(function ($event, $url, $response) {
  437. $response->location('http://book.cakephp.org');
  438. }, 'Controller.beforeRedirect');
  439. $response = $Controller->redirect('http://cakephp.org', 301);
  440. $this->assertEquals('http://book.cakephp.org', $response->header()['Location']);
  441. $this->assertEquals(301, $response->statusCode());
  442. }
  443. /**
  444. * test that beforeRedirect callback returning null doesn't affect things.
  445. *
  446. * @return void
  447. */
  448. public function testRedirectBeforeRedirectModifyingStatusCode()
  449. {
  450. $Response = $this->getMock('Cake\Network\Response', ['stop']);
  451. $Controller = new Controller(null, $Response);
  452. $Controller->eventManager()->attach(function ($event, $url, $response) {
  453. $response->statusCode(302);
  454. }, 'Controller.beforeRedirect');
  455. $response = $Controller->redirect('http://cakephp.org', 301, false);
  456. $this->assertEquals('http://cakephp.org', $response->header()['Location']);
  457. $this->assertEquals(302, $response->statusCode());
  458. }
  459. /**
  460. * test that beforeRedirect callback returning false in controller
  461. *
  462. * @return void
  463. */
  464. public function testRedirectBeforeRedirectListenerReturnFalse()
  465. {
  466. $Response = $this->getMock('Cake\Network\Response', ['stop', 'header']);
  467. $Controller = new Controller(null, $Response);
  468. $Controller->eventManager()->attach(function ($event, $url, $response) {
  469. return false;
  470. }, 'Controller.beforeRedirect');
  471. $Controller->response->expects($this->never())
  472. ->method('stop');
  473. $Controller->response->expects($this->never())
  474. ->method('header');
  475. $Controller->response->expects($this->never())
  476. ->method('statusCode');
  477. $result = $Controller->redirect('http://cakephp.org');
  478. $this->assertNull($result);
  479. }
  480. /**
  481. * testMergeVars method
  482. *
  483. * @return void
  484. */
  485. public function testMergeVars()
  486. {
  487. $request = new Request();
  488. $TestController = new TestController($request);
  489. $expected = [
  490. 'Html' => null,
  491. ];
  492. $this->assertEquals($expected, $TestController->helpers);
  493. $expected = [
  494. 'Security' => null,
  495. 'Cookie' => null,
  496. ];
  497. $this->assertEquals($expected, $TestController->components);
  498. $TestController = new AnotherTestController($request);
  499. $this->assertEquals(
  500. 'Posts',
  501. $TestController->modelClass,
  502. 'modelClass should not be overwritten when defined.'
  503. );
  504. }
  505. /**
  506. * testReferer method
  507. *
  508. * @return void
  509. */
  510. public function testReferer()
  511. {
  512. $request = $this->getMock('Cake\Network\Request', ['referer']);
  513. $request->expects($this->any())->method('referer')
  514. ->with(true)
  515. ->will($this->returnValue('/posts/index'));
  516. $Controller = new Controller($request);
  517. $result = $Controller->referer(null, true);
  518. $this->assertEquals('/posts/index', $result);
  519. $request = $this->getMock('Cake\Network\Request', ['referer']);
  520. $request->expects($this->any())->method('referer')
  521. ->with(true)
  522. ->will($this->returnValue('/posts/index'));
  523. $Controller = new Controller($request);
  524. $result = $Controller->referer(['controller' => 'posts', 'action' => 'index'], true);
  525. $this->assertEquals('/posts/index', $result);
  526. $request = $this->getMock('Cake\Network\Request', ['referer']);
  527. $request->expects($this->any())->method('referer')
  528. ->with(false)
  529. ->will($this->returnValue('http://localhost/posts/index'));
  530. $Controller = new Controller($request);
  531. $result = $Controller->referer();
  532. $this->assertEquals('http://localhost/posts/index', $result);
  533. $Controller = new Controller(null);
  534. $result = $Controller->referer();
  535. $this->assertEquals('/', $result);
  536. }
  537. /**
  538. * Test that the referer is not absolute if it is '/'.
  539. *
  540. * This avoids the base path being applied twice on string urls.
  541. *
  542. * @return void
  543. */
  544. public function testRefererSlash()
  545. {
  546. $request = $this->getMock('Cake\Network\Request', ['referer']);
  547. $request->base = '/base';
  548. Router::pushRequest($request);
  549. $request->expects($this->any())->method('referer')
  550. ->will($this->returnValue('/'));
  551. $controller = new Controller($request);
  552. $result = $controller->referer('/', true);
  553. $this->assertEquals('/', $result);
  554. $controller = new Controller($request);
  555. $result = $controller->referer('/some/path', true);
  556. $this->assertEquals('/base/some/path', $result);
  557. }
  558. /**
  559. * testSetAction method
  560. *
  561. * @return void
  562. */
  563. public function testSetAction()
  564. {
  565. $request = new Request('controller_posts/index');
  566. $TestController = new TestController($request);
  567. $TestController->setAction('view', 1, 2);
  568. $expected = ['testId' => 1, 'test2Id' => 2];
  569. $this->assertSame($expected, $TestController->request->data);
  570. $this->assertSame('view', $TestController->request->params['action']);
  571. }
  572. /**
  573. * Tests that the startup process calls the correct functions
  574. *
  575. * @return void
  576. */
  577. public function testStartupProcess()
  578. {
  579. $eventManager = $this->getMock('Cake\Event\EventManager');
  580. $controller = new Controller(null, null, null, $eventManager);
  581. $eventManager->expects($this->at(0))->method('dispatch')
  582. ->with(
  583. $this->logicalAnd(
  584. $this->isInstanceOf('Cake\Event\Event'),
  585. $this->attributeEqualTo('_name', 'Controller.initialize'),
  586. $this->attributeEqualTo('_subject', $controller)
  587. )
  588. )
  589. ->will($this->returnValue($this->getMock('Cake\Event\Event', null, [], '', false)));
  590. $eventManager->expects($this->at(1))->method('dispatch')
  591. ->with(
  592. $this->logicalAnd(
  593. $this->isInstanceOf('Cake\Event\Event'),
  594. $this->attributeEqualTo('_name', 'Controller.startup'),
  595. $this->attributeEqualTo('_subject', $controller)
  596. )
  597. )
  598. ->will($this->returnValue($this->getMock('Cake\Event\Event', null, [], '', false)));
  599. $controller->startupProcess();
  600. }
  601. /**
  602. * Tests that the shutdown process calls the correct functions
  603. *
  604. * @return void
  605. */
  606. public function testShutdownProcess()
  607. {
  608. $eventManager = $this->getMock('Cake\Event\EventManager');
  609. $controller = new Controller(null, null, null, $eventManager);
  610. $eventManager->expects($this->once())->method('dispatch')
  611. ->with(
  612. $this->logicalAnd(
  613. $this->isInstanceOf('Cake\Event\Event'),
  614. $this->attributeEqualTo('_name', 'Controller.shutdown'),
  615. $this->attributeEqualTo('_subject', $controller)
  616. )
  617. )
  618. ->will($this->returnValue($this->getMock('Cake\Event\Event', null, [], '', false)));
  619. $controller->shutdownProcess();
  620. }
  621. /**
  622. * test using Controller::paginate()
  623. *
  624. * @return void
  625. */
  626. public function testPaginate()
  627. {
  628. $request = new Request('controller_posts/index');
  629. $request->params['pass'] = [];
  630. $response = $this->getMock('Cake\Network\Response', ['httpCodes']);
  631. $Controller = new Controller($request, $response);
  632. $Controller->request->query['url'] = [];
  633. $this->assertEquals([], $Controller->paginate);
  634. $this->assertNotContains('Paginator', $Controller->helpers);
  635. $this->assertArrayNotHasKey('Paginator', $Controller->helpers);
  636. $results = $Controller->paginate('Posts');
  637. $this->assertInstanceOf('Cake\Datasource\ResultSetInterface', $results);
  638. $results = $Controller->paginate(TableRegistry::get('Posts'));
  639. $this->assertInstanceOf('Cake\Datasource\ResultSetInterface', $results);
  640. $this->assertSame($Controller->request->params['paging']['Posts']['page'], 1);
  641. $this->assertSame($Controller->request->params['paging']['Posts']['pageCount'], 1);
  642. $this->assertSame($Controller->request->params['paging']['Posts']['prevPage'], false);
  643. $this->assertSame($Controller->request->params['paging']['Posts']['nextPage'], false);
  644. }
  645. /**
  646. * test that paginate uses modelClass property.
  647. *
  648. * @return void
  649. */
  650. public function testPaginateUsesModelClass()
  651. {
  652. $request = new Request('controller_posts/index');
  653. $request->params['pass'] = [];
  654. $response = $this->getMock('Cake\Network\Response', ['httpCodes']);
  655. $Controller = new Controller($request, $response);
  656. $Controller->request->query['url'] = [];
  657. $Controller->modelClass = 'Posts';
  658. $results = $Controller->paginate();
  659. $this->assertInstanceOf('Cake\Datasource\ResultSetInterface', $results);
  660. }
  661. /**
  662. * testMissingAction method
  663. *
  664. * @expectedException \Cake\Controller\Exception\MissingActionException
  665. * @expectedExceptionMessage Action TestController::missing() could not be found, or is not accessible.
  666. * @return void
  667. */
  668. public function testInvokeActionMissingAction()
  669. {
  670. $url = new Request('test/missing');
  671. $url->addParams(['controller' => 'Test', 'action' => 'missing']);
  672. $response = $this->getMock('Cake\Network\Response');
  673. $Controller = new TestController($url, $response);
  674. $Controller->invokeAction();
  675. }
  676. /**
  677. * test invoking private methods.
  678. *
  679. * @expectedException \Cake\Controller\Exception\MissingActionException
  680. * @expectedExceptionMessage Action TestController::private_m() could not be found, or is not accessible.
  681. * @return void
  682. */
  683. public function testInvokeActionPrivate()
  684. {
  685. $url = new Request('test/private_m/');
  686. $url->addParams(['controller' => 'Test', 'action' => 'private_m']);
  687. $response = $this->getMock('Cake\Network\Response');
  688. $Controller = new TestController($url, $response);
  689. $Controller->invokeAction();
  690. }
  691. /**
  692. * test invoking protected methods.
  693. *
  694. * @expectedException \Cake\Controller\Exception\MissingActionException
  695. * @expectedExceptionMessage Action TestController::protected_m() could not be found, or is not accessible.
  696. * @return void
  697. */
  698. public function testInvokeActionProtected()
  699. {
  700. $url = new Request('test/protected_m/');
  701. $url->addParams(['controller' => 'Test', 'action' => 'protected_m']);
  702. $response = $this->getMock('Cake\Network\Response');
  703. $Controller = new TestController($url, $response);
  704. $Controller->invokeAction();
  705. }
  706. /**
  707. * test invoking controller methods.
  708. *
  709. * @expectedException \Cake\Controller\Exception\MissingActionException
  710. * @expectedExceptionMessage Action TestController::redirect() could not be found, or is not accessible.
  711. * @return void
  712. */
  713. public function testInvokeActionBaseMethods()
  714. {
  715. $url = new Request('test/redirect/');
  716. $url->addParams(['controller' => 'Test', 'action' => 'redirect']);
  717. $response = $this->getMock('Cake\Network\Response');
  718. $Controller = new TestController($url, $response);
  719. $Controller->invokeAction();
  720. }
  721. /**
  722. * test invoking controller methods.
  723. *
  724. * @return void
  725. */
  726. public function testInvokeActionReturnValue()
  727. {
  728. $url = new Request('test/returner/');
  729. $url->addParams([
  730. 'controller' => 'Test',
  731. 'action' => 'returner',
  732. 'pass' => []
  733. ]);
  734. $response = $this->getMock('Cake\Network\Response');
  735. $Controller = new TestController($url, $response);
  736. $result = $Controller->invokeAction();
  737. $this->assertEquals('I am from the controller.', $result);
  738. }
  739. /**
  740. * test that a classes namespace is used in the viewPath.
  741. *
  742. * @return void
  743. */
  744. public function testViewPathConventions()
  745. {
  746. $request = new Request('admin/posts');
  747. $request->addParams([
  748. 'prefix' => 'admin'
  749. ]);
  750. $response = $this->getMock('Cake\Network\Response');
  751. $Controller = new \TestApp\Controller\Admin\PostsController($request, $response);
  752. $Controller->eventManager()->on('Controller.beforeRender', function (Event $e) {
  753. return $e->subject()->response;
  754. });
  755. $Controller->render();
  756. $this->assertEquals('Admin' . DS . 'Posts', $Controller->getView()->viewPath);
  757. $request->addParams([
  758. 'prefix' => 'admin/super'
  759. ]);
  760. $response = $this->getMock('Cake\Network\Response');
  761. $Controller = new \TestApp\Controller\Admin\PostsController($request, $response);
  762. $Controller->eventManager()->on('Controller.beforeRender', function (Event $e) {
  763. return $e->subject()->response;
  764. });
  765. $Controller->render();
  766. $this->assertEquals('Admin' . DS . 'Super' . DS . 'Posts', $Controller->getView()->viewPath);
  767. $request = new Request('pages/home');
  768. $request->addParams([
  769. 'prefix' => false
  770. ]);
  771. $Controller = new \TestApp\Controller\PagesController($request, $response);
  772. $Controller->eventManager()->on('Controller.beforeRender', function (Event $e) {
  773. return $e->subject()->response;
  774. });
  775. $Controller->render();
  776. $this->assertEquals('Pages', $Controller->getView()->viewPath);
  777. }
  778. /**
  779. * Test the components() method.
  780. *
  781. * @return void
  782. */
  783. public function testComponents()
  784. {
  785. $request = new Request('/');
  786. $response = $this->getMock('Cake\Network\Response');
  787. $controller = new TestController($request, $response);
  788. $this->assertInstanceOf('Cake\Controller\ComponentRegistry', $controller->components());
  789. $result = $controller->components();
  790. $this->assertSame($result, $controller->components());
  791. }
  792. /**
  793. * Test 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. * @return void
  844. */
  845. public function testDeclaredDeprecatedProperty()
  846. {
  847. $controller = new TestController(new Request(), new Response());
  848. $theme = $controller->theme;
  849. // @codingStandardsIgnoreStart
  850. $this->assertEquals($theme, @$controller->getView()->theme);
  851. // @codingStandardsIgnoreEnd
  852. }
  853. /**
  854. * Test that view variables are being set after the beforeRender event gets dispatched
  855. *
  856. * @return void
  857. */
  858. public function testBeforeRenderViewVariables()
  859. {
  860. $controller = new PostsController();
  861. $controller->eventManager()->on('Controller.beforeRender', function (Event $event) {
  862. /* @var Controller $controller */
  863. $controller = $event->subject();
  864. $controller->set('testVariable', 'test');
  865. });
  866. $controller->render('index');
  867. $this->assertArrayHasKey('testVariable', $controller->View->viewVars);
  868. }
  869. }