RequestActionTraitTest.php 10 KB

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