From 271e6c64f0e4682e52df4f217d6a8c2a70cf4f69 Mon Sep 17 00:00:00 2001 From: franck Date: Thu, 5 Sep 2013 10:00:17 +0200 Subject: [PATCH 1/2] Before merging coupon --- core/lib/Thelia/Action/Currency.php | 27 +++++++++----- .../Controller/Admin/CurrencyController.php | 21 ++++++----- .../Core/Event/BaseUpdatePositionEvent.php | 10 ------ .../Event/CurrencyUpdatePositionEvent.php | 2 +- .../Smarty/Plugins/AdminUtilities.php | 4 +-- .../Model/Tools/ModelEventDispatcherTrait.php | 5 +++ .../Model/Tools/PositionManagementTrait.php | 36 +++++++++++-------- templates/admin/default/currencies.html | 4 +-- 8 files changed, 64 insertions(+), 45 deletions(-) diff --git a/core/lib/Thelia/Action/Currency.php b/core/lib/Thelia/Action/Currency.php index 68cc5904a..7f9c3281f 100644 --- a/core/lib/Thelia/Action/Currency.php +++ b/core/lib/Thelia/Action/Currency.php @@ -107,6 +107,7 @@ class Currency extends BaseAction implements EventSubscriberInterface CurrencyQuery::create()->filterByByDefault(true)->update(array('ByDefault' => false)); $currency + ->setDispatcher($this->getDispatcher()) ->setByDefault($event->getIsDefault()) ->save() ; @@ -139,7 +140,7 @@ class Currency extends BaseAction implements EventSubscriberInterface $rates_url = ConfigQuery::read('currency_rate_update_url', 'http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml'); - $rate_data = file_get_contents($rates_url); + $rate_data = @file_get_contents($rates_url); if ($rate_data && $sxe = new \SimpleXMLElement($rate_data)) { @@ -149,12 +150,16 @@ class Currency extends BaseAction implements EventSubscriberInterface $rate = floatval($last['rate']); if (null !== $currency = CurrencyQuery::create()->findOneByCode($code)) { - $currency->setRate($rate)->save(); + $currency + ->setDispatcher($this->getDispatcher()) + ->setRate($rate) + ->save() + ; } } } else { - throw new \RuntimeException(sprintf("Failed to get currency rates data from URL %s", $url)); + throw new \RuntimeException(sprintf("Failed to get currency rates data from URL %s", $rates_url)); } } @@ -165,12 +170,18 @@ class Currency extends BaseAction implements EventSubscriberInterface */ public function updatePosition(CurrencyUpdatePositionEvent $event) { - if (null !== $category = CurrencyQuery::create()->findOneById($event->getObjectId())) { + if (null !== $currency = CurrencyQuery::create()->findOneById($event->getObjectId())) { - if ($event->getMode() == BaseChangePositionEvent::POSITION_ABSOLUTE) - return $category->changeAbsolutePosition($event->getPosition()); - else - return $this->exchangePosition($event->getMode()); + $currency->setDispatcher($this->getDispatcher()); + + $mode = $event->getMode(); + + if ($mode == CurrencyUpdatePositionEvent::POSITION_ABSOLUTE) + return $currency->changeAbsolutePosition($event->getPosition()); + else if ($mode == CurrencyUpdatePositionEvent::POSITION_UP) + return $currency->movePositionUp(); + else if ($mode == CurrencyUpdatePositionEvent::POSITION_DOWN) + return $currency->movePositionDown(); } } diff --git a/core/lib/Thelia/Controller/Admin/CurrencyController.php b/core/lib/Thelia/Controller/Admin/CurrencyController.php index caa3d1bed..56acfb89d 100644 --- a/core/lib/Thelia/Controller/Admin/CurrencyController.php +++ b/core/lib/Thelia/Controller/Admin/CurrencyController.php @@ -316,17 +316,22 @@ class CurrencyController extends BaseAdminController if (null !== $response = $this->checkAuth("admin.configuration.currencies.update")) return $response; try { - $id = $this->getRequest()->get('currency_id', 0); $mode = $this->getRequest()->get('mode', null); + + if ($mode == 'up') + $mode = CurrencyUpdatePositionEvent::POSITION_UP; + else if ($mode == 'down') + $mode = CurrencyUpdatePositionEvent::POSITION_DOWN; + else + $mode = CurrencyUpdatePositionEvent::POSITION_ABSOLUTE; + $position = $this->getRequest()->get('position', null); - $event = new CurrencyUpdatePositionEvent(); - - $event - ->setObjectId($this->getRequest()->get('currency_id', 0)) - ->setPosition($this->getRequest()->get('position', 0)) - ->setMode($mode) - ; + $event = new CurrencyUpdatePositionEvent( + $this->getRequest()->get('currency_id', null), + $mode, + $this->getRequest()->get('position', null) + ); $this->dispatch(TheliaEvents::CURRENCY_UPDATE_POSITION, $event); } diff --git a/core/lib/Thelia/Core/Event/BaseUpdatePositionEvent.php b/core/lib/Thelia/Core/Event/BaseUpdatePositionEvent.php index 3587a0757..41907c29c 100644 --- a/core/lib/Thelia/Core/Event/BaseUpdatePositionEvent.php +++ b/core/lib/Thelia/Core/Event/BaseUpdatePositionEvent.php @@ -74,14 +74,4 @@ class BaseUpdatePositionEvent extends ActionEvent $this->object_id = $object_id; return $this; } - - public function getObjectId() - { - return $this->object_id; - } - - public function setObjectId($object_id) - { - $this->object_id = $object_id; - } } diff --git a/core/lib/Thelia/Core/Event/CurrencyUpdatePositionEvent.php b/core/lib/Thelia/Core/Event/CurrencyUpdatePositionEvent.php index 655e24c9a..3a3dbb18f 100644 --- a/core/lib/Thelia/Core/Event/CurrencyUpdatePositionEvent.php +++ b/core/lib/Thelia/Core/Event/CurrencyUpdatePositionEvent.php @@ -23,6 +23,6 @@ namespace Thelia\Core\Event; -class CategoryChangePositionEvent extends BaseChangePositionEvent +class CurrencyUpdatePositionEvent extends BaseUpdatePositionEvent { } \ No newline at end of file diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/AdminUtilities.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/AdminUtilities.php index fcea9ffd6..12ac13276 100644 --- a/core/lib/Thelia/Core/Template/Smarty/Plugins/AdminUtilities.php +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/AdminUtilities.php @@ -71,11 +71,11 @@ class AdminUtilities extends AbstractSmartyPlugin if ($permissions == null || $this->securityContext->isGranted("ADMIN", array($permission))) { return sprintf( '%s', - URL::getInstance()->absoluteUrl("$path/positionUp", array($url_parameter => $id)), + URL::getInstance()->absoluteUrl($path, array('mode' => 'up', $url_parameter => $id)), $in_place_edit_class, $id, $position, - URL::getInstance()->absoluteUrl("$path/positionDown", array($url_parameter => $id)) + URL::getInstance()->absoluteUrl($path, array('mode' => 'down', $url_parameter => $id)) ); } else { diff --git a/core/lib/Thelia/Model/Tools/ModelEventDispatcherTrait.php b/core/lib/Thelia/Model/Tools/ModelEventDispatcherTrait.php index 7e2e34d83..17af1e44d 100644 --- a/core/lib/Thelia/Model/Tools/ModelEventDispatcherTrait.php +++ b/core/lib/Thelia/Model/Tools/ModelEventDispatcherTrait.php @@ -44,6 +44,11 @@ trait ModelEventDispatcherTrait { return $this; } + public function getDispatcher() + { + return $this->dispatcher; + } + protected function dispatchEvent($eventName, ActionEvent $event) { if (!is_null($this->dispatcher)) { diff --git a/core/lib/Thelia/Model/Tools/PositionManagementTrait.php b/core/lib/Thelia/Model/Tools/PositionManagementTrait.php index eccbb00dd..3261f42ec 100644 --- a/core/lib/Thelia/Model/Tools/PositionManagementTrait.php +++ b/core/lib/Thelia/Model/Tools/PositionManagementTrait.php @@ -25,6 +25,7 @@ namespace Thelia\Model\Tools; use Propel\Runtime\ActiveQuery\PropelQuery; use Propel\Runtime\ActiveQuery\Criteria; +use Propel\Runtime\Propel; trait PositionManagementTrait { @@ -51,10 +52,12 @@ trait PositionManagementTrait { public function getNextPosition($parent) { $last = $this->createQuery() - ->filterByParent($parent) ->orderByPosition(Criteria::DESC) - ->limit(1) - ->findOne() + ->limit(1); + + if ($parent !== null) $last->filterByParent($parent); + + $last->findOne() ; return $last != null ? $last->getPosition() + 1 : 1; @@ -63,14 +66,14 @@ trait PositionManagementTrait { /** * Move up a object */ - protected function movePositionUp() { + public function movePositionUp() { $this->movePositionUpOrDown(true); } /** * Move down a object */ - protected function movePositionDown() { + public function movePositionDown() { $this->movePositionUpOrDown(false); } @@ -85,8 +88,9 @@ trait PositionManagementTrait { $my_position = $this->getPosition(); // Find object to exchange position with - $search = $this->createQuery() - ->filterByParent($this->getParent()); + $search = $this->createQuery(); + + if (method_exists($this, 'getParent')) $search->filterByParent($this->getParent()); // Up or down ? if ($up === true) { @@ -103,18 +107,21 @@ trait PositionManagementTrait { // If we found the proper object, exchange their positions if ($result) { - $cnx = Propel::getWriteConnection(CategoryTableMap::DATABASE_NAME); + // Find DATABASE_NAME constant + $mapClassName = self::TABLE_MAP; + $database_name = $mapClassName::DATABASE_NAME; + + $cnx = Propel::getWriteConnection($database_name); $cnx->beginTransaction(); try { $this - ->setDispatcher($this->getDispatcher()) ->setPosition($result->getPosition()) ->save() ; - $result->setPosition($my_position)->save(); + $result->setDispatcher($this->getDispatcher())->setPosition($my_position)->save(); $cnx->commit(); } catch (Exception $e) { @@ -128,7 +135,7 @@ trait PositionManagementTrait { * * @param newPosition */ - protected function changeAbsolutePosition($newPosition) + public function changeAbsolutePosition($newPosition) { // The current position $current_position = $this->getPosition(); @@ -136,7 +143,9 @@ trait PositionManagementTrait { if ($newPosition != null && $newPosition > 0 && $newPosition != $current_position) { // Find categories to offset - $search = $this->createQuery()->filterByParent($this->getParent()); + $search = $this->createQuery(); + + if (method_exists($this, 'getParent')) $search->filterByParent($this->getParent()); if ($newPosition > $current_position) { // The new position is after the current position -> we will offset + 1 all categories located between us and the new position @@ -158,11 +167,10 @@ trait PositionManagementTrait { try { foreach ($results as $result) { - $result->setPosition($result->getPosition() + $delta)->save($cnx); + $result->setDispatcher($this->getDispatcher())->setPosition($result->getPosition() + $delta)->save($cnx); } $this - ->setDispatcher($this->getDispatcher()) ->setPosition($newPosition) ->save($cnx) ; diff --git a/templates/admin/default/currencies.html b/templates/admin/default/currencies.html index d446de776..7b7610d59 100644 --- a/templates/admin/default/currencies.html +++ b/templates/admin/default/currencies.html @@ -138,7 +138,7 @@ {admin_position_block permission="admin.currencies.edit" - path="/admin/configuration/currencies" + path="/admin/configuration/currencies/update-position" url_parameter="currency_id" in_place_edit_class="currencyPositionChange" position="$POSITION" @@ -371,7 +371,7 @@ placement : 'left', success : function(response, newValue) { // The URL template - var url = "{url path='/admin/configuration/currencies/updatePosition' currency_id='__ID__' position='__POS__'}"; + var url = "{url path='/admin/configuration/currencies/update-position' currency_id='__ID__' position='__POS__'}"; // Perform subtitutions url = url.replace('__ID__', $(this).data('id')) From c8a5734a9e7e53ee14cec53a3b46e9c568dd258f Mon Sep 17 00:00:00 2001 From: franck Date: Fri, 6 Sep 2013 01:55:16 +0200 Subject: [PATCH 2/2] Added Tools\URL test case, and a test case superclass for initializing Tools\URL --- .../Thelia/Config/Resources/routing/admin.xml | 2 +- core/lib/Thelia/Tests/Action/ImageTest.php | 16 +- .../Core/Template/Element/BaseLoopTestor.php | 2 +- .../Thelia/Tests/TestCaseWithURLToolSetup.php | 64 ++++++ core/lib/Thelia/Tests/Tools/URLTest.php | 198 ++++++++++++++++++ core/lib/Thelia/Tools/URL.php | 33 ++- 6 files changed, 279 insertions(+), 36 deletions(-) create mode 100644 core/lib/Thelia/Tests/TestCaseWithURLToolSetup.php diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index ac652017c..313fb3a57 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -113,7 +113,7 @@ Thelia\Controller\Admin\CurrencyController::deleteAction - + Thelia\Controller\Admin\CurrencyController::updatePositionAction diff --git a/core/lib/Thelia/Tests/Action/ImageTest.php b/core/lib/Thelia/Tests/Action/ImageTest.php index 808a246f6..8cc6e5644 100755 --- a/core/lib/Thelia/Tests/Action/ImageTest.php +++ b/core/lib/Thelia/Tests/Action/ImageTest.php @@ -37,7 +37,7 @@ use Thelia\Tools\URL; * * @package Thelia\Tests\Action\ImageTest */ -class ImageTest extends \PHPUnit_Framework_TestCase +class ImageTest extends \Thelia\Tests\TestCaseWithURLToolSetup { protected $request; @@ -49,27 +49,13 @@ class ImageTest extends \PHPUnit_Framework_TestCase $dispatcher = $this->getMock("Symfony\Component\EventDispatcher\EventDispatcherInterface"); - $url = new URL($container, 'dev'); - $container->set("event_dispatcher", $dispatcher); - $container->set("thelia.url.manager", $dispatcher); $request = new Request(); $request->setSession($this->session); $container->set("request", $request); - $router = $this->getMock("Symfony\Component\Routing\Router"); - $container->set("router.admin", $router); - - $context = $this->getMock("Symfony\Component\Routing\RequestContext"); - $context->setHost('localhost'); - $context->setPort(80); - $context->setScheme('http'); - $context->getBaseUrl('/tagada/tsointsoin/'); - - $router->setContext($context); - return $container; } diff --git a/core/lib/Thelia/Tests/Core/Template/Element/BaseLoopTestor.php b/core/lib/Thelia/Tests/Core/Template/Element/BaseLoopTestor.php index c4b0192bc..7d14fbbc5 100755 --- a/core/lib/Thelia/Tests/Core/Template/Element/BaseLoopTestor.php +++ b/core/lib/Thelia/Tests/Core/Template/Element/BaseLoopTestor.php @@ -34,7 +34,7 @@ use Thelia\Core\HttpFoundation\Session\Session; * @author Etienne Roudeix * */ -abstract class BaseLoopTestor extends \PHPUnit_Framework_TestCase +abstract class BaseLoopTestor extends \Thelia\Tests\TestCaseWithURLToolSetup { protected $request; protected $dispatcher; diff --git a/core/lib/Thelia/Tests/TestCaseWithURLToolSetup.php b/core/lib/Thelia/Tests/TestCaseWithURLToolSetup.php new file mode 100644 index 000000000..927f7cf62 --- /dev/null +++ b/core/lib/Thelia/Tests/TestCaseWithURLToolSetup.php @@ -0,0 +1,64 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Tests; + +/** + * This class provides URL Tool class initialisation + * + * @package Thelia\Tests\TestCaseWithURLSetup + */ +class TestCaseWithURLToolSetup extends \PHPUnit_Framework_TestCase { + + + public function __construct() { + $this->setupURLTool(); + } + + protected function setupURLTool() { + + $container = new \Symfony\Component\DependencyInjection\ContainerBuilder(); + + $context = new \Symfony\Component\Routing\RequestContext( + '/thelia/index.php', + 'GET', + 'localhost', + 'http', + 80, + 443, + '/path/to/action' + ); + + $router = $this->getMockBuilder("Symfony\Component\Routing\Router") + ->disableOriginalConstructor() + ->getMock(); + + $router->expects($this->any()) + ->method('getContext') + ->will($this->returnValue($context)); + + $container->set("router.admin", $router); + + new \Thelia\Tools\URL($container); + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Tests/Tools/URLTest.php b/core/lib/Thelia/Tests/Tools/URLTest.php index 0c99651b4..86804f5c0 100755 --- a/core/lib/Thelia/Tests/Tools/URLTest.php +++ b/core/lib/Thelia/Tests/Tools/URLTest.php @@ -28,10 +28,208 @@ use Thelia\Tools\URL; /** * * @author Etienne Roudeix + * @author Franck Allimant * */ class URLTest extends \PHPUnit_Framework_TestCase { + protected $context; + + public function setUp() + { + $container = new \Symfony\Component\DependencyInjection\ContainerBuilder(); + + $router = $this->getMockBuilder("Symfony\Component\Routing\Router") + ->disableOriginalConstructor() + ->getMock(); + + $this->context = new \Symfony\Component\Routing\RequestContext( + '/thelia/index.php', + 'GET', + 'localhost', + 'http', + 80, + 443, + '/path/to/action' + ); + + $router->expects($this->any()) + ->method('getContext') + ->will($this->returnValue($this->context)); + + $container->set("router.admin", $router); + + new \Thelia\Tools\URL($container); + } + + public function testGetIndexPage() { + + $this->context->setBaseUrl('/thelia/index.php'); + $url = \Thelia\Tools\URL::getInstance()->getIndexPage(); + $this->assertEquals('http://localhost/thelia/index.php', $url); + + $this->context->setBaseUrl('/thelia/'); + $url = \Thelia\Tools\URL::getInstance()->getIndexPage(); + $this->assertEquals('http://localhost/thelia/', $url); + + $this->context->setBaseUrl('/thelia'); + $url = \Thelia\Tools\URL::getInstance()->getIndexPage(); + $this->assertEquals('http://localhost/thelia', $url); + + $this->context->setBaseUrl('/'); + $url = \Thelia\Tools\URL::getInstance()->getIndexPage(); + $this->assertEquals('http://localhost/', $url); + } + + public function testGetBaseUrl() { + $this->context->setBaseUrl('/thelia/index.php'); + $url = \Thelia\Tools\URL::getInstance()->getBaseUrl(); + $this->assertEquals('http://localhost/thelia/index.php', $url); + + $this->context->setBaseUrl('/thelia/'); + $url = \Thelia\Tools\URL::getInstance()->getBaseUrl(); + $this->assertEquals('http://localhost/thelia/', $url); + + $this->context->setBaseUrl('/'); + $url = \Thelia\Tools\URL::getInstance()->getBaseUrl(); + $this->assertEquals('http://localhost/', $url); + } + + public function testAbsoluteUrl() { + $this->context->setBaseUrl('/'); + $url = \Thelia\Tools\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'); + $this->assertEquals('http://localhost/thelia/path/to/action', $url); + + $this->context->setBaseUrl('/thelia'); + $url = \Thelia\Tools\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'); + $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'); + $this->assertEquals('http://myhost/path/to/action', $url); + + $this->context->setBaseUrl('/thelia/'); + $url = \Thelia\Tools\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'); + $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")); + $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")); + $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")); + $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")); + $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")); + $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")); + $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")); + $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")); + $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")); + $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")); + $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")); + $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); + $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); + $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); + $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); + $this->assertEquals('http://localhost/path/to/action?p1=v1&p2=v2', $url); + + } + + public function testAbsoluteUrlFromIndexWithParameters() { + $this->context->setBaseUrl('/thelia/index.php'); + $url = \Thelia\Tools\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")); + $this->assertEquals('http://localhost/thelia/?p1=v1&p2=v2', $url); + + $this->context->setBaseUrl('/'); + $url = \Thelia\Tools\URL::getInstance()->absoluteUrl('/', array("p1" => "v1", "p2" => "v2")); + $this->assertEquals('http://localhost/?p1=v1&p2=v2', $url); + + } + + public function testAbsoluteUrlPathOnlyFromIndexWithParameters() { + $this->context->setBaseUrl('/thelia/index.php'); + $url = \Thelia\Tools\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); + $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); + $this->assertEquals('http://localhost/?p1=v1&p2=v2', $url); + + } + public function testRetrieve() { diff --git a/core/lib/Thelia/Tools/URL.php b/core/lib/Thelia/Tools/URL.php index 777e4543c..152f82895 100755 --- a/core/lib/Thelia/Tools/URL.php +++ b/core/lib/Thelia/Tools/URL.php @@ -27,33 +27,30 @@ use Thelia\Model\ConfigQuery; use Thelia\Rewriting\RewritingResolver; use Thelia\Rewriting\RewritingRetriever; -use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Thelia\Core\HttpFoundation\Request; -use Symfony\Component\DependencyInjection\ContainerAware; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\Routing\RequestContext; class URL { protected $resolver = null; protected $retriever = null; - protected $container; - protected $environment; + protected $requestContext; const PATH_TO_FILE = true; const WITH_INDEX_PAGE = false; - private static $instance = null; + protected static $instance = null; - public function __construct(ContainerInterface $container, $environment) + public function __construct(ContainerInterface $container) { // Allow singleton style calls once intanciated. // For this to work, the URL service has to be instanciated very early. This is done manually // in TheliaHttpKernel, by calling $this->container->get('thelia.url.manager'); self::$instance = $this; - $this->container = $container; - $this->environment = $environment; + $this->requestContext = $container->get('router.admin')->getContext(); $this->retriever = new RewritingRetriever(); $this->resolver = new RewritingResolver(); @@ -79,28 +76,26 @@ class URL */ public function getBaseUrl() { - $requestContext = $this->container->get('router.admin')->getContext(); + if ($host = $this->requestContext->getHost()) { - if ($host = $requestContext->getHost()) { - - $scheme = $requestContext->getScheme(); + $scheme = $this->requestContext->getScheme(); $port = ''; - if ('http' === $scheme && 80 != $requestContext->getHttpPort()) { - $port = ':'.$requestContext->getHttpPort(); - } elseif ('https' === $scheme && 443 != $requestContext->getHttpsPort()) { - $port = ':'.$requestContext->getHttpsPort(); + if ('http' === $scheme && 80 != $this->requestContext->getHttpPort()) { + $port = ':'.$this->requestContext->getHttpPort(); + } elseif ('https' === $scheme && 443 != $this->requestContext->getHttpsPort()) { + $port = ':'.$this->requestContext->getHttpsPort(); } $schemeAuthority = "$scheme://$host"."$port"; } - return $schemeAuthority.$requestContext->getBaseUrl().'/'; + return $schemeAuthority.$this->requestContext->getBaseUrl(); } /** - * @return string the index page, which is basically the base_url in prod environment. + * @return string the index page, which is in fact the base URL. */ public function getIndexPage() { @@ -133,7 +128,7 @@ class URL } // Normalize the given path - $base = $base_url . ltrim($path, '/'); + $base = rtrim($base_url, '/') . '/' . ltrim($path, '/'); } else $base = $path;