Verified Commit 4616376f authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #2031223 by acbramley, Spokje, Berdir, ravi.shankar, smustgrave,...

Issue #2031223 by acbramley, Spokje, Berdir, ravi.shankar, smustgrave, larowlan, mondrake, a_c_m: Add linkByHrefExistsExact and linkByHrefNotExistsExact for matching links by href exactly

(cherry picked from commit 78a3d4a1)
parent 532841c6
Loading
Loading
Loading
Loading
Loading
+2 −8
Original line number Diff line number Diff line
@@ -75,14 +75,8 @@ public function testVersionHistoryTranslations(): void {
    $this->assertSession()->linkByHrefNotExists($firstRevision->getTranslation('es')->toUrl('revision-delete-form')->toString());

    $this->drupalGet($entity->getTranslation('es')->toUrl('version-history'));
    // We can't use linkByHrefNotExists here because it does a "contains" match
    // and the translated URLs contain the non translated ones.
    // i.e /es/entity_test_mul_revlog/1/revision/1/revert contains
    // /entity_test_mul_revlog/1/revision/1/revert.
    $xpath = $this->assertSession()->buildXPathQuery('//a[@href=:href]', [':href' => $firstRevision->toUrl('revision-revert-form')->toString()]);
    $this->assertEmpty($this->getSession()->getPage()->findAll('xpath', $xpath));
    $xpath = $this->assertSession()->buildXPathQuery('//a[@href=:href]', [':href' => $firstRevision->toUrl('revision-delete-form')->toString()]);
    $this->assertEmpty($this->getSession()->getPage()->findAll('xpath', $xpath));
    $this->assertSession()->linkByHrefNotExistsExact($firstRevision->toUrl('revision-revert-form')->toString());
    $this->assertSession()->linkByHrefNotExistsExact($firstRevision->toUrl('revision-delete-form')->toString());
    $this->assertSession()->linkByHrefExists($firstRevision->getTranslation('es')->toUrl('revision-revert-form')->toString());
    $this->assertSession()->linkByHrefExists($firstRevision->getTranslation('es')->toUrl('revision-delete-form')->toString());
  }
+98 −0
Original line number Diff line number Diff line
@@ -188,6 +188,104 @@ public function testInvalidLinkNotExistsExact() {
    $this->assertSession()->linkNotExistsExact('foo|bar|baz');
  }

  /**
   * Tests linkExistsByHref() functionality.
   *
   * @covers ::linkByHrefExists
   */
  public function testLinkByHrefExists(): void {
    $this->drupalGet('test-page');
    // Partial matching.
    $this->assertSession()->linkByHrefExists('/user');
    // Full matching.
    $this->assertSession()->linkByHrefExists('/user/login');
  }

  /**
   * Tests linkExistsByHref() functionality fail.
   *
   * @covers ::linkByHrefExists
   */
  public function testInvalidLinkByHrefExists(): void {
    $this->drupalGet('test-page');
    $this->expectException(ExpectationException::class);
    $this->assertSession()->linkByHrefExists('/foo');
  }

  /**
   * Tests linkByHrefNotExists() functionality.
   *
   * @covers ::linkByHrefNotExists
   */
  public function testLinkByHrefNotExists(): void {
    $this->drupalGet('test-page');
    $this->assertSession()->linkByHrefNotExists('/foo');
  }

  /**
   * Tests LinkByHrefNotExists() functionality fail partial match.
   *
   * @covers ::linkByHrefNotExists
   */
  public function testInvalidLinkByHrefNotExistsPartial(): void {
    $this->drupalGet('test-page');
    $this->expectException(ExpectationException::class);
    $this->assertSession()->linkByHrefNotExists('/user');
  }

  /**
   * Tests LinkByHrefNotExists() functionality fail full match.
   *
   * @covers ::linkByHrefNotExists
   */
  public function testInvalidLinkByHrefNotExistsFull(): void {
    $this->drupalGet('test-page');
    $this->expectException(ExpectationException::class);
    $this->assertSession()->linkByHrefNotExists('/user/login');
  }

  /**
   * Tests linkExistsByHref() functionality.
   *
   * @covers ::linkByHrefExistsExact
   */
  public function testLinkByHrefExistsExact(): void {
    $this->drupalGet('test-page');
    $this->assertSession()->linkByHrefExistsExact('/user/login');
  }

  /**
   * Tests linkByHrefExistsExact() functionality fail.
   *
   * @covers ::linkByHrefExistsExact
   */
  public function testInvalidLinkByHrefExistsExact(): void {
    $this->drupalGet('test-page');
    $this->expectException(ExpectationException::class);
    $this->assertSession()->linkByHrefExistsExact('/foo');
  }

  /**
   * Tests linkByHrefNotExistsExact() functionality.
   *
   * @covers ::linkByHrefNotExistsExact
   */
  public function testLinkByHrefNotExistsExact(): void {
    $this->drupalGet('test-page');
    $this->assertSession()->linkByHrefNotExistsExact('/foo');
  }

  /**
   * Tests linkByHrefNotExistsExact() functionality fail.
   *
   * @covers ::linkByHrefNotExistsExact
   */
  public function testInvalidLinkByHrefNotExistsExact(): void {
    $this->drupalGet('test-page');
    $this->expectException(ExpectationException::class);
    $this->assertSession()->linkByHrefNotExistsExact('/user/login');
  }

  /**
   * Tests legacy text asserts.
   *
+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.
   *