Verified Commit 48b85189 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #2031223 by Spokje, acbramley, Berdir, ravi.shankar, a_c_m: Add...

Issue #2031223 by Spokje, acbramley, Berdir, ravi.shankar, a_c_m: Add linkByHrefExistsExact and linkByHrefNotExistsExact for matching links by href exactly
parent 38618b1e
Loading
Loading
Loading
Loading
Loading
+44 −0
Original line number Diff line number Diff line
@@ -387,6 +387,29 @@ public function linkByHrefExists($href, $index = 0, $message = '') {
    $this->assert(!empty($links[$index]), $message);
  }

  /**
   * Passes if a link with a given href is found.
   *
   * @param string $href
   *   The full value of the 'href' attribute of the anchor tag.
   * @param int $index
   *   Link position counting from zero.
   * @param string $message
   *   (optional) A message to display with the assertion. Do not translate
   *   messages: use \Drupal\Component\Render\FormattableMarkup to embed
   *   variables in the message text, not t(). If left blank, a default message
   *   will be displayed.
   *
   * @throws \Behat\Mink\Exception\ExpectationException
   *   Thrown when element doesn't exist, or the link label is a different one.
   */
  public function linkByHrefExistsExact(string $href, int $index = 0, string $message = ''): void {
    $xpath = $this->buildXPathQuery('//a[@href=:href]', [':href' => $href]);
    $message = ($message ?: strtr('No link with href %href found.', ['%href' => $href]));
    $links = $this->session->getPage()->findAll('xpath', $xpath);
    $this->assert(!empty($links[$index]), $message);
  }

  /**
   * Passes if a link containing a given href (part) is not found.
   *
@@ -408,6 +431,27 @@ public function linkByHrefNotExists($href, $message = '') {
    $this->assert(empty($links), $message);
  }

  /**
   * Passes if a link with a given href is not found.
   *
   * @param string $href
   *   The full value of the 'href' attribute of the anchor tag.
   * @param string $message
   *   (optional) A message to display with the assertion. Do not translate
   *   messages: use \Drupal\Component\Render\FormattableMarkup to embed
   *   variables in the message text, not t(). If left blank, a default message
   *   will be displayed.
   *
   * @throws \Behat\Mink\Exception\ExpectationException
   *   Thrown when element doesn't exist, or the link label is a different one.
   */
  public function linkByHrefNotExistsExact(string $href, string $message = ''): void {
    $xpath = $this->buildXPathQuery('//a[@href=:href]', [':href' => $href]);
    $message = ($message ?: strtr('Link with href %href found.', ['%href' => $href]));
    $links = $this->session->getPage()->findAll('xpath', $xpath);
    $this->assert(empty($links), $message);
  }

  /**
   * Builds an XPath query.
   *