function DevelSwitchUserTest::testSwitchUserListItems

Same name and namespace in other branches
  1. 4.x tests/src/Functional/DevelSwitchUserTest.php \Drupal\Tests\devel\Functional\DevelSwitchUserTest::testSwitchUserListItems()

Test the user list items.

File

tests/src/Functional/DevelSwitchUserTest.php, line 155

Class

DevelSwitchUserTest
Tests switch user.

Namespace

Drupal\Tests\devel\Functional

Code

public function testSwitchUserListItems() : void {
  $anonymous = \Drupal::config('user.settings')->get('anonymous');
  $this->setBlockConfiguration('list_size', 2);
  // Login as web user so we are sure that this account is prioritized
  // in the list if not enough users with 'switch users' permission are
  // present.
  $this->drupalLogin($this->webUser);
  $this->drupalLogin($this->develUser);
  $this->drupalGet('');
  // Ensure that users with 'switch users' permission are prioritized.
  $this->assertSwitchUserListCount(2);
  $this->assertSwitchUserListContainsUser($this->develUser
    ->getDisplayName());
  $this->assertSwitchUserListContainsUser($this->switchUser
    ->getDisplayName());
  // Ensure that blocked users are not shown in the list.
  $this->switchUser
    ->set('status', 0)
    ->save();
  $this->drupalGet('');
  $this->assertSwitchUserListCount(2);
  $this->assertSwitchUserListContainsUser($this->develUser
    ->getDisplayName());
  $this->assertSwitchUserListContainsUser($this->webUser
    ->getDisplayName());
  $this->assertSwitchUserListNoContainsUser($this->switchUser
    ->getDisplayName());
  // Ensure that anonymous user are prioritized if include_anon is set to
  // true.
  $this->setBlockConfiguration('include_anon', TRUE);
  $this->drupalGet('');
  $this->assertSwitchUserListCount(2);
  $this->assertSwitchUserListContainsUser($this->develUser
    ->getDisplayName());
  $this->assertSwitchUserListContainsUser($anonymous);
  // Ensure that the switch user block works properly even if no prioritized
  // users are found (special handling for user 1).
  $this->drupalLogout();
  $this->develUser
    ->delete();
  $this->drupalLogin($this->rootUser);
  $this->drupalGet('');
  $this->assertSwitchUserListCount(2);
  // Removed assertion on rootUser which causes random test failures.
  // @todo Adjust the tests when user 1 option is completed.
  // @see https://www.drupal.org/project/devel/issues/3097047
  // @see https://www.drupal.org/project/devel/issues/3114264
  $this->assertSwitchUserListContainsUser($anonymous);
  $roleStorage = \Drupal::entityTypeManager()->getStorage('user_role');
  // Ensure that the switch user block works properly even if no roles have
  // the 'switch users' permission associated (special handling for user 1).
  /** @var array<string, \Drupal\user\RoleInterface> $roles */
  $roles = $roleStorage->loadMultiple();
  unset($roles[AccountInterface::ANONYMOUS_ROLE]);
  $roles = array_filter($roles, static fn($role): bool => $role->hasPermission('switch users'));
  $roleStorage->delete($roles);
  $this->drupalGet('');
  $this->assertSwitchUserListCount(2);
  // Removed assertion on rootUser which causes random test failures.
  // @todo Adjust the tests when user 1 option is completed.
  // @see https://www.drupal.org/project/devel/issues/3097047
  // @see https://www.drupal.org/project/devel/issues/3114264
  $this->assertSwitchUserListContainsUser($anonymous);
}