function WebAssert::cleanUrl

Same name in other branches
  1. 9 core/tests/Drupal/Tests/WebAssert.php \Drupal\Tests\WebAssert::cleanUrl()
  2. 8.9.x core/tests/Drupal/Tests/WebAssert.php \Drupal\Tests\WebAssert::cleanUrl()
  3. 10 core/tests/Drupal/Tests/WebAssert.php \Drupal\Tests\WebAssert::cleanUrl()

Trims the base URL from the URL.

Parameters

string|\Drupal\Core\Url $url: A url string, or object.

bool $include_query: Whether to include the query string in the return value.

Return value

string

2 calls to WebAssert::cleanUrl()
WebAssert::addressEquals in core/tests/Drupal/Tests/WebAssert.php
Checks that current session address is equals to provided one.
WebAssert::addressNotEquals in core/tests/Drupal/Tests/WebAssert.php
Checks that current session address is not equals to provided one.

File

core/tests/Drupal/Tests/WebAssert.php, line 57

Class

WebAssert
Defines a class with methods for asserting presence of elements during tests.

Namespace

Drupal\Tests

Code

protected function cleanUrl(string|Url $url, bool $include_query = FALSE) {
    if ($url instanceof Url) {
        $url = $url->setAbsolute()
            ->toString();
    }
    // Strip the base URL from the beginning for absolute URLs.
    if ($this->baseUrl !== '' && str_starts_with($url, $this->baseUrl)) {
        $url = substr($url, strlen($this->baseUrl));
    }
    $parts = parse_url($url);
    // Make sure there is a forward slash at the beginning of relative URLs for
    // consistency.
    if (empty($parts['host']) && !str_starts_with($url, '/')) {
        $parts['path'] = '/' . $parts['path'];
    }
    $fragment = empty($parts['fragment']) ? '' : '#' . $parts['fragment'];
    $path = empty($parts['path']) ? '/' : $parts['path'];
    $query = $include_query && !empty($parts['query']) ? '?' . $parts['query'] : '';
    return preg_replace('/^\\/[^\\.\\/]+\\.php\\//', '/', $path) . $query . $fragment;
}

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