/** * Tests link rendering with a URL and options. * * @dataProvider providerTestRenderAsLinkWithUrlAndOptions * @covers ::renderAsLink */ public function testRenderAsLinkWithUrlAndOptions(Url $url, $alter, Url $expected_url, $url_path, Url $expected_link_url, $link_html, $final_html = NULL) { $alter += ['make_link' => TRUE, 'url' => $url]; $final_html = isset($final_html) ? $final_html : $link_html; $this->setUpUrlIntegrationServices(); $this->setupDisplayWithEmptyArgumentsAndFields(); $field = $this->setupTestField(['alter' => $alter]); $field->field_alias = 'key'; $row = new ResultRow(['key' => 'value']); $expected_url->setOptions($expected_url->getOptions() + $this->defaultUrlOptions); $expected_link_url->setUrlGenerator($this->urlGenerator); $expected_url_options = $expected_url->getOptions(); unset($expected_url_options['attributes']); $this->urlGenerator->expects($this->once())->method('generateFromRoute')->with($expected_url->getRouteName(), $expected_url->getRouteParameters(), $expected_url_options)->willReturn($url_path); $result = $field->advancedRender($row); $this->assertEquals($final_html, $result); }
/** * Prior to set the response it check if we can redirect. * * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event * The event object. * @param \Drupal\Core\Url $url * The Url where we want to redirect. */ protected function setResponse(GetResponseEvent $event, Url $url) { $request = $event->getRequest(); $this->context->fromRequest($request); parse_str($request->getQueryString(), $query); $url->setOption('query', $query); $url->setAbsolute(TRUE); // We can only check access for routed URLs. if (!$url->isRouted() || $this->redirectChecker->canRedirect($url->getRouteName(), $request)) { // Add the 'rendered' cache tag, so that we can invalidate all responses // when settings are changed. $headers = ['X-Drupal-Cache-Tags' => 'rendered']; $event->setResponse(new RedirectResponse($url->toString(), 301, $headers)); } }
/** * Asserts that a given URL object matches the expectations. * * @param string $expected_route_name * The expected route name of the generated URL. * @param array $expected_route_parameters * The expected route parameters of the generated URL. * @param \Drupal\Core\Entity\Entity|\PHPUnit_Framework_MockObject_MockObject $entity * The entity that is expected to be set as a URL option. * @param bool $has_language * Whether or not the URL is expected to have a language option. * @param \Drupal\Core\Url $url * The URL option to make the assertions on. */ protected function assertUrl($expected_route_name, array $expected_route_parameters, $entity, $has_language, Url $url) { $this->assertEquals($expected_route_name, $url->getRouteName()); $this->assertEquals($expected_route_parameters, $url->getRouteParameters()); $this->assertEquals($this->entityTypeId, $url->getOption('entity_type')); $this->assertEquals($entity, $url->getOption('entity')); if ($has_language) { $this->assertEquals($this->langcode, $url->getOption('language')->getId()); } else { $this->assertNull($url->getOption('language')); } }
/** * Tests the getRouteName() method. * * @depends testCreateFromPath * * @expectedException \UnexpectedValueException * * @covers ::getRouteName() */ public function testGetRouteName(Url $url) { $url->getRouteName(); }
/** * Prior to set the response it check if we can redirect. * * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event * The event object. * @param \Drupal\Core\Url $url * The Url where we want to redirect. */ protected function setResponse(GetResponseEvent $event, Url $url) { $request = $event->getRequest(); $this->context->fromRequest($request); parse_str($request->getQueryString(), $query); $url->setOption('query', $query); $url->setAbsolute(TRUE); // We can only check access for routed URLs. if (!$url->isRouted() || $this->checker->canRedirect($request, $url->getRouteName())) { // Add the 'rendered' cache tag, so that we can invalidate all responses // when settings are changed. $response = new TrustedRedirectResponse($url->toString(), 301); $response->addCacheableDependency(CacheableMetadata::createFromRenderArray([])->addCacheTags(['rendered'])); $event->setResponse($response); } }