- 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.
- 🇫🇷France pbonnefoi
Ok good to know, thank you very much for the insights !
- 🇦🇺Australia fenstrat Australia
Just noting here that I saw these same odd failures, especially when running multiple drupalLogin() calls:
User <em class="placeholder">NAME</em> successfully logged in. Failed asserting that false is true.
And that calling
$this->getSession()->reset()
didn't solve it.Instead it was related to dumping the container, and the container differing while under test. In my case this was using Drupal Test Traits, see
🐛 DTT can use different cached container than running site causing issues with testing Fixed .