|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CodeIgniter\Helpers\URLHelper; |
| 4 | + |
| 5 | +use CodeIgniter\Config\Factories; |
| 6 | +use CodeIgniter\Config\Services; |
| 7 | +use CodeIgniter\HTTP\URI; |
| 8 | +use CodeIgniter\Router\Exceptions\RouterException; |
| 9 | +use CodeIgniter\Test\CIUnitTestCase; |
| 10 | +use Config\App; |
| 11 | + |
| 12 | +/** |
| 13 | + * @backupGlobals enabled |
| 14 | + */ |
| 15 | +final class BaseUrlTest extends CIUnitTestCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var App |
| 19 | + */ |
| 20 | + private $config; |
| 21 | + |
| 22 | + public static function setUpBeforeClass(): void |
| 23 | + { |
| 24 | + parent::setUpBeforeClass(); |
| 25 | + |
| 26 | + helper('url'); |
| 27 | + } |
| 28 | + |
| 29 | + protected function setUp(): void |
| 30 | + { |
| 31 | + parent::setUp(); |
| 32 | + |
| 33 | + Services::reset(true); |
| 34 | + |
| 35 | + // Set a common base configuration (overriden by individual tests) |
| 36 | + $this->config = new App(); |
| 37 | + $this->config->baseURL = 'http://example.com/'; |
| 38 | + $this->config->indexPage = 'index.php'; |
| 39 | + Factories::injectMock('config', 'App', $this->config); |
| 40 | + } |
| 41 | + |
| 42 | + public function tearDown(): void |
| 43 | + { |
| 44 | + parent::tearDown(); |
| 45 | + |
| 46 | + $_SERVER = []; |
| 47 | + } |
| 48 | + |
| 49 | + //-------------------------------------------------------------------- |
| 50 | + // Test base_url |
| 51 | + |
| 52 | + public function testBaseURLBasics() |
| 53 | + { |
| 54 | + $this->assertEquals('http://example.com', base_url()); |
| 55 | + } |
| 56 | + |
| 57 | + public function testBaseURLAttachesPath() |
| 58 | + { |
| 59 | + $this->assertEquals('http://example.com/foo', base_url('foo')); |
| 60 | + } |
| 61 | + |
| 62 | + public function testBaseURLAttachesPathArray() |
| 63 | + { |
| 64 | + $this->assertEquals('http://example.com/foo/bar', base_url(['foo', 'bar'])); |
| 65 | + } |
| 66 | + |
| 67 | + public function testBaseURLAttachesScheme() |
| 68 | + { |
| 69 | + $this->assertEquals('https://example.com/foo', base_url('foo', 'https')); |
| 70 | + } |
| 71 | + |
| 72 | + public function testBaseURLPathZero() |
| 73 | + { |
| 74 | + $this->assertEquals('http://example.com/0', base_url('0')); |
| 75 | + } |
| 76 | + |
| 77 | + public function testBaseURLHeedsBaseURL() |
| 78 | + { |
| 79 | + // Since we're on a CLI, we must provide our own URI |
| 80 | + $this->config->baseURL = 'http://example.com/public'; |
| 81 | + $request = Services::request($this->config); |
| 82 | + $request->uri = new URI('http://example.com/public'); |
| 83 | + |
| 84 | + Services::injectMock('request', $request); |
| 85 | + |
| 86 | + $this->assertEquals('http://example.com/public', base_url()); |
| 87 | + } |
| 88 | + |
| 89 | + public function testBaseURLNoTrailingSlash() |
| 90 | + { |
| 91 | + // Since we're on a CLI, we must provide our own URI |
| 92 | + $this->config->baseURL = 'http://example.com'; |
| 93 | + $request = Services::request($this->config); |
| 94 | + $request->uri = new URI('http://example.com/foobar'); |
| 95 | + |
| 96 | + Services::injectMock('request', $request); |
| 97 | + |
| 98 | + $this->assertEquals('http://example.com', base_url()); |
| 99 | + } |
| 100 | + |
| 101 | + public function testBaseURLExample() |
| 102 | + { |
| 103 | + $this->assertEquals('http://example.com/blog/post/123', base_url('blog/post/123')); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @see https://github.com/codeigniter4/CodeIgniter4/issues/240 |
| 108 | + */ |
| 109 | + public function testBaseURLWithSegments() |
| 110 | + { |
| 111 | + $_SERVER['HTTP_HOST'] = 'example.com'; |
| 112 | + $_SERVER['REQUEST_URI'] = '/test'; |
| 113 | + |
| 114 | + // Since we're on a CLI, we must provide our own URI |
| 115 | + $request = Services::request($this->config, false); |
| 116 | + $request->uri = new URI('http://example.com/test'); |
| 117 | + |
| 118 | + Services::injectMock('request', $request); |
| 119 | + |
| 120 | + $this->assertEquals('http://example.com', base_url()); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @see https://github.com/codeigniter4/CodeIgniter4/issues/867 |
| 125 | + */ |
| 126 | + public function testBaseURLHTTPS() |
| 127 | + { |
| 128 | + $_SERVER['HTTPS'] = 'on'; |
| 129 | + |
| 130 | + $this->assertEquals('https://example.com/blog/post/123', base_url('blog/post/123')); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @see https://github.com/codeigniter4/CodeIgniter4/issues/240 |
| 135 | + */ |
| 136 | + public function testBaseURLWithSegmentsAgain() |
| 137 | + { |
| 138 | + $_SERVER['HTTP_HOST'] = 'example.com'; |
| 139 | + $_SERVER['REQUEST_URI'] = '/test/page'; |
| 140 | + |
| 141 | + // Since we're on a CLI, we must provide our own URI |
| 142 | + $request = Services::request($this->config, false); |
| 143 | + $request->uri = new URI('http://example.com/test/page'); |
| 144 | + |
| 145 | + Services::injectMock('request', $request); |
| 146 | + |
| 147 | + $this->assertEquals('http://example.com', base_url()); |
| 148 | + $this->assertEquals('http://example.com/profile', base_url('profile')); |
| 149 | + } |
| 150 | + |
| 151 | + public function testBaseURLHasSubfolder() |
| 152 | + { |
| 153 | + $_SERVER['HTTP_HOST'] = 'example.com'; |
| 154 | + $_SERVER['REQUEST_URI'] = '/subfolder/test'; |
| 155 | + $_SERVER['SCRIPT_NAME'] = '/subfolder/index.php'; |
| 156 | + |
| 157 | + // Since we're on a CLI, we must provide our own URI |
| 158 | + $this->config->baseURL = 'http://example.com/subfolder/'; |
| 159 | + Factories::injectMock('config', 'App', $this->config); |
| 160 | + |
| 161 | + $request = Services::request($this->config, false); |
| 162 | + Services::injectMock('request', $request); |
| 163 | + |
| 164 | + $this->assertEquals('http://example.com/subfolder/foo', base_url('foo')); |
| 165 | + $this->assertEquals('http://example.com/subfolder', base_url()); |
| 166 | + } |
| 167 | + |
| 168 | + public function testBaseURLNoTrailingSlashHasSubfolder() |
| 169 | + { |
| 170 | + $_SERVER['HTTP_HOST'] = 'example.com'; |
| 171 | + $_SERVER['REQUEST_URI'] = '/subfolder/test'; |
| 172 | + $_SERVER['SCRIPT_NAME'] = '/subfolder/index.php'; |
| 173 | + |
| 174 | + // Since we're on a CLI, we must provide our own URI |
| 175 | + $this->config->baseURL = 'http://example.com/subfolder'; |
| 176 | + Factories::injectMock('config', 'App', $this->config); |
| 177 | + |
| 178 | + $request = Services::request($this->config, false); |
| 179 | + Services::injectMock('request', $request); |
| 180 | + |
| 181 | + $this->assertEquals('http://example.com/subfolder/foo', base_url('foo')); |
| 182 | + $this->assertEquals('http://example.com/subfolder', base_url()); |
| 183 | + } |
| 184 | + |
| 185 | + //-------------------------------------------------------------------- |
| 186 | + |
| 187 | + public function testBasedNoIndex() |
| 188 | + { |
| 189 | + $_SERVER['HTTP_HOST'] = 'example.com'; |
| 190 | + $_SERVER['REQUEST_URI'] = '/ci/v4/x/y'; |
| 191 | + |
| 192 | + $this->config->baseURL = 'http://example.com/ci/v4/'; |
| 193 | + $request = Services::request($this->config); |
| 194 | + $request->uri = new URI('http://example.com/ci/v4/x/y'); |
| 195 | + |
| 196 | + Services::injectMock('request', $request); |
| 197 | + |
| 198 | + $this->assertEquals('http://example.com/ci/v4/index.php/controller/method', site_url('controller/method', null, $this->config)); |
| 199 | + $this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $this->config)); |
| 200 | + } |
| 201 | + |
| 202 | + public function testBasedNoTrailingSlash() |
| 203 | + { |
| 204 | + $_SERVER['HTTP_HOST'] = 'example.com'; |
| 205 | + $_SERVER['REQUEST_URI'] = '/ci/v4/x/y'; |
| 206 | + |
| 207 | + $this->config->baseURL = 'http://example.com/ci/v4'; |
| 208 | + $request = Services::request($this->config); |
| 209 | + $request->uri = new URI('http://example.com/ci/v4/x/y'); |
| 210 | + |
| 211 | + Services::injectMock('request', $request); |
| 212 | + |
| 213 | + $this->assertEquals('http://example.com/ci/v4/index.php/controller/method', site_url('controller/method', null, $this->config)); |
| 214 | + $this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $this->config)); |
| 215 | + } |
| 216 | + |
| 217 | + public function testBasedWithIndex() |
| 218 | + { |
| 219 | + $_SERVER['HTTP_HOST'] = 'example.com'; |
| 220 | + $_SERVER['REQUEST_URI'] = '/ci/v4/index.php/x/y'; |
| 221 | + |
| 222 | + $this->config->baseURL = 'http://example.com/ci/v4/'; |
| 223 | + $request = Services::request($this->config); |
| 224 | + $request->uri = new URI('http://example.com/ci/v4/index.php/x/y'); |
| 225 | + |
| 226 | + Services::injectMock('request', $request); |
| 227 | + |
| 228 | + $this->assertEquals('http://example.com/ci/v4/index.php/controller/method', site_url('controller/method', null, $this->config)); |
| 229 | + $this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $this->config)); |
| 230 | + } |
| 231 | + |
| 232 | + public function testBasedWithoutIndex() |
| 233 | + { |
| 234 | + $_SERVER['HTTP_HOST'] = 'example.com'; |
| 235 | + $_SERVER['REQUEST_URI'] = '/ci/v4/x/y'; |
| 236 | + |
| 237 | + $this->config->baseURL = 'http://example.com/ci/v4/'; |
| 238 | + $this->config->indexPage = ''; |
| 239 | + $request = Services::request($this->config); |
| 240 | + $request->uri = new URI('http://example.com/ci/v4/x/y'); |
| 241 | + |
| 242 | + Services::injectMock('request', $request); |
| 243 | + |
| 244 | + $this->assertEquals('http://example.com/ci/v4/controller/method', site_url('controller/method', null, $this->config)); |
| 245 | + $this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $this->config)); |
| 246 | + } |
| 247 | + |
| 248 | + public function testBasedWithOtherIndex() |
| 249 | + { |
| 250 | + $_SERVER['HTTP_HOST'] = 'example.com'; |
| 251 | + $_SERVER['REQUEST_URI'] = '/ci/v4/x/y'; |
| 252 | + |
| 253 | + $this->config->baseURL = 'http://example.com/ci/v4/'; |
| 254 | + $this->config->indexPage = 'fc.php'; |
| 255 | + $request = Services::request($this->config); |
| 256 | + $request->uri = new URI('http://example.com/ci/v4/x/y'); |
| 257 | + |
| 258 | + Services::injectMock('request', $request); |
| 259 | + |
| 260 | + $this->assertEquals('http://example.com/ci/v4/fc.php/controller/method', site_url('controller/method', null, $this->config)); |
| 261 | + $this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $this->config)); |
| 262 | + } |
| 263 | +} |
0 commit comments