3 namespace MediaWiki\Session
;
12 * @covers MediaWiki\Session\SessionManager
14 class SessionManagerTest
extends MediaWikiTestCase
{
16 protected $config, $logger, $store;
18 protected function getManager() {
19 \ObjectCache
::$instances['testSessionStore'] = new TestBagOStuff();
20 $this->config
= new \
HashConfig( array(
21 'LanguageCode' => 'en',
22 'SessionCacheType' => 'testSessionStore',
23 'ObjectCacheSessionExpiry' => 100,
24 'SessionProviders' => array(
25 array( 'class' => 'DummySessionProvider' ),
28 $this->logger
= new \
TestLogger( false, function ( $m ) {
29 return substr( $m, 0, 15 ) === 'SessionBackend ' ?
null : $m;
31 $this->store
= new TestBagOStuff();
33 return new SessionManager( array(
34 'config' => $this->config
,
35 'logger' => $this->logger
,
36 'store' => $this->store
,
40 protected function objectCacheDef( $object ) {
41 return array( 'factory' => function () use ( $object ) {
46 public function testSingleton() {
47 $reset = TestUtils
::setSessionManagerSingleton( null );
49 $singleton = SessionManager
::singleton();
50 $this->assertInstanceOf( 'MediaWiki\\Session\\SessionManager', $singleton );
51 $this->assertSame( $singleton, SessionManager
::singleton() );
54 public function testGetGlobalSession() {
55 $context = \RequestContext
::getMain();
57 if ( !PHPSessionHandler
::isInstalled() ) {
58 PHPSessionHandler
::install( SessionManager
::singleton() );
60 $rProp = new \
ReflectionProperty( 'MediaWiki\\Session\\PHPSessionHandler', 'instance' );
61 $rProp->setAccessible( true );
62 $handler = \TestingAccessWrapper
::newFromObject( $rProp->getValue() );
63 $oldEnable = $handler->enable
;
64 $reset[] = new \
ScopedCallback( function () use ( $handler, $oldEnable ) {
65 if ( $handler->enable
) {
66 session_write_close();
68 $handler->enable
= $oldEnable;
70 $reset[] = TestUtils
::setSessionManagerSingleton( $this->getManager() );
72 $handler->enable
= true;
73 $request = new \
FauxRequest();
74 $context->setRequest( $request );
75 $id = $request->getSession()->getId();
78 $session = SessionManager
::getGlobalSession();
79 $this->assertSame( $id, $session->getId() );
81 session_id( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
82 $session = SessionManager
::getGlobalSession();
83 $this->assertSame( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', $session->getId() );
84 $this->assertSame( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', $request->getSession()->getId() );
86 session_write_close();
87 $handler->enable
= false;
88 $request = new \
FauxRequest();
89 $context->setRequest( $request );
90 $id = $request->getSession()->getId();
93 $session = SessionManager
::getGlobalSession();
94 $this->assertSame( $id, $session->getId() );
96 session_id( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
97 $session = SessionManager
::getGlobalSession();
98 $this->assertSame( $id, $session->getId() );
99 $this->assertSame( $id, $request->getSession()->getId() );
102 public function testConstructor() {
103 $manager = \TestingAccessWrapper
::newFromObject( $this->getManager() );
104 $this->assertSame( $this->config
, $manager->config
);
105 $this->assertSame( $this->logger
, $manager->logger
);
106 $this->assertSame( $this->store
, $manager->store
);
108 $manager = \TestingAccessWrapper
::newFromObject( new SessionManager() );
109 $this->assertSame( \RequestContext
::getMain()->getConfig(), $manager->config
);
111 $manager = \TestingAccessWrapper
::newFromObject( new SessionManager( array(
112 'config' => $this->config
,
114 $this->assertSame( \ObjectCache
::$instances['testSessionStore'], $manager->store
);
117 'config' => '$options[\'config\'] must be an instance of Config',
118 'logger' => '$options[\'logger\'] must be an instance of LoggerInterface',
119 'store' => '$options[\'store\'] must be an instance of BagOStuff',
120 ) as $key => $error ) {
122 new SessionManager( array( $key => new \stdClass
) );
123 $this->fail( 'Expected exception not thrown' );
124 } catch ( \InvalidArgumentException
$ex ) {
125 $this->assertSame( $error, $ex->getMessage() );
130 public function testGetSessionForRequest() {
131 $manager = $this->getManager();
132 $request = new \
FauxRequest();
136 $idEmpty = 'empty-session-------------------';
138 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
140 array( 'provideSessionInfo', 'newSessionInfo', '__toString', 'describe' )
143 $provider1 = $providerBuilder->getMock();
144 $provider1->expects( $this->any() )->method( 'provideSessionInfo' )
145 ->with( $this->identicalTo( $request ) )
146 ->will( $this->returnCallback( function ( $request ) {
147 return $request->info1
;
149 $provider1->expects( $this->any() )->method( 'newSessionInfo' )
150 ->will( $this->returnCallback( function () use ( $idEmpty, $provider1 ) {
151 return new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
152 'provider' => $provider1,
158 $provider1->expects( $this->any() )->method( '__toString' )
159 ->will( $this->returnValue( 'Provider1' ) );
160 $provider1->expects( $this->any() )->method( 'describe' )
161 ->will( $this->returnValue( '#1 sessions' ) );
163 $provider2 = $providerBuilder->getMock();
164 $provider2->expects( $this->any() )->method( 'provideSessionInfo' )
165 ->with( $this->identicalTo( $request ) )
166 ->will( $this->returnCallback( function ( $request ) {
167 return $request->info2
;
169 $provider2->expects( $this->any() )->method( '__toString' )
170 ->will( $this->returnValue( 'Provider2' ) );
171 $provider2->expects( $this->any() )->method( 'describe' )
172 ->will( $this->returnValue( '#2 sessions' ) );
174 $this->config
->set( 'SessionProviders', array(
175 $this->objectCacheDef( $provider1 ),
176 $this->objectCacheDef( $provider2 ),
179 // No provider returns info
180 $request->info1
= null;
181 $request->info2
= null;
182 $session = $manager->getSessionForRequest( $request );
183 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
184 $this->assertSame( $idEmpty, $session->getId() );
186 // Both providers return info, picks best one
187 $request->info1
= new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, array(
188 'provider' => $provider1,
189 'id' => ( $id1 = $manager->generateSessionId() ),
193 $request->info2
= new SessionInfo( SessionInfo
::MIN_PRIORITY +
2, array(
194 'provider' => $provider2,
195 'id' => ( $id2 = $manager->generateSessionId() ),
199 $session = $manager->getSessionForRequest( $request );
200 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
201 $this->assertSame( $id2, $session->getId() );
203 $request->info1
= new SessionInfo( SessionInfo
::MIN_PRIORITY +
2, array(
204 'provider' => $provider1,
205 'id' => ( $id1 = $manager->generateSessionId() ),
209 $request->info2
= new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, array(
210 'provider' => $provider2,
211 'id' => ( $id2 = $manager->generateSessionId() ),
215 $session = $manager->getSessionForRequest( $request );
216 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
217 $this->assertSame( $id1, $session->getId() );
220 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, array(
221 'provider' => $provider1,
222 'id' => ( $id1 = $manager->generateSessionId() ),
224 'userInfo' => UserInfo
::newAnonymous(),
227 $request->info2
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, array(
228 'provider' => $provider2,
229 'id' => ( $id2 = $manager->generateSessionId() ),
231 'userInfo' => UserInfo
::newAnonymous(),
235 $manager->getSessionForRequest( $request );
236 $this->fail( 'Expcected exception not thrown' );
237 } catch ( \OverFlowException
$ex ) {
238 $this->assertStringStartsWith(
239 'Multiple sessions for this request tied for top priority: ',
242 $this->assertCount( 2, $ex->sessionInfos
);
243 $this->assertContains( $request->info1
, $ex->sessionInfos
);
244 $this->assertContains( $request->info2
, $ex->sessionInfos
);
248 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, array(
249 'provider' => $provider2,
250 'id' => ( $id1 = $manager->generateSessionId() ),
254 $request->info2
= null;
256 $manager->getSessionForRequest( $request );
257 $this->fail( 'Expcected exception not thrown' );
258 } catch ( \UnexpectedValueException
$ex ) {
260 'Provider1 returned session info for a different provider: ' . $request->info1
,
265 // Unusable session info
266 $this->logger
->setCollect( true );
267 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, array(
268 'provider' => $provider1,
269 'id' => ( $id1 = $manager->generateSessionId() ),
271 'userInfo' => UserInfo
::newFromName( 'UTSysop', false ),
274 $request->info2
= new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
275 'provider' => $provider2,
276 'id' => ( $id2 = $manager->generateSessionId() ),
280 $session = $manager->getSessionForRequest( $request );
281 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
282 $this->assertSame( $id2, $session->getId() );
283 $this->logger
->setCollect( false );
285 // Unpersisted session ID
286 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, array(
287 'provider' => $provider1,
288 'id' => ( $id1 = $manager->generateSessionId() ),
289 'persisted' => false,
290 'userInfo' => UserInfo
::newFromName( 'UTSysop', true ),
293 $request->info2
= null;
294 $session = $manager->getSessionForRequest( $request );
295 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
296 $this->assertSame( $id1, $session->getId() );
298 $this->assertTrue( $session->isPersistent(), 'sanity check' );
301 public function testGetSessionById() {
302 $manager = $this->getManager();
304 $manager->getSessionById( 'bad' );
305 $this->fail( 'Expected exception not thrown' );
306 } catch ( \InvalidArgumentException
$ex ) {
307 $this->assertSame( 'Invalid session ID', $ex->getMessage() );
310 // Unknown session ID
311 $id = $manager->generateSessionId();
312 $session = $manager->getSessionById( $id, true );
313 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
314 $this->assertSame( $id, $session->getId() );
316 $id = $manager->generateSessionId();
317 $this->assertNull( $manager->getSessionById( $id, false ) );
319 // Known but unloadable session ID
320 $this->logger
->setCollect( true );
321 $id = $manager->generateSessionId();
322 $this->store
->setSession( $id, array( 'metadata' => array(
323 'userId' => User
::idFromName( 'UTSysop' ),
324 'userToken' => 'bad',
327 $this->assertNull( $manager->getSessionById( $id, true ) );
328 $this->assertNull( $manager->getSessionById( $id, false ) );
329 $this->logger
->setCollect( false );
332 $this->store
->setSession( $id, array() );
333 $session = $manager->getSessionById( $id, false );
334 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
335 $this->assertSame( $id, $session->getId() );
338 public function testGetEmptySession() {
339 $manager = $this->getManager();
340 $pmanager = \TestingAccessWrapper
::newFromObject( $manager );
341 $request = new \
FauxRequest();
343 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
344 ->setMethods( array( 'provideSessionInfo', 'newSessionInfo', '__toString' ) );
350 $provider1 = $providerBuilder->getMock();
351 $provider1->expects( $this->any() )->method( 'provideSessionInfo' )
352 ->will( $this->returnValue( null ) );
353 $provider1->expects( $this->any() )->method( 'newSessionInfo' )
354 ->with( $this->callback( function ( $id ) use ( &$expectId ) {
355 return $id === $expectId;
357 ->will( $this->returnCallback( function () use ( &$info1 ) {
360 $provider1->expects( $this->any() )->method( '__toString' )
361 ->will( $this->returnValue( 'MockProvider1' ) );
363 $provider2 = $providerBuilder->getMock();
364 $provider2->expects( $this->any() )->method( 'provideSessionInfo' )
365 ->will( $this->returnValue( null ) );
366 $provider2->expects( $this->any() )->method( 'newSessionInfo' )
367 ->with( $this->callback( function ( $id ) use ( &$expectId ) {
368 return $id === $expectId;
370 ->will( $this->returnCallback( function () use ( &$info2 ) {
373 $provider1->expects( $this->any() )->method( '__toString' )
374 ->will( $this->returnValue( 'MockProvider2' ) );
376 $this->config
->set( 'SessionProviders', array(
377 $this->objectCacheDef( $provider1 ),
378 $this->objectCacheDef( $provider2 ),
386 $manager->getEmptySession();
387 $this->fail( 'Expected exception not thrown' );
388 } catch ( \UnexpectedValueException
$ex ) {
390 'No provider could provide an empty session!',
397 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
398 'provider' => $provider1,
399 'id' => 'empty---------------------------',
404 $session = $manager->getEmptySession();
405 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
406 $this->assertSame( 'empty---------------------------', $session->getId() );
409 $expectId = 'expected------------------------';
410 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
411 'provider' => $provider1,
417 $session = $pmanager->getEmptySessionInternal( null, $expectId );
418 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
419 $this->assertSame( $expectId, $session->getId() );
422 $expectId = 'expected-----------------------2';
423 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
424 'provider' => $provider1,
425 'id' => "un$expectId",
431 $pmanager->getEmptySessionInternal( null, $expectId );
432 $this->fail( 'Expected exception not thrown' );
433 } catch ( \UnexpectedValueException
$ex ) {
435 'MockProvider1 returned empty session info with a wrong id: ' .
436 "un$expectId != $expectId",
442 $expectId = 'expected-----------------------2';
443 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
444 'provider' => $provider1,
450 $pmanager->getEmptySessionInternal( null, $expectId );
451 $this->fail( 'Expected exception not thrown' );
452 } catch ( \UnexpectedValueException
$ex ) {
454 'MockProvider1 returned empty session info with id flagged unsafe',
461 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
462 'provider' => $provider2,
463 'id' => 'empty---------------------------',
469 $manager->getEmptySession();
470 $this->fail( 'Expected exception not thrown' );
471 } catch ( \UnexpectedValueException
$ex ) {
473 'MockProvider1 returned an empty session info for a different provider: ' . $info1,
478 // Highest priority wins
480 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, array(
481 'provider' => $provider1,
482 'id' => 'empty1--------------------------',
486 $info2 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
487 'provider' => $provider2,
488 'id' => 'empty2--------------------------',
492 $session = $manager->getEmptySession();
493 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
494 $this->assertSame( 'empty1--------------------------', $session->getId() );
497 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, array(
498 'provider' => $provider1,
499 'id' => 'empty1--------------------------',
503 $info2 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
2, array(
504 'provider' => $provider2,
505 'id' => 'empty2--------------------------',
509 $session = $manager->getEmptySession();
510 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
511 $this->assertSame( 'empty2--------------------------', $session->getId() );
513 // Tied priorities throw an exception
515 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
516 'provider' => $provider1,
517 'id' => 'empty1--------------------------',
519 'userInfo' => UserInfo
::newAnonymous(),
522 $info2 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
523 'provider' => $provider2,
524 'id' => 'empty2--------------------------',
526 'userInfo' => UserInfo
::newAnonymous(),
530 $manager->getEmptySession();
531 $this->fail( 'Expected exception not thrown' );
532 } catch ( \UnexpectedValueException
$ex ) {
533 $this->assertStringStartsWith(
534 'Multiple empty sessions tied for top priority: ',
541 $pmanager->getEmptySessionInternal( null, 'bad' );
542 $this->fail( 'Expected exception not thrown' );
543 } catch ( \InvalidArgumentException
$ex ) {
544 $this->assertSame( 'Invalid session ID', $ex->getMessage() );
547 // Session already exists
548 $expectId = 'expected-----------------------3';
549 $this->store
->setSessionMeta( $expectId, array(
550 'provider' => 'MockProvider2',
556 $pmanager->getEmptySessionInternal( null, $expectId );
557 $this->fail( 'Expected exception not thrown' );
558 } catch ( \InvalidArgumentException
$ex ) {
559 $this->assertSame( 'Session ID already exists', $ex->getMessage() );
563 public function testGetVaryHeaders() {
564 $manager = $this->getManager();
566 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
567 ->setMethods( array( 'getVaryHeaders', '__toString' ) );
569 $provider1 = $providerBuilder->getMock();
570 $provider1->expects( $this->once() )->method( 'getVaryHeaders' )
571 ->will( $this->returnValue( array(
573 'Bar' => array( 'X', 'Bar1' ),
576 $provider1->expects( $this->any() )->method( '__toString' )
577 ->will( $this->returnValue( 'MockProvider1' ) );
579 $provider2 = $providerBuilder->getMock();
580 $provider2->expects( $this->once() )->method( 'getVaryHeaders' )
581 ->will( $this->returnValue( array(
583 'Bar' => array( 'X', 'Bar2' ),
584 'Quux' => array( 'Quux' ),
586 $provider2->expects( $this->any() )->method( '__toString' )
587 ->will( $this->returnValue( 'MockProvider2' ) );
589 $this->config
->set( 'SessionProviders', array(
590 $this->objectCacheDef( $provider1 ),
591 $this->objectCacheDef( $provider2 ),
596 'Bar' => array( 'X', 'Bar1', 3 => 'Bar2' ),
597 'Quux' => array( 'Quux' ),
601 $this->assertEquals( $expect, $manager->getVaryHeaders() );
603 // Again, to ensure it's cached
604 $this->assertEquals( $expect, $manager->getVaryHeaders() );
607 public function testGetVaryCookies() {
608 $manager = $this->getManager();
610 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
611 ->setMethods( array( 'getVaryCookies', '__toString' ) );
613 $provider1 = $providerBuilder->getMock();
614 $provider1->expects( $this->once() )->method( 'getVaryCookies' )
615 ->will( $this->returnValue( array( 'Foo', 'Bar' ) ) );
616 $provider1->expects( $this->any() )->method( '__toString' )
617 ->will( $this->returnValue( 'MockProvider1' ) );
619 $provider2 = $providerBuilder->getMock();
620 $provider2->expects( $this->once() )->method( 'getVaryCookies' )
621 ->will( $this->returnValue( array( 'Foo', 'Baz' ) ) );
622 $provider2->expects( $this->any() )->method( '__toString' )
623 ->will( $this->returnValue( 'MockProvider2' ) );
625 $this->config
->set( 'SessionProviders', array(
626 $this->objectCacheDef( $provider1 ),
627 $this->objectCacheDef( $provider2 ),
630 $expect = array( 'Foo', 'Bar', 'Baz' );
632 $this->assertEquals( $expect, $manager->getVaryCookies() );
634 // Again, to ensure it's cached
635 $this->assertEquals( $expect, $manager->getVaryCookies() );
638 public function testGetProviders() {
639 $realManager = $this->getManager();
640 $manager = \TestingAccessWrapper
::newFromObject( $realManager );
642 $this->config
->set( 'SessionProviders', array(
643 array( 'class' => 'DummySessionProvider' ),
645 $providers = $manager->getProviders();
646 $this->assertArrayHasKey( 'DummySessionProvider', $providers );
647 $provider = \TestingAccessWrapper
::newFromObject( $providers['DummySessionProvider'] );
648 $this->assertSame( $manager->logger
, $provider->logger
);
649 $this->assertSame( $manager->config
, $provider->config
);
650 $this->assertSame( $realManager, $provider->getManager() );
652 $this->config
->set( 'SessionProviders', array(
653 array( 'class' => 'DummySessionProvider' ),
654 array( 'class' => 'DummySessionProvider' ),
656 $manager->sessionProviders
= null;
658 $manager->getProviders();
659 $this->fail( 'Expected exception not thrown' );
660 } catch ( \UnexpectedValueException
$ex ) {
662 'Duplicate provider name "DummySessionProvider"',
668 public function testShutdown() {
669 $manager = \TestingAccessWrapper
::newFromObject( $this->getManager() );
670 $manager->setLogger( new \Psr\Log\
NullLogger() );
672 $mock = $this->getMock( 'stdClass', array( 'save' ) );
673 $mock->expects( $this->once() )->method( 'save' );
675 $manager->allSessionBackends
= array( $mock );
676 $manager->shutdown();
679 public function testGetSessionFromInfo() {
680 $manager = \TestingAccessWrapper
::newFromObject( $this->getManager() );
681 $request = new \
FauxRequest();
683 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
685 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
686 'provider' => $manager->getProvider( 'DummySessionProvider' ),
689 'userInfo' => UserInfo
::newFromName( 'UTSysop', true ),
692 \TestingAccessWrapper
::newFromObject( $info )->idIsSafe
= true;
693 $session1 = \TestingAccessWrapper
::newFromObject(
694 $manager->getSessionFromInfo( $info, $request )
696 $session2 = \TestingAccessWrapper
::newFromObject(
697 $manager->getSessionFromInfo( $info, $request )
700 $this->assertSame( $session1->backend
, $session2->backend
);
701 $this->assertNotEquals( $session1->index
, $session2->index
);
702 $this->assertSame( $session1->getSessionId(), $session2->getSessionId() );
703 $this->assertSame( $id, $session1->getId() );
705 \TestingAccessWrapper
::newFromObject( $info )->idIsSafe
= false;
706 $session3 = $manager->getSessionFromInfo( $info, $request );
707 $this->assertNotSame( $id, $session3->getId() );
710 public function testBackendRegistration() {
711 $manager = $this->getManager();
713 $session = $manager->getSessionForRequest( new \FauxRequest
);
714 $backend = \TestingAccessWrapper
::newFromObject( $session )->backend
;
715 $sessionId = $session->getSessionId();
716 $id = (string)$sessionId;
718 $this->assertSame( $sessionId, $manager->getSessionById( $id, true )->getSessionId() );
720 $manager->changeBackendId( $backend );
721 $this->assertSame( $sessionId, $session->getSessionId() );
722 $this->assertNotEquals( $id, (string)$sessionId );
723 $id = (string)$sessionId;
725 $this->assertSame( $sessionId, $manager->getSessionById( $id, true )->getSessionId() );
727 // Destruction of the session here causes the backend to be deregistered
731 $manager->changeBackendId( $backend );
732 $this->fail( 'Expected exception not thrown' );
733 } catch ( \InvalidArgumentException
$ex ) {
735 'Backend was not registered with this SessionManager', $ex->getMessage()
740 $manager->deregisterSessionBackend( $backend );
741 $this->fail( 'Expected exception not thrown' );
742 } catch ( \InvalidArgumentException
$ex ) {
744 'Backend was not registered with this SessionManager', $ex->getMessage()
748 $session = $manager->getSessionById( $id, true );
749 $this->assertSame( $sessionId, $session->getSessionId() );
752 public function testGenerateSessionId() {
753 $manager = $this->getManager();
755 $id = $manager->generateSessionId();
756 $this->assertTrue( SessionManager
::validateSessionId( $id ), "Generated ID: $id" );
759 public function testAutoCreateUser() {
760 global $wgGroupPermissions;
764 \ObjectCache
::$instances[__METHOD__
] = new TestBagOStuff();
765 $this->setMwGlobals( array( 'wgMainCacheType' => __METHOD__
) );
767 $this->stashMwGlobals( array( 'wgGroupPermissions' ) );
768 $wgGroupPermissions['*']['createaccount'] = true;
769 $wgGroupPermissions['*']['autocreateaccount'] = false;
771 // Replace the global singleton with one configured for testing
772 $manager = $this->getManager();
773 $reset = TestUtils
::setSessionManagerSingleton( $manager );
775 $logger = new \
TestLogger( true, function ( $m ) {
776 if ( substr( $m, 0, 15 ) === 'SessionBackend ' ) {
780 $m = str_replace( 'MediaWiki\Session\SessionManager::autoCreateUser: ', '', $m );
781 $m = preg_replace( '/ - from: .*$/', ' - from: XXX', $m );
784 $manager->setLogger( $logger );
786 $session = SessionManager
::getGlobalSession();
788 // Can't create an already-existing user
789 $user = User
::newFromName( 'UTSysop' );
790 $id = $user->getId();
791 $this->assertFalse( $manager->autoCreateUser( $user ) );
792 $this->assertSame( $id, $user->getId() );
793 $this->assertSame( 'UTSysop', $user->getName() );
794 $this->assertSame( array(), $logger->getBuffer() );
795 $logger->clearBuffer();
797 // Sanity check that creation works at all
798 $user = User
::newFromName( 'UTSessionAutoCreate1' );
799 $this->assertSame( 0, $user->getId(), 'sanity check' );
800 $this->assertTrue( $manager->autoCreateUser( $user ) );
801 $this->assertNotEquals( 0, $user->getId() );
802 $this->assertSame( 'UTSessionAutoCreate1', $user->getName() );
804 $user->getId(), User
::idFromName( 'UTSessionAutoCreate1', User
::READ_LATEST
)
806 $this->assertSame( array(
807 array( LogLevel
::INFO
, 'creating new user (UTSessionAutoCreate1) - from: XXX' ),
808 ), $logger->getBuffer() );
809 $logger->clearBuffer();
811 // Check lack of permissions
812 $wgGroupPermissions['*']['createaccount'] = false;
813 $wgGroupPermissions['*']['autocreateaccount'] = false;
814 $user = User
::newFromName( 'UTDoesNotExist' );
815 $this->assertFalse( $manager->autoCreateUser( $user ) );
816 $this->assertSame( 0, $user->getId() );
817 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
818 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
820 $this->assertSame( array(
821 array( LogLevel
::DEBUG
, 'user is blocked from this wiki, blacklisting' ),
822 ), $logger->getBuffer() );
823 $logger->clearBuffer();
825 // Check other permission
826 $wgGroupPermissions['*']['createaccount'] = false;
827 $wgGroupPermissions['*']['autocreateaccount'] = true;
828 $user = User
::newFromName( 'UTSessionAutoCreate2' );
829 $this->assertSame( 0, $user->getId(), 'sanity check' );
830 $this->assertTrue( $manager->autoCreateUser( $user ) );
831 $this->assertNotEquals( 0, $user->getId() );
832 $this->assertSame( 'UTSessionAutoCreate2', $user->getName() );
834 $user->getId(), User
::idFromName( 'UTSessionAutoCreate2', User
::READ_LATEST
)
836 $this->assertSame( array(
837 array( LogLevel
::INFO
, 'creating new user (UTSessionAutoCreate2) - from: XXX' ),
838 ), $logger->getBuffer() );
839 $logger->clearBuffer();
841 // Test account-creation block
843 $block = new \
Block( array(
844 'address' => $anon->getName(),
846 'reason' => __METHOD__
,
847 'expiry' => time() +
100500,
848 'createAccount' => true,
851 $this->assertInstanceOf( 'Block', $anon->isBlockedFromCreateAccount(), 'sanity check' );
852 $reset2 = new \
ScopedCallback( array( $block, 'delete' ) );
853 $user = User
::newFromName( 'UTDoesNotExist' );
854 $this->assertFalse( $manager->autoCreateUser( $user ) );
855 $this->assertSame( 0, $user->getId() );
856 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
857 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
858 \ScopedCallback
::consume( $reset2 );
860 $this->assertSame( array(
861 array( LogLevel
::DEBUG
, 'user is blocked from this wiki, blacklisting' ),
862 ), $logger->getBuffer() );
863 $logger->clearBuffer();
865 // Sanity check that creation still works
866 $user = User
::newFromName( 'UTSessionAutoCreate3' );
867 $this->assertSame( 0, $user->getId(), 'sanity check' );
868 $this->assertTrue( $manager->autoCreateUser( $user ) );
869 $this->assertNotEquals( 0, $user->getId() );
870 $this->assertSame( 'UTSessionAutoCreate3', $user->getName() );
872 $user->getId(), User
::idFromName( 'UTSessionAutoCreate3', User
::READ_LATEST
)
874 $this->assertSame( array(
875 array( LogLevel
::INFO
, 'creating new user (UTSessionAutoCreate3) - from: XXX' ),
876 ), $logger->getBuffer() );
877 $logger->clearBuffer();
879 // Test prevention by AuthPlugin
881 $oldWgAuth = $wgAuth;
882 $mockWgAuth = $this->getMock( 'AuthPlugin', array( 'autoCreate' ) );
883 $mockWgAuth->expects( $this->once() )->method( 'autoCreate' )
884 ->will( $this->returnValue( false ) );
885 $this->setMwGlobals( array(
886 'wgAuth' => $mockWgAuth,
888 $user = User
::newFromName( 'UTDoesNotExist' );
889 $this->assertFalse( $manager->autoCreateUser( $user ) );
890 $this->assertSame( 0, $user->getId() );
891 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
892 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
893 $this->setMwGlobals( array(
894 'wgAuth' => $oldWgAuth,
897 $this->assertSame( array(
898 array( LogLevel
::DEBUG
, 'denied by AuthPlugin' ),
899 ), $logger->getBuffer() );
900 $logger->clearBuffer();
902 // Test prevention by wfReadOnly()
903 $this->setMwGlobals( array(
904 'wgReadOnly' => 'Because',
906 $user = User
::newFromName( 'UTDoesNotExist' );
907 $this->assertFalse( $manager->autoCreateUser( $user ) );
908 $this->assertSame( 0, $user->getId() );
909 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
910 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
911 $this->setMwGlobals( array(
912 'wgReadOnly' => false,
915 $this->assertSame( array(
916 array( LogLevel
::DEBUG
, 'denied by wfReadOnly()' ),
917 ), $logger->getBuffer() );
918 $logger->clearBuffer();
920 // Test prevention by a previous session
921 $session->set( 'MWSession::AutoCreateBlacklist', 'test' );
922 $user = User
::newFromName( 'UTDoesNotExist' );
923 $this->assertFalse( $manager->autoCreateUser( $user ) );
924 $this->assertSame( 0, $user->getId() );
925 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
926 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
928 $this->assertSame( array(
929 array( LogLevel
::DEBUG
, 'blacklisted in session (test)' ),
930 ), $logger->getBuffer() );
931 $logger->clearBuffer();
933 // Test uncreatable name
934 $user = User
::newFromName( 'UTDoesNotExist@' );
935 $this->assertFalse( $manager->autoCreateUser( $user ) );
936 $this->assertSame( 0, $user->getId() );
937 $this->assertNotSame( 'UTDoesNotExist@', $user->getName() );
938 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
940 $this->assertSame( array(
941 array( LogLevel
::DEBUG
, 'Invalid username, blacklisting' ),
942 ), $logger->getBuffer() );
943 $logger->clearBuffer();
945 // Test AbortAutoAccount hook
946 $mock = $this->getMock( __CLASS__
, array( 'onAbortAutoAccount' ) );
947 $mock->expects( $this->once() )->method( 'onAbortAutoAccount' )
948 ->will( $this->returnCallback( function ( User
$user, &$msg ) {
952 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array( $mock ) ) );
953 $user = User
::newFromName( 'UTDoesNotExist' );
954 $this->assertFalse( $manager->autoCreateUser( $user ) );
955 $this->assertSame( 0, $user->getId() );
956 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
957 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
958 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array() ) );
960 $this->assertSame( array(
961 array( LogLevel
::DEBUG
, 'denied by hook: No way!' ),
962 ), $logger->getBuffer() );
963 $logger->clearBuffer();
965 // Test AbortAutoAccount hook screwing up the name
966 $mock = $this->getMock( 'stdClass', array( 'onAbortAutoAccount' ) );
967 $mock->expects( $this->once() )->method( 'onAbortAutoAccount' )
968 ->will( $this->returnCallback( function ( User
$user ) {
969 $user->setName( 'UTDoesNotExistEither' );
971 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array( $mock ) ) );
973 $user = User
::newFromName( 'UTDoesNotExist' );
974 $manager->autoCreateUser( $user );
975 $this->fail( 'Expected exception not thrown' );
976 } catch ( \UnexpectedValueException
$ex ) {
978 'AbortAutoAccount hook tried to change the user name',
982 $this->assertSame( 0, $user->getId() );
983 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
984 $this->assertNotSame( 'UTDoesNotExistEither', $user->getName() );
985 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
986 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExistEither', User
::READ_LATEST
) );
987 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array() ) );
989 $this->assertSame( array(), $logger->getBuffer() );
990 $logger->clearBuffer();
992 // Test for "exception backoff"
993 $user = User
::newFromName( 'UTDoesNotExist' );
994 $cache = \ObjectCache
::getLocalClusterInstance();
995 $backoffKey = wfMemcKey( 'MWSession', 'autocreate-failed', md5( $user->getName() ) );
996 $cache->set( $backoffKey, 1, 60 * 10 );
997 $this->assertFalse( $manager->autoCreateUser( $user ) );
998 $this->assertSame( 0, $user->getId() );
999 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
1000 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
1001 $cache->delete( $backoffKey );
1003 $this->assertSame( array(
1004 array( LogLevel
::DEBUG
, 'denied by prior creation attempt failures' ),
1005 ), $logger->getBuffer() );
1006 $logger->clearBuffer();
1008 // Sanity check that creation still works, and test completion hook
1009 $cb = $this->callback( function ( User
$user ) use ( $that ) {
1010 $that->assertNotEquals( 0, $user->getId() );
1011 $that->assertSame( 'UTSessionAutoCreate4', $user->getName() );
1012 $that->assertEquals(
1013 $user->getId(), User
::idFromName( 'UTSessionAutoCreate4', User
::READ_LATEST
)
1017 $mock = $this->getMock( 'stdClass',
1018 array( 'onAuthPluginAutoCreate', 'onLocalUserCreated' ) );
1019 $mock->expects( $this->once() )->method( 'onAuthPluginAutoCreate' )
1021 $mock->expects( $this->once() )->method( 'onLocalUserCreated' )
1022 ->with( $cb, $this->identicalTo( true ) );
1023 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1024 'AuthPluginAutoCreate' => array( $mock ),
1025 'LocalUserCreated' => array( $mock ),
1027 $user = User
::newFromName( 'UTSessionAutoCreate4' );
1028 $this->assertSame( 0, $user->getId(), 'sanity check' );
1029 $this->assertTrue( $manager->autoCreateUser( $user ) );
1030 $this->assertNotEquals( 0, $user->getId() );
1031 $this->assertSame( 'UTSessionAutoCreate4', $user->getName() );
1032 $this->assertEquals(
1034 User
::idFromName( 'UTSessionAutoCreate4', User
::READ_LATEST
)
1036 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1037 'AuthPluginAutoCreate' => array(),
1038 'LocalUserCreated' => array(),
1040 $this->assertSame( array(
1041 array( LogLevel
::INFO
, 'creating new user (UTSessionAutoCreate4) - from: XXX' ),
1042 ), $logger->getBuffer() );
1043 $logger->clearBuffer();
1046 public function onAbortAutoAccount( User
$user, &$msg ) {
1049 public function testPreventSessionsForUser() {
1050 $manager = $this->getManager();
1052 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
1053 ->setMethods( array( 'preventSessionsForUser', '__toString' ) );
1055 $provider1 = $providerBuilder->getMock();
1056 $provider1->expects( $this->once() )->method( 'preventSessionsForUser' )
1057 ->with( $this->equalTo( 'UTSysop' ) );
1058 $provider1->expects( $this->any() )->method( '__toString' )
1059 ->will( $this->returnValue( 'MockProvider1' ) );
1061 $this->config
->set( 'SessionProviders', array(
1062 $this->objectCacheDef( $provider1 ),
1065 $this->assertFalse( $manager->isUserSessionPrevented( 'UTSysop' ) );
1066 $manager->preventSessionsForUser( 'UTSysop' );
1067 $this->assertTrue( $manager->isUserSessionPrevented( 'UTSysop' ) );
1070 public function testLoadSessionInfoFromStore() {
1071 $manager = $this->getManager();
1072 $logger = new \
TestLogger( true, function ( $m ) {
1073 return preg_replace(
1074 '/^Session \[\d+\]\w+<(?:null|anon|[+-]:\d+:\w+)>\w+: /', 'Session X: ', $m
1077 $manager->setLogger( $logger );
1078 $request = new \
FauxRequest();
1080 // TestingAccessWrapper can't handle methods with reference arguments, sigh.
1081 $rClass = new \
ReflectionClass( $manager );
1082 $rMethod = $rClass->getMethod( 'loadSessionInfoFromStore' );
1083 $rMethod->setAccessible( true );
1084 $loadSessionInfoFromStore = function ( &$info ) use ( $rMethod, $manager, $request ) {
1085 return $rMethod->invokeArgs( $manager, array( &$info, $request ) );
1088 $userInfo = UserInfo
::newFromName( 'UTSysop', true );
1089 $unverifiedUserInfo = UserInfo
::newFromName( 'UTSysop', false );
1091 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
1093 'userId' => $userInfo->getId(),
1094 'userName' => $userInfo->getName(),
1095 'userToken' => $userInfo->getToken( true ),
1096 'provider' => 'Mock',
1099 $builder = $this->getMockBuilder( 'MediaWiki\\Session\\SessionProvider' )
1100 ->setMethods( array( '__toString', 'mergeMetadata', 'refreshSessionInfo' ) );
1102 $provider = $builder->getMockForAbstractClass();
1103 $provider->setManager( $manager );
1104 $provider->expects( $this->any() )->method( 'persistsSessionId' )
1105 ->will( $this->returnValue( true ) );
1106 $provider->expects( $this->any() )->method( 'canChangeUser' )
1107 ->will( $this->returnValue( true ) );
1108 $provider->expects( $this->any() )->method( 'refreshSessionInfo' )
1109 ->will( $this->returnValue( true ) );
1110 $provider->expects( $this->any() )->method( '__toString' )
1111 ->will( $this->returnValue( 'Mock' ) );
1112 $provider->expects( $this->any() )->method( 'mergeMetadata' )
1113 ->will( $this->returnCallback( function ( $a, $b ) {
1114 if ( $b === array( 'Throw' ) ) {
1115 throw new \
UnexpectedValueException( 'no merge!' );
1117 return array( 'Merged' );
1120 $provider2 = $builder->getMockForAbstractClass();
1121 $provider2->setManager( $manager );
1122 $provider2->expects( $this->any() )->method( 'persistsSessionId' )
1123 ->will( $this->returnValue( false ) );
1124 $provider2->expects( $this->any() )->method( 'canChangeUser' )
1125 ->will( $this->returnValue( false ) );
1126 $provider2->expects( $this->any() )->method( '__toString' )
1127 ->will( $this->returnValue( 'Mock2' ) );
1128 $provider2->expects( $this->any() )->method( 'refreshSessionInfo' )
1129 ->will( $this->returnCallback( function ( $info, $request, &$metadata ) {
1130 $metadata['changed'] = true;
1134 $provider3 = $builder->getMockForAbstractClass();
1135 $provider3->setManager( $manager );
1136 $provider3->expects( $this->any() )->method( 'persistsSessionId' )
1137 ->will( $this->returnValue( true ) );
1138 $provider3->expects( $this->any() )->method( 'canChangeUser' )
1139 ->will( $this->returnValue( true ) );
1140 $provider3->expects( $this->once() )->method( 'refreshSessionInfo' )
1141 ->will( $this->returnValue( false ) );
1142 $provider3->expects( $this->any() )->method( '__toString' )
1143 ->will( $this->returnValue( 'Mock3' ) );
1145 \TestingAccessWrapper
::newFromObject( $manager )->sessionProviders
= array(
1146 (string)$provider => $provider,
1147 (string)$provider2 => $provider2,
1148 (string)$provider3 => $provider3,
1151 // No metadata, basic usage
1152 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1153 'provider' => $provider,
1155 'userInfo' => $userInfo
1157 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1158 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1159 $this->assertFalse( $info->isIdSafe() );
1160 $this->assertSame( array(), $logger->getBuffer() );
1162 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1163 'provider' => $provider,
1164 'userInfo' => $userInfo
1166 $this->assertTrue( $info->isIdSafe(), 'sanity check' );
1167 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1168 $this->assertTrue( $info->isIdSafe() );
1169 $this->assertSame( array(), $logger->getBuffer() );
1171 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1172 'provider' => $provider2,
1174 'userInfo' => $userInfo
1176 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1177 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1178 $this->assertTrue( $info->isIdSafe() );
1179 $this->assertSame( array(), $logger->getBuffer() );
1181 // Unverified user, no metadata
1182 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1183 'provider' => $provider,
1185 'userInfo' => $unverifiedUserInfo
1187 $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
1188 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1189 $this->assertSame( array(
1190 array( LogLevel
::WARNING
, 'Session X: Unverified user provided and no metadata to auth it' )
1191 ), $logger->getBuffer() );
1192 $logger->clearBuffer();
1194 // No metadata, missing data
1195 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1197 'userInfo' => $userInfo
1199 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1200 $this->assertSame( array(
1201 array( LogLevel
::WARNING
, 'Session X: Null provider and no metadata' ),
1202 ), $logger->getBuffer() );
1203 $logger->clearBuffer();
1205 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1206 'provider' => $provider,
1209 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1210 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1211 $this->assertInstanceOf( 'MediaWiki\\Session\\UserInfo', $info->getUserInfo() );
1212 $this->assertTrue( $info->getUserInfo()->isVerified() );
1213 $this->assertTrue( $info->getUserInfo()->isAnon() );
1214 $this->assertFalse( $info->isIdSafe() );
1215 $this->assertSame( array(), $logger->getBuffer() );
1217 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1218 'provider' => $provider2,
1221 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1222 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1223 $this->assertSame( array(
1224 array( LogLevel
::INFO
, 'Session X: No user provided and provider cannot set user' )
1225 ), $logger->getBuffer() );
1226 $logger->clearBuffer();
1228 // Incomplete/bad metadata
1229 $this->store
->setRawSession( $id, true );
1230 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1231 $this->assertSame( array(
1232 array( LogLevel
::WARNING
, 'Session X: Bad data' ),
1233 ), $logger->getBuffer() );
1234 $logger->clearBuffer();
1236 $this->store
->setRawSession( $id, array( 'data' => array() ) );
1237 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1238 $this->assertSame( array(
1239 array( LogLevel
::WARNING
, 'Session X: Bad data structure' ),
1240 ), $logger->getBuffer() );
1241 $logger->clearBuffer();
1243 $this->store
->deleteSession( $id );
1244 $this->store
->setRawSession( $id, array( 'metadata' => $metadata ) );
1245 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1246 $this->assertSame( array(
1247 array( LogLevel
::WARNING
, 'Session X: Bad data structure' ),
1248 ), $logger->getBuffer() );
1249 $logger->clearBuffer();
1251 $this->store
->setRawSession( $id, array( 'metadata' => $metadata, 'data' => true ) );
1252 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1253 $this->assertSame( array(
1254 array( LogLevel
::WARNING
, 'Session X: Bad data structure' ),
1255 ), $logger->getBuffer() );
1256 $logger->clearBuffer();
1258 $this->store
->setRawSession( $id, array( 'metadata' => true, 'data' => array() ) );
1259 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1260 $this->assertSame( array(
1261 array( LogLevel
::WARNING
, 'Session X: Bad data structure' ),
1262 ), $logger->getBuffer() );
1263 $logger->clearBuffer();
1265 foreach ( $metadata as $key => $dummy ) {
1267 unset( $tmp[$key] );
1268 $this->store
->setRawSession( $id, array( 'metadata' => $tmp, 'data' => array() ) );
1269 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1270 $this->assertSame( array(
1271 array( LogLevel
::WARNING
, 'Session X: Bad metadata' ),
1272 ), $logger->getBuffer() );
1273 $logger->clearBuffer();
1276 // Basic usage with metadata
1277 $this->store
->setRawSession( $id, array( 'metadata' => $metadata, 'data' => array() ) );
1278 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1279 'provider' => $provider,
1281 'userInfo' => $userInfo
1283 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1284 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1285 $this->assertTrue( $info->isIdSafe() );
1286 $this->assertSame( array(), $logger->getBuffer() );
1288 // Mismatched provider
1289 $this->store
->setSessionMeta( $id, array( 'provider' => 'Bad' ) +
$metadata );
1290 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1291 'provider' => $provider,
1293 'userInfo' => $userInfo
1295 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1296 $this->assertSame( array(
1297 array( LogLevel
::WARNING
, 'Session X: Wrong provider, Bad !== Mock' ),
1298 ), $logger->getBuffer() );
1299 $logger->clearBuffer();
1302 $this->store
->setSessionMeta( $id, array( 'provider' => 'Bad' ) +
$metadata );
1303 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1305 'userInfo' => $userInfo
1307 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1308 $this->assertSame( array(
1309 array( LogLevel
::WARNING
, 'Session X: Unknown provider, Bad' ),
1310 ), $logger->getBuffer() );
1311 $logger->clearBuffer();
1314 $this->store
->setSessionMeta( $id, $metadata );
1315 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1317 'userInfo' => $userInfo
1319 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1320 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1321 $this->assertTrue( $info->isIdSafe() );
1322 $this->assertSame( array(), $logger->getBuffer() );
1324 // Bad user metadata
1325 $this->store
->setSessionMeta( $id, array( 'userId' => -1, 'userToken' => null ) +
$metadata );
1326 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1327 'provider' => $provider,
1330 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1331 $this->assertSame( array(
1332 array( LogLevel
::ERROR
, 'Session X: Invalid ID' ),
1333 ), $logger->getBuffer() );
1334 $logger->clearBuffer();
1336 $this->store
->setSessionMeta(
1337 $id, array( 'userId' => 0, 'userName' => '<X>', 'userToken' => null ) +
$metadata
1339 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1340 'provider' => $provider,
1343 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1344 $this->assertSame( array(
1345 array( LogLevel
::ERROR
, 'Session X: Invalid user name' ),
1346 ), $logger->getBuffer() );
1347 $logger->clearBuffer();
1349 // Mismatched user by ID
1350 $this->store
->setSessionMeta(
1351 $id, array( 'userId' => $userInfo->getId() +
1, 'userToken' => null ) +
$metadata
1353 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1354 'provider' => $provider,
1356 'userInfo' => $userInfo
1358 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1359 $this->assertSame( array(
1360 array( LogLevel
::WARNING
, 'Session X: User ID mismatch, 2 !== 1' ),
1361 ), $logger->getBuffer() );
1362 $logger->clearBuffer();
1364 // Mismatched user by name
1365 $this->store
->setSessionMeta(
1366 $id, array( 'userId' => 0, 'userName' => 'X', 'userToken' => null ) +
$metadata
1368 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1369 'provider' => $provider,
1371 'userInfo' => $userInfo
1373 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1374 $this->assertSame( array(
1375 array( LogLevel
::WARNING
, 'Session X: User name mismatch, X !== UTSysop' ),
1376 ), $logger->getBuffer() );
1377 $logger->clearBuffer();
1379 // ID matches, name doesn't
1380 $this->store
->setSessionMeta(
1381 $id, array( 'userId' => $userInfo->getId(), 'userName' => 'X', 'userToken' => null ) +
$metadata
1383 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1384 'provider' => $provider,
1386 'userInfo' => $userInfo
1388 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1389 $this->assertSame( array(
1391 LogLevel
::WARNING
, 'Session X: User ID matched but name didn\'t (rename?), X !== UTSysop'
1393 ), $logger->getBuffer() );
1394 $logger->clearBuffer();
1396 // Mismatched anon user
1397 $this->store
->setSessionMeta(
1398 $id, array( 'userId' => 0, 'userName' => null, 'userToken' => null ) +
$metadata
1400 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1401 'provider' => $provider,
1403 'userInfo' => $userInfo
1405 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1406 $this->assertSame( array(
1408 LogLevel
::WARNING
, 'Session X: Metadata has an anonymous user, but a non-anon user was provided'
1410 ), $logger->getBuffer() );
1411 $logger->clearBuffer();
1413 // Lookup user by ID
1414 $this->store
->setSessionMeta( $id, array( 'userToken' => null ) +
$metadata );
1415 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1416 'provider' => $provider,
1419 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1420 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1421 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1422 $this->assertTrue( $info->isIdSafe() );
1423 $this->assertSame( array(), $logger->getBuffer() );
1425 // Lookup user by name
1426 $this->store
->setSessionMeta(
1427 $id, array( 'userId' => 0, 'userName' => 'UTSysop', 'userToken' => null ) +
$metadata
1429 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1430 'provider' => $provider,
1433 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1434 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1435 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1436 $this->assertTrue( $info->isIdSafe() );
1437 $this->assertSame( array(), $logger->getBuffer() );
1439 // Lookup anonymous user
1440 $this->store
->setSessionMeta(
1441 $id, array( 'userId' => 0, 'userName' => null, 'userToken' => null ) +
$metadata
1443 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1444 'provider' => $provider,
1447 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1448 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1449 $this->assertTrue( $info->getUserInfo()->isAnon() );
1450 $this->assertTrue( $info->isIdSafe() );
1451 $this->assertSame( array(), $logger->getBuffer() );
1453 // Unverified user with metadata
1454 $this->store
->setSessionMeta( $id, $metadata );
1455 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1456 'provider' => $provider,
1458 'userInfo' => $unverifiedUserInfo
1460 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1461 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1462 $this->assertTrue( $info->getUserInfo()->isVerified() );
1463 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1464 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1465 $this->assertTrue( $info->isIdSafe() );
1466 $this->assertSame( array(), $logger->getBuffer() );
1468 // Unverified user with metadata
1469 $this->store
->setSessionMeta( $id, $metadata );
1470 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1471 'provider' => $provider,
1473 'userInfo' => $unverifiedUserInfo
1475 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1476 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1477 $this->assertTrue( $info->getUserInfo()->isVerified() );
1478 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1479 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1480 $this->assertTrue( $info->isIdSafe() );
1481 $this->assertSame( array(), $logger->getBuffer() );
1484 $this->store
->setSessionMeta( $id, array( 'userToken' => 'Bad' ) +
$metadata );
1485 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1486 'provider' => $provider,
1488 'userInfo' => $userInfo
1490 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1491 $this->assertSame( array(
1492 array( LogLevel
::WARNING
, 'Session X: User token mismatch' ),
1493 ), $logger->getBuffer() );
1494 $logger->clearBuffer();
1496 // Provider metadata
1497 $this->store
->setSessionMeta( $id, array( 'provider' => 'Mock2' ) +
$metadata );
1498 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1499 'provider' => $provider2,
1501 'userInfo' => $userInfo,
1502 'metadata' => array( 'Info' ),
1504 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1505 $this->assertSame( array( 'Info', 'changed' => true ), $info->getProviderMetadata() );
1506 $this->assertSame( array(), $logger->getBuffer() );
1508 $this->store
->setSessionMeta( $id, array( 'providerMetadata' => array( 'Saved' ) ) +
$metadata );
1509 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1510 'provider' => $provider,
1512 'userInfo' => $userInfo,
1514 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1515 $this->assertSame( array( 'Saved' ), $info->getProviderMetadata() );
1516 $this->assertSame( array(), $logger->getBuffer() );
1518 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1519 'provider' => $provider,
1521 'userInfo' => $userInfo,
1522 'metadata' => array( 'Info' ),
1524 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1525 $this->assertSame( array( 'Merged' ), $info->getProviderMetadata() );
1526 $this->assertSame( array(), $logger->getBuffer() );
1528 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1529 'provider' => $provider,
1531 'userInfo' => $userInfo,
1532 'metadata' => array( 'Throw' ),
1534 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1535 $this->assertSame( array(
1536 array( LogLevel
::WARNING
, 'Session X: Metadata merge failed: no merge!' ),
1537 ), $logger->getBuffer() );
1538 $logger->clearBuffer();
1540 // Remember from session
1541 $this->store
->setSessionMeta( $id, $metadata );
1542 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1543 'provider' => $provider,
1546 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1547 $this->assertFalse( $info->wasRemembered() );
1548 $this->assertSame( array(), $logger->getBuffer() );
1550 $this->store
->setSessionMeta( $id, array( 'remember' => true ) +
$metadata );
1551 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1552 'provider' => $provider,
1555 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1556 $this->assertTrue( $info->wasRemembered() );
1557 $this->assertSame( array(), $logger->getBuffer() );
1559 $this->store
->setSessionMeta( $id, array( 'remember' => false ) +
$metadata );
1560 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1561 'provider' => $provider,
1563 'userInfo' => $userInfo
1565 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1566 $this->assertTrue( $info->wasRemembered() );
1567 $this->assertSame( array(), $logger->getBuffer() );
1569 // forceHTTPS from session
1570 $this->store
->setSessionMeta( $id, $metadata );
1571 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1572 'provider' => $provider,
1574 'userInfo' => $userInfo
1576 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1577 $this->assertFalse( $info->forceHTTPS() );
1578 $this->assertSame( array(), $logger->getBuffer() );
1580 $this->store
->setSessionMeta( $id, array( 'forceHTTPS' => true ) +
$metadata );
1581 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1582 'provider' => $provider,
1584 'userInfo' => $userInfo
1586 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1587 $this->assertTrue( $info->forceHTTPS() );
1588 $this->assertSame( array(), $logger->getBuffer() );
1590 $this->store
->setSessionMeta( $id, array( 'forceHTTPS' => false ) +
$metadata );
1591 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1592 'provider' => $provider,
1594 'userInfo' => $userInfo,
1595 'forceHTTPS' => true
1597 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1598 $this->assertTrue( $info->forceHTTPS() );
1599 $this->assertSame( array(), $logger->getBuffer() );
1601 // "Persist" flag from session
1602 $this->store
->setSessionMeta( $id, $metadata );
1603 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1604 'provider' => $provider,
1606 'userInfo' => $userInfo
1608 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1609 $this->assertFalse( $info->wasPersisted() );
1610 $this->assertSame( array(), $logger->getBuffer() );
1612 $this->store
->setSessionMeta( $id, array( 'persisted' => true ) +
$metadata );
1613 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1614 'provider' => $provider,
1616 'userInfo' => $userInfo
1618 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1619 $this->assertTrue( $info->wasPersisted() );
1620 $this->assertSame( array(), $logger->getBuffer() );
1622 $this->store
->setSessionMeta( $id, array( 'persisted' => false ) +
$metadata );
1623 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1624 'provider' => $provider,
1626 'userInfo' => $userInfo,
1629 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1630 $this->assertTrue( $info->wasPersisted() );
1631 $this->assertSame( array(), $logger->getBuffer() );
1633 // Provider refreshSessionInfo() returning false
1634 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1635 'provider' => $provider3,
1637 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1638 $this->assertSame( array(), $logger->getBuffer() );
1643 $data = array( 'foo' => 1 );
1644 $this->store
->setSession( $id, array( 'metadata' => $metadata, 'data' => $data ) );
1645 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1646 'provider' => $provider,
1648 'userInfo' => $userInfo
1650 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1651 'SessionCheckInfo' => array( function ( &$reason, $i, $r, $m, $d ) use (
1652 $that, $info, $metadata, $data, $request, &$called
1654 $that->assertSame( $info->getId(), $i->getId() );
1655 $that->assertSame( $info->getProvider(), $i->getProvider() );
1656 $that->assertSame( $info->getUserInfo(), $i->getUserInfo() );
1657 $that->assertSame( $request, $r );
1658 $that->assertEquals( $metadata, $m );
1659 $that->assertEquals( $data, $d );
1664 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1665 $this->assertTrue( $called );
1666 $this->assertSame( array(
1667 array( LogLevel
::WARNING
, 'Session X: Hook aborted' ),
1668 ), $logger->getBuffer() );
1669 $logger->clearBuffer();