From 3e43de767e0eabd51616a29a60cab3302178c38a Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Sun, 18 Jan 2015 20:10:57 +0000
Subject: [PATCH] Issue #2331685 by pfrenssen, jibran, larowlan, cilefen:
 PHPUnit deprecated assertTag(), assertNotTag() and assertSelectEquals()

---
 .../Tests/Core/Template/AttributeTest.php     |  79 +++++++++++-
 .../Tests/Core/Utility/LinkGeneratorTest.php  | 114 ++++++++++++------
 2 files changed, 150 insertions(+), 43 deletions(-)

diff --git a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php
index d587db2e1832..b4a192dc3686 100644
--- a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php
+++ b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php
@@ -331,15 +331,86 @@ public function testPrint() {
 
     $content = $this->randomMachineName();
     $html = '<div' . (string) $attribute . '>' . $content . '</div>';
-    $this->assertSelectEquals('div.example-class', $content, 1, $html);
-    $this->assertSelectEquals('div.example-class2', $content, 0, $html);
+    $this->assertClass('example-class', $html);
+    $this->assertNoClass('example-class2', $html);
 
-    $this->assertSelectEquals('div#example-id', $content, 1, $html);
-    $this->assertSelectEquals('div#example-id2', $content, 0, $html);
+    $this->assertID('example-id', $html);
+    $this->assertNoID('example-id2', $html);
 
     $this->assertTrue(strpos($html, 'enabled') !== FALSE);
   }
 
+  /**
+   * Checks that the given CSS class is present in the given HTML snippet.
+   *
+   * @param string $class
+   *   The CSS class to check.
+   * @param string $html
+   *   The HTML snippet to check.
+   */
+  protected function assertClass($class, $html) {
+    $xpath = "//*[@class='$class']";
+    self::assertTrue((bool) $this->getXPathResultCount($xpath, $html));
+  }
+
+  /**
+   * Checks that the given CSS class is not present in the given HTML snippet.
+   *
+   * @param string $class
+   *   The CSS class to check.
+   * @param string $html
+   *   The HTML snippet to check.
+   */
+  protected function assertNoClass($class, $html) {
+    $xpath = "//*[@class='$class']";
+    self::assertFalse((bool) $this->getXPathResultCount($xpath, $html));
+  }
+
+  /**
+   * Checks that the given CSS ID is present in the given HTML snippet.
+   *
+   * @param string $id
+   *   The CSS ID to check.
+   * @param string $html
+   *   The HTML snippet to check.
+   */
+  protected function assertID($id, $html) {
+    $xpath = "//*[@id='$id']";
+    self::assertTrue((bool) $this->getXPathResultCount($xpath, $html));
+  }
+
+  /**
+   * Checks that the given CSS ID is not present in the given HTML snippet.
+   *
+   * @param string $id
+   *   The CSS ID to check.
+   * @param string $html
+   *   The HTML snippet to check.
+   */
+  protected function assertNoID($id, $html) {
+    $xpath = "//*[@id='$id']";
+    self::assertFalse((bool) $this->getXPathResultCount($xpath, $html));
+  }
+
+  /**
+   * Counts the occurrences of the given XPath query in a given HTML snippet.
+   *
+   * @param string $query
+   *   The XPath query to execute.
+   * @param string $html
+   *   The HTML snippet to check.
+   *
+   * @return int
+   *   The number of results that are found.
+   */
+  protected function getXPathResultCount($query, $html) {
+    $document = new \DOMDocument;
+    $document->loadHTML($html);
+    $xpath = new \DOMXPath($document);
+
+    return $xpath->query($query)->length;
+  }
+
   /**
    * Tests the storage method.
    */
