RequestTransformerTest.php 11 KB

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