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 ( strpos( $m, 'SessionBackend ' ) === 0
38 ||
strpos( $m, 'SessionManager using store ' ) === 0
41 $this->store
= new TestBagOStuff();
43 return new SessionManager( [
44 'config' => $this->config
,
45 'logger' => $this->logger
,
46 'store' => $this->store
,
50 protected function objectCacheDef( $object ) {
51 return [ 'factory' => function () use ( $object ) {
56 public function testSingleton() {
57 $reset = TestUtils
::setSessionManagerSingleton( null );
59 $singleton = SessionManager
::singleton();
60 $this->assertInstanceOf( SessionManager
::class, $singleton );
61 $this->assertSame( $singleton, SessionManager
::singleton() );
64 public function testGetGlobalSession() {
65 $context = \RequestContext
::getMain();
67 if ( !PHPSessionHandler
::isInstalled() ) {
68 PHPSessionHandler
::install( SessionManager
::singleton() );
70 $rProp = new \
ReflectionProperty( PHPSessionHandler
::class, 'instance' );
71 $rProp->setAccessible( true );
72 $handler = TestingAccessWrapper
::newFromObject( $rProp->getValue() );
73 $oldEnable = $handler->enable
;
74 $reset[] = new \Wikimedia\
ScopedCallback( function () use ( $handler, $oldEnable ) {
75 if ( $handler->enable
) {
76 session_write_close();
78 $handler->enable
= $oldEnable;
80 $reset[] = TestUtils
::setSessionManagerSingleton( $this->getManager() );
82 $handler->enable
= true;
83 $request = new \
FauxRequest();
84 $context->setRequest( $request );
85 $id = $request->getSession()->getId();
87 session_write_close();
89 $session = SessionManager
::getGlobalSession();
90 $this->assertSame( $id, $session->getId() );
92 session_id( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
93 $session = SessionManager
::getGlobalSession();
94 $this->assertSame( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', $session->getId() );
95 $this->assertSame( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', $request->getSession()->getId() );
97 session_write_close();
98 $handler->enable
= false;
99 $request = new \
FauxRequest();
100 $context->setRequest( $request );
101 $id = $request->getSession()->getId();
104 $session = SessionManager
::getGlobalSession();
105 $this->assertSame( $id, $session->getId() );
107 session_id( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
108 $session = SessionManager
::getGlobalSession();
109 $this->assertSame( $id, $session->getId() );
110 $this->assertSame( $id, $request->getSession()->getId() );
113 public function testConstructor() {
114 $manager = TestingAccessWrapper
::newFromObject( $this->getManager() );
115 $this->assertSame( $this->config
, $manager->config
);
116 $this->assertSame( $this->logger
, $manager->logger
);
117 $this->assertSame( $this->store
, $manager->store
);
119 $manager = TestingAccessWrapper
::newFromObject( new SessionManager() );
120 $this->assertSame( \RequestContext
::getMain()->getConfig(), $manager->config
);
122 $manager = TestingAccessWrapper
::newFromObject( new SessionManager( [
123 'config' => $this->config
,
125 $this->assertSame( \ObjectCache
::$instances['testSessionStore'], $manager->store
);
128 'config' => '$options[\'config\'] must be an instance of Config',
129 'logger' => '$options[\'logger\'] must be an instance of LoggerInterface',
130 'store' => '$options[\'store\'] must be an instance of BagOStuff',
131 ] as $key => $error ) {
133 new SessionManager( [ $key => new \stdClass
] );
134 $this->fail( 'Expected exception not thrown' );
135 } catch ( \InvalidArgumentException
$ex ) {
136 $this->assertSame( $error, $ex->getMessage() );
141 public function testGetSessionForRequest() {
142 $manager = $this->getManager();
143 $request = new \
FauxRequest();
144 $request->unpersist1
= false;
145 $request->unpersist2
= false;
149 $idEmpty = 'empty-session-------------------';
151 $providerBuilder = $this->getMockBuilder( \DummySessionProvider
::class )
153 [ 'provideSessionInfo', 'newSessionInfo', '__toString', 'describe', 'unpersistSession' ]
156 $provider1 = $providerBuilder->getMock();
157 $provider1->expects( $this->any() )->method( 'provideSessionInfo' )
158 ->with( $this->identicalTo( $request ) )
159 ->will( $this->returnCallback( function ( $request ) {
160 return $request->info1
;
162 $provider1->expects( $this->any() )->method( 'newSessionInfo' )
163 ->will( $this->returnCallback( function () use ( $idEmpty, $provider1 ) {
164 return new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
165 'provider' => $provider1,
171 $provider1->expects( $this->any() )->method( '__toString' )
172 ->will( $this->returnValue( 'Provider1' ) );
173 $provider1->expects( $this->any() )->method( 'describe' )
174 ->will( $this->returnValue( '#1 sessions' ) );
175 $provider1->expects( $this->any() )->method( 'unpersistSession' )
176 ->will( $this->returnCallback( function ( $request ) {
177 $request->unpersist1
= true;
180 $provider2 = $providerBuilder->getMock();
181 $provider2->expects( $this->any() )->method( 'provideSessionInfo' )
182 ->with( $this->identicalTo( $request ) )
183 ->will( $this->returnCallback( function ( $request ) {
184 return $request->info2
;
186 $provider2->expects( $this->any() )->method( '__toString' )
187 ->will( $this->returnValue( 'Provider2' ) );
188 $provider2->expects( $this->any() )->method( 'describe' )
189 ->will( $this->returnValue( '#2 sessions' ) );
190 $provider2->expects( $this->any() )->method( 'unpersistSession' )
191 ->will( $this->returnCallback( function ( $request ) {
192 $request->unpersist2
= true;
195 $this->config
->set( 'SessionProviders', [
196 $this->objectCacheDef( $provider1 ),
197 $this->objectCacheDef( $provider2 ),
200 // No provider returns info
201 $request->info1
= null;
202 $request->info2
= null;
203 $session = $manager->getSessionForRequest( $request );
204 $this->assertInstanceOf( Session
::class, $session );
205 $this->assertSame( $idEmpty, $session->getId() );
206 $this->assertFalse( $request->unpersist1
);
207 $this->assertFalse( $request->unpersist2
);
209 // Both providers return info, picks best one
210 $request->info1
= new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, [
211 'provider' => $provider1,
212 'id' => ( $id1 = $manager->generateSessionId() ),
216 $request->info2
= new SessionInfo( SessionInfo
::MIN_PRIORITY +
2, [
217 'provider' => $provider2,
218 'id' => ( $id2 = $manager->generateSessionId() ),
222 $session = $manager->getSessionForRequest( $request );
223 $this->assertInstanceOf( Session
::class, $session );
224 $this->assertSame( $id2, $session->getId() );
225 $this->assertFalse( $request->unpersist1
);
226 $this->assertFalse( $request->unpersist2
);
228 $request->info1
= new SessionInfo( SessionInfo
::MIN_PRIORITY +
2, [
229 'provider' => $provider1,
230 'id' => ( $id1 = $manager->generateSessionId() ),
234 $request->info2
= new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, [
235 'provider' => $provider2,
236 'id' => ( $id2 = $manager->generateSessionId() ),
240 $session = $manager->getSessionForRequest( $request );
241 $this->assertInstanceOf( Session
::class, $session );
242 $this->assertSame( $id1, $session->getId() );
243 $this->assertFalse( $request->unpersist1
);
244 $this->assertFalse( $request->unpersist2
);
247 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, [
248 'provider' => $provider1,
249 'id' => ( $id1 = $manager->generateSessionId() ),
251 'userInfo' => UserInfo
::newAnonymous(),
254 $request->info2
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, [
255 'provider' => $provider2,
256 'id' => ( $id2 = $manager->generateSessionId() ),
258 'userInfo' => UserInfo
::newAnonymous(),
262 $manager->getSessionForRequest( $request );
263 $this->fail( 'Expcected exception not thrown' );
264 } catch ( SessionOverflowException
$ex ) {
265 $this->assertStringStartsWith(
266 'Multiple sessions for this request tied for top priority: ',
269 $this->assertCount( 2, $ex->getSessionInfos() );
270 $this->assertContains( $request->info1
, $ex->getSessionInfos() );
271 $this->assertContains( $request->info2
, $ex->getSessionInfos() );
273 $this->assertFalse( $request->unpersist1
);
274 $this->assertFalse( $request->unpersist2
);
277 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, [
278 'provider' => $provider2,
279 'id' => ( $id1 = $manager->generateSessionId() ),
283 $request->info2
= null;
285 $manager->getSessionForRequest( $request );
286 $this->fail( 'Expcected exception not thrown' );
287 } catch ( \UnexpectedValueException
$ex ) {
289 'Provider1 returned session info for a different provider: ' . $request->info1
,
293 $this->assertFalse( $request->unpersist1
);
294 $this->assertFalse( $request->unpersist2
);
296 // Unusable session info
297 $this->logger
->setCollect( true );
298 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, [
299 'provider' => $provider1,
300 'id' => ( $id1 = $manager->generateSessionId() ),
302 'userInfo' => UserInfo
::newFromName( 'UTSysop', false ),
305 $request->info2
= new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
306 'provider' => $provider2,
307 'id' => ( $id2 = $manager->generateSessionId() ),
311 $session = $manager->getSessionForRequest( $request );
312 $this->assertInstanceOf( Session
::class, $session );
313 $this->assertSame( $id2, $session->getId() );
314 $this->logger
->setCollect( false );
315 $this->assertTrue( $request->unpersist1
);
316 $this->assertFalse( $request->unpersist2
);
317 $request->unpersist1
= false;
319 $this->logger
->setCollect( true );
320 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, [
321 'provider' => $provider1,
322 'id' => ( $id1 = $manager->generateSessionId() ),
326 $request->info2
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, [
327 'provider' => $provider2,
328 'id' => ( $id2 = $manager->generateSessionId() ),
330 'userInfo' => UserInfo
::newFromName( 'UTSysop', false ),
333 $session = $manager->getSessionForRequest( $request );
334 $this->assertInstanceOf( Session
::class, $session );
335 $this->assertSame( $id1, $session->getId() );
336 $this->logger
->setCollect( false );
337 $this->assertFalse( $request->unpersist1
);
338 $this->assertTrue( $request->unpersist2
);
339 $request->unpersist2
= false;
341 // Unpersisted session ID
342 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, [
343 'provider' => $provider1,
344 'id' => ( $id1 = $manager->generateSessionId() ),
345 'persisted' => false,
346 'userInfo' => UserInfo
::newFromName( 'UTSysop', true ),
349 $request->info2
= null;
350 $session = $manager->getSessionForRequest( $request );
351 $this->assertInstanceOf( Session
::class, $session );
352 $this->assertSame( $id1, $session->getId() );
353 $this->assertTrue( $request->unpersist1
); // The saving of the session does it
354 $this->assertFalse( $request->unpersist2
);
356 $this->assertTrue( $session->isPersistent(), 'sanity check' );
359 public function testGetSessionById() {
360 $manager = $this->getManager();
362 $manager->getSessionById( 'bad' );
363 $this->fail( 'Expected exception not thrown' );
364 } catch ( \InvalidArgumentException
$ex ) {
365 $this->assertSame( 'Invalid session ID', $ex->getMessage() );
368 // Unknown session ID
369 $id = $manager->generateSessionId();
370 $session = $manager->getSessionById( $id, true );
371 $this->assertInstanceOf( Session
::class, $session );
372 $this->assertSame( $id, $session->getId() );
374 $id = $manager->generateSessionId();
375 $this->assertNull( $manager->getSessionById( $id, false ) );
377 // Known but unloadable session ID
378 $this->logger
->setCollect( true );
379 $id = $manager->generateSessionId();
380 $this->store
->setSession( $id, [ 'metadata' => [
381 'userId' => User
::idFromName( 'UTSysop' ),
382 'userToken' => 'bad',
385 $this->assertNull( $manager->getSessionById( $id, true ) );
386 $this->assertNull( $manager->getSessionById( $id, false ) );
387 $this->logger
->setCollect( false );
390 $this->store
->setSession( $id, [] );
391 $session = $manager->getSessionById( $id, false );
392 $this->assertInstanceOf( Session
::class, $session );
393 $this->assertSame( $id, $session->getId() );
395 // Store isn't checked if the session is already loaded
396 $this->store
->setSession( $id, [ 'metadata' => [
397 'userId' => User
::idFromName( 'UTSysop' ),
398 'userToken' => 'bad',
400 $session2 = $manager->getSessionById( $id, false );
401 $this->assertInstanceOf( Session
::class, $session2 );
402 $this->assertSame( $id, $session2->getId() );
403 unset( $session, $session2 );
404 $this->logger
->setCollect( true );
405 $this->assertNull( $manager->getSessionById( $id, true ) );
406 $this->logger
->setCollect( false );
408 // Failure to create an empty session
409 $manager = $this->getManager();
410 $provider = $this->getMockBuilder( \DummySessionProvider
::class )
411 ->setMethods( [ 'provideSessionInfo', 'newSessionInfo', '__toString' ] )
413 $provider->expects( $this->any() )->method( 'provideSessionInfo' )
414 ->will( $this->returnValue( null ) );
415 $provider->expects( $this->any() )->method( 'newSessionInfo' )
416 ->will( $this->returnValue( null ) );
417 $provider->expects( $this->any() )->method( '__toString' )
418 ->will( $this->returnValue( 'MockProvider' ) );
419 $this->config
->set( 'SessionProviders', [
420 $this->objectCacheDef( $provider ),
422 $this->logger
->setCollect( true );
423 $this->assertNull( $manager->getSessionById( $id, true ) );
424 $this->logger
->setCollect( false );
426 [ LogLevel
::ERROR
, 'Failed to create empty session: {exception}' ]
427 ], $this->logger
->getBuffer() );
430 public function testGetEmptySession() {
431 $manager = $this->getManager();
432 $pmanager = TestingAccessWrapper
::newFromObject( $manager );
433 $request = new \
FauxRequest();
435 $providerBuilder = $this->getMockBuilder( \DummySessionProvider
::class )
436 ->setMethods( [ 'provideSessionInfo', 'newSessionInfo', '__toString' ] );
442 $provider1 = $providerBuilder->getMock();
443 $provider1->expects( $this->any() )->method( 'provideSessionInfo' )
444 ->will( $this->returnValue( null ) );
445 $provider1->expects( $this->any() )->method( 'newSessionInfo' )
446 ->with( $this->callback( function ( $id ) use ( &$expectId ) {
447 return $id === $expectId;
449 ->will( $this->returnCallback( function () use ( &$info1 ) {
452 $provider1->expects( $this->any() )->method( '__toString' )
453 ->will( $this->returnValue( 'MockProvider1' ) );
455 $provider2 = $providerBuilder->getMock();
456 $provider2->expects( $this->any() )->method( 'provideSessionInfo' )
457 ->will( $this->returnValue( null ) );
458 $provider2->expects( $this->any() )->method( 'newSessionInfo' )
459 ->with( $this->callback( function ( $id ) use ( &$expectId ) {
460 return $id === $expectId;
462 ->will( $this->returnCallback( function () use ( &$info2 ) {
465 $provider1->expects( $this->any() )->method( '__toString' )
466 ->will( $this->returnValue( 'MockProvider2' ) );
468 $this->config
->set( 'SessionProviders', [
469 $this->objectCacheDef( $provider1 ),
470 $this->objectCacheDef( $provider2 ),
478 $manager->getEmptySession();
479 $this->fail( 'Expected exception not thrown' );
480 } catch ( \UnexpectedValueException
$ex ) {
482 'No provider could provide an empty session!',
489 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
490 'provider' => $provider1,
491 'id' => 'empty---------------------------',
496 $session = $manager->getEmptySession();
497 $this->assertInstanceOf( Session
::class, $session );
498 $this->assertSame( 'empty---------------------------', $session->getId() );
501 $expectId = 'expected------------------------';
502 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
503 'provider' => $provider1,
509 $session = $pmanager->getEmptySessionInternal( null, $expectId );
510 $this->assertInstanceOf( Session
::class, $session );
511 $this->assertSame( $expectId, $session->getId() );
514 $expectId = 'expected-----------------------2';
515 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
516 'provider' => $provider1,
517 'id' => "un$expectId",
523 $pmanager->getEmptySessionInternal( null, $expectId );
524 $this->fail( 'Expected exception not thrown' );
525 } catch ( \UnexpectedValueException
$ex ) {
527 'MockProvider1 returned empty session info with a wrong id: ' .
528 "un$expectId != $expectId",
534 $expectId = 'expected-----------------------2';
535 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
536 'provider' => $provider1,
542 $pmanager->getEmptySessionInternal( null, $expectId );
543 $this->fail( 'Expected exception not thrown' );
544 } catch ( \UnexpectedValueException
$ex ) {
546 'MockProvider1 returned empty session info with id flagged unsafe',
553 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
554 'provider' => $provider2,
555 'id' => 'empty---------------------------',
561 $manager->getEmptySession();
562 $this->fail( 'Expected exception not thrown' );
563 } catch ( \UnexpectedValueException
$ex ) {
565 'MockProvider1 returned an empty session info for a different provider: ' . $info1,
570 // Highest priority wins
572 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, [
573 'provider' => $provider1,
574 'id' => 'empty1--------------------------',
578 $info2 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
579 'provider' => $provider2,
580 'id' => 'empty2--------------------------',
584 $session = $manager->getEmptySession();
585 $this->assertInstanceOf( Session
::class, $session );
586 $this->assertSame( 'empty1--------------------------', $session->getId() );
589 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, [
590 'provider' => $provider1,
591 'id' => 'empty1--------------------------',
595 $info2 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
2, [
596 'provider' => $provider2,
597 'id' => 'empty2--------------------------',
601 $session = $manager->getEmptySession();
602 $this->assertInstanceOf( Session
::class, $session );
603 $this->assertSame( 'empty2--------------------------', $session->getId() );
605 // Tied priorities throw an exception
607 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
608 'provider' => $provider1,
609 'id' => 'empty1--------------------------',
611 'userInfo' => UserInfo
::newAnonymous(),
614 $info2 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
615 'provider' => $provider2,
616 'id' => 'empty2--------------------------',
618 'userInfo' => UserInfo
::newAnonymous(),
622 $manager->getEmptySession();
623 $this->fail( 'Expected exception not thrown' );
624 } catch ( \UnexpectedValueException
$ex ) {
625 $this->assertStringStartsWith(
626 'Multiple empty sessions tied for top priority: ',
633 $pmanager->getEmptySessionInternal( null, 'bad' );
634 $this->fail( 'Expected exception not thrown' );
635 } catch ( \InvalidArgumentException
$ex ) {
636 $this->assertSame( 'Invalid session ID', $ex->getMessage() );
639 // Session already exists
640 $expectId = 'expected-----------------------3';
641 $this->store
->setSessionMeta( $expectId, [
642 'provider' => 'MockProvider2',
648 $pmanager->getEmptySessionInternal( null, $expectId );
649 $this->fail( 'Expected exception not thrown' );
650 } catch ( \InvalidArgumentException
$ex ) {
651 $this->assertSame( 'Session ID already exists', $ex->getMessage() );
655 public function testInvalidateSessionsForUser() {
656 $user = User
::newFromName( 'UTSysop' );
657 $manager = $this->getManager();
659 $providerBuilder = $this->getMockBuilder( \DummySessionProvider
::class )
660 ->setMethods( [ 'invalidateSessionsForUser', '__toString' ] );
662 $provider1 = $providerBuilder->getMock();
663 $provider1->expects( $this->once() )->method( 'invalidateSessionsForUser' )
664 ->with( $this->identicalTo( $user ) );
665 $provider1->expects( $this->any() )->method( '__toString' )
666 ->will( $this->returnValue( 'MockProvider1' ) );
668 $provider2 = $providerBuilder->getMock();
669 $provider2->expects( $this->once() )->method( 'invalidateSessionsForUser' )
670 ->with( $this->identicalTo( $user ) );
671 $provider2->expects( $this->any() )->method( '__toString' )
672 ->will( $this->returnValue( 'MockProvider2' ) );
674 $this->config
->set( 'SessionProviders', [
675 $this->objectCacheDef( $provider1 ),
676 $this->objectCacheDef( $provider2 ),
679 $oldToken = $user->getToken( true );
680 $manager->invalidateSessionsForUser( $user );
681 $this->assertNotEquals( $oldToken, $user->getToken() );
684 public function testGetVaryHeaders() {
685 $manager = $this->getManager();
687 $providerBuilder = $this->getMockBuilder( \DummySessionProvider
::class )
688 ->setMethods( [ 'getVaryHeaders', '__toString' ] );
690 $provider1 = $providerBuilder->getMock();
691 $provider1->expects( $this->once() )->method( 'getVaryHeaders' )
692 ->will( $this->returnValue( [
694 'Bar' => [ 'X', 'Bar1' ],
697 $provider1->expects( $this->any() )->method( '__toString' )
698 ->will( $this->returnValue( 'MockProvider1' ) );
700 $provider2 = $providerBuilder->getMock();
701 $provider2->expects( $this->once() )->method( 'getVaryHeaders' )
702 ->will( $this->returnValue( [
704 'Bar' => [ 'X', 'Bar2' ],
705 'Quux' => [ 'Quux' ],
707 $provider2->expects( $this->any() )->method( '__toString' )
708 ->will( $this->returnValue( 'MockProvider2' ) );
710 $this->config
->set( 'SessionProviders', [
711 $this->objectCacheDef( $provider1 ),
712 $this->objectCacheDef( $provider2 ),
722 $this->assertEquals( $expect, $manager->getVaryHeaders() );
724 // Again, to ensure it's cached
725 $this->assertEquals( $expect, $manager->getVaryHeaders() );
728 public function testGetVaryCookies() {
729 $manager = $this->getManager();
731 $providerBuilder = $this->getMockBuilder( \DummySessionProvider
::class )
732 ->setMethods( [ 'getVaryCookies', '__toString' ] );
734 $provider1 = $providerBuilder->getMock();
735 $provider1->expects( $this->once() )->method( 'getVaryCookies' )
736 ->will( $this->returnValue( [ 'Foo', 'Bar' ] ) );
737 $provider1->expects( $this->any() )->method( '__toString' )
738 ->will( $this->returnValue( 'MockProvider1' ) );
740 $provider2 = $providerBuilder->getMock();
741 $provider2->expects( $this->once() )->method( 'getVaryCookies' )
742 ->will( $this->returnValue( [ 'Foo', 'Baz' ] ) );
743 $provider2->expects( $this->any() )->method( '__toString' )
744 ->will( $this->returnValue( 'MockProvider2' ) );
746 $this->config
->set( 'SessionProviders', [
747 $this->objectCacheDef( $provider1 ),
748 $this->objectCacheDef( $provider2 ),
751 $expect = [ 'Foo', 'Bar', 'Baz' ];
753 $this->assertEquals( $expect, $manager->getVaryCookies() );
755 // Again, to ensure it's cached
756 $this->assertEquals( $expect, $manager->getVaryCookies() );
759 public function testGetProviders() {
760 $realManager = $this->getManager();
761 $manager = TestingAccessWrapper
::newFromObject( $realManager );
763 $this->config
->set( 'SessionProviders', [
764 [ 'class' => \DummySessionProvider
::class ],
766 $providers = $manager->getProviders();
767 $this->assertArrayHasKey( 'DummySessionProvider', $providers );
768 $provider = TestingAccessWrapper
::newFromObject( $providers['DummySessionProvider'] );
769 $this->assertSame( $manager->logger
, $provider->logger
);
770 $this->assertSame( $manager->config
, $provider->config
);
771 $this->assertSame( $realManager, $provider->getManager() );
773 $this->config
->set( 'SessionProviders', [
774 [ 'class' => \DummySessionProvider
::class ],
775 [ 'class' => \DummySessionProvider
::class ],
777 $manager->sessionProviders
= null;
779 $manager->getProviders();
780 $this->fail( 'Expected exception not thrown' );
781 } catch ( \UnexpectedValueException
$ex ) {
783 'Duplicate provider name "DummySessionProvider"',
789 public function testShutdown() {
790 $manager = TestingAccessWrapper
::newFromObject( $this->getManager() );
791 $manager->setLogger( new \Psr\Log\
NullLogger() );
793 $mock = $this->getMockBuilder( stdClass
::class )
794 ->setMethods( [ 'shutdown' ] )->getMock();
795 $mock->expects( $this->once() )->method( 'shutdown' );
797 $manager->allSessionBackends
= [ $mock ];
798 $manager->shutdown();
801 public function testGetSessionFromInfo() {
802 $manager = TestingAccessWrapper
::newFromObject( $this->getManager() );
803 $request = new \
FauxRequest();
805 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
807 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
808 'provider' => $manager->getProvider( 'DummySessionProvider' ),
811 'userInfo' => UserInfo
::newFromName( 'UTSysop', true ),
814 TestingAccessWrapper
::newFromObject( $info )->idIsSafe
= true;
815 $session1 = TestingAccessWrapper
::newFromObject(
816 $manager->getSessionFromInfo( $info, $request )
818 $session2 = TestingAccessWrapper
::newFromObject(
819 $manager->getSessionFromInfo( $info, $request )
822 $this->assertSame( $session1->backend
, $session2->backend
);
823 $this->assertNotEquals( $session1->index
, $session2->index
);
824 $this->assertSame( $session1->getSessionId(), $session2->getSessionId() );
825 $this->assertSame( $id, $session1->getId() );
827 TestingAccessWrapper
::newFromObject( $info )->idIsSafe
= false;
828 $session3 = $manager->getSessionFromInfo( $info, $request );
829 $this->assertNotSame( $id, $session3->getId() );
832 public function testBackendRegistration() {
833 $manager = $this->getManager();
835 $session = $manager->getSessionForRequest( new \FauxRequest
);
836 $backend = TestingAccessWrapper
::newFromObject( $session )->backend
;
837 $sessionId = $session->getSessionId();
838 $id = (string)$sessionId;
840 $this->assertSame( $sessionId, $manager->getSessionById( $id, true )->getSessionId() );
842 $manager->changeBackendId( $backend );
843 $this->assertSame( $sessionId, $session->getSessionId() );
844 $this->assertNotEquals( $id, (string)$sessionId );
845 $id = (string)$sessionId;
847 $this->assertSame( $sessionId, $manager->getSessionById( $id, true )->getSessionId() );
849 // Destruction of the session here causes the backend to be deregistered
853 $manager->changeBackendId( $backend );
854 $this->fail( 'Expected exception not thrown' );
855 } catch ( \InvalidArgumentException
$ex ) {
857 'Backend was not registered with this SessionManager', $ex->getMessage()
862 $manager->deregisterSessionBackend( $backend );
863 $this->fail( 'Expected exception not thrown' );
864 } catch ( \InvalidArgumentException
$ex ) {
866 'Backend was not registered with this SessionManager', $ex->getMessage()
870 $session = $manager->getSessionById( $id, true );
871 $this->assertSame( $sessionId, $session->getSessionId() );
874 public function testGenerateSessionId() {
875 $manager = $this->getManager();
877 $id = $manager->generateSessionId();
878 $this->assertTrue( SessionManager
::validateSessionId( $id ), "Generated ID: $id" );
881 public function testPreventSessionsForUser() {
882 $manager = $this->getManager();
884 $providerBuilder = $this->getMockBuilder( \DummySessionProvider
::class )
885 ->setMethods( [ 'preventSessionsForUser', '__toString' ] );
887 $provider1 = $providerBuilder->getMock();
888 $provider1->expects( $this->once() )->method( 'preventSessionsForUser' )
889 ->with( $this->equalTo( 'UTSysop' ) );
890 $provider1->expects( $this->any() )->method( '__toString' )
891 ->will( $this->returnValue( 'MockProvider1' ) );
893 $this->config
->set( 'SessionProviders', [
894 $this->objectCacheDef( $provider1 ),
897 $this->assertFalse( $manager->isUserSessionPrevented( 'UTSysop' ) );
898 $manager->preventSessionsForUser( 'UTSysop' );
899 $this->assertTrue( $manager->isUserSessionPrevented( 'UTSysop' ) );
902 public function testLoadSessionInfoFromStore() {
903 $manager = $this->getManager();
904 $logger = new \
TestLogger( true );
905 $manager->setLogger( $logger );
906 $request = new \
FauxRequest();
908 // TestingAccessWrapper can't handle methods with reference arguments, sigh.
909 $rClass = new \
ReflectionClass( $manager );
910 $rMethod = $rClass->getMethod( 'loadSessionInfoFromStore' );
911 $rMethod->setAccessible( true );
912 $loadSessionInfoFromStore = function ( &$info ) use ( $rMethod, $manager, $request ) {
913 return $rMethod->invokeArgs( $manager, [ &$info, $request ] );
916 $userInfo = UserInfo
::newFromName( 'UTSysop', true );
917 $unverifiedUserInfo = UserInfo
::newFromName( 'UTSysop', false );
919 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
921 'userId' => $userInfo->getId(),
922 'userName' => $userInfo->getName(),
923 'userToken' => $userInfo->getToken( true ),
924 'provider' => 'Mock',
927 $builder = $this->getMockBuilder( SessionProvider
::class )
928 ->setMethods( [ '__toString', 'mergeMetadata', 'refreshSessionInfo' ] );
930 $provider = $builder->getMockForAbstractClass();
931 $provider->setManager( $manager );
932 $provider->expects( $this->any() )->method( 'persistsSessionId' )
933 ->will( $this->returnValue( true ) );
934 $provider->expects( $this->any() )->method( 'canChangeUser' )
935 ->will( $this->returnValue( true ) );
936 $provider->expects( $this->any() )->method( 'refreshSessionInfo' )
937 ->will( $this->returnValue( true ) );
938 $provider->expects( $this->any() )->method( '__toString' )
939 ->will( $this->returnValue( 'Mock' ) );
940 $provider->expects( $this->any() )->method( 'mergeMetadata' )
941 ->will( $this->returnCallback( function ( $a, $b ) {
942 if ( $b === [ 'Throw' ] ) {
943 throw new MetadataMergeException( 'no merge!' );
948 $provider2 = $builder->getMockForAbstractClass();
949 $provider2->setManager( $manager );
950 $provider2->expects( $this->any() )->method( 'persistsSessionId' )
951 ->will( $this->returnValue( false ) );
952 $provider2->expects( $this->any() )->method( 'canChangeUser' )
953 ->will( $this->returnValue( false ) );
954 $provider2->expects( $this->any() )->method( '__toString' )
955 ->will( $this->returnValue( 'Mock2' ) );
956 $provider2->expects( $this->any() )->method( 'refreshSessionInfo' )
957 ->will( $this->returnCallback( function ( $info, $request, &$metadata ) {
958 $metadata['changed'] = true;
962 $provider3 = $builder->getMockForAbstractClass();
963 $provider3->setManager( $manager );
964 $provider3->expects( $this->any() )->method( 'persistsSessionId' )
965 ->will( $this->returnValue( true ) );
966 $provider3->expects( $this->any() )->method( 'canChangeUser' )
967 ->will( $this->returnValue( true ) );
968 $provider3->expects( $this->once() )->method( 'refreshSessionInfo' )
969 ->will( $this->returnValue( false ) );
970 $provider3->expects( $this->any() )->method( '__toString' )
971 ->will( $this->returnValue( 'Mock3' ) );
973 TestingAccessWrapper
::newFromObject( $manager )->sessionProviders
= [
974 (string)$provider => $provider,
975 (string)$provider2 => $provider2,
976 (string)$provider3 => $provider3,
979 // No metadata, basic usage
980 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
981 'provider' => $provider,
983 'userInfo' => $userInfo
985 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
986 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
987 $this->assertFalse( $info->isIdSafe() );
988 $this->assertSame( [], $logger->getBuffer() );
990 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
991 'provider' => $provider,
992 'userInfo' => $userInfo
994 $this->assertTrue( $info->isIdSafe(), 'sanity check' );
995 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
996 $this->assertTrue( $info->isIdSafe() );
997 $this->assertSame( [], $logger->getBuffer() );
999 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1000 'provider' => $provider2,
1002 'userInfo' => $userInfo
1004 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1005 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1006 $this->assertTrue( $info->isIdSafe() );
1007 $this->assertSame( [], $logger->getBuffer() );
1009 // Unverified user, no metadata
1010 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1011 'provider' => $provider,
1013 'userInfo' => $unverifiedUserInfo
1015 $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
1016 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1017 $this->assertSame( [
1020 'Session "{session}": Unverified user provided and no metadata to auth it',
1022 ], $logger->getBuffer() );
1023 $logger->clearBuffer();
1025 // No metadata, missing data
1026 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1028 'userInfo' => $userInfo
1030 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1031 $this->assertSame( [
1032 [ LogLevel
::WARNING
, 'Session "{session}": Null provider and no metadata' ],
1033 ], $logger->getBuffer() );
1034 $logger->clearBuffer();
1036 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1037 'provider' => $provider,
1040 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1041 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1042 $this->assertInstanceOf( UserInfo
::class, $info->getUserInfo() );
1043 $this->assertTrue( $info->getUserInfo()->isVerified() );
1044 $this->assertTrue( $info->getUserInfo()->isAnon() );
1045 $this->assertFalse( $info->isIdSafe() );
1046 $this->assertSame( [], $logger->getBuffer() );
1048 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1049 'provider' => $provider2,
1052 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1053 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1054 $this->assertSame( [
1055 [ LogLevel
::INFO
, 'Session "{session}": No user provided and provider cannot set user' ]
1056 ], $logger->getBuffer() );
1057 $logger->clearBuffer();
1059 // Incomplete/bad metadata
1060 $this->store
->setRawSession( $id, true );
1061 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1062 $this->assertSame( [
1063 [ LogLevel
::WARNING
, 'Session "{session}": Bad data' ],
1064 ], $logger->getBuffer() );
1065 $logger->clearBuffer();
1067 $this->store
->setRawSession( $id, [ 'data' => [] ] );
1068 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1069 $this->assertSame( [
1070 [ LogLevel
::WARNING
, 'Session "{session}": Bad data structure' ],
1071 ], $logger->getBuffer() );
1072 $logger->clearBuffer();
1074 $this->store
->deleteSession( $id );
1075 $this->store
->setRawSession( $id, [ 'metadata' => $metadata ] );
1076 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1077 $this->assertSame( [
1078 [ LogLevel
::WARNING
, 'Session "{session}": Bad data structure' ],
1079 ], $logger->getBuffer() );
1080 $logger->clearBuffer();
1082 $this->store
->setRawSession( $id, [ 'metadata' => $metadata, 'data' => true ] );
1083 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1084 $this->assertSame( [
1085 [ LogLevel
::WARNING
, 'Session "{session}": Bad data structure' ],
1086 ], $logger->getBuffer() );
1087 $logger->clearBuffer();
1089 $this->store
->setRawSession( $id, [ 'metadata' => true, 'data' => [] ] );
1090 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1091 $this->assertSame( [
1092 [ LogLevel
::WARNING
, 'Session "{session}": Bad data structure' ],
1093 ], $logger->getBuffer() );
1094 $logger->clearBuffer();
1096 foreach ( $metadata as $key => $dummy ) {
1098 unset( $tmp[$key] );
1099 $this->store
->setRawSession( $id, [ 'metadata' => $tmp, 'data' => [] ] );
1100 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1101 $this->assertSame( [
1102 [ LogLevel
::WARNING
, 'Session "{session}": Bad metadata' ],
1103 ], $logger->getBuffer() );
1104 $logger->clearBuffer();
1107 // Basic usage with metadata
1108 $this->store
->setRawSession( $id, [ 'metadata' => $metadata, 'data' => [] ] );
1109 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1110 'provider' => $provider,
1112 'userInfo' => $userInfo
1114 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1115 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1116 $this->assertTrue( $info->isIdSafe() );
1117 $this->assertSame( [], $logger->getBuffer() );
1119 // Mismatched provider
1120 $this->store
->setSessionMeta( $id, [ 'provider' => 'Bad' ] +
$metadata );
1121 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1122 'provider' => $provider,
1124 'userInfo' => $userInfo
1126 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1127 $this->assertSame( [
1128 [ LogLevel
::WARNING
, 'Session "{session}": Wrong provider Bad !== Mock' ],
1129 ], $logger->getBuffer() );
1130 $logger->clearBuffer();
1133 $this->store
->setSessionMeta( $id, [ 'provider' => 'Bad' ] +
$metadata );
1134 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1136 'userInfo' => $userInfo
1138 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1139 $this->assertSame( [
1140 [ LogLevel
::WARNING
, 'Session "{session}": Unknown provider Bad' ],
1141 ], $logger->getBuffer() );
1142 $logger->clearBuffer();
1145 $this->store
->setSessionMeta( $id, $metadata );
1146 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1148 'userInfo' => $userInfo
1150 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1151 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1152 $this->assertTrue( $info->isIdSafe() );
1153 $this->assertSame( [], $logger->getBuffer() );
1155 // Bad user metadata
1156 $this->store
->setSessionMeta( $id, [ 'userId' => -1, 'userToken' => null ] +
$metadata );
1157 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1158 'provider' => $provider,
1161 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1162 $this->assertSame( [
1163 [ LogLevel
::ERROR
, 'Session "{session}": {exception}' ],
1164 ], $logger->getBuffer() );
1165 $logger->clearBuffer();
1167 $this->store
->setSessionMeta(
1168 $id, [ 'userId' => 0, 'userName' => '<X>', 'userToken' => null ] +
$metadata
1170 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1171 'provider' => $provider,
1174 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1175 $this->assertSame( [
1176 [ LogLevel
::ERROR
, 'Session "{session}": {exception}', ],
1177 ], $logger->getBuffer() );
1178 $logger->clearBuffer();
1180 // Mismatched user by ID
1181 $this->store
->setSessionMeta(
1182 $id, [ 'userId' => $userInfo->getId() +
1, 'userToken' => null ] +
$metadata
1184 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1185 'provider' => $provider,
1187 'userInfo' => $userInfo
1189 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1190 $this->assertSame( [
1191 [ LogLevel
::WARNING
, 'Session "{session}": User ID mismatch, {uid_a} !== {uid_b}' ],
1192 ], $logger->getBuffer() );
1193 $logger->clearBuffer();
1195 // Mismatched user by name
1196 $this->store
->setSessionMeta(
1197 $id, [ 'userId' => 0, 'userName' => 'X', 'userToken' => null ] +
$metadata
1199 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1200 'provider' => $provider,
1202 'userInfo' => $userInfo
1204 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1205 $this->assertSame( [
1206 [ LogLevel
::WARNING
, 'Session "{session}": User name mismatch, {uname_a} !== {uname_b}' ],
1207 ], $logger->getBuffer() );
1208 $logger->clearBuffer();
1210 // ID matches, name doesn't
1211 $this->store
->setSessionMeta(
1212 $id, [ 'userId' => $userInfo->getId(), 'userName' => 'X', 'userToken' => null ] +
$metadata
1214 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1215 'provider' => $provider,
1217 'userInfo' => $userInfo
1219 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1220 $this->assertSame( [
1223 'Session "{session}": User ID matched but name didn\'t (rename?), {uname_a} !== {uname_b}'
1225 ], $logger->getBuffer() );
1226 $logger->clearBuffer();
1228 // Mismatched anon user
1229 $this->store
->setSessionMeta(
1230 $id, [ 'userId' => 0, 'userName' => null, 'userToken' => null ] +
$metadata
1232 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1233 'provider' => $provider,
1235 'userInfo' => $userInfo
1237 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1238 $this->assertSame( [
1241 'Session "{session}": Metadata has an anonymous user, ' .
1242 'but a non-anon user was provided',
1244 ], $logger->getBuffer() );
1245 $logger->clearBuffer();
1247 // Lookup user by ID
1248 $this->store
->setSessionMeta( $id, [ 'userToken' => null ] +
$metadata );
1249 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1250 'provider' => $provider,
1253 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1254 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1255 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1256 $this->assertTrue( $info->isIdSafe() );
1257 $this->assertSame( [], $logger->getBuffer() );
1259 // Lookup user by name
1260 $this->store
->setSessionMeta(
1261 $id, [ 'userId' => 0, 'userName' => 'UTSysop', 'userToken' => null ] +
$metadata
1263 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1264 'provider' => $provider,
1267 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1268 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1269 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1270 $this->assertTrue( $info->isIdSafe() );
1271 $this->assertSame( [], $logger->getBuffer() );
1273 // Lookup anonymous user
1274 $this->store
->setSessionMeta(
1275 $id, [ 'userId' => 0, 'userName' => null, 'userToken' => null ] +
$metadata
1277 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1278 'provider' => $provider,
1281 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1282 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1283 $this->assertTrue( $info->getUserInfo()->isAnon() );
1284 $this->assertTrue( $info->isIdSafe() );
1285 $this->assertSame( [], $logger->getBuffer() );
1287 // Unverified user with metadata
1288 $this->store
->setSessionMeta( $id, $metadata );
1289 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1290 'provider' => $provider,
1292 'userInfo' => $unverifiedUserInfo
1294 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1295 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1296 $this->assertTrue( $info->getUserInfo()->isVerified() );
1297 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1298 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1299 $this->assertTrue( $info->isIdSafe() );
1300 $this->assertSame( [], $logger->getBuffer() );
1302 // Unverified user with metadata
1303 $this->store
->setSessionMeta( $id, $metadata );
1304 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1305 'provider' => $provider,
1307 'userInfo' => $unverifiedUserInfo
1309 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1310 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1311 $this->assertTrue( $info->getUserInfo()->isVerified() );
1312 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1313 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1314 $this->assertTrue( $info->isIdSafe() );
1315 $this->assertSame( [], $logger->getBuffer() );
1318 $this->store
->setSessionMeta( $id, [ 'userToken' => 'Bad' ] +
$metadata );
1319 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1320 'provider' => $provider,
1322 'userInfo' => $userInfo
1324 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1325 $this->assertSame( [
1326 [ LogLevel
::WARNING
, 'Session "{session}": User token mismatch' ],
1327 ], $logger->getBuffer() );
1328 $logger->clearBuffer();
1330 // Provider metadata
1331 $this->store
->setSessionMeta( $id, [ 'provider' => 'Mock2' ] +
$metadata );
1332 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1333 'provider' => $provider2,
1335 'userInfo' => $userInfo,
1336 'metadata' => [ 'Info' ],
1338 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1339 $this->assertSame( [ 'Info', 'changed' => true ], $info->getProviderMetadata() );
1340 $this->assertSame( [], $logger->getBuffer() );
1342 $this->store
->setSessionMeta( $id, [ 'providerMetadata' => [ 'Saved' ] ] +
$metadata );
1343 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1344 'provider' => $provider,
1346 'userInfo' => $userInfo,
1348 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1349 $this->assertSame( [ 'Saved' ], $info->getProviderMetadata() );
1350 $this->assertSame( [], $logger->getBuffer() );
1352 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1353 'provider' => $provider,
1355 'userInfo' => $userInfo,
1356 'metadata' => [ 'Info' ],
1358 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1359 $this->assertSame( [ 'Merged' ], $info->getProviderMetadata() );
1360 $this->assertSame( [], $logger->getBuffer() );
1362 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1363 'provider' => $provider,
1365 'userInfo' => $userInfo,
1366 'metadata' => [ 'Throw' ],
1368 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1369 $this->assertSame( [
1372 'Session "{session}": Metadata merge failed: {exception}',
1374 ], $logger->getBuffer() );
1375 $logger->clearBuffer();
1377 // Remember from session
1378 $this->store
->setSessionMeta( $id, $metadata );
1379 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1380 'provider' => $provider,
1383 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1384 $this->assertFalse( $info->wasRemembered() );
1385 $this->assertSame( [], $logger->getBuffer() );
1387 $this->store
->setSessionMeta( $id, [ 'remember' => true ] +
$metadata );
1388 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1389 'provider' => $provider,
1392 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1393 $this->assertTrue( $info->wasRemembered() );
1394 $this->assertSame( [], $logger->getBuffer() );
1396 $this->store
->setSessionMeta( $id, [ 'remember' => false ] +
$metadata );
1397 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1398 'provider' => $provider,
1400 'userInfo' => $userInfo
1402 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1403 $this->assertTrue( $info->wasRemembered() );
1404 $this->assertSame( [], $logger->getBuffer() );
1406 // forceHTTPS from session
1407 $this->store
->setSessionMeta( $id, $metadata );
1408 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1409 'provider' => $provider,
1411 'userInfo' => $userInfo
1413 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1414 $this->assertFalse( $info->forceHTTPS() );
1415 $this->assertSame( [], $logger->getBuffer() );
1417 $this->store
->setSessionMeta( $id, [ 'forceHTTPS' => true ] +
$metadata );
1418 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1419 'provider' => $provider,
1421 'userInfo' => $userInfo
1423 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1424 $this->assertTrue( $info->forceHTTPS() );
1425 $this->assertSame( [], $logger->getBuffer() );
1427 $this->store
->setSessionMeta( $id, [ 'forceHTTPS' => false ] +
$metadata );
1428 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1429 'provider' => $provider,
1431 'userInfo' => $userInfo,
1432 'forceHTTPS' => true
1434 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1435 $this->assertTrue( $info->forceHTTPS() );
1436 $this->assertSame( [], $logger->getBuffer() );
1438 // "Persist" flag from session
1439 $this->store
->setSessionMeta( $id, $metadata );
1440 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1441 'provider' => $provider,
1443 'userInfo' => $userInfo
1445 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1446 $this->assertFalse( $info->wasPersisted() );
1447 $this->assertSame( [], $logger->getBuffer() );
1449 $this->store
->setSessionMeta( $id, [ 'persisted' => true ] +
$metadata );
1450 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1451 'provider' => $provider,
1453 'userInfo' => $userInfo
1455 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1456 $this->assertTrue( $info->wasPersisted() );
1457 $this->assertSame( [], $logger->getBuffer() );
1459 $this->store
->setSessionMeta( $id, [ 'persisted' => false ] +
$metadata );
1460 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1461 'provider' => $provider,
1463 'userInfo' => $userInfo,
1466 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1467 $this->assertTrue( $info->wasPersisted() );
1468 $this->assertSame( [], $logger->getBuffer() );
1470 // Provider refreshSessionInfo() returning false
1471 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1472 'provider' => $provider3,
1474 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1475 $this->assertSame( [], $logger->getBuffer() );
1479 $data = [ 'foo' => 1 ];
1480 $this->store
->setSession( $id, [ 'metadata' => $metadata, 'data' => $data ] );
1481 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1482 'provider' => $provider,
1484 'userInfo' => $userInfo
1486 $this->mergeMwGlobalArrayValue( 'wgHooks', [
1487 'SessionCheckInfo' => [ function ( &$reason, $i, $r, $m, $d ) use (
1488 $info, $metadata, $data, $request, &$called
1490 $this->assertSame( $info->getId(), $i->getId() );
1491 $this->assertSame( $info->getProvider(), $i->getProvider() );
1492 $this->assertSame( $info->getUserInfo(), $i->getUserInfo() );
1493 $this->assertSame( $request, $r );
1494 $this->assertEquals( $metadata, $m );
1495 $this->assertEquals( $data, $d );
1500 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1501 $this->assertTrue( $called );
1502 $this->assertSame( [
1503 [ LogLevel
::WARNING
, 'Session "{session}": Hook aborted' ],
1504 ], $logger->getBuffer() );
1505 $logger->clearBuffer();
1506 $this->mergeMwGlobalArrayValue( 'wgHooks', [ 'SessionCheckInfo' => [] ] );
1508 // forceUse deletes bad backend data
1509 $this->store
->setSessionMeta( $id, [ 'userToken' => 'Bad' ] +
$metadata );
1510 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
1511 'provider' => $provider,
1513 'userInfo' => $userInfo,
1516 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1517 $this->assertFalse( $this->store
->getSession( $id ) );
1518 $this->assertSame( [
1519 [ LogLevel
::WARNING
, 'Session "{session}": User token mismatch' ],
1520 ], $logger->getBuffer() );
1521 $logger->clearBuffer();