From 50a5e19c1b69ba6e6e6faebb2d80ee9530936cd5 Mon Sep 17 00:00:00 2001
From: Pieter Frenssen <pieter@frenssen.be>
Date: Wed, 15 May 2013 08:26:49 +0200
Subject: [PATCH] Issue #1988780 by pfrenssen: Fixed
 DrupalWebTestCase::buildXPathQuery() tries to handle backreferences in
 argument values.

---
 core/modules/simpletest/lib/Drupal/simpletest/Tests/BrowserTest.php | 4 ++++
 core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php       | 5 +++--
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/core/modules/simpletest/lib/Drupal/simpletest/Tests/BrowserTest.php b/core/modules/simpletest/lib/Drupal/simpletest/Tests/BrowserTest.php
index 0e27818..50765af 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/Tests/BrowserTest.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/Tests/BrowserTest.php
@@ -52,6 +52,7 @@ function testXPathEscaping() {
 <body>
 <a href="link1">A "weird" link, just to bother the dumb "XPath 1.0"</a>
 <a href="link2">A second "even more weird" link, in memory of George O'Malley</a>
+<a href="link3">A \$third$ link, so weird it's worth $1 million</a>
 </body>
 </html>
 EOF;
@@ -63,5 +64,8 @@ function testXPathEscaping() {
 
     $urls = $this->xpath('//a[text()=:text]', array(':text' => 'A second "even more weird" link, in memory of George O\'Malley'));
     $this->assertEqual($urls[0]['href'], 'link2', 'Match with mixed single and double quotes.');
+
+    $urls = $this->xpath('//a[text()=:text]', array(':text' => 'A $third$ link, so weird it\'s worth $1 million'));
+    $this->assertEqual($urls[0]['href'], 'link3', 'Match with a regular expression back reference symbol (dollar sign).');
   }
 }
diff --git a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
index 4b66223..2918b49 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
@@ -1809,9 +1809,10 @@ protected function buildXPathQuery($xpath, array $args = array()) {
         // Explode the text at the quote characters.
         $parts = explode('"', $value);
 
-        // Quote the parts.
+        // Quote the parts. Escape dollar signs so they are not mistaken as
+        // backreferences when passed on to preg_replace().
         foreach ($parts as &$part) {
-          $part = '"' . $part . '"';
+          $part = '"' . str_replace('$', '\\$', $part) . '"';
         }
 
         // Return the string.
-- 
1.8.2.2

