You are here

public function DevelSwitchUserTest::testSwitchUserListItems in Devel 4.x

Same name and namespace in other branches
  1. 8.3 tests/src/Functional/DevelSwitchUserTest.php \Drupal\Tests\devel\Functional\DevelSwitchUserTest::testSwitchUserListItems()
  2. 8 tests/src/Functional/DevelSwitchUserTest.php \Drupal\Tests\devel\Functional\DevelSwitchUserTest::testSwitchUserListItems()
  3. 8.2 tests/src/Functional/DevelSwitchUserTest.php \Drupal\Tests\devel\Functional\DevelSwitchUserTest::testSwitchUserListItems()

Test the user list items.

File

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

Class

DevelSwitchUserTest
Tests switch user.

Namespace

Drupal\Tests\devel\Functional

Code

public function testSwitchUserListItems() {
  $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);

  // Ensure that the switch user block works properly even if no roles have
  // the 'switch users' permission associated (special handling for user 1).
  $roles = user_roles(TRUE, 'switch users');
  \Drupal::entityTypeManager()
    ->getStorage('user_role')
    ->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);
}