使用Drupal的UserManager清理Behat测试中的用户

时间:2018-06-02 03:16:57

标签: php drupal bdd behat

我是Behat的新手,并尝试使用Behat版本3.4.3正确运行此测试。

001 Scenario: Staff users can create accounts             # features/user.feature:9
  And I register "byron@poetsforever.com" 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

以下是我认为是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;
  }

有关如何转换此更新以修复弃用错误的步骤的任何指示将非常感激,或者有关如何让Behat忽略错误并继续运行的想法。到目前为止没有运气。谢谢!

2 个答案:

答案 0 :(得分:0)

似乎你所要做的就是替换

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

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

阅读完RawDrupalContext.php后,至少这对我来说是合乎逻辑的。

答案 1 :(得分:0)

I have come across the exact same issue with a form allowing for manual user creation. It's very unclear how behat handles entities, but it ultimately is using a \stdClass and not the User class you would expect:

Here's the code & gherkin syntax we used to accomplish:

 /**
   * Create a user and register with Behat for cleanup.
   *
   * Let behat know we manually created a user so it can clean it up in its
   * AfterScenario hook.
   *
   * @TODO: Is there no existing method for this step??
   *
   * @Then I have manually created :username
   */
  public function manuallyCreatedUser($username) {
    $user = user_load_by_name($username);
    if ($user) {
      $user_obj = (object) array(
        'name' => $username,
        'uid' => $user->id(),
        'pass' => '',
        'mail' => $user->getEmail(),
        'role' => implode(",", $user->getRoles()),
      );
      $this->users[$username] = $user_obj;
    }
  }

There's a few things going on here:

  • Behat is not using a User object to keep track, but instead a \stdClass with properties.
  • This simply creates the object behat is expecting
  • Came to this solution by following along RawDrupalContext and ultimately Drupal8::userDelete()