Behat's Guzzle Engine Renames Uploaded Files

Recently, while debugging why I wasn't able to test a certain error message in Drupal containing an uploaded file name with Behat, I discovered that Guzzle renames uploaded files. This is a bit of a pain in the you-know-what. If my Behat scenario uploaded, for example, my-awesome-file.csv, Guzzle would rename the file 4173a805135c1869e3ad964ba47b44e2b980e6c8.csv, and then, more annoyingly, rename it 5bfcc1071d115c41864998ce38a04517ec8d6584.csv on the next upload, and then rename it again on the following upload.

In short, there was no way to output a file name in a drupal_set_message() that allowed me to test it directly with Behat's

Then /^I should see the error message(?:| containing) "([^"]*)"$/

without using Selenium integration. It's important for me to only use the headless browser because we are planning on integrating these tests into our Jenkins environment, and that environment doesn't have the capacity to deal with Firefox in headless mode (which still requires X11, for some reason).

After reading through the Behat code, I noticed that Behat wasn't testing each error message directly. It was pulling the text from the defined error area (.messages.error) in the generated HTML and using strpos() to see if the text in question was contained in the text extracted.

I decided to rewrite my error message with the file name at the very end of the message and as much other descriptive text before that as possible to make sure I'm testing the error I think I'm testing (because my form allows multiple files to be uploaded at the same time for cross-referenced processing). So, my Behat test went from:

Then I should see the error message "Uploaded file 'my-awesome.csv' contains errors."


Then I should see the error message "Errors found at line 4 column 6 of the 'field_uploaded_file_5' file named "

So, hopefully this is useful to you, if you're banging your head against the wall as I did.