You are here

protected function WebAssert::cleanUrl in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/WebAssert.php \Drupal\Tests\WebAssert::cleanUrl()
2 calls to WebAssert::cleanUrl()
WebAssert::addressEquals in core/tests/Drupal/Tests/WebAssert.php
WebAssert::addressNotEquals in core/tests/Drupal/Tests/WebAssert.php

File

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

Class

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

Namespace

Drupal\Tests

Code

protected function cleanUrl($url, $include_query = FALSE) {
  if ($url instanceof Url) {
    $url = $url
      ->setAbsolute()
      ->toString();
  }

  // Strip the base URL from the beginning for absolute URLs.
  if ($this->baseUrl !== '' && strpos($url, $this->baseUrl) === 0) {
    $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']) && strpos($url, '/') !== 0) {
    $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;
}