RequestActionTraitTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Routing;
  15. use Cake\Core\Configure;
  16. use Cake\Core\Plugin;
  17. use Cake\Http\ServerRequest;
  18. use Cake\Routing\DispatcherFactory;
  19. use Cake\Routing\Router;
  20. use Cake\TestSuite\TestCase;
  21. use Cake\Utility\Security;
  22. /**
  23. */
  24. class RequestActionTraitTest extends TestCase
  25. {
  26. /**
  27. * fixtures
  28. *
  29. * @var string
  30. */
  31. public $fixtures = ['core.comments', 'core.posts', 'core.test_plugin_comments'];
  32. /**
  33. * Setup
  34. *
  35. * @return void
  36. */
  37. public function setUp()
  38. {
  39. parent::setUp();
  40. static::setAppNamespace();
  41. Security::setSalt('not-the-default');
  42. DispatcherFactory::add('Routing');
  43. DispatcherFactory::add('ControllerFactory');
  44. $this->object = $this->getObjectForTrait('Cake\Routing\RequestActionTrait');
  45. Router::connect('/request_action/:action/*', ['controller' => 'RequestAction']);
  46. Router::connect('/tests_apps/:action/*', ['controller' => 'TestsApps']);
  47. }
  48. /**
  49. * teardown
  50. *
  51. * @return void
  52. */
  53. public function tearDown()
  54. {
  55. parent::tearDown();
  56. DispatcherFactory::clear();
  57. Router::reload();
  58. }
  59. /**
  60. * testRequestAction method
  61. *
  62. * @return void
  63. */
  64. public function testRequestAction()
  65. {
  66. $this->assertNull(Router::getRequest(), 'request stack should be empty.');
  67. $result = $this->object->requestAction('');
  68. $this->assertFalse($result);
  69. $this->assertNull(Router::getRequest(), 'requests were not popped off the stack, this will break url generation');
  70. $result = $this->object->requestAction('/request_action/test_request_action');
  71. $expected = 'This is a test';
  72. $this->assertEquals($expected, $result);
  73. $this->assertNull(Router::getRequest(), 'requests were not popped off the stack, this will break url generation');
  74. $result = $this->object->requestAction(Configure::read('App.fullBaseUrl') . '/request_action/test_request_action');
  75. $expected = 'This is a test';
  76. $this->assertEquals($expected, $result);
  77. $this->assertNull(Router::getRequest(), 'requests were not popped off the stack, this will break url generation');
  78. $result = $this->object->requestAction('/request_action/another_ra_test/2/5');
  79. $expected = 7;
  80. $this->assertEquals($expected, $result);
  81. $this->assertNull(Router::getRequest(), 'requests were not popped off the stack, this will break url generation');
  82. $result = $this->object->requestAction('/tests_apps/index', ['return']);
  83. $expected = 'This is the TestsAppsController index view ';
  84. $this->assertEquals($expected, $result);
  85. $this->assertNull(Router::getRequest(), 'requests were not popped off the stack, this will break url generation');
  86. $result = $this->object->requestAction('/tests_apps/some_method');
  87. $expected = 5;
  88. $this->assertEquals($expected, $result);
  89. $this->assertNull(Router::getRequest(), 'requests were not popped off the stack, this will break url generation');
  90. $result = $this->object->requestAction('/request_action/paginate_request_action');
  91. $this->assertNull($result);
  92. $this->assertNull(Router::getRequest(), 'requests were not popped off the stack, this will break url generation');
  93. $result = $this->object->requestAction('/request_action/normal_request_action');
  94. $expected = 'Hello World';
  95. $this->assertEquals($expected, $result);
  96. $this->assertNull(Router::getRequest(), 'requests were not popped off the stack, this will break url generation');
  97. }
  98. /**
  99. * test requestAction() and plugins.
  100. *
  101. * @return void
  102. */
  103. public function testRequestActionPlugins()
  104. {
  105. Plugin::load('TestPlugin');
  106. Router::reload();
  107. Router::connect('/test_plugin/tests/:action/*', ['controller' => 'Tests', 'plugin' => 'TestPlugin']);
  108. $result = $this->object->requestAction('/test_plugin/tests/index', ['return']);
  109. $expected = 'test plugin index';
  110. $this->assertEquals($expected, $result);
  111. $result = $this->object->requestAction('/test_plugin/tests/index/some_param', ['return']);
  112. $expected = 'test plugin index';
  113. $this->assertEquals($expected, $result);
  114. $result = $this->object->requestAction(
  115. ['controller' => 'Tests', 'action' => 'index', 'plugin' => 'TestPlugin'],
  116. ['return']
  117. );
  118. $expected = 'test plugin index';
  119. $this->assertEquals($expected, $result);
  120. $result = $this->object->requestAction('/test_plugin/tests/some_method');
  121. $expected = 25;
  122. $this->assertEquals($expected, $result);
  123. $result = $this->object->requestAction(
  124. ['controller' => 'Tests', 'action' => 'some_method', 'plugin' => 'TestPlugin']
  125. );
  126. $expected = 25;
  127. $this->assertEquals($expected, $result);
  128. }
  129. /**
  130. * test requestAction() with arrays.
  131. *
  132. * @return void
  133. */
  134. public function testRequestActionArray()
  135. {
  136. Plugin::load(['TestPlugin']);
  137. $result = $this->object->requestAction(
  138. ['controller' => 'RequestAction', 'action' => 'test_request_action']
  139. );
  140. $expected = 'This is a test';
  141. $this->assertEquals($expected, $result);
  142. $result = $this->object->requestAction(
  143. ['controller' => 'RequestAction', 'action' => 'another_ra_test'],
  144. ['pass' => ['5', '7']]
  145. );
  146. $expected = 12;
  147. $this->assertEquals($expected, $result);
  148. $result = $this->object->requestAction(
  149. ['controller' => 'TestsApps', 'action' => 'index'],
  150. ['return']
  151. );
  152. $expected = 'This is the TestsAppsController index view ';
  153. $this->assertEquals($expected, $result);
  154. $result = $this->object->requestAction(['controller' => 'TestsApps', 'action' => 'some_method']);
  155. $expected = 5;
  156. $this->assertEquals($expected, $result);
  157. $result = $this->object->requestAction(
  158. ['controller' => 'RequestAction', 'action' => 'normal_request_action']
  159. );
  160. $expected = 'Hello World';
  161. $this->assertEquals($expected, $result);
  162. $result = $this->object->requestAction(
  163. ['controller' => 'RequestAction', 'action' => 'paginate_request_action']
  164. );
  165. $this->assertNull($result);
  166. $result = $this->object->requestAction(
  167. ['controller' => 'RequestAction', 'action' => 'paginate_request_action'],
  168. ['pass' => [5]]
  169. );
  170. $this->assertNull($result);
  171. }
  172. /**
  173. * Test that the required parameter names are seeded by requestAction.
  174. *
  175. * @return void
  176. */
  177. public function testRequestActionArraySetParamNames()
  178. {
  179. $result = $this->object->requestAction(
  180. ['controller' => 'RequestAction', 'action' => 'params_pass']
  181. );
  182. $result = json_decode($result, true);
  183. $this->assertArrayHasKey('action', $result['params']);
  184. $this->assertArrayHasKey('controller', $result['params']);
  185. $this->assertArrayHasKey('plugin', $result['params']);
  186. }
  187. /**
  188. * Test that requestAction() does not forward the 0 => return value.
  189. *
  190. * @return void
  191. */
  192. public function testRequestActionRemoveReturnParam()
  193. {
  194. $result = $this->object->requestAction(
  195. '/request_action/param_check',
  196. ['return']
  197. );
  198. $this->assertEquals('', $result, 'Return key was found');
  199. }
  200. /**
  201. * Test that requestAction() is populating $this->params properly
  202. *
  203. * @return void
  204. */
  205. public function testRequestActionParamParseAndPass()
  206. {
  207. $result = $this->object->requestAction('/request_action/params_pass');
  208. $result = json_decode($result, true);
  209. $this->assertEquals('request_action/params_pass', $result['url']);
  210. $this->assertEquals('RequestAction', $result['params']['controller']);
  211. $this->assertEquals('params_pass', $result['params']['action']);
  212. $this->assertNull($result['params']['plugin']);
  213. }
  214. /**
  215. * Test that requestAction() is populates the base and webroot properties properly
  216. *
  217. * @return void
  218. */
  219. public function testRequestActionBaseAndWebroot()
  220. {
  221. $request = new ServerRequest([
  222. 'base' => '/subdir',
  223. 'webroot' => '/subdir/'
  224. ]);
  225. Router::setRequestInfo($request);
  226. $result = $this->object->requestAction('/request_action/params_pass');
  227. $result = json_decode($result, true);
  228. $this->assertEquals($request->base, $result['base']);
  229. $this->assertEquals($request->webroot, $result['webroot']);
  230. }
  231. /**
  232. * test that requestAction does not fish data out of the POST
  233. * superglobal.
  234. *
  235. * @return void
  236. */
  237. public function testRequestActionNoPostPassing()
  238. {
  239. $_POST = [
  240. 'item' => 'value'
  241. ];
  242. $result = $this->object->requestAction(['controller' => 'RequestAction', 'action' => 'post_pass']);
  243. $result = json_decode($result, true);
  244. $this->assertEmpty($result);
  245. $result = $this->object->requestAction(
  246. ['controller' => 'RequestAction', 'action' => 'post_pass'],
  247. ['post' => $_POST]
  248. );
  249. $result = json_decode($result, true);
  250. $this->assertEquals($_POST, $result);
  251. }
  252. /**
  253. * test that requestAction() can get query data from the query string and
  254. * query option.
  255. *
  256. * @return void
  257. */
  258. public function testRequestActionWithQueryString()
  259. {
  260. $query = ['page' => 1, 'sort' => 'title'];
  261. $result = $this->object->requestAction(
  262. ['controller' => 'RequestAction', 'action' => 'query_pass'],
  263. ['query' => $query]
  264. );
  265. $result = json_decode($result, true);
  266. $this->assertEquals($query, $result);
  267. $result = $this->object->requestAction([
  268. 'controller' => 'RequestAction',
  269. 'action' => 'query_pass',
  270. '?' => $query
  271. ]);
  272. $result = json_decode($result, true);
  273. $this->assertEquals($query, $result);
  274. $result = $this->object->requestAction(
  275. '/request_action/query_pass?page=3&sort=body'
  276. );
  277. $result = json_decode($result, true);
  278. $expected = ['page' => 3, 'sort' => 'body'];
  279. $this->assertEquals($expected, $result);
  280. }
  281. /**
  282. * Test requestAction with post data.
  283. *
  284. * @return void
  285. */
  286. public function testRequestActionPostWithData()
  287. {
  288. $data = [
  289. 'Post' => ['id' => 2]
  290. ];
  291. $result = $this->object->requestAction(
  292. ['controller' => 'RequestAction', 'action' => 'post_pass'],
  293. ['post' => $data]
  294. );
  295. $result = json_decode($result, true);
  296. $this->assertEquals($data, $result);
  297. $result = $this->object->requestAction(
  298. '/request_action/post_pass',
  299. ['post' => $data]
  300. );
  301. $result = json_decode($result, true);
  302. $this->assertEquals($data, $result);
  303. }
  304. /**
  305. * Test that requestAction handles get parameters correctly.
  306. *
  307. * @return void
  308. */
  309. public function testRequestActionGetParameters()
  310. {
  311. $result = $this->object->requestAction(
  312. '/request_action/params_pass?get=value&limit=5'
  313. );
  314. $result = json_decode($result, true);
  315. $this->assertEquals('value', $result['query']['get']);
  316. $result = $this->object->requestAction(
  317. ['controller' => 'RequestAction', 'action' => 'params_pass'],
  318. ['query' => ['get' => 'value', 'limit' => 5]]
  319. );
  320. $result = json_decode($result, true);
  321. $this->assertEquals('value', $result['query']['get']);
  322. }
  323. /**
  324. * Test that requestAction handles cookies correctly.
  325. *
  326. * @return void
  327. */
  328. public function testRequestActionCookies()
  329. {
  330. $cookies = [
  331. 'foo' => 'bar'
  332. ];
  333. $result = $this->object->requestAction(
  334. '/request_action/cookie_pass',
  335. ['cookies' => $cookies]
  336. );
  337. $result = json_decode($result, true);
  338. $this->assertEquals($cookies, $result);
  339. }
  340. /**
  341. * Test that environment overrides can be set.
  342. *
  343. * @return void
  344. */
  345. public function testRequestActionEnvironment()
  346. {
  347. $result = $this->object->requestAction('/request_action/params_pass');
  348. $result = json_decode($result, true);
  349. $this->assertEquals('', $result['contentType'], 'Original content type not found.');
  350. $result = $this->object->requestAction(
  351. '/request_action/params_pass',
  352. ['environment' => ['CONTENT_TYPE' => 'application/json']]
  353. );
  354. $result = json_decode($result, true);
  355. $this->assertEquals('application/json', $result['contentType']);
  356. }
  357. /**
  358. * Tests that it is possible to transmit the session for the request
  359. *
  360. * @return void
  361. */
  362. public function testRequestActionSession()
  363. {
  364. $result = $this->object->requestAction('/request_action/session_test');
  365. $this->assertNull($result);
  366. $session = $this->getMockBuilder('Cake\Network\Session')->getMock();
  367. $session->expects($this->once())
  368. ->method('read')
  369. ->with('foo')
  370. ->will($this->returnValue('bar'));
  371. $result = $this->object->requestAction(
  372. '/request_action/session_test',
  373. ['session' => $session]
  374. );
  375. $this->assertEquals('bar', $result);
  376. }
  377. /**
  378. * requestAction relies on both the RoutingFilter and ControllerFactory
  379. * filters being connected. Ensure it can correct the missing state.
  380. *
  381. * @return void
  382. */
  383. public function testRequestActionAddsRequiredFilters()
  384. {
  385. DispatcherFactory::clear();
  386. $result = $this->object->requestAction('/request_action/test_request_action');
  387. $expected = 'This is a test';
  388. $this->assertEquals($expected, $result);
  389. }
  390. }