RequestActionTraitTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Routing;
  15. use Cake\Core\App;
  16. use Cake\Core\Configure;
  17. use Cake\Core\Plugin;
  18. use Cake\Routing\DispatcherFactory;
  19. use Cake\Routing\RequestActionTrait;
  20. use Cake\Routing\Router;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. */
  24. class RequestActionTraitTest extends TestCase {
  25. /**
  26. * fixtures
  27. *
  28. * @var string
  29. */
  30. public $fixtures = array('core.post', 'core.test_plugin_comment', 'core.comment');
  31. /**
  32. * Setup
  33. *
  34. * @return void
  35. */
  36. public function setUp() {
  37. parent::setUp();
  38. Configure::write('App.namespace', 'TestApp');
  39. Configure::write('Security.salt', 'not-the-default');
  40. DispatcherFactory::add('Routing');
  41. DispatcherFactory::add('ControllerFactory');
  42. $this->object = $this->getObjectForTrait('Cake\Routing\RequestActionTrait');
  43. }
  44. /**
  45. * teardown
  46. *
  47. * @return void
  48. */
  49. public function tearDown() {
  50. parent::tearDown();
  51. DispatcherFactory::clear();
  52. }
  53. /**
  54. * testRequestAction method
  55. *
  56. * @return void
  57. */
  58. public function testRequestAction() {
  59. $this->assertNull(Router::getRequest(), 'request stack should be empty.');
  60. $result = $this->object->requestAction('');
  61. $this->assertFalse($result);
  62. $result = $this->object->requestAction('/request_action/test_request_action');
  63. $expected = 'This is a test';
  64. $this->assertEquals($expected, $result);
  65. $result = $this->object->requestAction(Configure::read('App.fullBaseUrl') . '/request_action/test_request_action');
  66. $expected = 'This is a test';
  67. $this->assertEquals($expected, $result);
  68. $result = $this->object->requestAction('/request_action/another_ra_test/2/5');
  69. $expected = 7;
  70. $this->assertEquals($expected, $result);
  71. $result = $this->object->requestAction('/tests_apps/index', array('return'));
  72. $expected = 'This is the TestsAppsController index view ';
  73. $this->assertEquals($expected, $result);
  74. $result = $this->object->requestAction('/tests_apps/some_method');
  75. $expected = 5;
  76. $this->assertEquals($expected, $result);
  77. $result = $this->object->requestAction('/request_action/paginate_request_action');
  78. $this->assertNull($result);
  79. $result = $this->object->requestAction('/request_action/normal_request_action');
  80. $expected = 'Hello World';
  81. $this->assertEquals($expected, $result);
  82. $this->assertNull(Router::getRequest(), 'requests were not popped off the stack, this will break url generation');
  83. }
  84. /**
  85. * test requestAction() and plugins.
  86. *
  87. * @return void
  88. */
  89. public function testRequestActionPlugins() {
  90. Plugin::load('TestPlugin');
  91. Router::reload();
  92. $result = $this->object->requestAction('/test_plugin/tests/index', array('return'));
  93. $expected = 'test plugin index';
  94. $this->assertEquals($expected, $result);
  95. $result = $this->object->requestAction('/test_plugin/tests/index/some_param', array('return'));
  96. $expected = 'test plugin index';
  97. $this->assertEquals($expected, $result);
  98. $result = $this->object->requestAction(
  99. array('controller' => 'tests', 'action' => 'index', 'plugin' => 'test_plugin'), array('return')
  100. );
  101. $expected = 'test plugin index';
  102. $this->assertEquals($expected, $result);
  103. $result = $this->object->requestAction('/test_plugin/tests/some_method');
  104. $expected = 25;
  105. $this->assertEquals($expected, $result);
  106. $result = $this->object->requestAction(
  107. array('controller' => 'tests', 'action' => 'some_method', 'plugin' => 'test_plugin')
  108. );
  109. $expected = 25;
  110. $this->assertEquals($expected, $result);
  111. }
  112. /**
  113. * test requestAction() with arrays.
  114. *
  115. * @return void
  116. */
  117. public function testRequestActionArray() {
  118. Plugin::load(array('TestPlugin'));
  119. $result = $this->object->requestAction(
  120. array('controller' => 'request_action', 'action' => 'test_request_action')
  121. );
  122. $expected = 'This is a test';
  123. $this->assertEquals($expected, $result);
  124. $result = $this->object->requestAction(
  125. array('controller' => 'request_action', 'action' => 'another_ra_test'),
  126. array('pass' => array('5', '7'))
  127. );
  128. $expected = 12;
  129. $this->assertEquals($expected, $result);
  130. $result = $this->object->requestAction(
  131. array('controller' => 'tests_apps', 'action' => 'index'), array('return')
  132. );
  133. $expected = 'This is the TestsAppsController index view ';
  134. $this->assertEquals($expected, $result);
  135. $result = $this->object->requestAction(array('controller' => 'tests_apps', 'action' => 'some_method'));
  136. $expected = 5;
  137. $this->assertEquals($expected, $result);
  138. $result = $this->object->requestAction(
  139. array('controller' => 'request_action', 'action' => 'normal_request_action')
  140. );
  141. $expected = 'Hello World';
  142. $this->assertEquals($expected, $result);
  143. $result = $this->object->requestAction(
  144. array('controller' => 'request_action', 'action' => 'paginate_request_action')
  145. );
  146. $this->assertNull($result);
  147. $result = $this->object->requestAction(
  148. array('controller' => 'request_action', 'action' => 'paginate_request_action'),
  149. array('pass' => array(5))
  150. );
  151. $this->assertNull($result);
  152. }
  153. /**
  154. * Test that requestAction() does not forward the 0 => return value.
  155. *
  156. * @return void
  157. */
  158. public function testRequestActionRemoveReturnParam() {
  159. $result = $this->object->requestAction(
  160. '/request_action/param_check', array('return')
  161. );
  162. $this->assertEquals('', $result, 'Return key was found');
  163. }
  164. /**
  165. * Test that requestAction() is populating $this->params properly
  166. *
  167. * @return void
  168. */
  169. public function testRequestActionParamParseAndPass() {
  170. $result = $this->object->requestAction('/request_action/params_pass');
  171. $result = json_decode($result, true);
  172. $this->assertEquals('request_action/params_pass', $result['url']);
  173. $this->assertEquals('request_action', $result['params']['controller']);
  174. $this->assertEquals('params_pass', $result['params']['action']);
  175. $this->assertNull($result['params']['plugin']);
  176. }
  177. /**
  178. * test that requestAction does not fish data out of the POST
  179. * superglobal.
  180. *
  181. * @return void
  182. */
  183. public function testRequestActionNoPostPassing() {
  184. $_POST = array(
  185. 'item' => 'value'
  186. );
  187. $result = $this->object->requestAction(array('controller' => 'request_action', 'action' => 'post_pass'));
  188. $result = json_decode($result, true);
  189. $this->assertEmpty($result);
  190. $result = $this->object->requestAction(
  191. array('controller' => 'request_action', 'action' => 'post_pass'),
  192. array('post' => $_POST)
  193. );
  194. $result = json_decode($result, true);
  195. $this->assertEquals($_POST, $result);
  196. }
  197. /**
  198. * test that requestAction() can get query data from the query string and
  199. * query option.
  200. *
  201. * @return void
  202. */
  203. public function testRequestActionWithQueryString() {
  204. Router::reload();
  205. require CAKE . 'Config/routes.php';
  206. $query = ['page' => 1, 'sort' => 'title'];
  207. $result = $this->object->requestAction(
  208. ['controller' => 'request_action', 'action' => 'query_pass'],
  209. ['query' => $query]
  210. );
  211. $result = json_decode($result, true);
  212. $this->assertEquals($query, $result);
  213. $result = $this->object->requestAction([
  214. 'controller' => 'request_action',
  215. 'action' => 'query_pass',
  216. '?' => $query
  217. ]);
  218. $result = json_decode($result, true);
  219. $this->assertEquals($query, $result);
  220. $result = $this->object->requestAction(
  221. '/request_action/query_pass?page=3&sort=body'
  222. );
  223. $result = json_decode($result, true);
  224. $expected = ['page' => 3, 'sort' => 'body'];
  225. $this->assertEquals($expected, $result);
  226. }
  227. /**
  228. * Test requestAction with post data.
  229. *
  230. * @return void
  231. */
  232. public function testRequestActionPostWithData() {
  233. $data = array(
  234. 'Post' => array('id' => 2)
  235. );
  236. $result = $this->object->requestAction(
  237. array('controller' => 'request_action', 'action' => 'post_pass'),
  238. array('post' => $data)
  239. );
  240. $result = json_decode($result, true);
  241. $this->assertEquals($data, $result);
  242. $result = $this->object->requestAction(
  243. '/request_action/post_pass',
  244. array('post' => $data)
  245. );
  246. $result = json_decode($result, true);
  247. $this->assertEquals($data, $result);
  248. }
  249. /**
  250. * Test that requestAction handles get parameters correctly.
  251. *
  252. * @return void
  253. */
  254. public function testRequestActionGetParameters() {
  255. $result = $this->object->requestAction(
  256. '/request_action/params_pass?get=value&limit=5'
  257. );
  258. $result = json_decode($result, true);
  259. $this->assertEquals('value', $result['query']['get']);
  260. $result = $this->object->requestAction(
  261. array('controller' => 'request_action', 'action' => 'params_pass'),
  262. array('query' => array('get' => 'value', 'limit' => 5))
  263. );
  264. $result = json_decode($result, true);
  265. $this->assertEquals('value', $result['query']['get']);
  266. }
  267. /**
  268. * Test that environment overrides can be set.
  269. *
  270. * @return void
  271. */
  272. public function testRequestActionEnvironment() {
  273. $result = $this->object->requestAction('/request_action/params_pass');
  274. $result = json_decode($result, true);
  275. $this->assertEquals('', $result['contentType'], 'Original content type not found.');
  276. $result = $this->object->requestAction(
  277. '/request_action/params_pass',
  278. ['environment' => ['CONTENT_TYPE' => 'application/json']]
  279. );
  280. $result = json_decode($result, true);
  281. $this->assertEquals('application/json', $result['contentType']);
  282. }
  283. /**
  284. * Tests that it is possible to transmit the session for the request
  285. *
  286. * @return void
  287. */
  288. public function testRequestActionSession() {
  289. $result = $this->object->requestAction('/request_action/session_test');
  290. $this->assertNull($result);
  291. $session = $this->getMock('Cake\Network\Session');
  292. $session->expects($this->once())
  293. ->method('read')
  294. ->with('foo')
  295. ->will($this->returnValue('bar'));
  296. $result = $this->object->requestAction(
  297. '/request_action/session_test',
  298. ['session' => $session]
  299. );
  300. $this->assertEquals('bar', $result);
  301. }
  302. }