RequestTransformerTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.3.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Http;
  16. use Cake\Core\Configure;
  17. use Cake\Http\RequestTransformer;
  18. use Cake\Http\ServerRequestFactory;
  19. use Cake\Network\Request;
  20. use Cake\Network\Session;
  21. use Cake\TestSuite\TestCase;
  22. use Zend\Diactoros\Stream;
  23. /**
  24. * Test for RequestTransformer
  25. */
  26. class RequestTransformerTest extends TestCase
  27. {
  28. /**
  29. * Test transforming GET params.
  30. *
  31. * @return void
  32. */
  33. public function testToCakeGetParams()
  34. {
  35. $psr = ServerRequestFactory::fromGlobals(null, ['a' => 'b', 'c' => ['d' => 'e']]);
  36. $cake = RequestTransformer::toCake($psr);
  37. $this->assertEquals('b', $cake->query('a'));
  38. $this->assertEquals(['d' => 'e'], $cake->query('c'));
  39. $this->assertEmpty($cake->data);
  40. $this->assertEmpty($cake->cookies);
  41. }
  42. /**
  43. * Test transforming POST params.
  44. *
  45. * @return void
  46. */
  47. public function testToCakePostParams()
  48. {
  49. $psr = ServerRequestFactory::fromGlobals(null, null, ['title' => 'first post', 'some' => 'data']);
  50. $cake = RequestTransformer::toCake($psr);
  51. $this->assertEquals('first post', $cake->data('title'));
  52. $this->assertEquals('data', $cake->data('some'));
  53. $this->assertEmpty($cake->query);
  54. $this->assertEmpty($cake->cookies);
  55. }
  56. /**
  57. * Test transforming COOKIE params.
  58. *
  59. * @return void
  60. */
  61. public function testToCakeCookies()
  62. {
  63. $psr = ServerRequestFactory::fromGlobals(null, null, null, ['gtm' => 'watchingyou']);
  64. $cake = RequestTransformer::toCake($psr);
  65. $this->assertEmpty($cake->query);
  66. $this->assertEmpty($cake->data);
  67. $this->assertEquals('watchingyou', $cake->cookie('gtm'));
  68. }
  69. /**
  70. * Test transforming header and server params.
  71. *
  72. * @return void
  73. */
  74. public function testToCakeHeadersAndEnvironment()
  75. {
  76. $server = [
  77. 'HTTPS' => 'on',
  78. 'HTTP_HOST' => 'example.com',
  79. 'REQUEST_METHOD' => 'PATCH',
  80. 'HTTP_ACCEPT' => 'application/json',
  81. 'SERVER_PROTOCOL' => '1.1',
  82. 'SERVER_PORT' => 443,
  83. ];
  84. $psr = ServerRequestFactory::fromGlobals($server);
  85. $psr = $psr->withHeader('Api-Token', 'abc123')
  86. ->withAddedHeader('X-thing', 'one')
  87. ->withAddedHeader('X-thing', 'two');
  88. $cake = RequestTransformer::toCake($psr);
  89. $this->assertEmpty($cake->query);
  90. $this->assertEmpty($cake->data);
  91. $this->assertEmpty($cake->cookie);
  92. $this->assertSame('application/json', $cake->header('accept'));
  93. $this->assertSame('abc123', $cake->header('Api-Token'));
  94. $this->assertSame('one,two', $cake->header('X-thing'));
  95. $this->assertSame('PATCH', $cake->method());
  96. $this->assertSame('https', $cake->scheme());
  97. $this->assertSame(443, $cake->port());
  98. $this->assertSame('example.com', $cake->host());
  99. }
  100. /**
  101. * Test transforming with no routing parameters
  102. * still has the required keys.
  103. *
  104. * @return void
  105. */
  106. public function testToCakeParamsEmpty()
  107. {
  108. $psr = ServerRequestFactory::fromGlobals();
  109. $cake = RequestTransformer::toCake($psr);
  110. $this->assertArrayHasKey('controller', $cake->params);
  111. $this->assertArrayHasKey('action', $cake->params);
  112. $this->assertArrayHasKey('plugin', $cake->params);
  113. $this->assertArrayHasKey('_ext', $cake->params);
  114. $this->assertArrayHasKey('pass', $cake->params);
  115. }
  116. /**
  117. * Test transforming with non-empty params.
  118. *
  119. * @return void
  120. */
  121. public function testToCakeParamsPopulated()
  122. {
  123. $psr = ServerRequestFactory::fromGlobals();
  124. $psr = $psr->withAttribute('params', ['controller' => 'Articles', 'action' => 'index']);
  125. $cake = RequestTransformer::toCake($psr);
  126. $this->assertEmpty($cake->query);
  127. $this->assertEmpty($cake->data);
  128. $this->assertEmpty($cake->cookie);
  129. $this->assertSame('Articles', $cake->param('controller'));
  130. $this->assertSame('index', $cake->param('action'));
  131. $this->assertArrayHasKey('plugin', $cake->params);
  132. $this->assertArrayHasKey('_ext', $cake->params);
  133. $this->assertArrayHasKey('pass', $cake->params);
  134. }
  135. /**
  136. * Test transforming uploaded files
  137. *
  138. * @return void
  139. */
  140. public function testToCakeUploadedFiles()
  141. {
  142. $files = [
  143. 'no_file' => [
  144. 'name' => ['file' => ''],
  145. 'type' => ['file' => ''],
  146. 'tmp_name' => ['file' => ''],
  147. 'error' => ['file' => UPLOAD_ERR_NO_FILE],
  148. 'size' => ['file' => 0]
  149. ],
  150. 'image_main' => [
  151. 'name' => ['file' => 'born on.txt'],
  152. 'type' => ['file' => 'text/plain'],
  153. 'tmp_name' => ['file' => __FILE__],
  154. 'error' => ['file' => 0],
  155. 'size' => ['file' => 17178]
  156. ],
  157. 0 => [
  158. 'name' => ['image' => 'scratch.text'],
  159. 'type' => ['image' => 'text/plain'],
  160. 'tmp_name' => ['image' => __FILE__],
  161. 'error' => ['image' => 0],
  162. 'size' => ['image' => 1490]
  163. ],
  164. 'pictures' => [
  165. 'name' => [
  166. 0 => ['file' => 'a-file.png'],
  167. 1 => ['file' => 'a-moose.png']
  168. ],
  169. 'type' => [
  170. 0 => ['file' => 'image/png'],
  171. 1 => ['file' => 'image/jpg']
  172. ],
  173. 'tmp_name' => [
  174. 0 => ['file' => __FILE__],
  175. 1 => ['file' => __FILE__]
  176. ],
  177. 'error' => [
  178. 0 => ['file' => 0],
  179. 1 => ['file' => 0]
  180. ],
  181. 'size' => [
  182. 0 => ['file' => 17188],
  183. 1 => ['file' => 2010]
  184. ],
  185. ]
  186. ];
  187. $post = [
  188. 'pictures' => [
  189. 0 => ['name' => 'A cat'],
  190. 1 => ['name' => 'A moose']
  191. ],
  192. 0 => [
  193. 'name' => 'A dog'
  194. ]
  195. ];
  196. $psr = ServerRequestFactory::fromGlobals(null, null, $post, null, $files);
  197. $request = RequestTransformer::toCake($psr);
  198. $expected = [
  199. 'image_main' => [
  200. 'file' => [
  201. 'name' => 'born on.txt',
  202. 'type' => 'text/plain',
  203. 'tmp_name' => __FILE__,
  204. 'error' => 0,
  205. 'size' => 17178,
  206. ]
  207. ],
  208. 'no_file' => [
  209. 'file' => [
  210. 'name' => '',
  211. 'type' => '',
  212. 'tmp_name' => '',
  213. 'error' => UPLOAD_ERR_NO_FILE,
  214. 'size' => 0,
  215. ]
  216. ],
  217. 'pictures' => [
  218. 0 => [
  219. 'name' => 'A cat',
  220. 'file' => [
  221. 'name' => 'a-file.png',
  222. 'type' => 'image/png',
  223. 'tmp_name' => __FILE__,
  224. 'error' => 0,
  225. 'size' => 17188,
  226. ]
  227. ],
  228. 1 => [
  229. 'name' => 'A moose',
  230. 'file' => [
  231. 'name' => 'a-moose.png',
  232. 'type' => 'image/jpg',
  233. 'tmp_name' => __FILE__,
  234. 'error' => 0,
  235. 'size' => 2010,
  236. ]
  237. ]
  238. ],
  239. 0 => [
  240. 'name' => 'A dog',
  241. 'image' => [
  242. 'name' => 'scratch.text',
  243. 'type' => 'text/plain',
  244. 'tmp_name' => __FILE__,
  245. 'error' => 0,
  246. 'size' => 1490
  247. ]
  248. ]
  249. ];
  250. $this->assertEquals($expected, $request->data);
  251. }
  252. /**
  253. * Test that the transformed request sets the session path
  254. * as expected.
  255. *
  256. * @return void
  257. */
  258. public function testToCakeBaseSessionPath()
  259. {
  260. Configure::write('App.baseUrl', false);
  261. $server = [
  262. 'DOCUMENT_ROOT' => '/cake/repo/branches',
  263. 'PHP_SELF' => '/thisapp/webroot/index.php',
  264. 'REQUEST_URI' => '/posts/view/1',
  265. ];
  266. $psr = ServerRequestFactory::fromGlobals($server);
  267. $cake = RequestTransformer::toCake($psr);
  268. $this->assertEquals('/thisapp/', ini_get('session.cookie_path'));
  269. }
  270. /**
  271. * Test transforming session objects
  272. *
  273. * @return void
  274. */
  275. public function testToCakeSession()
  276. {
  277. $psr = ServerRequestFactory::fromGlobals();
  278. $session = new Session(['defaults' => 'php']);
  279. $session->write('test', 'value');
  280. $psr = $psr->withAttribute('session', $session);
  281. $cake = RequestTransformer::toCake($psr);
  282. $this->assertSame($session, $cake->session());
  283. }
  284. /**
  285. * Test transforming request bodies
  286. *
  287. * @return void
  288. */
  289. public function testToCakeRequestBody()
  290. {
  291. $psr = ServerRequestFactory::fromGlobals();
  292. $stream = new Stream('php://memory', 'rw');
  293. $stream->write('{"hello":"world"}');
  294. $stream->rewind();
  295. $psr = $psr->withBody($stream);
  296. $cake = RequestTransformer::toCake($psr);
  297. $this->assertSame(['hello' => 'world'], $cake->input('json_decode', true));
  298. }
  299. }