ServerRequestFactoryTest.php 11 KB

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