Skip to content

Commit d7e8f04

Browse files
authored
Merge pull request #2259 from jim-parry/test/uri
Add URI & url_helper tests
2 parents 064d85b + 4519111 commit d7e8f04

5 files changed

Lines changed: 183 additions & 11 deletions

File tree

system/Pager/PagerRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class PagerRenderer
8181
*/
8282
protected $pageCount;
8383
/**
84-
* URI? unused?
84+
* URI base for pagination links
8585
*
8686
* @var integer
8787
*/

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: 75 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,79 @@ 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/ci/v4/x/y');
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+
$this->assertEquals(base_url(uri_string()), current_url());
1106+
}
1107+
1108+
public function testBasedWithIndex()
1109+
{
1110+
$_SERVER['HTTP_HOST'] = 'example.com';
1111+
$_SERVER['REQUEST_URI'] = '/ci/v4/index.php/x/y';
1112+
1113+
$config = new App();
1114+
$config->baseURL = 'http://example.com/ci/v4/';
1115+
$config->indexPage = 'index.php';
1116+
$request = Services::request($config);
1117+
$request->uri = new URI('http://example.com/ci/v4/index.php/x/y');
1118+
1119+
Services::injectMock('request', $request);
1120+
1121+
$this->assertEquals('http://example.com/ci/v4/index.php/controller/method', site_url('controller/method', null, $config));
1122+
$this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $config));
1123+
$this->assertEquals(base_url(uri_string()), current_url());
1124+
}
1125+
1126+
public function testBasedWithoutIndex()
1127+
{
1128+
$_SERVER['HTTP_HOST'] = 'example.com';
1129+
$_SERVER['REQUEST_URI'] = '/ci/v4/x/y';
1130+
1131+
$config = new App();
1132+
$config->baseURL = 'http://example.com/ci/v4/';
1133+
$config->indexPage = '';
1134+
$request = Services::request($config);
1135+
$request->uri = new URI('http://example.com/ci/v4/x/y');
1136+
1137+
Services::injectMock('request', $request);
1138+
1139+
$this->assertEquals('http://example.com/ci/v4/controller/method', site_url('controller/method', null, $config));
1140+
$this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $config));
1141+
$this->assertEquals(base_url(uri_string()), current_url());
1142+
}
1143+
1144+
public function testBasedWithOtherIndex()
1145+
{
1146+
$_SERVER['HTTP_HOST'] = 'example.com';
1147+
$_SERVER['REQUEST_URI'] = '/ci/v4/x/y';
1148+
1149+
$config = new App();
1150+
$config->baseURL = 'http://example.com/ci/v4/';
1151+
$config->indexPage = 'fc.php';
1152+
$request = Services::request($config);
1153+
$request->uri = new URI('http://example.com/ci/v4/x/y');
1154+
1155+
Services::injectMock('request', $request);
1156+
1157+
$this->assertEquals('http://example.com/ci/v4/fc.php/controller/method', site_url('controller/method', null, $config));
1158+
$this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $config));
1159+
$this->assertEquals(base_url(uri_string()), current_url());
1160+
}
1161+
10881162
}

tests/system/Pager/PagerTest.php

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php namespace CodeIgniter\Pager;
22

3+
use CodeIgniter\HTTP\URI;
34
use CodeIgniter\Pager\Exceptions\PagerException;
5+
use Config\App;
46
use Config\Pager;
57
use Config\Services;
68

@@ -19,13 +21,22 @@ class PagerTest extends \CIUnitTestCase
1921
protected function setUp()
2022
{
2123
parent::setUp();
22-
2324
helper('url');
2425

25-
$_SERVER['HTTP_HOST'] = 'example.com';
26-
$_GET = [];
27-
$this->config = new Pager();
28-
$this->pager = new \CodeIgniter\Pager\Pager($this->config, Services::renderer());
26+
$_SERVER['HTTP_HOST'] = 'example.com';
27+
$_SERVER['REQUEST_URI'] = '/';
28+
$_GET = [];
29+
30+
$config = new App();
31+
$config->baseURL = 'http://example.com/';
32+
$request = Services::request($config);
33+
$request->uri = new URI('http://example.com');
34+
35+
Services::injectMock('request', $request);
36+
37+
$_GET = [];
38+
$this->config = new Pager();
39+
$this->pager = new \CodeIgniter\Pager\Pager($this->config, Services::renderer());
2940
}
3041

3142
public function testSetPathRemembersPath()
@@ -354,4 +365,32 @@ public function testHeadLinks()
354365
$this->assertContains('<link rel="canonical"', $last_page);
355366
$this->assertNotContains('<link rel="next"', $last_page);
356367
}
368+
369+
public function testBasedURI()
370+
{
371+
$_SERVER['HTTP_HOST'] = 'example.com';
372+
$_SERVER['REQUEST_URI'] = '/ci/v4/x/y';
373+
$_GET = [];
374+
375+
$config = new App();
376+
$config->baseURL = 'http://example.com/ci/v4/';
377+
$config->indexPage = 'fc.php';
378+
$request = Services::request($config);
379+
$request->uri = new URI('http://example.com/ci/v4/x/y');
380+
381+
Services::injectMock('request', $request);
382+
383+
$this->config = new Pager();
384+
$this->pager = new \CodeIgniter\Pager\Pager($this->config, Services::renderer());
385+
386+
$_GET['page'] = 2;
387+
388+
$this->pager->store('foo', 2, 12, 70);
389+
390+
$expected = current_url(true);
391+
$expected = (string)$expected->setQuery('page=1');
392+
393+
$this->assertEquals((string)$expected, $this->pager->getPreviousPageURI('foo'));
394+
}
395+
357396
}

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)