From e347a62918287f483db52d1e4a6b2e158594c9de Mon Sep 17 00:00:00 2001
From: GoZ <goz@226961.no-reply.drupal.org>
Date: Sat, 15 Jul 2017 10:49:20 +0200
Subject: [PATCH] Issue #2860175 by GoZ: Add exact option to
 AssertLegacyTrait::assertLink(), WebAssert::linkExists() and
 WebAssert::linkNotExists()

---
 .../Drupal/FunctionalTests/BrowserTestBaseTest.php | 52 ++++++++++++++++++++++
 core/tests/Drupal/Tests/WebAssert.php              | 44 ++++++++++++++++++
 2 files changed, 96 insertions(+)

diff --git a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php
index 3a07501..a829184 100644
--- a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php
+++ b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php
@@ -138,6 +138,58 @@ public function testPipeCharInLocator() {
   }
 
   /**
+   * Tests linkExistsExact() functionality.
+   *
+   * @see \Drupal\Tests\WebAssert::linkExistsExact()
+   */
+  public function testLinkExistsExact() {
+    $this->drupalGet('test-pipe-char');
+    $this->assertSession()->linkExistsExact('foo|bar|baz');
+  }
+
+  /**
+   * Tests linkExistsExact() functionality fail.
+   *
+   * @see \Drupal\Tests\WebAssert::linkExistsExact()
+   */
+  public function testLinkNonExistsExact() {
+    $this->drupalGet('test-pipe-char');
+    try {
+      $this->assertSession()->linkExistsExact('foo|bar');
+      $this->fail('The exactly "foor|bar" label link was found.');
+    }
+    catch (ExpectationException $e) {
+      $this->pass('testLinkNonExistsExact correctly failed. The exactly "foor|bar" label link was not found.');
+    }
+  }
+
+  /**
+   * Tests linkNotExistsExact() functionality.
+   *
+   * @see \Drupal\Tests\WebAssert::linkNotExistsExact()
+   */
+  public function testNotLinkExistsExact() {
+    $this->drupalGet('test-pipe-char');
+    $this->assertSession()->linkNotExistsExact('foo|bar');
+  }
+
+  /**
+   * Tests linkNotExistsExact() functionality fail.
+   *
+   * @see \Drupal\Tests\WebAssert::linkNotExistsExact()
+   */
+  public function testNotLinkNonExistsExact() {
+    $this->drupalGet('test-pipe-char');
+    try {
+      $this->assertSession()->linkNotExistsExact('foo|bar|baz');
+      $this->fail('The exactly "foo|bar|baz" label link was not found.');
+    }
+    catch (ExpectationException $e) {
+      $this->pass('testNotLinkNonExistsExact correctly failed. The exactly "foo|bar|baz" label link was found.');
+    }
+  }
+
+  /**
    * Tests legacy text asserts.
    */
   public function testLegacyTextAsserts() {
diff --git a/core/tests/Drupal/Tests/WebAssert.php b/core/tests/Drupal/Tests/WebAssert.php
index ea42517..9171a3f 100644
--- a/core/tests/Drupal/Tests/WebAssert.php
+++ b/core/tests/Drupal/Tests/WebAssert.php
@@ -232,6 +232,29 @@ public function linkExists($label, $index = 0, $message = '') {
   }
 
   /**
+   * Passes if a link with the exactly specified label is found.
+   *
+   * An optional link index may be passed.
+   *
+   * @param string $label
+   *   Text between the anchor tags.
+   * @param int $index
+   *   Link position counting from zero.
+   * @param string $message
+   *   (optional) A message to display with the assertion. Do not translate
+   *   messages: use strtr() 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 linkExistsExact($label, $index = 0, $message = '') {
+    $message = ($message ? $message : strtr('Link with label %label found.', ['%label' => $label]));
+    $links = $this->session->getPage()->findAll('named_exact', ['link', $label]);
+    $this->assert(!empty($links[$index]), $message);
+  }
+
+  /**
    * Passes if a link with the specified label is not found.
    *
    * An optional link index may be passed.
@@ -253,6 +276,27 @@ public function linkNotExists($label, $message = '') {
   }
 
   /**
+   * Passes if a link with the exactly specified label is not found.
+   *
+   * An optional link index may be passed.
+   *
+   * @param string $label
+   *   Text between the anchor tags.
+   * @param string $message
+   *   (optional) A message to display with the assertion. Do not translate
+   *   messages: use strtr() 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 linkNotExistsExact($label, $message = '') {
+    $message = ($message ? $message : strtr('Link with label %label not found.', ['%label' => $label]));
+    $links = $this->session->getPage()->findAll('named_exact', ['link', $label]);
+    $this->assert(empty($links), $message);
+  }
+
+  /**
    * Passes if a link containing a given href (part) is found.
    *
    * @param string $href
-- 
2.10.1 (Apple Git-78)

