ServerRequestFactoryTest.php 8.2 KB

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