ServerRequestFactoryTest.php 53 KB

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