diff --git a/system/Pager/PagerRenderer.php b/system/Pager/PagerRenderer.php index a532c36d1163..e7ce557f92ec 100644 --- a/system/Pager/PagerRenderer.php +++ b/system/Pager/PagerRenderer.php @@ -81,7 +81,7 @@ class PagerRenderer */ protected $pageCount; /** - * URI? unused? + * URI base for pagination links * * @var integer */ diff --git a/tests/system/HTTP/URITest.php b/tests/system/HTTP/URITest.php index 9c6d497cb875..579a454a5a91 100644 --- a/tests/system/HTTP/URITest.php +++ b/tests/system/HTTP/URITest.php @@ -1,6 +1,8 @@ setSegment(6, 'banana'); } + //-------------------------------------------------------------------- + // Exploratory testing, investigating https://github.com/codeigniter4/CodeIgniter4/issues/2016 + + public function testBasedNoIndex() + { + Services::reset(); + + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/ci/v4/controller/method'; + + $config = new App(); + $config->baseURL = 'http://example.com/ci/v4'; + $config->indexPage = 'index.php'; + $request = Services::request($config); + $request->uri = new URI('http://example.com/ci/v4/controller/method'); + + Services::injectMock('request', $request); + + // going through request + $this->assertEquals('http://example.com/ci/v4/controller/method', (string) $request->uri); + $this->assertEquals('/ci/v4/controller/method', $request->uri->getPath()); + + // standalone + $uri = new URI('http://example.com/ci/v4/controller/method'); + $this->assertEquals('http://example.com/ci/v4/controller/method', (string) $uri); + $this->assertEquals('/ci/v4/controller/method', $uri->getPath()); + + $this->assertEquals($uri->getPath(), $request->uri->getPath()); + } + + public function testBasedWithIndex() + { + Services::reset(); + + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/ci/v4/index.php/controller/method'; + + $config = new App(); + $config->baseURL = 'http://example.com/ci/v4'; + $config->indexPage = 'index.php'; + $request = Services::request($config); + $request->uri = new URI('http://example.com/ci/v4/index.php/controller/method'); + + Services::injectMock('request', $request); + + // going through request + $this->assertEquals('http://example.com/ci/v4/index.php/controller/method', (string) $request->uri); + $this->assertEquals('/ci/v4/index.php/controller/method', $request->uri->getPath()); + + // standalone + $uri = new URI('http://example.com/ci/v4/index.php/controller/method'); + $this->assertEquals('http://example.com/ci/v4/index.php/controller/method', (string) $uri); + $this->assertEquals('/ci/v4/index.php/controller/method', $uri->getPath()); + + $this->assertEquals($uri->getPath(), $request->uri->getPath()); + } + } diff --git a/tests/system/Helpers/URLHelperTest.php b/tests/system/Helpers/URLHelperTest.php index 564ce6d34796..a5cf314fb7da 100644 --- a/tests/system/Helpers/URLHelperTest.php +++ b/tests/system/Helpers/URLHelperTest.php @@ -1,5 +1,4 @@ baseURL = 'http://example.com/ci/v4/'; + $config->indexPage = 'index.php'; + $request = Services::request($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, $config)); + $this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $config)); + $this->assertEquals(base_url(uri_string()), current_url()); + } + + public function testBasedWithIndex() + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/ci/v4/index.php/x/y'; + + $config = new App(); + $config->baseURL = 'http://example.com/ci/v4/'; + $config->indexPage = 'index.php'; + $request = Services::request($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, $config)); + $this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $config)); + $this->assertEquals(base_url(uri_string()), current_url()); + } + + public function testBasedWithoutIndex() + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/ci/v4/x/y'; + + $config = new App(); + $config->baseURL = 'http://example.com/ci/v4/'; + $config->indexPage = ''; + $request = Services::request($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, $config)); + $this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $config)); + $this->assertEquals(base_url(uri_string()), current_url()); + } + + public function testBasedWithOtherIndex() + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/ci/v4/x/y'; + + $config = new App(); + $config->baseURL = 'http://example.com/ci/v4/'; + $config->indexPage = 'fc.php'; + $request = Services::request($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, $config)); + $this->assertEquals('http://example.com/ci/v4/controller/method', base_url('controller/method', null, $config)); + $this->assertEquals(base_url(uri_string()), current_url()); + } + } diff --git a/tests/system/Pager/PagerTest.php b/tests/system/Pager/PagerTest.php index bb013a72e8ee..366aa456f239 100644 --- a/tests/system/Pager/PagerTest.php +++ b/tests/system/Pager/PagerTest.php @@ -1,6 +1,8 @@ config = new Pager(); - $this->pager = new \CodeIgniter\Pager\Pager($this->config, Services::renderer()); + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/'; + $_GET = []; + + $config = new App(); + $config->baseURL = 'http://example.com/'; + $request = Services::request($config); + $request->uri = new URI('http://example.com'); + + Services::injectMock('request', $request); + + $_GET = []; + $this->config = new Pager(); + $this->pager = new \CodeIgniter\Pager\Pager($this->config, Services::renderer()); } public function testSetPathRemembersPath() @@ -354,4 +365,32 @@ public function testHeadLinks() $this->assertContains('assertNotContains('baseURL = 'http://example.com/ci/v4/'; + $config->indexPage = 'fc.php'; + $request = Services::request($config); + $request->uri = new URI('http://example.com/ci/v4/x/y'); + + Services::injectMock('request', $request); + + $this->config = new Pager(); + $this->pager = new \CodeIgniter\Pager\Pager($this->config, Services::renderer()); + + $_GET['page'] = 2; + + $this->pager->store('foo', 2, 12, 70); + + $expected = current_url(true); + $expected = (string)$expected->setQuery('page=1'); + + $this->assertEquals((string)$expected, $this->pager->getPreviousPageURI('foo')); + } + } diff --git a/user_guide_src/source/helpers/url_helper.rst b/user_guide_src/source/helpers/url_helper.rst index 61e7481407d6..fac7b9c9118f 100644 --- a/user_guide_src/source/helpers/url_helper.rst +++ b/user_guide_src/source/helpers/url_helper.rst @@ -30,9 +30,9 @@ The following functions are available: :rtype: string Returns your site URL, as specified in your config file. The index.php - file (or whatever you have set as your site **index_page** in your config + file (or whatever you have set as your site **indexPage** in your config file) will be added to the URL, as will any URI segments you pass to the - function, plus the **url_suffix** as set in your config file. + function. You are encouraged to use this function any time you need to generate a local URL so that your pages become more portable in the event your URL @@ -67,7 +67,7 @@ The following functions are available: echo base_url(); This function returns the same thing as :php:func:`site_url()`, without - the *index_page* or *url_suffix* being appended. + the *indexPage* being appended. Also like :php:func:`site_url()`, you can supply segments as a string or an array. Here is a string example:: @@ -130,7 +130,7 @@ The following functions are available: :returns: 'index_page' value :rtype: mixed - Returns your site **index_page**, as specified in your config file. + Returns your site **indexPage**, as specified in your config file. Example:: echo index_page();