From 39def322cd8fb5eff2c411cc697cbe70815287a4 Mon Sep 17 00:00:00 2001 From: Jim Parry Date: Mon, 23 Sep 2019 01:23:13 -0700 Subject: [PATCH 1/3] Add URI & url_helper tests --- tests/system/HTTP/URITest.php | 59 ++++++++++++++++ tests/system/Helpers/URLHelperTest.php | 72 +++++++++++++++++++- user_guide_src/source/helpers/url_helper.rst | 8 +-- 3 files changed, 134 insertions(+), 5 deletions(-) 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..c71907edb290 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/somewhere'); + + 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)); + } + + 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/somewhere'); + + 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)); + } + + 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/somewhere'); + + 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)); + } + + 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/somewhere'); + + 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)); + } + } 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(); From ebb55664ce75ca903a92c4d3ad4f7d4541d9cf09 Mon Sep 17 00:00:00 2001 From: Jim Parry Date: Mon, 23 Sep 2019 06:19:37 -0700 Subject: [PATCH 2/3] Add Pager testing for current_url --- system/Pager/PagerRenderer.php | 2 +- tests/system/Helpers/URLHelperTest.php | 12 ++++++---- tests/system/Pager/PagerTest.php | 31 +++++++++++++++++++++++++- 3 files changed, 39 insertions(+), 6 deletions(-) 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/Helpers/URLHelperTest.php b/tests/system/Helpers/URLHelperTest.php index c71907edb290..a5cf314fb7da 100644 --- a/tests/system/Helpers/URLHelperTest.php +++ b/tests/system/Helpers/URLHelperTest.php @@ -1096,12 +1096,13 @@ public function testBasedNoIndex() $config->baseURL = 'http://example.com/ci/v4/'; $config->indexPage = 'index.php'; $request = Services::request($config); - $request->uri = new URI('http://example.com/somewhere'); + $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() @@ -1113,12 +1114,13 @@ public function testBasedWithIndex() $config->baseURL = 'http://example.com/ci/v4/'; $config->indexPage = 'index.php'; $request = Services::request($config); - $request->uri = new URI('http://example.com/somewhere'); + $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() @@ -1130,12 +1132,13 @@ public function testBasedWithoutIndex() $config->baseURL = 'http://example.com/ci/v4/'; $config->indexPage = ''; $request = Services::request($config); - $request->uri = new URI('http://example.com/somewhere'); + $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() @@ -1147,12 +1150,13 @@ public function testBasedWithOtherIndex() $config->baseURL = 'http://example.com/ci/v4/'; $config->indexPage = 'fc.php'; $request = Services::request($config); - $request->uri = new URI('http://example.com/somewhere'); + $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..8216f7e20973 100644 --- a/tests/system/Pager/PagerTest.php +++ b/tests/system/Pager/PagerTest.php @@ -1,6 +1,8 @@ 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')); + } + } From 45191119cbc753ab8d576628ff6cc2de9d0a7454 Mon Sep 17 00:00:00 2001 From: Jim Parry Date: Mon, 23 Sep 2019 06:57:46 -0700 Subject: [PATCH 3/3] PagerTest setup consistency fix --- tests/system/Pager/PagerTest.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/tests/system/Pager/PagerTest.php b/tests/system/Pager/PagerTest.php index 8216f7e20973..366aa456f239 100644 --- a/tests/system/Pager/PagerTest.php +++ b/tests/system/Pager/PagerTest.php @@ -23,10 +23,20 @@ protected function setUp() parent::setUp(); helper('url'); - $_SERVER['HTTP_HOST'] = 'example.com'; - $_GET = []; - $this->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()