RequestActionTraitTest.php 15 KB

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