ServerRequestFactoryTest.php 11 KB

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