Merge branch 'master' of github.com:thelia/thelia

This commit is contained in:
Manuel Raynaud
2014-02-04 12:59:16 +01:00
2 changed files with 78 additions and 41 deletions

View File

@@ -59,149 +59,149 @@ class URLTest extends \PHPUnit_Framework_TestCase
$container->set("router.admin", $router);
new \Thelia\Tools\URL($container);
new URL($container);
}
public function testGetIndexPage()
{
$this->context->setBaseUrl('/thelia/index.php');
$url = \Thelia\Tools\URL::getInstance()->getIndexPage();
$url = URL::getInstance()->getIndexPage();
$this->assertEquals('http://localhost/thelia/index.php', $url);
$this->context->setBaseUrl('/thelia/');
$url = \Thelia\Tools\URL::getInstance()->getIndexPage();
$url = URL::getInstance()->getIndexPage();
$this->assertEquals('http://localhost/thelia/', $url);
$this->context->setBaseUrl('/thelia');
$url = \Thelia\Tools\URL::getInstance()->getIndexPage();
$url = URL::getInstance()->getIndexPage();
$this->assertEquals('http://localhost/thelia', $url);
$this->context->setBaseUrl('/');
$url = \Thelia\Tools\URL::getInstance()->getIndexPage();
$url = URL::getInstance()->getIndexPage();
$this->assertEquals('http://localhost/', $url);
}
public function testGetBaseUrl()
{
$this->context->setBaseUrl('/thelia/index.php');
$url = \Thelia\Tools\URL::getInstance()->getBaseUrl();
$url = URL::getInstance()->getBaseUrl();
$this->assertEquals('http://localhost/thelia/index.php', $url);
$this->context->setBaseUrl('/thelia/');
$url = \Thelia\Tools\URL::getInstance()->getBaseUrl();
$url = URL::getInstance()->getBaseUrl();
$this->assertEquals('http://localhost/thelia/', $url);
$this->context->setBaseUrl('/');
$url = \Thelia\Tools\URL::getInstance()->getBaseUrl();
$url = URL::getInstance()->getBaseUrl();
$this->assertEquals('http://localhost/', $url);
}
public function testAbsoluteUrl()
{
$this->context->setBaseUrl('/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/path/to/action');
$url = URL::getInstance()->absoluteUrl('/path/to/action');
$this->assertEquals('http://localhost/path/to/action', $url);
$this->context->setBaseUrl('/thelia/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/path/to/action');
$url = URL::getInstance()->absoluteUrl('/path/to/action');
$this->assertEquals('http://localhost/thelia/path/to/action', $url);
$this->context->setBaseUrl('/thelia');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/path/to/action');
$url = URL::getInstance()->absoluteUrl('/path/to/action');
$this->assertEquals('http://localhost/thelia/path/to/action', $url);
$this->context->setBaseUrl('/thelia/index.php');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/path/to/action');
$url = URL::getInstance()->absoluteUrl('/path/to/action');
$this->assertEquals('http://localhost/thelia/index.php/path/to/action', $url);
}
public function testAbsoluteUrlOnAbsolutePath()
{
$this->context->setBaseUrl('/thelia/index.php');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('http://myhost/path/to/action');
$url = URL::getInstance()->absoluteUrl('http://myhost/path/to/action');
$this->assertEquals('http://myhost/path/to/action', $url);
$this->context->setBaseUrl('/thelia/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('http://myhost/path/to/action');
$url = URL::getInstance()->absoluteUrl('http://myhost/path/to/action');
$this->assertEquals('http://myhost/path/to/action', $url);
$this->context->setBaseUrl('/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('http://myhost/path/to/action');
$url = URL::getInstance()->absoluteUrl('http://myhost/path/to/action');
$this->assertEquals('http://myhost/path/to/action', $url);
}
public function testAbsoluteUrlOnAbsolutePathWithParameters()
{
$this->context->setBaseUrl('/thelia/index.php');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('http://myhost/path/to/action', array("p1" => "v1", "p2" => "v2"));
$url = URL::getInstance()->absoluteUrl('http://myhost/path/to/action', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://myhost/path/to/action?p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/thelia/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('http://myhost/path/to/action', array("p1" => "v1", "p2" => "v2"));
$url = URL::getInstance()->absoluteUrl('http://myhost/path/to/action', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://myhost/path/to/action?p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('http://myhost/path/to/action', array("p1" => "v1", "p2" => "v2"));
$url = URL::getInstance()->absoluteUrl('http://myhost/path/to/action', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://myhost/path/to/action?p1=v1&p2=v2', $url);
}
public function testAbsoluteUrlOnAbsolutePathWithParametersAddParameters()
{
$this->context->setBaseUrl('/thelia/index.php');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('http://myhost/path/to/action?p0=v0', array("p1" => "v1", "p2" => "v2"));
$url = URL::getInstance()->absoluteUrl('http://myhost/path/to/action?p0=v0', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://myhost/path/to/action?p0=v0&p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/thelia/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('http://myhost/path/to/action?p0=v0', array("p1" => "v1", "p2" => "v2"));
$url = URL::getInstance()->absoluteUrl('http://myhost/path/to/action?p0=v0', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://myhost/path/to/action?p0=v0&p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('http://myhost/path/to/action?p0=v0', array("p1" => "v1", "p2" => "v2"));
$url = URL::getInstance()->absoluteUrl('http://myhost/path/to/action?p0=v0', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://myhost/path/to/action?p0=v0&p1=v1&p2=v2', $url);
}
public function testAbsoluteUrlWithParameters()
{
$this->context->setBaseUrl('/thelia/index.php');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/path/to/action', array("p1" => "v1", "p2" => "v2"));
$url = URL::getInstance()->absoluteUrl('/path/to/action', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://localhost/thelia/index.php/path/to/action?p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/thelia/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/path/to/action', array("p1" => "v1", "p2" => "v2"));
$url = URL::getInstance()->absoluteUrl('/path/to/action', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://localhost/thelia/path/to/action?p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/thelia');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/path/to/action', array("p1" => "v1", "p2" => "v2"));
$url = URL::getInstance()->absoluteUrl('/path/to/action', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://localhost/thelia/path/to/action?p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/thelia');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('path/to/action', array("p1" => "v1", "p2" => "v2"));
$url = URL::getInstance()->absoluteUrl('path/to/action', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://localhost/thelia/path/to/action?p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/path/to/action', array("p1" => "v1", "p2" => "v2"));
$url = URL::getInstance()->absoluteUrl('/path/to/action', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://localhost/path/to/action?p1=v1&p2=v2', $url);
}
public function testAbsoluteUrlPathOnly()
{
$this->context->setBaseUrl('/thelia/index.php');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/path/to/action', array(), URL::PATH_TO_FILE);
$url = URL::getInstance()->absoluteUrl('/path/to/action', array(), URL::PATH_TO_FILE);
$this->assertEquals('http://localhost/thelia/path/to/action', $url);
}
public function testAbsoluteUrlPathOnlyWithParameters()
{
$this->context->setBaseUrl('/thelia/index.php');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/path/to/action', array("p1" => "v1", "p2" => "v2"), URL::PATH_TO_FILE);
$url = URL::getInstance()->absoluteUrl('/path/to/action', array("p1" => "v1", "p2" => "v2"), URL::PATH_TO_FILE);
$this->assertEquals('http://localhost/thelia/path/to/action?p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/thelia/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/path/to/action', array("p1" => "v1", "p2" => "v2"), URL::PATH_TO_FILE);
$url = URL::getInstance()->absoluteUrl('/path/to/action', array("p1" => "v1", "p2" => "v2"), URL::PATH_TO_FILE);
$this->assertEquals('http://localhost/thelia/path/to/action?p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/path/to/action', array("p1" => "v1", "p2" => "v2"), URL::PATH_TO_FILE);
$url = URL::getInstance()->absoluteUrl('/path/to/action', array("p1" => "v1", "p2" => "v2"), URL::PATH_TO_FILE);
$this->assertEquals('http://localhost/path/to/action?p1=v1&p2=v2', $url);
}
@@ -209,15 +209,15 @@ class URLTest extends \PHPUnit_Framework_TestCase
public function testAbsoluteUrlFromIndexWithParameters()
{
$this->context->setBaseUrl('/thelia/index.php');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/', array("p1" => "v1", "p2" => "v2"));
$url = URL::getInstance()->absoluteUrl('/', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://localhost/thelia/index.php/?p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/thelia/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/', array("p1" => "v1", "p2" => "v2"));
$url = URL::getInstance()->absoluteUrl('/', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://localhost/thelia/?p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/', array("p1" => "v1", "p2" => "v2"));
$url = URL::getInstance()->absoluteUrl('/', array("p1" => "v1", "p2" => "v2"));
$this->assertEquals('http://localhost/?p1=v1&p2=v2', $url);
}
@@ -225,19 +225,51 @@ class URLTest extends \PHPUnit_Framework_TestCase
public function testAbsoluteUrlPathOnlyFromIndexWithParameters()
{
$this->context->setBaseUrl('/thelia/index.php');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/', array("p1" => "v1", "p2" => "v2"), URL::PATH_TO_FILE);
$url = URL::getInstance()->absoluteUrl('/', array("p1" => "v1", "p2" => "v2"), URL::PATH_TO_FILE);
$this->assertEquals('http://localhost/thelia/?p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/thelia/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/', array("p1" => "v1", "p2" => "v2"), URL::PATH_TO_FILE);
$url = URL::getInstance()->absoluteUrl('/', array("p1" => "v1", "p2" => "v2"), URL::PATH_TO_FILE);
$this->assertEquals('http://localhost/thelia/?p1=v1&p2=v2', $url);
$this->context->setBaseUrl('/');
$url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/', array("p1" => "v1", "p2" => "v2"), URL::PATH_TO_FILE);
$url = URL::getInstance()->absoluteUrl('/', array("p1" => "v1", "p2" => "v2"), URL::PATH_TO_FILE);
$this->assertEquals('http://localhost/?p1=v1&p2=v2', $url);
}
public function testAbsoluteUrlPathWithParameterReplacement()
{
$this->context->setBaseUrl('/thelia/index.php');
$url = URL::getInstance()->absoluteUrl(
'http://localhost/index_dev.php/admin/categories/update?category_id=1&current_tab=general&folder_id=0',
array("category_id" => "2", "edit_language_id" => "1", "current_tab" => "general"),
URL::PATH_TO_FILE
);
$this->assertEquals('http://localhost/index_dev.php/admin/categories/update?folder_id=0&category_id=2&edit_language_id=1&current_tab=general', $url);
$url = URL::getInstance()->absoluteUrl(
'http://localhost/index_dev.php/admin/categories/update?category_id=1&current_tab=general&folder_id=0',
array("edit_language_id" => "1"),
URL::PATH_TO_FILE
);
$this->assertEquals('http://localhost/index_dev.php/admin/categories/update?category_id=1&current_tab=general&folder_id=0&edit_language_id=1', $url);
$url = URL::getInstance()->absoluteUrl(
'http://localhost/index_dev.php/admin/categories/update?category_id=1&current_tab=general&folder_id=0',
array(),
URL::PATH_TO_FILE
);
$this->assertEquals('http://localhost/index_dev.php/admin/categories/update?category_id=1&current_tab=general&folder_id=0', $url);
$url = URL::getInstance()->absoluteUrl(
'http://localhost/index_dev.php/admin/categories/update?category_id=1',
array("category_id" => "2", "edit_language_id" => "1", "current_tab" => "general"),
URL::PATH_TO_FILE
);
$this->assertEquals('http://localhost/index_dev.php/admin/categories/update?category_id=2&edit_language_id=1&current_tab=general', $url);
}
public function testRetrieve()
{

View File

@@ -145,17 +145,22 @@ class URL
foreach ($parameters as $name => $value) {
// Remove this parameter from base URL to prevent duplicate parameters
$base = preg_replace('/([?&])'.$name.'=([^&])*(&|$)/', '', $base);
$base = preg_replace('/([?&])'.$name.'=([^&])*(&|$)/', '$1', $base);
$queryString .= sprintf("%s=%s&", urlencode($name), urlencode($value));
}
}
$sepChar = strstr($base, '?') === false ? '?' : '&';
if ('' !== $queryString = rtrim($queryString, "&")) {
if ('' !== $queryString = rtrim($queryString, "&")) $queryString = $sepChar . $queryString;
return rtrim($base, '&') . $queryString;
$base = rtrim($base, "?&");
$sepChar = strstr($base, '?') === false ? '?' : '&';
$queryString = $sepChar . $queryString;
}
return $base . $queryString;
}
/**