Commit e0035420 authored by catch's avatar catch
Browse files

Revert "fix: #3560202 UserCreationTrait::create* methods can never return false"

This reverts commit 261f34a6.
parent 261f34a6
Loading
Loading
Loading
Loading
Loading
+45 −26
Original line number Diff line number Diff line
@@ -143,13 +143,24 @@ protected function setCurrentUser(AccountInterface $account): void {
   * @param array $values
   *   (optional) An array of initial user field values.
   *
   * @return \Drupal\user\UserInterface
   *   A fully loaded user object with pass_raw property.
   * @return \Drupal\user\Entity\User|false
   *   A fully loaded user object with pass_raw property, or FALSE if account
   *   creation fails.
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   *   If the user creation fails.
   */
  protected function createUser(array $permissions = [], $name = NULL, $admin = FALSE, array $values = []): UserInterface {
  protected function createUser(array $permissions = [], $name = NULL, $admin = FALSE, array $values = []): UserInterface|false {
    // Create a role with the given permission set, if any.
    $rid = FALSE;
    if ($permissions) {
      $rid = $this->createRole($permissions);
      if (!$rid) {
        return FALSE;
      }
    }

    // Create a user assigned to that role.
    $edit = $values;
    if ($name) {
      $edit['name'] = $name;
@@ -162,9 +173,8 @@ protected function createUser(array $permissions = [], $name = NULL, $admin = FA
      'pass' => \Drupal::service('password_generator')->generate(),
      'status' => 1,
    ];

    if ($permissions) {
      $edit['roles'] = [$this->createRole($permissions)];
    if ($rid) {
      $edit['roles'] = [$rid];
    }

    if ($admin) {
@@ -174,7 +184,11 @@ protected function createUser(array $permissions = [], $name = NULL, $admin = FA
    $account = User::create($edit);
    $account->save();

    $this->assertNotNull($account->id(), "User created with name {$edit['name']} and pass {$edit['pass']}");
    $valid_user = $account->id() !== NULL;
    $this->assertTrue($valid_user, "User created with name {$edit['name']} and pass {$edit['pass']}");
    if (!$valid_user) {
      return FALSE;
    }

    // Add the raw password so that we can log in as this user.
    $account->pass_raw = $edit['pass'];
@@ -194,17 +208,17 @@ protected function createUser(array $permissions = [], $name = NULL, $admin = FA
   *   (optional) The weight for the role. Defaults to NULL which sets the
   *   weight to maximum + 1.
   *
   * @return string
   *   Role ID of newly created role.
   * @return string|false
   *   Role ID of newly created role, or FALSE if role creation failed.
   */
  protected function createAdminRole($rid = NULL, $name = NULL, $weight = NULL): string {
  protected function createAdminRole($rid = NULL, $name = NULL, $weight = NULL): string|false {
    $rid = $this->createRole([], $rid, $name, $weight);

    if ($rid) {
      /** @var \Drupal\user\RoleInterface $role */
      $role = Role::load($rid);
      $role->setIsAdmin(TRUE);
      $role->save();

    }
    return $rid;
  }

@@ -221,10 +235,10 @@ protected function createAdminRole($rid = NULL, $name = NULL, $weight = NULL): s
   *   (optional) The weight for the role. Defaults to NULL which sets the
   *   weight to maximum + 1.
   *
   * @return string
   *   Role ID of newly created role.
   * @return string|false
   *   Role ID of newly created role, or FALSE if role creation failed.
   */
  protected function createRole(array $permissions, $rid = NULL, $name = NULL, $weight = NULL): string {
  protected function createRole(array $permissions, $rid = NULL, $name = NULL, $weight = NULL): string|false {
    // Generate a random, lowercase machine name if none was passed.
    if (!isset($rid)) {
      $rid = $this->randomMachineName(8);
@@ -251,6 +265,7 @@ protected function createRole(array $permissions, $rid = NULL, $name = NULL, $we

    $this->assertSame(SAVED_NEW, $result, "Created role ID {$role->id()} with name {$role->label()}.");

    if ($result === SAVED_NEW) {
      // Grant the specified permissions to the role, if any.
      if (!empty($permissions)) {
        $this->grantPermissions($role, $permissions);
@@ -260,6 +275,10 @@ protected function createRole(array $permissions, $rid = NULL, $name = NULL, $we
      }
      return $role->id();
    }
    else {
      return FALSE;
    }
  }

  /**
   * Checks whether a given list of permission names is valid.
+1 −1
Original line number Diff line number Diff line
@@ -57,7 +57,7 @@ public function setUp(): void {
  /**
   * {@inheritdoc}
   */
  protected function drupalCreateUser(array $permissions = [], $name = NULL, $admin = FALSE, array $values = []): UserInterface {
  protected function drupalCreateUser(array $permissions = [], $name = NULL, $admin = FALSE, array $values = []): UserInterface|false {
    // Ensure that users and roles are managed outside a workspace context.
    return \Drupal::service('workspaces.manager')->executeOutsideWorkspace(function () use ($permissions, $name, $admin, $values) {
      $permissions = array_merge($permissions, [