From daa912a86831656f7091099ce3aaa7f5213da33d 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.

---
 .../simpletest/lib/Drupal/simpletest/Tests/BrowserTest.php        | 8 ++++++++
 core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php     | 8 +++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/core/modules/simpletest/lib/Drupal/simpletest/Tests/BrowserTest.php b/core/modules/simpletest/lib/Drupal/simpletest/Tests/BrowserTest.php
index 0e27818..1187137 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/Tests/BrowserTest.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/Tests/BrowserTest.php
@@ -52,6 +52,8 @@ 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>
+<a href="link4">A fourth link, containing alternative \\1 regex backreferences \\2</a>
 </body>
 </html>
 EOF;
@@ -63,5 +65,11 @@ 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).');
+
+    $urls = $this->xpath('//a[text()=:text]', array(':text' => 'A fourth link, containing alternative \\1 regex backreferences \\2'));
+    $this->assertEqual($urls[0]['href'], 'link4', 'Match with another regular expression back reference symbol (double backslash).');
   }
 }
diff --git a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
index 4b66223..0359a7b 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
@@ -1817,7 +1817,13 @@ protected function buildXPathQuery($xpath, array $args = array()) {
         // Return the string.
         $value = count($parts) > 1 ? 'concat(' . implode(', \'"\', ', $parts) . ')' : $parts[0];
       }
-      $xpath = preg_replace('/' . preg_quote($placeholder) . '\b/', $value, $xpath);
+
+      // Use preg_replace_callback() instead of preg_replace() to prevent the
+      // regular expression engine from trying to substitute backreferences.
+      $replacement = function ($matches) use ($value) {
+        return $value;
+      };
+      $xpath = preg_replace_callback('/' . preg_quote($placeholder) . '\b/', $replacement, $xpath);
     }
     return $xpath;
   }
-- 
1.8.2.3

