function UrlTest::testDrupalParseUrl
Same name and namespace in other branches
- 9 core/modules/system/tests/src/Kernel/Common/UrlTest.php \Drupal\Tests\system\Kernel\Common\UrlTest::testDrupalParseUrl()
- 10 core/modules/system/tests/src/Kernel/Common/UrlTest.php \Drupal\Tests\system\Kernel\Common\UrlTest::testDrupalParseUrl()
- 11.x core/modules/system/tests/src/Kernel/Common/UrlTest.php \Drupal\Tests\system\Kernel\Common\UrlTest::testDrupalParseUrl()
Tests UrlHelper::parse().
File
-
core/
modules/ system/ tests/ src/ Functional/ Common/ UrlTest.php, line 258
Class
- UrlTest
- Confirm that <a href="/api/drupal/core%21lib%21Drupal%21Core%21Url.php/class/Url/8.9.x" title="Defines an object that holds information about a URL." class="local">\Drupal\Core\Url</a>, <a href="/api/drupal/core%21lib%21Drupal%21Component%21Utility%21UrlHelper.php/function/UrlHelper%3A%3AfilterQueryParameters/8.9.x" title="Filters a URL query parameter array to remove unwanted elements." class="local">\Drupal\Component\Utility\UrlHelper::filterQueryParameters</a>(), <a href="/api/drupal/core%21lib%21Drupal%21Component%21Utility%21UrlHelper.php/function/UrlHelper%3A%3AbuildQuery/8.9.x" title="Parses an array into a valid, rawurlencoded query string." class="local">\Drupal\Component\Utility\UrlHelper::buildQuery</a>(), and <a href="/api/drupal/core%21lib%21Drupal%21Core%21Utility%21LinkGeneratorInterface.php/function/LinkGeneratorInterface%3A%3Agenerate/8.9.x" title="Renders a link to a URL." class="local">\Drupal\Core\Utility\LinkGeneratorInterface::generate</a>() work correctly with various input.
Namespace
Drupal\Tests\system\Functional\CommonCode
public function testDrupalParseUrl() {
// Relative, absolute, and external URLs, without/with explicit script path,
// without/with Drupal path.
foreach ([
'',
'/',
'https://www.drupal.org/',
] as $absolute) {
foreach ([
'',
'index.php/',
] as $script) {
foreach ([
'',
'foo/bar',
] as $path) {
$url = $absolute . $script . $path . '?foo=bar&bar=baz&baz#foo';
$expected = [
'path' => $absolute . $script . $path,
'query' => [
'foo' => 'bar',
'bar' => 'baz',
'baz' => '',
],
'fragment' => 'foo',
];
$this->assertEqual(UrlHelper::parse($url), $expected, 'URL parsed correctly.');
}
}
}
// Relative URL that is known to confuse parse_url().
$url = 'foo/bar:1';
$result = [
'path' => 'foo/bar:1',
'query' => [],
'fragment' => '',
];
$this->assertEqual(UrlHelper::parse($url), $result, 'Relative URL parsed correctly.');
// Test that drupal can recognize an absolute URL. Used to prevent attack vectors.
$url = 'https://www.drupal.org/foo/bar?foo=bar&bar=baz&baz#foo';
$this->assertTrue(UrlHelper::isExternal($url), 'Correctly identified an external URL.');
// Test that UrlHelper::parse() does not allow spoofing a URL to force a malicious redirect.
$parts = UrlHelper::parse('forged:http://cwe.mitre.org/data/definitions/601.html');
$this->assertFalse(UrlHelper::isValid($parts['path'], TRUE), '\\Drupal\\Component\\Utility\\UrlHelper::isValid() correctly parsed a forged URL.');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.