ServerRequestFactoryTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. * setup
  26. *
  27. * @return void
  28. */
  29. public function setUp()
  30. {
  31. parent::setUp();
  32. $this->server = $_SERVER;
  33. }
  34. /**
  35. * teardown
  36. *
  37. * @return void
  38. */
  39. public function tearDown()
  40. {
  41. parent::tearDown();
  42. $_SERVER = $this->server;
  43. }
  44. /**
  45. * Test fromGlobals includes the session
  46. *
  47. * @return void
  48. */
  49. public function testFromGlobalsUrlSession()
  50. {
  51. Configure::write('App.base', '/basedir');
  52. $server = [
  53. 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
  54. 'PHP_SELF' => '/index.php',
  55. 'REQUEST_URI' => '/posts/add',
  56. ];
  57. $res = ServerRequestFactory::fromGlobals($server);
  58. $session = $res->getAttribute('session');
  59. $this->assertInstanceOf('Cake\Network\Session', $session);
  60. $this->assertEquals('/basedir/', ini_get('session.cookie_path'), 'Needs trailing / for cookie to work');
  61. }
  62. /**
  63. * Test fromGlobals with App.base defined.
  64. *
  65. * @return void
  66. */
  67. public function testFromGlobalsUrlBaseDefined()
  68. {
  69. Configure::write('App.base', 'basedir');
  70. $server = [
  71. 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
  72. 'PHP_SELF' => '/index.php',
  73. 'REQUEST_URI' => '/posts/add',
  74. ];
  75. $res = ServerRequestFactory::fromGlobals($server);
  76. $this->assertEquals('basedir', $res->getAttribute('base'));
  77. $this->assertEquals('basedir/', $res->getAttribute('webroot'));
  78. $this->assertEquals('/posts/add', $res->getUri()->getPath());
  79. }
  80. /**
  81. * Test fromGlobals with mod-rewrite server configuration.
  82. *
  83. * @return void
  84. */
  85. public function testFromGlobalsUrlModRewrite()
  86. {
  87. Configure::write('App.baseUrl', false);
  88. $server = [
  89. 'DOCUMENT_ROOT' => '/cake/repo/branches',
  90. 'PHP_SELF' => '/urlencode me/webroot/index.php',
  91. 'REQUEST_URI' => '/posts/view/1',
  92. ];
  93. $res = ServerRequestFactory::fromGlobals($server);
  94. $this->assertEquals('/urlencode%20me', $res->getAttribute('base'));
  95. $this->assertEquals('/urlencode%20me/', $res->getAttribute('webroot'));
  96. $this->assertEquals('/posts/view/1', $res->getUri()->getPath());
  97. }
  98. /**
  99. * Test fromGlobals with mod-rewrite in the root dir.
  100. *
  101. * @return void
  102. */
  103. public function testFromGlobalsUrlModRewriteRootDir()
  104. {
  105. $server = [
  106. 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
  107. 'PHP_SELF' => '/index.php',
  108. 'REQUEST_URI' => '/posts/add',
  109. ];
  110. $res = ServerRequestFactory::fromGlobals($server);
  111. $this->assertEquals('', $res->getAttribute('base'));
  112. $this->assertEquals('/', $res->getAttribute('webroot'));
  113. $this->assertEquals('/posts/add', $res->getUri()->getPath());
  114. }
  115. /**
  116. * Test fromGlobals with App.baseUrl defined implying no
  117. * mod-rewrite and no virtual path.
  118. *
  119. * @return void
  120. */
  121. public function testFromGlobalsUrlNoModRewriteWebrootDir()
  122. {
  123. Configure::write('App', [
  124. 'dir' => 'app',
  125. 'webroot' => 'webroot',
  126. 'base' => false,
  127. 'baseUrl' => '/cake/index.php'
  128. ]);
  129. $server = [
  130. 'DOCUMENT_ROOT' => '/Users/markstory/Sites',
  131. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/webroot/index.php',
  132. 'PHP_SELF' => '/cake/webroot/index.php/posts/index',
  133. 'REQUEST_URI' => '/cake/webroot/index.php',
  134. ];
  135. $res = ServerRequestFactory::fromGlobals($server);
  136. $this->assertSame('/cake/webroot/', $res->getAttribute('webroot'));
  137. $this->assertSame('/cake/index.php', $res->getAttribute('base'));
  138. $this->assertSame('/', $res->getUri()->getPath());
  139. }
  140. /**
  141. * Test fromGlobals with App.baseUrl defined implying no
  142. * mod-rewrite
  143. *
  144. * @return void
  145. */
  146. public function testFromGlobalsUrlNoModRewrite()
  147. {
  148. Configure::write('App', [
  149. 'dir' => 'app',
  150. 'webroot' => 'webroot',
  151. 'base' => false,
  152. 'baseUrl' => '/cake/index.php'
  153. ]);
  154. $server = [
  155. 'DOCUMENT_ROOT' => '/Users/markstory/Sites',
  156. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/index.php',
  157. 'PHP_SELF' => '/cake/index.php/posts/index',
  158. 'REQUEST_URI' => '/cake/index.php/posts/index',
  159. ];
  160. $res = ServerRequestFactory::fromGlobals($server);
  161. $this->assertSame('/cake/webroot/', $res->getAttribute('webroot'));
  162. $this->assertSame('/cake/index.php', $res->getAttribute('base'));
  163. $this->assertSame('/posts/index', $res->getUri()->getPath());
  164. }
  165. /**
  166. * Test fromGlobals with App.baseUrl defined implying no
  167. * mod-rewrite in the root dir.
  168. *
  169. * @return void
  170. */
  171. public function testFromGlobalsUrlNoModRewriteRootDir()
  172. {
  173. Configure::write('App', [
  174. 'dir' => 'cake',
  175. 'webroot' => 'webroot',
  176. 'base' => false,
  177. 'baseUrl' => '/index.php'
  178. ]);
  179. $server = [
  180. 'DOCUMENT_ROOT' => '/Users/markstory/Sites/cake',
  181. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/index.php',
  182. 'PHP_SELF' => '/index.php/posts/add',
  183. 'REQUEST_URI' => '/index.php/posts/add',
  184. ];
  185. $res = ServerRequestFactory::fromGlobals($server);
  186. $this->assertEquals('/webroot/', $res->getAttribute('webroot'));
  187. $this->assertEquals('/index.php', $res->getAttribute('base'));
  188. $this->assertEquals('/posts/add', $res->getUri()->getPath());
  189. }
  190. }