You are here

function UrlTest::testExternalUrls in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Common/UrlTest.php \Drupal\system\Tests\Common\UrlTest::testExternalUrls()

Tests external URL handling.

File

core/modules/system/src/Tests/Common/UrlTest.php, line 293
Contains \Drupal\system\Tests\Common\UrlTest.

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\system\Tests\Common

Code

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
    ->assertEqual($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, array(
    'fragment' => $fragment,
  ))
    ->toString();
  $this
    ->assertEqual($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
    ->assertEqual($url, $result);

  // Verify external URL can be extended with a query string.
  $url = $test_url;
  $query = array(
    $this
      ->randomMachineName(5) => $this
      ->randomMachineName(5),
  );
  $result = Url::fromUri($url, array(
    'query' => $query,
  ))
    ->toString();
  $this
    ->assertEqual($url . '?' . http_build_query($query, '', '&'), $result, 'External URL can be extended with a query string in $options.');

  // Verify query string can be extended in an external URL.
  $url = $test_url . '?drupal=awesome';
  $query = array(
    $this
      ->randomMachineName(5) => $this
      ->randomMachineName(5),
  );
  $result = Url::fromUri($url, array(
    'query' => $query,
  ))
    ->toString();
  $this
    ->assertEqual($url . '&' . http_build_query($query, '', '&'), $result);
}