RequestTransformerTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. $cake = RequestTransformer::toCake($psr);
  86. $this->assertEmpty($cake->query);
  87. $this->assertEmpty($cake->data);
  88. $this->assertEmpty($cake->cookie);
  89. $this->assertSame('application/json', $cake->header('accept'));
  90. $this->assertSame('PATCH', $cake->method());
  91. $this->assertSame('https', $cake->scheme());
  92. $this->assertSame(443, $cake->port());
  93. $this->assertSame('example.com', $cake->host());
  94. }
  95. /**
  96. * Test transforming with no routing parameters
  97. * still has the required keys.
  98. *
  99. * @return void
  100. */
  101. public function testToCakeParamsEmpty()
  102. {
  103. $psr = ServerRequestFactory::fromGlobals();
  104. $cake = RequestTransformer::toCake($psr);
  105. $this->assertArrayHasKey('controller', $cake->params);
  106. $this->assertArrayHasKey('action', $cake->params);
  107. $this->assertArrayHasKey('plugin', $cake->params);
  108. $this->assertArrayHasKey('_ext', $cake->params);
  109. $this->assertArrayHasKey('pass', $cake->params);
  110. }
  111. /**
  112. * Test transforming with non-empty params.
  113. *
  114. * @return void
  115. */
  116. public function testToCakeParamsPopulated()
  117. {
  118. $psr = ServerRequestFactory::fromGlobals();
  119. $psr = $psr->withAttribute('params', ['controller' => 'Articles', 'action' => 'index']);
  120. $cake = RequestTransformer::toCake($psr);
  121. $this->assertEmpty($cake->query);
  122. $this->assertEmpty($cake->data);
  123. $this->assertEmpty($cake->cookie);
  124. $this->assertSame('Articles', $cake->param('controller'));
  125. $this->assertSame('index', $cake->param('action'));
  126. $this->assertArrayHasKey('plugin', $cake->params);
  127. $this->assertArrayHasKey('_ext', $cake->params);
  128. $this->assertArrayHasKey('pass', $cake->params);
  129. }
  130. /**
  131. * Test transforming uploaded files
  132. *
  133. * @return void
  134. */
  135. public function testToCakeUploadedFiles()
  136. {
  137. $files = [
  138. 'image_main' => [
  139. 'name' => ['file' => 'born on.txt'],
  140. 'type' => ['file' => 'text/plain'],
  141. 'tmp_name' => ['file' => __FILE__],
  142. 'error' => ['file' => 0],
  143. 'size' => ['file' => 17178]
  144. ],
  145. 0 => [
  146. 'name' => ['image' => 'scratch.text'],
  147. 'type' => ['image' => 'text/plain'],
  148. 'tmp_name' => ['image' => __FILE__],
  149. 'error' => ['image' => 0],
  150. 'size' => ['image' => 1490]
  151. ],
  152. 'pictures' => [
  153. 'name' => [
  154. 0 => ['file' => 'a-file.png'],
  155. 1 => ['file' => 'a-moose.png']
  156. ],
  157. 'type' => [
  158. 0 => ['file' => 'image/png'],
  159. 1 => ['file' => 'image/jpg']
  160. ],
  161. 'tmp_name' => [
  162. 0 => ['file' => __FILE__],
  163. 1 => ['file' => __FILE__]
  164. ],
  165. 'error' => [
  166. 0 => ['file' => 0],
  167. 1 => ['file' => 0]
  168. ],
  169. 'size' => [
  170. 0 => ['file' => 17188],
  171. 1 => ['file' => 2010]
  172. ],
  173. ]
  174. ];
  175. $post = [
  176. 'pictures' => [
  177. 0 => ['name' => 'A cat'],
  178. 1 => ['name' => 'A moose']
  179. ],
  180. 0 => [
  181. 'name' => 'A dog'
  182. ]
  183. ];
  184. $psr = ServerRequestFactory::fromGlobals(null, null, $post, null, $files);
  185. $request = RequestTransformer::toCake($psr);
  186. $expected = [
  187. 'image_main' => [
  188. 'file' => [
  189. 'name' => 'born on.txt',
  190. 'type' => 'text/plain',
  191. 'tmp_name' => __FILE__,
  192. 'error' => 0,
  193. 'size' => 17178,
  194. ]
  195. ],
  196. 'pictures' => [
  197. 0 => [
  198. 'name' => 'A cat',
  199. 'file' => [
  200. 'name' => 'a-file.png',
  201. 'type' => 'image/png',
  202. 'tmp_name' => __FILE__,
  203. 'error' => 0,
  204. 'size' => 17188,
  205. ]
  206. ],
  207. 1 => [
  208. 'name' => 'A moose',
  209. 'file' => [
  210. 'name' => 'a-moose.png',
  211. 'type' => 'image/jpg',
  212. 'tmp_name' => __FILE__,
  213. 'error' => 0,
  214. 'size' => 2010,
  215. ]
  216. ]
  217. ],
  218. 0 => [
  219. 'name' => 'A dog',
  220. 'image' => [
  221. 'name' => 'scratch.text',
  222. 'type' => 'text/plain',
  223. 'tmp_name' => __FILE__,
  224. 'error' => 0,
  225. 'size' => 1490
  226. ]
  227. ]
  228. ];
  229. $this->assertEquals($expected, $request->data);
  230. }
  231. /**
  232. * Test that the transformed request sets the session path
  233. * as expected.
  234. *
  235. * @return void
  236. */
  237. public function testToCakeBaseSessionPath()
  238. {
  239. Configure::write('App.baseUrl', false);
  240. $server = [
  241. 'DOCUMENT_ROOT' => '/cake/repo/branches',
  242. 'PHP_SELF' => '/thisapp/webroot/index.php',
  243. 'REQUEST_URI' => '/posts/view/1',
  244. ];
  245. $psr = ServerRequestFactory::fromGlobals($server);
  246. $cake = RequestTransformer::toCake($psr);
  247. $this->assertEquals('/thisapp', ini_get('session.cookie_path'));
  248. }
  249. /**
  250. * Test transforming session objects
  251. *
  252. * @return void
  253. */
  254. public function testToCakeSession()
  255. {
  256. $psr = ServerRequestFactory::fromGlobals();
  257. $session = new Session(['defaults' => 'php']);
  258. $session->write('test', 'value');
  259. $psr = $psr->withAttribute('session', $session);
  260. $cake = RequestTransformer::toCake($psr);
  261. $this->assertSame($session, $cake->session());
  262. }
  263. /**
  264. * Test transforming request bodies
  265. *
  266. * @return void
  267. */
  268. public function testToCakeRequestBody()
  269. {
  270. $psr = ServerRequestFactory::fromGlobals();
  271. $stream = new Stream('php://memory', 'rw');
  272. $stream->write('{"hello":"world"}');
  273. $stream->rewind();
  274. $psr = $psr->withBody($stream);
  275. $cake = RequestTransformer::toCake($psr);
  276. $this->assertSame(['hello' => 'world'], $cake->input('json_decode', true));
  277. }
  278. }