From 85e7e7920a97f20105a90c58dd27867e28b901eb Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 1 Oct 2015 10:38:22 +0200 Subject: [PATCH] Url: Fix detection of the current base url when stripping it from a given path --- library/Icinga/Web/Url.php | 9 ++++++--- test/php/library/Icinga/Web/UrlTest.php | 9 +++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/library/Icinga/Web/Url.php b/library/Icinga/Web/Url.php index e314aa145..3a9c9d1f2 100644 --- a/library/Icinga/Web/Url.php +++ b/library/Icinga/Web/Url.php @@ -161,9 +161,12 @@ class Url if ($urlPath && $urlPath[0] === '/') { if ($baseUrl) { $urlPath = substr($urlPath, 1); - } elseif (strpos($urlPath, $request->getBaseUrl()) === 0) { - $baseUrl = $request->getBaseUrl(); - $urlPath = substr($urlPath, strlen($baseUrl) + 1); + } else { + $requestBaseUrl = $request->getBaseUrl(); + if ($requestBaseUrl && $requestBaseUrl !== '/' && strpos($urlPath, $requestBaseUrl) === 0) { + $urlPath = substr($urlPath, strlen($requestBaseUrl) + 1); + $baseUrl = $requestBaseUrl; + } } } elseif (! $baseUrl) { $baseUrl = $request->getBaseUrl(); diff --git a/test/php/library/Icinga/Web/UrlTest.php b/test/php/library/Icinga/Web/UrlTest.php index 5dd3b7045..21d87bae7 100644 --- a/test/php/library/Icinga/Web/UrlTest.php +++ b/test/php/library/Icinga/Web/UrlTest.php @@ -158,17 +158,14 @@ class UrlTest extends BaseTestCase ); } - /** - * @depends testWhetherFromPathProperlyRecognizesAndDecodesQueryParameters - */ - public function testWhetherGetRelativeUrlReturnsTheRelativeUrl() + public function testWhetherGetRelativeUrlReturnsTheEmptyStringForAbsoluteUrls() { $url = Url::fromPath('/my/test/url.html?param=val¶m2=val2'); $this->assertEquals( - 'my/test/url.html?param=val¶m2=val2', + '', $url->getRelativeUrl(), - 'Url::getRelativeUrl does not return the relative url' + 'Url::getRelativeUrl does not return the empty string for absolute urls' ); }