RequestActionTraitTest.php 10 KB

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