ServerRequestFactoryTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. /**
  21. * Test case for the server factory.
  22. */
  23. class ServerRequestFactoryTest extends TestCase
  24. {
  25. /**
  26. * @var array|null
  27. */
  28. protected $server = null;
  29. /**
  30. * @var array|null
  31. */
  32. protected $post = null;
  33. /**
  34. * @var array|null
  35. */
  36. protected $files = null;
  37. /**
  38. * @var array|null
  39. */
  40. protected $cookies = null;
  41. /**
  42. * @var array|null
  43. */
  44. protected $get = null;
  45. /**
  46. * setup
  47. *
  48. * @return void
  49. */
  50. public function setUp()
  51. {
  52. parent::setUp();
  53. $this->server = $_SERVER;
  54. $this->post = $_POST;
  55. $this->files = $_FILES;
  56. $this->cookies = $_COOKIE;
  57. $this->get = $_GET;
  58. }
  59. /**
  60. * teardown
  61. *
  62. * @return void
  63. */
  64. public function tearDown()
  65. {
  66. parent::tearDown();
  67. $_SERVER = $this->server;
  68. $_POST = $this->post;
  69. $_FILES = $this->files;
  70. $_COOKIE = $this->cookies;
  71. $_GET = $this->get;
  72. }
  73. /**
  74. * Test fromGlobals reads super globals
  75. *
  76. * @return void
  77. */
  78. public function testFromGlobalsSuperGlobals()
  79. {
  80. $_POST = [
  81. 'title' => 'custom'
  82. ];
  83. $_FILES = [
  84. 'image' => [
  85. 'tmp_name' => __FILE__,
  86. 'error' => 0,
  87. 'name' => 'cats.png',
  88. 'type' => 'image/png',
  89. 'size' => 2112
  90. ]
  91. ];
  92. $_COOKIE = ['key' => 'value'];
  93. $_GET = ['query' => 'string'];
  94. $res = ServerRequestFactory::fromGlobals();
  95. $this->assertSame($_COOKIE['key'], $res->getCookie('key'));
  96. $this->assertSame($_GET['query'], $res->getQuery('query'));
  97. $this->assertArrayHasKey('title', $res->getData());
  98. $this->assertArrayHasKey('image', $res->getData());
  99. $this->assertSame($_FILES['image'], $res->getData('image'));
  100. $this->assertCount(1, $res->getUploadedFiles());
  101. }
  102. /**
  103. * Test fromGlobals input
  104. *
  105. * @return void
  106. */
  107. public function testFromGlobalsInput()
  108. {
  109. $res = ServerRequestFactory::fromGlobals();
  110. $this->assertSame('', $res->input());
  111. }
  112. /**
  113. * Test fromGlobals includes the session
  114. *
  115. * @preserveGlobalState disabled
  116. * @runInSeparateProcess
  117. * @return void
  118. */
  119. public function testFromGlobalsUrlSession()
  120. {
  121. Configure::write('App.base', '/basedir');
  122. $server = [
  123. 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
  124. 'PHP_SELF' => '/index.php',
  125. 'REQUEST_URI' => '/posts/add',
  126. ];
  127. $res = ServerRequestFactory::fromGlobals($server);
  128. $session = $res->getAttribute('session');
  129. $this->assertInstanceOf(Session::class, $session);
  130. $this->assertEquals('/basedir/', ini_get('session.cookie_path'), 'Needs trailing / for cookie to work');
  131. }
  132. /**
  133. * Test fromGlobals with App.base defined.
  134. *
  135. * @return void
  136. */
  137. public function testFromGlobalsUrlBaseDefined()
  138. {
  139. Configure::write('App.base', 'basedir');
  140. $server = [
  141. 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
  142. 'PHP_SELF' => '/index.php',
  143. 'REQUEST_URI' => '/posts/add',
  144. ];
  145. $res = ServerRequestFactory::fromGlobals($server);
  146. $this->assertEquals('basedir', $res->getAttribute('base'));
  147. $this->assertEquals('basedir/', $res->getAttribute('webroot'));
  148. $this->assertEquals('/posts/add', $res->getUri()->getPath());
  149. }
  150. /**
  151. * Test fromGlobals with mod-rewrite server configuration.
  152. *
  153. * @return void
  154. */
  155. public function testFromGlobalsUrlModRewrite()
  156. {
  157. Configure::write('App.baseUrl', false);
  158. $server = [
  159. 'DOCUMENT_ROOT' => '/cake/repo/branches',
  160. 'PHP_SELF' => '/urlencode me/webroot/index.php',
  161. 'REQUEST_URI' => '/posts/view/1',
  162. ];
  163. $res = ServerRequestFactory::fromGlobals($server);
  164. $this->assertEquals('/urlencode%20me', $res->getAttribute('base'));
  165. $this->assertEquals('/urlencode%20me/', $res->getAttribute('webroot'));
  166. $this->assertEquals('/posts/view/1', $res->getUri()->getPath());
  167. }
  168. /**
  169. * Test fromGlobals with mod-rewrite in the root dir.
  170. *
  171. * @return void
  172. */
  173. public function testFromGlobalsUrlModRewriteRootDir()
  174. {
  175. $server = [
  176. 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
  177. 'PHP_SELF' => '/index.php',
  178. 'REQUEST_URI' => '/posts/add',
  179. ];
  180. $res = ServerRequestFactory::fromGlobals($server);
  181. $this->assertEquals('', $res->getAttribute('base'));
  182. $this->assertEquals('/', $res->getAttribute('webroot'));
  183. $this->assertEquals('/posts/add', $res->getUri()->getPath());
  184. }
  185. /**
  186. * Test fromGlobals with App.baseUrl defined implying no
  187. * mod-rewrite and no virtual path.
  188. *
  189. * @return void
  190. */
  191. public function testFromGlobalsUrlNoModRewriteWebrootDir()
  192. {
  193. Configure::write('App', [
  194. 'dir' => 'app',
  195. 'webroot' => 'webroot',
  196. 'base' => false,
  197. 'baseUrl' => '/cake/index.php'
  198. ]);
  199. $server = [
  200. 'DOCUMENT_ROOT' => '/Users/markstory/Sites',
  201. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/webroot/index.php',
  202. 'PHP_SELF' => '/cake/webroot/index.php/posts/index',
  203. 'REQUEST_URI' => '/cake/webroot/index.php',
  204. ];
  205. $res = ServerRequestFactory::fromGlobals($server);
  206. $this->assertSame('/cake/webroot/', $res->getAttribute('webroot'));
  207. $this->assertSame('/cake/index.php', $res->getAttribute('base'));
  208. $this->assertSame('/', $res->getUri()->getPath());
  209. }
  210. /**
  211. * Test fromGlobals with App.baseUrl defined implying no
  212. * mod-rewrite
  213. *
  214. * @return void
  215. */
  216. public function testFromGlobalsUrlNoModRewrite()
  217. {
  218. Configure::write('App', [
  219. 'dir' => 'app',
  220. 'webroot' => 'webroot',
  221. 'base' => false,
  222. 'baseUrl' => '/cake/index.php'
  223. ]);
  224. $server = [
  225. 'DOCUMENT_ROOT' => '/Users/markstory/Sites',
  226. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/index.php',
  227. 'PHP_SELF' => '/cake/index.php/posts/index',
  228. 'REQUEST_URI' => '/cake/index.php/posts/index',
  229. ];
  230. $res = ServerRequestFactory::fromGlobals($server);
  231. $this->assertSame('/cake/webroot/', $res->getAttribute('webroot'));
  232. $this->assertSame('/cake/index.php', $res->getAttribute('base'));
  233. $this->assertSame('/posts/index', $res->getUri()->getPath());
  234. }
  235. /**
  236. * Test fromGlobals with App.baseUrl defined implying no
  237. * mod-rewrite in the root dir.
  238. *
  239. * @return void
  240. */
  241. public function testFromGlobalsUrlNoModRewriteRootDir()
  242. {
  243. Configure::write('App', [
  244. 'dir' => 'cake',
  245. 'webroot' => 'webroot',
  246. 'base' => false,
  247. 'baseUrl' => '/index.php'
  248. ]);
  249. $server = [
  250. 'DOCUMENT_ROOT' => '/Users/markstory/Sites/cake',
  251. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/index.php',
  252. 'PHP_SELF' => '/index.php/posts/add',
  253. 'REQUEST_URI' => '/index.php/posts/add',
  254. ];
  255. $res = ServerRequestFactory::fromGlobals($server);
  256. $this->assertEquals('/webroot/', $res->getAttribute('webroot'));
  257. $this->assertEquals('/index.php', $res->getAttribute('base'));
  258. $this->assertEquals('/posts/add', $res->getUri()->getPath());
  259. }
  260. }