3 namespace MediaWiki\Session
;
12 * @covers MediaWiki\Session\SessionManager
14 class SessionManagerTest
extends MediaWikiTestCase
{
16 protected $config, $logger, $store;
18 protected function getManager() {
19 \ObjectCache
::$instances['testSessionStore'] = new TestBagOStuff();
20 $this->config
= new \
HashConfig( [
21 'LanguageCode' => 'en',
22 'SessionCacheType' => 'testSessionStore',
23 'ObjectCacheSessionExpiry' => 100,
24 'SessionProviders' => [
25 [ 'class' => 'DummySessionProvider' ],
28 $this->logger
= new \
TestLogger( false, function ( $m ) {
29 return substr( $m, 0, 15 ) === 'SessionBackend ' ?
null : $m;
31 $this->store
= new TestBagOStuff();
33 return new SessionManager( [
34 'config' => $this->config
,
35 'logger' => $this->logger
,
36 'store' => $this->store
,
40 protected function objectCacheDef( $object ) {
41 return [ 'factory' => function () use ( $object ) {
46 public function testSingleton() {
47 $reset = TestUtils
::setSessionManagerSingleton( null );
49 $singleton = SessionManager
::singleton();
50 $this->assertInstanceOf( SessionManager
::class, $singleton );
51 $this->assertSame( $singleton, SessionManager
::singleton() );
54 public function testGetGlobalSession() {
55 $context = \RequestContext
::getMain();
57 if ( !PHPSessionHandler
::isInstalled() ) {
58 PHPSessionHandler
::install( SessionManager
::singleton() );
60 $rProp = new \
ReflectionProperty( PHPSessionHandler
::class, 'instance' );
61 $rProp->setAccessible( true );
62 $handler = \TestingAccessWrapper
::newFromObject( $rProp->getValue() );
63 $oldEnable = $handler->enable
;
64 $reset[] = new \Wikimedia\
ScopedCallback( function () use ( $handler, $oldEnable ) {
65 if ( $handler->enable
) {
66 session_write_close();
68 $handler->enable
= $oldEnable;
70 $reset[] = TestUtils
::setSessionManagerSingleton( $this->getManager() );
72 $handler->enable
= true;
73 $request = new \
FauxRequest();
74 $context->setRequest( $request );
75 $id = $request->getSession()->getId();
78 $session = SessionManager
::getGlobalSession();
79 $this->assertSame( $id, $session->getId() );
81 session_id( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
82 $session = SessionManager
::getGlobalSession();
83 $this->assertSame( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', $session->getId() );
84 $this->assertSame( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', $request->getSession()->getId() );
86 session_write_close();
87 $handler->enable
= false;
88 $request = new \
FauxRequest();
89 $context->setRequest( $request );
90 $id = $request->getSession()->getId();
93 $session = SessionManager
::getGlobalSession();
94 $this->assertSame( $id, $session->getId() );
96 session_id( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
97 $session = SessionManager
::getGlobalSession();
98 $this->assertSame( $id, $session->getId() );
99 $this->assertSame( $id, $request->getSession()->getId() );
102 public function testConstructor() {
103 $manager = \TestingAccessWrapper
::newFromObject( $this->getManager() );
104 $this->assertSame( $this->config
, $manager->config
);
105 $this->assertSame( $this->logger
, $manager->logger
);
106 $this->assertSame( $this->store
, $manager->store
);
108 $manager = \TestingAccessWrapper
::newFromObject( new SessionManager() );
109 $this->assertSame( \RequestContext
::getMain()->getConfig(), $manager->config
);
111 $manager = \TestingAccessWrapper
::newFromObject( new SessionManager( [
112 'config' => $this->config
,
114 $this->assertSame( \ObjectCache
::$instances['testSessionStore'], $manager->store
);
117 'config' => '$options[\'config\'] must be an instance of Config',
118 'logger' => '$options[\'logger\'] must be an instance of LoggerInterface',
119 'store' => '$options[\'store\'] must be an instance of BagOStuff',
120 ] as $key => $error ) {
122 new SessionManager( [ $key => new \stdClass
] );
123 $this->fail( 'Expected exception not thrown' );
124 } catch ( \InvalidArgumentException
$ex ) {
125 $this->assertSame( $error, $ex->getMessage() );
130 public function testGetSessionForRequest() {
131 $manager = $this->getManager();
132 $request = new \
FauxRequest();
133 $request->unpersist1
= false;
134 $request->unpersist2
= false;
138 $idEmpty = 'empty-session-------------------';
140 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
142 [ 'provideSessionInfo', 'newSessionInfo', '__toString', 'describe', 'unpersistSession' ]
145 $provider1 = $providerBuilder->getMock();
146 $provider1->expects( $this->any() )->method( 'provideSessionInfo' )
147 ->with( $this->identicalTo( $request ) )
148 ->will( $this->returnCallback( function ( $request ) {
149 return $request->info1
;
151 $provider1->expects( $this->any() )->method( 'newSessionInfo' )
152 ->will( $this->returnCallback( function () use ( $idEmpty, $provider1 ) {
153 return new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
154 'provider' => $provider1,
160 $provider1->expects( $this->any() )->method( '__toString' )
161 ->will( $this->returnValue( 'Provider1' ) );
162 $provider1->expects( $this->any() )->method( 'describe' )
163 ->will( $this->returnValue( '#1 sessions' ) );
164 $provider1->expects( $this->any() )->method( 'unpersistSession' )
165 ->will( $this->returnCallback( function ( $request ) {
166 $request->unpersist1
= true;
169 $provider2 = $providerBuilder->getMock();
170 $provider2->expects( $this->any() )->method( 'provideSessionInfo' )
171 ->with( $this->identicalTo( $request ) )
172 ->will( $this->returnCallback( function ( $request ) {
173 return $request->info2
;
175 $provider2->expects( $this->any() )->method( '__toString' )
176 ->will( $this->returnValue( 'Provider2' ) );
177 $provider2->expects( $this->any() )->method( 'describe' )
178 ->will( $this->returnValue( '#2 sessions' ) );
179 $provider2->expects( $this->any() )->method( 'unpersistSession' )
180 ->will( $this->returnCallback( function ( $request ) {
181 $request->unpersist2
= true;
184 $this->config
->set( 'SessionProviders', [
185 $this->objectCacheDef( $provider1 ),
186 $this->objectCacheDef( $provider2 ),
189 // No provider returns info
190 $request->info1
= null;
191 $request->info2
= null;
192 $session = $manager->getSessionForRequest( $request );
193 $this->assertInstanceOf( Session
::class, $session );
194 $this->assertSame( $idEmpty, $session->getId() );
195 $this->assertFalse( $request->unpersist1
);
196 $this->assertFalse( $request->unpersist2
);
198 // Both providers return info, picks best one
199 $request->info1
= new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, [
200 'provider' => $provider1,
201 'id' => ( $id1 = $manager->generateSessionId() ),
205 $request->info2
= new SessionInfo( SessionInfo
::MIN_PRIORITY +
2, [
206 'provider' => $provider2,
207 'id' => ( $id2 = $manager->generateSessionId() ),
211 $session = $manager->getSessionForRequest( $request );
212 $this->assertInstanceOf( Session
::class, $session );
213 $this->assertSame( $id2, $session->getId() );
214 $this->assertFalse( $request->unpersist1
);
215 $this->assertFalse( $request->unpersist2
);
217 $request->info1
= new SessionInfo( SessionInfo
::MIN_PRIORITY +
2, [
218 'provider' => $provider1,
219 'id' => ( $id1 = $manager->generateSessionId() ),
223 $request->info2
= new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, [
224 'provider' => $provider2,
225 'id' => ( $id2 = $manager->generateSessionId() ),
229 $session = $manager->getSessionForRequest( $request );
230 $this->assertInstanceOf( Session
::class, $session );
231 $this->assertSame( $id1, $session->getId() );
232 $this->assertFalse( $request->unpersist1
);
233 $this->assertFalse( $request->unpersist2
);
236 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, [
237 'provider' => $provider1,
238 'id' => ( $id1 = $manager->generateSessionId() ),
240 'userInfo' => UserInfo
::newAnonymous(),
243 $request->info2
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, [
244 'provider' => $provider2,
245 'id' => ( $id2 = $manager->generateSessionId() ),
247 'userInfo' => UserInfo
::newAnonymous(),
251 $manager->getSessionForRequest( $request );
252 $this->fail( 'Expcected exception not thrown' );
253 } catch ( \OverflowException
$ex ) {
254 $this->assertStringStartsWith(
255 'Multiple sessions for this request tied for top priority: ',
258 $this->assertCount( 2, $ex->sessionInfos
);
259 $this->assertContains( $request->info1
, $ex->sessionInfos
);
260 $this->assertContains( $request->info2
, $ex->sessionInfos
);
262 $this->assertFalse( $request->unpersist1
);
263 $this->assertFalse( $request->unpersist2
);
266 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, [
267 'provider' => $provider2,
268 'id' => ( $id1 = $manager->generateSessionId() ),
272 $request->info2
= null;
274 $manager->getSessionForRequest( $request );
275 $this->fail( 'Expcected exception not thrown' );
276 } catch ( \UnexpectedValueException
$ex ) {
278 'Provider1 returned session info for a different provider: ' . $request->info1
,
282 $this->assertFalse( $request->unpersist1
);
283 $this->assertFalse( $request->unpersist2
);
285 // Unusable session info
286 $this->logger
->setCollect( true );
287 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, [
288 'provider' => $provider1,
289 'id' => ( $id1 = $manager->generateSessionId() ),
291 'userInfo' => UserInfo
::newFromName( 'UTSysop', false ),
294 $request->info2
= new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
295 'provider' => $provider2,
296 'id' => ( $id2 = $manager->generateSessionId() ),
300 $session = $manager->getSessionForRequest( $request );
301 $this->assertInstanceOf( Session
::class, $session );
302 $this->assertSame( $id2, $session->getId() );
303 $this->logger
->setCollect( false );
304 $this->assertTrue( $request->unpersist1
);
305 $this->assertFalse( $request->unpersist2
);
306 $request->unpersist1
= false;
308 $this->logger
->setCollect( true );
309 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, [
310 'provider' => $provider1,
311 'id' => ( $id1 = $manager->generateSessionId() ),
315 $request->info2
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, [
316 'provider' => $provider2,
317 'id' => ( $id2 = $manager->generateSessionId() ),
319 'userInfo' => UserInfo
::newFromName( 'UTSysop', false ),
322 $session = $manager->getSessionForRequest( $request );
323 $this->assertInstanceOf( Session
::class, $session );
324 $this->assertSame( $id1, $session->getId() );
325 $this->logger
->setCollect( false );
326 $this->assertFalse( $request->unpersist1
);
327 $this->assertTrue( $request->unpersist2
);
328 $request->unpersist2
= false;
330 // Unpersisted session ID
331 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, [
332 'provider' => $provider1,
333 'id' => ( $id1 = $manager->generateSessionId() ),
334 'persisted' => false,
335 'userInfo' => UserInfo
::newFromName( 'UTSysop', true ),
338 $request->info2
= null;
339 $session = $manager->getSessionForRequest( $request );
340 $this->assertInstanceOf( Session
::class, $session );
341 $this->assertSame( $id1, $session->getId() );
342 $this->assertTrue( $request->unpersist1
); // The saving of the session does it
343 $this->assertFalse( $request->unpersist2
);
345 $this->assertTrue( $session->isPersistent(), 'sanity check' );
348 public function testGetSessionById() {
349 $manager = $this->getManager();
351 $manager->getSessionById( 'bad' );
352 $this->fail( 'Expected exception not thrown' );
353 } catch ( \InvalidArgumentException
$ex ) {
354 $this->assertSame( 'Invalid session ID', $ex->getMessage() );
357 // Unknown session ID
358 $id = $manager->generateSessionId();
359 $session = $manager->getSessionById( $id, true );
360 $this->assertInstanceOf( Session
::class, $session );
361 $this->assertSame( $id, $session->getId() );
363 $id = $manager->generateSessionId();
364 $this->assertNull( $manager->getSessionById( $id, false ) );
366 // Known but unloadable session ID
367 $this->logger
->setCollect( true );
368 $id = $manager->generateSessionId();
369 $this->store
->setSession( $id, [ 'metadata' => [
370 'userId' => User
::idFromName( 'UTSysop' ),
371 'userToken' => 'bad',
374 $this->assertNull( $manager->getSessionById( $id, true ) );
375 $this->assertNull( $manager->getSessionById( $id, false ) );
376 $this->logger
->setCollect( false );
379 $this->store
->setSession( $id, [] );
380 $session = $manager->getSessionById( $id, false );
381 $this->assertInstanceOf( Session
::class, $session );
382 $this->assertSame( $id, $session->getId() );
384 // Store isn't checked if the session is already loaded
385 $this->store
->setSession( $id, [ 'metadata' => [
386 'userId' => User
::idFromName( 'UTSysop' ),
387 'userToken' => 'bad',
389 $session2 = $manager->getSessionById( $id, false );
390 $this->assertInstanceOf( Session
::class, $session2 );
391 $this->assertSame( $id, $session2->getId() );
392 unset( $session, $session2 );
393 $this->logger
->setCollect( true );
394 $this->assertNull( $manager->getSessionById( $id, true ) );
395 $this->logger
->setCollect( false );
397 // Failure to create an empty session
398 $manager = $this->getManager();
399 $provider = $this->getMockBuilder( 'DummySessionProvider' )
400 ->setMethods( [ 'provideSessionInfo', 'newSessionInfo', '__toString' ] )
402 $provider->expects( $this->any() )->method( 'provideSessionInfo' )
403 ->will( $this->returnValue( null ) );
404 $provider->expects( $this->any() )->method( 'newSessionInfo' )
405 ->will( $this->returnValue( null ) );
406 $provider->expects( $this->any() )->method( '__toString' )
407 ->will( $this->returnValue( 'MockProvider' ) );
408 $this->config
->set( 'SessionProviders', [
409 $this->objectCacheDef( $provider ),
411 $this->logger
->setCollect( true );
412 $this->assertNull( $manager->getSessionById( $id, true ) );
413 $this->logger
->setCollect( false );
415 [ LogLevel
::ERROR
, 'Failed to create empty session: {exception}' ]
416 ], $this->logger
->getBuffer() );
419 public function testGetEmptySession() {
420 $manager = $this->getManager();
421 $pmanager = \TestingAccessWrapper
::newFromObject( $manager );
422 $request = new \
FauxRequest();
424 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
425 ->setMethods( [ 'provideSessionInfo', 'newSessionInfo', '__toString' ] );
431 $provider1 = $providerBuilder->getMock();
432 $provider1->expects( $this->any() )->method( 'provideSessionInfo' )
433 ->will( $this->returnValue( null ) );
434 $provider1->expects( $this->any() )->method( 'newSessionInfo' )
435 ->with( $this->callback( function ( $id ) use ( &$expectId ) {
436 return $id === $expectId;
438 ->will( $this->returnCallback( function () use ( &$info1 ) {
441 $provider1->expects( $this->any() )->method( '__toString' )
442 ->will( $this->returnValue( 'MockProvider1' ) );
444 $provider2 = $providerBuilder->getMock();
445 $provider2->expects( $this->any() )->method( 'provideSessionInfo' )
446 ->will( $this->returnValue( null ) );
447 $provider2->expects( $this->any() )->method( 'newSessionInfo' )
448 ->with( $this->callback( function ( $id ) use ( &$expectId ) {
449 return $id === $expectId;
451 ->will( $this->returnCallback( function () use ( &$info2 ) {
454 $provider1->expects( $this->any() )->method( '__toString' )
455 ->will( $this->returnValue( 'MockProvider2' ) );
457 $this->config
->set( 'SessionProviders', [
458 $this->objectCacheDef( $provider1 ),
459 $this->objectCacheDef( $provider2 ),
467 $manager->getEmptySession();
468 $this->fail( 'Expected exception not thrown' );
469 } catch ( \UnexpectedValueException
$ex ) {
471 'No provider could provide an empty session!',
478 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
479 'provider' => $provider1,
480 'id' => 'empty---------------------------',
485 $session = $manager->getEmptySession();
486 $this->assertInstanceOf( Session
::class, $session );
487 $this->assertSame( 'empty---------------------------', $session->getId() );
490 $expectId = 'expected------------------------';
491 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
492 'provider' => $provider1,
498 $session = $pmanager->getEmptySessionInternal( null, $expectId );
499 $this->assertInstanceOf( Session
::class, $session );
500 $this->assertSame( $expectId, $session->getId() );
503 $expectId = 'expected-----------------------2';
504 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
505 'provider' => $provider1,
506 'id' => "un$expectId",
512 $pmanager->getEmptySessionInternal( null, $expectId );
513 $this->fail( 'Expected exception not thrown' );
514 } catch ( \UnexpectedValueException
$ex ) {
516 'MockProvider1 returned empty session info with a wrong id: ' .
517 "un$expectId != $expectId",
523 $expectId = 'expected-----------------------2';
524 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
525 'provider' => $provider1,
531 $pmanager->getEmptySessionInternal( null, $expectId );
532 $this->fail( 'Expected exception not thrown' );
533 } catch ( \UnexpectedValueException
$ex ) {
535 'MockProvider1 returned empty session info with id flagged unsafe',
542 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
543 'provider' => $provider2,
544 'id' => 'empty---------------------------',
550 $manager->getEmptySession();
551 $this->fail( 'Expected exception not thrown' );
552 } catch ( \UnexpectedValueException
$ex ) {
554 'MockProvider1 returned an empty session info for a different provider: ' . $info1,
559 // Highest priority wins
561 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, [
562 'provider' => $provider1,
563 'id' => 'empty1--------------------------',
567 $info2 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
568 'provider' => $provider2,
569 'id' => 'empty2--------------------------',
573 $session = $manager->getEmptySession();
574 $this->assertInstanceOf( Session
::class, $session );
575 $this->assertSame( 'empty1--------------------------', $session->getId() );
578 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, [
579 'provider' => $provider1,
580 'id' => 'empty1--------------------------',
584 $info2 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
2, [
585 'provider' => $provider2,
586 'id' => 'empty2--------------------------',
590 $session = $manager->getEmptySession();
591 $this->assertInstanceOf( Session
::class, $session );
592 $this->assertSame( 'empty2--------------------------', $session->getId() );
594 // Tied priorities throw an exception
596 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
597 'provider' => $provider1,
598 'id' => 'empty1--------------------------',
600 'userInfo' => UserInfo
::newAnonymous(),
603 $info2 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
604 'provider' => $provider2,
605 'id' => 'empty2--------------------------',
607 'userInfo' => UserInfo
::newAnonymous(),
611 $manager->getEmptySession();
612 $this->fail( 'Expected exception not thrown' );
613 } catch ( \UnexpectedValueException
$ex ) {
614 $this->assertStringStartsWith(
615 'Multiple empty sessions tied for top priority: ',
622 $pmanager->getEmptySessionInternal( null, 'bad' );
623 $this->fail( 'Expected exception not thrown' );
624 } catch ( \InvalidArgumentException
$ex ) {
625 $this->assertSame( 'Invalid session ID', $ex->getMessage() );
628 // Session already exists
629 $expectId = 'expected-----------------------3';
630 $this->store
->setSessionMeta( $expectId, [
631 'provider' => 'MockProvider2',
637 $pmanager->getEmptySessionInternal( null, $expectId );
638 $this->fail( 'Expected exception not thrown' );
639 } catch ( \InvalidArgumentException
$ex ) {
640 $this->assertSame( 'Session ID already exists', $ex->getMessage() );
644 public function testInvalidateSessionsForUser() {
645 $user = User
::newFromName( 'UTSysop' );
646 $manager = $this->getManager();
648 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
649 ->setMethods( [ 'invalidateSessionsForUser', '__toString' ] );
651 $provider1 = $providerBuilder->getMock();
652 $provider1->expects( $this->once() )->method( 'invalidateSessionsForUser' )
653 ->with( $this->identicalTo( $user ) );
654 $provider1->expects( $this->any() )->method( '__toString' )
655 ->will( $this->returnValue( 'MockProvider1' ) );
657 $provider2 = $providerBuilder->getMock();
658 $provider2->expects( $this->once() )->method( 'invalidateSessionsForUser' )
659 ->with( $this->identicalTo( $user ) );
660 $provider2->expects( $this->any() )->method( '__toString' )
661 ->will( $this->returnValue( 'MockProvider2' ) );
663 $this->config
->set( 'SessionProviders', [
664 $this->objectCacheDef( $provider1 ),
665 $this->objectCacheDef( $provider2 ),
668 $oldToken = $user->getToken( true );
669 $manager->invalidateSessionsForUser( $user );
670 $this->assertNotEquals( $oldToken, $user->getToken() );
673 public function testGetVaryHeaders() {
674 $manager = $this->getManager();
676 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
677 ->setMethods( [ 'getVaryHeaders', '__toString' ] );
679 $provider1 = $providerBuilder->getMock();
680 $provider1->expects( $this->once() )->method( 'getVaryHeaders' )
681 ->will( $this->returnValue( [
683 'Bar' => [ 'X', 'Bar1' ],
686 $provider1->expects( $this->any() )->method( '__toString' )
687 ->will( $this->returnValue( 'MockProvider1' ) );
689 $provider2 = $providerBuilder->getMock();
690 $provider2->expects( $this->once() )->method( 'getVaryHeaders' )
691 ->will( $this->returnValue( [
693 'Bar' => [ 'X', 'Bar2' ],
694 'Quux' => [ 'Quux' ],
696 $provider2->expects( $this->any() )->method( '__toString' )
697 ->will( $this->returnValue( 'MockProvider2' ) );
699 $this->config
->set( 'SessionProviders', [
700 $this->objectCacheDef( $provider1 ),
701 $this->objectCacheDef( $provider2 ),
706 'Bar' => [ 'X', 'Bar1', 3 => 'Bar2' ],
707 'Quux' => [ 'Quux' ],
711 $this->assertEquals( $expect, $manager->getVaryHeaders() );
713 // Again, to ensure it's cached
714 $this->assertEquals( $expect, $manager->getVaryHeaders() );
717 public function testGetVaryCookies() {
718 $manager = $this->getManager();
720 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
721 ->setMethods( [ 'getVaryCookies', '__toString' ] );
723 $provider1 = $providerBuilder->getMock();
724 $provider1->expects( $this->once() )->method( 'getVaryCookies' )
725 ->will( $this->returnValue( [ 'Foo', 'Bar' ] ) );
726 $provider1->expects( $this->any() )->method( '__toString' )
727 ->will( $this->returnValue( 'MockProvider1' ) );
729 $provider2 = $providerBuilder->getMock();
730 $provider2->expects( $this->once() )->method( 'getVaryCookies' )
731 ->will( $this->returnValue( [ 'Foo', 'Baz' ] ) );
732 $provider2->expects( $this->any() )->method( '__toString' )
733 ->will( $this->returnValue( 'MockProvider2' ) );
735 $this->config
->set( 'SessionProviders', [
736 $this->objectCacheDef( $provider1 ),
737 $this->objectCacheDef( $provider2 ),
740 $expect = [ 'Foo', 'Bar', 'Baz' ];
742 $this->assertEquals( $expect, $manager->getVaryCookies() );
744 // Again, to ensure it's cached
745 $this->assertEquals( $expect, $manager->getVaryCookies() );
748 public function testGetProviders() {
749 $realManager = $this->getManager();
750 $manager = \TestingAccessWrapper
::newFromObject( $realManager );
752 $this->config
->set( 'SessionProviders', [
753 [ 'class' => 'DummySessionProvider' ],
755 $providers = $manager->getProviders();
756 $this->assertArrayHasKey( 'DummySessionProvider', $providers );
757 $provider = \TestingAccessWrapper
::newFromObject( $providers['DummySessionProvider'] );
758 $this->assertSame( $manager->logger
, $provider->logger
);
759 $this->assertSame( $manager->config
, $provider->config
);
760 $this->assertSame( $realManager, $provider->getManager() );
762 $this->config
->set( 'SessionProviders', [
763 [ 'class' => 'DummySessionProvider' ],
764 [ 'class' => 'DummySessionProvider' ],
766 $manager->sessionProviders
= null;
768 $manager->getProviders();
769 $this->fail( 'Expected exception not thrown' );
770 } catch ( \UnexpectedValueException
$ex ) {
772 'Duplicate provider name "DummySessionProvider"',
778 public function testShutdown() {
779 $manager = \TestingAccessWrapper
::newFromObject( $this->getManager() );
780 $manager->setLogger( new \Psr\Log\
NullLogger() );
782 $mock = $this->getMock( 'stdClass', [ 'shutdown' ] );
783 $mock->expects( $this->once() )->method( 'shutdown' );
785 $manager->allSessionBackends
= [ $mock ];
786 $manager->shutdown();
789 public function testGetSessionFromInfo() {
790 $manager = \TestingAccessWrapper
::newFromObject( $this->getManager() );
791 $request = new \
FauxRequest();
793 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
795 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
796 'provider' => $manager->getProvider( 'DummySessionProvider' ),
799 'userInfo' => UserInfo
::newFromName( 'UTSysop', true ),
802 \TestingAccessWrapper
::newFromObject( $info )->idIsSafe
= true;
803 $session1 = \TestingAccessWrapper
::newFromObject(
804 $manager->getSessionFromInfo( $info, $request )
806 $session2 = \TestingAccessWrapper
::newFromObject(
807 $manager->getSessionFromInfo( $info, $request )
810 $this->assertSame( $session1->backend
, $session2->backend
);
811 $this->assertNotEquals( $session1->index
, $session2->index
);
812 $this->assertSame( $session1->getSessionId(), $session2->getSessionId() );
813 $this->assertSame( $id, $session1->getId() );
815 \TestingAccessWrapper
::newFromObject( $info )->idIsSafe
= false;
816 $session3 = $manager->getSessionFromInfo( $info, $request );
817 $this->assertNotSame( $id, $session3->getId() );
820 public function testBackendRegistration() {
821 $manager = $this->getManager();
823 $session = $manager->getSessionForRequest( new \FauxRequest
);
824 $backend = \TestingAccessWrapper
::newFromObject( $session )->backend
;
825 $sessionId = $session->getSessionId();
826 $id = (string)$sessionId;
828 $this->assertSame( $sessionId, $manager->getSessionById( $id, true )->getSessionId() );
830 $manager->changeBackendId( $backend );
831 $this->assertSame( $sessionId, $session->getSessionId() );
832 $this->assertNotEquals( $id, (string)$sessionId );
833 $id = (string)$sessionId;
835 $this->assertSame( $sessionId, $manager->getSessionById( $id, true )->getSessionId() );
837 // Destruction of the session here causes the backend to be deregistered
841 $manager->changeBackendId( $backend );
842 $this->fail( 'Expected exception not thrown' );
843 } catch ( \InvalidArgumentException
$ex ) {
845 'Backend was not registered with this SessionManager', $ex->getMessage()
850 $manager->deregisterSessionBackend( $backend );
851 $this->fail( 'Expected exception not thrown' );
852 } catch ( \InvalidArgumentException
$ex ) {
854 'Backend was not registered with this SessionManager', $ex->getMessage()
858 $session = $manager->getSessionById( $id, true );
859 $this->assertSame( $sessionId, $session->getSessionId() );
862 public function testGenerateSessionId() {
863 $manager = $this->getManager();
865 $id = $manager->generateSessionId();
866 $this->assertTrue( SessionManager
::validateSessionId( $id ), "Generated ID: $id" );
869 public function testPreventSessionsForUser() {
870 $manager = $this->getManager();
872 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
873 ->setMethods( [ 'preventSessionsForUser', '__toString' ] );
875 $provider1 = $providerBuilder->getMock();
876 $provider1->expects( $this->once() )->method( 'preventSessionsForUser' )
877 ->with( $this->equalTo( 'UTSysop' ) );
878 $provider1->expects( $this->any() )->method( '__toString' )
879 ->will( $this->returnValue( 'MockProvider1' ) );
881 $this->config
->set( 'SessionProviders', [
882 $this->objectCacheDef( $provider1 ),
885 $this->assertFalse( $manager->isUserSessionPrevented( 'UTSysop' ) );
886 $manager->preventSessionsForUser( 'UTSysop' );
887 $this->assertTrue( $manager->isUserSessionPrevented( 'UTSysop' ) );
890 public function testLoadSessionInfoFromStore() {
891 $manager = $this->getManager();
892 $logger = new \
TestLogger( true );
893 $manager->setLogger( $logger );
894 $request = new \
FauxRequest();
896 // TestingAccessWrapper can't handle methods with reference arguments, sigh.
897 $rClass = new \
ReflectionClass( $manager );
898 $rMethod = $rClass->getMethod( 'loadSessionInfoFromStore' );
899 $rMethod->setAccessible( true );
900 $loadSessionInfoFromStore = function ( &$info ) use ( $rMethod, $manager, $request ) {
901 return $rMethod->invokeArgs( $manager, [ &$info, $request ] );
904 $userInfo = UserInfo
::newFromName( 'UTSysop', true );
905 $unverifiedUserInfo = UserInfo
::newFromName( 'UTSysop', false );
907 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
909 'userId' => $userInfo->getId(),
910 'userName' => $userInfo->getName(),
911 'userToken' => $userInfo->getToken( true ),
912 'provider' => 'Mock',
915 $builder = $this->getMockBuilder( SessionProvider
::class )
916 ->setMethods( [ '__toString', 'mergeMetadata', 'refreshSessionInfo' ] );
918 $provider = $builder->getMockForAbstractClass();
919 $provider->setManager( $manager );
920 $provider->expects( $this->any() )->method( 'persistsSessionId' )
921 ->will( $this->returnValue( true ) );
922 $provider->expects( $this->any() )->method( 'canChangeUser' )
923 ->will( $this->returnValue( true ) );
924 $provider->expects( $this->any() )->method( 'refreshSessionInfo' )
925 ->will( $this->returnValue( true ) );
926 $provider->expects( $this->any() )->method( '__toString' )
927 ->will( $this->returnValue( 'Mock' ) );
928 $provider->expects( $this->any() )->method( 'mergeMetadata' )
929 ->will( $this->returnCallback( function ( $a, $b ) {
930 if ( $b === [ 'Throw' ] ) {
931 throw new MetadataMergeException( 'no merge!' );
936 $provider2 = $builder->getMockForAbstractClass();
937 $provider2->setManager( $manager );
938 $provider2->expects( $this->any() )->method( 'persistsSessionId' )
939 ->will( $this->returnValue( false ) );
940 $provider2->expects( $this->any() )->method( 'canChangeUser' )
941 ->will( $this->returnValue( false ) );
942 $provider2->expects( $this->any() )->method( '__toString' )
943 ->will( $this->returnValue( 'Mock2' ) );
944 $provider2->expects( $this->any() )->method( 'refreshSessionInfo' )
945 ->will( $this->returnCallback( function ( $info, $request, &$metadata ) {
946 $metadata['changed'] = true;
950 $provider3 = $builder->getMockForAbstractClass();
951 $provider3->setManager( $manager );
952 $provider3->expects( $this->any() )->method( 'persistsSessionId' )
953 ->will( $this->returnValue( true ) );
954 $provider3->expects( $this->any() )->method( 'canChangeUser' )
955 ->will( $this->returnValue( true ) );
956 $provider3->expects( $this->once() )->method( 'refreshSessionInfo' )
957 ->will( $this->returnValue( false ) );
958 $provider3->expects( $this->any() )->method( '__toString' )
959 ->will( $this->returnValue( 'Mock3' ) );
961 \TestingAccessWrapper
::newFromObject( $manager )->sessionProviders
= [
962 (string)$provider => $provider,
963 (string)$provider2 => $provider2,
964 (string)$provider3 => $provider3,
967 // No metadata, basic usage
968 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
969 'provider' => $provider,
971 'userInfo' => $userInfo
973 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
974 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
975 $this->assertFalse( $info->isIdSafe() );
976 $this->assertSame( [], $logger->getBuffer() );
978 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
979 'provider' => $provider,
980 'userInfo' => $userInfo
982 $this->assertTrue( $info->isIdSafe(), 'sanity check' );
983 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
984 $this->assertTrue( $info->isIdSafe() );
985 $this->assertSame( [], $logger->getBuffer() );
987 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
988 'provider' => $provider2,
990 'userInfo' => $userInfo
992 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
993 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
994 $this->assertTrue( $info->isIdSafe() );
995 $this->assertSame( [], $logger->getBuffer() );
997 // Unverified user, no metadata
998 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
999 'provider' => $provider,
1001 'userInfo' => $unverifiedUserInfo
1003 $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
1004 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1005 $this->assertSame( [
1008 'Session "{session}": Unverified user provided and no metadata to auth it',
1010 ], $logger->getBuffer() );
1011 $logger->clearBuffer();
1013 // No metadata, missing data
1014 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1016 'userInfo' => $userInfo
1018 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1019 $this->assertSame( [
1020 [ LogLevel
::WARNING
, 'Session "{session}": Null provider and no metadata' ],
1021 ], $logger->getBuffer() );
1022 $logger->clearBuffer();
1024 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1025 'provider' => $provider,
1028 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1029 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1030 $this->assertInstanceOf( UserInfo
::class, $info->getUserInfo() );
1031 $this->assertTrue( $info->getUserInfo()->isVerified() );
1032 $this->assertTrue( $info->getUserInfo()->isAnon() );
1033 $this->assertFalse( $info->isIdSafe() );
1034 $this->assertSame( [], $logger->getBuffer() );
1036 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1037 'provider' => $provider2,
1040 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1041 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1042 $this->assertSame( [
1043 [ LogLevel
::INFO
, 'Session "{session}": No user provided and provider cannot set user' ]
1044 ], $logger->getBuffer() );
1045 $logger->clearBuffer();
1047 // Incomplete/bad metadata
1048 $this->store
->setRawSession( $id, true );
1049 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1050 $this->assertSame( [
1051 [ LogLevel
::WARNING
, 'Session "{session}": Bad data' ],
1052 ], $logger->getBuffer() );
1053 $logger->clearBuffer();
1055 $this->store
->setRawSession( $id, [ 'data' => [] ] );
1056 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1057 $this->assertSame( [
1058 [ LogLevel
::WARNING
, 'Session "{session}": Bad data structure' ],
1059 ], $logger->getBuffer() );
1060 $logger->clearBuffer();
1062 $this->store
->deleteSession( $id );
1063 $this->store
->setRawSession( $id, [ 'metadata' => $metadata ] );
1064 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1065 $this->assertSame( [
1066 [ LogLevel
::WARNING
, 'Session "{session}": Bad data structure' ],
1067 ], $logger->getBuffer() );
1068 $logger->clearBuffer();
1070 $this->store
->setRawSession( $id, [ 'metadata' => $metadata, 'data' => true ] );
1071 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1072 $this->assertSame( [
1073 [ LogLevel
::WARNING
, 'Session "{session}": Bad data structure' ],
1074 ], $logger->getBuffer() );
1075 $logger->clearBuffer();
1077 $this->store
->setRawSession( $id, [ 'metadata' => true, 'data' => [] ] );
1078 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1079 $this->assertSame( [
1080 [ LogLevel
::WARNING
, 'Session "{session}": Bad data structure' ],
1081 ], $logger->getBuffer() );
1082 $logger->clearBuffer();
1084 foreach ( $metadata as $key => $dummy ) {
1086 unset( $tmp[$key] );
1087 $this->store
->setRawSession( $id, [ 'metadata' => $tmp, 'data' => [] ] );
1088 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1089 $this->assertSame( [
1090 [ LogLevel
::WARNING
, 'Session "{session}": Bad metadata' ],
1091 ], $logger->getBuffer() );
1092 $logger->clearBuffer();
1095 // Basic usage with metadata
1096 $this->store
->setRawSession( $id, [ 'metadata' => $metadata, 'data' => [] ] );
1097 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1098 'provider' => $provider,
1100 'userInfo' => $userInfo
1102 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1103 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1104 $this->assertTrue( $info->isIdSafe() );
1105 $this->assertSame( [], $logger->getBuffer() );
1107 // Mismatched provider
1108 $this->store
->setSessionMeta( $id, [ 'provider' => 'Bad' ] +
$metadata );
1109 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1110 'provider' => $provider,
1112 'userInfo' => $userInfo
1114 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1115 $this->assertSame( [
1116 [ LogLevel
::WARNING
, 'Session "{session}": Wrong provider Bad !== Mock' ],
1117 ], $logger->getBuffer() );
1118 $logger->clearBuffer();
1121 $this->store
->setSessionMeta( $id, [ 'provider' => 'Bad' ] +
$metadata );
1122 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1124 'userInfo' => $userInfo
1126 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1127 $this->assertSame( [
1128 [ LogLevel
::WARNING
, 'Session "{session}": Unknown provider Bad' ],
1129 ], $logger->getBuffer() );
1130 $logger->clearBuffer();
1133 $this->store
->setSessionMeta( $id, $metadata );
1134 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1136 'userInfo' => $userInfo
1138 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1139 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1140 $this->assertTrue( $info->isIdSafe() );
1141 $this->assertSame( [], $logger->getBuffer() );
1143 // Bad user metadata
1144 $this->store
->setSessionMeta( $id, [ 'userId' => -1, 'userToken' => null ] +
$metadata );
1145 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1146 'provider' => $provider,
1149 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1150 $this->assertSame( [
1151 [ LogLevel
::ERROR
, 'Session "{session}": {exception}' ],
1152 ], $logger->getBuffer() );
1153 $logger->clearBuffer();
1155 $this->store
->setSessionMeta(
1156 $id, [ 'userId' => 0, 'userName' => '<X>', 'userToken' => null ] +
$metadata
1158 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1159 'provider' => $provider,
1162 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1163 $this->assertSame( [
1164 [ LogLevel
::ERROR
, 'Session "{session}": {exception}', ],
1165 ], $logger->getBuffer() );
1166 $logger->clearBuffer();
1168 // Mismatched user by ID
1169 $this->store
->setSessionMeta(
1170 $id, [ 'userId' => $userInfo->getId() +
1, 'userToken' => null ] +
$metadata
1172 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1173 'provider' => $provider,
1175 'userInfo' => $userInfo
1177 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1178 $this->assertSame( [
1179 [ LogLevel
::WARNING
, 'Session "{session}": User ID mismatch, {uid_a} !== {uid_b}' ],
1180 ], $logger->getBuffer() );
1181 $logger->clearBuffer();
1183 // Mismatched user by name
1184 $this->store
->setSessionMeta(
1185 $id, [ 'userId' => 0, 'userName' => 'X', 'userToken' => null ] +
$metadata
1187 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1188 'provider' => $provider,
1190 'userInfo' => $userInfo
1192 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1193 $this->assertSame( [
1194 [ LogLevel
::WARNING
, 'Session "{session}": User name mismatch, {uname_a} !== {uname_b}' ],
1195 ], $logger->getBuffer() );
1196 $logger->clearBuffer();
1198 // ID matches, name doesn't
1199 $this->store
->setSessionMeta(
1200 $id, [ 'userId' => $userInfo->getId(), 'userName' => 'X', 'userToken' => null ] +
$metadata
1202 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1203 'provider' => $provider,
1205 'userInfo' => $userInfo
1207 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1208 $this->assertSame( [
1211 'Session "{session}": User ID matched but name didn\'t (rename?), {uname_a} !== {uname_b}'
1213 ], $logger->getBuffer() );
1214 $logger->clearBuffer();
1216 // Mismatched anon user
1217 $this->store
->setSessionMeta(
1218 $id, [ 'userId' => 0, 'userName' => null, 'userToken' => null ] +
$metadata
1220 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1221 'provider' => $provider,
1223 'userInfo' => $userInfo
1225 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1226 $this->assertSame( [
1229 'Session "{session}": Metadata has an anonymous user, ' .
1230 'but a non-anon user was provided',
1232 ], $logger->getBuffer() );
1233 $logger->clearBuffer();
1235 // Lookup user by ID
1236 $this->store
->setSessionMeta( $id, [ 'userToken' => null ] +
$metadata );
1237 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1238 'provider' => $provider,
1241 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1242 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1243 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1244 $this->assertTrue( $info->isIdSafe() );
1245 $this->assertSame( [], $logger->getBuffer() );
1247 // Lookup user by name
1248 $this->store
->setSessionMeta(
1249 $id, [ 'userId' => 0, 'userName' => 'UTSysop', 'userToken' => null ] +
$metadata
1251 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1252 'provider' => $provider,
1255 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1256 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1257 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1258 $this->assertTrue( $info->isIdSafe() );
1259 $this->assertSame( [], $logger->getBuffer() );
1261 // Lookup anonymous user
1262 $this->store
->setSessionMeta(
1263 $id, [ 'userId' => 0, 'userName' => null, 'userToken' => null ] +
$metadata
1265 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1266 'provider' => $provider,
1269 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1270 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1271 $this->assertTrue( $info->getUserInfo()->isAnon() );
1272 $this->assertTrue( $info->isIdSafe() );
1273 $this->assertSame( [], $logger->getBuffer() );
1275 // Unverified user with metadata
1276 $this->store
->setSessionMeta( $id, $metadata );
1277 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1278 'provider' => $provider,
1280 'userInfo' => $unverifiedUserInfo
1282 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1283 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1284 $this->assertTrue( $info->getUserInfo()->isVerified() );
1285 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1286 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1287 $this->assertTrue( $info->isIdSafe() );
1288 $this->assertSame( [], $logger->getBuffer() );
1290 // Unverified user with metadata
1291 $this->store
->setSessionMeta( $id, $metadata );
1292 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1293 'provider' => $provider,
1295 'userInfo' => $unverifiedUserInfo
1297 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1298 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1299 $this->assertTrue( $info->getUserInfo()->isVerified() );
1300 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1301 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1302 $this->assertTrue( $info->isIdSafe() );
1303 $this->assertSame( [], $logger->getBuffer() );
1306 $this->store
->setSessionMeta( $id, [ 'userToken' => 'Bad' ] +
$metadata );
1307 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1308 'provider' => $provider,
1310 'userInfo' => $userInfo
1312 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1313 $this->assertSame( [
1314 [ LogLevel
::WARNING
, 'Session "{session}": User token mismatch' ],
1315 ], $logger->getBuffer() );
1316 $logger->clearBuffer();
1318 // Provider metadata
1319 $this->store
->setSessionMeta( $id, [ 'provider' => 'Mock2' ] +
$metadata );
1320 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1321 'provider' => $provider2,
1323 'userInfo' => $userInfo,
1324 'metadata' => [ 'Info' ],
1326 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1327 $this->assertSame( [ 'Info', 'changed' => true ], $info->getProviderMetadata() );
1328 $this->assertSame( [], $logger->getBuffer() );
1330 $this->store
->setSessionMeta( $id, [ 'providerMetadata' => [ 'Saved' ] ] +
$metadata );
1331 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1332 'provider' => $provider,
1334 'userInfo' => $userInfo,
1336 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1337 $this->assertSame( [ 'Saved' ], $info->getProviderMetadata() );
1338 $this->assertSame( [], $logger->getBuffer() );
1340 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1341 'provider' => $provider,
1343 'userInfo' => $userInfo,
1344 'metadata' => [ 'Info' ],
1346 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1347 $this->assertSame( [ 'Merged' ], $info->getProviderMetadata() );
1348 $this->assertSame( [], $logger->getBuffer() );
1350 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1351 'provider' => $provider,
1353 'userInfo' => $userInfo,
1354 'metadata' => [ 'Throw' ],
1356 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1357 $this->assertSame( [
1360 'Session "{session}": Metadata merge failed: {exception}',
1362 ], $logger->getBuffer() );
1363 $logger->clearBuffer();
1365 // Remember from session
1366 $this->store
->setSessionMeta( $id, $metadata );
1367 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1368 'provider' => $provider,
1371 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1372 $this->assertFalse( $info->wasRemembered() );
1373 $this->assertSame( [], $logger->getBuffer() );
1375 $this->store
->setSessionMeta( $id, [ 'remember' => true ] +
$metadata );
1376 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1377 'provider' => $provider,
1380 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1381 $this->assertTrue( $info->wasRemembered() );
1382 $this->assertSame( [], $logger->getBuffer() );
1384 $this->store
->setSessionMeta( $id, [ 'remember' => false ] +
$metadata );
1385 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1386 'provider' => $provider,
1388 'userInfo' => $userInfo
1390 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1391 $this->assertTrue( $info->wasRemembered() );
1392 $this->assertSame( [], $logger->getBuffer() );
1394 // forceHTTPS from session
1395 $this->store
->setSessionMeta( $id, $metadata );
1396 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1397 'provider' => $provider,
1399 'userInfo' => $userInfo
1401 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1402 $this->assertFalse( $info->forceHTTPS() );
1403 $this->assertSame( [], $logger->getBuffer() );
1405 $this->store
->setSessionMeta( $id, [ 'forceHTTPS' => true ] +
$metadata );
1406 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1407 'provider' => $provider,
1409 'userInfo' => $userInfo
1411 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1412 $this->assertTrue( $info->forceHTTPS() );
1413 $this->assertSame( [], $logger->getBuffer() );
1415 $this->store
->setSessionMeta( $id, [ 'forceHTTPS' => false ] +
$metadata );
1416 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1417 'provider' => $provider,
1419 'userInfo' => $userInfo,
1420 'forceHTTPS' => true
1422 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1423 $this->assertTrue( $info->forceHTTPS() );
1424 $this->assertSame( [], $logger->getBuffer() );
1426 // "Persist" flag from session
1427 $this->store
->setSessionMeta( $id, $metadata );
1428 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1429 'provider' => $provider,
1431 'userInfo' => $userInfo
1433 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1434 $this->assertFalse( $info->wasPersisted() );
1435 $this->assertSame( [], $logger->getBuffer() );
1437 $this->store
->setSessionMeta( $id, [ 'persisted' => true ] +
$metadata );
1438 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1439 'provider' => $provider,
1441 'userInfo' => $userInfo
1443 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1444 $this->assertTrue( $info->wasPersisted() );
1445 $this->assertSame( [], $logger->getBuffer() );
1447 $this->store
->setSessionMeta( $id, [ 'persisted' => false ] +
$metadata );
1448 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1449 'provider' => $provider,
1451 'userInfo' => $userInfo,
1454 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1455 $this->assertTrue( $info->wasPersisted() );
1456 $this->assertSame( [], $logger->getBuffer() );
1458 // Provider refreshSessionInfo() returning false
1459 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1460 'provider' => $provider3,
1462 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1463 $this->assertSame( [], $logger->getBuffer() );
1467 $data = [ 'foo' => 1 ];
1468 $this->store
->setSession( $id, [ 'metadata' => $metadata, 'data' => $data ] );
1469 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1470 'provider' => $provider,
1472 'userInfo' => $userInfo
1474 $this->mergeMwGlobalArrayValue( 'wgHooks', [
1475 'SessionCheckInfo' => [ function ( &$reason, $i, $r, $m, $d ) use (
1476 $info, $metadata, $data, $request, &$called
1478 $this->assertSame( $info->getId(), $i->getId() );
1479 $this->assertSame( $info->getProvider(), $i->getProvider() );
1480 $this->assertSame( $info->getUserInfo(), $i->getUserInfo() );
1481 $this->assertSame( $request, $r );
1482 $this->assertEquals( $metadata, $m );
1483 $this->assertEquals( $data, $d );
1488 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1489 $this->assertTrue( $called );
1490 $this->assertSame( [
1491 [ LogLevel
::WARNING
, 'Session "{session}": Hook aborted' ],
1492 ], $logger->getBuffer() );
1493 $logger->clearBuffer();
1494 $this->mergeMwGlobalArrayValue( 'wgHooks', [ 'SessionCheckInfo' => [] ] );
1496 // forceUse deletes bad backend data
1497 $this->store
->setSessionMeta( $id, [ 'userToken' => 'Bad' ] +
$metadata );
1498 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1499 'provider' => $provider,
1501 'userInfo' => $userInfo,
1504 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1505 $this->assertFalse( $this->store
->getSession( $id ) );
1506 $this->assertSame( [
1507 [ LogLevel
::WARNING
, 'Session "{session}": User token mismatch' ],
1508 ], $logger->getBuffer() );
1509 $logger->clearBuffer();