function 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.

6 calls to WebTestBase::getAbsoluteUrl()
BrowserTest::testGetAbsoluteUrl in core/modules/simpletest/src/Tests/BrowserTest.php
Test \Drupal\simpletest\WebTestBase::getAbsoluteUrl().
SearchTestBase::submitGetForm in core/modules/search/src/Tests/SearchTestBase.php
Simulates submission of a form using GET instead of POST.
WebTestBase::buildUrl in core/modules/simpletest/src/WebTestBase.php
Builds an a absolute URL from a system path or a URL object.
WebTestBase::checkForMetaRefresh in core/modules/simpletest/src/WebTestBase.php
Checks for meta refresh tag and if found call drupalGet() recursively.
WebTestBase::clickLinkHelper in core/modules/simpletest/src/WebTestBase.php
Provides a helper for ::clickLink() and ::clickLinkPartialName().

... See full list

File

core/modules/simpletest/src/WebTestBase.php, line 1758

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.