ActionDispatcherTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.3.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Http;
  16. use Cake\Event\Event;
  17. use Cake\Http\ActionDispatcher;
  18. use Cake\Http\Response;
  19. use Cake\Http\ServerRequest;
  20. use Cake\Http\Session;
  21. use Cake\Routing\DispatcherFactory;
  22. use Cake\Routing\Router;
  23. use Cake\TestSuite\TestCase;
  24. /**
  25. * Test case for the ActionDispatcher.
  26. */
  27. class ActionDispatcherTest extends TestCase
  28. {
  29. /**
  30. * Setup
  31. *
  32. * @return void
  33. */
  34. public function setUp()
  35. {
  36. parent::setUp();
  37. Router::reload();
  38. static::setAppNamespace();
  39. $this->dispatcher = new ActionDispatcher();
  40. }
  41. /**
  42. * Teardown
  43. *
  44. * @return void
  45. */
  46. public function tearDown()
  47. {
  48. parent::tearDown();
  49. DispatcherFactory::clear();
  50. }
  51. /**
  52. * Ensure the constructor args end up on the right protected properties.
  53. *
  54. * @return void
  55. */
  56. public function testConstructorArgs()
  57. {
  58. $factory = $this->getMockBuilder('Cake\Http\ControllerFactory')->getMock();
  59. $events = $this->getMockBuilder('Cake\Event\EventManager')->getMock();
  60. $dispatcher = new ActionDispatcher($factory, $events);
  61. $this->assertAttributeSame($events, '_eventManager', $dispatcher);
  62. $this->assertAttributeSame($factory, 'factory', $dispatcher);
  63. }
  64. /**
  65. * Ensure that filters connected to the DispatcherFactory are
  66. * also applied
  67. *
  68. * @group deprecated
  69. * @return void
  70. */
  71. public function testDispatcherFactoryCompat()
  72. {
  73. $this->deprecated(function () {
  74. $filter = $this->getMockBuilder('Cake\Routing\DispatcherFilter')
  75. ->setMethods(['beforeDispatch', 'afterDispatch'])
  76. ->getMock();
  77. DispatcherFactory::add($filter);
  78. $dispatcher = new ActionDispatcher(null, null, DispatcherFactory::filters());
  79. $this->assertCount(1, $dispatcher->getFilters());
  80. $this->assertSame($filter, $dispatcher->getFilters()[0]);
  81. });
  82. }
  83. /**
  84. * Test adding routing filters
  85. *
  86. * @group deprecated
  87. * @return void
  88. */
  89. public function testAddFilter()
  90. {
  91. $this->deprecated(function () {
  92. $this->assertCount(0, $this->dispatcher->getFilters());
  93. $events = $this->dispatcher->getEventManager();
  94. $this->assertCount(0, $events->listeners('Dispatcher.beforeDispatch'));
  95. $this->assertCount(0, $events->listeners('Dispatcher.afterDispatch'));
  96. $filter = $this->getMockBuilder('Cake\Routing\DispatcherFilter')
  97. ->setMethods(['beforeDispatch', 'afterDispatch'])
  98. ->getMock();
  99. $this->dispatcher->addFilter($filter);
  100. $this->assertCount(1, $this->dispatcher->getFilters());
  101. $this->assertCount(1, $events->listeners('Dispatcher.beforeDispatch'));
  102. $this->assertCount(1, $events->listeners('Dispatcher.afterDispatch'));
  103. });
  104. }
  105. /**
  106. * Ensure that aborting in the beforeDispatch doesn't invoke the controller
  107. *
  108. * @return void
  109. */
  110. public function testBeforeDispatchEventAbort()
  111. {
  112. $response = new Response();
  113. $dispatcher = new ActionDispatcher();
  114. $req = new ServerRequest();
  115. $res = new Response();
  116. $dispatcher->getEventManager()->on('Dispatcher.beforeDispatch', function () use ($response) {
  117. return $response;
  118. });
  119. $result = $dispatcher->dispatch($req, $res);
  120. $this->assertSame($response, $result, 'Should be response from filter.');
  121. }
  122. /**
  123. * Ensure afterDispatch can replace the response
  124. *
  125. * @return void
  126. */
  127. public function testDispatchAfterDispatchEventModifyResponse()
  128. {
  129. $req = new ServerRequest([
  130. 'url' => '/cakes',
  131. 'params' => [
  132. 'plugin' => null,
  133. 'controller' => 'Cakes',
  134. 'action' => 'index',
  135. 'pass' => [],
  136. ],
  137. 'session' => new Session
  138. ]);
  139. $res = new Response();
  140. $this->dispatcher->getEventManager()->on('Dispatcher.afterDispatch', function (Event $event) {
  141. $response = $event->getData('response');
  142. $event->setData('response', $response->withStringBody('Filter body'));
  143. });
  144. $result = $this->dispatcher->dispatch($req, $res);
  145. $this->assertSame('Filter body', (string)$result->getBody(), 'Should be response from filter.');
  146. }
  147. /**
  148. * Test that a controller action returning a response
  149. * results in no afterDispatch event.
  150. *
  151. * @return void
  152. */
  153. public function testDispatchActionReturnResponseNoAfterDispatch()
  154. {
  155. $req = new ServerRequest([
  156. 'url' => '/cakes',
  157. 'params' => [
  158. 'plugin' => null,
  159. 'controller' => 'Cakes',
  160. 'action' => 'index',
  161. 'pass' => [],
  162. 'return' => true,
  163. ],
  164. ]);
  165. $res = new Response();
  166. $this->dispatcher->getEventManager()->on('Dispatcher.afterDispatch', function () {
  167. $this->fail('no afterDispatch event should be fired');
  168. });
  169. $result = $this->dispatcher->dispatch($req, $res);
  170. $this->assertSame('Hello Jane', (string)$result->getBody(), 'Response from controller.');
  171. }
  172. /**
  173. * Test that dispatching sets the Router request state.
  174. *
  175. * @return void
  176. */
  177. public function testDispatchSetsRequestContext()
  178. {
  179. $this->assertNull(Router::getRequest());
  180. $req = new ServerRequest([
  181. 'url' => '/cakes',
  182. 'params' => [
  183. 'plugin' => null,
  184. 'controller' => 'Cakes',
  185. 'action' => 'index',
  186. 'pass' => [],
  187. 'return' => true,
  188. ],
  189. ]);
  190. $res = new Response();
  191. $this->dispatcher->dispatch($req, $res);
  192. $this->assertSame($req, Router::getRequest(true));
  193. }
  194. /**
  195. * test invalid response from dispatch process.
  196. *
  197. * @return void
  198. */
  199. public function testDispatchInvalidResponse()
  200. {
  201. $this->expectException(\LogicException::class);
  202. $this->expectExceptionMessage('Controller actions can only return Cake\Http\Response or null');
  203. $req = new ServerRequest([
  204. 'url' => '/cakes',
  205. 'params' => [
  206. 'plugin' => null,
  207. 'controller' => 'Cakes',
  208. 'action' => 'invalid',
  209. 'pass' => [],
  210. ],
  211. ]);
  212. $res = new Response();
  213. $result = $this->dispatcher->dispatch($req, $res);
  214. }
  215. /**
  216. * Test dispatch with autorender
  217. *
  218. * @return void
  219. */
  220. public function testDispatchAutoRender()
  221. {
  222. $request = new ServerRequest([
  223. 'url' => 'posts',
  224. 'params' => [
  225. 'controller' => 'Posts',
  226. 'action' => 'index',
  227. 'pass' => [],
  228. ]
  229. ]);
  230. $response = new Response();
  231. $result = $this->dispatcher->dispatch($request, $response);
  232. $this->assertInstanceOf('Cake\Http\Response', $result);
  233. $this->assertContains('posts index', (string)$result->getBody());
  234. }
  235. /**
  236. * Test dispatch with autorender=false
  237. *
  238. * @return void
  239. */
  240. public function testDispatchAutoRenderFalse()
  241. {
  242. $request = new ServerRequest([
  243. 'url' => 'posts',
  244. 'params' => [
  245. 'controller' => 'Cakes',
  246. 'action' => 'noRender',
  247. 'pass' => [],
  248. ]
  249. ]);
  250. $response = new Response();
  251. $result = $this->dispatcher->dispatch($request, $response);
  252. $this->assertInstanceOf('Cake\Http\Response', $result);
  253. $this->assertContains('autoRender false body', (string)$result->getBody());
  254. }
  255. /**
  256. * testMissingController method
  257. *
  258. * @return void
  259. */
  260. public function testMissingController()
  261. {
  262. $this->expectException(\Cake\Routing\Exception\MissingControllerException::class);
  263. $this->expectExceptionMessage('Controller class SomeController could not be found.');
  264. $request = new ServerRequest([
  265. 'url' => 'some_controller/home',
  266. 'params' => [
  267. 'controller' => 'SomeController',
  268. 'action' => 'home',
  269. ]
  270. ]);
  271. $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
  272. $this->dispatcher->dispatch($request, $response);
  273. }
  274. /**
  275. * testMissingControllerInterface method
  276. *
  277. * @return void
  278. */
  279. public function testMissingControllerInterface()
  280. {
  281. $this->expectException(\Cake\Routing\Exception\MissingControllerException::class);
  282. $this->expectExceptionMessage('Controller class Interface could not be found.');
  283. $request = new ServerRequest([
  284. 'url' => 'interface/index',
  285. 'params' => [
  286. 'controller' => 'Interface',
  287. 'action' => 'index',
  288. ]
  289. ]);
  290. $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
  291. $this->dispatcher->dispatch($request, $response);
  292. }
  293. /**
  294. * testMissingControllerInterface method
  295. *
  296. * @return void
  297. */
  298. public function testMissingControllerAbstract()
  299. {
  300. $this->expectException(\Cake\Routing\Exception\MissingControllerException::class);
  301. $this->expectExceptionMessage('Controller class Abstract could not be found.');
  302. $request = new ServerRequest([
  303. 'url' => 'abstract/index',
  304. 'params' => [
  305. 'controller' => 'Abstract',
  306. 'action' => 'index',
  307. ]
  308. ]);
  309. $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
  310. $this->dispatcher->dispatch($request, $response);
  311. }
  312. /**
  313. * Test that lowercase controller names result in missing controller errors.
  314. *
  315. * In case-insensitive file systems, lowercase controller names will kind of work.
  316. * This causes annoying deployment issues for lots of folks.
  317. *
  318. * @return void
  319. */
  320. public function testMissingControllerLowercase()
  321. {
  322. $this->expectException(\Cake\Routing\Exception\MissingControllerException::class);
  323. $this->expectExceptionMessage('Controller class somepages could not be found.');
  324. $request = new ServerRequest([
  325. 'url' => 'pages/home',
  326. 'params' => [
  327. 'plugin' => null,
  328. 'controller' => 'somepages',
  329. 'action' => 'display',
  330. 'pass' => ['home'],
  331. ]
  332. ]);
  333. $response = $this->getMockBuilder('Cake\Http\Response')->getMock();
  334. $this->dispatcher->dispatch($request, $response);
  335. }
  336. /**
  337. * Ensure that a controller's startup event can stop the request.
  338. *
  339. * @return void
  340. */
  341. public function testStartupProcessAbort()
  342. {
  343. $request = new ServerRequest([
  344. 'url' => 'cakes/index',
  345. 'params' => [
  346. 'plugin' => null,
  347. 'controller' => 'Cakes',
  348. 'action' => 'index',
  349. 'stop' => 'startup',
  350. 'pass' => [],
  351. ]
  352. ]);
  353. $response = new Response();
  354. $result = $this->dispatcher->dispatch($request, $response);
  355. $this->assertSame('startup stop', (string)$result->getBody());
  356. }
  357. /**
  358. * Ensure that a controllers startup process can emit a response
  359. *
  360. * @return void
  361. */
  362. public function testShutdownProcessResponse()
  363. {
  364. $request = new ServerRequest([
  365. 'url' => 'cakes/index',
  366. 'params' => [
  367. 'plugin' => null,
  368. 'controller' => 'Cakes',
  369. 'action' => 'index',
  370. 'stop' => 'shutdown',
  371. 'pass' => [],
  372. ]
  373. ]);
  374. $response = new Response();
  375. $result = $this->dispatcher->dispatch($request, $response);
  376. $this->assertSame('shutdown stop', (string)$result->getBody());
  377. }
  378. }