Skip to content

Commit 39def32

Browse files
committed
Add URI & url_helper tests
1 parent 064d85b commit 39def32

3 files changed

Lines changed: 134 additions & 5 deletions

File tree

tests/system/HTTP/URITest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace CodeIgniter\HTTP;
33

4+
use Config\App;
5+
use CodeIgniter\Config\Services;
46
use CodeIgniter\HTTP\Exceptions\HTTPException;
57

68
class URITest extends \CIUnitTestCase
@@ -781,4 +783,61 @@ public function testSetBadSegment()
781783
$uri->setSegment(6, 'banana');
782784
}
783785

786+
//--------------------------------------------------------------------
787+
// Exploratory testing, investigating https://github.com/codeigniter4/CodeIgniter4/issues/2016
788+
789+
public function testBasedNoIndex()
790+
{
791+
Services::reset();
792+
793+
$_SERVER['HTTP_HOST'] = 'example.com';
794+
$_SERVER['REQUEST_URI'] = '/ci/v4/controller/method';
795+
796+
$config = new App();
797+
$config->baseURL = 'http://example.com/ci/v4';
798+
$config->indexPage = 'index.php';
799+
$request = Services::request($config);
800+
$request->uri = new URI('http://example.com/ci/v4/controller/method');
801+
802+
Services::injectMock('request', $request);
803+
804+
// going through request
805+
$this->assertEquals('http://example.com/ci/v4/controller/method', (string) $request->uri);
806+
$this->assertEquals('/ci/v4/controller/method', $request->uri->getPath());
807+
808+
// standalone
809+
$uri = new URI('http://example.com/ci/v4/controller/method');
810+
$this->assertEquals('http://example.com/ci/v4/controller/method', (string) $uri);
811+
$this->assertEquals('/ci/v4/controller/method', $uri->getPath());
812+
813+
$this->assertEquals($uri->getPath(), $request->uri->getPath());
814+
}
815+
816+
public function testBasedWithIndex()
817+
{
818+
Services::reset();
819+
820+
$_SERVER['HTTP_HOST'] = 'example.com';
821+
$_SERVER['REQUEST_URI'] = '/ci/v4/index.php/controller/method';
822+
823+
$config = new App();
824+
$config->baseURL = 'http://example.com/ci/v4';
825+
$config->indexPage = 'index.php';
826+
$request = Services::request($config);
827+
$request->uri = new URI('http://example.com/ci/v4/index.php/controller/method');
828+
829+
Services::injectMock('request', $request);
830+
831+
// going through request
832+
$this->assertEquals('http://example.com/ci/v4/index.php/controller/method', (string) $request->uri);
833+
$this->assertEquals('/ci/v4/index.php/controller/method', $request->uri->getPath());
834+
835+
// standalone
836+
$uri = new URI('http://example.com/ci/v4/index.php/controller/method');
837+
$this->assertEquals('http://example.com/ci/v4/index.php/controller/method', (string) $uri);
838+
$this->assertEquals('/ci/v4/index.php/controller/method', $uri->getPath());
839+
840+
$this->assertEquals($uri->getPath(), $request->uri->getPath());
841+
}
842+
784843
}

tests/system/Helpers/URLHelperTest.php

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
namespace CodeIgniter\Helpers;
43

54
use Config\App;
@@ -1085,4 +1084,75 @@ public function testUrlTitleExtraDashes()
10851084
}
10861085
}
10871086

1087+
//--------------------------------------------------------------------
1088+
// Exploratory testing, investigating https://github.com/codeigniter4/CodeIgniter4/issues/2016
1089+
1090+
public function testBasedNoIndex()
1091+
{
1092+
$_SERVER['HTTP_HOST'] = 'example.com';
1093+
$_SERVER['REQUEST_URI'] = '/ci/v4/x/y';
1094+
1095+
$config = new App();
1096+
$config->baseURL = 'http://example.com/ci/v4/';
1097+
$config->indexPage = 'index.php';
1098+
$request = Services::request($config);
1099+
$request->uri = new URI('http://example.com/somewhere');
1100+
1101+
Services::injectMock('request', $request);
1102+
1103+
$this->assertEquals('http://example.com/ci/v4/index.php/controller/method', site_url('controller/method', null, $config));
1104+
$this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $config));
1105+
}
1106+
1107+
public function testBasedWithIndex()
1108+
{
1109+
$_SERVER['HTTP_HOST'] = 'example.com';
1110+
$_SERVER['REQUEST_URI'] = '/ci/v4/index.php/x/y';
1111+
1112+
$config = new App();
1113+
$config->baseURL = 'http://example.com/ci/v4/';
1114+
$config->indexPage = 'index.php';
1115+
$request = Services::request($config);
1116+
$request->uri = new URI('http://example.com/somewhere');
1117+
1118+
Services::injectMock('request', $request);
1119+
1120+
$this->assertEquals('http://example.com/ci/v4/index.php/controller/method', site_url('controller/method', null, $config));
1121+
$this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $config));
1122+
}
1123+
1124+
public function testBasedWithoutIndex()
1125+
{
1126+
$_SERVER['HTTP_HOST'] = 'example.com';
1127+
$_SERVER['REQUEST_URI'] = '/ci/v4/x/y';
1128+
1129+
$config = new App();
1130+
$config->baseURL = 'http://example.com/ci/v4/';
1131+
$config->indexPage = '';
1132+
$request = Services::request($config);
1133+
$request->uri = new URI('http://example.com/somewhere');
1134+
1135+
Services::injectMock('request', $request);
1136+
1137+
$this->assertEquals('http://example.com/ci/v4/controller/method', site_url('controller/method', null, $config));
1138+
$this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $config));
1139+
}
1140+
1141+
public function testBasedWithOtherIndex()
1142+
{
1143+
$_SERVER['HTTP_HOST'] = 'example.com';
1144+
$_SERVER['REQUEST_URI'] = '/ci/v4/x/y';
1145+
1146+
$config = new App();
1147+
$config->baseURL = 'http://example.com/ci/v4/';
1148+
$config->indexPage = 'fc.php';
1149+
$request = Services::request($config);
1150+
$request->uri = new URI('http://example.com/somewhere');
1151+
1152+
Services::injectMock('request', $request);
1153+
1154+
$this->assertEquals('http://example.com/ci/v4/fc.php/controller/method', site_url('controller/method', null, $config));
1155+
$this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $config));
1156+
}
1157+
10881158
}

user_guide_src/source/helpers/url_helper.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ The following functions are available:
3030
:rtype: string
3131

3232
Returns your site URL, as specified in your config file. The index.php
33-
file (or whatever you have set as your site **index_page** in your config
33+
file (or whatever you have set as your site **indexPage** in your config
3434
file) will be added to the URL, as will any URI segments you pass to the
35-
function, plus the **url_suffix** as set in your config file.
35+
function.
3636

3737
You are encouraged to use this function any time you need to generate a
3838
local URL so that your pages become more portable in the event your URL
@@ -67,7 +67,7 @@ The following functions are available:
6767
echo base_url();
6868

6969
This function returns the same thing as :php:func:`site_url()`, without
70-
the *index_page* or *url_suffix* being appended.
70+
the *indexPage* being appended.
7171

7272
Also like :php:func:`site_url()`, you can supply segments as a string or
7373
an array. Here is a string example::
@@ -130,7 +130,7 @@ The following functions are available:
130130
:returns: 'index_page' value
131131
:rtype: mixed
132132

133-
Returns your site **index_page**, as specified in your config file.
133+
Returns your site **indexPage**, as specified in your config file.
134134
Example::
135135

136136
echo index_page();

0 commit comments

Comments
 (0)