Join GitHub today
"When I attach the file" and Drupal temporary files #355
The following steps work the first occasion they are run, but not on subsequent occasions:
What is needed is to keep track of uploaded files and to clean them up after scenarios.
I suggest we add:
In fact, #337 will probably include making a generic entity cleanup mechanism, rather than endlessly proliferating near-duplicate entity-type-specific mechanisms.
referenced this issue
Feb 7, 2017
Sorry, I overlooked the obvious: this is a UI step not an API step, so we don't have a definitive reference to the file entity associated with the uploaded step, so we can't clean it up.
It's an instance of the more general problem of cleaning up changes to entities made by steps using the UI.
I can't see a good global solution. But we might be able to help people create their own solutions if the driver had an entityDelete function that could take a very flexible condition argument. With this developers could then create their own hook functions to delete entities meeting the conditions.
In the case of files, there could be an @cleanCreatedFiles hook that deleted any file entity created after the start time of the scenario.
OK, I'm slow today.
This is already handled for nodes and comments by Drupal itself using hook_ENTITY_TYPE_predelete to delete content created by a user. So when a user created by something like "Given I am logged in as" is cleaned up, their nodes and comments created through the UI is cleaned up.
The file module doesn't use hook_user_predelete to clean up file entities when a user is deleted, hence my steps fail.
Perhaps what we need is for the Drupal extension to have user delete/predelete hooks as well as the existing user create hooks. Then we could specify additional clean up for Behat created users, including files they 'own'.
Only if it is configured to do so, you can also configure Drupal to unpublish the content when deleting the user, or to mark it as being owned by the anonymous user. In these cases the content will remain after the test and needs to be cleaned up manually.
The rule in general is that anything created through the UI should be cleaned up manually at the end of the test.
Yes something like
In project code this is really easy to do. In D8 I use an
Then in the subcontexts of my modules I provide a human readable step, but this can also be using the generic step definition from above. Here is the one for nodes: