Skip to content

Commit

Permalink
Url: Fix detection of the current base url when stripping it from a g…
Browse files Browse the repository at this point in the history
…iven path
  • Loading branch information
Johannes Meyer committed Oct 1, 2015
1 parent 5f7f0a9 commit 85e7e79
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
9 changes: 6 additions & 3 deletions library/Icinga/Web/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,12 @@ public static function fromPath($url, array $params = array(), $request = null)
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();
Expand Down
9 changes: 3 additions & 6 deletions test/php/library/Icinga/Web/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,14 @@ public function testWhetherGetAbsoluteUrlReturnsTheAbsoluteUrl()
);
}

/**
* @depends testWhetherFromPathProperlyRecognizesAndDecodesQueryParameters
*/
public function testWhetherGetRelativeUrlReturnsTheRelativeUrl()
public function testWhetherGetRelativeUrlReturnsTheEmptyStringForAbsoluteUrls()
{
$url = Url::fromPath('/my/test/url.html?param=val&param2=val2');

$this->assertEquals(
'my/test/url.html?param=val&param2=val2',
'',
$url->getRelativeUrl(),
'Url::getRelativeUrl does not return the relative url'
'Url::getRelativeUrl does not return the empty string for absolute urls'
);
}

Expand Down

0 comments on commit 85e7e79

Please sign in to comment.