UserHelpersTest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace Drupal\FunctionalTests;
  3. use Drupal\Tests\BrowserTestBase;
  4. /**
  5. * Tests User related helper methods of WebTestBase.
  6. *
  7. * @group browsertestbase
  8. */
  9. class UserHelpersTest extends BrowserTestBase {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. protected $defaultTheme = 'stark';
  14. /**
  15. * Tests WebTestBase::drupalUserIsLoggedIn().
  16. */
  17. public function testDrupalUserIsLoggedIn() {
  18. $first_user = $this->drupalCreateUser();
  19. $second_user = $this->drupalCreateUser();
  20. // After logging in, the first user should be logged in, the second not.
  21. $this->drupalLogin($first_user);
  22. $this->assertTrue($this->drupalUserIsLoggedIn($first_user));
  23. $this->assertFalse($this->drupalUserIsLoggedIn($second_user));
  24. // Verify that logged in state is retained across pages.
  25. $this->drupalGet('');
  26. $this->assertTrue($this->drupalUserIsLoggedIn($first_user));
  27. $this->assertFalse($this->drupalUserIsLoggedIn($second_user));
  28. // After logging out, both users should be logged out.
  29. $this->drupalLogout();
  30. $this->assertFalse($this->drupalUserIsLoggedIn($first_user));
  31. $this->assertFalse($this->drupalUserIsLoggedIn($second_user));
  32. // After logging back in, the second user should still be logged out.
  33. $this->drupalLogin($first_user);
  34. $this->assertTrue($this->drupalUserIsLoggedIn($first_user));
  35. $this->assertFalse($this->drupalUserIsLoggedIn($second_user));
  36. // After logging in the second user, the first one should be logged out.
  37. $this->drupalLogin($second_user);
  38. $this->assertTrue($this->drupalUserIsLoggedIn($second_user));
  39. $this->assertFalse($this->drupalUserIsLoggedIn($first_user));
  40. // After logging out, both should be logged out.
  41. $this->drupalLogout();
  42. $this->assertFalse($this->drupalUserIsLoggedIn($first_user));
  43. $this->assertFalse($this->drupalUserIsLoggedIn($second_user));
  44. }
  45. }