Dollar signs followed by a number in the values of replacement arguments passed to DrupalWebTestCase::buildXPathQuery() seem to be handled as a regular expression back reference, causing certain values to be changed inappropriately.
$query = $this->buildXPathQuery(':test', array(':test' => '$1bc'));
I expected $query to have the value: "$1bc"
Actual value however is: "bc"
Use case: we use DrupalWebTestCase::randomString() quite often to generate node titles. DrupalWebTestCase::randomString() might generate something that is recognized as a back reference.
Comment | File | Size | Author |
---|---|---|---|
#14 | 1988780-13-simpletest-backreferences-test_only.patch | 1.37 KB | pfrenssen |
#13 | interdiff.txt | 1.01 KB | pfrenssen |
#13 | 1988780-13-simpletest-backreferences.patch | 1.98 KB | pfrenssen |
Comments
Comment #1
pfrenssenThis problem is also present in D8, let's fix it there first.
Comment #3
pfrenssenComment #4
Cyberwolf CreditAttribution: Cyberwolf commentedAre backreferences starting with double slash handled as well (for example: \\1)? That also seems to be an issue.
Comment #5
pfrenssenGood catch, supported them in this patch, and followed your advice about using preg_replace_callback(). Also added a test to cover the double slash backreferences.
Comment #6
angel.hI've made the 7.x patch based on the patch 5 from the previous comment.
Comment #7
angel.hComment #8
Cyberwolf CreditAttribution: Cyberwolf commentedLooks good to me, thanks!
Comment #9
alexpottCommitted 43b389d and pushed to 8.x. Thanks!
Now #6 needs review for 7.x
Comment #10
Damien Tournoud CreditAttribution: Damien Tournoud commentedNeeds work for Drupal 7, as anonymous functions are only supported on PHP 5.3+ and Drupal 7 supports 5.2+.
Comment #11
angel.hComment #12
angel.hComment #12.0
angel.hsmall correction
Comment #13
pfrenssenIt's not possible to do this with preg_replace_callback() in PHP 5.2. You can only create anonymous functions using create_function() but this does not allow to pass variables from the current scope, like you can do with closures in PHP5.3.
Let's try it with the venerable str_replace() instead.
Comment #14
pfrenssenForgot to upload the test_only patch. This one should fail.
Comment #17
pfrenssenTests fail because str_replace() does not respect the word boundary as preg_replace('//\b/') does, it also replaces :date in "xsd:dateTime" below:
We have to respect these word boundaries.