RequestActionTraitTest.php 15 KB

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