RequestActionTraitTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright 2005-2012, 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 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since CakePHP(tm) v 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. $this->markTestIncomplete('Need to revisit once models work again.');
  38. Configure::write('App.namespace', 'TestApp');
  39. Configure::write('Security.salt', 'not-the-default');
  40. $this->object = $this->getObjectForTrait('Cake\Routing\RequestActionTrait');
  41. }
  42. /**
  43. * testRequestAction method
  44. *
  45. * @return void
  46. */
  47. public function testRequestAction() {
  48. $this->assertNull(Router::getRequest(), 'request stack should be empty.');
  49. $result = $this->object->requestAction('');
  50. $this->assertFalse($result);
  51. $result = $this->object->requestAction('/request_action/test_request_action');
  52. $expected = 'This is a test';
  53. $this->assertEquals($expected, $result);
  54. $result = $this->object->requestAction(Configure::read('App.fullBaseUrl') . '/request_action/test_request_action');
  55. $expected = 'This is a test';
  56. $this->assertEquals($expected, $result);
  57. $result = $this->object->requestAction('/request_action/another_ra_test/2/5');
  58. $expected = 7;
  59. $this->assertEquals($expected, $result);
  60. $result = $this->object->requestAction('/tests_apps/index', array('return'));
  61. $expected = 'This is the TestsAppsController index view ';
  62. $this->assertEquals($expected, $result);
  63. $result = $this->object->requestAction('/tests_apps/some_method');
  64. $expected = 5;
  65. $this->assertEquals($expected, $result);
  66. $result = $this->object->requestAction('/request_action/paginate_request_action');
  67. $this->assertTrue($result);
  68. $result = $this->object->requestAction('/request_action/normal_request_action');
  69. $expected = 'Hello World';
  70. $this->assertEquals($expected, $result);
  71. $this->assertNull(Router::getRequest(), 'requests were not popped off the stack, this will break url generation');
  72. }
  73. /**
  74. * test requestAction() and plugins.
  75. *
  76. * @return void
  77. */
  78. public function testRequestActionPlugins() {
  79. Plugin::load('TestPlugin');
  80. Router::reload();
  81. $result = $this->object->requestAction('/test_plugin/tests/index', array('return'));
  82. $expected = 'test plugin index';
  83. $this->assertEquals($expected, $result);
  84. $result = $this->object->requestAction('/test_plugin/tests/index/some_param', array('return'));
  85. $expected = 'test plugin index';
  86. $this->assertEquals($expected, $result);
  87. $result = $this->object->requestAction(
  88. array('controller' => 'tests', 'action' => 'index', 'plugin' => 'test_plugin'), array('return')
  89. );
  90. $expected = 'test plugin index';
  91. $this->assertEquals($expected, $result);
  92. $result = $this->object->requestAction('/test_plugin/tests/some_method');
  93. $expected = 25;
  94. $this->assertEquals($expected, $result);
  95. $result = $this->object->requestAction(
  96. array('controller' => 'tests', 'action' => 'some_method', 'plugin' => 'test_plugin')
  97. );
  98. $expected = 25;
  99. $this->assertEquals($expected, $result);
  100. }
  101. /**
  102. * test requestAction() with arrays.
  103. *
  104. * @return void
  105. */
  106. public function testRequestActionArray() {
  107. Plugin::load(array('TestPlugin'));
  108. $result = $this->object->requestAction(
  109. array('controller' => 'request_action', 'action' => 'test_request_action')
  110. );
  111. $expected = 'This is a test';
  112. $this->assertEquals($expected, $result);
  113. $result = $this->object->requestAction(
  114. array('controller' => 'request_action', 'action' => 'another_ra_test'),
  115. array('pass' => array('5', '7'))
  116. );
  117. $expected = 12;
  118. $this->assertEquals($expected, $result);
  119. $result = $this->object->requestAction(
  120. array('controller' => 'tests_apps', 'action' => 'index'), array('return')
  121. );
  122. $expected = 'This is the TestsAppsController index view ';
  123. $this->assertEquals($expected, $result);
  124. $result = $this->object->requestAction(array('controller' => 'tests_apps', 'action' => 'some_method'));
  125. $expected = 5;
  126. $this->assertEquals($expected, $result);
  127. $result = $this->object->requestAction(
  128. array('controller' => 'request_action', 'action' => 'normal_request_action')
  129. );
  130. $expected = 'Hello World';
  131. $this->assertEquals($expected, $result);
  132. $result = $this->object->requestAction(
  133. array('controller' => 'request_action', 'action' => 'paginate_request_action')
  134. );
  135. $this->assertTrue($result);
  136. $result = $this->object->requestAction(
  137. array('controller' => 'request_action', 'action' => 'paginate_request_action'),
  138. array('pass' => array(5))
  139. );
  140. $this->assertTrue($result);
  141. }
  142. /**
  143. * Test that requestAction() does not forward the 0 => return value.
  144. *
  145. * @return void
  146. */
  147. public function testRequestActionRemoveReturnParam() {
  148. $result = $this->object->requestAction(
  149. '/request_action/param_check', array('return')
  150. );
  151. $this->assertEquals('', $result, 'Return key was found');
  152. }
  153. /**
  154. * Test that requestAction() is populating $this->params properly
  155. *
  156. * @return void
  157. */
  158. public function testRequestActionParamParseAndPass() {
  159. $result = $this->object->requestAction('/request_action/params_pass');
  160. $this->assertEquals('request_action/params_pass', $result->url);
  161. $this->assertEquals('request_action', $result['controller']);
  162. $this->assertEquals('params_pass', $result['action']);
  163. $this->assertEquals(null, $result['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. $expected = null;
  177. $this->assertEmpty($result);
  178. $result = $this->object->requestAction(
  179. array('controller' => 'request_action', 'action' => 'post_pass'),
  180. array('post' => $_POST)
  181. );
  182. $expected = $_POST;
  183. $this->assertEquals($expected, $result);
  184. }
  185. /**
  186. * test that requestAction() can get query data from the query string and
  187. * query option.
  188. *
  189. * @return void
  190. */
  191. public function testRequestActionWithQueryString() {
  192. Router::reload();
  193. require CAKE . 'Config/routes.php';
  194. $query = ['page' => 1, 'sort' => 'title'];
  195. $result = $this->object->requestAction(
  196. ['controller' => 'request_action', 'action' => 'query_pass'],
  197. ['query' => $query]
  198. );
  199. $this->assertEquals($query, $result);
  200. $result = $this->object->requestAction(
  201. '/request_action/query_pass?page=3&sort=body'
  202. );
  203. $expected = ['page' => 3, 'sort' => 'body'];
  204. $this->assertEquals($expected, $result);
  205. }
  206. /**
  207. * Test requestAction with post data.
  208. *
  209. * @return void
  210. */
  211. public function testRequestActionPostWithData() {
  212. $data = array(
  213. 'Post' => array('id' => 2)
  214. );
  215. $result = $this->object->requestAction(
  216. array('controller' => 'request_action', 'action' => 'post_pass'),
  217. array('post' => $data)
  218. );
  219. $this->assertEquals($data, $result);
  220. $result = $this->object->requestAction(
  221. '/request_action/post_pass',
  222. array('post' => $data)
  223. );
  224. $this->assertEquals($data, $result);
  225. }
  226. /**
  227. * Test that requestAction handles get parameters correctly.
  228. *
  229. * @return void
  230. */
  231. public function testRequestActionGetParameters() {
  232. $result = $this->object->requestAction(
  233. '/request_action/params_pass?get=value&limit=5'
  234. );
  235. $this->assertEquals('value', $result->query['get']);
  236. $result = $this->object->requestAction(
  237. array('controller' => 'request_action', 'action' => 'params_pass'),
  238. array('query' => array('get' => 'value', 'limit' => 5))
  239. );
  240. $this->assertEquals('value', $result->query['get']);
  241. }
  242. }