diff --git a/tests/system/Helpers/URLHelper/BaseUrlTest.php b/tests/system/Helpers/URLHelper/BaseUrlTest.php new file mode 100644 index 000000000000..761049da9af4 --- /dev/null +++ b/tests/system/Helpers/URLHelper/BaseUrlTest.php @@ -0,0 +1,260 @@ +config = new App(); + $this->config->baseURL = 'http://example.com/'; + $this->config->indexPage = 'index.php'; + Factories::injectMock('config', 'App', $this->config); + } + + public function tearDown(): void + { + parent::tearDown(); + + $_SERVER = []; + } + + //-------------------------------------------------------------------- + // Test base_url + + public function testBaseURLBasics() + { + $this->assertEquals('http://example.com', base_url()); + } + + public function testBaseURLAttachesPath() + { + $this->assertEquals('http://example.com/foo', base_url('foo')); + } + + public function testBaseURLAttachesPathArray() + { + $this->assertEquals('http://example.com/foo/bar', base_url(['foo', 'bar'])); + } + + public function testBaseURLAttachesScheme() + { + $this->assertEquals('https://example.com/foo', base_url('foo', 'https')); + } + + public function testBaseURLPathZero() + { + $this->assertEquals('http://example.com/0', base_url('0')); + } + + public function testBaseURLHeedsBaseURL() + { + // Since we're on a CLI, we must provide our own URI + $this->config->baseURL = 'http://example.com/public'; + $request = Services::request($this->config); + $request->uri = new URI('http://example.com/public'); + + Services::injectMock('request', $request); + + $this->assertEquals('http://example.com/public', base_url()); + } + + public function testBaseURLNoTrailingSlash() + { + // Since we're on a CLI, we must provide our own URI + $this->config->baseURL = 'http://example.com'; + $request = Services::request($this->config); + $request->uri = new URI('http://example.com/foobar'); + + Services::injectMock('request', $request); + + $this->assertEquals('http://example.com', base_url()); + } + + public function testBaseURLExample() + { + $this->assertEquals('http://example.com/blog/post/123', base_url('blog/post/123')); + } + + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/240 + */ + public function testBaseURLWithSegments() + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/test'; + + // Since we're on a CLI, we must provide our own URI + $request = Services::request($this->config, false); + $request->uri = new URI('http://example.com/test'); + + Services::injectMock('request', $request); + + $this->assertEquals('http://example.com', base_url()); + } + + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/867 + */ + public function testBaseURLHTTPS() + { + $_SERVER['HTTPS'] = 'on'; + + $this->assertEquals('https://example.com/blog/post/123', base_url('blog/post/123')); + } + + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/240 + */ + public function testBaseURLWithSegmentsAgain() + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/test/page'; + + // Since we're on a CLI, we must provide our own URI + $request = Services::request($this->config, false); + $request->uri = new URI('http://example.com/test/page'); + + Services::injectMock('request', $request); + + $this->assertEquals('http://example.com', base_url()); + $this->assertEquals('http://example.com/profile', base_url('profile')); + } + + public function testBaseURLHasSubfolder() + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/subfolder/test'; + $_SERVER['SCRIPT_NAME'] = '/subfolder/index.php'; + + // Since we're on a CLI, we must provide our own URI + $this->config->baseURL = 'http://example.com/subfolder/'; + Factories::injectMock('config', 'App', $this->config); + + $request = Services::request($this->config, false); + Services::injectMock('request', $request); + + $this->assertEquals('http://example.com/subfolder/foo', base_url('foo')); + $this->assertEquals('http://example.com/subfolder', base_url()); + } + + public function testBaseURLNoTrailingSlashHasSubfolder() + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/subfolder/test'; + $_SERVER['SCRIPT_NAME'] = '/subfolder/index.php'; + + // Since we're on a CLI, we must provide our own URI + $this->config->baseURL = 'http://example.com/subfolder'; + Factories::injectMock('config', 'App', $this->config); + + $request = Services::request($this->config, false); + Services::injectMock('request', $request); + + $this->assertEquals('http://example.com/subfolder/foo', base_url('foo')); + $this->assertEquals('http://example.com/subfolder', base_url()); + } + + //-------------------------------------------------------------------- + + public function testBasedNoIndex() + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/ci/v4/x/y'; + + $this->config->baseURL = 'http://example.com/ci/v4/'; + $request = Services::request($this->config); + $request->uri = new URI('http://example.com/ci/v4/x/y'); + + Services::injectMock('request', $request); + + $this->assertEquals('http://example.com/ci/v4/index.php/controller/method', site_url('controller/method', null, $this->config)); + $this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $this->config)); + } + + public function testBasedNoTrailingSlash() + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/ci/v4/x/y'; + + $this->config->baseURL = 'http://example.com/ci/v4'; + $request = Services::request($this->config); + $request->uri = new URI('http://example.com/ci/v4/x/y'); + + Services::injectMock('request', $request); + + $this->assertEquals('http://example.com/ci/v4/index.php/controller/method', site_url('controller/method', null, $this->config)); + $this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $this->config)); + } + + public function testBasedWithIndex() + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/ci/v4/index.php/x/y'; + + $this->config->baseURL = 'http://example.com/ci/v4/'; + $request = Services::request($this->config); + $request->uri = new URI('http://example.com/ci/v4/index.php/x/y'); + + Services::injectMock('request', $request); + + $this->assertEquals('http://example.com/ci/v4/index.php/controller/method', site_url('controller/method', null, $this->config)); + $this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $this->config)); + } + + public function testBasedWithoutIndex() + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/ci/v4/x/y'; + + $this->config->baseURL = 'http://example.com/ci/v4/'; + $this->config->indexPage = ''; + $request = Services::request($this->config); + $request->uri = new URI('http://example.com/ci/v4/x/y'); + + Services::injectMock('request', $request); + + $this->assertEquals('http://example.com/ci/v4/controller/method', site_url('controller/method', null, $this->config)); + $this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $this->config)); + } + + public function testBasedWithOtherIndex() + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/ci/v4/x/y'; + + $this->config->baseURL = 'http://example.com/ci/v4/'; + $this->config->indexPage = 'fc.php'; + $request = Services::request($this->config); + $request->uri = new URI('http://example.com/ci/v4/x/y'); + + Services::injectMock('request', $request); + + $this->assertEquals('http://example.com/ci/v4/fc.php/controller/method', site_url('controller/method', null, $this->config)); + $this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $this->config)); + } +} diff --git a/tests/system/Helpers/URLHelper/CurrentUrlTest.php b/tests/system/Helpers/URLHelper/CurrentUrlTest.php new file mode 100644 index 000000000000..baba2fc4fd49 --- /dev/null +++ b/tests/system/Helpers/URLHelper/CurrentUrlTest.php @@ -0,0 +1,346 @@ +config = new App(); + $this->config->baseURL = 'http://example.com/'; + $this->config->indexPage = 'index.php'; + Factories::injectMock('config', 'App', $this->config); + + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/'; + $_SERVER['SCRIPT_NAME'] = '/index.php'; + } + + public function tearDown(): void + { + parent::tearDown(); + + $_SERVER = []; + } + + //-------------------------------------------------------------------- + // current_url + //-------------------------------------------------------------------- + + public function testCurrentURLReturnsBasicURL() + { + // Since we're on a CLI, we must provide our own URI + $this->config->baseURL = 'http://example.com/public'; + + $this->assertEquals('http://example.com/public/', current_url()); + } + + public function testCurrentURLReturnsObject() + { + // Since we're on a CLI, we must provide our own URI + $this->config->baseURL = 'http://example.com/public'; + + $url = current_url(true); + + $this->assertInstanceOf(URI::class, $url); + $this->assertEquals('http://example.com/public/', (string) $url); + } + + public function testCurrentURLEquivalence() + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/public'; + $_SERVER['SCRIPT_NAME'] = '/index.php'; + + // Since we're on a CLI, we must provide our own URI + Factories::injectMock('config', 'App', $this->config); + + $request = Services::request($this->config); + Services::injectMock('request', $request); + + $this->assertEquals(base_url(uri_string()), current_url()); + } + + public function testCurrentURLInSubfolder() + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/foo/public/bar?baz=quip'; + $_SERVER['SCRIPT_NAME'] = '/foo/public/index.php'; + + // Since we're on a CLI, we must provide our own URI + $this->config->baseURL = 'http://example.com/foo/public'; + Factories::injectMock('config', 'App', $this->config); + + $request = Services::request($this->config); + Services::injectMock('request', $request); + + $this->assertEquals('http://example.com/foo/public/bar', current_url()); + $this->assertEquals('http://example.com/foo/public/bar?baz=quip', (string) current_url(true)); + + $uri = current_url(true); + $this->assertEquals(['bar'], $uri->getSegments()); + $this->assertEquals('bar', $uri->getSegment(1)); + $this->assertEquals('example.com', $uri->getHost()); + $this->assertEquals('http', $uri->getScheme()); + } + + public function testCurrentURLWithPortInSubfolder() + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['SERVER_PORT'] = '8080'; + $_SERVER['REQUEST_URI'] = '/foo/public/bar?baz=quip'; + $_SERVER['SCRIPT_NAME'] = '/foo/public/index.php'; + + // Since we're on a CLI, we must provide our own URI + $this->config->baseURL = 'http://example.com:8080/foo/public'; + Factories::injectMock('config', 'App', $this->config); + + $request = Services::request($this->config); + Services::injectMock('request', $request); + + $this->assertEquals('http://example.com:8080/foo/public/bar', current_url()); + $this->assertEquals('http://example.com:8080/foo/public/bar?baz=quip', (string) current_url(true)); + + $uri = current_url(true); + $this->assertEquals(['bar'], $uri->getSegments()); + $this->assertEquals('bar', $uri->getSegment(1)); + $this->assertEquals('example.com', $uri->getHost()); + $this->assertEquals('http', $uri->getScheme()); + $this->assertEquals('8080', $uri->getPort()); + } + + //-------------------------------------------------------------------- + // uri_string + //-------------------------------------------------------------------- + + public function testUriStringAbsolute() + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/assets/image.jpg'; + + $request = Services::request($this->config); + $request->uri = new URI('http://example.com/assets/image.jpg'); + + Services::injectMock('request', $request); + + $url = current_url(); + $this->assertEquals('/assets/image.jpg', uri_string()); + } + + public function testUriStringRelative() + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/assets/image.jpg'; + + $request = Services::request($this->config); + $request->uri = new URI('http://example.com/assets/image.jpg'); + + Services::injectMock('request', $request); + + $url = current_url(); + $this->assertEquals('assets/image.jpg', uri_string(true)); + } + + public function testUriStringNoTrailingSlashAbsolute() + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/assets/image.jpg'; + + $this->config->baseURL = 'http://example.com'; + $request = Services::request($this->config); + $request->uri = new URI('http://example.com/assets/image.jpg'); + + Services::injectMock('request', $request); + + $url = current_url(); + $this->assertEquals('/assets/image.jpg', uri_string()); + } + + public function testUriStringNoTrailingSlashRelative() + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/assets/image.jpg'; + + $this->config->baseURL = 'http://example.com'; + $request = Services::request($this->config); + $request->uri = new URI('http://example.com/assets/image.jpg'); + + Services::injectMock('request', $request); + + $url = current_url(); + $this->assertEquals('assets/image.jpg', uri_string(true)); + } + + public function testUriStringEmptyAbsolute() + { + $request = Services::request($this->config); + $request->uri = new URI('http://example.com/'); + + Services::injectMock('request', $request); + + $url = current_url(); + $this->assertEquals('/', uri_string()); + } + + public function testUriStringEmptyRelative() + { + $request = Services::request($this->config); + $request->uri = new URI('http://example.com/'); + + Services::injectMock('request', $request); + + $url = current_url(); + $this->assertEquals('', uri_string(true)); + } + + public function testUriStringSubfolderAbsolute() + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/subfolder/assets/image.jpg'; + + $this->config->baseURL = 'http://example.com/subfolder/'; + $request = Services::request($this->config); + $request->uri = new URI('http://example.com/subfolder/assets/image.jpg'); + + Services::injectMock('request', $request); + + $url = current_url(); + $this->assertEquals('/subfolder/assets/image.jpg', uri_string()); + } + + public function testUriStringSubfolderRelative() + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/assets/image.jpg'; + $_SERVER['REQUEST_URI'] = '/subfolder/assets/image.jpg'; + + $this->config->baseURL = 'http://example.com/subfolder/'; + $request = Services::request($this->config); + $request->uri = new URI('http://example.com/subfolder/assets/image.jpg'); + + Services::injectMock('request', $request); + + $url = current_url(); + $this->assertEquals('assets/image.jpg', uri_string(true)); + } + + //-------------------------------------------------------------------- + // uri_is + //-------------------------------------------------------------------- + + public function urlIsProvider() + { + return [ + [ + 'foo/bar', + 'foo/bar', + true, + ], + [ + 'foo/bar', + 'foo*', + true, + ], + [ + 'foo/bar', + 'foo', + false, + ], + [ + 'foo/bar', + 'baz/foo/bar', + false, + ], + [ + '', + 'foo*', + false, + ], + [ + 'foo/', + 'foo*', + true, + ], + [ + 'foo/', + 'foo', + true, + ], + ]; + } + + /** + * @dataProvider urlIsProvider + */ + public function testUrlIs(string $currentPath, string $testPath, bool $expected) + { + $_SERVER['HTTP_HOST'] = 'example.com'; + + $request = Services::request(); + $request->uri = new URI('http://example.com/' . $currentPath); + Services::injectMock('request', $request); + + $this->assertEquals($expected, url_is($testPath)); + } + + /** + * @dataProvider urlIsProvider + */ + public function testUrlIsNoIndex(string $currentPath, string $testPath, bool $expected) + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $this->config->indexPage = ''; + + $request = Services::request($this->config); + $request->uri = new URI('http://example.com/' . $currentPath); + Services::injectMock('request', $request); + + $this->assertEquals($expected, url_is($testPath)); + } + + /** + * @dataProvider urlIsProvider + */ + public function testUrlIsWithSubfolder(string $currentPath, string $testPath, bool $expected) + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $this->config->baseURL = 'http://example.com/subfolder/'; + + $request = Services::request($this->config); + $request->uri = new URI('http://example.com/subfolder/' . $currentPath); + Services::injectMock('request', $request); + + $this->assertEquals($expected, url_is($testPath)); + } +} diff --git a/tests/system/Helpers/URLHelperTest.php b/tests/system/Helpers/URLHelper/MiscUrlTest.php similarity index 59% rename from tests/system/Helpers/URLHelperTest.php rename to tests/system/Helpers/URLHelper/MiscUrlTest.php index b641c863cca5..5c6c732a1185 100644 --- a/tests/system/Helpers/URLHelperTest.php +++ b/tests/system/Helpers/URLHelper/MiscUrlTest.php @@ -1,6 +1,6 @@ config = new App(); $this->config->baseURL = 'http://example.com/'; $this->config->indexPage = 'index.php'; - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/'; - - //Config::injectMock('App', $this->config); + Factories::injectMock('config', 'App', $this->config); } public function tearDown(): void @@ -44,423 +43,6 @@ public function tearDown(): void $_SERVER = []; } - //-------------------------------------------------------------------- - - /** - * @dataProvider siteUrlProvider - */ - public function testSiteUrl($baseURL, $indexPage, $param, $protocol, $expected) - { - // Set the config - $this->config->baseURL = $baseURL; - $this->config->indexPage = $indexPage; - - // Mock the Request - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/'); - Services::injectMock('request', $request); - - $this->assertEquals($expected, site_url($param, $protocol, $this->config)); - } - - public function siteUrlProvider() - { - // baseURL, indexPage, param, protocol, expected - return [ - [ - 'http://example.com/', - 'index.php', - '', - null, - 'http://example.com/index.php', - ], - [ - 'http://example.com', - 'index.php', - '', - null, - 'http://example.com/index.php', - ], - [ - 'http://example.com/', - '', - '', - null, - 'http://example.com/', - ], - [ - 'http://example.com/', - 'banana.php', - '', - null, - 'http://example.com/banana.php', - ], - [ - 'http://example.com/', - '', - 'abc', - null, - 'http://example.com/abc', - ], - [ - 'http://example.com/public/', - 'index.php', - '', - null, - 'http://example.com/public/index.php', - ], - [ - 'http://example.com/public/', - '', - '', - null, - 'http://example.com/public/', - ], - [ - 'http://example.com/public', - '', - '', - null, - 'http://example.com/public/', - ], - [ - 'http://example.com/public', - 'index.php', - '/', - null, - 'http://example.com/public/index.php/', - ], - [ - 'http://example.com/public/', - 'index.php', - '/', - null, - 'http://example.com/public/index.php/', - ], - [ - 'http://example.com/', - 'index.php', - 'foo', - null, - 'http://example.com/index.php/foo', - ], - [ - 'http://example.com/', - 'index.php', - '0', - null, - 'http://example.com/index.php/0', - ], - [ - 'http://example.com/public', - 'index.php', - 'foo', - null, - 'http://example.com/public/index.php/foo', - ], - [ - 'http://example.com/', - 'index.php', - 'foo', - 'ftp', - 'ftp://example.com/index.php/foo', - ], - [ - 'http://example.com/', - 'index.php', - 'news/local/123', - null, - 'http://example.com/index.php/news/local/123', - ], - [ - 'http://example.com/', - 'index.php', - [ - 'news', - 'local', - '123', - ], null, - 'http://example.com/index.php/news/local/123', - ], - ]; - } - - public function testSiteURLHTTPS() - { - $_SERVER['HTTPS'] = 'on'; - - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/'); - - Services::injectMock('request', $request); - - $this->assertEquals('https://example.com/index.php', site_url('', null, $this->config)); - } - - /** - * @see https://github.com/codeigniter4/CodeIgniter4/issues/240 - */ - public function testSiteURLWithSegments() - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/test'; - - // Since we're on a CLI, we must provide our own URI - $request = Services::request($this->config, false); - $request->uri = new URI('http://example.com/test'); - - Services::injectMock('request', $request); - - $this->assertEquals('http://example.com/index.php', site_url()); - } - - /** - * @see https://github.com/codeigniter4/CodeIgniter4/issues/240 - */ - public function testSiteURLWithSegmentsAgain() - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/test/page'; - - // Since we're on a CLI, we must provide our own URI - $request = Services::request($this->config, false); - $request->uri = new URI('http://example.com/test/page'); - - Services::injectMock('request', $request); - - $this->assertEquals('http://example.com/index.php', site_url()); - $this->assertEquals('http://example.com/index.php/profile', site_url('profile')); - } - - //-------------------------------------------------------------------- - // Test base_url - - public function testBaseURLBasics() - { - $this->assertEquals('http://example.com', base_url()); - } - - public function testBaseURLAttachesPath() - { - $this->assertEquals('http://example.com/foo', base_url('foo')); - } - - public function testBaseURLAttachesPathArray() - { - $this->assertEquals('http://example.com/foo/bar', base_url(['foo', 'bar'])); - } - - public function testBaseURLAttachesScheme() - { - $this->assertEquals('https://example.com/foo', base_url('foo', 'https')); - } - - public function testBaseURLPathZero() - { - $this->assertEquals('http://example.com/0', base_url('0')); - } - - public function testBaseURLHeedsBaseURL() - { - // Since we're on a CLI, we must provide our own URI - $this->config->baseURL = 'http://example.com/public'; - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/public'); - - Services::injectMock('request', $request); - - $this->assertEquals('http://example.com/public', base_url()); - } - - public function testBaseURLNoTrailingSlash() - { - // Since we're on a CLI, we must provide our own URI - $this->config->baseURL = 'http://example.com'; - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/foobar'); - - Services::injectMock('request', $request); - - $this->assertEquals('http://example.com', base_url()); - } - - public function testBaseURLExample() - { - $this->assertEquals('http://example.com/blog/post/123', base_url('blog/post/123')); - } - - /** - * @see https://github.com/codeigniter4/CodeIgniter4/issues/240 - */ - public function testBaseURLWithSegments() - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/test'; - - // Since we're on a CLI, we must provide our own URI - $request = Services::request($this->config, false); - $request->uri = new URI('http://example.com/test'); - - Services::injectMock('request', $request); - - $this->assertEquals('http://example.com', base_url()); - } - - /** - * @see https://github.com/codeigniter4/CodeIgniter4/issues/867 - */ - public function testBaseURLHTTPS() - { - $_SERVER['HTTPS'] = 'on'; - - $this->assertEquals('https://example.com/blog/post/123', base_url('blog/post/123')); - } - - /** - * @see https://github.com/codeigniter4/CodeIgniter4/issues/240 - */ - public function testBaseURLWithSegmentsAgain() - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/test/page'; - - // Since we're on a CLI, we must provide our own URI - $request = Services::request($this->config, false); - $request->uri = new URI('http://example.com/test/page'); - - Services::injectMock('request', $request); - - $this->assertEquals('http://example.com', base_url()); - $this->assertEquals('http://example.com/profile', base_url('profile')); - } - - public function testBaseURLHasSubfolder() - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/subfolder/test'; - $_SERVER['SCRIPT_NAME'] = '/subfolder/index.php'; - - // Since we're on a CLI, we must provide our own URI - $this->config->baseURL = 'http://example.com/subfolder/'; - Factories::injectMock('config', 'App', $this->config); - - $request = Services::request($this->config, false); - Services::injectMock('request', $request); - - $this->assertEquals('http://example.com/subfolder/foo', base_url('foo')); - $this->assertEquals('http://example.com/subfolder', base_url()); - } - - public function testBaseURLNoTrailingSlashHasSubfolder() - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/subfolder/test'; - $_SERVER['SCRIPT_NAME'] = '/subfolder/index.php'; - - // Since we're on a CLI, we must provide our own URI - $this->config->baseURL = 'http://example.com/subfolder'; - Factories::injectMock('config', 'App', $this->config); - - $request = Services::request($this->config, false); - Services::injectMock('request', $request); - - $this->assertEquals('http://example.com/subfolder/foo', base_url('foo')); - $this->assertEquals('http://example.com/subfolder', base_url()); - } - - //-------------------------------------------------------------------- - // Test current_url - - public function testCurrentURLReturnsBasicURL() - { - // Since we're on a CLI, we must provide our own URI - $this->config->baseURL = 'http://example.com/public'; - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/public'); - - Services::injectMock('request', $request); - - $this->assertEquals('http://example.com/public', current_url()); - } - - public function testCurrentURLReturnsObject() - { - // Since we're on a CLI, we must provide our own URI - $this->config->baseURL = 'http://example.com/public'; - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/public'); - - Services::injectMock('request', $request); - - $url = current_url(true); - - $this->assertInstanceOf(URI::class, $url); - $this->assertEquals('http://example.com/public', (string) $url); - } - - public function testCurrentURLEquivalence() - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/public'; - $_SERVER['SCRIPT_NAME'] = '/index.php'; - - // Since we're on a CLI, we must provide our own URI - Factories::injectMock('config', 'App', $this->config); - - $request = Services::request($this->config); - Services::injectMock('request', $request); - - $this->assertEquals(base_url(uri_string()), current_url()); - } - - public function testCurrentURLInSubfolder() - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/foo/public/bar?baz=quip'; - $_SERVER['SCRIPT_NAME'] = '/foo/public/index.php'; - - // Since we're on a CLI, we must provide our own URI - $this->config->baseURL = 'http://example.com/foo/public'; - Factories::injectMock('config', 'App', $this->config); - - $request = Services::request($this->config); - Services::injectMock('request', $request); - - $this->assertEquals('http://example.com/foo/public/bar', current_url()); - $this->assertEquals('http://example.com/foo/public/bar?baz=quip', (string) current_url(true)); - - $uri = current_url(true); - $this->assertEquals(['bar'], $uri->getSegments()); - $this->assertEquals('bar', $uri->getSegment(1)); - $this->assertEquals('example.com', $uri->getHost()); - $this->assertEquals('http', $uri->getScheme()); - } - - public function testCurrentURLWithPortInSubfolder() - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['SERVER_PORT'] = '8080'; - $_SERVER['REQUEST_URI'] = '/foo/public/bar?baz=quip'; - $_SERVER['SCRIPT_NAME'] = '/foo/public/index.php'; - - // Since we're on a CLI, we must provide our own URI - $this->config->baseURL = 'http://example.com:8080/foo/public'; - Factories::injectMock('config', 'App', $this->config); - - $request = Services::request($this->config); - Services::injectMock('request', $request); - - $this->assertEquals('http://example.com:8080/foo/public/bar', current_url()); - $this->assertEquals('http://example.com:8080/foo/public/bar?baz=quip', (string) current_url(true)); - - $uri = current_url(true); - $this->assertEquals(['bar'], $uri->getSegments()); - $this->assertEquals('bar', $uri->getSegment(1)); - $this->assertEquals('example.com', $uri->getHost()); - $this->assertEquals('http', $uri->getScheme()); - $this->assertEquals('8080', $uri->getPort()); - } - //-------------------------------------------------------------------- // Test previous_url @@ -501,120 +83,6 @@ public function testPreviousURLUsesRefererIfNeeded() $this->assertEquals($uri1, previous_url()); } - //-------------------------------------------------------------------- - // Test uri_string - - public function testUriStringAbsolute() - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/assets/image.jpg'; - - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/assets/image.jpg'); - - Services::injectMock('request', $request); - - $url = current_url(); - $this->assertEquals('/assets/image.jpg', uri_string()); - } - - public function testUriStringRelative() - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/assets/image.jpg'; - - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/assets/image.jpg'); - - Services::injectMock('request', $request); - - $url = current_url(); - $this->assertEquals('assets/image.jpg', uri_string(true)); - } - - public function testUriStringNoTrailingSlashAbsolute() - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/assets/image.jpg'; - - $this->config->baseURL = 'http://example.com'; - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/assets/image.jpg'); - - Services::injectMock('request', $request); - - $url = current_url(); - $this->assertEquals('/assets/image.jpg', uri_string()); - } - - public function testUriStringNoTrailingSlashRelative() - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/assets/image.jpg'; - - $this->config->baseURL = 'http://example.com'; - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/assets/image.jpg'); - - Services::injectMock('request', $request); - - $url = current_url(); - $this->assertEquals('assets/image.jpg', uri_string(true)); - } - - public function testUriStringEmptyAbsolute() - { - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/'); - - Services::injectMock('request', $request); - - $url = current_url(); - $this->assertEquals('/', uri_string()); - } - - public function testUriStringEmptyRelative() - { - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/'); - - Services::injectMock('request', $request); - - $url = current_url(); - $this->assertEquals('', uri_string(true)); - } - - public function testUriStringSubfolderAbsolute() - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/subfolder/assets/image.jpg'; - - $this->config->baseURL = 'http://example.com/subfolder/'; - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/subfolder/assets/image.jpg'); - - Services::injectMock('request', $request); - - $url = current_url(); - $this->assertEquals('/subfolder/assets/image.jpg', uri_string()); - } - - public function testUriStringSubfolderRelative() - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/assets/image.jpg'; - $_SERVER['REQUEST_URI'] = '/subfolder/assets/image.jpg'; - - $this->config->baseURL = 'http://example.com/subfolder/'; - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/subfolder/assets/image.jpg'); - - Services::injectMock('request', $request); - - $url = current_url(); - $this->assertEquals('assets/image.jpg', uri_string(true)); - } - //-------------------------------------------------------------------- // Test index_page @@ -1319,85 +787,6 @@ public function testMbUrlTitleExtraDashes() } } - //-------------------------------------------------------------------- - - public function testBasedNoIndex() - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/ci/v4/x/y'; - - $this->config->baseURL = 'http://example.com/ci/v4/'; - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/ci/v4/x/y'); - - Services::injectMock('request', $request); - - $this->assertEquals('http://example.com/ci/v4/index.php/controller/method', site_url('controller/method', null, $this->config)); - $this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $this->config)); - } - - public function testBasedNoTrailingSlash() - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/ci/v4/x/y'; - - $this->config->baseURL = 'http://example.com/ci/v4'; - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/ci/v4/x/y'); - - Services::injectMock('request', $request); - - $this->assertEquals('http://example.com/ci/v4/index.php/controller/method', site_url('controller/method', null, $this->config)); - $this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $this->config)); - } - - public function testBasedWithIndex() - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/ci/v4/index.php/x/y'; - - $this->config->baseURL = 'http://example.com/ci/v4/'; - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/ci/v4/index.php/x/y'); - - Services::injectMock('request', $request); - - $this->assertEquals('http://example.com/ci/v4/index.php/controller/method', site_url('controller/method', null, $this->config)); - $this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $this->config)); - } - - public function testBasedWithoutIndex() - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/ci/v4/x/y'; - - $this->config->baseURL = 'http://example.com/ci/v4/'; - $this->config->indexPage = ''; - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/ci/v4/x/y'); - - Services::injectMock('request', $request); - - $this->assertEquals('http://example.com/ci/v4/controller/method', site_url('controller/method', null, $this->config)); - $this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $this->config)); - } - - public function testBasedWithOtherIndex() - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/ci/v4/x/y'; - - $this->config->baseURL = 'http://example.com/ci/v4/'; - $this->config->indexPage = 'fc.php'; - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/ci/v4/x/y'); - - Services::injectMock('request', $request); - - $this->assertEquals('http://example.com/ci/v4/fc.php/controller/method', site_url('controller/method', null, $this->config)); - $this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $this->config)); - } - /** * @dataProvider urlToProvider */ @@ -1453,89 +842,4 @@ public function urlToMissingRoutesProvider() ], ]; } - - public function urlIsProvider() - { - return [ - [ - 'foo/bar', - 'foo/bar', - true, - ], - [ - 'foo/bar', - 'foo*', - true, - ], - [ - 'foo/bar', - 'foo', - false, - ], - [ - 'foo/bar', - 'baz/foo/bar', - false, - ], - [ - '', - 'foo*', - false, - ], - [ - 'foo/', - 'foo*', - true, - ], - [ - 'foo/', - 'foo', - true, - ], - ]; - } - - /** - * @dataProvider urlIsProvider - */ - public function testUrlIs(string $currentPath, string $testPath, bool $expected) - { - $_SERVER['HTTP_HOST'] = 'example.com'; - - $request = Services::request(); - $request->uri = new URI('http://example.com/' . $currentPath); - Services::injectMock('request', $request); - - $this->assertEquals($expected, url_is($testPath)); - } - - /** - * @dataProvider urlIsProvider - */ - public function testUrlIsNoIndex(string $currentPath, string $testPath, bool $expected) - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $this->config->indexPage = ''; - - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/' . $currentPath); - Services::injectMock('request', $request); - - $this->assertEquals($expected, url_is($testPath)); - } - - /** - * @dataProvider urlIsProvider - */ - public function testUrlIsWithSubfolder(string $currentPath, string $testPath, bool $expected) - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $this->config->baseURL = 'http://example.com/subfolder/'; - - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/subfolder/' . $currentPath); - Services::injectMock('request', $request); - - $this->assertEquals($expected, url_is($testPath)); - } } diff --git a/tests/system/Helpers/URLHelper/SiteUrlTest.php b/tests/system/Helpers/URLHelper/SiteUrlTest.php new file mode 100644 index 000000000000..af4235370c6e --- /dev/null +++ b/tests/system/Helpers/URLHelper/SiteUrlTest.php @@ -0,0 +1,233 @@ +config = new App(); + $this->config->baseURL = 'http://example.com/'; + $this->config->indexPage = 'index.php'; + Factories::injectMock('config', 'App', $this->config); + } + + public function tearDown(): void + { + parent::tearDown(); + + $_SERVER = []; + } + + //-------------------------------------------------------------------- + + /** + * @dataProvider siteUrlProvider + */ + public function testSiteUrl($baseURL, $indexPage, $param, $protocol, $expected) + { + // Set the config + $this->config->baseURL = $baseURL; + $this->config->indexPage = $indexPage; + + // Mock the Request + $request = Services::request($this->config); + $request->uri = new URI('http://example.com/'); + Services::injectMock('request', $request); + + $this->assertEquals($expected, site_url($param, $protocol, $this->config)); + } + + public function siteUrlProvider() + { + // baseURL, indexPage, param, protocol, expected + return [ + [ + 'http://example.com/', + 'index.php', + '', + null, + 'http://example.com/index.php', + ], + [ + 'http://example.com', + 'index.php', + '', + null, + 'http://example.com/index.php', + ], + [ + 'http://example.com/', + '', + '', + null, + 'http://example.com/', + ], + [ + 'http://example.com/', + 'banana.php', + '', + null, + 'http://example.com/banana.php', + ], + [ + 'http://example.com/', + '', + 'abc', + null, + 'http://example.com/abc', + ], + [ + 'http://example.com/public/', + 'index.php', + '', + null, + 'http://example.com/public/index.php', + ], + [ + 'http://example.com/public/', + '', + '', + null, + 'http://example.com/public/', + ], + [ + 'http://example.com/public', + '', + '', + null, + 'http://example.com/public/', + ], + [ + 'http://example.com/public', + 'index.php', + '/', + null, + 'http://example.com/public/index.php/', + ], + [ + 'http://example.com/public/', + 'index.php', + '/', + null, + 'http://example.com/public/index.php/', + ], + [ + 'http://example.com/', + 'index.php', + 'foo', + null, + 'http://example.com/index.php/foo', + ], + [ + 'http://example.com/', + 'index.php', + '0', + null, + 'http://example.com/index.php/0', + ], + [ + 'http://example.com/public', + 'index.php', + 'foo', + null, + 'http://example.com/public/index.php/foo', + ], + [ + 'http://example.com/', + 'index.php', + 'foo', + 'ftp', + 'ftp://example.com/index.php/foo', + ], + [ + 'http://example.com/', + 'index.php', + 'news/local/123', + null, + 'http://example.com/index.php/news/local/123', + ], + [ + 'http://example.com/', + 'index.php', + [ + 'news', + 'local', + '123', + ], null, + 'http://example.com/index.php/news/local/123', + ], + ]; + } + + public function testSiteURLHTTPS() + { + $_SERVER['HTTPS'] = 'on'; + + $request = Services::request($this->config); + $request->uri = new URI('http://example.com/'); + + Services::injectMock('request', $request); + + $this->assertEquals('https://example.com/index.php', site_url('', null, $this->config)); + } + + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/240 + */ + public function testSiteURLWithSegments() + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/test'; + + // Since we're on a CLI, we must provide our own URI + $request = Services::request($this->config, false); + $request->uri = new URI('http://example.com/test'); + + Services::injectMock('request', $request); + + $this->assertEquals('http://example.com/index.php', site_url()); + } + + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/240 + */ + public function testSiteURLWithSegmentsAgain() + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/test/page'; + + // Since we're on a CLI, we must provide our own URI + $request = Services::request($this->config, false); + $request->uri = new URI('http://example.com/test/page'); + + Services::injectMock('request', $request); + + $this->assertEquals('http://example.com/index.php', site_url()); + $this->assertEquals('http://example.com/index.php/profile', site_url('profile')); + } +}