function UrlTest::testExternalUrls
Same name in other branches
- 9 core/modules/system/tests/src/Kernel/Common/UrlTest.php \Drupal\Tests\system\Kernel\Common\UrlTest::testExternalUrls()
- 8.9.x core/modules/system/tests/src/Functional/Common/UrlTest.php \Drupal\Tests\system\Functional\Common\UrlTest::testExternalUrls()
- 10 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 262
Class
- UrlTest
- Tests the Url object.
Namespace
Drupal\Tests\system\Kernel\CommonCode
public function testExternalUrls() : void {
$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);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.