public function UrlTest::testExternalUrls in Drupal 10
Same name and namespace in other branches
- 9 core/modules/system/tests/src/Kernel/Common/UrlTest.php \Drupal\Tests\system\Kernel\Common\UrlTest::testExternalUrls()
Tests external URL handling.
File
- core/
modules/ system/ tests/ src/ Kernel/ Common/ UrlTest.php, line 259
Class
- UrlTest
- Confirm that \Drupal\Core\Url, \Drupal\Component\Utility\UrlHelper::filterQueryParameters(), \Drupal\Component\Utility\UrlHelper::buildQuery(), and \Drupal\Core\Utility\LinkGeneratorInterface::generate() work correctly with various input.
Namespace
Drupal\Tests\system\Kernel\CommonCode
public function testExternalUrls() {
$test_url = 'https://www.drupal.org/';
// Verify external URL can contain a fragment.
$url = $test_url . '#drupal';
$result = Url::fromUri($url)
->toString();
$this
->assertEquals($url, $result, 'External URL with fragment works without a fragment in $options.');
// Verify fragment can be overridden in an external URL.
$url = $test_url . '#drupal';
$fragment = $this
->randomMachineName(10);
$result = Url::fromUri($url, [
'fragment' => $fragment,
])
->toString();
$this
->assertEquals($test_url . '#' . $fragment, $result, 'External URL fragment is overridden with a custom fragment in $options.');
// Verify external URL can contain a query string.
$url = $test_url . '?drupal=awesome';
$result = Url::fromUri($url)
->toString();
$this
->assertEquals($url, $result);
// Verify external URL can contain a query string with an integer key.
$url = $test_url . '?120=1';
$result = Url::fromUri($url)
->toString();
$this
->assertEquals($url, $result);
// Verify external URL can be extended with a query string.
$url = $test_url;
$query = [
'awesome' => 'drupal',
];
$result = Url::fromUri($url, [
'query' => $query,
])
->toString();
$this
->assertSame('https://www.drupal.org/?awesome=drupal', $result);
// Verify query string can be extended in an external URL.
$url = $test_url . '?drupal=awesome';
$query = [
'awesome' => 'drupal',
];
$result = Url::fromUri($url, [
'query' => $query,
])
->toString();
$this
->assertEquals('https://www.drupal.org/?drupal=awesome&awesome=drupal', $result);
}