Chris@0: randomMachineName();
Chris@0: $path = "";
Chris@0: $encoded_path = "3CSCRIPT%3Ealert%28%27XSS%27%29%3C/SCRIPT%3E";
Chris@0:
Chris@0: $link = \Drupal::l($text, Url::fromUserInput('/' . $path));
Chris@0: $this->assertTrue(strpos($link, $encoded_path) !== FALSE && strpos($link, $path) === FALSE, format_string('XSS attack @path was filtered by \Drupal\Core\Utility\LinkGeneratorInterface::generate().', ['@path' => $path]));
Chris@0:
Chris@0: // Test \Drupal\Core\Url.
Chris@0: $link = Url::fromUri('base:' . $path)->toString();
Chris@0: $this->assertTrue(strpos($link, $encoded_path) !== FALSE && strpos($link, $path) === FALSE, format_string('XSS attack @path was filtered by #theme', ['@path' => $path]));
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests that #type=link bubbles outbound route/path processors' metadata.
Chris@0: */
Chris@0: public function testLinkBubbleableMetadata() {
Chris@0: $cases = [
Chris@0: ['Regular link', 'internal:/user', [], ['contexts' => [], 'tags' => [], 'max-age' => Cache::PERMANENT], []],
Chris@0: ['Regular link, absolute', 'internal:/user', ['absolute' => TRUE], ['contexts' => ['url.site'], 'tags' => [], 'max-age' => Cache::PERMANENT], []],
Chris@0: ['Route processor link', 'route:system.run_cron', [], ['contexts' => ['session'], 'tags' => [], 'max-age' => Cache::PERMANENT], ['placeholders' => []]],
Chris@0: ['Route processor link, absolute', 'route:system.run_cron', ['absolute' => TRUE], ['contexts' => ['url.site', 'session'], 'tags' => [], 'max-age' => Cache::PERMANENT], ['placeholders' => []]],
Chris@0: ['Path processor link', 'internal:/user/1', [], ['contexts' => [], 'tags' => ['user:1'], 'max-age' => Cache::PERMANENT], []],
Chris@0: ['Path processor link, absolute', 'internal:/user/1', ['absolute' => TRUE], ['contexts' => ['url.site'], 'tags' => ['user:1'], 'max-age' => Cache::PERMANENT], []],
Chris@0: ];
Chris@0:
Chris@0: foreach ($cases as $case) {
Chris@0: list($title, $uri, $options, $expected_cacheability, $expected_attachments) = $case;
Chris@0: $expected_cacheability['contexts'] = Cache::mergeContexts($expected_cacheability['contexts'], ['languages:language_interface', 'theme', 'user.permissions']);
Chris@0: $link = [
Chris@0: '#type' => 'link',
Chris@0: '#title' => $title,
Chris@0: '#options' => $options,
Chris@0: '#url' => Url::fromUri($uri),
Chris@0: ];
Chris@0: \Drupal::service('renderer')->renderRoot($link);
Chris@0: $this->pass($title);
Chris@0: $this->assertEqual($expected_cacheability, $link['#cache']);
Chris@0: $this->assertEqual($expected_attachments, $link['#attached']);
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests that default and custom attributes are handled correctly on links.
Chris@0: */
Chris@0: public function testLinkAttributes() {
Chris@0: /** @var \Drupal\Core\Render\RendererInterface $renderer */
Chris@0: $renderer = $this->container->get('renderer');
Chris@0:
Chris@0: // Test that hreflang is added when a link has a known language.
Chris@0: $language = new Language(['id' => 'fr', 'name' => 'French']);
Chris@0: $hreflang_link = [
Chris@0: '#type' => 'link',
Chris@0: '#options' => [
Chris@0: 'language' => $language,
Chris@0: ],
Chris@0: '#url' => Url::fromUri('https://www.drupal.org'),
Chris@0: '#title' => 'bar',
Chris@0: ];
Chris@0: $langcode = $language->getId();
Chris@0:
Chris@0: // Test that the default hreflang handling for links does not override a
Chris@0: // hreflang attribute explicitly set in the render array.
Chris@0: $hreflang_override_link = $hreflang_link;
Chris@0: $hreflang_override_link['#options']['attributes']['hreflang'] = 'foo';
Chris@0:
Chris@0: $rendered = $renderer->renderRoot($hreflang_link);
Chris@0: $this->assertTrue($this->hasAttribute('hreflang', $rendered, $langcode), format_string('hreflang attribute with value @langcode is present on a rendered link when langcode is provided in the render array.', ['@langcode' => $langcode]));
Chris@0:
Chris@0: $rendered = $renderer->renderRoot($hreflang_override_link);
Chris@0: $this->assertTrue($this->hasAttribute('hreflang', $rendered, 'foo'), format_string('hreflang attribute with value @hreflang is present on a rendered link when @hreflang is provided in the render array.', ['@hreflang' => 'foo']));
Chris@0:
Chris@0: // Test the active class in links produced by
Chris@0: // \Drupal\Core\Utility\LinkGeneratorInterface::generate() and #type 'link'.
Chris@0: $options_no_query = [];
Chris@0: $options_query = [
Chris@0: 'query' => [
Chris@0: 'foo' => 'bar',
Chris@0: 'one' => 'two',
Chris@0: ],
Chris@0: ];
Chris@0: $options_query_reverse = [
Chris@0: 'query' => [
Chris@0: 'one' => 'two',
Chris@0: 'foo' => 'bar',
Chris@0: ],
Chris@0: ];
Chris@0:
Chris@0: // Test #type link.
Chris@0: $path = 'common-test/type-link-active-class';
Chris@0:
Chris@0: $this->drupalGet($path, $options_no_query);
Chris@0: $links = $this->xpath('//a[@href = :href and contains(@class, :class)]', [':href' => Url::fromRoute('common_test.l_active_class', [], $options_no_query)->toString(), ':class' => 'is-active']);
Chris@0: $this->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page is marked active.');
Chris@0:
Chris@0: $links = $this->xpath('//a[@href = :href and not(contains(@class, :class))]', [':href' => Url::fromRoute('common_test.l_active_class', [], $options_query)->toString(), ':class' => 'is-active']);
Chris@0: $this->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page with a query string when the current page has no query string is not marked active.');
Chris@0:
Chris@0: $this->drupalGet($path, $options_query);
Chris@0: $links = $this->xpath('//a[@href = :href and contains(@class, :class)]', [':href' => Url::fromRoute('common_test.l_active_class', [], $options_query)->toString(), ':class' => 'is-active']);
Chris@0: $this->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page with a query string that matches the current query string is marked active.');
Chris@0:
Chris@0: $links = $this->xpath('//a[@href = :href and contains(@class, :class)]', [':href' => Url::fromRoute('common_test.l_active_class', [], $options_query_reverse)->toString(), ':class' => 'is-active']);
Chris@0: $this->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page with a query string that has matching parameters to the current query string but in a different order is marked active.');
Chris@0:
Chris@0: $links = $this->xpath('//a[@href = :href and not(contains(@class, :class))]', [':href' => Url::fromRoute('common_test.l_active_class', [], $options_no_query)->toString(), ':class' => 'is-active']);
Chris@0: $this->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page without a query string when the current page has a query string is not marked active.');
Chris@0:
Chris@0: // Test adding a custom class in links produced by
Chris@0: // \Drupal\Core\Utility\LinkGeneratorInterface::generate() and #type 'link'.
Chris@0: // Test the link generator.
Chris@0: $class_l = $this->randomMachineName();
Chris@0: $link_l = \Drupal::l($this->randomMachineName(), new Url('', [], ['attributes' => ['class' => [$class_l]]]));
Chris@0: $this->assertTrue($this->hasAttribute('class', $link_l, $class_l), format_string('Custom class @class is present on link when requested by l()', ['@class' => $class_l]));
Chris@0:
Chris@0: // Test #type.
Chris@0: $class_theme = $this->randomMachineName();
Chris@0: $type_link = [
Chris@0: '#type' => 'link',
Chris@0: '#title' => $this->randomMachineName(),
Chris@0: '#url' => Url::fromRoute(''),
Chris@0: '#options' => [
Chris@0: 'attributes' => [
Chris@0: 'class' => [$class_theme],
Chris@0: ],
Chris@0: ],
Chris@0: ];
Chris@0: $link_theme = $renderer->renderRoot($type_link);
Chris@0: $this->assertTrue($this->hasAttribute('class', $link_theme, $class_theme), format_string('Custom class @class is present on link when requested by #type', ['@class' => $class_theme]));
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests that link functions support render arrays as 'text'.
Chris@0: */
Chris@0: public function testLinkRenderArrayText() {
Chris@0: /** @var \Drupal\Core\Render\RendererInterface $renderer */
Chris@0: $renderer = $this->container->get('renderer');
Chris@0:
Chris@0: // Build a link with the link generator for reference.
Chris@0: $l = \Drupal::l('foo', Url::fromUri('https://www.drupal.org'));
Chris@0:
Chris@0: // Test a renderable array passed to the link generator.
Chris@0: $renderer->executeInRenderContext(new RenderContext(), function () use ($renderer, $l) {
Chris@0: $renderable_text = ['#markup' => 'foo'];
Chris@0: $l_renderable_text = \Drupal::l($renderable_text, Url::fromUri('https://www.drupal.org'));
Chris@0: $this->assertEqual($l_renderable_text, $l);
Chris@0: });
Chris@0:
Chris@0: // Test a themed link with plain text 'text'.
Chris@0: $type_link_plain_array = [
Chris@0: '#type' => 'link',
Chris@0: '#title' => 'foo',
Chris@0: '#url' => Url::fromUri('https://www.drupal.org'),
Chris@0: ];
Chris@0: $type_link_plain = $renderer->renderRoot($type_link_plain_array);
Chris@0: $this->assertEqual($type_link_plain, $l);
Chris@0:
Chris@0: // Build a themed link with renderable 'text'.
Chris@0: $type_link_nested_array = [
Chris@0: '#type' => 'link',
Chris@0: '#title' => ['#markup' => 'foo'],
Chris@0: '#url' => Url::fromUri('https://www.drupal.org'),
Chris@0: ];
Chris@0: $type_link_nested = $renderer->renderRoot($type_link_nested_array);
Chris@0: $this->assertEqual($type_link_nested, $l);
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Checks for class existence in link.
Chris@0: *
Chris@0: * @param $link
Chris@0: * URL to search.
Chris@0: * @param $class
Chris@0: * Element class to search for.
Chris@0: *
Chris@0: * @return bool
Chris@0: * TRUE if the class is found, FALSE otherwise.
Chris@0: */
Chris@0: private function hasAttribute($attribute, $link, $class) {
Chris@0: return preg_match('|' . $attribute . '="([^\"\s]+\s+)*' . $class . '|', $link);
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests UrlHelper::filterQueryParameters().
Chris@0: */
Chris@0: public function testDrupalGetQueryParameters() {
Chris@0: $original = [
Chris@0: 'a' => 1,
Chris@0: 'b' => [
Chris@0: 'd' => 4,
Chris@0: 'e' => [
Chris@0: 'f' => 5,
Chris@0: ],
Chris@0: ],
Chris@0: 'c' => 3,
Chris@0: ];
Chris@0:
Chris@0: // First-level exclusion.
Chris@0: $result = $original;
Chris@0: unset($result['b']);
Chris@0: $this->assertEqual(UrlHelper::filterQueryParameters($original, ['b']), $result, "'b' was removed.");
Chris@0:
Chris@0: // Second-level exclusion.
Chris@0: $result = $original;
Chris@0: unset($result['b']['d']);
Chris@0: $this->assertEqual(UrlHelper::filterQueryParameters($original, ['b[d]']), $result, "'b[d]' was removed.");
Chris@0:
Chris@0: // Third-level exclusion.
Chris@0: $result = $original;
Chris@0: unset($result['b']['e']['f']);
Chris@0: $this->assertEqual(UrlHelper::filterQueryParameters($original, ['b[e][f]']), $result, "'b[e][f]' was removed.");
Chris@0:
Chris@0: // Multiple exclusions.
Chris@0: $result = $original;
Chris@0: unset($result['a'], $result['b']['e'], $result['c']);
Chris@0: $this->assertEqual(UrlHelper::filterQueryParameters($original, ['a', 'b[e]', 'c']), $result, "'a', 'b[e]', 'c' were removed.");
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests UrlHelper::parse().
Chris@0: */
Chris@0: public function testDrupalParseUrl() {
Chris@0: // Relative, absolute, and external URLs, without/with explicit script path,
Chris@0: // without/with Drupal path.
Chris@0: foreach (['', '/', 'https://www.drupal.org/'] as $absolute) {
Chris@0: foreach (['', 'index.php/'] as $script) {
Chris@0: foreach (['', 'foo/bar'] as $path) {
Chris@0: $url = $absolute . $script . $path . '?foo=bar&bar=baz&baz#foo';
Chris@0: $expected = [
Chris@0: 'path' => $absolute . $script . $path,
Chris@0: 'query' => ['foo' => 'bar', 'bar' => 'baz', 'baz' => ''],
Chris@0: 'fragment' => 'foo',
Chris@0: ];
Chris@0: $this->assertEqual(UrlHelper::parse($url), $expected, 'URL parsed correctly.');
Chris@0: }
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: // Relative URL that is known to confuse parse_url().
Chris@0: $url = 'foo/bar:1';
Chris@0: $result = [
Chris@0: 'path' => 'foo/bar:1',
Chris@0: 'query' => [],
Chris@0: 'fragment' => '',
Chris@0: ];
Chris@0: $this->assertEqual(UrlHelper::parse($url), $result, 'Relative URL parsed correctly.');
Chris@0:
Chris@0: // Test that drupal can recognize an absolute URL. Used to prevent attack vectors.
Chris@0: $url = 'https://www.drupal.org/foo/bar?foo=bar&bar=baz&baz#foo';
Chris@0: $this->assertTrue(UrlHelper::isExternal($url), 'Correctly identified an external URL.');
Chris@0:
Chris@0: // Test that UrlHelper::parse() does not allow spoofing a URL to force a malicious redirect.
Chris@0: $parts = UrlHelper::parse('forged:http://cwe.mitre.org/data/definitions/601.html');
Chris@0: $this->assertFalse(UrlHelper::isValid($parts['path'], TRUE), '\Drupal\Component\Utility\UrlHelper::isValid() correctly parsed a forged URL.');
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests external URL handling.
Chris@0: */
Chris@0: public function testExternalUrls() {
Chris@0: $test_url = 'https://www.drupal.org/';
Chris@0:
Chris@0: // Verify external URL can contain a fragment.
Chris@0: $url = $test_url . '#drupal';
Chris@0: $result = Url::fromUri($url)->toString();
Chris@0: $this->assertEqual($url, $result, 'External URL with fragment works without a fragment in $options.');
Chris@0:
Chris@0: // Verify fragment can be overridden in an external URL.
Chris@0: $url = $test_url . '#drupal';
Chris@0: $fragment = $this->randomMachineName(10);
Chris@0: $result = Url::fromUri($url, ['fragment' => $fragment])->toString();
Chris@0: $this->assertEqual($test_url . '#' . $fragment, $result, 'External URL fragment is overridden with a custom fragment in $options.');
Chris@0:
Chris@0: // Verify external URL can contain a query string.
Chris@0: $url = $test_url . '?drupal=awesome';
Chris@0: $result = Url::fromUri($url)->toString();
Chris@0: $this->assertEqual($url, $result);
Chris@0:
Chris@0: // Verify external URL can be extended with a query string.
Chris@0: $url = $test_url;
Chris@0: $query = [$this->randomMachineName(5) => $this->randomMachineName(5)];
Chris@0: $result = Url::fromUri($url, ['query' => $query])->toString();
Chris@0: $this->assertEqual($url . '?' . http_build_query($query, '', '&'), $result, 'External URL can be extended with a query string in $options.');
Chris@0:
Chris@0: // Verify query string can be extended in an external URL.
Chris@0: $url = $test_url . '?drupal=awesome';
Chris@0: $query = [$this->randomMachineName(5) => $this->randomMachineName(5)];
Chris@0: $result = Url::fromUri($url, ['query' => $query])->toString();
Chris@0: $this->assertEqual($url . '&' . http_build_query($query, '', '&'), $result);
Chris@0: }
Chris@0:
Chris@0: }