/** * {@inheritdoc} */ public function execute($object = NULL) { $url = $this->configuration['url']; // Leave external URLs unchanged, and assemble others as absolute URLs // relative to the site's base URL. if (!UrlHelper::isExternal($url)) { $parts = UrlHelper::parse($url); // @todo '<front>' is valid input for BC reasons, may be removed by // https://www.drupal.org/node/2421941 if ($parts['path'] === '<front>') { $parts['path'] = ''; } $uri = 'base:' . $parts['path']; $options = ['query' => $parts['query'], 'fragment' => $parts['fragment'], 'absolute' => TRUE]; // Treat this as if it's user input of a path relative to the site's // base URL. $url = $this->unroutedUrlAssembler->assemble($uri, $options); } $response = new RedirectResponse($url); $listener = function ($event) use($response) { $event->setResponse($response); }; // Add the listener to the event dispatcher. $this->dispatcher->addListener(KernelEvents::RESPONSE, $listener); }
/** * {@inheritdoc} */ public function setEmbedProvider($provider) { $provider_parsed = UrlHelper::parse($provider); $provider_parsed['query'] = array_filter($provider_parsed['query'], function ($value) { return $value !== '{callback}'; }); $provider_parsed['absolute'] = TRUE; $this->embed_provider = $this->urlAssembler->assemble($provider_parsed['path'], $provider_parsed); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->requestContext = $this->getMockBuilder('Drupal\\Core\\Routing\\RequestContext')->disableOriginalConstructor()->getMock(); $this->requestContext->expects($this->any())->method('getCompleteBaseUrl')->willReturn('http://example.com/drupal'); $this->urlAssembler = $this->getMock(UnroutedUrlAssemblerInterface::class); $this->urlAssembler->expects($this->any())->method('assemble')->willReturnMap([['base:test', ['query' => [], 'fragment' => '', 'absolute' => TRUE], FALSE, 'http://example.com/drupal/test'], ['base:example.com', ['query' => [], 'fragment' => '', 'absolute' => TRUE], FALSE, 'http://example.com/drupal/example.com'], ['base:example:com', ['query' => [], 'fragment' => '', 'absolute' => TRUE], FALSE, 'http://example.com/drupal/example:com'], ['base:javascript:alert(0)', ['query' => [], 'fragment' => '', 'absolute' => TRUE], FALSE, 'http://example.com/drupal/javascript:alert(0)']]); $container = new Container(); $container->set('router.request_context', $this->requestContext); \Drupal::setContainer($container); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->urlAssembler = $this->getMock('Drupal\\Core\\Utility\\UnroutedUrlAssemblerInterface'); $this->urlAssembler->expects($this->any())->method('assemble')->will($this->returnArgument(0)); $this->router = $this->getMock('Drupal\\Tests\\Core\\Routing\\TestRouterInterface'); $container = new ContainerBuilder(); $container->set('router.no_access_checks', $this->router); $container->set('unrouted_url_assembler', $this->urlAssembler); \Drupal::setContainer($container); }
/** * Tests the generate() method with an external URL. * * The set_active_class option is set to TRUE to ensure this does not cause * an error together with an external URL. * * @covers ::generate */ public function testGenerateExternal() { $this->urlAssembler->expects($this->once())->method('assemble')->with('http://drupal.org', array('set_active_class' => TRUE, 'external' => TRUE) + $this->defaultOptions)->will($this->returnArgument(0)); $this->moduleHandler->expects($this->once())->method('alter')->with('link', $this->isType('array')); $this->urlAssembler->expects($this->once())->method('assemble')->with('http://drupal.org', array('set_active_class' => TRUE, 'external' => TRUE) + $this->defaultOptions)->willReturnArgument(0); $url = Url::fromUri('http://drupal.org'); $url->setUrlGenerator($this->urlGenerator); $url->setUnroutedUrlAssembler($this->urlAssembler); $url->setOption('set_active_class', TRUE); $result = $this->linkGenerator->generate('Drupal', $url); $this->assertLink(array('attributes' => array('href' => 'http://drupal.org'), 'content' => 'Drupal'), $result); }
/** * Tests the generate() method with a url containing double quotes. * * @covers ::generate */ public function testGenerateUrlWithQuotes() { $this->urlAssembler->expects($this->once())->method('assemble')->with('base:example', array('query' => array('foo' => '"bar"', 'zoo' => 'baz')) + $this->defaultOptions)->willReturn((new GeneratedUrl())->setGeneratedUrl('/example?foo=%22bar%22&zoo=baz')); $path_validator = $this->getMock('Drupal\\Core\\Path\\PathValidatorInterface'); $container_builder = new ContainerBuilder(); $container_builder->set('path.validator', $path_validator); \Drupal::setContainer($container_builder); $path = '/example?foo="bar"&zoo=baz'; $url = Url::fromUserInput($path); $url->setUrlGenerator($this->urlGenerator); $url->setUnroutedUrlAssembler($this->urlAssembler); $result = $this->linkGenerator->generate('Drupal', $url); $this->assertLink(array('attributes' => array('href' => '/example?foo=%22bar%22&zoo=baz'), 'content' => 'Drupal'), $result, 1); }
/** * Tests the redirectForm() method when no redirect is expected. * * @covers ::redirectForm */ public function testRedirectWithoutResult() { $form_submitter = $this->getFormSubmitter(); $this->urlGenerator->expects($this->never())->method('generateFromRoute'); $this->unroutedUrlAssembler->expects($this->never())->method('assemble'); $container = new ContainerBuilder(); $container->set('url_generator', $this->urlGenerator); $container->set('unrouted_url_assembler', $this->unroutedUrlAssembler); \Drupal::setContainer($container); $form_state = $this->getMock('Drupal\\Core\\Form\\FormStateInterface'); $form_state->expects($this->once())->method('getRedirect')->willReturn(FALSE); $redirect = $form_submitter->redirectForm($form_state); $this->assertNull($redirect); }
/** * Converts the passed in destination into an absolute URL. * * @param string $destination * The path for the destination. In case it starts with a slash it should * have the base path included already. * @param string $scheme_and_host * The scheme and host string of the current request. * * @return string * The destination as absolute URL. */ protected function getDestinationAsAbsoluteUrl($destination, $scheme_and_host) { if (!UrlHelper::isExternal($destination)) { // The destination query parameter can be a relative URL in the sense of // not including the scheme and host, but its path is expected to be // absolute (start with a '/'). For such a case, prepend the scheme and // host, because the 'Location' header must be absolute. if (strpos($destination, '/') === 0) { $destination = $scheme_and_host . $destination; } else { // Legacy destination query parameters can be internal paths that have // not yet been converted to URLs. $destination = UrlHelper::parse($destination); $uri = 'base:' . $destination['path']; $options = ['query' => $destination['query'], 'fragment' => $destination['fragment'], 'absolute' => TRUE]; // Treat this as if it's user input of a path relative to the site's // base URL. $destination = $this->unroutedUrlAssembler->assemble($uri, $options); } } return $destination; }
/** * Helper function to assert that a library was correctly loaded. * * Asserts that all the correct files were loaded and all the incorrect ones * were not. * * @param $name * The name of the files that should be loaded. The current testing system * knows of 'example_1', 'example_2', 'example_3' and 'example_4'. Each name * has an associated JavaScript, CSS and PHP file that will be asserted. All * other files will be asserted to not be loaded. See * tests/example/README.txt for more information on how the loading of the * files is tested. * @param $label * (optional) A label to prepend to the assertion messages, to make them * less ambiguous. * @param $extensions * (optional) The expected file extensions of $name. Defaults to * array('js', 'css', 'php'). */ function assertLibraryFiles($name, $label = '', $extensions = array('js', 'css', 'php')) { $label = $label !== '' ? "{$label}: " : ''; // Test that the wrong files are not loaded... $names = array('example_1' => FALSE, 'example_2' => FALSE, 'example_3' => FALSE, 'example_4' => FALSE); // ...and the correct ones are. $names[$name] = TRUE; // Test for the specific HTML that the different file types appear as in the // DOM. $html = array('js' => array('<script src="', '"></script>'), 'css' => array('<link rel="stylesheet" href="', '" media="all" />'), 'php' => array('<li>', '</li>')); $html_expected = array(); $html_not_expected = array(); foreach ($names as $name => $expected) { foreach ($extensions as $extension) { $filepath = drupal_get_path('module', 'libraries') . "/tests/example/{$name}.{$extension}"; // JavaScript and CSS files appear as full URLs and with an appended // query string. if (in_array($extension, array('js', 'css'))) { $filepath = $this->urlAssembler->assemble("base://{$filepath}", ['query' => [$this->state->get('system.css_js_query_string') ?: '0' => NULL], 'absolute' => TRUE]); // If index.php is part of the generated URLs, we need to strip it. //$filepath = str_replace('index.php/', '', $filepath); } list($prefix, $suffix) = $html[$extension]; $raw = $prefix . $filepath . $suffix; if ($expected) { $html_expected[] = Html::escape($raw); $this->assertRaw($raw, "{$label}{$name}.{$extension} found."); } else { $html_not_expected[] = Html::escape($raw); $this->assertNoRaw($raw, "{$label}{$name}.{$extension} not found."); } } } $html_expected = '<ul><li><pre>' . implode('</pre></li><li><pre>', $html_expected) . '</pre></li></ul>'; $html_not_expected = '<ul><li><pre>' . implode('</pre></li><li><pre>', $html_not_expected) . '</pre></li></ul>'; $this->verbose("Strings of HTML that are expected to be present:{$html_expected}Strings of HTML that are expected to not be present:{$html_not_expected}"); }
/** * {@inheritdoc} */ public function getRelationUri($entity_type, $bundle, $field_name) { return $this->urlAssembler->assemble("base:rest/relation/{$entity_type}/{$bundle}/{$field_name}", array('absolute' => TRUE)); }
/** * Get a type link for a bundle. * * @param string $entity_type * The bundle's entity type. * @param string $bundle * The name of the bundle. * * @return array * The URI that identifies this bundle. */ public function getTypeUri($entity_type, $bundle) { // @todo Make the base path configurable. return $this->urlAssembler->assemble("base:rest/type/{$entity_type}/{$bundle}", array('absolute' => TRUE)); }