$reset = TestUtils::setSessionManagerSingleton( null );
$singleton = SessionManager::singleton();
- $this->assertInstanceOf( 'MediaWiki\\Session\\SessionManager', $singleton );
+ $this->assertInstanceOf( SessionManager::class, $singleton );
$this->assertSame( $singleton, SessionManager::singleton() );
}
if ( !PHPSessionHandler::isInstalled() ) {
PHPSessionHandler::install( SessionManager::singleton() );
}
- $rProp = new \ReflectionProperty( 'MediaWiki\\Session\\PHPSessionHandler', 'instance' );
+ $rProp = new \ReflectionProperty( PHPSessionHandler::class, 'instance' );
$rProp->setAccessible( true );
$handler = \TestingAccessWrapper::newFromObject( $rProp->getValue() );
$oldEnable = $handler->enable;
public function testGetSessionForRequest() {
$manager = $this->getManager();
$request = new \FauxRequest();
+ $request->unpersist1 = false;
+ $request->unpersist2 = false;
$id1 = '';
$id2 = '';
$providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
->setMethods(
- [ 'provideSessionInfo', 'newSessionInfo', '__toString', 'describe' ]
+ [ 'provideSessionInfo', 'newSessionInfo', '__toString', 'describe', 'unpersistSession' ]
);
$provider1 = $providerBuilder->getMock();
->will( $this->returnValue( 'Provider1' ) );
$provider1->expects( $this->any() )->method( 'describe' )
->will( $this->returnValue( '#1 sessions' ) );
+ $provider1->expects( $this->any() )->method( 'unpersistSession' )
+ ->will( $this->returnCallback( function ( $request ) {
+ $request->unpersist1 = true;
+ } ) );
$provider2 = $providerBuilder->getMock();
$provider2->expects( $this->any() )->method( 'provideSessionInfo' )
->will( $this->returnValue( 'Provider2' ) );
$provider2->expects( $this->any() )->method( 'describe' )
->will( $this->returnValue( '#2 sessions' ) );
+ $provider2->expects( $this->any() )->method( 'unpersistSession' )
+ ->will( $this->returnCallback( function ( $request ) {
+ $request->unpersist2 = true;
+ } ) );
$this->config->set( 'SessionProviders', [
$this->objectCacheDef( $provider1 ),
$request->info1 = null;
$request->info2 = null;
$session = $manager->getSessionForRequest( $request );
- $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
+ $this->assertInstanceOf( Session::class, $session );
$this->assertSame( $idEmpty, $session->getId() );
+ $this->assertFalse( $request->unpersist1 );
+ $this->assertFalse( $request->unpersist2 );
// Both providers return info, picks best one
$request->info1 = new SessionInfo( SessionInfo::MIN_PRIORITY + 1, [
'idIsSafe' => true,
] );
$session = $manager->getSessionForRequest( $request );
- $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
+ $this->assertInstanceOf( Session::class, $session );
$this->assertSame( $id2, $session->getId() );
+ $this->assertFalse( $request->unpersist1 );
+ $this->assertFalse( $request->unpersist2 );
$request->info1 = new SessionInfo( SessionInfo::MIN_PRIORITY + 2, [
'provider' => $provider1,
'idIsSafe' => true,
] );
$session = $manager->getSessionForRequest( $request );
- $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
+ $this->assertInstanceOf( Session::class, $session );
$this->assertSame( $id1, $session->getId() );
+ $this->assertFalse( $request->unpersist1 );
+ $this->assertFalse( $request->unpersist2 );
// Tied priorities
$request->info1 = new SessionInfo( SessionInfo::MAX_PRIORITY, [
try {
$manager->getSessionForRequest( $request );
$this->fail( 'Expcected exception not thrown' );
- } catch ( \OverFlowException $ex ) {
+ } catch ( \OverflowException $ex ) {
$this->assertStringStartsWith(
'Multiple sessions for this request tied for top priority: ',
$ex->getMessage()
$this->assertContains( $request->info1, $ex->sessionInfos );
$this->assertContains( $request->info2, $ex->sessionInfos );
}
+ $this->assertFalse( $request->unpersist1 );
+ $this->assertFalse( $request->unpersist2 );
// Bad provider
$request->info1 = new SessionInfo( SessionInfo::MAX_PRIORITY, [
$ex->getMessage()
);
}
+ $this->assertFalse( $request->unpersist1 );
+ $this->assertFalse( $request->unpersist2 );
// Unusable session info
$this->logger->setCollect( true );
'idIsSafe' => true,
] );
$session = $manager->getSessionForRequest( $request );
- $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
+ $this->assertInstanceOf( Session::class, $session );
$this->assertSame( $id2, $session->getId() );
$this->logger->setCollect( false );
+ $this->assertTrue( $request->unpersist1 );
+ $this->assertFalse( $request->unpersist2 );
+ $request->unpersist1 = false;
+
+ $this->logger->setCollect( true );
+ $request->info1 = new SessionInfo( SessionInfo::MAX_PRIORITY, [
+ 'provider' => $provider1,
+ 'id' => ( $id1 = $manager->generateSessionId() ),
+ 'persisted' => true,
+ 'idIsSafe' => true,
+ ] );
+ $request->info2 = new SessionInfo( SessionInfo::MAX_PRIORITY, [
+ 'provider' => $provider2,
+ 'id' => ( $id2 = $manager->generateSessionId() ),
+ 'persisted' => true,
+ 'userInfo' => UserInfo::newFromName( 'UTSysop', false ),
+ 'idIsSafe' => true,
+ ] );
+ $session = $manager->getSessionForRequest( $request );
+ $this->assertInstanceOf( Session::class, $session );
+ $this->assertSame( $id1, $session->getId() );
+ $this->logger->setCollect( false );
+ $this->assertFalse( $request->unpersist1 );
+ $this->assertTrue( $request->unpersist2 );
+ $request->unpersist2 = false;
// Unpersisted session ID
$request->info1 = new SessionInfo( SessionInfo::MAX_PRIORITY, [
] );
$request->info2 = null;
$session = $manager->getSessionForRequest( $request );
- $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
+ $this->assertInstanceOf( Session::class, $session );
$this->assertSame( $id1, $session->getId() );
+ $this->assertTrue( $request->unpersist1 ); // The saving of the session does it
+ $this->assertFalse( $request->unpersist2 );
$session->persist();
$this->assertTrue( $session->isPersistent(), 'sanity check' );
}
// Unknown session ID
$id = $manager->generateSessionId();
$session = $manager->getSessionById( $id, true );
- $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
+ $this->assertInstanceOf( Session::class, $session );
$this->assertSame( $id, $session->getId() );
$id = $manager->generateSessionId();
// Known session ID
$this->store->setSession( $id, [] );
$session = $manager->getSessionById( $id, false );
- $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
+ $this->assertInstanceOf( Session::class, $session );
$this->assertSame( $id, $session->getId() );
+
+ // Store isn't checked if the session is already loaded
+ $this->store->setSession( $id, [ 'metadata' => [
+ 'userId' => User::idFromName( 'UTSysop' ),
+ 'userToken' => 'bad',
+ ] ] );
+ $session2 = $manager->getSessionById( $id, false );
+ $this->assertInstanceOf( Session::class, $session2 );
+ $this->assertSame( $id, $session2->getId() );
+ unset( $session, $session2 );
+ $this->logger->setCollect( true );
+ $this->assertNull( $manager->getSessionById( $id, true ) );
+ $this->logger->setCollect( false );
+
+ // Failure to create an empty session
+ $manager = $this->getManager();
+ $provider = $this->getMockBuilder( 'DummySessionProvider' )
+ ->setMethods( [ 'provideSessionInfo', 'newSessionInfo', '__toString' ] )
+ ->getMock();
+ $provider->expects( $this->any() )->method( 'provideSessionInfo' )
+ ->will( $this->returnValue( null ) );
+ $provider->expects( $this->any() )->method( 'newSessionInfo' )
+ ->will( $this->returnValue( null ) );
+ $provider->expects( $this->any() )->method( '__toString' )
+ ->will( $this->returnValue( 'MockProvider' ) );
+ $this->config->set( 'SessionProviders', [
+ $this->objectCacheDef( $provider ),
+ ] );
+ $this->logger->setCollect( true );
+ $this->assertNull( $manager->getSessionById( $id, true ) );
+ $this->logger->setCollect( false );
+ $this->assertSame( [
+ [ LogLevel::ERROR, 'Failed to create empty session: {exception}' ]
+ ], $this->logger->getBuffer() );
}
public function testGetEmptySession() {
] );
$info2 = null;
$session = $manager->getEmptySession();
- $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
+ $this->assertInstanceOf( Session::class, $session );
$this->assertSame( 'empty---------------------------', $session->getId() );
// Info, explicitly
] );
$info2 = null;
$session = $pmanager->getEmptySessionInternal( null, $expectId );
- $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
+ $this->assertInstanceOf( Session::class, $session );
$this->assertSame( $expectId, $session->getId() );
// Wrong ID
'idIsSafe' => true,
] );
$session = $manager->getEmptySession();
- $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
+ $this->assertInstanceOf( Session::class, $session );
$this->assertSame( 'empty1--------------------------', $session->getId() );
$expectId = null;
'idIsSafe' => true,
] );
$session = $manager->getEmptySession();
- $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
+ $this->assertInstanceOf( Session::class, $session );
$this->assertSame( 'empty2--------------------------', $session->getId() );
// Tied priorities throw an exception
\ObjectCache::$instances[__METHOD__] = new TestBagOStuff();
$this->setMwGlobals( [ 'wgMainCacheType' => __METHOD__ ] );
- $this->setMWGlobals( [
+ $this->setMwGlobals( [
'wgAuth' => new AuthPlugin,
] );
'provider' => 'Mock',
];
- $builder = $this->getMockBuilder( 'MediaWiki\\Session\\SessionProvider' )
+ $builder = $this->getMockBuilder( SessionProvider::class )
->setMethods( [ '__toString', 'mergeMetadata', 'refreshSessionInfo' ] );
$provider = $builder->getMockForAbstractClass();
] );
$this->assertFalse( $info->isIdSafe(), 'sanity check' );
$this->assertTrue( $loadSessionInfoFromStore( $info ) );
- $this->assertInstanceOf( 'MediaWiki\\Session\\UserInfo', $info->getUserInfo() );
+ $this->assertInstanceOf( UserInfo::class, $info->getUserInfo() );
$this->assertTrue( $info->getUserInfo()->isVerified() );
$this->assertTrue( $info->getUserInfo()->isAnon() );
$this->assertFalse( $info->isIdSafe() );