Follow-up of #2004408: Allow modules to alter the result of EntityListController::getOperations and #2020209: Random failures due to non uniqueness of randomString() and randomName()

There are still random failures in the entity operations test from time to time. And it's not the fault of that test, we just always only used random names whenever we wanted to assert something on a generated HTML page.

@alexpott and I sat together in Dublin and we tracked the problem down. At least in assertLink() and probably also other places that use xpath and our own xpath placeholder implementation, we do two things

- We run it through the normalize-path() xpath function. This removes whitespaces at the beginning and end *and* replaces repeated whitespaces with a single one
- We insert values using a preg_replace() and don't quote the replaced value correctly, we only quote the placeholder. This is a problem because values can contain special things like placeholders (\[0-9] I think) that are then altered.

So far, we identified two problematic patterns due to that:
- strings that start/end with a space or have repeated spaces. We added a partial fix by trimming the role label in WebTestBase but maybe we should prevent such random strings by default?
- The mentioned preg_match placeholders, the example we had was \8. We can either prevent that too or make sure that we quote it properly everywhere use it.

$string = 'test\8';
preg_replace('/(.+)/', $string, 'string') == $string; // FALSE
preg_replace('/(.+)/', preg_quote($string), 'string') == $string; // TRUE

- A pattern that I just had in a test looks like this: $7@|{v$S. This is even more fun:

$string = '$7@|{v$S';
print preg_replace('/(.+)/', $string, 'string'); // @|{v$S
print preg_replace('/(.+)/', preg_quote($string), 'string'); // $7@\|\{v$S
preg_replace('/(.+)/', $string, 'string') == $string; // FALSE
preg_replace('/(.+)/', preg_quote($string), 'string') == $string; // FALSE

So, preg_quote() doesn't work for everything.

Comments

Berdir’s picture

A possible fix this might also be a "DO NOT use this for anything you want to assert on a page, RANDOM FAILS WILL HAPPEN. You have been warned." :)

alexpott’s picture