diff --git a/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php index 8f24139..a08de40 100644 --- a/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php +++ b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php @@ -219,14 +219,13 @@ protected function assertNoOption($id, $option) { /** * Passes if the internal browser's URL matches the given path. * - * @param \Drupal\Core\Url|string $path - * The expected system path or URL. + * @param string $path + * The expected system path. * * @deprecated Scheduled for removal in Drupal 9.0.0. * Use $this->assertSession()->addressEquals() instead. */ protected function assertUrl($path) { - $path = "/$path"; $this->assertSession()->addressEquals($path); } diff --git a/core/tests/Drupal/Tests/BrowserTestBase.php b/core/tests/Drupal/Tests/BrowserTestBase.php index 867db61..ddfaf1b 100644 --- a/core/tests/Drupal/Tests/BrowserTestBase.php +++ b/core/tests/Drupal/Tests/BrowserTestBase.php @@ -554,7 +554,7 @@ public function getSession($name = NULL) { * A new web-assert option for asserting the presence of elements with. */ public function assertSession($name = NULL) { - return new WebAssert($this->getSession($name)); + return new WebAssert($this->getSession($name), $this->baseUrl); } /** diff --git a/core/tests/Drupal/Tests/WebAssert.php b/core/tests/Drupal/Tests/WebAssert.php index da3a86d..ec31fdf 100644 --- a/core/tests/Drupal/Tests/WebAssert.php +++ b/core/tests/Drupal/Tests/WebAssert.php @@ -6,6 +6,7 @@ use Behat\Mink\WebAssert as MinkWebAssert; use Behat\Mink\Element\TraversableElement; use Behat\Mink\Exception\ElementNotFoundException; +use Behat\Mink\Session; use Drupal\Component\Utility\Html; /** @@ -14,6 +15,42 @@ class WebAssert extends MinkWebAssert { /** + * The absolute URL of the site under test. + * + * @var string + */ + protected $baseUrl = ''; + + /** + * Constructor. + * + * @param \Behat\Mink\Session $session + * The Behat session object; + * @param string $base_url + * The base URL of the site under test. + */ + public function __construct(Session $session, $base_url = '') { + parent::__construct($session); + $this->baseUrl = $base_url; + } + + /** + * {@inheritdoc} + */ + protected function cleanUrl($url) { + // Strip the base URL from the beginning for absolute URLs. + if ($this->baseUrl !== '' && strpos($url, $this->baseUrl) === 0) { + $url = substr($url, strlen($this->baseUrl)); + } + // Make sure there is a forward slash at the beginning of relative URLs for + // consistency. + if (parse_url($url, PHP_URL_HOST) === NULL && strpos($url, '/') !== 0) { + $url = "/$url"; + } + return parent::cleanUrl($url); + } + + /** * Checks that specific button exists on the current page. * * @param string $button