Remember me How do you go about testing the Remember me functionality?
L5.1, PHPunit (I'm using the Sentinel package, but converting the code from standard Laravel won't be an issue)
The way most logins do the "Remember Me" feature is to set a cookie with a time set far in the future. In your acceptance testing you could check that the cookie header is set a long way in the future. Ultimately though if your using a package it should already be tested and all your doing by testing this functionality is double testing this feature.
How do you go about testing the Remember me functionality?
You just need to test the boolean value of the form checkbox is passed to the Auth::attempt method.
Then laravel is heavily tested, no need to re-test its features.
In my case I was able to test if log in creates a 'remember_web_*' cookie with the following code:
/** @test */
public function user_can_choose_to_keep_session_open_or_not() {
$user = User::factory()
->create(['password' => Hash::make('password')]);
$cookyJar1 = $this->post('/login', [
'username' => $user->username,
'password' => 'password',
'remember_me' => '0'
])->headers->getCookies();
$this->assertFalse($this->hasRememberWebCooky($cookyJar1));
auth()->logout();
$cookyJar2 = $this->post('/login', [
'username' => $user->username,
'password' => 'password',
'remember_me' => '1'
])->headers->getCookies();
$this->assertTrue($this->hasRememberWebCooky($cookyJar2));
}
protected function hasRememberWebCooky(array $cookyJar): bool {
foreach ($cookyJar as $cooky) {
if (Str::startsWith($cooky->getName(), 'remember_web_')) {
return true;
}
}
return false;
}
Please sign in or create an account to participate in this conversation.