Skip to content

Commit

Permalink
Merge pull request #4063 from MGatner/siteurl-test
Browse files Browse the repository at this point in the history
site_url tests
  • Loading branch information
MGatner authored Jan 5, 2021
2 parents f76017b + ce8e373 commit 6b990ac
Showing 1 changed file with 124 additions and 87 deletions.
211 changes: 124 additions & 87 deletions tests/system/Helpers/URLHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down

0 comments on commit 6b990ac

Please sign in to comment.