You are here

protected function WebTestBase::getAbsoluteUrl in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/simpletest/src/WebTestBase.php \Drupal\simpletest\WebTestBase::getAbsoluteUrl()

Takes a path and returns an absolute path.

This method is implemented in the way that browsers work, see https://url.spec.whatwg.org/#relative-state for more information about the possible cases.

Parameters

string $path: A path from the internal browser content.

Return value

string The $path with $base_url prepended, if necessary.

11 calls to WebTestBase::getAbsoluteUrl()
BrowserTest::testGetAbsoluteUrl in core/modules/simpletest/src/Tests/BrowserTest.php
Test \Drupal\simpletest\WebTestBase::getAbsoluteUrl().
CommentPagerTest::clickLinkWithXPath in core/modules/comment/src/Tests/CommentPagerTest.php
Follows a link found at a give xpath query.
ConfigInstallWebTest::testPreExistingConfigInstall in core/modules/config/src/Tests/ConfigInstallWebTest.php
Tests pre-existing configuration detection.
EntityReferenceAdminTest::testFieldAdminHandler in core/modules/field/src/Tests/EntityReference/EntityReferenceAdminTest.php
Tests the Entity Reference Admin UI.
PagerTest::testPagerQueryParametersAndCacheContext in core/modules/system/src/Tests/Pager/PagerTest.php
Test proper functioning of the query parameters and the pager cache context.

... See full list

File

core/modules/simpletest/src/WebTestBase.php, line 2503
Contains \Drupal\simpletest\WebTestBase.

Class

WebTestBase
Test case for typical Drupal tests.

Namespace

Drupal\simpletest

Code

protected function getAbsoluteUrl($path) {
  global $base_url, $base_path;
  $parts = parse_url($path);

  // In case the $path has a host, it is already an absolute URL and we are
  // done.
  if (!empty($parts['host'])) {
    return $path;
  }

  // In case the $path contains just a query, we turn it into an absolute URL
  // with the same scheme, host and path, see
  // https://url.spec.whatwg.org/#relative-state.
  if (array_keys($parts) === [
    'query',
  ]) {
    $current_uri = new Uri($this
      ->getUrl());
    return (string) $current_uri
      ->withQuery($parts['query']);
  }
  if (empty($parts['host'])) {

    // Ensure that we have a string (and no xpath object).
    $path = (string) $path;

    // Strip $base_path, if existent.
    $length = strlen($base_path);
    if (substr($path, 0, $length) === $base_path) {
      $path = substr($path, $length);
    }

    // Ensure that we have an absolute path.
    if (empty($path) || $path[0] !== '/') {
      $path = '/' . $path;
    }

    // Finally, prepend the $base_url.
    $path = $base_url . $path;
  }
  return $path;
}