ServerRequestFactoryTest.php 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  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\ServerRequest;
  19. use Cake\Http\ServerRequestFactory;
  20. use Cake\Http\Session;
  21. use Cake\TestSuite\TestCase;
  22. use Laminas\Diactoros\Exception\InvalidArgumentException;
  23. use Laminas\Diactoros\UploadedFile;
  24. use Psr\Http\Message\UploadedFileInterface;
  25. /**
  26. * Test case for the server factory.
  27. */
  28. class ServerRequestFactoryTest extends TestCase
  29. {
  30. /**
  31. * Test fromGlobals reads super globals
  32. */
  33. public function testFromGlobalsSuperGlobals(): void
  34. {
  35. $post = [
  36. 'title' => 'custom',
  37. ];
  38. $files = [
  39. 'image' => [
  40. 'tmp_name' => __FILE__,
  41. 'error' => 0,
  42. 'name' => 'cats.png',
  43. 'type' => 'image/png',
  44. 'size' => 2112,
  45. ],
  46. ];
  47. $cookies = ['key' => 'value'];
  48. $query = ['query' => 'string'];
  49. $res = ServerRequestFactory::fromGlobals([], $query, $post, $cookies, $files);
  50. $this->assertSame($cookies['key'], $res->getCookie('key'));
  51. $this->assertSame($query['query'], $res->getQuery('query'));
  52. $this->assertArrayHasKey('title', $res->getData());
  53. $this->assertArrayHasKey('image', $res->getData());
  54. $this->assertCount(1, $res->getUploadedFiles());
  55. /** @var \Psr\Http\Message\UploadedFileInterface $expected */
  56. $expected = $res->getData('image');
  57. $this->assertInstanceOf(UploadedFileInterface::class, $expected);
  58. $this->assertSame($files['image']['size'], $expected->getSize());
  59. $this->assertSame($files['image']['error'], $expected->getError());
  60. $this->assertSame($files['image']['name'], $expected->getClientFilename());
  61. $this->assertSame($files['image']['type'], $expected->getClientMediaType());
  62. }
  63. public function testFromGlobalsUriScheme(): void
  64. {
  65. $server = [
  66. 'DOCUMENT_ROOT' => '/cake/repo/webroot',
  67. 'PHP_SELF' => '/index.php',
  68. 'REQUEST_URI' => '/posts/add',
  69. 'HTTP_X_FORWARDED_PROTO' => 'https',
  70. ];
  71. $request = ServerRequestFactory::fromGlobals($server);
  72. $this->assertSame('http', $request->scheme());
  73. $this->assertSame('http', $request->getUri()->getScheme());
  74. $request->setTrustedProxies([]);
  75. // Yeah even setting an empty list of proxies does the trick.
  76. $this->assertSame('https', $request->scheme());
  77. $this->assertSame('https', $request->getUri()->getScheme());
  78. }
  79. /**
  80. * Test fromGlobals includes the session
  81. *
  82. * @preserveGlobalState disabled
  83. * @runInSeparateProcess
  84. */
  85. public function testFromGlobalsUrlSession(): void
  86. {
  87. Configure::write('App.base', '/basedir');
  88. $server = [
  89. 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
  90. 'PHP_SELF' => '/index.php',
  91. 'REQUEST_URI' => '/posts/add',
  92. ];
  93. $res = ServerRequestFactory::fromGlobals($server);
  94. $session = $res->getAttribute('session');
  95. $this->assertInstanceOf(Session::class, $session);
  96. $this->assertSame('/basedir/', ini_get('session.cookie_path'), 'Needs trailing / for cookie to work');
  97. }
  98. /**
  99. * Test fromGlobals with App.base defined.
  100. */
  101. public function testFromGlobalsUrlBaseDefined(): void
  102. {
  103. Configure::write('App.base', 'basedir');
  104. $server = [
  105. 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
  106. 'PHP_SELF' => '/index.php',
  107. 'REQUEST_URI' => '/posts/add',
  108. ];
  109. $res = ServerRequestFactory::fromGlobals($server);
  110. $this->assertSame('basedir', $res->getAttribute('base'));
  111. $this->assertSame('basedir/', $res->getAttribute('webroot'));
  112. $this->assertSame('/posts/add', $res->getUri()->getPath());
  113. }
  114. /**
  115. * Test fromGlobals with mod-rewrite server configuration.
  116. */
  117. public function testFromGlobalsUrlModRewrite(): void
  118. {
  119. Configure::write('App.baseUrl', false);
  120. $server = [
  121. 'DOCUMENT_ROOT' => '/cake/repo/branches',
  122. 'PHP_SELF' => '/urlencode me/webroot/index.php',
  123. 'REQUEST_URI' => '/posts/view/1',
  124. ];
  125. $res = ServerRequestFactory::fromGlobals($server);
  126. $this->assertSame('/urlencode%20me', $res->getAttribute('base'));
  127. $this->assertSame('/urlencode%20me/', $res->getAttribute('webroot'));
  128. $this->assertSame('/posts/view/1', $res->getUri()->getPath());
  129. $request = ServerRequestFactory::fromGlobals([
  130. 'DOCUMENT_ROOT' => '/cake/repo/branches',
  131. 'PHP_SELF' => '/1.2.x.x/webroot/index.php',
  132. 'PATH_INFO' => '/posts/view/1',
  133. ]);
  134. $this->assertSame('/1.2.x.x', $request->getAttribute('base'));
  135. $this->assertSame('/1.2.x.x/', $request->getAttribute('webroot'));
  136. $this->assertSame('/posts/view/1', $request->getRequestTarget());
  137. $request = ServerRequestFactory::fromGlobals([
  138. 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/test/',
  139. 'PHP_SELF' => '/webroot/index.php',
  140. ]);
  141. $this->assertSame('', $request->getAttribute('base'));
  142. $this->assertSame('/', $request->getAttribute('webroot'));
  143. $request = ServerRequestFactory::fromGlobals([
  144. 'DOCUMENT_ROOT' => '/some/apps/where',
  145. 'PHP_SELF' => '/webroot/index.php',
  146. ]);
  147. $this->assertSame('', $request->getAttribute('base'));
  148. $this->assertSame('/', $request->getAttribute('webroot'));
  149. Configure::write('App.dir', 'auth');
  150. $request = ServerRequestFactory::fromGlobals([
  151. 'DOCUMENT_ROOT' => '/cake/repo/branches',
  152. 'PHP_SELF' => '/demos/webroot/index.php',
  153. ]);
  154. $this->assertSame('/demos', $request->getAttribute('base'));
  155. $this->assertSame('/demos/', $request->getAttribute('webroot'));
  156. Configure::write('App.dir', 'code');
  157. $request = ServerRequestFactory::fromGlobals([
  158. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  159. 'PHP_SELF' => '/clients/PewterReport/webroot/index.php',
  160. ]);
  161. $this->assertSame('/clients/PewterReport', $request->getAttribute('base'));
  162. $this->assertSame('/clients/PewterReport/', $request->getAttribute('webroot'));
  163. }
  164. /**
  165. * Test baseUrl with ModRewrite alias
  166. */
  167. public function testBaseUrlwithModRewriteAlias(): void
  168. {
  169. Configure::write('App.base', '/control');
  170. $request = ServerRequestFactory::fromGlobals([
  171. 'DOCUMENT_ROOT' => '/home/aplusnur/public_html',
  172. 'PHP_SELF' => '/control/index.php',
  173. ]);
  174. $this->assertSame('/control', $request->getAttribute('base'));
  175. $this->assertSame('/control/', $request->getAttribute('webroot'));
  176. Configure::write('App.base', false);
  177. Configure::write('App.dir', 'affiliate');
  178. Configure::write('App.webroot', 'newaffiliate');
  179. $request = ServerRequestFactory::fromGlobals([
  180. 'DOCUMENT_ROOT' => '/var/www/abtravaff/html',
  181. 'PHP_SELF' => '/newaffiliate/index.php',
  182. ]);
  183. $this->assertSame('', $request->getAttribute('base'));
  184. $this->assertSame('/', $request->getAttribute('webroot'));
  185. }
  186. /**
  187. * Test base, webroot, URL and here parsing when there is URL rewriting but
  188. * CakePHP gets called with index.php in URL nonetheless.
  189. *
  190. * Tests uri with
  191. *
  192. * - index.php/
  193. * - index.php/
  194. * - index.php/apples/
  195. * - index.php/bananas/eat/tasty_banana
  196. */
  197. public function testBaseUrlWithModRewriteAndIndexPhp(): void
  198. {
  199. $request = ServerRequestFactory::fromGlobals([
  200. 'DOCUMENT_ROOT' => '/cakephp/webroot/index.php',
  201. 'PHP_SELF' => '/cakephp/webroot/index.php',
  202. ]);
  203. $this->assertSame('/cakephp', $request->getAttribute('base'));
  204. $this->assertSame('/cakephp/', $request->getAttribute('webroot'));
  205. $this->assertSame('/', $request->getRequestTarget());
  206. $request = ServerRequestFactory::fromGlobals([
  207. 'REQUEST_URI' => '/cakephp/webroot/index.php/',
  208. 'PHP_SELF' => '/cakephp/webroot/index.php/',
  209. 'PATH_INFO' => '/',
  210. ]);
  211. $this->assertSame('/cakephp', $request->getAttribute('base'));
  212. $this->assertSame('/cakephp/', $request->getAttribute('webroot'));
  213. $this->assertSame('/', $request->getRequestTarget());
  214. $request = ServerRequestFactory::fromGlobals([
  215. 'REQUEST_URI' => '/cakephp/webroot/index.php/apples',
  216. 'PHP_SELF' => '/cakephp/webroot/index.php/apples',
  217. 'PATH_INFO' => '/apples',
  218. ]);
  219. $this->assertSame('/cakephp', $request->getAttribute('base'));
  220. $this->assertSame('/cakephp/', $request->getAttribute('webroot'));
  221. $this->assertSame('/apples', $request->getRequestTarget());
  222. $request = ServerRequestFactory::fromGlobals([
  223. 'REQUEST_URI' => '/cakephp/webroot/index.php/melons/share/',
  224. 'PHP_SELF' => '/cakephp/webroot/index.php/melons/share/',
  225. 'PATH_INFO' => '/melons/share/',
  226. ]);
  227. $this->assertSame('/cakephp', $request->getAttribute('base'));
  228. $this->assertSame('/cakephp/', $request->getAttribute('webroot'));
  229. $this->assertSame('/melons/share/', $request->getRequestTarget());
  230. $request = ServerRequestFactory::fromGlobals([
  231. 'REQUEST_URI' => '/cakephp/webroot/index.php/bananas/eat/tasty_banana',
  232. 'PHP_SELF' => '/cakephp/webroot/index.php/bananas/eat/tasty_banana',
  233. 'PATH_INFO' => '/bananas/eat/tasty_banana',
  234. ]);
  235. $this->assertSame('/cakephp', $request->getAttribute('base'));
  236. $this->assertSame('/cakephp/', $request->getAttribute('webroot'));
  237. $this->assertSame('/bananas/eat/tasty_banana', $request->getRequestTarget());
  238. }
  239. /**
  240. * Test that even if mod_rewrite is on, and the url contains index.php
  241. * and there are numerous //s that the base/webroot is calculated correctly.
  242. */
  243. public function testBaseUrlWithModRewriteAndExtraSlashes(): void
  244. {
  245. $request = ServerRequestFactory::fromGlobals([
  246. 'REQUEST_URI' => '/cakephp/webroot///index.php/bananas/eat',
  247. 'PHP_SELF' => '/cakephp/webroot///index.php/bananas/eat',
  248. 'PATH_INFO' => '/bananas/eat',
  249. ]);
  250. $this->assertSame('/cakephp', $request->getAttribute('base'));
  251. $this->assertSame('/cakephp/', $request->getAttribute('webroot'));
  252. $this->assertSame('/bananas/eat', $request->getRequestTarget());
  253. }
  254. /**
  255. * Test fromGlobals with mod-rewrite in the root dir.
  256. */
  257. public function testFromGlobalsUrlModRewriteRootDir(): void
  258. {
  259. $server = [
  260. 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
  261. 'PHP_SELF' => '/index.php',
  262. 'REQUEST_URI' => '/posts/add',
  263. ];
  264. $res = ServerRequestFactory::fromGlobals($server);
  265. $this->assertSame('', $res->getAttribute('base'));
  266. $this->assertSame('/', $res->getAttribute('webroot'));
  267. $this->assertSame('/posts/add', $res->getUri()->getPath());
  268. }
  269. /**
  270. * Test fromGlobals with App.baseUrl defined implying no
  271. * mod-rewrite and no virtual path.
  272. */
  273. public function testFromGlobalsUrlNoModRewriteWebrootDir(): void
  274. {
  275. Configure::write('App', [
  276. 'dir' => 'app',
  277. 'webroot' => 'www',
  278. 'base' => false,
  279. 'baseUrl' => '/cake/index.php',
  280. ]);
  281. $server = [
  282. 'DOCUMENT_ROOT' => '/Users/markstory/Sites',
  283. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/www/index.php',
  284. 'PHP_SELF' => '/cake/www/index.php/posts/index',
  285. 'REQUEST_URI' => '/cake/www/index.php',
  286. ];
  287. $res = ServerRequestFactory::fromGlobals($server);
  288. $this->assertSame('/cake/www/', $res->getAttribute('webroot'));
  289. $this->assertSame('/cake/index.php', $res->getAttribute('base'));
  290. $this->assertSame('/', $res->getUri()->getPath());
  291. }
  292. /**
  293. * Test fromGlobals with App.baseUrl defined implying no
  294. * mod-rewrite
  295. */
  296. public function testFromGlobalsUrlNoModRewrite(): void
  297. {
  298. Configure::write('App', [
  299. 'dir' => 'app',
  300. 'webroot' => 'webroot',
  301. 'base' => false,
  302. 'baseUrl' => '/cake/index.php',
  303. ]);
  304. $server = [
  305. 'DOCUMENT_ROOT' => '/Users/markstory/Sites',
  306. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/index.php',
  307. 'PHP_SELF' => '/cake/index.php/posts/index',
  308. 'REQUEST_URI' => '/cake/index.php/posts/index',
  309. ];
  310. $res = ServerRequestFactory::fromGlobals($server);
  311. $this->assertSame('/cake/webroot/', $res->getAttribute('webroot'));
  312. $this->assertSame('/cake/index.php', $res->getAttribute('base'));
  313. $this->assertSame('/posts/index', $res->getUri()->getPath());
  314. }
  315. /**
  316. * Test fromGlobals with App.baseUrl defined implying no
  317. * mod-rewrite in the root dir.
  318. */
  319. public function testFromGlobalsUrlNoModRewriteRootDir(): void
  320. {
  321. Configure::write('App', [
  322. 'dir' => 'cake',
  323. 'webroot' => 'webroot',
  324. 'base' => false,
  325. 'baseUrl' => '/index.php',
  326. ]);
  327. $server = [
  328. 'DOCUMENT_ROOT' => '/Users/markstory/Sites/cake',
  329. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/index.php',
  330. 'PHP_SELF' => '/index.php/posts/add',
  331. 'REQUEST_URI' => '/index.php/posts/add',
  332. ];
  333. $res = ServerRequestFactory::fromGlobals($server);
  334. $this->assertSame('/webroot/', $res->getAttribute('webroot'));
  335. $this->assertSame('/index.php', $res->getAttribute('base'));
  336. $this->assertSame('/posts/add', $res->getUri()->getPath());
  337. }
  338. /**
  339. * Check that a sub-directory containing app|webroot doesn't get mishandled when re-writing is off.
  340. */
  341. public function testBaseUrlWithAppAndWebrootInDirname(): void
  342. {
  343. Configure::write('App.baseUrl', '/approval/index.php');
  344. $request = ServerRequestFactory::fromGlobals([
  345. 'DOCUMENT_ROOT' => '/Users/markstory/Sites/',
  346. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/approval/index.php',
  347. ]);
  348. $this->assertSame('/approval/index.php', $request->getAttribute('base'));
  349. $this->assertSame('/approval/webroot/', $request->getAttribute('webroot'));
  350. Configure::write('App.baseUrl', '/webrootable/index.php');
  351. $request = ServerRequestFactory::fromGlobals([
  352. 'DOCUMENT_ROOT' => '/Users/markstory/Sites/',
  353. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/webrootable/index.php',
  354. ]);
  355. $this->assertSame('/webrootable/index.php', $request->getAttribute('base'));
  356. $this->assertSame('/webrootable/webroot/', $request->getAttribute('webroot'));
  357. }
  358. /**
  359. * Test baseUrl and webroot with baseUrl
  360. */
  361. public function testBaseUrlAndWebrootWithBaseUrl(): void
  362. {
  363. Configure::write('App.dir', 'App');
  364. Configure::write('App.baseUrl', '/App/webroot/index.php');
  365. $request = ServerRequestFactory::fromGlobals();
  366. $this->assertSame('/App/webroot/index.php', $request->getAttribute('base'));
  367. $this->assertSame('/App/webroot/', $request->getAttribute('webroot'));
  368. Configure::write('App.baseUrl', '/App/webroot/test.php');
  369. $request = ServerRequestFactory::fromGlobals();
  370. $this->assertSame('/App/webroot/test.php', $request->getAttribute('base'));
  371. $this->assertSame('/App/webroot/', $request->getAttribute('webroot'));
  372. Configure::write('App.baseUrl', '/App/index.php');
  373. $request = ServerRequestFactory::fromGlobals();
  374. $this->assertSame('/App/index.php', $request->getAttribute('base'));
  375. $this->assertSame('/App/webroot/', $request->getAttribute('webroot'));
  376. Configure::write('App.baseUrl', '/CakeBB/App/webroot/index.php');
  377. $request = ServerRequestFactory::fromGlobals();
  378. $this->assertSame('/CakeBB/App/webroot/index.php', $request->getAttribute('base'));
  379. $this->assertSame('/CakeBB/App/webroot/', $request->getAttribute('webroot'));
  380. Configure::write('App.baseUrl', '/CakeBB/App/index.php');
  381. $request = ServerRequestFactory::fromGlobals();
  382. $this->assertSame('/CakeBB/App/index.php', $request->getAttribute('base'));
  383. $this->assertSame('/CakeBB/App/webroot/', $request->getAttribute('webroot'));
  384. Configure::write('App.baseUrl', '/CakeBB/index.php');
  385. $request = ServerRequestFactory::fromGlobals();
  386. $this->assertSame('/CakeBB/index.php', $request->getAttribute('base'));
  387. $this->assertSame('/CakeBB/webroot/', $request->getAttribute('webroot'));
  388. Configure::write('App.baseUrl', '/dbhauser/index.php');
  389. $request = ServerRequestFactory::fromGlobals([
  390. 'DOCUMENT_ROOT' => '/kunden/homepages/4/d181710652/htdocs/joomla',
  391. 'SCRIPT_FILENAME' => '/kunden/homepages/4/d181710652/htdocs/joomla/dbhauser/index.php',
  392. ]);
  393. $this->assertSame('/dbhauser/index.php', $request->getAttribute('base'));
  394. $this->assertSame('/dbhauser/webroot/', $request->getAttribute('webroot'));
  395. }
  396. /**
  397. * Test that a request with a . in the main GET parameter is filtered out.
  398. * PHP changes GET parameter keys containing dots to _.
  399. */
  400. public function testGetParamsWithDot(): void
  401. {
  402. $request = ServerRequestFactory::fromGlobals([
  403. 'PHP_SELF' => '/webroot/index.php',
  404. 'REQUEST_URI' => '/posts/index/add.add',
  405. ]);
  406. $this->assertSame('', $request->getAttribute('base'));
  407. $this->assertEquals([], $request->getQueryParams());
  408. $request = ServerRequestFactory::fromGlobals([
  409. 'PHP_SELF' => '/cake_dev/webroot/index.php',
  410. 'REQUEST_URI' => '/cake_dev/posts/index/add.add',
  411. ]);
  412. $this->assertSame('/cake_dev', $request->getAttribute('base'));
  413. $this->assertEquals([], $request->getQueryParams());
  414. }
  415. /**
  416. * Test that a request with urlencoded bits in the main GET parameter are filtered out.
  417. */
  418. public function testGetParamWithUrlencodedElement(): void
  419. {
  420. $request = ServerRequestFactory::fromGlobals([
  421. 'PHP_SELF' => '/webroot/index.php',
  422. 'REQUEST_URI' => '/posts/add/%E2%88%82%E2%88%82',
  423. ]);
  424. $this->assertSame('', $request->getAttribute('base'));
  425. $this->assertEquals([], $request->getQueryParams());
  426. $request = ServerRequestFactory::fromGlobals([
  427. 'PHP_SELF' => '/cake_dev/webroot/index.php',
  428. 'REQUEST_URI' => '/cake_dev/posts/add/%E2%88%82%E2%88%82',
  429. ]);
  430. $this->assertSame('/cake_dev', $request->getAttribute('base'));
  431. $this->assertEquals([], $request->getQueryParams());
  432. }
  433. /**
  434. * Generator for environment configurations
  435. *
  436. * @return array Environment array
  437. */
  438. public static function environmentGenerator(): array
  439. {
  440. return [
  441. [
  442. 'IIS - No rewrite base path',
  443. [
  444. 'App' => [
  445. 'base' => false,
  446. 'baseUrl' => '/index.php',
  447. 'dir' => 'TestApp',
  448. 'webroot' => 'webroot',
  449. ],
  450. 'SERVER' => [
  451. 'SCRIPT_NAME' => '/index.php',
  452. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  453. 'QUERY_STRING' => '',
  454. 'REQUEST_URI' => '/index.php',
  455. 'URL' => '/index.php',
  456. 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\index.php',
  457. 'ORIG_PATH_INFO' => '/index.php',
  458. 'PATH_INFO' => '',
  459. 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\index.php',
  460. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  461. 'PHP_SELF' => '/index.php',
  462. ],
  463. ],
  464. [
  465. 'base' => '/index.php',
  466. 'webroot' => '/webroot/',
  467. 'url' => '',
  468. ],
  469. ],
  470. [
  471. 'IIS - No rewrite with path, no PHP_SELF',
  472. [
  473. 'App' => [
  474. 'base' => false,
  475. 'baseUrl' => '/index.php?',
  476. 'dir' => 'TestApp',
  477. 'webroot' => 'webroot',
  478. ],
  479. 'SERVER' => [
  480. 'QUERY_STRING' => '/posts/add',
  481. 'REQUEST_URI' => '/index.php?/posts/add',
  482. 'PHP_SELF' => '',
  483. 'URL' => '/index.php?/posts/add',
  484. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  485. 'argv' => ['/posts/add'],
  486. 'argc' => 1,
  487. ],
  488. ],
  489. [
  490. 'url' => 'posts/add',
  491. 'base' => '/index.php?',
  492. 'webroot' => '/webroot/',
  493. ],
  494. ],
  495. [
  496. 'IIS - No rewrite sub dir 2',
  497. [
  498. 'App' => [
  499. 'base' => false,
  500. 'baseUrl' => '/site/index.php',
  501. 'dir' => 'TestApp',
  502. 'webroot' => 'webroot',
  503. ],
  504. 'SERVER' => [
  505. 'SCRIPT_NAME' => '/site/index.php',
  506. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  507. 'QUERY_STRING' => '',
  508. 'REQUEST_URI' => '/site/index.php',
  509. 'URL' => '/site/index.php',
  510. 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
  511. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  512. 'PHP_SELF' => '/site/index.php',
  513. 'argv' => [],
  514. 'argc' => 0,
  515. ],
  516. ],
  517. [
  518. 'url' => '',
  519. 'base' => '/site/index.php',
  520. 'webroot' => '/site/webroot/',
  521. ],
  522. ],
  523. [
  524. 'IIS - No rewrite sub dir 2 with path',
  525. [
  526. 'App' => [
  527. 'base' => false,
  528. 'baseUrl' => '/site/index.php',
  529. 'dir' => 'TestApp',
  530. 'webroot' => 'webroot',
  531. ],
  532. 'SERVER' => [
  533. 'SCRIPT_NAME' => '/site/index.php',
  534. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  535. 'QUERY_STRING' => '/posts/add',
  536. 'REQUEST_URI' => '/site/index.php/posts/add',
  537. 'URL' => '/site/index.php/posts/add',
  538. 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
  539. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  540. 'PHP_SELF' => '/site/index.php/posts/add',
  541. 'argv' => ['/posts/add'],
  542. 'argc' => 1,
  543. ],
  544. ],
  545. [
  546. 'url' => 'posts/add',
  547. 'base' => '/site/index.php',
  548. 'webroot' => '/site/webroot/',
  549. ],
  550. ],
  551. [
  552. 'Apache - No rewrite, document root set to webroot, requesting path',
  553. [
  554. 'App' => [
  555. 'base' => false,
  556. 'baseUrl' => '/index.php',
  557. 'dir' => 'TestApp',
  558. 'webroot' => 'webroot',
  559. ],
  560. 'SERVER' => [
  561. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/App/webroot',
  562. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/App/webroot/index.php',
  563. 'QUERY_STRING' => '',
  564. 'REQUEST_URI' => '/index.php/posts/index',
  565. 'SCRIPT_NAME' => '/index.php',
  566. 'PATH_INFO' => '/posts/index',
  567. 'PHP_SELF' => '/index.php/posts/index',
  568. ],
  569. ],
  570. [
  571. 'url' => 'posts/index',
  572. 'base' => '/index.php',
  573. 'webroot' => '/',
  574. ],
  575. ],
  576. [
  577. 'Apache - No rewrite, document root set to webroot, requesting root',
  578. [
  579. 'App' => [
  580. 'base' => false,
  581. 'baseUrl' => '/index.php',
  582. 'dir' => 'TestApp',
  583. 'webroot' => 'webroot',
  584. ],
  585. 'SERVER' => [
  586. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/App/webroot',
  587. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/App/webroot/index.php',
  588. 'QUERY_STRING' => '',
  589. 'REQUEST_URI' => '/index.php',
  590. 'SCRIPT_NAME' => '/index.php',
  591. 'PATH_INFO' => '',
  592. 'PHP_SELF' => '/index.php',
  593. ],
  594. ],
  595. [
  596. 'url' => '',
  597. 'base' => '/index.php',
  598. 'webroot' => '/',
  599. ],
  600. ],
  601. [
  602. 'Apache - No rewrite, document root set above top level cake dir, requesting path',
  603. [
  604. 'App' => [
  605. 'base' => false,
  606. 'baseUrl' => '/site/index.php',
  607. 'dir' => 'TestApp',
  608. 'webroot' => 'webroot',
  609. ],
  610. 'SERVER' => [
  611. 'SERVER_NAME' => 'localhost',
  612. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  613. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  614. 'REQUEST_URI' => '/site/index.php/posts/index',
  615. 'SCRIPT_NAME' => '/site/index.php',
  616. 'PATH_INFO' => '/posts/index',
  617. 'PHP_SELF' => '/site/index.php/posts/index',
  618. ],
  619. ],
  620. [
  621. 'url' => 'posts/index',
  622. 'base' => '/site/index.php',
  623. 'webroot' => '/site/webroot/',
  624. ],
  625. ],
  626. [
  627. 'Apache - No rewrite, document root set above top level cake dir, request root, no PATH_INFO',
  628. [
  629. 'App' => [
  630. 'base' => false,
  631. 'baseUrl' => '/site/index.php',
  632. 'dir' => 'TestApp',
  633. 'webroot' => 'webroot',
  634. ],
  635. 'SERVER' => [
  636. 'SERVER_NAME' => 'localhost',
  637. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  638. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  639. 'REQUEST_URI' => '/site/index.php/',
  640. 'SCRIPT_NAME' => '/site/index.php',
  641. 'PHP_SELF' => '/site/index.php/',
  642. ],
  643. ],
  644. [
  645. 'url' => '',
  646. 'base' => '/site/index.php',
  647. 'webroot' => '/site/webroot/',
  648. ],
  649. ],
  650. [
  651. 'Apache - No rewrite, document root set above top level cake dir, request path, with GET',
  652. [
  653. 'App' => [
  654. 'base' => false,
  655. 'baseUrl' => '/site/index.php',
  656. 'dir' => 'TestApp',
  657. 'webroot' => 'webroot',
  658. ],
  659. 'GET' => ['a' => 'b', 'c' => 'd'],
  660. 'SERVER' => [
  661. 'SERVER_NAME' => 'localhost',
  662. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  663. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  664. 'REQUEST_URI' => '/site/index.php/posts/index?a=b&c=d',
  665. 'SCRIPT_NAME' => '/site/index.php',
  666. 'PATH_INFO' => '/posts/index',
  667. 'PHP_SELF' => '/site/index.php/posts/index',
  668. 'QUERY_STRING' => 'a=b&c=d',
  669. ],
  670. ],
  671. [
  672. 'urlParams' => ['a' => 'b', 'c' => 'd'],
  673. 'url' => 'posts/index',
  674. 'base' => '/site/index.php',
  675. 'webroot' => '/site/webroot/',
  676. ],
  677. ],
  678. [
  679. 'Apache - w/rewrite, document root set above top level cake dir, request root, no PATH_INFO',
  680. [
  681. 'App' => [
  682. 'base' => false,
  683. 'baseUrl' => false,
  684. 'dir' => 'TestApp',
  685. 'webroot' => 'webroot',
  686. ],
  687. 'SERVER' => [
  688. 'SERVER_NAME' => 'localhost',
  689. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  690. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  691. 'REQUEST_URI' => '/site/',
  692. 'SCRIPT_NAME' => '/site/webroot/index.php',
  693. 'PHP_SELF' => '/site/webroot/index.php',
  694. ],
  695. ],
  696. [
  697. 'url' => '',
  698. 'base' => '/site',
  699. 'webroot' => '/site/',
  700. ],
  701. ],
  702. [
  703. 'Apache - w/rewrite, document root above top level cake dir, request root, no PATH_INFO/REQUEST_URI',
  704. [
  705. 'App' => [
  706. 'base' => false,
  707. 'baseUrl' => false,
  708. 'dir' => 'TestApp',
  709. 'webroot' => 'webroot',
  710. ],
  711. 'SERVER' => [
  712. 'SERVER_NAME' => 'localhost',
  713. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  714. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  715. 'SCRIPT_NAME' => '/site/webroot/index.php',
  716. 'PHP_SELF' => '/site/webroot/index.php',
  717. 'PATH_INFO' => null,
  718. 'REQUEST_URI' => null,
  719. ],
  720. ],
  721. [
  722. 'url' => '',
  723. 'base' => '/site',
  724. 'webroot' => '/site/',
  725. ],
  726. ],
  727. [
  728. 'Apache - w/rewrite, document root set to webroot, request root, no PATH_INFO/REQUEST_URI',
  729. [
  730. 'App' => [
  731. 'base' => false,
  732. 'baseUrl' => false,
  733. 'dir' => 'TestApp',
  734. 'webroot' => 'webroot',
  735. ],
  736. 'SERVER' => [
  737. 'SERVER_NAME' => 'localhost',
  738. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/webroot',
  739. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/webroot/index.php',
  740. 'SCRIPT_NAME' => '/index.php',
  741. 'PHP_SELF' => '/index.php',
  742. 'PATH_INFO' => null,
  743. 'REQUEST_URI' => null,
  744. ],
  745. ],
  746. [
  747. 'url' => '',
  748. 'base' => '',
  749. 'webroot' => '/',
  750. ],
  751. ],
  752. [
  753. 'Apache - w/rewrite, document root set above top level cake dir, request root, absolute REQUEST_URI',
  754. [
  755. 'App' => [
  756. 'base' => false,
  757. 'baseUrl' => false,
  758. 'dir' => 'TestApp',
  759. 'webroot' => 'webroot',
  760. ],
  761. 'SERVER' => [
  762. 'SERVER_NAME' => 'localhost',
  763. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  764. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  765. 'REQUEST_URI' => '/site/posts/index',
  766. 'SCRIPT_NAME' => '/site/webroot/index.php',
  767. 'PHP_SELF' => '/site/webroot/index.php',
  768. ],
  769. ],
  770. [
  771. 'url' => 'posts/index',
  772. 'base' => '/site',
  773. 'webroot' => '/site/',
  774. ],
  775. ],
  776. [
  777. 'Nginx - w/rewrite, document root set to webroot, request root, no PATH_INFO',
  778. [
  779. 'App' => [
  780. 'base' => false,
  781. 'baseUrl' => false,
  782. 'dir' => 'TestApp',
  783. 'webroot' => 'webroot',
  784. ],
  785. 'SERVER' => [
  786. 'SERVER_NAME' => 'localhost',
  787. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/webroot',
  788. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/webroot/index.php',
  789. 'SCRIPT_NAME' => '/index.php',
  790. 'PHP_SELF' => '/index.php',
  791. 'PATH_INFO' => null,
  792. 'REQUEST_URI' => '/posts/add',
  793. ],
  794. ],
  795. [
  796. 'url' => 'posts/add',
  797. 'base' => '',
  798. 'webroot' => '/',
  799. 'urlParams' => [],
  800. ],
  801. ],
  802. [
  803. 'Nginx - w/rewrite, document root set above top level cake dir, request root, no PATH_INFO, base parameter set',
  804. [
  805. 'App' => [
  806. 'base' => false,
  807. 'baseUrl' => false,
  808. 'dir' => 'app',
  809. 'webroot' => 'webroot',
  810. ],
  811. 'SERVER' => [
  812. 'SERVER_NAME' => 'localhost',
  813. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  814. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/App/webroot/index.php',
  815. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  816. 'PHP_SELF' => '/site/webroot/index.php',
  817. 'PATH_INFO' => null,
  818. 'REQUEST_URI' => '/site/posts/add',
  819. ],
  820. ],
  821. [
  822. 'url' => 'posts/add',
  823. 'base' => '/site',
  824. 'webroot' => '/site/',
  825. 'urlParams' => [],
  826. ],
  827. ],
  828. ];
  829. }
  830. /**
  831. * Test environment detection
  832. *
  833. * @dataProvider environmentGenerator
  834. * @param string $name
  835. * @param array $data
  836. * @param array $expected
  837. */
  838. public function testEnvironmentDetection($name, $data, $expected): void
  839. {
  840. if (isset($data['App'])) {
  841. Configure::write('App', $data['App']);
  842. }
  843. $request = ServerRequestFactory::fromGlobals(
  844. $data['SERVER'] ?? null,
  845. $data['GET'] ?? null
  846. );
  847. $uri = $request->getUri();
  848. $this->assertSame('/' . $expected['url'], $uri->getPath(), 'Uri->getPath() is incorrect');
  849. $this->assertEquals($expected['base'], $request->getAttribute('base'), 'base is incorrect');
  850. $this->assertEquals($expected['webroot'], $request->getAttribute('webroot'), 'webroot is incorrect');
  851. if (isset($expected['urlParams'])) {
  852. $this->assertEquals($expected['urlParams'], $request->getQueryParams(), 'GET param mismatch');
  853. }
  854. }
  855. public function testFormUrlEncodedBodyParsing(): void
  856. {
  857. $data = [
  858. 'Article' => ['title'],
  859. ];
  860. $request = ServerRequestFactory::fromGlobals([
  861. 'REQUEST_METHOD' => 'PUT',
  862. 'CONTENT_TYPE' => 'application/x-www-form-urlencoded; charset=UTF-8',
  863. 'CAKEPHP_INPUT' => 'Article[]=title',
  864. ]);
  865. $this->assertEquals($data, $request->getData());
  866. $data = ['one' => 1, 'two' => 'three'];
  867. $request = ServerRequestFactory::fromGlobals([
  868. 'REQUEST_METHOD' => 'PUT',
  869. 'CONTENT_TYPE' => 'application/x-www-form-urlencoded; charset=UTF-8',
  870. 'CAKEPHP_INPUT' => 'one=1&two=three',
  871. ]);
  872. $this->assertEquals($data, $request->getData());
  873. $request = ServerRequestFactory::fromGlobals([
  874. 'REQUEST_METHOD' => 'DELETE',
  875. 'CONTENT_TYPE' => 'application/x-www-form-urlencoded; charset=UTF-8',
  876. 'CAKEPHP_INPUT' => 'Article[title]=Testing&action=update',
  877. ]);
  878. $expected = [
  879. 'Article' => ['title' => 'Testing'],
  880. 'action' => 'update',
  881. ];
  882. $this->assertEquals($expected, $request->getData());
  883. $data = [
  884. 'Article' => ['title'],
  885. 'Tag' => ['Tag' => [1, 2]],
  886. ];
  887. $request = ServerRequestFactory::fromGlobals([
  888. 'REQUEST_METHOD' => 'PATCH',
  889. 'CONTENT_TYPE' => 'application/x-www-form-urlencoded; charset=UTF-8',
  890. 'CAKEPHP_INPUT' => 'Article[]=title&Tag[Tag][]=1&Tag[Tag][]=2',
  891. ]);
  892. $this->assertEquals($data, $request->getData());
  893. }
  894. /**
  895. * Test method overrides coming in from POST data.
  896. */
  897. public function testMethodOverrides(): void
  898. {
  899. $post = ['_method' => 'POST'];
  900. $request = ServerRequestFactory::fromGlobals([], [], $post);
  901. $this->assertSame('POST', $request->getEnv('REQUEST_METHOD'));
  902. $post = ['_method' => 'DELETE'];
  903. $request = ServerRequestFactory::fromGlobals([], [], $post);
  904. $this->assertSame('DELETE', $request->getEnv('REQUEST_METHOD'));
  905. $request = ServerRequestFactory::fromGlobals(['HTTP_X_HTTP_METHOD_OVERRIDE' => 'PUT']);
  906. $this->assertSame('PUT', $request->getEnv('REQUEST_METHOD'));
  907. $request = ServerRequestFactory::fromGlobals(
  908. ['REQUEST_METHOD' => 'POST'],
  909. [],
  910. ['_method' => 'PUT']
  911. );
  912. $this->assertSame('PUT', $request->getEnv('REQUEST_METHOD'));
  913. $this->assertSame('POST', $request->getEnv('ORIGINAL_REQUEST_METHOD'));
  914. }
  915. /**
  916. * Test getServerParams
  917. */
  918. public function testGetServerParams(): void
  919. {
  920. $vars = [
  921. 'REQUEST_METHOD' => 'PUT',
  922. 'HTTPS' => 'on',
  923. ];
  924. $request = ServerRequestFactory::fromGlobals($vars);
  925. $expected = $vars + [
  926. 'CONTENT_TYPE' => null,
  927. 'HTTP_CONTENT_TYPE' => null,
  928. 'ORIGINAL_REQUEST_METHOD' => 'PUT',
  929. 'HTTP_HOST' => 'localhost',
  930. ];
  931. $this->assertSame($expected, $request->getServerParams());
  932. }
  933. /**
  934. * Tests that overriding the method to GET will clean all request
  935. * data, to better simulate a GET request.
  936. */
  937. public function testMethodOverrideEmptyParsedBody(): void
  938. {
  939. $body = ['_method' => 'GET', 'foo' => 'bar'];
  940. $request = ServerRequestFactory::fromGlobals(
  941. ['REQUEST_METHOD' => 'POST'],
  942. [],
  943. $body
  944. );
  945. $this->assertEmpty($request->getParsedBody());
  946. $request = ServerRequestFactory::fromGlobals(
  947. [
  948. 'REQUEST_METHOD' => 'POST',
  949. 'HTTP_X_HTTP_METHOD_OVERRIDE' => 'GET',
  950. ],
  951. [],
  952. ['foo' => 'bar']
  953. );
  954. $this->assertEmpty($request->getParsedBody());
  955. }
  956. /**
  957. * Tests the default file upload merging behavior.
  958. */
  959. public function testFromGlobalsWithFiles(): void
  960. {
  961. $this->assertNull(Configure::read('App.uploadedFilesAsObjects'));
  962. $files = [
  963. 'file' => [
  964. 'name' => 'file.txt',
  965. 'type' => 'text/plain',
  966. 'tmp_name' => __FILE__,
  967. 'error' => 0,
  968. 'size' => 1234,
  969. ],
  970. ];
  971. $request = ServerRequestFactory::fromGlobals(null, null, null, null, $files);
  972. /** @var \Laminas\Diactoros\UploadedFile $expected */
  973. $expected = $request->getData('file');
  974. $this->assertSame($files['file']['size'], $expected->getSize());
  975. $this->assertSame($files['file']['error'], $expected->getError());
  976. $this->assertSame($files['file']['name'], $expected->getClientFilename());
  977. $this->assertSame($files['file']['type'], $expected->getClientMediaType());
  978. }
  979. /**
  980. * Test processing files with `file` field names.
  981. */
  982. public function testFilesNested(): void
  983. {
  984. $files = [
  985. 'image_main' => [
  986. 'name' => ['file' => 'born on.txt'],
  987. 'type' => ['file' => 'text/plain'],
  988. 'tmp_name' => ['file' => __FILE__],
  989. 'error' => ['file' => 0],
  990. 'size' => ['file' => 17178],
  991. ],
  992. 0 => [
  993. 'name' => ['image' => 'scratch.text'],
  994. 'type' => ['image' => 'text/plain'],
  995. 'tmp_name' => ['image' => __FILE__],
  996. 'error' => ['image' => 0],
  997. 'size' => ['image' => 1490],
  998. ],
  999. 'pictures' => [
  1000. 'name' => [
  1001. 0 => ['file' => 'a-file.png'],
  1002. 1 => ['file' => 'a-moose.png'],
  1003. ],
  1004. 'type' => [
  1005. 0 => ['file' => 'image/png'],
  1006. 1 => ['file' => 'image/jpg'],
  1007. ],
  1008. 'tmp_name' => [
  1009. 0 => ['file' => __FILE__],
  1010. 1 => ['file' => __FILE__],
  1011. ],
  1012. 'error' => [
  1013. 0 => ['file' => 0],
  1014. 1 => ['file' => 0],
  1015. ],
  1016. 'size' => [
  1017. 0 => ['file' => 17188],
  1018. 1 => ['file' => 2010],
  1019. ],
  1020. ],
  1021. ];
  1022. $post = [
  1023. 'pictures' => [
  1024. 0 => ['name' => 'A cat'],
  1025. 1 => ['name' => 'A moose'],
  1026. ],
  1027. 0 => [
  1028. 'name' => 'A dog',
  1029. ],
  1030. ];
  1031. $request = ServerRequestFactory::fromGlobals(null, null, $post, null, $files);
  1032. $expected = [
  1033. 'image_main' => [
  1034. 'file' => new UploadedFile(
  1035. __FILE__,
  1036. 17178,
  1037. 0,
  1038. 'born on.txt',
  1039. 'text/plain'
  1040. ),
  1041. ],
  1042. 'pictures' => [
  1043. 0 => [
  1044. 'name' => 'A cat',
  1045. 'file' => new UploadedFile(
  1046. __FILE__,
  1047. 17188,
  1048. 0,
  1049. 'a-file.png',
  1050. 'image/png'
  1051. ),
  1052. ],
  1053. 1 => [
  1054. 'name' => 'A moose',
  1055. 'file' => new UploadedFile(
  1056. __FILE__,
  1057. 2010,
  1058. 0,
  1059. 'a-moose.png',
  1060. 'image/jpg'
  1061. ),
  1062. ],
  1063. ],
  1064. 0 => [
  1065. 'name' => 'A dog',
  1066. 'image' => new UploadedFile(
  1067. __FILE__,
  1068. 1490,
  1069. 0,
  1070. 'scratch.text',
  1071. 'text/plain'
  1072. ),
  1073. ],
  1074. ];
  1075. $this->assertEquals($expected, $request->getData());
  1076. $uploads = $request->getUploadedFiles();
  1077. $this->assertCount(3, $uploads);
  1078. $this->assertArrayHasKey(0, $uploads);
  1079. $this->assertSame('scratch.text', $uploads[0]['image']->getClientFilename());
  1080. $this->assertArrayHasKey('pictures', $uploads);
  1081. $this->assertSame('a-file.png', $uploads['pictures'][0]['file']->getClientFilename());
  1082. $this->assertSame('a-moose.png', $uploads['pictures'][1]['file']->getClientFilename());
  1083. $this->assertArrayHasKey('image_main', $uploads);
  1084. $this->assertSame('born on.txt', $uploads['image_main']['file']->getClientFilename());
  1085. }
  1086. /**
  1087. * Test processing a file input with no .'s in it.
  1088. */
  1089. public function testFilesFlat(): void
  1090. {
  1091. $files = [
  1092. 'birth_cert' => [
  1093. 'name' => 'born on.txt',
  1094. 'type' => 'application/octet-stream',
  1095. 'tmp_name' => __FILE__,
  1096. 'error' => 0,
  1097. 'size' => 123,
  1098. ],
  1099. ];
  1100. $request = ServerRequestFactory::fromGlobals([], [], [], [], $files);
  1101. $this->assertInstanceOf(UploadedFileInterface::class, $request->getData()['birth_cert']);
  1102. $uploads = $request->getUploadedFiles();
  1103. $this->assertCount(1, $uploads);
  1104. $this->assertArrayHasKey('birth_cert', $uploads);
  1105. $this->assertSame('born on.txt', $uploads['birth_cert']->getClientFilename());
  1106. $this->assertSame(0, $uploads['birth_cert']->getError());
  1107. $this->assertSame('application/octet-stream', $uploads['birth_cert']->getClientMediaType());
  1108. $this->assertSame(123, $uploads['birth_cert']->getSize());
  1109. }
  1110. /**
  1111. * Test that files in the 0th index work.
  1112. */
  1113. public function testFilesZeroithIndex(): void
  1114. {
  1115. $files = [
  1116. 0 => [
  1117. 'name' => 'cake_sqlserver_patch.patch',
  1118. 'type' => 'text/plain',
  1119. 'tmp_name' => __FILE__,
  1120. 'error' => 0,
  1121. 'size' => 6271,
  1122. ],
  1123. ];
  1124. $request = ServerRequestFactory::fromGlobals([], [], [], [], $files);
  1125. $this->assertInstanceOf(UploadedFileInterface::class, $request->getData()[0]);
  1126. $uploads = $request->getUploadedFiles();
  1127. $this->assertCount(1, $uploads);
  1128. $this->assertSame($files[0]['name'], $uploads[0]->getClientFilename());
  1129. }
  1130. /**
  1131. * Tests that file uploads are merged into the post data as objects instead of as arrays.
  1132. */
  1133. public function testFilesAsObjectsInRequestData(): void
  1134. {
  1135. $files = [
  1136. 'flat' => [
  1137. 'name' => 'flat.txt',
  1138. 'type' => 'text/plain',
  1139. 'tmp_name' => __FILE__,
  1140. 'error' => 0,
  1141. 'size' => 1,
  1142. ],
  1143. 'nested' => [
  1144. 'name' => ['file' => 'nested.txt'],
  1145. 'type' => ['file' => 'text/plain'],
  1146. 'tmp_name' => ['file' => __FILE__],
  1147. 'error' => ['file' => 0],
  1148. 'size' => ['file' => 12],
  1149. ],
  1150. 0 => [
  1151. 'name' => 'numeric.txt',
  1152. 'type' => 'text/plain',
  1153. 'tmp_name' => __FILE__,
  1154. 'error' => 0,
  1155. 'size' => 123,
  1156. ],
  1157. 1 => [
  1158. 'name' => ['file' => 'numeric-nested.txt'],
  1159. 'type' => ['file' => 'text/plain'],
  1160. 'tmp_name' => ['file' => __FILE__],
  1161. 'error' => ['file' => 0],
  1162. 'size' => ['file' => 1234],
  1163. ],
  1164. 'deep' => [
  1165. 'name' => [
  1166. 0 => ['file' => 'deep-1.txt'],
  1167. 1 => ['file' => 'deep-2.txt'],
  1168. ],
  1169. 'type' => [
  1170. 0 => ['file' => 'text/plain'],
  1171. 1 => ['file' => 'text/plain'],
  1172. ],
  1173. 'tmp_name' => [
  1174. 0 => ['file' => __FILE__],
  1175. 1 => ['file' => __FILE__],
  1176. ],
  1177. 'error' => [
  1178. 0 => ['file' => 0],
  1179. 1 => ['file' => 0],
  1180. ],
  1181. 'size' => [
  1182. 0 => ['file' => 12345],
  1183. 1 => ['file' => 123456],
  1184. ],
  1185. ],
  1186. ];
  1187. $post = [
  1188. 'flat' => ['existing'],
  1189. 'nested' => [
  1190. 'name' => 'nested',
  1191. 'file' => ['existing'],
  1192. ],
  1193. 'deep' => [
  1194. 0 => [
  1195. 'name' => 'deep 1',
  1196. 'file' => ['existing'],
  1197. ],
  1198. 1 => [
  1199. 'name' => 'deep 2',
  1200. ],
  1201. ],
  1202. 1 => [
  1203. 'name' => 'numeric nested',
  1204. ],
  1205. ];
  1206. $expected = [
  1207. 'flat' => new UploadedFile(
  1208. __FILE__,
  1209. 1,
  1210. 0,
  1211. 'flat.txt',
  1212. 'text/plain'
  1213. ),
  1214. 'nested' => [
  1215. 'name' => 'nested',
  1216. 'file' => new UploadedFile(
  1217. __FILE__,
  1218. 12,
  1219. 0,
  1220. 'nested.txt',
  1221. 'text/plain'
  1222. ),
  1223. ],
  1224. 'deep' => [
  1225. 0 => [
  1226. 'name' => 'deep 1',
  1227. 'file' => new UploadedFile(
  1228. __FILE__,
  1229. 12345,
  1230. 0,
  1231. 'deep-1.txt',
  1232. 'text/plain'
  1233. ),
  1234. ],
  1235. 1 => [
  1236. 'name' => 'deep 2',
  1237. 'file' => new UploadedFile(
  1238. __FILE__,
  1239. 123456,
  1240. 0,
  1241. 'deep-2.txt',
  1242. 'text/plain'
  1243. ),
  1244. ],
  1245. ],
  1246. 0 => new UploadedFile(
  1247. __FILE__,
  1248. 123,
  1249. 0,
  1250. 'numeric.txt',
  1251. 'text/plain'
  1252. ),
  1253. 1 => [
  1254. 'name' => 'numeric nested',
  1255. 'file' => new UploadedFile(
  1256. __FILE__,
  1257. 1234,
  1258. 0,
  1259. 'numeric-nested.txt',
  1260. 'text/plain'
  1261. ),
  1262. ],
  1263. ];
  1264. $request = ServerRequestFactory::fromGlobals([], [], $post, [], $files);
  1265. $this->assertEquals($expected, $request->getData());
  1266. }
  1267. /**
  1268. * Test passing invalid files list structure.
  1269. */
  1270. public function testFilesWithInvalidStructure(): void
  1271. {
  1272. $this->expectException(InvalidArgumentException::class);
  1273. $this->expectExceptionMessage('Invalid value in files specification');
  1274. ServerRequestFactory::fromGlobals([], [], [], [], [
  1275. [
  1276. 'invalid' => [
  1277. 'data',
  1278. ],
  1279. ],
  1280. ]);
  1281. }
  1282. public function testCreateServerRequest(): void
  1283. {
  1284. $factory = new ServerRequestFactory();
  1285. $request = $factory->createServerRequest('GET', 'https://cakephp.org/team', ['foo' => 'bar']);
  1286. $this->assertInstanceOf(ServerRequest::class, $request);
  1287. $this->assertSame('GET', $request->getMethod());
  1288. $this->assertSame('/team', $request->getRequestTarget());
  1289. $expected = ['foo' => 'bar', 'REQUEST_METHOD' => 'GET'];
  1290. $this->assertEquals($expected, $request->getServerParams());
  1291. }
  1292. }