ControllerTest.php 30 KB

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