Search code examples
phpdrupalbddbehat

Using Drupal's UserManager to clean up user in Behat test


I'm new to Behat and trying to get this test working correctly with Behat version 3.4.3.

001 Scenario: Staff users can create accounts             # features/user.feature:9
  And I register "[email protected]" for cleanup # features/user.feature:21
    16384: Interacting directly with the RawDrupalContext::$users property has been deprecated.
    Use RawDrupalContext::getUserManager->getUsers() instead. in /home/vagrant/.composer/vendor/drupal/drupal-extension/src/Drupal/DrupalExtension/Context/RawDrupalContext.php line 152

And here is what I believe to be the offending step in my FeatureContext.php:

/**
 * @When I register :email for cleanup
 */


public function registerUserByEmailForCleanup($email) {
    // User must exist or this will throw an error.
    $user = $this->userLoadByEmail($email);
    $this->users[$user->name] = $user;
  }

Any pointers on how to convert this to update the step to fix the deprecation error would be much appreciated, or ideas on how to get Behat to ignore the error and keep running. Have had no luck so far. Thanks!


Solution

  • Seems all you have to do is to replace

    $this->users[$user->name] = $user;
    

    with

    $this->getUserManager()->addUser($user);
    

    At least this is what seems logical to me after reading through RawDrupalContext.php.