| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452 |
- <?php
- declare(strict_types=1);
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 3.3.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Http;
- use Cake\Core\Configure;
- use Cake\Http\ServerRequestFactory;
- use Cake\Http\Session;
- use Cake\TestSuite\TestCase;
- use Laminas\Diactoros\Exception\InvalidArgumentException;
- use Laminas\Diactoros\UploadedFile;
- use Psr\Http\Message\UploadedFileInterface;
- /**
- * Test case for the server factory.
- */
- class ServerRequestFactoryTest extends TestCase
- {
- /**
- * Test fromGlobals reads super globals
- */
- public function testFromGlobalsSuperGlobals(): void
- {
- $post = [
- 'title' => 'custom',
- ];
- $files = [
- 'image' => [
- 'tmp_name' => __FILE__,
- 'error' => 0,
- 'name' => 'cats.png',
- 'type' => 'image/png',
- 'size' => 2112,
- ],
- ];
- $cookies = ['key' => 'value'];
- $query = ['query' => 'string'];
- $res = ServerRequestFactory::fromGlobals([], $query, $post, $cookies, $files);
- $this->assertSame($cookies['key'], $res->getCookie('key'));
- $this->assertSame($query['query'], $res->getQuery('query'));
- $this->assertArrayHasKey('title', $res->getData());
- $this->assertArrayHasKey('image', $res->getData());
- $this->assertCount(1, $res->getUploadedFiles());
- /** @var \Psr\Http\Message\UploadedFileInterface $expected */
- $expected = $res->getData('image');
- $this->assertInstanceOf(UploadedFileInterface::class, $expected);
- $this->assertSame($files['image']['size'], $expected->getSize());
- $this->assertSame($files['image']['error'], $expected->getError());
- $this->assertSame($files['image']['name'], $expected->getClientFilename());
- $this->assertSame($files['image']['type'], $expected->getClientMediaType());
- }
- public function testFromGlobalsUriScheme(): void
- {
- $server = [
- 'DOCUMENT_ROOT' => '/cake/repo/webroot',
- 'PHP_SELF' => '/index.php',
- 'REQUEST_URI' => '/posts/add',
- 'HTTP_X_FORWARDED_PROTO' => 'https',
- ];
- $request = ServerRequestFactory::fromGlobals($server);
- $this->assertSame('http', $request->scheme());
- $this->assertSame('http', $request->getUri()->getScheme());
- $request->setTrustedProxies([]);
- // Yeah even setting an empty list of proxies does the trick.
- $this->assertSame('https', $request->scheme());
- $this->assertSame('https', $request->getUri()->getScheme());
- }
- /**
- * Test fromGlobals includes the session
- *
- * @preserveGlobalState disabled
- * @runInSeparateProcess
- */
- public function testFromGlobalsUrlSession(): void
- {
- Configure::write('App.base', '/basedir');
- $server = [
- 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
- 'PHP_SELF' => '/index.php',
- 'REQUEST_URI' => '/posts/add',
- ];
- $res = ServerRequestFactory::fromGlobals($server);
- $session = $res->getAttribute('session');
- $this->assertInstanceOf(Session::class, $session);
- $this->assertSame('/basedir/', ini_get('session.cookie_path'), 'Needs trailing / for cookie to work');
- }
- /**
- * Test fromGlobals with App.base defined.
- */
- public function testFromGlobalsUrlBaseDefined(): void
- {
- Configure::write('App.base', 'basedir');
- $server = [
- 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
- 'PHP_SELF' => '/index.php',
- 'REQUEST_URI' => '/posts/add',
- ];
- $res = ServerRequestFactory::fromGlobals($server);
- $this->assertSame('basedir', $res->getAttribute('base'));
- $this->assertSame('basedir/', $res->getAttribute('webroot'));
- $this->assertSame('/posts/add', $res->getUri()->getPath());
- }
- /**
- * Test fromGlobals with mod-rewrite server configuration.
- */
- public function testFromGlobalsUrlModRewrite(): void
- {
- Configure::write('App.baseUrl', false);
- $server = [
- 'DOCUMENT_ROOT' => '/cake/repo/branches',
- 'PHP_SELF' => '/urlencode me/webroot/index.php',
- 'REQUEST_URI' => '/posts/view/1',
- ];
- $res = ServerRequestFactory::fromGlobals($server);
- $this->assertSame('/urlencode%20me', $res->getAttribute('base'));
- $this->assertSame('/urlencode%20me/', $res->getAttribute('webroot'));
- $this->assertSame('/posts/view/1', $res->getUri()->getPath());
- $request = ServerRequestFactory::fromGlobals([
- 'DOCUMENT_ROOT' => '/cake/repo/branches',
- 'PHP_SELF' => '/1.2.x.x/webroot/index.php',
- 'PATH_INFO' => '/posts/view/1',
- ]);
- $this->assertSame('/1.2.x.x', $request->getAttribute('base'));
- $this->assertSame('/1.2.x.x/', $request->getAttribute('webroot'));
- $this->assertSame('/posts/view/1', $request->getRequestTarget());
- $request = ServerRequestFactory::fromGlobals([
- 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/test/',
- 'PHP_SELF' => '/webroot/index.php',
- ]);
- $this->assertSame('', $request->getAttribute('base'));
- $this->assertSame('/', $request->getAttribute('webroot'));
- $request = ServerRequestFactory::fromGlobals([
- 'DOCUMENT_ROOT' => '/some/apps/where',
- 'PHP_SELF' => '/webroot/index.php',
- ]);
- $this->assertSame('', $request->getAttribute('base'));
- $this->assertSame('/', $request->getAttribute('webroot'));
- Configure::write('App.dir', 'auth');
- $request = ServerRequestFactory::fromGlobals([
- 'DOCUMENT_ROOT' => '/cake/repo/branches',
- 'PHP_SELF' => '/demos/webroot/index.php',
- ]);
- $this->assertSame('/demos', $request->getAttribute('base'));
- $this->assertSame('/demos/', $request->getAttribute('webroot'));
- Configure::write('App.dir', 'code');
- $request = ServerRequestFactory::fromGlobals([
- 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
- 'PHP_SELF' => '/clients/PewterReport/webroot/index.php',
- ]);
- $this->assertSame('/clients/PewterReport', $request->getAttribute('base'));
- $this->assertSame('/clients/PewterReport/', $request->getAttribute('webroot'));
- }
- /**
- * Test baseUrl with ModRewrite alias
- */
- public function testBaseUrlwithModRewriteAlias(): void
- {
- Configure::write('App.base', '/control');
- $request = ServerRequestFactory::fromGlobals([
- 'DOCUMENT_ROOT' => '/home/aplusnur/public_html',
- 'PHP_SELF' => '/control/index.php',
- ]);
- $this->assertSame('/control', $request->getAttribute('base'));
- $this->assertSame('/control/', $request->getAttribute('webroot'));
- Configure::write('App.base', false);
- Configure::write('App.dir', 'affiliate');
- Configure::write('App.webroot', 'newaffiliate');
- $request = ServerRequestFactory::fromGlobals([
- 'DOCUMENT_ROOT' => '/var/www/abtravaff/html',
- 'PHP_SELF' => '/newaffiliate/index.php',
- ]);
- $this->assertSame('', $request->getAttribute('base'));
- $this->assertSame('/', $request->getAttribute('webroot'));
- }
- /**
- * Test base, webroot, URL and here parsing when there is URL rewriting but
- * CakePHP gets called with index.php in URL nonetheless.
- *
- * Tests uri with
- *
- * - index.php/
- * - index.php/
- * - index.php/apples/
- * - index.php/bananas/eat/tasty_banana
- */
- public function testBaseUrlWithModRewriteAndIndexPhp(): void
- {
- $request = ServerRequestFactory::fromGlobals([
- 'DOCUMENT_ROOT' => '/cakephp/webroot/index.php',
- 'PHP_SELF' => '/cakephp/webroot/index.php',
- ]);
- $this->assertSame('/cakephp', $request->getAttribute('base'));
- $this->assertSame('/cakephp/', $request->getAttribute('webroot'));
- $this->assertSame('/', $request->getRequestTarget());
- $request = ServerRequestFactory::fromGlobals([
- 'REQUEST_URI' => '/cakephp/webroot/index.php/',
- 'PHP_SELF' => '/cakephp/webroot/index.php/',
- 'PATH_INFO' => '/',
- ]);
- $this->assertSame('/cakephp', $request->getAttribute('base'));
- $this->assertSame('/cakephp/', $request->getAttribute('webroot'));
- $this->assertSame('/', $request->getRequestTarget());
- $request = ServerRequestFactory::fromGlobals([
- 'REQUEST_URI' => '/cakephp/webroot/index.php/apples',
- 'PHP_SELF' => '/cakephp/webroot/index.php/apples',
- 'PATH_INFO' => '/apples',
- ]);
- $this->assertSame('/cakephp', $request->getAttribute('base'));
- $this->assertSame('/cakephp/', $request->getAttribute('webroot'));
- $this->assertSame('/apples', $request->getRequestTarget());
- $request = ServerRequestFactory::fromGlobals([
- 'REQUEST_URI' => '/cakephp/webroot/index.php/melons/share/',
- 'PHP_SELF' => '/cakephp/webroot/index.php/melons/share/',
- 'PATH_INFO' => '/melons/share/',
- ]);
- $this->assertSame('/cakephp', $request->getAttribute('base'));
- $this->assertSame('/cakephp/', $request->getAttribute('webroot'));
- $this->assertSame('/melons/share/', $request->getRequestTarget());
- $request = ServerRequestFactory::fromGlobals([
- 'REQUEST_URI' => '/cakephp/webroot/index.php/bananas/eat/tasty_banana',
- 'PHP_SELF' => '/cakephp/webroot/index.php/bananas/eat/tasty_banana',
- 'PATH_INFO' => '/bananas/eat/tasty_banana',
- ]);
- $this->assertSame('/cakephp', $request->getAttribute('base'));
- $this->assertSame('/cakephp/', $request->getAttribute('webroot'));
- $this->assertSame('/bananas/eat/tasty_banana', $request->getRequestTarget());
- }
- /**
- * Test that even if mod_rewrite is on, and the url contains index.php
- * and there are numerous //s that the base/webroot is calculated correctly.
- */
- public function testBaseUrlWithModRewriteAndExtraSlashes(): void
- {
- $request = ServerRequestFactory::fromGlobals([
- 'REQUEST_URI' => '/cakephp/webroot///index.php/bananas/eat',
- 'PHP_SELF' => '/cakephp/webroot///index.php/bananas/eat',
- 'PATH_INFO' => '/bananas/eat',
- ]);
- $this->assertSame('/cakephp', $request->getAttribute('base'));
- $this->assertSame('/cakephp/', $request->getAttribute('webroot'));
- $this->assertSame('/bananas/eat', $request->getRequestTarget());
- }
- /**
- * Test fromGlobals with mod-rewrite in the root dir.
- */
- public function testFromGlobalsUrlModRewriteRootDir(): void
- {
- $server = [
- 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
- 'PHP_SELF' => '/index.php',
- 'REQUEST_URI' => '/posts/add',
- ];
- $res = ServerRequestFactory::fromGlobals($server);
- $this->assertSame('', $res->getAttribute('base'));
- $this->assertSame('/', $res->getAttribute('webroot'));
- $this->assertSame('/posts/add', $res->getUri()->getPath());
- }
- /**
- * Test fromGlobals with App.baseUrl defined implying no
- * mod-rewrite and no virtual path.
- */
- public function testFromGlobalsUrlNoModRewriteWebrootDir(): void
- {
- Configure::write('App', [
- 'dir' => 'app',
- 'webroot' => 'www',
- 'base' => false,
- 'baseUrl' => '/cake/index.php',
- ]);
- $server = [
- 'DOCUMENT_ROOT' => '/Users/markstory/Sites',
- 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/www/index.php',
- 'PHP_SELF' => '/cake/www/index.php/posts/index',
- 'REQUEST_URI' => '/cake/www/index.php',
- ];
- $res = ServerRequestFactory::fromGlobals($server);
- $this->assertSame('/cake/www/', $res->getAttribute('webroot'));
- $this->assertSame('/cake/index.php', $res->getAttribute('base'));
- $this->assertSame('/', $res->getUri()->getPath());
- }
- /**
- * Test fromGlobals with App.baseUrl defined implying no
- * mod-rewrite
- */
- public function testFromGlobalsUrlNoModRewrite(): void
- {
- Configure::write('App', [
- 'dir' => 'app',
- 'webroot' => 'webroot',
- 'base' => false,
- 'baseUrl' => '/cake/index.php',
- ]);
- $server = [
- 'DOCUMENT_ROOT' => '/Users/markstory/Sites',
- 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/index.php',
- 'PHP_SELF' => '/cake/index.php/posts/index',
- 'REQUEST_URI' => '/cake/index.php/posts/index',
- ];
- $res = ServerRequestFactory::fromGlobals($server);
- $this->assertSame('/cake/webroot/', $res->getAttribute('webroot'));
- $this->assertSame('/cake/index.php', $res->getAttribute('base'));
- $this->assertSame('/posts/index', $res->getUri()->getPath());
- }
- /**
- * Test fromGlobals with App.baseUrl defined implying no
- * mod-rewrite in the root dir.
- */
- public function testFromGlobalsUrlNoModRewriteRootDir(): void
- {
- Configure::write('App', [
- 'dir' => 'cake',
- 'webroot' => 'webroot',
- 'base' => false,
- 'baseUrl' => '/index.php',
- ]);
- $server = [
- 'DOCUMENT_ROOT' => '/Users/markstory/Sites/cake',
- 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/index.php',
- 'PHP_SELF' => '/index.php/posts/add',
- 'REQUEST_URI' => '/index.php/posts/add',
- ];
- $res = ServerRequestFactory::fromGlobals($server);
- $this->assertSame('/webroot/', $res->getAttribute('webroot'));
- $this->assertSame('/index.php', $res->getAttribute('base'));
- $this->assertSame('/posts/add', $res->getUri()->getPath());
- }
- /**
- * Check that a sub-directory containing app|webroot doesn't get mishandled when re-writing is off.
- */
- public function testBaseUrlWithAppAndWebrootInDirname(): void
- {
- Configure::write('App.baseUrl', '/approval/index.php');
- $request = ServerRequestFactory::fromGlobals([
- 'DOCUMENT_ROOT' => '/Users/markstory/Sites/',
- 'SCRIPT_FILENAME' => '/Users/markstory/Sites/approval/index.php',
- ]);
- $this->assertSame('/approval/index.php', $request->getAttribute('base'));
- $this->assertSame('/approval/webroot/', $request->getAttribute('webroot'));
- Configure::write('App.baseUrl', '/webrootable/index.php');
- $request = ServerRequestFactory::fromGlobals([
- 'DOCUMENT_ROOT' => '/Users/markstory/Sites/',
- 'SCRIPT_FILENAME' => '/Users/markstory/Sites/webrootable/index.php',
- ]);
- $this->assertSame('/webrootable/index.php', $request->getAttribute('base'));
- $this->assertSame('/webrootable/webroot/', $request->getAttribute('webroot'));
- }
- /**
- * Test baseUrl and webroot with baseUrl
- */
- public function testBaseUrlAndWebrootWithBaseUrl(): void
- {
- Configure::write('App.dir', 'App');
- Configure::write('App.baseUrl', '/App/webroot/index.php');
- $request = ServerRequestFactory::fromGlobals();
- $this->assertSame('/App/webroot/index.php', $request->getAttribute('base'));
- $this->assertSame('/App/webroot/', $request->getAttribute('webroot'));
- Configure::write('App.baseUrl', '/App/webroot/test.php');
- $request = ServerRequestFactory::fromGlobals();
- $this->assertSame('/App/webroot/test.php', $request->getAttribute('base'));
- $this->assertSame('/App/webroot/', $request->getAttribute('webroot'));
- Configure::write('App.baseUrl', '/App/index.php');
- $request = ServerRequestFactory::fromGlobals();
- $this->assertSame('/App/index.php', $request->getAttribute('base'));
- $this->assertSame('/App/webroot/', $request->getAttribute('webroot'));
- Configure::write('App.baseUrl', '/CakeBB/App/webroot/index.php');
- $request = ServerRequestFactory::fromGlobals();
- $this->assertSame('/CakeBB/App/webroot/index.php', $request->getAttribute('base'));
- $this->assertSame('/CakeBB/App/webroot/', $request->getAttribute('webroot'));
- Configure::write('App.baseUrl', '/CakeBB/App/index.php');
- $request = ServerRequestFactory::fromGlobals();
- $this->assertSame('/CakeBB/App/index.php', $request->getAttribute('base'));
- $this->assertSame('/CakeBB/App/webroot/', $request->getAttribute('webroot'));
- Configure::write('App.baseUrl', '/CakeBB/index.php');
- $request = ServerRequestFactory::fromGlobals();
- $this->assertSame('/CakeBB/index.php', $request->getAttribute('base'));
- $this->assertSame('/CakeBB/webroot/', $request->getAttribute('webroot'));
- Configure::write('App.baseUrl', '/dbhauser/index.php');
- $request = ServerRequestFactory::fromGlobals([
- 'DOCUMENT_ROOT' => '/kunden/homepages/4/d181710652/htdocs/joomla',
- 'SCRIPT_FILENAME' => '/kunden/homepages/4/d181710652/htdocs/joomla/dbhauser/index.php',
- ]);
- $this->assertSame('/dbhauser/index.php', $request->getAttribute('base'));
- $this->assertSame('/dbhauser/webroot/', $request->getAttribute('webroot'));
- }
- /**
- * Test that a request with a . in the main GET parameter is filtered out.
- * PHP changes GET parameter keys containing dots to _.
- */
- public function testGetParamsWithDot(): void
- {
- $request = ServerRequestFactory::fromGlobals([
- 'PHP_SELF' => '/webroot/index.php',
- 'REQUEST_URI' => '/posts/index/add.add',
- ]);
- $this->assertSame('', $request->getAttribute('base'));
- $this->assertEquals([], $request->getQueryParams());
- $request = ServerRequestFactory::fromGlobals([
- 'PHP_SELF' => '/cake_dev/webroot/index.php',
- 'REQUEST_URI' => '/cake_dev/posts/index/add.add',
- ]);
- $this->assertSame('/cake_dev', $request->getAttribute('base'));
- $this->assertEquals([], $request->getQueryParams());
- }
- /**
- * Test that a request with urlencoded bits in the main GET parameter are filtered out.
- */
- public function testGetParamWithUrlencodedElement(): void
- {
- $request = ServerRequestFactory::fromGlobals([
- 'PHP_SELF' => '/webroot/index.php',
- 'REQUEST_URI' => '/posts/add/%E2%88%82%E2%88%82',
- ]);
- $this->assertSame('', $request->getAttribute('base'));
- $this->assertEquals([], $request->getQueryParams());
- $request = ServerRequestFactory::fromGlobals([
- 'PHP_SELF' => '/cake_dev/webroot/index.php',
- 'REQUEST_URI' => '/cake_dev/posts/add/%E2%88%82%E2%88%82',
- ]);
- $this->assertSame('/cake_dev', $request->getAttribute('base'));
- $this->assertEquals([], $request->getQueryParams());
- }
- /**
- * Generator for environment configurations
- *
- * @return array Environment array
- */
- public static function environmentGenerator(): array
- {
- return [
- [
- 'IIS - No rewrite base path',
- [
- 'App' => [
- 'base' => false,
- 'baseUrl' => '/index.php',
- 'dir' => 'TestApp',
- 'webroot' => 'webroot',
- ],
- 'SERVER' => [
- 'SCRIPT_NAME' => '/index.php',
- 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
- 'QUERY_STRING' => '',
- 'REQUEST_URI' => '/index.php',
- 'URL' => '/index.php',
- 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\index.php',
- 'ORIG_PATH_INFO' => '/index.php',
- 'PATH_INFO' => '',
- 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\index.php',
- 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
- 'PHP_SELF' => '/index.php',
- ],
- ],
- [
- 'base' => '/index.php',
- 'webroot' => '/webroot/',
- 'url' => '',
- ],
- ],
- [
- 'IIS - No rewrite with path, no PHP_SELF',
- [
- 'App' => [
- 'base' => false,
- 'baseUrl' => '/index.php?',
- 'dir' => 'TestApp',
- 'webroot' => 'webroot',
- ],
- 'SERVER' => [
- 'QUERY_STRING' => '/posts/add',
- 'REQUEST_URI' => '/index.php?/posts/add',
- 'PHP_SELF' => '',
- 'URL' => '/index.php?/posts/add',
- 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
- 'argv' => ['/posts/add'],
- 'argc' => 1,
- ],
- ],
- [
- 'url' => 'posts/add',
- 'base' => '/index.php?',
- 'webroot' => '/webroot/',
- ],
- ],
- [
- 'IIS - No rewrite sub dir 2',
- [
- 'App' => [
- 'base' => false,
- 'baseUrl' => '/site/index.php',
- 'dir' => 'TestApp',
- 'webroot' => 'webroot',
- ],
- 'SERVER' => [
- 'SCRIPT_NAME' => '/site/index.php',
- 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
- 'QUERY_STRING' => '',
- 'REQUEST_URI' => '/site/index.php',
- 'URL' => '/site/index.php',
- 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
- 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
- 'PHP_SELF' => '/site/index.php',
- 'argv' => [],
- 'argc' => 0,
- ],
- ],
- [
- 'url' => '',
- 'base' => '/site/index.php',
- 'webroot' => '/site/webroot/',
- ],
- ],
- [
- 'IIS - No rewrite sub dir 2 with path',
- [
- 'App' => [
- 'base' => false,
- 'baseUrl' => '/site/index.php',
- 'dir' => 'TestApp',
- 'webroot' => 'webroot',
- ],
- 'SERVER' => [
- 'SCRIPT_NAME' => '/site/index.php',
- 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
- 'QUERY_STRING' => '/posts/add',
- 'REQUEST_URI' => '/site/index.php/posts/add',
- 'URL' => '/site/index.php/posts/add',
- 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
- 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
- 'PHP_SELF' => '/site/index.php/posts/add',
- 'argv' => ['/posts/add'],
- 'argc' => 1,
- ],
- ],
- [
- 'url' => 'posts/add',
- 'base' => '/site/index.php',
- 'webroot' => '/site/webroot/',
- ],
- ],
- [
- 'Apache - No rewrite, document root set to webroot, requesting path',
- [
- 'App' => [
- 'base' => false,
- 'baseUrl' => '/index.php',
- 'dir' => 'TestApp',
- 'webroot' => 'webroot',
- ],
- 'SERVER' => [
- 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/App/webroot',
- 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/App/webroot/index.php',
- 'QUERY_STRING' => '',
- 'REQUEST_URI' => '/index.php/posts/index',
- 'SCRIPT_NAME' => '/index.php',
- 'PATH_INFO' => '/posts/index',
- 'PHP_SELF' => '/index.php/posts/index',
- ],
- ],
- [
- 'url' => 'posts/index',
- 'base' => '/index.php',
- 'webroot' => '/',
- ],
- ],
- [
- 'Apache - No rewrite, document root set to webroot, requesting root',
- [
- 'App' => [
- 'base' => false,
- 'baseUrl' => '/index.php',
- 'dir' => 'TestApp',
- 'webroot' => 'webroot',
- ],
- 'SERVER' => [
- 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/App/webroot',
- 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/App/webroot/index.php',
- 'QUERY_STRING' => '',
- 'REQUEST_URI' => '/index.php',
- 'SCRIPT_NAME' => '/index.php',
- 'PATH_INFO' => '',
- 'PHP_SELF' => '/index.php',
- ],
- ],
- [
- 'url' => '',
- 'base' => '/index.php',
- 'webroot' => '/',
- ],
- ],
- [
- 'Apache - No rewrite, document root set above top level cake dir, requesting path',
- [
- 'App' => [
- 'base' => false,
- 'baseUrl' => '/site/index.php',
- 'dir' => 'TestApp',
- 'webroot' => 'webroot',
- ],
- 'SERVER' => [
- 'SERVER_NAME' => 'localhost',
- 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
- 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
- 'REQUEST_URI' => '/site/index.php/posts/index',
- 'SCRIPT_NAME' => '/site/index.php',
- 'PATH_INFO' => '/posts/index',
- 'PHP_SELF' => '/site/index.php/posts/index',
- ],
- ],
- [
- 'url' => 'posts/index',
- 'base' => '/site/index.php',
- 'webroot' => '/site/webroot/',
- ],
- ],
- [
- 'Apache - No rewrite, document root set above top level cake dir, request root, no PATH_INFO',
- [
- 'App' => [
- 'base' => false,
- 'baseUrl' => '/site/index.php',
- 'dir' => 'TestApp',
- 'webroot' => 'webroot',
- ],
- 'SERVER' => [
- 'SERVER_NAME' => 'localhost',
- 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
- 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
- 'REQUEST_URI' => '/site/index.php/',
- 'SCRIPT_NAME' => '/site/index.php',
- 'PHP_SELF' => '/site/index.php/',
- ],
- ],
- [
- 'url' => '',
- 'base' => '/site/index.php',
- 'webroot' => '/site/webroot/',
- ],
- ],
- [
- 'Apache - No rewrite, document root set above top level cake dir, request path, with GET',
- [
- 'App' => [
- 'base' => false,
- 'baseUrl' => '/site/index.php',
- 'dir' => 'TestApp',
- 'webroot' => 'webroot',
- ],
- 'GET' => ['a' => 'b', 'c' => 'd'],
- 'SERVER' => [
- 'SERVER_NAME' => 'localhost',
- 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
- 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
- 'REQUEST_URI' => '/site/index.php/posts/index?a=b&c=d',
- 'SCRIPT_NAME' => '/site/index.php',
- 'PATH_INFO' => '/posts/index',
- 'PHP_SELF' => '/site/index.php/posts/index',
- 'QUERY_STRING' => 'a=b&c=d',
- ],
- ],
- [
- 'urlParams' => ['a' => 'b', 'c' => 'd'],
- 'url' => 'posts/index',
- 'base' => '/site/index.php',
- 'webroot' => '/site/webroot/',
- ],
- ],
- [
- 'Apache - w/rewrite, document root set above top level cake dir, request root, no PATH_INFO',
- [
- 'App' => [
- 'base' => false,
- 'baseUrl' => false,
- 'dir' => 'TestApp',
- 'webroot' => 'webroot',
- ],
- 'SERVER' => [
- 'SERVER_NAME' => 'localhost',
- 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
- 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
- 'REQUEST_URI' => '/site/',
- 'SCRIPT_NAME' => '/site/webroot/index.php',
- 'PHP_SELF' => '/site/webroot/index.php',
- ],
- ],
- [
- 'url' => '',
- 'base' => '/site',
- 'webroot' => '/site/',
- ],
- ],
- [
- 'Apache - w/rewrite, document root above top level cake dir, request root, no PATH_INFO/REQUEST_URI',
- [
- 'App' => [
- 'base' => false,
- 'baseUrl' => false,
- 'dir' => 'TestApp',
- 'webroot' => 'webroot',
- ],
- 'SERVER' => [
- 'SERVER_NAME' => 'localhost',
- 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
- 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
- 'SCRIPT_NAME' => '/site/webroot/index.php',
- 'PHP_SELF' => '/site/webroot/index.php',
- 'PATH_INFO' => null,
- 'REQUEST_URI' => null,
- ],
- ],
- [
- 'url' => '',
- 'base' => '/site',
- 'webroot' => '/site/',
- ],
- ],
- [
- 'Apache - w/rewrite, document root set to webroot, request root, no PATH_INFO/REQUEST_URI',
- [
- 'App' => [
- 'base' => false,
- 'baseUrl' => false,
- 'dir' => 'TestApp',
- 'webroot' => 'webroot',
- ],
- 'SERVER' => [
- 'SERVER_NAME' => 'localhost',
- 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/webroot',
- 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/webroot/index.php',
- 'SCRIPT_NAME' => '/index.php',
- 'PHP_SELF' => '/index.php',
- 'PATH_INFO' => null,
- 'REQUEST_URI' => null,
- ],
- ],
- [
- 'url' => '',
- 'base' => '',
- 'webroot' => '/',
- ],
- ],
- [
- 'Apache - w/rewrite, document root set above top level cake dir, request root, absolute REQUEST_URI',
- [
- 'App' => [
- 'base' => false,
- 'baseUrl' => false,
- 'dir' => 'TestApp',
- 'webroot' => 'webroot',
- ],
- 'SERVER' => [
- 'SERVER_NAME' => 'localhost',
- 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
- 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
- 'REQUEST_URI' => '/site/posts/index',
- 'SCRIPT_NAME' => '/site/webroot/index.php',
- 'PHP_SELF' => '/site/webroot/index.php',
- ],
- ],
- [
- 'url' => 'posts/index',
- 'base' => '/site',
- 'webroot' => '/site/',
- ],
- ],
- [
- 'Nginx - w/rewrite, document root set to webroot, request root, no PATH_INFO',
- [
- 'App' => [
- 'base' => false,
- 'baseUrl' => false,
- 'dir' => 'TestApp',
- 'webroot' => 'webroot',
- ],
- 'SERVER' => [
- 'SERVER_NAME' => 'localhost',
- 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/webroot',
- 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/webroot/index.php',
- 'SCRIPT_NAME' => '/index.php',
- 'PHP_SELF' => '/index.php',
- 'PATH_INFO' => null,
- 'REQUEST_URI' => '/posts/add',
- ],
- ],
- [
- 'url' => 'posts/add',
- 'base' => '',
- 'webroot' => '/',
- 'urlParams' => [],
- ],
- ],
- [
- 'Nginx - w/rewrite, document root set above top level cake dir, request root, no PATH_INFO, base parameter set',
- [
- 'App' => [
- 'base' => false,
- 'baseUrl' => false,
- 'dir' => 'app',
- 'webroot' => 'webroot',
- ],
- 'SERVER' => [
- 'SERVER_NAME' => 'localhost',
- 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
- 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/App/webroot/index.php',
- 'SCRIPT_NAME' => '/site/app/webroot/index.php',
- 'PHP_SELF' => '/site/webroot/index.php',
- 'PATH_INFO' => null,
- 'REQUEST_URI' => '/site/posts/add',
- ],
- ],
- [
- 'url' => 'posts/add',
- 'base' => '/site',
- 'webroot' => '/site/',
- 'urlParams' => [],
- ],
- ],
- ];
- }
- /**
- * Test environment detection
- *
- * @dataProvider environmentGenerator
- * @param string $name
- * @param array $data
- * @param array $expected
- */
- public function testEnvironmentDetection($name, $data, $expected): void
- {
- if (isset($data['App'])) {
- Configure::write('App', $data['App']);
- }
- $request = ServerRequestFactory::fromGlobals(
- $data['SERVER'] ?? null,
- $data['GET'] ?? null
- );
- $uri = $request->getUri();
- $this->assertSame('/' . $expected['url'], $uri->getPath(), 'Uri->getPath() is incorrect');
- $this->assertEquals($expected['base'], $request->getAttribute('base'), 'base is incorrect');
- $this->assertEquals($expected['webroot'], $request->getAttribute('webroot'), 'webroot is incorrect');
- if (isset($expected['urlParams'])) {
- $this->assertEquals($expected['urlParams'], $request->getQueryParams(), 'GET param mismatch');
- }
- }
- public function testFormUrlEncodedBodyParsing(): void
- {
- $data = [
- 'Article' => ['title'],
- ];
- $request = ServerRequestFactory::fromGlobals([
- 'REQUEST_METHOD' => 'PUT',
- 'CONTENT_TYPE' => 'application/x-www-form-urlencoded; charset=UTF-8',
- 'CAKEPHP_INPUT' => 'Article[]=title',
- ]);
- $this->assertEquals($data, $request->getData());
- $data = ['one' => 1, 'two' => 'three'];
- $request = ServerRequestFactory::fromGlobals([
- 'REQUEST_METHOD' => 'PUT',
- 'CONTENT_TYPE' => 'application/x-www-form-urlencoded; charset=UTF-8',
- 'CAKEPHP_INPUT' => 'one=1&two=three',
- ]);
- $this->assertEquals($data, $request->getData());
- $request = ServerRequestFactory::fromGlobals([
- 'REQUEST_METHOD' => 'DELETE',
- 'CONTENT_TYPE' => 'application/x-www-form-urlencoded; charset=UTF-8',
- 'CAKEPHP_INPUT' => 'Article[title]=Testing&action=update',
- ]);
- $expected = [
- 'Article' => ['title' => 'Testing'],
- 'action' => 'update',
- ];
- $this->assertEquals($expected, $request->getData());
- $data = [
- 'Article' => ['title'],
- 'Tag' => ['Tag' => [1, 2]],
- ];
- $request = ServerRequestFactory::fromGlobals([
- 'REQUEST_METHOD' => 'PATCH',
- 'CONTENT_TYPE' => 'application/x-www-form-urlencoded; charset=UTF-8',
- 'CAKEPHP_INPUT' => 'Article[]=title&Tag[Tag][]=1&Tag[Tag][]=2',
- ]);
- $this->assertEquals($data, $request->getData());
- }
- /**
- * Test method overrides coming in from POST data.
- */
- public function testMethodOverrides(): void
- {
- $post = ['_method' => 'POST'];
- $request = ServerRequestFactory::fromGlobals([], [], $post);
- $this->assertSame('POST', $request->getEnv('REQUEST_METHOD'));
- $post = ['_method' => 'DELETE'];
- $request = ServerRequestFactory::fromGlobals([], [], $post);
- $this->assertSame('DELETE', $request->getEnv('REQUEST_METHOD'));
- $request = ServerRequestFactory::fromGlobals(['HTTP_X_HTTP_METHOD_OVERRIDE' => 'PUT']);
- $this->assertSame('PUT', $request->getEnv('REQUEST_METHOD'));
- $request = ServerRequestFactory::fromGlobals(
- ['REQUEST_METHOD' => 'POST'],
- [],
- ['_method' => 'PUT']
- );
- $this->assertSame('PUT', $request->getEnv('REQUEST_METHOD'));
- $this->assertSame('POST', $request->getEnv('ORIGINAL_REQUEST_METHOD'));
- }
- /**
- * Test getServerParams
- */
- public function testGetServerParams(): void
- {
- $vars = [
- 'REQUEST_METHOD' => 'PUT',
- 'HTTPS' => 'on',
- ];
- $request = ServerRequestFactory::fromGlobals($vars);
- $expected = $vars + [
- 'CONTENT_TYPE' => null,
- 'HTTP_CONTENT_TYPE' => null,
- 'ORIGINAL_REQUEST_METHOD' => 'PUT',
- 'HTTP_HOST' => 'localhost',
- ];
- $this->assertSame($expected, $request->getServerParams());
- }
- /**
- * Tests that overriding the method to GET will clean all request
- * data, to better simulate a GET request.
- */
- public function testMethodOverrideEmptyParsedBody(): void
- {
- $body = ['_method' => 'GET', 'foo' => 'bar'];
- $request = ServerRequestFactory::fromGlobals(
- ['REQUEST_METHOD' => 'POST'],
- [],
- $body
- );
- $this->assertEmpty($request->getParsedBody());
- $request = ServerRequestFactory::fromGlobals(
- [
- 'REQUEST_METHOD' => 'POST',
- 'HTTP_X_HTTP_METHOD_OVERRIDE' => 'GET',
- ],
- [],
- ['foo' => 'bar']
- );
- $this->assertEmpty($request->getParsedBody());
- }
- /**
- * Tests the default file upload merging behavior.
- */
- public function testFromGlobalsWithFilesAsObjectsDefault(): void
- {
- $this->assertNull(Configure::read('App.uploadedFilesAsObjects'));
- $files = [
- 'file' => [
- 'name' => 'file.txt',
- 'type' => 'text/plain',
- 'tmp_name' => __FILE__,
- 'error' => 0,
- 'size' => 1234,
- ],
- ];
- $request = ServerRequestFactory::fromGlobals(null, null, null, null, $files);
- /** @var \Laminas\Diactoros\UploadedFile $expected */
- $expected = $request->getData('file');
- $this->assertSame($files['file']['size'], $expected->getSize());
- $this->assertSame($files['file']['error'], $expected->getError());
- $this->assertSame($files['file']['name'], $expected->getClientFilename());
- $this->assertSame($files['file']['type'], $expected->getClientMediaType());
- }
- /**
- * Tests the "as arrays" file upload merging behavior.
- */
- public function testFromGlobalsWithFilesAsObjectsDisabled(): void
- {
- Configure::write('App.uploadedFilesAsObjects', false);
- $files = [
- 'file' => [
- 'name' => 'file.txt',
- 'type' => 'text/plain',
- 'tmp_name' => __FILE__,
- 'error' => 0,
- 'size' => 1234,
- ],
- ];
- $request = ServerRequestFactory::fromGlobals(null, null, null, null, $files);
- $expected = [
- 'file' => [
- 'tmp_name' => __FILE__,
- 'error' => 0,
- 'name' => 'file.txt',
- 'type' => 'text/plain',
- 'size' => 1234,
- ],
- ];
- $this->assertEquals($expected, $request->getData());
- }
- /**
- * Tests the "as objects" file upload merging behavior.
- */
- public function testFromGlobalsWithFilesAsObjectsEnabled(): void
- {
- Configure::write('App.uploadedFilesAsObjects', true);
- $files = [
- 'file' => [
- 'name' => 'file.txt',
- 'type' => 'text/plain',
- 'tmp_name' => __FILE__,
- 'error' => 0,
- 'size' => 1234,
- ],
- ];
- $request = ServerRequestFactory::fromGlobals(null, null, null, null, $files);
- $expected = [
- 'file' => new UploadedFile(
- __FILE__,
- 1234,
- 0,
- 'file.txt',
- 'text/plain'
- ),
- ];
- $this->assertEquals($expected, $request->getData());
- }
- /**
- * Test processing files with `file` field names.
- */
- public function testFilesNested(): void
- {
- $files = [
- 'image_main' => [
- 'name' => ['file' => 'born on.txt'],
- 'type' => ['file' => 'text/plain'],
- 'tmp_name' => ['file' => __FILE__],
- 'error' => ['file' => 0],
- 'size' => ['file' => 17178],
- ],
- 0 => [
- 'name' => ['image' => 'scratch.text'],
- 'type' => ['image' => 'text/plain'],
- 'tmp_name' => ['image' => __FILE__],
- 'error' => ['image' => 0],
- 'size' => ['image' => 1490],
- ],
- 'pictures' => [
- 'name' => [
- 0 => ['file' => 'a-file.png'],
- 1 => ['file' => 'a-moose.png'],
- ],
- 'type' => [
- 0 => ['file' => 'image/png'],
- 1 => ['file' => 'image/jpg'],
- ],
- 'tmp_name' => [
- 0 => ['file' => __FILE__],
- 1 => ['file' => __FILE__],
- ],
- 'error' => [
- 0 => ['file' => 0],
- 1 => ['file' => 0],
- ],
- 'size' => [
- 0 => ['file' => 17188],
- 1 => ['file' => 2010],
- ],
- ],
- ];
- $post = [
- 'pictures' => [
- 0 => ['name' => 'A cat'],
- 1 => ['name' => 'A moose'],
- ],
- 0 => [
- 'name' => 'A dog',
- ],
- ];
- $request = ServerRequestFactory::fromGlobals(null, null, $post, null, $files);
- $expected = [
- 'image_main' => [
- 'file' => new UploadedFile(
- __FILE__,
- 17178,
- 0,
- 'born on.txt',
- 'text/plain'
- ),
- ],
- 'pictures' => [
- 0 => [
- 'name' => 'A cat',
- 'file' => new UploadedFile(
- __FILE__,
- 17188,
- 0,
- 'a-file.png',
- 'image/png'
- ),
- ],
- 1 => [
- 'name' => 'A moose',
- 'file' => new UploadedFile(
- __FILE__,
- 2010,
- 0,
- 'a-moose.png',
- 'image/jpg'
- ),
- ],
- ],
- 0 => [
- 'name' => 'A dog',
- 'image' => new UploadedFile(
- __FILE__,
- 1490,
- 0,
- 'scratch.text',
- 'text/plain'
- ),
- ],
- ];
- $this->assertEquals($expected, $request->getData());
- $uploads = $request->getUploadedFiles();
- $this->assertCount(3, $uploads);
- $this->assertArrayHasKey(0, $uploads);
- $this->assertSame('scratch.text', $uploads[0]['image']->getClientFilename());
- $this->assertArrayHasKey('pictures', $uploads);
- $this->assertSame('a-file.png', $uploads['pictures'][0]['file']->getClientFilename());
- $this->assertSame('a-moose.png', $uploads['pictures'][1]['file']->getClientFilename());
- $this->assertArrayHasKey('image_main', $uploads);
- $this->assertSame('born on.txt', $uploads['image_main']['file']->getClientFilename());
- }
- /**
- * Test processing a file input with no .'s in it.
- */
- public function testFilesFlat(): void
- {
- $files = [
- 'birth_cert' => [
- 'name' => 'born on.txt',
- 'type' => 'application/octet-stream',
- 'tmp_name' => __FILE__,
- 'error' => 0,
- 'size' => 123,
- ],
- ];
- Configure::write('App.uploadedFilesAsObjects', false);
- $request = ServerRequestFactory::fromGlobals([], [], [], [], $files);
- $this->assertEquals($files, $request->getData());
- Configure::write('App.uploadedFilesAsObjects', true);
- $uploads = $request->getUploadedFiles();
- $this->assertCount(1, $uploads);
- $this->assertArrayHasKey('birth_cert', $uploads);
- $this->assertSame('born on.txt', $uploads['birth_cert']->getClientFilename());
- $this->assertSame(0, $uploads['birth_cert']->getError());
- $this->assertSame('application/octet-stream', $uploads['birth_cert']->getClientMediaType());
- $this->assertSame(123, $uploads['birth_cert']->getSize());
- }
- /**
- * Test that files in the 0th index work.
- */
- public function testFilesZeroithIndex(): void
- {
- $files = [
- 0 => [
- 'name' => 'cake_sqlserver_patch.patch',
- 'type' => 'text/plain',
- 'tmp_name' => __FILE__,
- 'error' => 0,
- 'size' => 6271,
- ],
- ];
- Configure::write('App.uploadedFilesAsObjects', false);
- $request = ServerRequestFactory::fromGlobals([], [], [], [], $files);
- $this->assertEquals($files, $request->getData());
- Configure::write('App.uploadedFilesAsObjects', true);
- $uploads = $request->getUploadedFiles();
- $this->assertCount(1, $uploads);
- $this->assertSame($files[0]['name'], $uploads[0]->getClientFilename());
- }
- /**
- * Tests that file uploads are merged into the post data as objects instead of as arrays.
- */
- public function testFilesAsObjectsInRequestData(): void
- {
- $files = [
- 'flat' => [
- 'name' => 'flat.txt',
- 'type' => 'text/plain',
- 'tmp_name' => __FILE__,
- 'error' => 0,
- 'size' => 1,
- ],
- 'nested' => [
- 'name' => ['file' => 'nested.txt'],
- 'type' => ['file' => 'text/plain'],
- 'tmp_name' => ['file' => __FILE__],
- 'error' => ['file' => 0],
- 'size' => ['file' => 12],
- ],
- 0 => [
- 'name' => 'numeric.txt',
- 'type' => 'text/plain',
- 'tmp_name' => __FILE__,
- 'error' => 0,
- 'size' => 123,
- ],
- 1 => [
- 'name' => ['file' => 'numeric-nested.txt'],
- 'type' => ['file' => 'text/plain'],
- 'tmp_name' => ['file' => __FILE__],
- 'error' => ['file' => 0],
- 'size' => ['file' => 1234],
- ],
- 'deep' => [
- 'name' => [
- 0 => ['file' => 'deep-1.txt'],
- 1 => ['file' => 'deep-2.txt'],
- ],
- 'type' => [
- 0 => ['file' => 'text/plain'],
- 1 => ['file' => 'text/plain'],
- ],
- 'tmp_name' => [
- 0 => ['file' => __FILE__],
- 1 => ['file' => __FILE__],
- ],
- 'error' => [
- 0 => ['file' => 0],
- 1 => ['file' => 0],
- ],
- 'size' => [
- 0 => ['file' => 12345],
- 1 => ['file' => 123456],
- ],
- ],
- ];
- $post = [
- 'flat' => ['existing'],
- 'nested' => [
- 'name' => 'nested',
- 'file' => ['existing'],
- ],
- 'deep' => [
- 0 => [
- 'name' => 'deep 1',
- 'file' => ['existing'],
- ],
- 1 => [
- 'name' => 'deep 2',
- ],
- ],
- 1 => [
- 'name' => 'numeric nested',
- ],
- ];
- $expected = [
- 'flat' => new UploadedFile(
- __FILE__,
- 1,
- 0,
- 'flat.txt',
- 'text/plain'
- ),
- 'nested' => [
- 'name' => 'nested',
- 'file' => new UploadedFile(
- __FILE__,
- 12,
- 0,
- 'nested.txt',
- 'text/plain'
- ),
- ],
- 'deep' => [
- 0 => [
- 'name' => 'deep 1',
- 'file' => new UploadedFile(
- __FILE__,
- 12345,
- 0,
- 'deep-1.txt',
- 'text/plain'
- ),
- ],
- 1 => [
- 'name' => 'deep 2',
- 'file' => new UploadedFile(
- __FILE__,
- 123456,
- 0,
- 'deep-2.txt',
- 'text/plain'
- ),
- ],
- ],
- 0 => new UploadedFile(
- __FILE__,
- 123,
- 0,
- 'numeric.txt',
- 'text/plain'
- ),
- 1 => [
- 'name' => 'numeric nested',
- 'file' => new UploadedFile(
- __FILE__,
- 1234,
- 0,
- 'numeric-nested.txt',
- 'text/plain'
- ),
- ],
- ];
- $request = ServerRequestFactory::fromGlobals([], [], $post, [], $files);
- $this->assertEquals($expected, $request->getData());
- }
- /**
- * Test passing invalid files list structure.
- */
- public function testFilesWithInvalidStructure(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Invalid value in files specification');
- ServerRequestFactory::fromGlobals([], [], [], [], [
- [
- 'invalid' => [
- 'data',
- ],
- ],
- ]);
- }
- }
|