ServerRequestFactoryTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 with App.base defined.
  46. *
  47. * @return void
  48. */
  49. public function testFromGlobalsUrlBaseDefined()
  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. $this->assertEquals('basedir', $res->getAttribute('base'));
  59. $this->assertEquals('basedir/', $res->getAttribute('webroot'));
  60. $this->assertEquals('/posts/add', $res->getUri()->getPath());
  61. }
  62. /**
  63. * Test fromGlobals with mod-rewrite server configuration.
  64. *
  65. * @return void
  66. */
  67. public function testFromGlobalsUrlModRewrite()
  68. {
  69. Configure::write('App.baseUrl', false);
  70. $server = [
  71. 'DOCUMENT_ROOT' => '/cake/repo/branches',
  72. 'PHP_SELF' => '/urlencode me/webroot/index.php',
  73. 'REQUEST_URI' => '/posts/view/1',
  74. ];
  75. $res = ServerRequestFactory::fromGlobals($server);
  76. $this->assertEquals('/urlencode%20me', $res->getAttribute('base'));
  77. $this->assertEquals('/urlencode%20me/', $res->getAttribute('webroot'));
  78. $this->assertEquals('/posts/view/1', $res->getUri()->getPath());
  79. }
  80. /**
  81. * Test fromGlobals with mod-rewrite in the root dir.
  82. *
  83. * @return void
  84. */
  85. public function testFromGlobalsUrlModRewriteRootDir()
  86. {
  87. $server = [
  88. 'DOCUMENT_ROOT' => '/cake/repo/branches/1.2.x.x/webroot',
  89. 'PHP_SELF' => '/index.php',
  90. 'REQUEST_URI' => '/posts/add',
  91. ];
  92. $res = ServerRequestFactory::fromGlobals($server);
  93. $this->assertEquals('', $res->getAttribute('base'));
  94. $this->assertEquals('/', $res->getAttribute('webroot'));
  95. $this->assertEquals('/posts/add', $res->getUri()->getPath());
  96. }
  97. /**
  98. * Test fromGlobals with App.baseUrl defined implying no
  99. * mod-rewrite and no virtual path.
  100. *
  101. * @return void
  102. */
  103. public function testFromGlobalsUrlNoModRewriteWebrootDir()
  104. {
  105. Configure::write('App', [
  106. 'dir' => 'app',
  107. 'webroot' => 'webroot',
  108. 'base' => false,
  109. 'baseUrl' => '/cake/index.php'
  110. ]);
  111. $server = [
  112. 'DOCUMENT_ROOT' => '/Users/markstory/Sites',
  113. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/webroot/index.php',
  114. 'PHP_SELF' => '/cake/webroot/index.php/posts/index',
  115. 'REQUEST_URI' => '/cake/webroot/index.php',
  116. ];
  117. $res = ServerRequestFactory::fromGlobals($server);
  118. $this->assertSame('/cake/webroot/', $res->getAttribute('webroot'));
  119. $this->assertSame('/cake/index.php', $res->getAttribute('base'));
  120. $this->assertSame('/', $res->getUri()->getPath());
  121. }
  122. /**
  123. * Test fromGlobals with App.baseUrl defined implying no
  124. * mod-rewrite
  125. *
  126. * @return void
  127. */
  128. public function testFromGlobalsUrlNoModRewrite()
  129. {
  130. Configure::write('App', [
  131. 'dir' => 'app',
  132. 'webroot' => 'webroot',
  133. 'base' => false,
  134. 'baseUrl' => '/cake/index.php'
  135. ]);
  136. $server = [
  137. 'DOCUMENT_ROOT' => '/Users/markstory/Sites',
  138. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/index.php',
  139. 'PHP_SELF' => '/cake/index.php/posts/index',
  140. 'REQUEST_URI' => '/cake/index.php/posts/index',
  141. ];
  142. $res = ServerRequestFactory::fromGlobals($server);
  143. $this->assertSame('/cake/webroot/', $res->getAttribute('webroot'));
  144. $this->assertSame('/cake/index.php', $res->getAttribute('base'));
  145. $this->assertSame('/posts/index', $res->getUri()->getPath());
  146. }
  147. /**
  148. * Test fromGlobals with App.baseUrl defined implying no
  149. * mod-rewrite in the root dir.
  150. *
  151. * @return void
  152. */
  153. public function testFromGlobalsUrlNoModRewriteRootDir()
  154. {
  155. Configure::write('App', [
  156. 'dir' => 'cake',
  157. 'webroot' => 'webroot',
  158. 'base' => false,
  159. 'baseUrl' => '/index.php'
  160. ]);
  161. $server = [
  162. 'DOCUMENT_ROOT' => '/Users/markstory/Sites/cake',
  163. 'SCRIPT_FILENAME' => '/Users/markstory/Sites/cake/index.php',
  164. 'PHP_SELF' => '/index.php/posts/add',
  165. 'REQUEST_URI' => '/index.php/posts/add',
  166. ];
  167. $res = ServerRequestFactory::fromGlobals($server);
  168. $this->assertEquals('/webroot/', $res->getAttribute('webroot'));
  169. $this->assertEquals('/index.php', $res->getAttribute('base'));
  170. $this->assertEquals('/posts/add', $res->getUri()->getPath());
  171. }
  172. }