ControllerTest.php 30 KB

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