From ce8e373d67bd7b5479935aababfb332cfa3c0689 Mon Sep 17 00:00:00 2001 From: MGatner Date: Tue, 5 Jan 2021 02:59:53 +0000 Subject: [PATCH] Refactor site_url tests with provider --- tests/system/Helpers/URLHelperTest.php | 211 +++++++++++++++---------- 1 file changed, 124 insertions(+), 87 deletions(-) diff --git a/tests/system/Helpers/URLHelperTest.php b/tests/system/Helpers/URLHelperTest.php index 15ec432fb83a..9f1da78ba601 100644 --- a/tests/system/Helpers/URLHelperTest.php +++ b/tests/system/Helpers/URLHelperTest.php @@ -42,112 +42,149 @@ public function tearDown(): void } //-------------------------------------------------------------------- - // Test site_url - public function testSiteURLBasics() - { - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/'); - - Services::injectMock('request', $request); - - $this->assertEquals('http://example.com/index.php', site_url('', null, $this->config)); - } - - 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)); - } - - public function testSiteURLNoTrailingSlash() - { - $this->config->baseURL = 'http://example.com'; - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/index.php'); - - Services::injectMock('request', $request); - - $this->assertEquals('http://example.com/index.php', site_url('', null, $this->config)); - } - - public function testSiteURLNoIndex() - { - $this->config->indexPage = ''; - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/'); - - Services::injectMock('request', $request); - - $this->assertEquals('http://example.com/', site_url('', null, $this->config)); - } - - public function testSiteURLDifferentIndex() - { - $this->config->indexPage = 'banana.php'; - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/'); - - Services::injectMock('request', $request); - - $this->assertEquals('http://example.com/banana.php', site_url('', null, $this->config)); - } - - public function testSiteURLNoIndexButPath() + /** + * @dataProvider siteUrlProvider + */ + public function testSiteUrl($baseURL, $indexPage, $param, $protocol, $expected) { - $this->config->indexPage = ''; - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/'); - - Services::injectMock('request', $request); + // Set the config + $this->config->baseURL = $baseURL; + $this->config->indexPage = $indexPage; - $this->assertEquals('http://example.com/abc', site_url('abc', null, $this->config)); - } - - public function testSiteURLAttachesPath() - { + // Mock the Request $request = Services::request($this->config); $request->uri = new URI('http://example.com/'); - Services::injectMock('request', $request); - $this->assertEquals('http://example.com/index.php/foo', site_url('foo', null, $this->config)); + $this->assertEquals($expected, site_url($param, $protocol, $this->config)); } - public function testSiteURLAttachesScheme() + public function siteUrlProvider() { - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/'); - - Services::injectMock('request', $request); - - $this->assertEquals('ftp://example.com/index.php/foo', site_url('foo', 'ftp', $this->config)); + // 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/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 testSiteURLExample() + public function testSiteURLHTTPS() { - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/'); - - Services::injectMock('request', $request); - - $this->assertEquals('http://example.com/index.php/news/local/123', site_url('news/local/123', null, $this->config)); - } + $_SERVER['HTTPS'] = 'on'; - public function testSiteURLSegments() - { $request = Services::request($this->config); $request->uri = new URI('http://example.com/'); Services::injectMock('request', $request); - $this->assertEquals('http://example.com/index.php/news/local/123', site_url(['news', 'local', '123'], null, $this->config)); + $this->assertEquals('https://example.com/index.php', site_url('', null, $this->config)); } /**