/** * Bubbles the bubbleable metadata to the current render context. * * @param \Drupal\Core\GeneratedUrl $generated_url * The generated URL whose bubbleable metadata to bubble. * @param array $options * (optional) The URL options. Defaults to none. */ protected function bubble(GeneratedUrl $generated_url, array $options = []) { // Bubbling metadata makes sense only if the code is executed inside a // render context. All code running outside controllers has no render // context by default, so URLs used there are not supposed to affect the // response cacheability. if ($this->renderer->hasRenderContext()) { $build = []; $generated_url->applyTo($build); $this->renderer->render($build); } }
/** * 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' => \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('/subdir/'); $this->assertEqual($url, $this->urlGenerator->generateFromRoute('<current>', [], [], TRUE)); // Test request with subdir on other page. $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('/subdir/node/add'); $this->assertEqual($url, $this->urlGenerator->generateFromRoute('<current>', [], [], TRUE)); // Test request without subdir on the homepage. $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('<current>', [], [], TRUE)); // Test request without subdir on other page. $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('/node/add'); $this->assertEqual($url, $this->urlGenerator->generateFromRoute('<current>', [], [], TRUE)); // Test request without a found route. This happens for example on an // not found exception page. $server = ['SCRIPT_NAME' => '/index.php', 'SCRIPT_FILENAME' => \Drupal::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->assertEqual($url, $this->urlGenerator->generateFromRoute('<current>', [], [], TRUE)); }
/** * @covers ::assemble */ 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); }
/** * Tests the output process. */ public function testProcessOutbound() { $expected_cacheability = (new BubbleableMetadata())->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' => \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)); // Test request with subdir on other page. $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)); // Test request without subdir on the homepage. $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)); // Test request without subdir on other page. $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)); }