diff --git a/core/lib/Drupal/Core/Utility/UnroutedUrlAssembler.php b/core/lib/Drupal/Core/Utility/UnroutedUrlAssembler.php index bc21ea8..03e13c2 100644 --- a/core/lib/Drupal/Core/Utility/UnroutedUrlAssembler.php +++ b/core/lib/Drupal/Core/Utility/UnroutedUrlAssembler.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Utility; +use Drupal\Core\Url; use Drupal\Component\Utility\String; use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Config\ConfigFactoryInterface; @@ -109,9 +110,7 @@ protected function buildLocalUrl($uri, array $options = []) { $request = $this->requestStack->getCurrentRequest(); // Remove the base: scheme. - // @todo Consider using a class constant for this in - // https://www.drupal.org/node/2417459 - $uri = substr($uri, 5); + $uri = substr($uri, strlen(Url::SCHEME_BASE)); // Allow (outbound) path processing, if needed. A valid use case is the path // alias overview form: diff --git a/core/tests/Drupal/Tests/Core/UrlTest.php b/core/tests/Drupal/Tests/Core/UrlTest.php index dcc2c66..bf42140 100644 --- a/core/tests/Drupal/Tests/Core/UrlTest.php +++ b/core/tests/Drupal/Tests/Core/UrlTest.php @@ -773,6 +773,66 @@ public function testFromRouteUriWithMissingRouteName() { } /** + * Tests the fromBaseRelativePath() + * + * @covers ::fromBaseRelativePath + * + * @dataProvider providerTestFromBaseRelativePath + */ + public function testFromBaseRelativePath($path, $options, $uri_string) { + $url = Url::fromBaseRelativePath($path, $options); + + $this->assertFalse($url->isRouted()); + $this->assertSame(Url::SCHEME_BASE, parse_url($url->toUriString(), PHP_URL_SCHEME)); + $this->assertSame($uri_string, $url->toUriString()); + } + + /** + * Data provider for testing base relative paths. + */ + public function providerTestFromBaseRelativePath() { + return [ + ['robots.txt', [], Url::SCHEME_BASE . ':robots.txt'], + ['/robots.txt', [], Url::SCHEME_BASE . ':robots.txt'], + ['//robots.txt', [], Url::SCHEME_BASE . ':robots.txt'], + ['/blog/cats?foo=bar', [], Url::SCHEME_BASE . ':blog/cats?foo=bar'], + ['/blog/cats', ['query'=>['foo' => 'bar']], Url::SCHEME_BASE . ':blog/cats?foo=bar'], + ['/blog/cats#foo', [], Url::SCHEME_BASE . ':blog/cats#foo'], + ['/blog/cats', ['fragment'=>'foo'], Url::SCHEME_BASE . ':blog/cats#foo'], + ]; + } + + /** + * Tests the fromHostRelativePath() + * + * @covers ::fromHostRelativePath + * + * @dataProvider providerTestFromHostRelativePath + */ + public function testFromHostRelativePath($path, $options, $uri_string) { + $url = Url::fromHostRelativePath($path, $options); + + $this->assertFalse($url->isRouted()); + $this->assertSame(Url::SCHEME_BASE, parse_url($url->toUriString(), PHP_URL_SCHEME)); + $this->assertSame($uri_string, $url->toUriString()); + } + + /** + * Data provider for testing base relative paths. + */ + public function providerTestFromHostRelativePath() { + return [ + ['index.html', [], Url::SCHEME_BASE . ':/index.html'], + ['/index.html', [], Url::SCHEME_BASE . ':/index.html'], + ['//index.html', [], Url::SCHEME_BASE . ':/index.html'], + ['/blog/cats?foo=bar', [], Url::SCHEME_BASE . ':/blog/cats?foo=bar'], + ['/blog/cats', ['query'=>['foo' => 'bar']], Url::SCHEME_BASE . ':/blog/cats?foo=bar'], + ['/blog/cats#foo', [], Url::SCHEME_BASE . ':/blog/cats#foo'], + ['/blog/cats', ['fragment'=>'foo'], Url::SCHEME_BASE . ':/blog/cats#foo'], + ]; + } + + /** * Creates a mock access manager for the access tests. * * @param bool $access