ServerRequestFactoryTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. * @runInSeparateProcess
  115. * @return void
  116. */
  117. public function testFromGlobalsUrlSession()
  118. {
  119. Configure::write('App.base', '/basedir');
  120. $server = [
  121. 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
  122. 'PHP_SELF' => '/index.php',
  123. 'REQUEST_URI' => '/posts/add',
  124. ];
  125. $res = ServerRequestFactory::fromGlobals($server);
  126. $session = $res->getAttribute('session');
  127. $this->assertInstanceOf('Cake\Network\Session', $session);
  128. $this->assertEquals('/basedir/', ini_get('session.cookie_path'), 'Needs trailing / for cookie to work');
  129. }
  130. /**
  131. * Test fromGlobals with App.base defined.
  132. *
  133. * @return void
  134. */
  135. public function testFromGlobalsUrlBaseDefined()
  136. {
  137. Configure::write('App.base', 'basedir');
  138. $server = [
  139. 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
  140. 'PHP_SELF' => '/index.php',
  141. 'REQUEST_URI' => '/posts/add',
  142. ];
  143. $res = ServerRequestFactory::fromGlobals($server);
  144. $this->assertEquals('basedir', $res->getAttribute('base'));
  145. $this->assertEquals('basedir/', $res->getAttribute('webroot'));
  146. $this->assertEquals('/posts/add', $res->getUri()->getPath());
  147. }
  148. /**
  149. * Test fromGlobals with mod-rewrite server configuration.
  150. *
  151. * @return void
  152. */
  153. public function testFromGlobalsUrlModRewrite()
  154. {
  155. Configure::write('App.baseUrl', false);
  156. $server = [
  157. 'DOCUMENT_ROOT' => '/cake/repo/branches',
  158. 'PHP_SELF' => '/urlencode me/webroot/index.php',
  159. 'REQUEST_URI' => '/posts/view/1',
  160. ];
  161. $res = ServerRequestFactory::fromGlobals($server);
  162. $this->assertEquals('/urlencode%20me', $res->getAttribute('base'));
  163. $this->assertEquals('/urlencode%20me/', $res->getAttribute('webroot'));
  164. $this->assertEquals('/posts/view/1', $res->getUri()->getPath());
  165. }
  166. /**
  167. * Test fromGlobals with mod-rewrite in the root dir.
  168. *
  169. * @return void
  170. */
  171. public function testFromGlobalsUrlModRewriteRootDir()
  172. {
  173. $server = [
  174. 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
  175. 'PHP_SELF' => '/index.php',
  176. 'REQUEST_URI' => '/posts/add',
  177. ];
  178. $res = ServerRequestFactory::fromGlobals($server);
  179. $this->assertEquals('', $res->getAttribute('base'));
  180. $this->assertEquals('/', $res->getAttribute('webroot'));
  181. $this->assertEquals('/posts/add', $res->getUri()->getPath());
  182. }
  183. /**
  184. * Test fromGlobals with App.baseUrl defined implying no
  185. * mod-rewrite and no virtual path.
  186. *
  187. * @return void
  188. */
  189. public function testFromGlobalsUrlNoModRewriteWebrootDir()
  190. {
  191. Configure::write('App', [
  192. 'dir' => 'app',
  193. 'webroot' => 'webroot',
  194. 'base' => false,
  195. 'baseUrl' => '/cake/index.php'
  196. ]);
  197. $server = [
  198. 'DOCUMENT_ROOT' => '/Users/markstory/Sites',
  199. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/webroot/index.php',
  200. 'PHP_SELF' => '/cake/webroot/index.php/posts/index',
  201. 'REQUEST_URI' => '/cake/webroot/index.php',
  202. ];
  203. $res = ServerRequestFactory::fromGlobals($server);
  204. $this->assertSame('/cake/webroot/', $res->getAttribute('webroot'));
  205. $this->assertSame('/cake/index.php', $res->getAttribute('base'));
  206. $this->assertSame('/', $res->getUri()->getPath());
  207. }
  208. /**
  209. * Test fromGlobals with App.baseUrl defined implying no
  210. * mod-rewrite
  211. *
  212. * @return void
  213. */
  214. public function testFromGlobalsUrlNoModRewrite()
  215. {
  216. Configure::write('App', [
  217. 'dir' => 'app',
  218. 'webroot' => 'webroot',
  219. 'base' => false,
  220. 'baseUrl' => '/cake/index.php'
  221. ]);
  222. $server = [
  223. 'DOCUMENT_ROOT' => '/Users/markstory/Sites',
  224. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/index.php',
  225. 'PHP_SELF' => '/cake/index.php/posts/index',
  226. 'REQUEST_URI' => '/cake/index.php/posts/index',
  227. ];
  228. $res = ServerRequestFactory::fromGlobals($server);
  229. $this->assertSame('/cake/webroot/', $res->getAttribute('webroot'));
  230. $this->assertSame('/cake/index.php', $res->getAttribute('base'));
  231. $this->assertSame('/posts/index', $res->getUri()->getPath());
  232. }
  233. /**
  234. * Test fromGlobals with App.baseUrl defined implying no
  235. * mod-rewrite in the root dir.
  236. *
  237. * @return void
  238. */
  239. public function testFromGlobalsUrlNoModRewriteRootDir()
  240. {
  241. Configure::write('App', [
  242. 'dir' => 'cake',
  243. 'webroot' => 'webroot',
  244. 'base' => false,
  245. 'baseUrl' => '/index.php'
  246. ]);
  247. $server = [
  248. 'DOCUMENT_ROOT' => '/Users/markstory/Sites/cake',
  249. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/index.php',
  250. 'PHP_SELF' => '/index.php/posts/add',
  251. 'REQUEST_URI' => '/index.php/posts/add',
  252. ];
  253. $res = ServerRequestFactory::fromGlobals($server);
  254. $this->assertEquals('/webroot/', $res->getAttribute('webroot'));
  255. $this->assertEquals('/index.php', $res->getAttribute('base'));
  256. $this->assertEquals('/posts/add', $res->getUri()->getPath());
  257. }
  258. }