jhedstrom / drupalextension

An integration layer between Behat, Mink Extension, and Drupal.
GNU General Public License v2.0
209 stars 192 forks source link

Don't throw an exception when attempting to delete role that doesn't exist #551

Closed bkosborne closed 5 years ago

bkosborne commented 5 years ago

In RawDrupalContext, there is an AfterScenario that deletes roles that were programmatically created by other step definitions that were executed:

  /**
   * Remove any created roles.
   *
   * @AfterScenario
   */
  public function cleanRoles() {
    // Remove any roles that were created.
    foreach ($this->roles as $rid) {
      $this->getDriver()->roleDelete($rid);
    }
    $this->roles = array();
  }

For the Drupal8 core, this code gets run:

  /**
   * {@inheritdoc}
   */
  public function roleDelete($role_name) {
    $role = user_role_load($role_name);

    if (!$role) {
      throw new \RuntimeException(sprintf('No role "%s" exists.', $role_name));
    }

    $role->delete();
  }

The problem is that the role may no longer exist, since actions taken during the step may have deleted the role. This exception causes the test to fail even though everything is fine.

I noticed that the other "delete" methods on the Drupal8 core first check that the entity being deleted exists before deleting it. I think we should do the same here.

bkosborne commented 5 years ago

Oops, this actually belongs here: https://github.com/jhedstrom/DrupalDriver/issues/210