class CookieSessionProviderTest extends MediaWikiTestCase {
private function getConfig() {
- global $wgCookieExpiration;
return new \HashConfig( [
'CookiePrefix' => 'CookiePrefix',
'CookiePath' => 'CookiePath',
'CookieSecure' => true,
'CookieHttpOnly' => true,
'SessionName' => false,
- 'ExtendedLoginCookies' => [ 'UserID', 'Token' ],
- 'ExtendedLoginCookieExpiration' => $wgCookieExpiration * 2,
+ 'CookieExpiration' => 100,
+ 'ExtendedLoginCookieExpiration' => 200,
] );
}
public function testBasics() {
$provider = new CookieSessionProvider( [ 'priority' => 10 ] );
- $this->assertTrue( $provider->persistsSessionID() );
+ $this->assertTrue( $provider->persistsSessionId() );
$this->assertTrue( $provider->canChangeUser() );
+ $extendedCookies = [ 'UserID', 'UserName', 'Token' ];
+
+ $this->assertEquals(
+ $extendedCookies,
+ \TestingAccessWrapper::newFromObject( $provider )->getExtendedLoginCookies(),
+ 'List of extended cookies (subclasses can add values, but we\'re calling the core one here)'
+ );
+
$msg = $provider->whyNoSession();
$this->assertInstanceOf( 'Message', $msg );
$this->assertSame( 'sessionprovider-nocookies', $msg->getKey() );
$provider->setConfig( $this->getConfig() );
$provider->setManager( new SessionManager() );
- $user = User::newFromName( 'UTSysop' );
+ $user = static::getTestSysop()->getUser();
$id = $user->getId();
$name = $user->getName();
$token = $user->getToken( true );
}
public function testPersistSession() {
- $this->setMwGlobals( [ 'wgCookieExpiration' => 100 ] );
-
$provider = new CookieSessionProvider( [
'priority' => 1,
'sessionName' => 'MySessionName',
$sessionId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
$store = new TestBagOStuff();
- $user = User::newFromName( 'UTSysop' );
+ $user = static::getTestSysop()->getUser();
$anon = new User;
$backend = new SessionBackend(
*/
public function testCookieData( $secure, $remember ) {
$this->setMwGlobals( [
- 'wgCookieExpiration' => 100,
'wgSecureLogin' => false,
] );
$provider->setManager( SessionManager::singleton() );
$sessionId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
- $user = User::newFromName( 'UTSysop' );
+ $user = static::getTestSysop()->getUser();
$this->assertFalse( $user->requiresHTTPS(), 'sanity check' );
$backend = new SessionBackend(
'httpOnly' => $config->get( 'CookieHttpOnly' ),
'raw' => false,
];
+
+ $normalExpiry = $config->get( 'CookieExpiration' );
$extendedExpiry = $config->get( 'ExtendedLoginCookieExpiration' );
$extendedExpiry = (int)( $extendedExpiry === null ? 0 : $extendedExpiry );
- $this->assertEquals( [ 'UserID', 'Token' ], $config->get( 'ExtendedLoginCookies' ),
- 'sanity check' );
$expect = [
'MySessionName' => [
'value' => (string)$sessionId,
] + $defaults,
'xUserID' => [
'value' => (string)$user->getId(),
- 'expire' => $extendedExpiry,
+ 'expire' => $remember ? $extendedExpiry : $normalExpiry,
] + $defaults,
'xUserName' => [
'value' => $user->getName(),
+ 'expire' => $remember ? $extendedExpiry : $normalExpiry
] + $defaults,
'xToken' => [
'value' => $remember ? $user->getToken() : '',
$sessionId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
$store = new TestBagOStuff();
- $user = User::newFromName( 'UTSysop' );
+ $user = static::getTestSysop()->getUser();
$anon = new User;
$backend = new SessionBackend(
$this->assertNull( $provider->getCookie( $request, 'Baz', 'x' ) );
}
+ public function testGetRememberUserDuration() {
+ $config = $this->getConfig();
+ $provider = new CookieSessionProvider( [ 'priority' => 10 ] );
+ $provider->setLogger( new \Psr\Log\NullLogger() );
+ $provider->setConfig( $config );
+ $provider->setManager( SessionManager::singleton() );
+
+ $this->assertSame( 200, $provider->getRememberUserDuration() );
+
+ $config->set( 'ExtendedLoginCookieExpiration', null );
+
+ $this->assertSame( 100, $provider->getRememberUserDuration() );
+
+ $config->set( 'ExtendedLoginCookieExpiration', 0 );
+
+ $this->assertSame( null, $provider->getRememberUserDuration() );
+ }
+
+ public function testGetLoginCookieExpiration() {
+ $config = $this->getConfig();
+ $provider = \TestingAccessWrapper::newFromObject( new CookieSessionProvider( [
+ 'priority' => 10
+ ] ) );
+ $provider->setLogger( new \Psr\Log\NullLogger() );
+ $provider->setConfig( $config );
+ $provider->setManager( SessionManager::singleton() );
+
+ // First cookie is an extended cookie, remember me true
+ $this->assertSame( 200, $provider->getLoginCookieExpiration( 'Token', true ) );
+ $this->assertSame( 100, $provider->getLoginCookieExpiration( 'User', true ) );
+
+ // First cookie is an extended cookie, remember me false
+ $this->assertSame( 100, $provider->getLoginCookieExpiration( 'UserID', false ) );
+ $this->assertSame( 100, $provider->getLoginCookieExpiration( 'User', false ) );
+
+ $config->set( 'ExtendedLoginCookieExpiration', null );
+
+ $this->assertSame( 100, $provider->getLoginCookieExpiration( 'Token', true ) );
+ $this->assertSame( 100, $provider->getLoginCookieExpiration( 'User', true ) );
+
+ $this->assertSame( 100, $provider->getLoginCookieExpiration( 'Token', false ) );
+ $this->assertSame( 100, $provider->getLoginCookieExpiration( 'User', false ) );
+ }
}