function DevelSwitchUserTest::testSwitchUserFunctionality

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

Tests switch user basic functionality.

File

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

Class

DevelSwitchUserTest
Tests switch user.

Namespace

Drupal\Tests\devel\Functional

Code

public function testSwitchUserFunctionality() : void {
  $this->drupalLogin($this->webUser);
  $this->drupalGet('');
  $this->assertSession()
    ->pageTextNotContains($this->block
    ->label());
  // Ensure that a token is required to switch user.
  $this->drupalGet('/devel/switch/' . $this->webUser
    ->getDisplayName());
  $this->assertSession()
    ->statusCodeEquals(403);
  $this->drupalLogin($this->develUser);
  $this->drupalGet('');
  $this->assertSession()
    ->pageTextContains($this->block
    ->label());
  // Ensure that if name in not passed the controller returns access denied.
  $this->drupalGet('/devel/switch');
  $this->assertSession()
    ->statusCodeEquals(403);
  // Ensure that a token is required to switch user.
  $this->drupalGet('/devel/switch/' . $this->switchUser
    ->getDisplayName());
  $this->assertSession()
    ->statusCodeEquals(403);
  // Switch to another user account.
  $this->drupalGet('/user/' . $this->switchUser
    ->id());
  $this->clickLink($this->switchUser
    ->getDisplayName());
  $this->assertSessionByUid($this->switchUser
    ->id());
  $this->assertNoSessionByUid($this->develUser
    ->id());
  // Switch back to initial account.
  $this->clickLink($this->develUser
    ->getDisplayName());
  $this->assertNoSessionByUid($this->switchUser
    ->id());
  $this->assertSessionByUid($this->develUser
    ->id());
  // Use the search form to switch to another account.
  $edit = [
    'userid' => $this->switchUser
      ->getDisplayName(),
  ];
  $this->submitForm($edit, 'Switch');
  $this->assertSessionByUid($this->switchUser
    ->id());
  $this->assertNoSessionByUid($this->develUser
    ->id());
  // Use the form with username of the maximum length. Mimic the autofill
  // result by adding " (userid)" at the end.
  $edit = [
    'userid' => $this->longUser
      ->getDisplayName() . sprintf(' (%s)', $this->longUser
      ->id()),
  ];
  $this->submitForm($edit, 'Switch');
  $this->assertSessionByUid($this->longUser
    ->id());
  $this->assertNoSessionByUid($this->switchUser
    ->id());
}