wiki.techinc.nl/tests/phpunit/includes/auth/RememberMeAuthenticationRequestTest.php
Cindy Cicalese 0b17d02d92 Make RememberMe authentication behavior configurable.
RememberMeAuthentication request shows a "remember me" checkbox that,
when checked, will extend the amount of time before the authenticated
session expires. It was previously not possible to extend the session
when the login form is skipped, which happens when there are no
non-skippable fields on the form. This patch introduces a configuration
variable, $wgRememberMe. Valid values are:

- CHOOSE_REMEMBER: the user may be able to choose whether to be
remembered or not (depends upon whether login form is skipped)

- FORCE_CHOOSE_REMEMBER: the user will be able to choose whether to be
remembered or not (forces login form not to be skipped)

- ALWAYS_REMEMBER: the authenticated session will always be extended

- NEVER_REMEMBER: the authenticated session will never be extended

Default behavior is identical to what it was before adding this
functionality.

Bug: T265263
Change-Id: I779aae3c1b96b380b50092245f616219088b038d
2021-03-14 12:31:41 -04:00

112 lines
3.4 KiB
PHP

<?php
namespace MediaWiki\Auth;
use Wikimedia\TestingAccessWrapper;
/**
* @group AuthManager
* @covers \MediaWiki\Auth\RememberMeAuthenticationRequest
*/
class RememberMeAuthenticationRequestTest extends AuthenticationRequestTestCase {
public static function provideGetFieldInfo() {
return [
[ [ 1 ] ],
[ [ null ] ],
];
}
public function testGetFieldInfo_2() {
$req = new RememberMeAuthenticationRequest();
$reqWrapper = TestingAccessWrapper::newFromObject( $req );
$reqWrapper->expiration = 30 * 24 * 3600;
$this->assertNotEmpty( $req->getFieldInfo() );
$reqWrapper->expiration = null;
$this->assertSame( [], $req->getFieldInfo() );
}
public function testNoChoice() {
$req = new RememberMeAuthenticationRequest(
RememberMeAuthenticationRequest::ALWAYS_REMEMBER
);
$reqWrapper = TestingAccessWrapper::newFromObject( $req );
$this->assertSame( [], $req->getFieldInfo() );
$this->assertNotNull( $reqWrapper->expiration );
$req = new RememberMeAuthenticationRequest(
RememberMeAuthenticationRequest::NEVER_REMEMBER
);
$reqWrapper = TestingAccessWrapper::newFromObject( $req );
$this->assertSame( [], $req->getFieldInfo() );
$this->assertNull( $reqWrapper->expiration );
}
public function testInvalid() {
$this->expectException( '\UnexpectedValueException' );
new RememberMeAuthenticationRequest( 'invalid value' );
}
protected function getInstance( array $args = [] ) {
if ( isset( $args[1] ) ) {
$req = new RememberMeAuthenticationRequest( $args[1] );
} else {
$req = new RememberMeAuthenticationRequest();
}
$reqWrapper = TestingAccessWrapper::newFromObject( $req );
$reqWrapper->expiration = $args[0];
return $req;
}
public function provideLoadFromSubmission() {
return [
'Empty request' => [
[ 30 * 24 * 3600 ],
[],
[ 'expiration' => 30 * 24 * 3600, 'rememberMe' => false ]
],
'RememberMe present' => [
[ 30 * 24 * 3600 ],
[ 'rememberMe' => '' ],
[ 'expiration' => 30 * 24 * 3600, 'rememberMe' => true ]
],
'RememberMe present but session provider cannot remember' => [
[ null ],
[ 'rememberMe' => '' ],
false
],
'Empty request (CHOOSE_REMEMBER)' => [
[ 30 * 24 * 3600, RememberMeAuthenticationRequest::CHOOSE_REMEMBER ],
[],
[ 'expiration' => 30 * 24 * 3600, 'rememberMe' => false ]
],
'RememberMe present (CHOOSE_REMEMBER)' => [
[ 30 * 24 * 3600, RememberMeAuthenticationRequest::CHOOSE_REMEMBER ],
[ 'rememberMe' => '' ],
[ 'expiration' => 30 * 24 * 3600, 'rememberMe' => true ]
],
'RememberMe present but session provider cannot remember (CHOOSE_REMEMBER)' => [
[ null, RememberMeAuthenticationRequest::CHOOSE_REMEMBER ],
[ 'rememberMe' => '' ],
false
],
'Empty request (FORCE_CHOOSE_REMEMBER)' => [
[ 30 * 24 * 3600, RememberMeAuthenticationRequest::FORCE_CHOOSE_REMEMBER ],
[],
[ 'expiration' => 30 * 24 * 3600, 'rememberMe' => false, 'skippable' => false ]
],
'RememberMe present (FORCE_CHOOSE_REMEMBER)' => [
[ 30 * 24 * 3600, RememberMeAuthenticationRequest::FORCE_CHOOSE_REMEMBER ],
[ 'rememberMe' => '' ],
[ 'expiration' => 30 * 24 * 3600, 'rememberMe' => true, 'skippable' => false ]
],
'RememberMe present but session provider cannot remember (FORCE_CHOOSE_REMEMBER)' => [
[ null, RememberMeAuthenticationRequest::FORCE_CHOOSE_REMEMBER ],
[ 'rememberMe' => '', 'skippable' => false ],
false
],
];
}
}