RequestActionTraitTest.php 15 KB

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