From 852f0bdd23efe3264b92467e6fce58af2cf90f23 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Mon, 9 May 2016 06:48:10 -0500
Subject: [PATCH] Issue #2720869 by jibran: Remove the use of deprecated
 CssSelector and use CssSelectorConverter instead

(cherry picked from commit 2bef9564ca6f66cc4e52ec45e7d9a14ac832b1fa)
---
 .../simpletest/src/AssertContentTrait.php     |  4 +--
 .../Entity/FieldEntityTranslationTest.php     |  6 ++---
 .../JavascriptTestBase.php                    |  5 ++--
 core/tests/Drupal/Tests/BrowserTestBase.php   | 27 ++++++++++++++++---
 4 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/core/modules/simpletest/src/AssertContentTrait.php b/core/modules/simpletest/src/AssertContentTrait.php
index 8bbdb57a0538..e8da02ce8679 100644
--- a/core/modules/simpletest/src/AssertContentTrait.php
+++ b/core/modules/simpletest/src/AssertContentTrait.php
@@ -7,7 +7,7 @@
 use Drupal\Component\Utility\SafeMarkup;
 use Drupal\Component\Utility\Xss;
 use Drupal\Core\Render\RenderContext;
-use Symfony\Component\CssSelector\CssSelector;
+use Symfony\Component\CssSelector\CssSelectorConverter;
 
 /**
  * Provides test methods to assert content.
@@ -247,7 +247,7 @@ protected function xpath($xpath, array $arguments = []) {
    *   selector to an XPath selector.
    */
   protected function cssSelect($selector) {
-    return $this->xpath(CssSelector::toXPath($selector));
+    return $this->xpath((new CssSelectorConverter())->toXPath($selector));
   }
 
   /**
diff --git a/core/modules/views/src/Tests/Entity/FieldEntityTranslationTest.php b/core/modules/views/src/Tests/Entity/FieldEntityTranslationTest.php
index 12c59e4abc5d..eed71930198e 100644
--- a/core/modules/views/src/Tests/Entity/FieldEntityTranslationTest.php
+++ b/core/modules/views/src/Tests/Entity/FieldEntityTranslationTest.php
@@ -7,7 +7,7 @@
 use Drupal\node\Entity\Node;
 use Drupal\node\Entity\NodeType;
 use Drupal\views\Tests\ViewTestBase;
-use Symfony\Component\CssSelector\CssSelector;
+use Symfony\Component\CssSelector\CssSelectorConverter;
 
 /**
  * Tests the rendering of fields (base fields) and their translations.
@@ -171,8 +171,8 @@ protected function assertRows($expected = []) {
     $rows = $this->cssSelect('div.views-row');
     foreach ($rows as $row) {
       $actual[] = [
-        'title' => (string) $row->xpath(CssSelector::toXPath('.views-field-title span.field-content a'))[0],
-        'sticky' => (string) $row->xpath(CssSelector::toXPath('.views-field-sticky span.field-content'))[0],
+        'title' => (string) $row->xpath((new CssSelectorConverter())->toXPath('.views-field-title span.field-content a'))[0],
+        'sticky' => (string) $row->xpath((new CssSelectorConverter())->toXPath('.views-field-sticky span.field-content'))[0],
       ];
     }
     $this->assertEqual($actual, $expected);
diff --git a/core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php b/core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php
index 9f0f016ea570..8f1ead5cc6d9 100644
--- a/core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php
+++ b/core/tests/Drupal/FunctionalJavascriptTests/JavascriptTestBase.php
@@ -3,7 +3,6 @@
 namespace Drupal\FunctionalJavascriptTests;
 
 use Drupal\Tests\BrowserTestBase;
-use Symfony\Component\CssSelector\CssSelector;
 use Zumba\Mink\Driver\PhantomJSDriver;
 
 /**
@@ -46,7 +45,7 @@ protected function initMink() {
    *   \Behat\Mink\Element\NodeElement::isVisible() instead.
    */
   protected function assertElementVisible($css_selector, $message = '') {
-    $this->assertTrue($this->getSession()->getDriver()->isVisible(CssSelector::toXPath($css_selector)), $message);
+    $this->assertTrue($this->getSession()->getDriver()->isVisible($this->cssSelectToXpath($css_selector)), $message);
   }
 
   /**
@@ -61,7 +60,7 @@ protected function assertElementVisible($css_selector, $message = '') {
    *   \Behat\Mink\Element\NodeElement::isVisible() instead.
    */
   protected function assertElementNotVisible($css_selector, $message = '') {
-    $this->assertFalse($this->getSession()->getDriver()->isVisible(CssSelector::toXPath($css_selector)), $message);
+    $this->assertFalse($this->getSession()->getDriver()->isVisible($this->cssSelectToXpath($css_selector)), $message);
   }
 
   /**
diff --git a/core/tests/Drupal/Tests/BrowserTestBase.php b/core/tests/Drupal/Tests/BrowserTestBase.php
index cefb7538f6c8..aa71fc128132 100644
--- a/core/tests/Drupal/Tests/BrowserTestBase.php
+++ b/core/tests/Drupal/Tests/BrowserTestBase.php
@@ -23,7 +23,7 @@
 use Drupal\user\Entity\Role;
 use Drupal\user\Entity\User;
 use Drupal\user\UserInterface;
-use Symfony\Component\CssSelector\CssSelector;
+use Symfony\Component\CssSelector\CssSelectorConverter;
 use Symfony\Component\HttpFoundation\Request;
 
 /**
@@ -1447,7 +1447,7 @@ protected function drupalUserIsLoggedIn(UserInterface $account) {
    *   Optional message to show alongside the assertion.
    */
   protected function assertElementPresent($css_selector, $message = '') {
-    $this->assertNotEmpty($this->getSession()->getDriver()->find(CssSelector::toXPath($css_selector)), $message);
+    $this->assertNotEmpty($this->getSession()->getDriver()->find($this->cssSelectToXpath($css_selector)), $message);
   }
 
   /**
@@ -1459,7 +1459,7 @@ protected function assertElementPresent($css_selector, $message = '') {
    *   Optional message to show alongside the assertion.
    */
   protected function assertElementNotPresent($css_selector, $message = '') {
-    $this->assertEmpty($this->getSession()->getDriver()->find(CssSelector::toXPath($css_selector)), $message);
+    $this->assertEmpty($this->getSession()->getDriver()->find($this->cssSelectToXpath($css_selector)), $message);
   }
 
   /**
@@ -1469,7 +1469,7 @@ protected function assertElementNotPresent($css_selector, $message = '') {
    *   The CSS selector identifying the element to click.
    */
   protected function click($css_selector) {
-    $this->getSession()->getDriver()->click(CssSelector::toXPath($css_selector));
+    $this->getSession()->getDriver()->click($this->cssSelectToXpath($css_selector));
   }
 
   /**
@@ -1529,4 +1529,23 @@ protected function getHtmlOutputHeaders() {
     return '<hr />Headers: <pre>' . Html::escape(var_export($headers, TRUE)) . '</pre>';
   }
 
+  /**
+   * Translates a CSS expression to its XPath equivalent.
+   *
+   * The search is relative to the root element (HTML tag normally) of the page.
+   *
+   * @param string $selector
+   *   CSS selector to use in the search.
+   * @param bool $html
+   *   (optional) Enables HTML support. Disable it for XML documents.
+   * @param string $prefix
+   *   (optional) The prefix for the XPath expression.
+   *
+   * @return string
+   *   The equivalent XPath of a CSS expression.
+   */
+  protected function cssSelectToXpath($selector, $html = TRUE, $prefix = 'descendant-or-self::') {
+    return (new CssSelectorConverter($html))->toXPath($selector, $prefix);
+  }
+
 }
-- 
GitLab