View source
<?php
namespace Drupal\system\Tests\RouteProcessor;
use Drupal\Core\Cache\Cache;
use Drupal\Core\GeneratedUrl;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\simpletest\KernelTestBase;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
class RouteNoneTest extends KernelTestBase {
public static $modules = [
'system',
];
protected $urlGenerator;
protected function setUp() {
parent::setUp();
$this
->installSchema('system', [
'router',
]);
\Drupal::service('router.builder')
->rebuild();
$this->urlGenerator = \Drupal::urlGenerator();
}
public function testProcessOutbound() {
$expected_cacheability = (new BubbleableMetadata())
->setCacheMaxAge(Cache::PERMANENT);
$request_stack = \Drupal::requestStack();
$request_context = \Drupal::service('router.request_context');
$server = [
'SCRIPT_NAME' => '/subdir/index.php',
'SCRIPT_FILENAME' => \Drupal::root() . '/index.php',
'SERVER_NAME' => 'http://www.example.com',
];
$request = Request::create('/subdir', 'GET', [], [], [], $server);
$request->attributes
->set(RouteObjectInterface::ROUTE_NAME, '<front>');
$request->attributes
->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/'));
$request_stack
->push($request);
$request_context
->fromRequest($request);
$url = GeneratedUrl::createFromObject($expected_cacheability)
->setGeneratedUrl('');
$this
->assertEqual($url, $this->urlGenerator
->generateFromRoute('<none>', [], [], TRUE, TRUE));
$url = GeneratedUrl::createFromObject($expected_cacheability)
->setGeneratedUrl('#test-fragment');
$this
->assertEqual($url, $this->urlGenerator
->generateFromRoute('<none>', [], [
'fragment' => 'test-fragment',
], TRUE));
$server = [
'SCRIPT_NAME' => '/subdir/index.php',
'SCRIPT_FILENAME' => \Drupal::root() . '/index.php',
'SERVER_NAME' => 'http://www.example.com',
];
$request = Request::create('/subdir/node/add', 'GET', [], [], [], $server);
$request->attributes
->set(RouteObjectInterface::ROUTE_NAME, 'node.add');
$request->attributes
->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/node/add'));
$request_stack
->push($request);
$request_context
->fromRequest($request);
$url = GeneratedUrl::createFromObject($expected_cacheability)
->setGeneratedUrl('');
$this
->assertEqual($url, $this->urlGenerator
->generateFromRoute('<none>', [], [], TRUE, TRUE));
$url = GeneratedUrl::createFromObject($expected_cacheability)
->setGeneratedUrl('#test-fragment');
$this
->assertEqual($url, $this->urlGenerator
->generateFromRoute('<none>', [], [
'fragment' => 'test-fragment',
], TRUE));
$server = [
'SCRIPT_NAME' => '/index.php',
'SCRIPT_FILENAME' => \Drupal::root() . '/index.php',
'SERVER_NAME' => 'http://www.example.com',
];
$request = Request::create('/', 'GET', [], [], [], $server);
$request->attributes
->set(RouteObjectInterface::ROUTE_NAME, '<front>');
$request->attributes
->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/'));
$request_stack
->push($request);
$request_context
->fromRequest($request);
$url = GeneratedUrl::createFromObject($expected_cacheability)
->setGeneratedUrl('');
$this
->assertEqual($url, $this->urlGenerator
->generateFromRoute('<none>', [], [], TRUE, TRUE));
$url = GeneratedUrl::createFromObject($expected_cacheability)
->setGeneratedUrl('#test-fragment');
$this
->assertEqual($url, $this->urlGenerator
->generateFromRoute('<none>', [], [
'fragment' => 'test-fragment',
], TRUE));
$server = [
'SCRIPT_NAME' => '/index.php',
'SCRIPT_FILENAME' => \Drupal::root() . '/index.php',
'SERVER_NAME' => 'http://www.example.com',
];
$request = Request::create('/node/add', 'GET', [], [], [], $server);
$request->attributes
->set(RouteObjectInterface::ROUTE_NAME, 'node.add');
$request->attributes
->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/node/add'));
$request_stack
->push($request);
$request_context
->fromRequest($request);
$url = GeneratedUrl::createFromObject($expected_cacheability)
->setGeneratedUrl('');
$this
->assertEqual($url, $this->urlGenerator
->generateFromRoute('<none>', [], [], TRUE, TRUE));
$url = GeneratedUrl::createFromObject($expected_cacheability)
->setGeneratedUrl('#test-fragment');
$this
->assertEqual($url, $this->urlGenerator
->generateFromRoute('<none>', [], [
'fragment' => 'test-fragment',
], TRUE));
}
}