ServerRequestFactoryTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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\UploadedFile;
  22. use Psr\Http\Message\UploadedFileInterface;
  23. /**
  24. * Test case for the server factory.
  25. */
  26. class ServerRequestFactoryTest extends TestCase
  27. {
  28. /**
  29. * @var array|null
  30. */
  31. protected $server;
  32. /**
  33. * @var array|null
  34. */
  35. protected $post;
  36. /**
  37. * @var array|null
  38. */
  39. protected $files;
  40. /**
  41. * @var array|null
  42. */
  43. protected $cookies;
  44. /**
  45. * @var array|null
  46. */
  47. protected $get;
  48. /**
  49. * setup
  50. *
  51. * @return void
  52. */
  53. public function setUp(): void
  54. {
  55. parent::setUp();
  56. $this->server = $_SERVER;
  57. $this->post = $_POST;
  58. $this->files = $_FILES;
  59. $this->cookies = $_COOKIE;
  60. $this->get = $_GET;
  61. }
  62. /**
  63. * teardown
  64. *
  65. * @return void
  66. */
  67. public function tearDown(): void
  68. {
  69. parent::tearDown();
  70. $_SERVER = $this->server;
  71. $_POST = $this->post;
  72. $_FILES = $this->files;
  73. $_COOKIE = $this->cookies;
  74. $_GET = $this->get;
  75. }
  76. /**
  77. * Test fromGlobals reads super globals
  78. *
  79. * @return void
  80. */
  81. public function testFromGlobalsSuperGlobals()
  82. {
  83. $_POST = [
  84. 'title' => 'custom',
  85. ];
  86. $_FILES = [
  87. 'image' => [
  88. 'tmp_name' => __FILE__,
  89. 'error' => 0,
  90. 'name' => 'cats.png',
  91. 'type' => 'image/png',
  92. 'size' => 2112,
  93. ],
  94. ];
  95. $_COOKIE = ['key' => 'value'];
  96. $_GET = ['query' => 'string'];
  97. $res = ServerRequestFactory::fromGlobals();
  98. $this->assertSame($_COOKIE['key'], $res->getCookie('key'));
  99. $this->assertSame($_GET['query'], $res->getQuery('query'));
  100. $this->assertArrayHasKey('title', $res->getData());
  101. $this->assertArrayHasKey('image', $res->getData());
  102. $this->assertCount(1, $res->getUploadedFiles());
  103. /** @var \Laminas\Diactoros\UploadedFile $expected */
  104. $expected = $res->getData('image');
  105. $this->assertInstanceOf(UploadedFileInterface::class, $expected);
  106. $this->assertSame($_FILES['image']['size'], $expected->getSize());
  107. $this->assertSame($_FILES['image']['error'], $expected->getError());
  108. $this->assertSame($_FILES['image']['name'], $expected->getClientFilename());
  109. $this->assertSame($_FILES['image']['type'], $expected->getClientMediaType());
  110. }
  111. /**
  112. * Test fromGlobals input
  113. *
  114. * @return void
  115. */
  116. public function testFromGlobalsInput()
  117. {
  118. $res = ServerRequestFactory::fromGlobals();
  119. $this->assertSame('', $res->input());
  120. }
  121. /**
  122. * Test fromGlobals includes the session
  123. *
  124. * @preserveGlobalState disabled
  125. * @runInSeparateProcess
  126. * @return void
  127. */
  128. public function testFromGlobalsUrlSession()
  129. {
  130. Configure::write('App.base', '/basedir');
  131. $server = [
  132. 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
  133. 'PHP_SELF' => '/index.php',
  134. 'REQUEST_URI' => '/posts/add',
  135. ];
  136. $res = ServerRequestFactory::fromGlobals($server);
  137. $session = $res->getAttribute('session');
  138. $this->assertInstanceOf(Session::class, $session);
  139. $this->assertSame('/basedir/', ini_get('session.cookie_path'), 'Needs trailing / for cookie to work');
  140. }
  141. /**
  142. * Test fromGlobals with App.base defined.
  143. *
  144. * @return void
  145. */
  146. public function testFromGlobalsUrlBaseDefined()
  147. {
  148. Configure::write('App.base', 'basedir');
  149. $server = [
  150. 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
  151. 'PHP_SELF' => '/index.php',
  152. 'REQUEST_URI' => '/posts/add',
  153. ];
  154. $res = ServerRequestFactory::fromGlobals($server);
  155. $this->assertSame('basedir', $res->getAttribute('base'));
  156. $this->assertSame('basedir/', $res->getAttribute('webroot'));
  157. $this->assertSame('/posts/add', $res->getUri()->getPath());
  158. }
  159. /**
  160. * Test fromGlobals with mod-rewrite server configuration.
  161. *
  162. * @return void
  163. */
  164. public function testFromGlobalsUrlModRewrite()
  165. {
  166. Configure::write('App.baseUrl', false);
  167. $server = [
  168. 'DOCUMENT_ROOT' => '/cake/repo/branches',
  169. 'PHP_SELF' => '/urlencode me/webroot/index.php',
  170. 'REQUEST_URI' => '/posts/view/1',
  171. ];
  172. $res = ServerRequestFactory::fromGlobals($server);
  173. $this->assertSame('/urlencode%20me', $res->getAttribute('base'));
  174. $this->assertSame('/urlencode%20me/', $res->getAttribute('webroot'));
  175. $this->assertSame('/posts/view/1', $res->getUri()->getPath());
  176. }
  177. /**
  178. * Test fromGlobals with mod-rewrite in the root dir.
  179. *
  180. * @return void
  181. */
  182. public function testFromGlobalsUrlModRewriteRootDir()
  183. {
  184. $server = [
  185. 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
  186. 'PHP_SELF' => '/index.php',
  187. 'REQUEST_URI' => '/posts/add',
  188. ];
  189. $res = ServerRequestFactory::fromGlobals($server);
  190. $this->assertSame('', $res->getAttribute('base'));
  191. $this->assertSame('/', $res->getAttribute('webroot'));
  192. $this->assertSame('/posts/add', $res->getUri()->getPath());
  193. }
  194. /**
  195. * Test fromGlobals with App.baseUrl defined implying no
  196. * mod-rewrite and no virtual path.
  197. *
  198. * @return void
  199. */
  200. public function testFromGlobalsUrlNoModRewriteWebrootDir()
  201. {
  202. Configure::write('App', [
  203. 'dir' => 'app',
  204. 'webroot' => 'www',
  205. 'base' => false,
  206. 'baseUrl' => '/cake/index.php',
  207. ]);
  208. $server = [
  209. 'DOCUMENT_ROOT' => '/Users/markstory/Sites',
  210. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/www/index.php',
  211. 'PHP_SELF' => '/cake/www/index.php/posts/index',
  212. 'REQUEST_URI' => '/cake/www/index.php',
  213. ];
  214. $res = ServerRequestFactory::fromGlobals($server);
  215. $this->assertSame('/cake/www/', $res->getAttribute('webroot'));
  216. $this->assertSame('/cake/index.php', $res->getAttribute('base'));
  217. $this->assertSame('/', $res->getUri()->getPath());
  218. }
  219. /**
  220. * Test fromGlobals with App.baseUrl defined implying no
  221. * mod-rewrite
  222. *
  223. * @return void
  224. */
  225. public function testFromGlobalsUrlNoModRewrite()
  226. {
  227. Configure::write('App', [
  228. 'dir' => 'app',
  229. 'webroot' => 'webroot',
  230. 'base' => false,
  231. 'baseUrl' => '/cake/index.php',
  232. ]);
  233. $server = [
  234. 'DOCUMENT_ROOT' => '/Users/markstory/Sites',
  235. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/index.php',
  236. 'PHP_SELF' => '/cake/index.php/posts/index',
  237. 'REQUEST_URI' => '/cake/index.php/posts/index',
  238. ];
  239. $res = ServerRequestFactory::fromGlobals($server);
  240. $this->assertSame('/cake/webroot/', $res->getAttribute('webroot'));
  241. $this->assertSame('/cake/index.php', $res->getAttribute('base'));
  242. $this->assertSame('/posts/index', $res->getUri()->getPath());
  243. }
  244. /**
  245. * Test fromGlobals with App.baseUrl defined implying no
  246. * mod-rewrite in the root dir.
  247. *
  248. * @return void
  249. */
  250. public function testFromGlobalsUrlNoModRewriteRootDir()
  251. {
  252. Configure::write('App', [
  253. 'dir' => 'cake',
  254. 'webroot' => 'webroot',
  255. 'base' => false,
  256. 'baseUrl' => '/index.php',
  257. ]);
  258. $server = [
  259. 'DOCUMENT_ROOT' => '/Users/markstory/Sites/cake',
  260. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/index.php',
  261. 'PHP_SELF' => '/index.php/posts/add',
  262. 'REQUEST_URI' => '/index.php/posts/add',
  263. ];
  264. $res = ServerRequestFactory::fromGlobals($server);
  265. $this->assertSame('/webroot/', $res->getAttribute('webroot'));
  266. $this->assertSame('/index.php', $res->getAttribute('base'));
  267. $this->assertSame('/posts/add', $res->getUri()->getPath());
  268. }
  269. /**
  270. * Tests the default file upload merging behavior.
  271. *
  272. * @return void
  273. */
  274. public function testFromGlobalsWithFilesAsObjectsDefault()
  275. {
  276. $this->assertNull(Configure::read('App.uploadedFilesAsObjects'));
  277. $files = [
  278. 'file' => [
  279. 'name' => 'file.txt',
  280. 'type' => 'text/plain',
  281. 'tmp_name' => __FILE__,
  282. 'error' => 0,
  283. 'size' => 1234,
  284. ],
  285. ];
  286. $request = ServerRequestFactory::fromGlobals(null, null, null, null, $files);
  287. /** @var \Laminas\Diactoros\UploadedFile $expected */
  288. $expected = $request->getData('file');
  289. $this->assertSame($files['file']['size'], $expected->getSize());
  290. $this->assertSame($files['file']['error'], $expected->getError());
  291. $this->assertSame($files['file']['name'], $expected->getClientFilename());
  292. $this->assertSame($files['file']['type'], $expected->getClientMediaType());
  293. }
  294. /**
  295. * Tests the "as arrays" file upload merging behavior.
  296. *
  297. * @return void
  298. */
  299. public function testFromGlobalsWithFilesAsObjectsDisabled()
  300. {
  301. Configure::write('App.uploadedFilesAsObjects', false);
  302. $files = [
  303. 'file' => [
  304. 'name' => 'file.txt',
  305. 'type' => 'text/plain',
  306. 'tmp_name' => __FILE__,
  307. 'error' => 0,
  308. 'size' => 1234,
  309. ],
  310. ];
  311. $request = ServerRequestFactory::fromGlobals(null, null, null, null, $files);
  312. $expected = [
  313. 'file' => [
  314. 'tmp_name' => __FILE__,
  315. 'error' => 0,
  316. 'name' => 'file.txt',
  317. 'type' => 'text/plain',
  318. 'size' => 1234,
  319. ],
  320. ];
  321. $this->assertEquals($expected, $request->getData());
  322. }
  323. /**
  324. * Tests the "as objects" file upload merging behavior.
  325. *
  326. * @return void
  327. */
  328. public function testFromGlobalsWithFilesAsObjectsEnabled()
  329. {
  330. Configure::write('App.uploadedFilesAsObjects', true);
  331. $files = [
  332. 'file' => [
  333. 'name' => 'file.txt',
  334. 'type' => 'text/plain',
  335. 'tmp_name' => __FILE__,
  336. 'error' => 0,
  337. 'size' => 1234,
  338. ],
  339. ];
  340. $request = ServerRequestFactory::fromGlobals(null, null, null, null, $files);
  341. $expected = [
  342. 'file' => new UploadedFile(
  343. __FILE__,
  344. 1234,
  345. 0,
  346. 'file.txt',
  347. 'text/plain'
  348. ),
  349. ];
  350. $this->assertEquals($expected, $request->getData());
  351. }
  352. }