I am trying to run some functional tests in DDEV that check functions of protected routes. I can log in the first couple of users fine, but after that drupaLogin() fails to generate a logged in session.
Example:
$this->drupalGet(Url::fromRoute('mymodule.secondary_login'));
$this->submitForm([
'name' => '[email protected]',
'pass' => 'test',
], 'Log in');
// Try to go to /user.
$this->drupalGet('/user');
$this->assertSession()->addressEquals('member');
// More navigation and assertion checks here
// Make the cookie stale and test the redirect.
$this->getSession()->setCookie('session', NULL);
$this->drupalGet('/');
$this->assertSession()->addressEquals('member/login');
$this->drupalGet(Url::fromRoute('mymodule.secondary_logout'));
$this->assertFalse($this->loggedInUser);
// User is anon and session should be clear at this point, but drupalLogin fails
$authenticated_user = $this->drupalCreateUser([], 'Authenticated User', FALSE);
$this->drupalLogin($authenticated_user);
$this->drupalGet('/member');
$this->drupalGet(Url::fromRoute('mos_ticketure.logout'));
$this->assertSession()->statusCodeEquals(403);
Url::fromRoute('mymodule.secondary_login')
is a login form that handles setting a cookie and registering a new user with ExternalAuth. secondary_logout handles destroying the user session (copied from Drupal core logout()) as well as cleaning up custom cookies.
By the time it gets down to log in the Authenticated User, drupalLogin fails an assert check with:
User <em class="placeholder">Authenticated User</em> successfully logged in.
Failed asserting that false is true.
If I move it to a brand new test method, this simple test works fine:
$this->drupalGet(Url::fromRoute('mymodule.login'));
$this->submitForm([
'name' => '[email protected]',
'pass' => 'test',
], 'Log in');
$this->drupalGet(Url::fromRoute('mymodule.logout'));
$authenticated_user = $this->drupalCreateUser([], 'Authenticated User', FALSE);
$this->drupalLogin($authenticated_user);
$this->drupalGet('/member');
$this->drupalGet(Url::fromRoute('mymodule.logout'));
$this->assertSession()->statusCodeEquals(403);
If I click through these scenarios manually, the custom functionality does indeed appear to do what it should. Is this some sort of weird behavior with PHPUnit Functional tests with Mink unable to properly track cookies and session data in larger tests?