3 namespace MediaWiki\Session
;
8 use Wikimedia\TestingAccessWrapper
;
13 * @covers MediaWiki\Session\SessionManager
15 class SessionManagerTest
extends MediaWikiTestCase
{
17 /** @var \HashConfig */
20 /** @var \TestLogger */
23 /** @var TestBagOStuff */
26 protected function getManager() {
27 \ObjectCache
::$instances['testSessionStore'] = new TestBagOStuff();
28 $this->config
= new \
HashConfig( [
29 'LanguageCode' => 'en',
30 'SessionCacheType' => 'testSessionStore',
31 'ObjectCacheSessionExpiry' => 100,
32 'SessionProviders' => [
33 [ 'class' => \DummySessionProvider
::class ],
36 $this->logger
= new \
TestLogger( false, function ( $m ) {
37 return substr( $m, 0, 15 ) === 'SessionBackend ' ?
null : $m;
39 $this->store
= new TestBagOStuff();
41 return new SessionManager( [
42 'config' => $this->config
,
43 'logger' => $this->logger
,
44 'store' => $this->store
,
48 protected function objectCacheDef( $object ) {
49 return [ 'factory' => function () use ( $object ) {
54 public function testSingleton() {
55 $reset = TestUtils
::setSessionManagerSingleton( null );
57 $singleton = SessionManager
::singleton();
58 $this->assertInstanceOf( SessionManager
::class, $singleton );
59 $this->assertSame( $singleton, SessionManager
::singleton() );
62 public function testGetGlobalSession() {
63 $context = \RequestContext
::getMain();
65 if ( !PHPSessionHandler
::isInstalled() ) {
66 PHPSessionHandler
::install( SessionManager
::singleton() );
68 $rProp = new \
ReflectionProperty( PHPSessionHandler
::class, 'instance' );
69 $rProp->setAccessible( true );
70 $handler = TestingAccessWrapper
::newFromObject( $rProp->getValue() );
71 $oldEnable = $handler->enable
;
72 $reset[] = new \Wikimedia\
ScopedCallback( function () use ( $handler, $oldEnable ) {
73 if ( $handler->enable
) {
74 session_write_close();
76 $handler->enable
= $oldEnable;
78 $reset[] = TestUtils
::setSessionManagerSingleton( $this->getManager() );
80 $handler->enable
= true;
81 $request = new \
FauxRequest();
82 $context->setRequest( $request );
83 $id = $request->getSession()->getId();
86 $session = SessionManager
::getGlobalSession();
87 $this->assertSame( $id, $session->getId() );
89 session_id( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
90 $session = SessionManager
::getGlobalSession();
91 $this->assertSame( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', $session->getId() );
92 $this->assertSame( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', $request->getSession()->getId() );
94 session_write_close();
95 $handler->enable
= false;
96 $request = new \
FauxRequest();
97 $context->setRequest( $request );
98 $id = $request->getSession()->getId();
101 $session = SessionManager
::getGlobalSession();
102 $this->assertSame( $id, $session->getId() );
104 session_id( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
105 $session = SessionManager
::getGlobalSession();
106 $this->assertSame( $id, $session->getId() );
107 $this->assertSame( $id, $request->getSession()->getId() );
110 public function testConstructor() {
111 $manager = TestingAccessWrapper
::newFromObject( $this->getManager() );
112 $this->assertSame( $this->config
, $manager->config
);
113 $this->assertSame( $this->logger
, $manager->logger
);
114 $this->assertSame( $this->store
, $manager->store
);
116 $manager = TestingAccessWrapper
::newFromObject( new SessionManager() );
117 $this->assertSame( \RequestContext
::getMain()->getConfig(), $manager->config
);
119 $manager = TestingAccessWrapper
::newFromObject( new SessionManager( [
120 'config' => $this->config
,
122 $this->assertSame( \ObjectCache
::$instances['testSessionStore'], $manager->store
);
125 'config' => '$options[\'config\'] must be an instance of Config',
126 'logger' => '$options[\'logger\'] must be an instance of LoggerInterface',
127 'store' => '$options[\'store\'] must be an instance of BagOStuff',
128 ] as $key => $error ) {
130 new SessionManager( [ $key => new \stdClass
] );
131 $this->fail( 'Expected exception not thrown' );
132 } catch ( \InvalidArgumentException
$ex ) {
133 $this->assertSame( $error, $ex->getMessage() );
138 public function testGetSessionForRequest() {
139 $manager = $this->getManager();
140 $request = new \
FauxRequest();
141 $request->unpersist1
= false;
142 $request->unpersist2
= false;
146 $idEmpty = 'empty-session-------------------';
148 $providerBuilder = $this->getMockBuilder( \DummySessionProvider
::class )
150 [ 'provideSessionInfo', 'newSessionInfo', '__toString', 'describe', 'unpersistSession' ]
153 $provider1 = $providerBuilder->getMock();
154 $provider1->expects( $this->any() )->method( 'provideSessionInfo' )
155 ->with( $this->identicalTo( $request ) )
156 ->will( $this->returnCallback( function ( $request ) {
157 return $request->info1
;
159 $provider1->expects( $this->any() )->method( 'newSessionInfo' )
160 ->will( $this->returnCallback( function () use ( $idEmpty, $provider1 ) {
161 return new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
162 'provider' => $provider1,
168 $provider1->expects( $this->any() )->method( '__toString' )
169 ->will( $this->returnValue( 'Provider1' ) );
170 $provider1->expects( $this->any() )->method( 'describe' )
171 ->will( $this->returnValue( '#1 sessions' ) );
172 $provider1->expects( $this->any() )->method( 'unpersistSession' )
173 ->will( $this->returnCallback( function ( $request ) {
174 $request->unpersist1
= true;
177 $provider2 = $providerBuilder->getMock();
178 $provider2->expects( $this->any() )->method( 'provideSessionInfo' )
179 ->with( $this->identicalTo( $request ) )
180 ->will( $this->returnCallback( function ( $request ) {
181 return $request->info2
;
183 $provider2->expects( $this->any() )->method( '__toString' )
184 ->will( $this->returnValue( 'Provider2' ) );
185 $provider2->expects( $this->any() )->method( 'describe' )
186 ->will( $this->returnValue( '#2 sessions' ) );
187 $provider2->expects( $this->any() )->method( 'unpersistSession' )
188 ->will( $this->returnCallback( function ( $request ) {
189 $request->unpersist2
= true;
192 $this->config
->set( 'SessionProviders', [
193 $this->objectCacheDef( $provider1 ),
194 $this->objectCacheDef( $provider2 ),
197 // No provider returns info
198 $request->info1
= null;
199 $request->info2
= null;
200 $session = $manager->getSessionForRequest( $request );
201 $this->assertInstanceOf( Session
::class, $session );
202 $this->assertSame( $idEmpty, $session->getId() );
203 $this->assertFalse( $request->unpersist1
);
204 $this->assertFalse( $request->unpersist2
);
206 // Both providers return info, picks best one
207 $request->info1
= new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, [
208 'provider' => $provider1,
209 'id' => ( $id1 = $manager->generateSessionId() ),
213 $request->info2
= new SessionInfo( SessionInfo
::MIN_PRIORITY +
2, [
214 'provider' => $provider2,
215 'id' => ( $id2 = $manager->generateSessionId() ),
219 $session = $manager->getSessionForRequest( $request );
220 $this->assertInstanceOf( Session
::class, $session );
221 $this->assertSame( $id2, $session->getId() );
222 $this->assertFalse( $request->unpersist1
);
223 $this->assertFalse( $request->unpersist2
);
225 $request->info1
= new SessionInfo( SessionInfo
::MIN_PRIORITY +
2, [
226 'provider' => $provider1,
227 'id' => ( $id1 = $manager->generateSessionId() ),
231 $request->info2
= new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, [
232 'provider' => $provider2,
233 'id' => ( $id2 = $manager->generateSessionId() ),
237 $session = $manager->getSessionForRequest( $request );
238 $this->assertInstanceOf( Session
::class, $session );
239 $this->assertSame( $id1, $session->getId() );
240 $this->assertFalse( $request->unpersist1
);
241 $this->assertFalse( $request->unpersist2
);
244 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, [
245 'provider' => $provider1,
246 'id' => ( $id1 = $manager->generateSessionId() ),
248 'userInfo' => UserInfo
::newAnonymous(),
251 $request->info2
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, [
252 'provider' => $provider2,
253 'id' => ( $id2 = $manager->generateSessionId() ),
255 'userInfo' => UserInfo
::newAnonymous(),
259 $manager->getSessionForRequest( $request );
260 $this->fail( 'Expcected exception not thrown' );
261 } catch ( \OverflowException
$ex ) {
262 $this->assertStringStartsWith(
263 'Multiple sessions for this request tied for top priority: ',
266 $this->assertCount( 2, $ex->sessionInfos
);
267 $this->assertContains( $request->info1
, $ex->sessionInfos
);
268 $this->assertContains( $request->info2
, $ex->sessionInfos
);
270 $this->assertFalse( $request->unpersist1
);
271 $this->assertFalse( $request->unpersist2
);
274 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, [
275 'provider' => $provider2,
276 'id' => ( $id1 = $manager->generateSessionId() ),
280 $request->info2
= null;
282 $manager->getSessionForRequest( $request );
283 $this->fail( 'Expcected exception not thrown' );
284 } catch ( \UnexpectedValueException
$ex ) {
286 'Provider1 returned session info for a different provider: ' . $request->info1
,
290 $this->assertFalse( $request->unpersist1
);
291 $this->assertFalse( $request->unpersist2
);
293 // Unusable session info
294 $this->logger
->setCollect( true );
295 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, [
296 'provider' => $provider1,
297 'id' => ( $id1 = $manager->generateSessionId() ),
299 'userInfo' => UserInfo
::newFromName( 'UTSysop', false ),
302 $request->info2
= new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
303 'provider' => $provider2,
304 'id' => ( $id2 = $manager->generateSessionId() ),
308 $session = $manager->getSessionForRequest( $request );
309 $this->assertInstanceOf( Session
::class, $session );
310 $this->assertSame( $id2, $session->getId() );
311 $this->logger
->setCollect( false );
312 $this->assertTrue( $request->unpersist1
);
313 $this->assertFalse( $request->unpersist2
);
314 $request->unpersist1
= false;
316 $this->logger
->setCollect( true );
317 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, [
318 'provider' => $provider1,
319 'id' => ( $id1 = $manager->generateSessionId() ),
323 $request->info2
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, [
324 'provider' => $provider2,
325 'id' => ( $id2 = $manager->generateSessionId() ),
327 'userInfo' => UserInfo
::newFromName( 'UTSysop', false ),
330 $session = $manager->getSessionForRequest( $request );
331 $this->assertInstanceOf( Session
::class, $session );
332 $this->assertSame( $id1, $session->getId() );
333 $this->logger
->setCollect( false );
334 $this->assertFalse( $request->unpersist1
);
335 $this->assertTrue( $request->unpersist2
);
336 $request->unpersist2
= false;
338 // Unpersisted session ID
339 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, [
340 'provider' => $provider1,
341 'id' => ( $id1 = $manager->generateSessionId() ),
342 'persisted' => false,
343 'userInfo' => UserInfo
::newFromName( 'UTSysop', true ),
346 $request->info2
= null;
347 $session = $manager->getSessionForRequest( $request );
348 $this->assertInstanceOf( Session
::class, $session );
349 $this->assertSame( $id1, $session->getId() );
350 $this->assertTrue( $request->unpersist1
); // The saving of the session does it
351 $this->assertFalse( $request->unpersist2
);
353 $this->assertTrue( $session->isPersistent(), 'sanity check' );
356 public function testGetSessionById() {
357 $manager = $this->getManager();
359 $manager->getSessionById( 'bad' );
360 $this->fail( 'Expected exception not thrown' );
361 } catch ( \InvalidArgumentException
$ex ) {
362 $this->assertSame( 'Invalid session ID', $ex->getMessage() );
365 // Unknown session ID
366 $id = $manager->generateSessionId();
367 $session = $manager->getSessionById( $id, true );
368 $this->assertInstanceOf( Session
::class, $session );
369 $this->assertSame( $id, $session->getId() );
371 $id = $manager->generateSessionId();
372 $this->assertNull( $manager->getSessionById( $id, false ) );
374 // Known but unloadable session ID
375 $this->logger
->setCollect( true );
376 $id = $manager->generateSessionId();
377 $this->store
->setSession( $id, [ 'metadata' => [
378 'userId' => User
::idFromName( 'UTSysop' ),
379 'userToken' => 'bad',
382 $this->assertNull( $manager->getSessionById( $id, true ) );
383 $this->assertNull( $manager->getSessionById( $id, false ) );
384 $this->logger
->setCollect( false );
387 $this->store
->setSession( $id, [] );
388 $session = $manager->getSessionById( $id, false );
389 $this->assertInstanceOf( Session
::class, $session );
390 $this->assertSame( $id, $session->getId() );
392 // Store isn't checked if the session is already loaded
393 $this->store
->setSession( $id, [ 'metadata' => [
394 'userId' => User
::idFromName( 'UTSysop' ),
395 'userToken' => 'bad',
397 $session2 = $manager->getSessionById( $id, false );
398 $this->assertInstanceOf( Session
::class, $session2 );
399 $this->assertSame( $id, $session2->getId() );
400 unset( $session, $session2 );
401 $this->logger
->setCollect( true );
402 $this->assertNull( $manager->getSessionById( $id, true ) );
403 $this->logger
->setCollect( false );
405 // Failure to create an empty session
406 $manager = $this->getManager();
407 $provider = $this->getMockBuilder( \DummySessionProvider
::class )
408 ->setMethods( [ 'provideSessionInfo', 'newSessionInfo', '__toString' ] )
410 $provider->expects( $this->any() )->method( 'provideSessionInfo' )
411 ->will( $this->returnValue( null ) );
412 $provider->expects( $this->any() )->method( 'newSessionInfo' )
413 ->will( $this->returnValue( null ) );
414 $provider->expects( $this->any() )->method( '__toString' )
415 ->will( $this->returnValue( 'MockProvider' ) );
416 $this->config
->set( 'SessionProviders', [
417 $this->objectCacheDef( $provider ),
419 $this->logger
->setCollect( true );
420 $this->assertNull( $manager->getSessionById( $id, true ) );
421 $this->logger
->setCollect( false );
423 [ LogLevel
::ERROR
, 'Failed to create empty session: {exception}' ]
424 ], $this->logger
->getBuffer() );
427 public function testGetEmptySession() {
428 $manager = $this->getManager();
429 $pmanager = TestingAccessWrapper
::newFromObject( $manager );
430 $request = new \
FauxRequest();
432 $providerBuilder = $this->getMockBuilder( \DummySessionProvider
::class )
433 ->setMethods( [ 'provideSessionInfo', 'newSessionInfo', '__toString' ] );
439 $provider1 = $providerBuilder->getMock();
440 $provider1->expects( $this->any() )->method( 'provideSessionInfo' )
441 ->will( $this->returnValue( null ) );
442 $provider1->expects( $this->any() )->method( 'newSessionInfo' )
443 ->with( $this->callback( function ( $id ) use ( &$expectId ) {
444 return $id === $expectId;
446 ->will( $this->returnCallback( function () use ( &$info1 ) {
449 $provider1->expects( $this->any() )->method( '__toString' )
450 ->will( $this->returnValue( 'MockProvider1' ) );
452 $provider2 = $providerBuilder->getMock();
453 $provider2->expects( $this->any() )->method( 'provideSessionInfo' )
454 ->will( $this->returnValue( null ) );
455 $provider2->expects( $this->any() )->method( 'newSessionInfo' )
456 ->with( $this->callback( function ( $id ) use ( &$expectId ) {
457 return $id === $expectId;
459 ->will( $this->returnCallback( function () use ( &$info2 ) {
462 $provider1->expects( $this->any() )->method( '__toString' )
463 ->will( $this->returnValue( 'MockProvider2' ) );
465 $this->config
->set( 'SessionProviders', [
466 $this->objectCacheDef( $provider1 ),
467 $this->objectCacheDef( $provider2 ),
475 $manager->getEmptySession();
476 $this->fail( 'Expected exception not thrown' );
477 } catch ( \UnexpectedValueException
$ex ) {
479 'No provider could provide an empty session!',
486 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
487 'provider' => $provider1,
488 'id' => 'empty---------------------------',
493 $session = $manager->getEmptySession();
494 $this->assertInstanceOf( Session
::class, $session );
495 $this->assertSame( 'empty---------------------------', $session->getId() );
498 $expectId = 'expected------------------------';
499 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
500 'provider' => $provider1,
506 $session = $pmanager->getEmptySessionInternal( null, $expectId );
507 $this->assertInstanceOf( Session
::class, $session );
508 $this->assertSame( $expectId, $session->getId() );
511 $expectId = 'expected-----------------------2';
512 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
513 'provider' => $provider1,
514 'id' => "un$expectId",
520 $pmanager->getEmptySessionInternal( null, $expectId );
521 $this->fail( 'Expected exception not thrown' );
522 } catch ( \UnexpectedValueException
$ex ) {
524 'MockProvider1 returned empty session info with a wrong id: ' .
525 "un$expectId != $expectId",
531 $expectId = 'expected-----------------------2';
532 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
533 'provider' => $provider1,
539 $pmanager->getEmptySessionInternal( null, $expectId );
540 $this->fail( 'Expected exception not thrown' );
541 } catch ( \UnexpectedValueException
$ex ) {
543 'MockProvider1 returned empty session info with id flagged unsafe',
550 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
551 'provider' => $provider2,
552 'id' => 'empty---------------------------',
558 $manager->getEmptySession();
559 $this->fail( 'Expected exception not thrown' );
560 } catch ( \UnexpectedValueException
$ex ) {
562 'MockProvider1 returned an empty session info for a different provider: ' . $info1,
567 // Highest priority wins
569 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, [
570 'provider' => $provider1,
571 'id' => 'empty1--------------------------',
575 $info2 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
576 'provider' => $provider2,
577 'id' => 'empty2--------------------------',
581 $session = $manager->getEmptySession();
582 $this->assertInstanceOf( Session
::class, $session );
583 $this->assertSame( 'empty1--------------------------', $session->getId() );
586 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, [
587 'provider' => $provider1,
588 'id' => 'empty1--------------------------',
592 $info2 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
2, [
593 'provider' => $provider2,
594 'id' => 'empty2--------------------------',
598 $session = $manager->getEmptySession();
599 $this->assertInstanceOf( Session
::class, $session );
600 $this->assertSame( 'empty2--------------------------', $session->getId() );
602 // Tied priorities throw an exception
604 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
605 'provider' => $provider1,
606 'id' => 'empty1--------------------------',
608 'userInfo' => UserInfo
::newAnonymous(),
611 $info2 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
612 'provider' => $provider2,
613 'id' => 'empty2--------------------------',
615 'userInfo' => UserInfo
::newAnonymous(),
619 $manager->getEmptySession();
620 $this->fail( 'Expected exception not thrown' );
621 } catch ( \UnexpectedValueException
$ex ) {
622 $this->assertStringStartsWith(
623 'Multiple empty sessions tied for top priority: ',
630 $pmanager->getEmptySessionInternal( null, 'bad' );
631 $this->fail( 'Expected exception not thrown' );
632 } catch ( \InvalidArgumentException
$ex ) {
633 $this->assertSame( 'Invalid session ID', $ex->getMessage() );
636 // Session already exists
637 $expectId = 'expected-----------------------3';
638 $this->store
->setSessionMeta( $expectId, [
639 'provider' => 'MockProvider2',
645 $pmanager->getEmptySessionInternal( null, $expectId );
646 $this->fail( 'Expected exception not thrown' );
647 } catch ( \InvalidArgumentException
$ex ) {
648 $this->assertSame( 'Session ID already exists', $ex->getMessage() );
652 public function testInvalidateSessionsForUser() {
653 $user = User
::newFromName( 'UTSysop' );
654 $manager = $this->getManager();
656 $providerBuilder = $this->getMockBuilder( \DummySessionProvider
::class )
657 ->setMethods( [ 'invalidateSessionsForUser', '__toString' ] );
659 $provider1 = $providerBuilder->getMock();
660 $provider1->expects( $this->once() )->method( 'invalidateSessionsForUser' )
661 ->with( $this->identicalTo( $user ) );
662 $provider1->expects( $this->any() )->method( '__toString' )
663 ->will( $this->returnValue( 'MockProvider1' ) );
665 $provider2 = $providerBuilder->getMock();
666 $provider2->expects( $this->once() )->method( 'invalidateSessionsForUser' )
667 ->with( $this->identicalTo( $user ) );
668 $provider2->expects( $this->any() )->method( '__toString' )
669 ->will( $this->returnValue( 'MockProvider2' ) );
671 $this->config
->set( 'SessionProviders', [
672 $this->objectCacheDef( $provider1 ),
673 $this->objectCacheDef( $provider2 ),
676 $oldToken = $user->getToken( true );
677 $manager->invalidateSessionsForUser( $user );
678 $this->assertNotEquals( $oldToken, $user->getToken() );
681 public function testGetVaryHeaders() {
682 $manager = $this->getManager();
684 $providerBuilder = $this->getMockBuilder( \DummySessionProvider
::class )
685 ->setMethods( [ 'getVaryHeaders', '__toString' ] );
687 $provider1 = $providerBuilder->getMock();
688 $provider1->expects( $this->once() )->method( 'getVaryHeaders' )
689 ->will( $this->returnValue( [
691 'Bar' => [ 'X', 'Bar1' ],
694 $provider1->expects( $this->any() )->method( '__toString' )
695 ->will( $this->returnValue( 'MockProvider1' ) );
697 $provider2 = $providerBuilder->getMock();
698 $provider2->expects( $this->once() )->method( 'getVaryHeaders' )
699 ->will( $this->returnValue( [
701 'Bar' => [ 'X', 'Bar2' ],
702 'Quux' => [ 'Quux' ],
704 $provider2->expects( $this->any() )->method( '__toString' )
705 ->will( $this->returnValue( 'MockProvider2' ) );
707 $this->config
->set( 'SessionProviders', [
708 $this->objectCacheDef( $provider1 ),
709 $this->objectCacheDef( $provider2 ),
714 'Bar' => [ 'X', 'Bar1', 3 => 'Bar2' ],
715 'Quux' => [ 'Quux' ],
719 $this->assertEquals( $expect, $manager->getVaryHeaders() );
721 // Again, to ensure it's cached
722 $this->assertEquals( $expect, $manager->getVaryHeaders() );
725 public function testGetVaryCookies() {
726 $manager = $this->getManager();
728 $providerBuilder = $this->getMockBuilder( \DummySessionProvider
::class )
729 ->setMethods( [ 'getVaryCookies', '__toString' ] );
731 $provider1 = $providerBuilder->getMock();
732 $provider1->expects( $this->once() )->method( 'getVaryCookies' )
733 ->will( $this->returnValue( [ 'Foo', 'Bar' ] ) );
734 $provider1->expects( $this->any() )->method( '__toString' )
735 ->will( $this->returnValue( 'MockProvider1' ) );
737 $provider2 = $providerBuilder->getMock();
738 $provider2->expects( $this->once() )->method( 'getVaryCookies' )
739 ->will( $this->returnValue( [ 'Foo', 'Baz' ] ) );
740 $provider2->expects( $this->any() )->method( '__toString' )
741 ->will( $this->returnValue( 'MockProvider2' ) );
743 $this->config
->set( 'SessionProviders', [
744 $this->objectCacheDef( $provider1 ),
745 $this->objectCacheDef( $provider2 ),
748 $expect = [ 'Foo', 'Bar', 'Baz' ];
750 $this->assertEquals( $expect, $manager->getVaryCookies() );
752 // Again, to ensure it's cached
753 $this->assertEquals( $expect, $manager->getVaryCookies() );
756 public function testGetProviders() {
757 $realManager = $this->getManager();
758 $manager = TestingAccessWrapper
::newFromObject( $realManager );
760 $this->config
->set( 'SessionProviders', [
761 [ 'class' => \DummySessionProvider
::class ],
763 $providers = $manager->getProviders();
764 $this->assertArrayHasKey( 'DummySessionProvider', $providers );
765 $provider = TestingAccessWrapper
::newFromObject( $providers['DummySessionProvider'] );
766 $this->assertSame( $manager->logger
, $provider->logger
);
767 $this->assertSame( $manager->config
, $provider->config
);
768 $this->assertSame( $realManager, $provider->getManager() );
770 $this->config
->set( 'SessionProviders', [
771 [ 'class' => \DummySessionProvider
::class ],
772 [ 'class' => \DummySessionProvider
::class ],
774 $manager->sessionProviders
= null;
776 $manager->getProviders();
777 $this->fail( 'Expected exception not thrown' );
778 } catch ( \UnexpectedValueException
$ex ) {
780 'Duplicate provider name "DummySessionProvider"',
786 public function testShutdown() {
787 $manager = TestingAccessWrapper
::newFromObject( $this->getManager() );
788 $manager->setLogger( new \Psr\Log\
NullLogger() );
790 $mock = $this->getMockBuilder( stdClass
::class )
791 ->setMethods( [ 'shutdown' ] )->getMock();
792 $mock->expects( $this->once() )->method( 'shutdown' );
794 $manager->allSessionBackends
= [ $mock ];
795 $manager->shutdown();
798 public function testGetSessionFromInfo() {
799 $manager = TestingAccessWrapper
::newFromObject( $this->getManager() );
800 $request = new \
FauxRequest();
802 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
804 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
805 'provider' => $manager->getProvider( 'DummySessionProvider' ),
808 'userInfo' => UserInfo
::newFromName( 'UTSysop', true ),
811 TestingAccessWrapper
::newFromObject( $info )->idIsSafe
= true;
812 $session1 = TestingAccessWrapper
::newFromObject(
813 $manager->getSessionFromInfo( $info, $request )
815 $session2 = TestingAccessWrapper
::newFromObject(
816 $manager->getSessionFromInfo( $info, $request )
819 $this->assertSame( $session1->backend
, $session2->backend
);
820 $this->assertNotEquals( $session1->index
, $session2->index
);
821 $this->assertSame( $session1->getSessionId(), $session2->getSessionId() );
822 $this->assertSame( $id, $session1->getId() );
824 TestingAccessWrapper
::newFromObject( $info )->idIsSafe
= false;
825 $session3 = $manager->getSessionFromInfo( $info, $request );
826 $this->assertNotSame( $id, $session3->getId() );
829 public function testBackendRegistration() {
830 $manager = $this->getManager();
832 $session = $manager->getSessionForRequest( new \FauxRequest
);
833 $backend = TestingAccessWrapper
::newFromObject( $session )->backend
;
834 $sessionId = $session->getSessionId();
835 $id = (string)$sessionId;
837 $this->assertSame( $sessionId, $manager->getSessionById( $id, true )->getSessionId() );
839 $manager->changeBackendId( $backend );
840 $this->assertSame( $sessionId, $session->getSessionId() );
841 $this->assertNotEquals( $id, (string)$sessionId );
842 $id = (string)$sessionId;
844 $this->assertSame( $sessionId, $manager->getSessionById( $id, true )->getSessionId() );
846 // Destruction of the session here causes the backend to be deregistered
850 $manager->changeBackendId( $backend );
851 $this->fail( 'Expected exception not thrown' );
852 } catch ( \InvalidArgumentException
$ex ) {
854 'Backend was not registered with this SessionManager', $ex->getMessage()
859 $manager->deregisterSessionBackend( $backend );
860 $this->fail( 'Expected exception not thrown' );
861 } catch ( \InvalidArgumentException
$ex ) {
863 'Backend was not registered with this SessionManager', $ex->getMessage()
867 $session = $manager->getSessionById( $id, true );
868 $this->assertSame( $sessionId, $session->getSessionId() );
871 public function testGenerateSessionId() {
872 $manager = $this->getManager();
874 $id = $manager->generateSessionId();
875 $this->assertTrue( SessionManager
::validateSessionId( $id ), "Generated ID: $id" );
878 public function testPreventSessionsForUser() {
879 $manager = $this->getManager();
881 $providerBuilder = $this->getMockBuilder( \DummySessionProvider
::class )
882 ->setMethods( [ 'preventSessionsForUser', '__toString' ] );
884 $provider1 = $providerBuilder->getMock();
885 $provider1->expects( $this->once() )->method( 'preventSessionsForUser' )
886 ->with( $this->equalTo( 'UTSysop' ) );
887 $provider1->expects( $this->any() )->method( '__toString' )
888 ->will( $this->returnValue( 'MockProvider1' ) );
890 $this->config
->set( 'SessionProviders', [
891 $this->objectCacheDef( $provider1 ),
894 $this->assertFalse( $manager->isUserSessionPrevented( 'UTSysop' ) );
895 $manager->preventSessionsForUser( 'UTSysop' );
896 $this->assertTrue( $manager->isUserSessionPrevented( 'UTSysop' ) );
899 public function testLoadSessionInfoFromStore() {
900 $manager = $this->getManager();
901 $logger = new \
TestLogger( true );
902 $manager->setLogger( $logger );
903 $request = new \
FauxRequest();
905 // TestingAccessWrapper can't handle methods with reference arguments, sigh.
906 $rClass = new \
ReflectionClass( $manager );
907 $rMethod = $rClass->getMethod( 'loadSessionInfoFromStore' );
908 $rMethod->setAccessible( true );
909 $loadSessionInfoFromStore = function ( &$info ) use ( $rMethod, $manager, $request ) {
910 return $rMethod->invokeArgs( $manager, [ &$info, $request ] );
913 $userInfo = UserInfo
::newFromName( 'UTSysop', true );
914 $unverifiedUserInfo = UserInfo
::newFromName( 'UTSysop', false );
916 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
918 'userId' => $userInfo->getId(),
919 'userName' => $userInfo->getName(),
920 'userToken' => $userInfo->getToken( true ),
921 'provider' => 'Mock',
924 $builder = $this->getMockBuilder( SessionProvider
::class )
925 ->setMethods( [ '__toString', 'mergeMetadata', 'refreshSessionInfo' ] );
927 $provider = $builder->getMockForAbstractClass();
928 $provider->setManager( $manager );
929 $provider->expects( $this->any() )->method( 'persistsSessionId' )
930 ->will( $this->returnValue( true ) );
931 $provider->expects( $this->any() )->method( 'canChangeUser' )
932 ->will( $this->returnValue( true ) );
933 $provider->expects( $this->any() )->method( 'refreshSessionInfo' )
934 ->will( $this->returnValue( true ) );
935 $provider->expects( $this->any() )->method( '__toString' )
936 ->will( $this->returnValue( 'Mock' ) );
937 $provider->expects( $this->any() )->method( 'mergeMetadata' )
938 ->will( $this->returnCallback( function ( $a, $b ) {
939 if ( $b === [ 'Throw' ] ) {
940 throw new MetadataMergeException( 'no merge!' );
945 $provider2 = $builder->getMockForAbstractClass();
946 $provider2->setManager( $manager );
947 $provider2->expects( $this->any() )->method( 'persistsSessionId' )
948 ->will( $this->returnValue( false ) );
949 $provider2->expects( $this->any() )->method( 'canChangeUser' )
950 ->will( $this->returnValue( false ) );
951 $provider2->expects( $this->any() )->method( '__toString' )
952 ->will( $this->returnValue( 'Mock2' ) );
953 $provider2->expects( $this->any() )->method( 'refreshSessionInfo' )
954 ->will( $this->returnCallback( function ( $info, $request, &$metadata ) {
955 $metadata['changed'] = true;
959 $provider3 = $builder->getMockForAbstractClass();
960 $provider3->setManager( $manager );
961 $provider3->expects( $this->any() )->method( 'persistsSessionId' )
962 ->will( $this->returnValue( true ) );
963 $provider3->expects( $this->any() )->method( 'canChangeUser' )
964 ->will( $this->returnValue( true ) );
965 $provider3->expects( $this->once() )->method( 'refreshSessionInfo' )
966 ->will( $this->returnValue( false ) );
967 $provider3->expects( $this->any() )->method( '__toString' )
968 ->will( $this->returnValue( 'Mock3' ) );
970 TestingAccessWrapper
::newFromObject( $manager )->sessionProviders
= [
971 (string)$provider => $provider,
972 (string)$provider2 => $provider2,
973 (string)$provider3 => $provider3,
976 // No metadata, basic usage
977 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
978 'provider' => $provider,
980 'userInfo' => $userInfo
982 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
983 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
984 $this->assertFalse( $info->isIdSafe() );
985 $this->assertSame( [], $logger->getBuffer() );
987 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
988 'provider' => $provider,
989 'userInfo' => $userInfo
991 $this->assertTrue( $info->isIdSafe(), 'sanity check' );
992 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
993 $this->assertTrue( $info->isIdSafe() );
994 $this->assertSame( [], $logger->getBuffer() );
996 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
997 'provider' => $provider2,
999 'userInfo' => $userInfo
1001 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1002 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1003 $this->assertTrue( $info->isIdSafe() );
1004 $this->assertSame( [], $logger->getBuffer() );
1006 // Unverified user, no metadata
1007 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1008 'provider' => $provider,
1010 'userInfo' => $unverifiedUserInfo
1012 $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
1013 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1014 $this->assertSame( [
1017 'Session "{session}": Unverified user provided and no metadata to auth it',
1019 ], $logger->getBuffer() );
1020 $logger->clearBuffer();
1022 // No metadata, missing data
1023 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1025 'userInfo' => $userInfo
1027 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1028 $this->assertSame( [
1029 [ LogLevel
::WARNING
, 'Session "{session}": Null provider and no metadata' ],
1030 ], $logger->getBuffer() );
1031 $logger->clearBuffer();
1033 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1034 'provider' => $provider,
1037 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1038 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1039 $this->assertInstanceOf( UserInfo
::class, $info->getUserInfo() );
1040 $this->assertTrue( $info->getUserInfo()->isVerified() );
1041 $this->assertTrue( $info->getUserInfo()->isAnon() );
1042 $this->assertFalse( $info->isIdSafe() );
1043 $this->assertSame( [], $logger->getBuffer() );
1045 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1046 'provider' => $provider2,
1049 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1050 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1051 $this->assertSame( [
1052 [ LogLevel
::INFO
, 'Session "{session}": No user provided and provider cannot set user' ]
1053 ], $logger->getBuffer() );
1054 $logger->clearBuffer();
1056 // Incomplete/bad metadata
1057 $this->store
->setRawSession( $id, true );
1058 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1059 $this->assertSame( [
1060 [ LogLevel
::WARNING
, 'Session "{session}": Bad data' ],
1061 ], $logger->getBuffer() );
1062 $logger->clearBuffer();
1064 $this->store
->setRawSession( $id, [ 'data' => [] ] );
1065 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1066 $this->assertSame( [
1067 [ LogLevel
::WARNING
, 'Session "{session}": Bad data structure' ],
1068 ], $logger->getBuffer() );
1069 $logger->clearBuffer();
1071 $this->store
->deleteSession( $id );
1072 $this->store
->setRawSession( $id, [ 'metadata' => $metadata ] );
1073 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1074 $this->assertSame( [
1075 [ LogLevel
::WARNING
, 'Session "{session}": Bad data structure' ],
1076 ], $logger->getBuffer() );
1077 $logger->clearBuffer();
1079 $this->store
->setRawSession( $id, [ 'metadata' => $metadata, 'data' => true ] );
1080 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1081 $this->assertSame( [
1082 [ LogLevel
::WARNING
, 'Session "{session}": Bad data structure' ],
1083 ], $logger->getBuffer() );
1084 $logger->clearBuffer();
1086 $this->store
->setRawSession( $id, [ 'metadata' => true, 'data' => [] ] );
1087 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1088 $this->assertSame( [
1089 [ LogLevel
::WARNING
, 'Session "{session}": Bad data structure' ],
1090 ], $logger->getBuffer() );
1091 $logger->clearBuffer();
1093 foreach ( $metadata as $key => $dummy ) {
1095 unset( $tmp[$key] );
1096 $this->store
->setRawSession( $id, [ 'metadata' => $tmp, 'data' => [] ] );
1097 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1098 $this->assertSame( [
1099 [ LogLevel
::WARNING
, 'Session "{session}": Bad metadata' ],
1100 ], $logger->getBuffer() );
1101 $logger->clearBuffer();
1104 // Basic usage with metadata
1105 $this->store
->setRawSession( $id, [ 'metadata' => $metadata, 'data' => [] ] );
1106 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1107 'provider' => $provider,
1109 'userInfo' => $userInfo
1111 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1112 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1113 $this->assertTrue( $info->isIdSafe() );
1114 $this->assertSame( [], $logger->getBuffer() );
1116 // Mismatched provider
1117 $this->store
->setSessionMeta( $id, [ 'provider' => 'Bad' ] +
$metadata );
1118 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1119 'provider' => $provider,
1121 'userInfo' => $userInfo
1123 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1124 $this->assertSame( [
1125 [ LogLevel
::WARNING
, 'Session "{session}": Wrong provider Bad !== Mock' ],
1126 ], $logger->getBuffer() );
1127 $logger->clearBuffer();
1130 $this->store
->setSessionMeta( $id, [ 'provider' => 'Bad' ] +
$metadata );
1131 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1133 'userInfo' => $userInfo
1135 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1136 $this->assertSame( [
1137 [ LogLevel
::WARNING
, 'Session "{session}": Unknown provider Bad' ],
1138 ], $logger->getBuffer() );
1139 $logger->clearBuffer();
1142 $this->store
->setSessionMeta( $id, $metadata );
1143 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1145 'userInfo' => $userInfo
1147 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1148 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1149 $this->assertTrue( $info->isIdSafe() );
1150 $this->assertSame( [], $logger->getBuffer() );
1152 // Bad user metadata
1153 $this->store
->setSessionMeta( $id, [ 'userId' => -1, 'userToken' => null ] +
$metadata );
1154 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1155 'provider' => $provider,
1158 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1159 $this->assertSame( [
1160 [ LogLevel
::ERROR
, 'Session "{session}": {exception}' ],
1161 ], $logger->getBuffer() );
1162 $logger->clearBuffer();
1164 $this->store
->setSessionMeta(
1165 $id, [ 'userId' => 0, 'userName' => '<X>', 'userToken' => null ] +
$metadata
1167 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1168 'provider' => $provider,
1171 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1172 $this->assertSame( [
1173 [ LogLevel
::ERROR
, 'Session "{session}": {exception}', ],
1174 ], $logger->getBuffer() );
1175 $logger->clearBuffer();
1177 // Mismatched user by ID
1178 $this->store
->setSessionMeta(
1179 $id, [ 'userId' => $userInfo->getId() +
1, 'userToken' => null ] +
$metadata
1181 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1182 'provider' => $provider,
1184 'userInfo' => $userInfo
1186 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1187 $this->assertSame( [
1188 [ LogLevel
::WARNING
, 'Session "{session}": User ID mismatch, {uid_a} !== {uid_b}' ],
1189 ], $logger->getBuffer() );
1190 $logger->clearBuffer();
1192 // Mismatched user by name
1193 $this->store
->setSessionMeta(
1194 $id, [ 'userId' => 0, 'userName' => 'X', 'userToken' => null ] +
$metadata
1196 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1197 'provider' => $provider,
1199 'userInfo' => $userInfo
1201 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1202 $this->assertSame( [
1203 [ LogLevel
::WARNING
, 'Session "{session}": User name mismatch, {uname_a} !== {uname_b}' ],
1204 ], $logger->getBuffer() );
1205 $logger->clearBuffer();
1207 // ID matches, name doesn't
1208 $this->store
->setSessionMeta(
1209 $id, [ 'userId' => $userInfo->getId(), 'userName' => 'X', 'userToken' => null ] +
$metadata
1211 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1212 'provider' => $provider,
1214 'userInfo' => $userInfo
1216 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1217 $this->assertSame( [
1220 'Session "{session}": User ID matched but name didn\'t (rename?), {uname_a} !== {uname_b}'
1222 ], $logger->getBuffer() );
1223 $logger->clearBuffer();
1225 // Mismatched anon user
1226 $this->store
->setSessionMeta(
1227 $id, [ 'userId' => 0, 'userName' => null, 'userToken' => null ] +
$metadata
1229 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1230 'provider' => $provider,
1232 'userInfo' => $userInfo
1234 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1235 $this->assertSame( [
1238 'Session "{session}": Metadata has an anonymous user, ' .
1239 'but a non-anon user was provided',
1241 ], $logger->getBuffer() );
1242 $logger->clearBuffer();
1244 // Lookup user by ID
1245 $this->store
->setSessionMeta( $id, [ 'userToken' => null ] +
$metadata );
1246 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1247 'provider' => $provider,
1250 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1251 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1252 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1253 $this->assertTrue( $info->isIdSafe() );
1254 $this->assertSame( [], $logger->getBuffer() );
1256 // Lookup user by name
1257 $this->store
->setSessionMeta(
1258 $id, [ 'userId' => 0, 'userName' => 'UTSysop', 'userToken' => null ] +
$metadata
1260 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1261 'provider' => $provider,
1264 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1265 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1266 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1267 $this->assertTrue( $info->isIdSafe() );
1268 $this->assertSame( [], $logger->getBuffer() );
1270 // Lookup anonymous user
1271 $this->store
->setSessionMeta(
1272 $id, [ 'userId' => 0, 'userName' => null, 'userToken' => null ] +
$metadata
1274 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1275 'provider' => $provider,
1278 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1279 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1280 $this->assertTrue( $info->getUserInfo()->isAnon() );
1281 $this->assertTrue( $info->isIdSafe() );
1282 $this->assertSame( [], $logger->getBuffer() );
1284 // Unverified user with metadata
1285 $this->store
->setSessionMeta( $id, $metadata );
1286 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1287 'provider' => $provider,
1289 'userInfo' => $unverifiedUserInfo
1291 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1292 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1293 $this->assertTrue( $info->getUserInfo()->isVerified() );
1294 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1295 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1296 $this->assertTrue( $info->isIdSafe() );
1297 $this->assertSame( [], $logger->getBuffer() );
1299 // Unverified user with metadata
1300 $this->store
->setSessionMeta( $id, $metadata );
1301 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1302 'provider' => $provider,
1304 'userInfo' => $unverifiedUserInfo
1306 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1307 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1308 $this->assertTrue( $info->getUserInfo()->isVerified() );
1309 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1310 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1311 $this->assertTrue( $info->isIdSafe() );
1312 $this->assertSame( [], $logger->getBuffer() );
1315 $this->store
->setSessionMeta( $id, [ 'userToken' => 'Bad' ] +
$metadata );
1316 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1317 'provider' => $provider,
1319 'userInfo' => $userInfo
1321 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1322 $this->assertSame( [
1323 [ LogLevel
::WARNING
, 'Session "{session}": User token mismatch' ],
1324 ], $logger->getBuffer() );
1325 $logger->clearBuffer();
1327 // Provider metadata
1328 $this->store
->setSessionMeta( $id, [ 'provider' => 'Mock2' ] +
$metadata );
1329 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1330 'provider' => $provider2,
1332 'userInfo' => $userInfo,
1333 'metadata' => [ 'Info' ],
1335 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1336 $this->assertSame( [ 'Info', 'changed' => true ], $info->getProviderMetadata() );
1337 $this->assertSame( [], $logger->getBuffer() );
1339 $this->store
->setSessionMeta( $id, [ 'providerMetadata' => [ 'Saved' ] ] +
$metadata );
1340 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1341 'provider' => $provider,
1343 'userInfo' => $userInfo,
1345 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1346 $this->assertSame( [ 'Saved' ], $info->getProviderMetadata() );
1347 $this->assertSame( [], $logger->getBuffer() );
1349 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1350 'provider' => $provider,
1352 'userInfo' => $userInfo,
1353 'metadata' => [ 'Info' ],
1355 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1356 $this->assertSame( [ 'Merged' ], $info->getProviderMetadata() );
1357 $this->assertSame( [], $logger->getBuffer() );
1359 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1360 'provider' => $provider,
1362 'userInfo' => $userInfo,
1363 'metadata' => [ 'Throw' ],
1365 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1366 $this->assertSame( [
1369 'Session "{session}": Metadata merge failed: {exception}',
1371 ], $logger->getBuffer() );
1372 $logger->clearBuffer();
1374 // Remember from session
1375 $this->store
->setSessionMeta( $id, $metadata );
1376 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1377 'provider' => $provider,
1380 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1381 $this->assertFalse( $info->wasRemembered() );
1382 $this->assertSame( [], $logger->getBuffer() );
1384 $this->store
->setSessionMeta( $id, [ 'remember' => true ] +
$metadata );
1385 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1386 'provider' => $provider,
1389 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1390 $this->assertTrue( $info->wasRemembered() );
1391 $this->assertSame( [], $logger->getBuffer() );
1393 $this->store
->setSessionMeta( $id, [ 'remember' => false ] +
$metadata );
1394 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1395 'provider' => $provider,
1397 'userInfo' => $userInfo
1399 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1400 $this->assertTrue( $info->wasRemembered() );
1401 $this->assertSame( [], $logger->getBuffer() );
1403 // forceHTTPS from session
1404 $this->store
->setSessionMeta( $id, $metadata );
1405 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1406 'provider' => $provider,
1408 'userInfo' => $userInfo
1410 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1411 $this->assertFalse( $info->forceHTTPS() );
1412 $this->assertSame( [], $logger->getBuffer() );
1414 $this->store
->setSessionMeta( $id, [ 'forceHTTPS' => true ] +
$metadata );
1415 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1416 'provider' => $provider,
1418 'userInfo' => $userInfo
1420 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1421 $this->assertTrue( $info->forceHTTPS() );
1422 $this->assertSame( [], $logger->getBuffer() );
1424 $this->store
->setSessionMeta( $id, [ 'forceHTTPS' => false ] +
$metadata );
1425 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1426 'provider' => $provider,
1428 'userInfo' => $userInfo,
1429 'forceHTTPS' => true
1431 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1432 $this->assertTrue( $info->forceHTTPS() );
1433 $this->assertSame( [], $logger->getBuffer() );
1435 // "Persist" flag from session
1436 $this->store
->setSessionMeta( $id, $metadata );
1437 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1438 'provider' => $provider,
1440 'userInfo' => $userInfo
1442 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1443 $this->assertFalse( $info->wasPersisted() );
1444 $this->assertSame( [], $logger->getBuffer() );
1446 $this->store
->setSessionMeta( $id, [ 'persisted' => true ] +
$metadata );
1447 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1448 'provider' => $provider,
1450 'userInfo' => $userInfo
1452 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1453 $this->assertTrue( $info->wasPersisted() );
1454 $this->assertSame( [], $logger->getBuffer() );
1456 $this->store
->setSessionMeta( $id, [ 'persisted' => false ] +
$metadata );
1457 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1458 'provider' => $provider,
1460 'userInfo' => $userInfo,
1463 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1464 $this->assertTrue( $info->wasPersisted() );
1465 $this->assertSame( [], $logger->getBuffer() );
1467 // Provider refreshSessionInfo() returning false
1468 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1469 'provider' => $provider3,
1471 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1472 $this->assertSame( [], $logger->getBuffer() );
1476 $data = [ 'foo' => 1 ];
1477 $this->store
->setSession( $id, [ 'metadata' => $metadata, 'data' => $data ] );
1478 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1479 'provider' => $provider,
1481 'userInfo' => $userInfo
1483 $this->mergeMwGlobalArrayValue( 'wgHooks', [
1484 'SessionCheckInfo' => [ function ( &$reason, $i, $r, $m, $d ) use (
1485 $info, $metadata, $data, $request, &$called
1487 $this->assertSame( $info->getId(), $i->getId() );
1488 $this->assertSame( $info->getProvider(), $i->getProvider() );
1489 $this->assertSame( $info->getUserInfo(), $i->getUserInfo() );
1490 $this->assertSame( $request, $r );
1491 $this->assertEquals( $metadata, $m );
1492 $this->assertEquals( $data, $d );
1497 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1498 $this->assertTrue( $called );
1499 $this->assertSame( [
1500 [ LogLevel
::WARNING
, 'Session "{session}": Hook aborted' ],
1501 ], $logger->getBuffer() );
1502 $logger->clearBuffer();
1503 $this->mergeMwGlobalArrayValue( 'wgHooks', [ 'SessionCheckInfo' => [] ] );
1505 // forceUse deletes bad backend data
1506 $this->store
->setSessionMeta( $id, [ 'userToken' => 'Bad' ] +
$metadata );
1507 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1508 'provider' => $provider,
1510 'userInfo' => $userInfo,
1513 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1514 $this->assertFalse( $this->store
->getSession( $id ) );
1515 $this->assertSame( [
1516 [ LogLevel
::WARNING
, 'Session "{session}": User token mismatch' ],
1517 ], $logger->getBuffer() );
1518 $logger->clearBuffer();