From 60988123f919087fec2e86001b724e0982c2f264 Mon Sep 17 00:00:00 2001 From: Franck Allimant Date: Tue, 4 Feb 2014 12:39:35 +0100 Subject: [PATCH] Fixed URL generation wich occasionally fails when parameters are added --- core/lib/Thelia/Tests/Tools/URLTest.php | 104 ++++++++++++++++-------- core/lib/Thelia/Tools/URL.php | 15 ++-- 2 files changed, 78 insertions(+), 41 deletions(-) diff --git a/core/lib/Thelia/Tests/Tools/URLTest.php b/core/lib/Thelia/Tests/Tools/URLTest.php index bf70e91c6..b996fe288 100755 --- a/core/lib/Thelia/Tests/Tools/URLTest.php +++ b/core/lib/Thelia/Tests/Tools/URLTest.php @@ -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¤t_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¤t_tab=general', $url); + + $url = URL::getInstance()->absoluteUrl( + 'http://localhost/index_dev.php/admin/categories/update?category_id=1¤t_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¤t_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¤t_tab=general&folder_id=0', + array(), + URL::PATH_TO_FILE + ); + $this->assertEquals('http://localhost/index_dev.php/admin/categories/update?category_id=1¤t_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¤t_tab=general', $url); + } + public function testRetrieve() { diff --git a/core/lib/Thelia/Tools/URL.php b/core/lib/Thelia/Tools/URL.php index 0334ac72e..c26c7b4a6 100644 --- a/core/lib/Thelia/Tools/URL.php +++ b/core/lib/Thelia/Tools/URL.php @@ -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; } /**