class RouteProcessorCurrentIntegrationTest
Same name and namespace in other branches
- 11.x core/tests/Drupal/KernelTests/Core/RouteProcessor/RouteProcessorCurrentIntegrationTest.php \Drupal\KernelTests\Core\RouteProcessor\RouteProcessorCurrentIntegrationTest
- 10 core/tests/Drupal/KernelTests/Core/RouteProcessor/RouteProcessorCurrentIntegrationTest.php \Drupal\KernelTests\Core\RouteProcessor\RouteProcessorCurrentIntegrationTest
- 8.9.x core/tests/Drupal/KernelTests/Core/RouteProcessor/RouteProcessorCurrentIntegrationTest.php \Drupal\KernelTests\Core\RouteProcessor\RouteProcessorCurrentIntegrationTest
Tests the <current> route processor.
@group route_processor
Hierarchy
- class \Drupal\KernelTests\KernelTestBase implements \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\KernelTests\AssertLegacyTrait, \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\ExtensionListTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\KernelTests\Core\RouteProcessor\RouteProcessorCurrentIntegrationTest extends \Drupal\KernelTests\KernelTestBase
Expanded class hierarchy of RouteProcessorCurrentIntegrationTest
See also
\Drupal\Core\RouteProcessor\RouteProcessorCurrent
File
-
core/
tests/ Drupal/ KernelTests/ Core/ RouteProcessor/ RouteProcessorCurrentIntegrationTest.php, line 19
Namespace
Drupal\KernelTests\Core\RouteProcessorView source
class RouteProcessorCurrentIntegrationTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
];
/**
* The URL generator.
*
* @var \Drupal\Core\Routing\UrlGeneratorInterface
*/
protected $urlGenerator;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->urlGenerator = \Drupal::urlGenerator();
}
/**
* Tests the output process.
*/
public function testProcessOutbound() {
$expected_cacheability = (new BubbleableMetadata())->addCacheContexts([
'route',
])
->setCacheMaxAge(Cache::PERMANENT);
$request_stack = \Drupal::requestStack();
/** @var \Symfony\Component\Routing\RequestContext $request_context */
$request_context = \Drupal::service('router.request_context');
// Test request with subdir on homepage.
$server = [
'SCRIPT_NAME' => '/subdir/index.php',
'SCRIPT_FILENAME' => $this->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('/subdir/');
$this->assertEquals($this->urlGenerator
->generateFromRoute('<current>', [], [], TRUE), $url);
// Test request with subdir on other page.
$server = [
'SCRIPT_NAME' => '/subdir/index.php',
'SCRIPT_FILENAME' => $this->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('/subdir/node/add');
$this->assertEquals($this->urlGenerator
->generateFromRoute('<current>', [], [], TRUE), $url);
// Test request without subdir on the homepage.
$server = [
'SCRIPT_NAME' => '/index.php',
'SCRIPT_FILENAME' => $this->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->assertEquals($this->urlGenerator
->generateFromRoute('<current>', [], [], TRUE), $url);
// Test request without subdir on other page.
$server = [
'SCRIPT_NAME' => '/index.php',
'SCRIPT_FILENAME' => $this->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('/node/add');
$this->assertEquals($this->urlGenerator
->generateFromRoute('<current>', [], [], TRUE), $url);
// Test request without a found route. This happens for example on an
// not found exception page.
$server = [
'SCRIPT_NAME' => '/index.php',
'SCRIPT_FILENAME' => $this->root . '/index.php',
'SERVER_NAME' => 'http://www.example.com',
];
$request = Request::create('/invalid-path', 'GET', [], [], [], $server);
$request_stack->push($request);
$request_context->fromRequest($request);
// In case we have no routing, the current route should point to the front,
// and the cacheability does not depend on the 'route' cache context, since
// no route was involved at all: this is fallback behavior.
$url = GeneratedUrl::createFromObject((new BubbleableMetadata())->setCacheMaxAge(Cache::PERMANENT))
->setGeneratedUrl('/');
$this->assertEquals($this->urlGenerator
->generateFromRoute('<current>', [], [], TRUE), $url);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.