RequestTransformerTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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\TestSuite\TestCase;
  21. /**
  22. * Test for RequestTransformer
  23. */
  24. class RequestTransformerTest extends TestCase
  25. {
  26. /**
  27. * Test transforming GET params.
  28. *
  29. * @return void
  30. */
  31. public function testToCakeGetParams()
  32. {
  33. $psr = ServerRequestFactory::fromGlobals(null, ['a' => 'b', 'c' => ['d' => 'e']]);
  34. $cake = RequestTransformer::toCake($psr);
  35. $this->assertEquals('b', $cake->query('a'));
  36. $this->assertEquals(['d' => 'e'], $cake->query('c'));
  37. $this->assertEmpty($cake->data);
  38. $this->assertEmpty($cake->cookies);
  39. }
  40. /**
  41. * Test transforming POST params.
  42. *
  43. * @return void
  44. */
  45. public function testToCakePostParams()
  46. {
  47. $psr = ServerRequestFactory::fromGlobals(null, null, ['title' => 'first post', 'some' => 'data']);
  48. $cake = RequestTransformer::toCake($psr);
  49. $this->assertEquals('first post', $cake->data('title'));
  50. $this->assertEquals('data', $cake->data('some'));
  51. $this->assertEmpty($cake->query);
  52. $this->assertEmpty($cake->cookies);
  53. }
  54. /**
  55. * Test transforming COOKIE params.
  56. *
  57. * @return void
  58. */
  59. public function testToCakeCookies()
  60. {
  61. $psr = ServerRequestFactory::fromGlobals(null, null, null, ['gtm' => 'watchingyou']);
  62. $cake = RequestTransformer::toCake($psr);
  63. $this->assertEmpty($cake->query);
  64. $this->assertEmpty($cake->data);
  65. $this->assertEquals('watchingyou', $cake->cookie('gtm'));
  66. }
  67. /**
  68. * Test transforming header and server params.
  69. *
  70. * @return void
  71. */
  72. public function testToCakeHeadersAndEnvironment()
  73. {
  74. $server = [
  75. 'HTTPS' => 'on',
  76. 'HTTP_HOST' => 'example.com',
  77. 'REQUEST_METHOD' => 'PATCH',
  78. 'HTTP_ACCEPT' => 'application/json',
  79. 'SERVER_PROTOCOL' => '1.1',
  80. 'SERVER_PORT' => 443,
  81. ];
  82. $psr = ServerRequestFactory::fromGlobals($server);
  83. $cake = RequestTransformer::toCake($psr);
  84. $this->assertEmpty($cake->query);
  85. $this->assertEmpty($cake->data);
  86. $this->assertEmpty($cake->cookie);
  87. $this->assertSame('application/json', $cake->header('accept'));
  88. $this->assertSame('PATCH', $cake->method());
  89. $this->assertSame('https', $cake->scheme());
  90. $this->assertSame(443, $cake->port());
  91. $this->assertSame('example.com', $cake->host());
  92. }
  93. /**
  94. * Test transforming with no routing parameters
  95. * still has the required keys.
  96. *
  97. * @return void
  98. */
  99. public function testToCakeParamsEmpty()
  100. {
  101. $psr = ServerRequestFactory::fromGlobals();
  102. $cake = RequestTransformer::toCake($psr);
  103. $this->assertArrayHasKey('controller', $cake->params);
  104. $this->assertArrayHasKey('action', $cake->params);
  105. $this->assertArrayHasKey('plugin', $cake->params);
  106. $this->assertArrayHasKey('_ext', $cake->params);
  107. $this->assertArrayHasKey('pass', $cake->params);
  108. }
  109. /**
  110. * Test transforming with non-empty params.
  111. *
  112. * @return void
  113. */
  114. public function testToCakeParamsPopulated()
  115. {
  116. $psr = ServerRequestFactory::fromGlobals();
  117. $psr = $psr->withAttribute('params', ['controller' => 'Articles', 'action' => 'index']);
  118. $cake = RequestTransformer::toCake($psr);
  119. $this->assertEmpty($cake->query);
  120. $this->assertEmpty($cake->data);
  121. $this->assertEmpty($cake->cookie);
  122. $this->assertSame('Articles', $cake->param('controller'));
  123. $this->assertSame('index', $cake->param('action'));
  124. $this->assertArrayHasKey('plugin', $cake->params);
  125. $this->assertArrayHasKey('_ext', $cake->params);
  126. $this->assertArrayHasKey('pass', $cake->params);
  127. }
  128. /**
  129. * Test transforming uploaded files
  130. *
  131. * @return void
  132. */
  133. public function testToCakeUploadedFiles()
  134. {
  135. $files = [
  136. 'image_main' => [
  137. 'name' => ['file' => 'born on.txt'],
  138. 'type' => ['file' => 'text/plain'],
  139. 'tmp_name' => ['file' => __FILE__],
  140. 'error' => ['file' => 0],
  141. 'size' => ['file' => 17178]
  142. ],
  143. 0 => [
  144. 'name' => ['image' => 'scratch.text'],
  145. 'type' => ['image' => 'text/plain'],
  146. 'tmp_name' => ['image' => __FILE__],
  147. 'error' => ['image' => 0],
  148. 'size' => ['image' => 1490]
  149. ],
  150. 'pictures' => [
  151. 'name' => [
  152. 0 => ['file' => 'a-file.png'],
  153. 1 => ['file' => 'a-moose.png']
  154. ],
  155. 'type' => [
  156. 0 => ['file' => 'image/png'],
  157. 1 => ['file' => 'image/jpg']
  158. ],
  159. 'tmp_name' => [
  160. 0 => ['file' => __FILE__],
  161. 1 => ['file' => __FILE__]
  162. ],
  163. 'error' => [
  164. 0 => ['file' => 0],
  165. 1 => ['file' => 0]
  166. ],
  167. 'size' => [
  168. 0 => ['file' => 17188],
  169. 1 => ['file' => 2010]
  170. ],
  171. ]
  172. ];
  173. $post = [
  174. 'pictures' => [
  175. 0 => ['name' => 'A cat'],
  176. 1 => ['name' => 'A moose']
  177. ],
  178. 0 => [
  179. 'name' => 'A dog'
  180. ]
  181. ];
  182. $psr = ServerRequestFactory::fromGlobals(null, null, $post, null, $files);
  183. $request = RequestTransformer::toCake($psr);
  184. $expected = [
  185. 'image_main' => [
  186. 'file' => [
  187. 'name' => 'born on.txt',
  188. 'type' => 'text/plain',
  189. 'tmp_name' => __FILE__,
  190. 'error' => 0,
  191. 'size' => 17178,
  192. ]
  193. ],
  194. 'pictures' => [
  195. 0 => [
  196. 'name' => 'A cat',
  197. 'file' => [
  198. 'name' => 'a-file.png',
  199. 'type' => 'image/png',
  200. 'tmp_name' => __FILE__,
  201. 'error' => 0,
  202. 'size' => 17188,
  203. ]
  204. ],
  205. 1 => [
  206. 'name' => 'A moose',
  207. 'file' => [
  208. 'name' => 'a-moose.png',
  209. 'type' => 'image/jpg',
  210. 'tmp_name' => __FILE__,
  211. 'error' => 0,
  212. 'size' => 2010,
  213. ]
  214. ]
  215. ],
  216. 0 => [
  217. 'name' => 'A dog',
  218. 'image' => [
  219. 'name' => 'scratch.text',
  220. 'type' => 'text/plain',
  221. 'tmp_name' => __FILE__,
  222. 'error' => 0,
  223. 'size' => 1490
  224. ]
  225. ]
  226. ];
  227. $this->assertEquals($expected, $request->data);
  228. }
  229. /**
  230. * Test that the transformed request sets the session path
  231. * as expected.
  232. *
  233. * @return void
  234. */
  235. public function testToCakeBaseSessionPath()
  236. {
  237. Configure::write('App.baseUrl', false);
  238. $server = [
  239. 'DOCUMENT_ROOT' => '/cake/repo/branches',
  240. 'PHP_SELF' => '/thisapp/webroot/index.php',
  241. 'REQUEST_URI' => '/posts/view/1',
  242. ];
  243. $psr = ServerRequestFactory::fromGlobals($server);
  244. $cake = RequestTransformer::toCake($psr);
  245. $this->assertEquals('/thisapp', ini_get('session.cookie_path'));
  246. }
  247. }