RequestActionTraitTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 MIT License (http://www.opensource.org/licenses/mit-license.php)
  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\RequestActionTrait;
  19. use Cake\Routing\Router;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. */
  23. class RequestActionTraitTest extends TestCase {
  24. /**
  25. * fixtures
  26. *
  27. * @var string
  28. */
  29. public $fixtures = array('core.post', 'core.test_plugin_comment', 'core.comment');
  30. /**
  31. * Setup
  32. *
  33. * @return void
  34. */
  35. public function setUp() {
  36. parent::setUp();
  37. Configure::write('App.namespace', 'TestApp');
  38. Configure::write('Security.salt', 'not-the-default');
  39. $this->object = $this->getObjectForTrait('Cake\Routing\RequestActionTrait');
  40. }
  41. /**
  42. * testRequestAction method
  43. *
  44. * @return void
  45. */
  46. public function testRequestAction() {
  47. $this->assertNull(Router::getRequest(), 'request stack should be empty.');
  48. $result = $this->object->requestAction('');
  49. $this->assertFalse($result);
  50. $result = $this->object->requestAction('/request_action/test_request_action');
  51. $expected = 'This is a test';
  52. $this->assertEquals($expected, $result);
  53. $result = $this->object->requestAction(Configure::read('App.fullBaseUrl') . '/request_action/test_request_action');
  54. $expected = 'This is a test';
  55. $this->assertEquals($expected, $result);
  56. $result = $this->object->requestAction('/request_action/another_ra_test/2/5');
  57. $expected = 7;
  58. $this->assertEquals($expected, $result);
  59. $result = $this->object->requestAction('/tests_apps/index', array('return'));
  60. $expected = 'This is the TestsAppsController index view ';
  61. $this->assertEquals($expected, $result);
  62. $result = $this->object->requestAction('/tests_apps/some_method');
  63. $expected = 5;
  64. $this->assertEquals($expected, $result);
  65. $result = $this->object->requestAction('/request_action/paginate_request_action');
  66. $this->assertNull($result);
  67. $result = $this->object->requestAction('/request_action/normal_request_action');
  68. $expected = 'Hello World';
  69. $this->assertEquals($expected, $result);
  70. $this->assertNull(Router::getRequest(), 'requests were not popped off the stack, this will break url generation');
  71. }
  72. /**
  73. * test requestAction() and plugins.
  74. *
  75. * @return void
  76. */
  77. public function testRequestActionPlugins() {
  78. Plugin::load('TestPlugin');
  79. Router::reload();
  80. $result = $this->object->requestAction('/test_plugin/tests/index', array('return'));
  81. $expected = 'test plugin index';
  82. $this->assertEquals($expected, $result);
  83. $result = $this->object->requestAction('/test_plugin/tests/index/some_param', array('return'));
  84. $expected = 'test plugin index';
  85. $this->assertEquals($expected, $result);
  86. $result = $this->object->requestAction(
  87. array('controller' => 'tests', 'action' => 'index', 'plugin' => 'test_plugin'), array('return')
  88. );
  89. $expected = 'test plugin index';
  90. $this->assertEquals($expected, $result);
  91. $result = $this->object->requestAction('/test_plugin/tests/some_method');
  92. $expected = 25;
  93. $this->assertEquals($expected, $result);
  94. $result = $this->object->requestAction(
  95. array('controller' => 'tests', 'action' => 'some_method', 'plugin' => 'test_plugin')
  96. );
  97. $expected = 25;
  98. $this->assertEquals($expected, $result);
  99. }
  100. /**
  101. * test requestAction() with arrays.
  102. *
  103. * @return void
  104. */
  105. public function testRequestActionArray() {
  106. Plugin::load(array('TestPlugin'));
  107. $result = $this->object->requestAction(
  108. array('controller' => 'request_action', 'action' => 'test_request_action')
  109. );
  110. $expected = 'This is a test';
  111. $this->assertEquals($expected, $result);
  112. $result = $this->object->requestAction(
  113. array('controller' => 'request_action', 'action' => 'another_ra_test'),
  114. array('pass' => array('5', '7'))
  115. );
  116. $expected = 12;
  117. $this->assertEquals($expected, $result);
  118. $result = $this->object->requestAction(
  119. array('controller' => 'tests_apps', 'action' => 'index'), array('return')
  120. );
  121. $expected = 'This is the TestsAppsController index view ';
  122. $this->assertEquals($expected, $result);
  123. $result = $this->object->requestAction(array('controller' => 'tests_apps', 'action' => 'some_method'));
  124. $expected = 5;
  125. $this->assertEquals($expected, $result);
  126. $result = $this->object->requestAction(
  127. array('controller' => 'request_action', 'action' => 'normal_request_action')
  128. );
  129. $expected = 'Hello World';
  130. $this->assertEquals($expected, $result);
  131. $result = $this->object->requestAction(
  132. array('controller' => 'request_action', 'action' => 'paginate_request_action')
  133. );
  134. $this->assertNull($result);
  135. $result = $this->object->requestAction(
  136. array('controller' => 'request_action', 'action' => 'paginate_request_action'),
  137. array('pass' => array(5))
  138. );
  139. $this->assertNull($result);
  140. }
  141. /**
  142. * Test that requestAction() does not forward the 0 => return value.
  143. *
  144. * @return void
  145. */
  146. public function testRequestActionRemoveReturnParam() {
  147. $result = $this->object->requestAction(
  148. '/request_action/param_check', array('return')
  149. );
  150. $this->assertEquals('', $result, 'Return key was found');
  151. }
  152. /**
  153. * Test that requestAction() is populating $this->params properly
  154. *
  155. * @return void
  156. */
  157. public function testRequestActionParamParseAndPass() {
  158. $result = $this->object->requestAction('/request_action/params_pass');
  159. $result = json_decode($result, true);
  160. $this->assertEquals('request_action/params_pass', $result['url']);
  161. $this->assertEquals('request_action', $result['params']['controller']);
  162. $this->assertEquals('params_pass', $result['params']['action']);
  163. $this->assertNull($result['params']['plugin']);
  164. }
  165. /**
  166. * test that requestAction does not fish data out of the POST
  167. * superglobal.
  168. *
  169. * @return void
  170. */
  171. public function testRequestActionNoPostPassing() {
  172. $_POST = array(
  173. 'item' => 'value'
  174. );
  175. $result = $this->object->requestAction(array('controller' => 'request_action', 'action' => 'post_pass'));
  176. $result = json_decode($result, true);
  177. $this->assertEmpty($result);
  178. $result = $this->object->requestAction(
  179. array('controller' => 'request_action', 'action' => 'post_pass'),
  180. array('post' => $_POST)
  181. );
  182. $result = json_decode($result, true);
  183. $expected = $_POST;
  184. $this->assertEquals($expected, $result);
  185. }
  186. /**
  187. * test that requestAction() can get query data from the query string and
  188. * query option.
  189. *
  190. * @return void
  191. */
  192. public function testRequestActionWithQueryString() {
  193. Router::reload();
  194. require CAKE . 'Config/routes.php';
  195. $query = ['page' => 1, 'sort' => 'title'];
  196. $result = $this->object->requestAction(
  197. ['controller' => 'request_action', 'action' => 'query_pass'],
  198. ['query' => $query]
  199. );
  200. $result = json_decode($result, true);
  201. $this->assertEquals($query, $result);
  202. $result = $this->object->requestAction([
  203. 'controller' => 'request_action',
  204. 'action' => 'query_pass',
  205. '?' => $query
  206. ]);
  207. $result = json_decode($result, true);
  208. $this->assertEquals($query, $result);
  209. $result = $this->object->requestAction(
  210. '/request_action/query_pass?page=3&sort=body'
  211. );
  212. $result = json_decode($result, true);
  213. $expected = ['page' => 3, 'sort' => 'body'];
  214. $this->assertEquals($expected, $result);
  215. }
  216. /**
  217. * Test requestAction with post data.
  218. *
  219. * @return void
  220. */
  221. public function testRequestActionPostWithData() {
  222. $data = array(
  223. 'Post' => array('id' => 2)
  224. );
  225. $result = $this->object->requestAction(
  226. array('controller' => 'request_action', 'action' => 'post_pass'),
  227. array('post' => $data)
  228. );
  229. $result = json_decode($result, true);
  230. $this->assertEquals($data, $result);
  231. $result = $this->object->requestAction(
  232. '/request_action/post_pass',
  233. array('post' => $data)
  234. );
  235. $result = json_decode($result, true);
  236. $this->assertEquals($data, $result);
  237. }
  238. /**
  239. * Test that requestAction handles get parameters correctly.
  240. *
  241. * @return void
  242. */
  243. public function testRequestActionGetParameters() {
  244. $result = $this->object->requestAction(
  245. '/request_action/params_pass?get=value&limit=5'
  246. );
  247. $result = json_decode($result, true);
  248. $this->assertEquals('value', $result['query']['get']);
  249. $result = $this->object->requestAction(
  250. array('controller' => 'request_action', 'action' => 'params_pass'),
  251. array('query' => array('get' => 'value', 'limit' => 5))
  252. );
  253. $result = json_decode($result, true);
  254. $this->assertEquals('value', $result['query']['get']);
  255. }
  256. }