ServerRequestFactoryTest.php 8.3 KB

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