Scenarios that requires the creation of an account where the email confirmation is required does not deletes the user after the test was executed.

Scenario: Register an account
    Given I am an anonymous user
    Given I am at "/"
    When I click "Log in"
    And I click "Create new account"
    And I enter "email@test.com" for "Email address"
    And I enter "myuser" for "Username"
    And I press "Create new account"
    Then I should see the text "A welcome message with further instructions has been sent to your email address."

Comments

uceap-web created an issue. See original summary.

jcandan’s picture

I can confirm this is an issue for me as well.

[edit]
I just realized that we're not using DrupalExtension to create the user, but instead simply filling out and submitting a form. Of course it wouldn't delete the user, right? Think we need to go another route on this, no?

jcandan’s picture

See the related issue (#2304315) for the solution.

[edit]
I spoke too soon. Didn't think through this enough. In our case, we want to ensure a specific success message after the registration form is submitted, to ensure that the email confirmation is required. I'll be back with a solution for that. Something to the effect of checking the associated site settings variable.

jcandan’s picture

Thanks to the comment of @larowlan at https://www.metaltoad.com/blog/what-i-learned-today-drupal-behat-scenari..., I was able to put together a solution.

  /**
   * Keep track of intended user names so they can be cleaned up.
   *
   * @var array
   */
  protected $intended_user_names = [];

  /**
   * Stores the user's name in $this->intended_user_names.
   *
   * This goes before a register form manipulation and submission.
   *
   * @Given I intend to create a user named :name
   *
   * @see cleanUsers()
   */
  public function intendUserName($name) {
    $this->intended_user_names[] = $name;
  }

  /**
   * Add intended users for clean up.
   *
   * Load users by name, add to user manager to allow parent to clean up.
   *
   * @afterScenario
   */
  public function cleanUsers() {
    if (!empty($this->intended_user_names)) {
      foreach ($this->intended_user_names as $name) {
        $user_obj = user_load_by_name($name);

        // userManager takes a simple object for user
        $user = (object) [
          'name' => $user_obj->get('name')->value,
          'email' => $user_obj->get('mail')->value,
          'pass' => $user_obj->get('pass')->value,
          'status' => $user_obj->get('status')->value,
          'uid' => $user_obj->get('uid')->value,
        ];
        $this->userManager->addUser($user);
      }
    }
  }

And now you can include a "Given I intend to create a user named "myuser", Example:

  @api
  Scenario: New registrations must verify email
    Given I am not logged in
    And I intend to create a user named "testy"
    When I go to "user/register"
    And I fill in the following:
      |First name     |Testy          |
      |Last name      |Testerson      |
      |Company        |Testy, Inc.    |
      |Phone          |5555555555     |
      |Email address  |testy@test.edu |
      |Username       |testy          |
    And I press the "Create new account" button
    Then I should see the success message "Thank you for applying for an account."
    And I should see the success message "Your account is currently pending approval by the site administrator."
    And I should see the success message "In the meantime, a welcome message with further instructions has been sent to your email address."

Basically, by letting behat know what user you intend to create, it can go back and remove it after the scenario is complete.