diff --git a/core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php b/core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php
index 1f371fadf432..f28f6e2ff0db 100644
--- a/core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php
+++ b/core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php
@@ -109,8 +109,7 @@ public function testGenerateHrefs($route_name, array $parameters, $absolute, $ex
     $url = new Url($route_name, $parameters, array('absolute' => $absolute));
     $url->setUrlGenerator($this->urlGenerator);
     $result = $this->linkGenerator->generate('Test', $url);
-    $this->assertTag(array(
-      'tag' => 'a',
+    $this->assertLink(array(
       'attributes' => array('href' => $expected_url),
       ), $result);
   }
@@ -134,8 +133,7 @@ public function testGenerate() {
     $url->setUrlGenerator($this->urlGenerator);
 
     $result = $this->linkGenerator->generate('Test', $url);
-    $this->assertTag(array(
-      'tag' => 'a',
+    $this->assertLink(array(
       'attributes' => array(
         'href' => '/test-route-1#the-fragment',
       ),
@@ -172,8 +170,7 @@ public function testGenerateExternal() {
     $url->setOption('set_active_class', TRUE);
 
     $result = $this->linkGenerator->generate('Drupal', $url);
-    $this->assertTag(array(
-      'tag' => 'a',
+    $this->assertLink(array(
       'attributes' => array(
         'href' => 'http://drupal.org',
       ),
@@ -200,8 +197,7 @@ public function testGenerateAttributes() {
     ));
     $url->setUrlGenerator($this->urlGenerator);
     $result = $this->linkGenerator->generate('Test', $url);
-    $this->assertTag(array(
-      'tag' => 'a',
+    $this->assertLink(array(
       'attributes' => array(
         'href' => '/test-route-1',
         'title' => 'Tooltip',
@@ -227,8 +223,7 @@ public function testGenerateQuery() {
     ));
     $url->setUrlGenerator($this->urlGenerator);
     $result = $this->linkGenerator->generate('Test', $url);
-    $this->assertTag(array(
-      'tag' => 'a',
+    $this->assertLink(array(
       'attributes' => array(
         'href' => '/test-route-1?test=value',
       ),
@@ -251,8 +246,7 @@ public function testGenerateParametersAsQuery() {
     $url = new Url('test_route_1', array('test' => 'value'), array());
     $url->setUrlGenerator($this->urlGenerator);
     $result = $this->linkGenerator->generate('Test', $url);
-    $this->assertTag(array(
-      'tag' => 'a',
+    $this->assertLink(array(
       'attributes' => array(
         'href' => '/test-route-1?test=value',
       ),
@@ -277,8 +271,7 @@ public function testGenerateOptions() {
     ));
     $url->setUrlGenerator($this->urlGenerator);
     $result = $this->linkGenerator->generate('Test', $url);
-    $this->assertTag(array(
-      'tag' => 'a',
+    $this->assertLink(array(
       'attributes' => array(
         'href' => '/test-route-1?test=value',
       ),
@@ -302,13 +295,7 @@ public function testGenerateXss() {
     $url = new Url('test_route_4');
     $url->setUrlGenerator($this->urlGenerator);
     $result = $this->linkGenerator->generate("<script>alert('XSS!')</script>", $url);
-    $this->assertNotTag(array(
-      'tag' => 'a',
-      'attributes' => array('href' => '/test-route-4'),
-      'child' => array(
-        'tag' => 'script',
-      ),
-    ), $result);
+    $this->assertNoXPathResults('//a[@href="/test-route-4"]/script', $result);
   }
 
   /**
@@ -336,8 +323,7 @@ public function testGenerateWithHtml() {
     ));
     $url->setUrlGenerator($this->urlGenerator);
     $result = $this->linkGenerator->generate('Test', $url);
-    $this->assertTag(array(
-      'tag' => 'a',
+    $this->assertLink(array(
       'attributes' => array(
         'href' => '/test-route-5',
         'title' => 'HTML Tooltip',
@@ -348,8 +334,7 @@ public function testGenerateWithHtml() {
     $url = new Url('test_route_5', array(), array('html' => TRUE));
     $url->setUrlGenerator($this->urlGenerator);
     $result = $this->linkGenerator->generate('<em>HTML output</em>', $url);
-    $this->assertTag(array(
-      'tag' => 'a',
+    $this->assertLink(array(
       'attributes' => array('href' => '/test-route-5'),
       'child' => array(
         'tag' => 'em',
@@ -390,8 +375,7 @@ public function testGenerateActive() {
     $url = new Url('test_route_1', array(), array('set_active_class' => TRUE));
     $url->setUrlGenerator($this->urlGenerator);
     $result = $this->linkGenerator->generate('Test', $url);
-    $this->assertTag(array(
-      'tag' => 'a',
+    $this->assertLink(array(
       'attributes' => array('data-drupal-link-system-path' => 'test-route-1'),
     ), $result);
 
@@ -399,10 +383,7 @@ public function testGenerateActive() {
     $url = new Url('test_route_1', array(), array('set_active_class' => FALSE));
     $url->setUrlGenerator($this->urlGenerator);
     $result = $this->linkGenerator->generate('Test', $url);
-    $this->assertNotTag(array(
-      'tag' => 'a',
-      'attributes' => array('data-drupal-link-system-path' => 'test-route-1'),
-    ), $result);
+    $this->assertNoXPathResults('//a[@data-drupal-link-system-path="test-route-1"]', $result);
 
     // Render a link with an associated language.
     $url = new Url('test_route_1', array(), array(
@@ -411,8 +392,7 @@ public function testGenerateActive() {
     ));
     $url->setUrlGenerator($this->urlGenerator);
     $result = $this->linkGenerator->generate('Test', $url);
-    $this->assertTag(array(
-      'tag' => 'a',
+    $this->assertLink(array(
       'attributes' => array(
         'data-drupal-link-system-path' => 'test-route-1',
         'hreflang' => 'de',
@@ -426,11 +406,10 @@ public function testGenerateActive() {
     ));
     $url->setUrlGenerator($this->urlGenerator);
     $result = $this->linkGenerator->generate('Test', $url);
-    $this->assertTag(array(
-      'tag' => 'a',
+    $this->assertLink(array(
       'attributes' => array(
         'data-drupal-link-system-path' => 'test-route-3',
-        'data-drupal-link-query' => 'regexp:/.*value.*example_1.*/',
+        'data-drupal-link-query' => '{"value":"example_1"}',
       ),
     ), $result);
 
@@ -441,15 +420,72 @@ public function testGenerateActive() {
     ));
     $url->setUrlGenerator($this->urlGenerator);
     $result = $this->linkGenerator->generate('Test', $url);
-    $this->assertTag(array(
-      'tag' => 'a',
+    $this->assertLink(array(
       'attributes' => array(
         'data-drupal-link-system-path' => 'test-route-4/1',
-        'data-drupal-link-query' => 'regexp:/.*value.*example_1.*/',
+        'data-drupal-link-query' => '{"value":"example_1"}',
       ),
     ), $result);
   }
 
+  /**
+   * Checks that a link with certain properties exists in a given HTML snippet.
+   *
+   * @param array $properties
+   *   An associative array of link properties, with the following keys:
+   *   - attributes: optional array of HTML attributes that should be present.
+   *   - content: optional link content.
+   * @param string $html
+   *   The HTML to check.
+   * @param int $count
+   *   How many times the link should be present in the HTML. Defaults to 1.
+   */
+  public static function assertLink(array $properties, $html, $count = 1) {
+    // Provide default values.
+    $properties += array('attributes' => array());
+
+    // Create an XPath query that selects a link element.
+    $query = '//a';
+
+    // Append XPath predicates for the attributes and content text.
+    $predicates = array();
+    foreach ($properties['attributes'] as $attribute => $value) {
+      $predicates[] = "@$attribute='$value'";
+    }
+    if (!empty($properties['content'])) {
+      $predicates[] = "contains(.,'{$properties['content']}')";
+    }
+    if (!empty($predicates)) {
+      $query .= '[' . implode(' and ', $predicates) . ']';
+    }
+
+    // Execute the query.
+    $document = new \DOMDocument;
+    $document->loadHTML($html);
+    $xpath = new \DOMXPath($document);
+
+    self::assertEquals($count, $xpath->query($query)->length);
+  }
+
+  /**
+   * Checks that the given XPath query has no results in a given HTML snippet.
+   *
+   * @param string $query
+   *   The XPath query to execute.
+   * @param string $html
+   *   The HTML snippet to check.
+   *
+   * @return int
+   *   The number of results that are found.
+   */
+  protected function assertNoXPathResults($query, $html) {
+    $document = new \DOMDocument;
+    $document->loadHTML($html);
+    $xpath = new \DOMXPath($document);
+
+    self::assertFalse((bool) $xpath->query($query)->length);
+  }
+
 }
 
 }
-- 
GitLab