ServerRequestFactoryTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.3.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Http;
  17. use Cake\Core\Configure;
  18. use Cake\Http\ServerRequestFactory;
  19. use Cake\Http\Session;
  20. use Cake\TestSuite\TestCase;
  21. use Laminas\Diactoros\Exception\InvalidArgumentException;
  22. use Laminas\Diactoros\UploadedFile;
  23. use Psr\Http\Message\UploadedFileInterface;
  24. /**
  25. * Test case for the server factory.
  26. */
  27. class ServerRequestFactoryTest extends TestCase
  28. {
  29. /**
  30. * @var array|null
  31. */
  32. protected $server;
  33. /**
  34. * @var array|null
  35. */
  36. protected $post;
  37. /**
  38. * @var array|null
  39. */
  40. protected $files;
  41. /**
  42. * @var array|null
  43. */
  44. protected $cookies;
  45. /**
  46. * @var array|null
  47. */
  48. protected $get;
  49. /**
  50. * setup
  51. *
  52. * @return void
  53. */
  54. public function setUp(): void
  55. {
  56. parent::setUp();
  57. $this->server = $_SERVER;
  58. $this->post = $_POST;
  59. $this->files = $_FILES;
  60. $this->cookies = $_COOKIE;
  61. $this->get = $_GET;
  62. }
  63. /**
  64. * teardown
  65. *
  66. * @return void
  67. */
  68. public function tearDown(): void
  69. {
  70. parent::tearDown();
  71. $_SERVER = $this->server;
  72. $_POST = $this->post;
  73. $_FILES = $this->files;
  74. $_COOKIE = $this->cookies;
  75. $_GET = $this->get;
  76. }
  77. /**
  78. * Test fromGlobals reads super globals
  79. *
  80. * @return void
  81. */
  82. public function testFromGlobalsSuperGlobals()
  83. {
  84. $_POST = [
  85. 'title' => 'custom',
  86. ];
  87. $_FILES = [
  88. 'image' => [
  89. 'tmp_name' => __FILE__,
  90. 'error' => 0,
  91. 'name' => 'cats.png',
  92. 'type' => 'image/png',
  93. 'size' => 2112,
  94. ],
  95. ];
  96. $_COOKIE = ['key' => 'value'];
  97. $_GET = ['query' => 'string'];
  98. $res = ServerRequestFactory::fromGlobals();
  99. $this->assertSame($_COOKIE['key'], $res->getCookie('key'));
  100. $this->assertSame($_GET['query'], $res->getQuery('query'));
  101. $this->assertArrayHasKey('title', $res->getData());
  102. $this->assertArrayHasKey('image', $res->getData());
  103. $this->assertCount(1, $res->getUploadedFiles());
  104. /** @var \Laminas\Diactoros\UploadedFile $expected */
  105. $expected = $res->getData('image');
  106. $this->assertInstanceOf(UploadedFileInterface::class, $expected);
  107. $this->assertSame($_FILES['image']['size'], $expected->getSize());
  108. $this->assertSame($_FILES['image']['error'], $expected->getError());
  109. $this->assertSame($_FILES['image']['name'], $expected->getClientFilename());
  110. $this->assertSame($_FILES['image']['type'], $expected->getClientMediaType());
  111. }
  112. /**
  113. * Test fromGlobals includes the session
  114. *
  115. * @preserveGlobalState disabled
  116. * @runInSeparateProcess
  117. * @return void
  118. */
  119. public function testFromGlobalsUrlSession()
  120. {
  121. Configure::write('App.base', '/basedir');
  122. $server = [
  123. 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
  124. 'PHP_SELF' => '/index.php',
  125. 'REQUEST_URI' => '/posts/add',
  126. ];
  127. $res = ServerRequestFactory::fromGlobals($server);
  128. $session = $res->getAttribute('session');
  129. $this->assertInstanceOf(Session::class, $session);
  130. $this->assertSame('/basedir/', ini_get('session.cookie_path'), 'Needs trailing / for cookie to work');
  131. }
  132. /**
  133. * Test fromGlobals with App.base defined.
  134. *
  135. * @return void
  136. */
  137. public function testFromGlobalsUrlBaseDefined()
  138. {
  139. Configure::write('App.base', 'basedir');
  140. $server = [
  141. 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
  142. 'PHP_SELF' => '/index.php',
  143. 'REQUEST_URI' => '/posts/add',
  144. ];
  145. $res = ServerRequestFactory::fromGlobals($server);
  146. $this->assertSame('basedir', $res->getAttribute('base'));
  147. $this->assertSame('basedir/', $res->getAttribute('webroot'));
  148. $this->assertSame('/posts/add', $res->getUri()->getPath());
  149. }
  150. /**
  151. * Test fromGlobals with mod-rewrite server configuration.
  152. *
  153. * @return void
  154. */
  155. public function testFromGlobalsUrlModRewrite()
  156. {
  157. Configure::write('App.baseUrl', false);
  158. $server = [
  159. 'DOCUMENT_ROOT' => '/cake/repo/branches',
  160. 'PHP_SELF' => '/urlencode me/webroot/index.php',
  161. 'REQUEST_URI' => '/posts/view/1',
  162. ];
  163. $res = ServerRequestFactory::fromGlobals($server);
  164. $this->assertSame('/urlencode%20me', $res->getAttribute('base'));
  165. $this->assertSame('/urlencode%20me/', $res->getAttribute('webroot'));
  166. $this->assertSame('/posts/view/1', $res->getUri()->getPath());
  167. }
  168. /**
  169. * Test fromGlobals with mod-rewrite in the root dir.
  170. *
  171. * @return void
  172. */
  173. public function testFromGlobalsUrlModRewriteRootDir()
  174. {
  175. $server = [
  176. 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
  177. 'PHP_SELF' => '/index.php',
  178. 'REQUEST_URI' => '/posts/add',
  179. ];
  180. $res = ServerRequestFactory::fromGlobals($server);
  181. $this->assertSame('', $res->getAttribute('base'));
  182. $this->assertSame('/', $res->getAttribute('webroot'));
  183. $this->assertSame('/posts/add', $res->getUri()->getPath());
  184. }
  185. /**
  186. * Test fromGlobals with App.baseUrl defined implying no
  187. * mod-rewrite and no virtual path.
  188. *
  189. * @return void
  190. */
  191. public function testFromGlobalsUrlNoModRewriteWebrootDir()
  192. {
  193. Configure::write('App', [
  194. 'dir' => 'app',
  195. 'webroot' => 'www',
  196. 'base' => false,
  197. 'baseUrl' => '/cake/index.php',
  198. ]);
  199. $server = [
  200. 'DOCUMENT_ROOT' => '/Users/markstory/Sites',
  201. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/www/index.php',
  202. 'PHP_SELF' => '/cake/www/index.php/posts/index',
  203. 'REQUEST_URI' => '/cake/www/index.php',
  204. ];
  205. $res = ServerRequestFactory::fromGlobals($server);
  206. $this->assertSame('/cake/www/', $res->getAttribute('webroot'));
  207. $this->assertSame('/cake/index.php', $res->getAttribute('base'));
  208. $this->assertSame('/', $res->getUri()->getPath());
  209. }
  210. /**
  211. * Test fromGlobals with App.baseUrl defined implying no
  212. * mod-rewrite
  213. *
  214. * @return void
  215. */
  216. public function testFromGlobalsUrlNoModRewrite()
  217. {
  218. Configure::write('App', [
  219. 'dir' => 'app',
  220. 'webroot' => 'webroot',
  221. 'base' => false,
  222. 'baseUrl' => '/cake/index.php',
  223. ]);
  224. $server = [
  225. 'DOCUMENT_ROOT' => '/Users/markstory/Sites',
  226. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/index.php',
  227. 'PHP_SELF' => '/cake/index.php/posts/index',
  228. 'REQUEST_URI' => '/cake/index.php/posts/index',
  229. ];
  230. $res = ServerRequestFactory::fromGlobals($server);
  231. $this->assertSame('/cake/webroot/', $res->getAttribute('webroot'));
  232. $this->assertSame('/cake/index.php', $res->getAttribute('base'));
  233. $this->assertSame('/posts/index', $res->getUri()->getPath());
  234. }
  235. /**
  236. * Test fromGlobals with App.baseUrl defined implying no
  237. * mod-rewrite in the root dir.
  238. *
  239. * @return void
  240. */
  241. public function testFromGlobalsUrlNoModRewriteRootDir()
  242. {
  243. Configure::write('App', [
  244. 'dir' => 'cake',
  245. 'webroot' => 'webroot',
  246. 'base' => false,
  247. 'baseUrl' => '/index.php',
  248. ]);
  249. $server = [
  250. 'DOCUMENT_ROOT' => '/Users/markstory/Sites/cake',
  251. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/index.php',
  252. 'PHP_SELF' => '/index.php/posts/add',
  253. 'REQUEST_URI' => '/index.php/posts/add',
  254. ];
  255. $res = ServerRequestFactory::fromGlobals($server);
  256. $this->assertSame('/webroot/', $res->getAttribute('webroot'));
  257. $this->assertSame('/index.php', $res->getAttribute('base'));
  258. $this->assertSame('/posts/add', $res->getUri()->getPath());
  259. }
  260. /**
  261. * @return void
  262. */
  263. public function testFormUrlEncodedBodyParsing()
  264. {
  265. $data = [
  266. 'Article' => ['title'],
  267. ];
  268. $request = ServerRequestFactory::fromGlobals([
  269. 'REQUEST_METHOD' => 'PUT',
  270. 'CONTENT_TYPE' => 'application/x-www-form-urlencoded; charset=UTF-8',
  271. 'CAKE_PHP_INPUT' => 'Article[]=title',
  272. ]);
  273. $this->assertEquals($data, $request->getData());
  274. $data = ['one' => 1, 'two' => 'three'];
  275. $request = ServerRequestFactory::fromGlobals([
  276. 'REQUEST_METHOD' => 'PUT',
  277. 'CONTENT_TYPE' => 'application/x-www-form-urlencoded; charset=UTF-8',
  278. 'CAKE_PHP_INPUT' => 'one=1&two=three',
  279. ]);
  280. $this->assertEquals($data, $request->getData());
  281. $request = ServerRequestFactory::fromGlobals([
  282. 'REQUEST_METHOD' => 'DELETE',
  283. 'CONTENT_TYPE' => 'application/x-www-form-urlencoded; charset=UTF-8',
  284. 'CAKE_PHP_INPUT' => 'Article[title]=Testing&action=update',
  285. ]);
  286. $expected = [
  287. 'Article' => ['title' => 'Testing'],
  288. 'action' => 'update',
  289. ];
  290. $this->assertEquals($expected, $request->getData());
  291. $data = [
  292. 'Article' => ['title'],
  293. 'Tag' => ['Tag' => [1, 2]],
  294. ];
  295. $request = ServerRequestFactory::fromGlobals([
  296. 'REQUEST_METHOD' => 'PATCH',
  297. 'CONTENT_TYPE' => 'application/x-www-form-urlencoded; charset=UTF-8',
  298. 'CAKE_PHP_INPUT' => 'Article[]=title&Tag[Tag][]=1&Tag[Tag][]=2',
  299. ]);
  300. $this->assertEquals($data, $request->getData());
  301. }
  302. /**
  303. * Test method overrides coming in from POST data.
  304. *
  305. * @return void
  306. */
  307. public function testMethodOverrides()
  308. {
  309. $post = ['_method' => 'POST'];
  310. $request = ServerRequestFactory::fromGlobals([], [], $post);
  311. $this->assertSame('POST', $request->getEnv('REQUEST_METHOD'));
  312. $post = ['_method' => 'DELETE'];
  313. $request = ServerRequestFactory::fromGlobals([], [], $post);
  314. $this->assertSame('DELETE', $request->getEnv('REQUEST_METHOD'));
  315. $request = ServerRequestFactory::fromGlobals(['HTTP_X_HTTP_METHOD_OVERRIDE' => 'PUT']);
  316. $this->assertSame('PUT', $request->getEnv('REQUEST_METHOD'));
  317. $request = ServerRequestFactory::fromGlobals(
  318. ['REQUEST_METHOD' => 'POST'],
  319. [],
  320. ['_method' => 'PUT']
  321. );
  322. $this->assertSame('PUT', $request->getEnv('REQUEST_METHOD'));
  323. $this->assertSame('POST', $request->getEnv('ORIGINAL_REQUEST_METHOD'));
  324. }
  325. /**
  326. * Test getServerParams
  327. *
  328. * @return void
  329. */
  330. public function testGetServerParams()
  331. {
  332. $vars = [
  333. 'REQUEST_METHOD' => 'PUT',
  334. 'HTTPS' => 'on',
  335. ];
  336. $request = ServerRequestFactory::fromGlobals($vars);
  337. $expected = $vars + [
  338. 'CONTENT_TYPE' => null,
  339. 'HTTP_CONTENT_TYPE' => null,
  340. 'ORIGINAL_REQUEST_METHOD' => 'PUT',
  341. ];
  342. $this->assertSame($expected, $request->getServerParams());
  343. }
  344. /**
  345. * Tests that overriding the method to GET will clean all request
  346. * data, to better simulate a GET request.
  347. *
  348. * @return void
  349. */
  350. public function testMethodOverrideEmptyParsedBody()
  351. {
  352. $body = ['_method' => 'GET', 'foo' => 'bar'];
  353. $request = ServerRequestFactory::fromGlobals(
  354. ['REQUEST_METHOD' => 'POST'],
  355. [],
  356. $body
  357. );
  358. $this->assertEmpty($request->getParsedBody());
  359. $request = ServerRequestFactory::fromGlobals(
  360. [
  361. 'REQUEST_METHOD' => 'POST',
  362. 'HTTP_X_HTTP_METHOD_OVERRIDE' => 'GET',
  363. ],
  364. [],
  365. ['foo' => 'bar']
  366. );
  367. $this->assertEmpty($request->getParsedBody());
  368. }
  369. /**
  370. * Tests the default file upload merging behavior.
  371. *
  372. * @return void
  373. */
  374. public function testFromGlobalsWithFilesAsObjectsDefault()
  375. {
  376. $this->assertNull(Configure::read('App.uploadedFilesAsObjects'));
  377. $files = [
  378. 'file' => [
  379. 'name' => 'file.txt',
  380. 'type' => 'text/plain',
  381. 'tmp_name' => __FILE__,
  382. 'error' => 0,
  383. 'size' => 1234,
  384. ],
  385. ];
  386. $request = ServerRequestFactory::fromGlobals(null, null, null, null, $files);
  387. /** @var \Laminas\Diactoros\UploadedFile $expected */
  388. $expected = $request->getData('file');
  389. $this->assertSame($files['file']['size'], $expected->getSize());
  390. $this->assertSame($files['file']['error'], $expected->getError());
  391. $this->assertSame($files['file']['name'], $expected->getClientFilename());
  392. $this->assertSame($files['file']['type'], $expected->getClientMediaType());
  393. }
  394. /**
  395. * Tests the "as arrays" file upload merging behavior.
  396. *
  397. * @return void
  398. */
  399. public function testFromGlobalsWithFilesAsObjectsDisabled()
  400. {
  401. Configure::write('App.uploadedFilesAsObjects', false);
  402. $files = [
  403. 'file' => [
  404. 'name' => 'file.txt',
  405. 'type' => 'text/plain',
  406. 'tmp_name' => __FILE__,
  407. 'error' => 0,
  408. 'size' => 1234,
  409. ],
  410. ];
  411. $request = ServerRequestFactory::fromGlobals(null, null, null, null, $files);
  412. $expected = [
  413. 'file' => [
  414. 'tmp_name' => __FILE__,
  415. 'error' => 0,
  416. 'name' => 'file.txt',
  417. 'type' => 'text/plain',
  418. 'size' => 1234,
  419. ],
  420. ];
  421. $this->assertEquals($expected, $request->getData());
  422. }
  423. /**
  424. * Tests the "as objects" file upload merging behavior.
  425. *
  426. * @return void
  427. */
  428. public function testFromGlobalsWithFilesAsObjectsEnabled()
  429. {
  430. Configure::write('App.uploadedFilesAsObjects', true);
  431. $files = [
  432. 'file' => [
  433. 'name' => 'file.txt',
  434. 'type' => 'text/plain',
  435. 'tmp_name' => __FILE__,
  436. 'error' => 0,
  437. 'size' => 1234,
  438. ],
  439. ];
  440. $request = ServerRequestFactory::fromGlobals(null, null, null, null, $files);
  441. $expected = [
  442. 'file' => new UploadedFile(
  443. __FILE__,
  444. 1234,
  445. 0,
  446. 'file.txt',
  447. 'text/plain'
  448. ),
  449. ];
  450. $this->assertEquals($expected, $request->getData());
  451. }
  452. /**
  453. * Test processing files with `file` field names.
  454. *
  455. * @return void
  456. */
  457. public function testFilesNested()
  458. {
  459. $files = [
  460. 'image_main' => [
  461. 'name' => ['file' => 'born on.txt'],
  462. 'type' => ['file' => 'text/plain'],
  463. 'tmp_name' => ['file' => __FILE__],
  464. 'error' => ['file' => 0],
  465. 'size' => ['file' => 17178],
  466. ],
  467. 0 => [
  468. 'name' => ['image' => 'scratch.text'],
  469. 'type' => ['image' => 'text/plain'],
  470. 'tmp_name' => ['image' => __FILE__],
  471. 'error' => ['image' => 0],
  472. 'size' => ['image' => 1490],
  473. ],
  474. 'pictures' => [
  475. 'name' => [
  476. 0 => ['file' => 'a-file.png'],
  477. 1 => ['file' => 'a-moose.png'],
  478. ],
  479. 'type' => [
  480. 0 => ['file' => 'image/png'],
  481. 1 => ['file' => 'image/jpg'],
  482. ],
  483. 'tmp_name' => [
  484. 0 => ['file' => __FILE__],
  485. 1 => ['file' => __FILE__],
  486. ],
  487. 'error' => [
  488. 0 => ['file' => 0],
  489. 1 => ['file' => 0],
  490. ],
  491. 'size' => [
  492. 0 => ['file' => 17188],
  493. 1 => ['file' => 2010],
  494. ],
  495. ],
  496. ];
  497. $post = [
  498. 'pictures' => [
  499. 0 => ['name' => 'A cat'],
  500. 1 => ['name' => 'A moose'],
  501. ],
  502. 0 => [
  503. 'name' => 'A dog',
  504. ],
  505. ];
  506. $request = ServerRequestFactory::fromGlobals(null, null, $post, null, $files);
  507. $expected = [
  508. 'image_main' => [
  509. 'file' => new UploadedFile(
  510. __FILE__,
  511. 17178,
  512. 0,
  513. 'born on.txt',
  514. 'text/plain'
  515. ),
  516. ],
  517. 'pictures' => [
  518. 0 => [
  519. 'name' => 'A cat',
  520. 'file' => new UploadedFile(
  521. __FILE__,
  522. 17188,
  523. 0,
  524. 'a-file.png',
  525. 'image/png'
  526. ),
  527. ],
  528. 1 => [
  529. 'name' => 'A moose',
  530. 'file' => new UploadedFile(
  531. __FILE__,
  532. 2010,
  533. 0,
  534. 'a-moose.png',
  535. 'image/jpg'
  536. ),
  537. ],
  538. ],
  539. 0 => [
  540. 'name' => 'A dog',
  541. 'image' => new UploadedFile(
  542. __FILE__,
  543. 1490,
  544. 0,
  545. 'scratch.text',
  546. 'text/plain'
  547. ),
  548. ],
  549. ];
  550. $this->assertEquals($expected, $request->getData());
  551. $uploads = $request->getUploadedFiles();
  552. $this->assertCount(3, $uploads);
  553. $this->assertArrayHasKey(0, $uploads);
  554. $this->assertSame('scratch.text', $uploads[0]['image']->getClientFilename());
  555. $this->assertArrayHasKey('pictures', $uploads);
  556. $this->assertSame('a-file.png', $uploads['pictures'][0]['file']->getClientFilename());
  557. $this->assertSame('a-moose.png', $uploads['pictures'][1]['file']->getClientFilename());
  558. $this->assertArrayHasKey('image_main', $uploads);
  559. $this->assertSame('born on.txt', $uploads['image_main']['file']->getClientFilename());
  560. }
  561. /**
  562. * Test processing a file input with no .'s in it.
  563. *
  564. * @return void
  565. */
  566. public function testFilesFlat()
  567. {
  568. $files = [
  569. 'birth_cert' => [
  570. 'name' => 'born on.txt',
  571. 'type' => 'application/octet-stream',
  572. 'tmp_name' => __FILE__,
  573. 'error' => 0,
  574. 'size' => 123,
  575. ],
  576. ];
  577. Configure::write('App.uploadedFilesAsObjects', false);
  578. $request = ServerRequestFactory::fromGlobals([], [], [], [], $files);
  579. $this->assertEquals($files, $request->getData());
  580. Configure::write('App.uploadedFilesAsObjects', true);
  581. $uploads = $request->getUploadedFiles();
  582. $this->assertCount(1, $uploads);
  583. $this->assertArrayHasKey('birth_cert', $uploads);
  584. $this->assertSame('born on.txt', $uploads['birth_cert']->getClientFilename());
  585. $this->assertSame(0, $uploads['birth_cert']->getError());
  586. $this->assertSame('application/octet-stream', $uploads['birth_cert']->getClientMediaType());
  587. $this->assertEquals(123, $uploads['birth_cert']->getSize());
  588. }
  589. /**
  590. * Test that files in the 0th index work.
  591. *
  592. * @return void
  593. */
  594. public function testFilesZeroithIndex()
  595. {
  596. $files = [
  597. 0 => [
  598. 'name' => 'cake_sqlserver_patch.patch',
  599. 'type' => 'text/plain',
  600. 'tmp_name' => __FILE__,
  601. 'error' => 0,
  602. 'size' => 6271,
  603. ],
  604. ];
  605. Configure::write('App.uploadedFilesAsObjects', false);
  606. $request = ServerRequestFactory::fromGlobals([], [], [], [], $files);
  607. $this->assertEquals($files, $request->getData());
  608. Configure::write('App.uploadedFilesAsObjects', true);
  609. $uploads = $request->getUploadedFiles();
  610. $this->assertCount(1, $uploads);
  611. $this->assertEquals($files[0]['name'], $uploads[0]->getClientFilename());
  612. }
  613. /**
  614. * Tests that file uploads are merged into the post data as objects instead of as arrays.
  615. *
  616. * @return void
  617. */
  618. public function testFilesAsObjectsInRequestData()
  619. {
  620. $files = [
  621. 'flat' => [
  622. 'name' => 'flat.txt',
  623. 'type' => 'text/plain',
  624. 'tmp_name' => __FILE__,
  625. 'error' => 0,
  626. 'size' => 1,
  627. ],
  628. 'nested' => [
  629. 'name' => ['file' => 'nested.txt'],
  630. 'type' => ['file' => 'text/plain'],
  631. 'tmp_name' => ['file' => __FILE__],
  632. 'error' => ['file' => 0],
  633. 'size' => ['file' => 12],
  634. ],
  635. 0 => [
  636. 'name' => 'numeric.txt',
  637. 'type' => 'text/plain',
  638. 'tmp_name' => __FILE__,
  639. 'error' => 0,
  640. 'size' => 123,
  641. ],
  642. 1 => [
  643. 'name' => ['file' => 'numeric-nested.txt'],
  644. 'type' => ['file' => 'text/plain'],
  645. 'tmp_name' => ['file' => __FILE__],
  646. 'error' => ['file' => 0],
  647. 'size' => ['file' => 1234],
  648. ],
  649. 'deep' => [
  650. 'name' => [
  651. 0 => ['file' => 'deep-1.txt'],
  652. 1 => ['file' => 'deep-2.txt'],
  653. ],
  654. 'type' => [
  655. 0 => ['file' => 'text/plain'],
  656. 1 => ['file' => 'text/plain'],
  657. ],
  658. 'tmp_name' => [
  659. 0 => ['file' => __FILE__],
  660. 1 => ['file' => __FILE__],
  661. ],
  662. 'error' => [
  663. 0 => ['file' => 0],
  664. 1 => ['file' => 0],
  665. ],
  666. 'size' => [
  667. 0 => ['file' => 12345],
  668. 1 => ['file' => 123456],
  669. ],
  670. ],
  671. ];
  672. $post = [
  673. 'flat' => ['existing'],
  674. 'nested' => [
  675. 'name' => 'nested',
  676. 'file' => ['existing'],
  677. ],
  678. 'deep' => [
  679. 0 => [
  680. 'name' => 'deep 1',
  681. 'file' => ['existing'],
  682. ],
  683. 1 => [
  684. 'name' => 'deep 2',
  685. ],
  686. ],
  687. 1 => [
  688. 'name' => 'numeric nested',
  689. ],
  690. ];
  691. $expected = [
  692. 'flat' => new UploadedFile(
  693. __FILE__,
  694. 1,
  695. 0,
  696. 'flat.txt',
  697. 'text/plain'
  698. ),
  699. 'nested' => [
  700. 'name' => 'nested',
  701. 'file' => new UploadedFile(
  702. __FILE__,
  703. 12,
  704. 0,
  705. 'nested.txt',
  706. 'text/plain'
  707. ),
  708. ],
  709. 'deep' => [
  710. 0 => [
  711. 'name' => 'deep 1',
  712. 'file' => new UploadedFile(
  713. __FILE__,
  714. 12345,
  715. 0,
  716. 'deep-1.txt',
  717. 'text/plain'
  718. ),
  719. ],
  720. 1 => [
  721. 'name' => 'deep 2',
  722. 'file' => new UploadedFile(
  723. __FILE__,
  724. 123456,
  725. 0,
  726. 'deep-2.txt',
  727. 'text/plain'
  728. ),
  729. ],
  730. ],
  731. 0 => new UploadedFile(
  732. __FILE__,
  733. 123,
  734. 0,
  735. 'numeric.txt',
  736. 'text/plain'
  737. ),
  738. 1 => [
  739. 'name' => 'numeric nested',
  740. 'file' => new UploadedFile(
  741. __FILE__,
  742. 1234,
  743. 0,
  744. 'numeric-nested.txt',
  745. 'text/plain'
  746. ),
  747. ],
  748. ];
  749. $request = ServerRequestFactory::fromGlobals([], [], $post, [], $files);
  750. $this->assertEquals($expected, $request->getData());
  751. }
  752. /**
  753. * Test passing invalid files list structure.
  754. *
  755. * @return void
  756. */
  757. public function testFilesWithInvalidStructure()
  758. {
  759. $this->expectException(InvalidArgumentException::class);
  760. $this->expectExceptionMessage('Invalid value in files specification');
  761. ServerRequestFactory::fromGlobals([], [], [], [], [
  762. [
  763. 'invalid' => [
  764. 'data',
  765. ],
  766. ],
  767. ]);
  768. }
  769. }