Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add URI & url_helper tests #2259

Merged
merged 3 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion system/Pager/PagerRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class PagerRenderer
*/
protected $pageCount;
/**
* URI? unused?
* URI base for pagination links
*
* @var integer
*/
Expand Down
59 changes: 59 additions & 0 deletions tests/system/HTTP/URITest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace CodeIgniter\HTTP;

use Config\App;
use CodeIgniter\Config\Services;
use CodeIgniter\HTTP\Exceptions\HTTPException;

class URITest extends \CIUnitTestCase
Expand Down Expand Up @@ -781,4 +783,61 @@ public function testSetBadSegment()
$uri->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());
}

}
76 changes: 75 additions & 1 deletion tests/system/Helpers/URLHelperTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

namespace CodeIgniter\Helpers;

use Config\App;
Expand Down Expand Up @@ -1085,4 +1084,79 @@ public function testUrlTitleExtraDashes()
}
}

//--------------------------------------------------------------------
// Exploratory testing, investigating https://github.com/codeigniter4/CodeIgniter4/issues/2016

public function testBasedNoIndex()
{
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/ci/v4/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/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());
}

}
49 changes: 44 additions & 5 deletions tests/system/Pager/PagerTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php namespace CodeIgniter\Pager;

use CodeIgniter\HTTP\URI;
use CodeIgniter\Pager\Exceptions\PagerException;
use Config\App;
use Config\Pager;
use Config\Services;

Expand All @@ -19,13 +21,22 @@ class PagerTest extends \CIUnitTestCase
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()
Expand Down Expand Up @@ -354,4 +365,32 @@ public function testHeadLinks()
$this->assertContains('<link rel="canonical"', $last_page);
$this->assertNotContains('<link rel="next"', $last_page);
}

public function testBasedURI()
{
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/ci/v4/x/y';
$_GET = [];

$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->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'));
}

}
8 changes: 4 additions & 4 deletions user_guide_src/source/helpers/url_helper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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::
Expand Down Expand Up @@ -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();
Expand Down