View source
<?php
namespace Drupal\Tests\Core\Utility;
use Drupal\Core\GeneratedUrl;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Utility\UnroutedUrlAssembler;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class UnroutedUrlAssemblerTest extends UnitTestCase {
protected $requestStack;
protected $configFactory;
protected $unroutedUrlAssembler;
protected $pathProcessor;
protected function setUp() {
parent::setUp();
$this->requestStack = new RequestStack();
$this->pathProcessor = $this
->getMock('Drupal\\Core\\PathProcessor\\OutboundPathProcessorInterface');
$this->unroutedUrlAssembler = new UnroutedUrlAssembler($this->requestStack, $this->pathProcessor);
}
public function testAssembleWithNeitherExternalNorDomainLocalUri() {
$this->unroutedUrlAssembler
->assemble('wrong-url');
}
public function testAssembleWithLeadingSlash() {
$this->unroutedUrlAssembler
->assemble('/drupal.org');
}
public function testAssembleWithExternalUrl($uri, array $options, $expected) {
$this
->setupRequestStack(FALSE);
$this
->assertEquals($expected, $this->unroutedUrlAssembler
->assemble($uri, $options));
$generated_url = $this->unroutedUrlAssembler
->assemble($uri, $options, TRUE);
$this
->assertEquals($expected, $generated_url
->getGeneratedUrl());
$this
->assertInstanceOf('\\Drupal\\Core\\Render\\BubbleableMetadata', $generated_url);
}
public function providerTestAssembleWithExternalUrl() {
return [
[
'http://example.com/test',
[],
'http://example.com/test',
],
[
'http://example.com/test',
[
'fragment' => 'example',
],
'http://example.com/test#example',
],
[
'http://example.com/test',
[
'fragment' => 'example',
],
'http://example.com/test#example',
],
[
'http://example.com/test',
[
'query' => [
'foo' => 'bar',
],
],
'http://example.com/test?foo=bar',
],
[
'http://example.com/test',
[
'https' => TRUE,
],
'https://example.com/test',
],
[
'https://example.com/test',
[
'https' => FALSE,
],
'http://example.com/test',
],
[
'https://example.com/test?foo=1#bar',
[],
'https://example.com/test?foo=1#bar',
],
[
'//www.drupal.org',
[],
'//www.drupal.org',
],
];
}
public function testAssembleWithLocalUri($uri, array $options, $subdir, $expected) {
$this
->setupRequestStack($subdir);
$this
->assertEquals($expected, $this->unroutedUrlAssembler
->assemble($uri, $options));
}
public function providerTestAssembleWithLocalUri() {
return [
[
'base:example',
[],
FALSE,
'/example',
],
[
'base:example',
[
'query' => [
'foo' => 'bar',
],
],
FALSE,
'/example?foo=bar',
],
[
'base:example',
[
'query' => [
'foo' => '"bar"',
],
],
FALSE,
'/example?foo=%22bar%22',
],
[
'base:example',
[
'query' => [
'foo' => '"bar"',
'zoo' => 'baz',
],
],
FALSE,
'/example?foo=%22bar%22&zoo=baz',
],
[
'base:example',
[
'fragment' => 'example',
],
FALSE,
'/example#example',
],
[
'base:example',
[],
TRUE,
'/subdir/example',
],
[
'base:example',
[
'query' => [
'foo' => 'bar',
],
],
TRUE,
'/subdir/example?foo=bar',
],
[
'base:example',
[
'fragment' => 'example',
],
TRUE,
'/subdir/example#example',
],
[
'base:/drupal.org',
[],
FALSE,
'/drupal.org',
],
];
}
public function testAssembleWithNotEnabledProcessing() {
$this
->setupRequestStack(FALSE);
$this->pathProcessor
->expects($this
->never())
->method('processOutbound');
$result = $this->unroutedUrlAssembler
->assemble('base:test-uri', []);
$this
->assertEquals('/test-uri', $result);
}
public function testAssembleWithEnabledProcessing() {
$this
->setupRequestStack(FALSE);
$this->pathProcessor
->expects($this
->exactly(2))
->method('processOutbound')
->willReturnCallback(function ($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
if ($bubbleable_metadata) {
$bubbleable_metadata
->setCacheContexts([
'some-cache-context',
]);
}
return 'test-other-uri';
});
$result = $this->unroutedUrlAssembler
->assemble('base:test-uri', [
'path_processing' => TRUE,
]);
$this
->assertEquals('/test-other-uri', $result);
$result = $this->unroutedUrlAssembler
->assemble('base:test-uri', [
'path_processing' => TRUE,
], TRUE);
$expected_generated_url = new GeneratedUrl();
$expected_generated_url
->setGeneratedUrl('/test-other-uri')
->setCacheContexts([
'some-cache-context',
]);
$this
->assertEquals($expected_generated_url, $result);
}
protected function setupRequestStack($subdir) {
$server = [];
if ($subdir) {
$server = [
'SCRIPT_NAME' => '/subdir/index.php',
'SCRIPT_FILENAME' => $this->root . '/index.php',
'SERVER_NAME' => 'http://www.example.com',
];
$request = Request::create('/subdir/');
}
else {
$request = Request::create('/');
}
$request->server
->add($server);
$this->requestStack
->push($request);
}
}