- Issue created by @kevinquillen
- 🇫🇷France pbonnefoi
I have a similar problem trying to fix a unit test on a contrib module, but adding $this->getSession()->reset(); did not work. Do you add it right after drupalLogout() ?
An improvement on the error message would be very nice as well because it does not make much sense.
The unit test causing problem is from the redirect_after_login contrib module.
- 🇺🇸United States kevinquillen
Calling drupalLogout() in this scenario is the problem because assert() fails (its in one of the test traits) - it thinks the session is still authenticated. This does not happen outside of a test.
I believe if the test method was updated to this, it would resolve the issue:
protected function drupalLogout() { // Make a request to the logout page, and redirect to the user page, the // idea being if you were properly logged out you should be seeing a login // screen. $assert_session = $this->assertSession(); $destination = Url::fromRoute('user.page')->toString(); $this->drupalGet(Url::fromRoute('user.logout', [], ['query' => ['destination' => $destination]])); $assert_session->fieldExists('name'); $assert_session->fieldExists('pass'); // @see BrowserTestBase::drupalUserIsLoggedIn() unset($this->loggedInUser->sessionId); $this->loggedInUser = FALSE; $this->getSession()->reset(); \Drupal::currentUser()->setAccount(new AnonymousUserSession()); }
Multiple login/logouts in a single test method that alters or adds to user session data or cookie data is otherwise problematic to test.