Url: Fix detection of the current base url when stripping it from a given path

This commit is contained in:
Johannes Meyer 2015-10-01 10:38:22 +02:00
parent 5f7f0a9e89
commit 85e7e7920a
2 changed files with 9 additions and 9 deletions

View File

@ -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();

View File

@ -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&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'
);
}