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 $user = User
::newFromName( 'UTSysop' );
1066 $token = $user->getToken( true );
1068 $this->assertFalse( $manager->isUserSessionPrevented( 'UTSysop' ) );
1069 $manager->preventSessionsForUser( 'UTSysop' );
1070 $this->assertNotEquals( $token, User
::newFromName( 'UTSysop' )->getToken() );
1071 $this->assertTrue( $manager->isUserSessionPrevented( 'UTSysop' ) );
1074 public function testLoadSessionInfoFromStore() {
1075 $manager = $this->getManager();
1076 $logger = new \
TestLogger( true, function ( $m ) {
1077 return preg_replace(
1078 '/^Session \[\d+\]\w+<(?:null|anon|[+-]:\d+:\w+)>\w+: /', 'Session X: ', $m
1081 $manager->setLogger( $logger );
1082 $request = new \
FauxRequest();
1084 // TestingAccessWrapper can't handle methods with reference arguments, sigh.
1085 $rClass = new \
ReflectionClass( $manager );
1086 $rMethod = $rClass->getMethod( 'loadSessionInfoFromStore' );
1087 $rMethod->setAccessible( true );
1088 $loadSessionInfoFromStore = function ( &$info ) use ( $rMethod, $manager, $request ) {
1089 return $rMethod->invokeArgs( $manager, array( &$info, $request ) );
1092 $userInfo = UserInfo
::newFromName( 'UTSysop', true );
1093 $unverifiedUserInfo = UserInfo
::newFromName( 'UTSysop', false );
1095 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
1097 'userId' => $userInfo->getId(),
1098 'userName' => $userInfo->getName(),
1099 'userToken' => $userInfo->getToken( true ),
1100 'provider' => 'Mock',
1103 $builder = $this->getMockBuilder( 'MediaWiki\\Session\\SessionProvider' )
1104 ->setMethods( array( '__toString', 'mergeMetadata', 'refreshSessionInfo' ) );
1106 $provider = $builder->getMockForAbstractClass();
1107 $provider->setManager( $manager );
1108 $provider->expects( $this->any() )->method( 'persistsSessionId' )
1109 ->will( $this->returnValue( true ) );
1110 $provider->expects( $this->any() )->method( 'canChangeUser' )
1111 ->will( $this->returnValue( true ) );
1112 $provider->expects( $this->any() )->method( 'refreshSessionInfo' )
1113 ->will( $this->returnValue( true ) );
1114 $provider->expects( $this->any() )->method( '__toString' )
1115 ->will( $this->returnValue( 'Mock' ) );
1116 $provider->expects( $this->any() )->method( 'mergeMetadata' )
1117 ->will( $this->returnCallback( function ( $a, $b ) {
1118 if ( $b === array( 'Throw' ) ) {
1119 throw new \
UnexpectedValueException( 'no merge!' );
1121 return array( 'Merged' );
1124 $provider2 = $builder->getMockForAbstractClass();
1125 $provider2->setManager( $manager );
1126 $provider2->expects( $this->any() )->method( 'persistsSessionId' )
1127 ->will( $this->returnValue( false ) );
1128 $provider2->expects( $this->any() )->method( 'canChangeUser' )
1129 ->will( $this->returnValue( false ) );
1130 $provider2->expects( $this->any() )->method( '__toString' )
1131 ->will( $this->returnValue( 'Mock2' ) );
1132 $provider2->expects( $this->any() )->method( 'refreshSessionInfo' )
1133 ->will( $this->returnCallback( function ( $info, $request, &$metadata ) {
1134 $metadata['changed'] = true;
1138 $provider3 = $builder->getMockForAbstractClass();
1139 $provider3->setManager( $manager );
1140 $provider3->expects( $this->any() )->method( 'persistsSessionId' )
1141 ->will( $this->returnValue( true ) );
1142 $provider3->expects( $this->any() )->method( 'canChangeUser' )
1143 ->will( $this->returnValue( true ) );
1144 $provider3->expects( $this->once() )->method( 'refreshSessionInfo' )
1145 ->will( $this->returnValue( false ) );
1146 $provider3->expects( $this->any() )->method( '__toString' )
1147 ->will( $this->returnValue( 'Mock3' ) );
1149 \TestingAccessWrapper
::newFromObject( $manager )->sessionProviders
= array(
1150 (string)$provider => $provider,
1151 (string)$provider2 => $provider2,
1152 (string)$provider3 => $provider3,
1155 // No metadata, basic usage
1156 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1157 'provider' => $provider,
1159 'userInfo' => $userInfo
1161 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1162 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1163 $this->assertFalse( $info->isIdSafe() );
1164 $this->assertSame( array(), $logger->getBuffer() );
1166 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1167 'provider' => $provider,
1168 'userInfo' => $userInfo
1170 $this->assertTrue( $info->isIdSafe(), 'sanity check' );
1171 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1172 $this->assertTrue( $info->isIdSafe() );
1173 $this->assertSame( array(), $logger->getBuffer() );
1175 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1176 'provider' => $provider2,
1178 'userInfo' => $userInfo
1180 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1181 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1182 $this->assertTrue( $info->isIdSafe() );
1183 $this->assertSame( array(), $logger->getBuffer() );
1185 // Unverified user, no metadata
1186 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1187 'provider' => $provider,
1189 'userInfo' => $unverifiedUserInfo
1191 $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
1192 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1193 $this->assertSame( array(
1194 array( LogLevel
::WARNING
, 'Session X: Unverified user provided and no metadata to auth it' )
1195 ), $logger->getBuffer() );
1196 $logger->clearBuffer();
1198 // No metadata, missing data
1199 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1201 'userInfo' => $userInfo
1203 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1204 $this->assertSame( array(
1205 array( LogLevel
::WARNING
, 'Session X: Null provider and no metadata' ),
1206 ), $logger->getBuffer() );
1207 $logger->clearBuffer();
1209 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1210 'provider' => $provider,
1213 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1214 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1215 $this->assertInstanceOf( 'MediaWiki\\Session\\UserInfo', $info->getUserInfo() );
1216 $this->assertTrue( $info->getUserInfo()->isVerified() );
1217 $this->assertTrue( $info->getUserInfo()->isAnon() );
1218 $this->assertFalse( $info->isIdSafe() );
1219 $this->assertSame( array(), $logger->getBuffer() );
1221 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1222 'provider' => $provider2,
1225 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1226 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1227 $this->assertSame( array(
1228 array( LogLevel
::INFO
, 'Session X: No user provided and provider cannot set user' )
1229 ), $logger->getBuffer() );
1230 $logger->clearBuffer();
1232 // Incomplete/bad metadata
1233 $this->store
->setRawSession( $id, true );
1234 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1235 $this->assertSame( array(
1236 array( LogLevel
::WARNING
, 'Session X: Bad data' ),
1237 ), $logger->getBuffer() );
1238 $logger->clearBuffer();
1240 $this->store
->setRawSession( $id, array( 'data' => array() ) );
1241 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1242 $this->assertSame( array(
1243 array( LogLevel
::WARNING
, 'Session X: Bad data structure' ),
1244 ), $logger->getBuffer() );
1245 $logger->clearBuffer();
1247 $this->store
->deleteSession( $id );
1248 $this->store
->setRawSession( $id, array( 'metadata' => $metadata ) );
1249 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1250 $this->assertSame( array(
1251 array( LogLevel
::WARNING
, 'Session X: Bad data structure' ),
1252 ), $logger->getBuffer() );
1253 $logger->clearBuffer();
1255 $this->store
->setRawSession( $id, array( 'metadata' => $metadata, 'data' => true ) );
1256 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1257 $this->assertSame( array(
1258 array( LogLevel
::WARNING
, 'Session X: Bad data structure' ),
1259 ), $logger->getBuffer() );
1260 $logger->clearBuffer();
1262 $this->store
->setRawSession( $id, array( 'metadata' => true, 'data' => array() ) );
1263 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1264 $this->assertSame( array(
1265 array( LogLevel
::WARNING
, 'Session X: Bad data structure' ),
1266 ), $logger->getBuffer() );
1267 $logger->clearBuffer();
1269 foreach ( $metadata as $key => $dummy ) {
1271 unset( $tmp[$key] );
1272 $this->store
->setRawSession( $id, array( 'metadata' => $tmp, 'data' => array() ) );
1273 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1274 $this->assertSame( array(
1275 array( LogLevel
::WARNING
, 'Session X: Bad metadata' ),
1276 ), $logger->getBuffer() );
1277 $logger->clearBuffer();
1280 // Basic usage with metadata
1281 $this->store
->setRawSession( $id, array( 'metadata' => $metadata, 'data' => array() ) );
1282 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1283 'provider' => $provider,
1285 'userInfo' => $userInfo
1287 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1288 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1289 $this->assertTrue( $info->isIdSafe() );
1290 $this->assertSame( array(), $logger->getBuffer() );
1292 // Mismatched provider
1293 $this->store
->setSessionMeta( $id, array( 'provider' => 'Bad' ) +
$metadata );
1294 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1295 'provider' => $provider,
1297 'userInfo' => $userInfo
1299 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1300 $this->assertSame( array(
1301 array( LogLevel
::WARNING
, 'Session X: Wrong provider, Bad !== Mock' ),
1302 ), $logger->getBuffer() );
1303 $logger->clearBuffer();
1306 $this->store
->setSessionMeta( $id, array( 'provider' => 'Bad' ) +
$metadata );
1307 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1309 'userInfo' => $userInfo
1311 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1312 $this->assertSame( array(
1313 array( LogLevel
::WARNING
, 'Session X: Unknown provider, Bad' ),
1314 ), $logger->getBuffer() );
1315 $logger->clearBuffer();
1318 $this->store
->setSessionMeta( $id, $metadata );
1319 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1321 'userInfo' => $userInfo
1323 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1324 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1325 $this->assertTrue( $info->isIdSafe() );
1326 $this->assertSame( array(), $logger->getBuffer() );
1328 // Bad user metadata
1329 $this->store
->setSessionMeta( $id, array( 'userId' => -1, 'userToken' => null ) +
$metadata );
1330 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1331 'provider' => $provider,
1334 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1335 $this->assertSame( array(
1336 array( LogLevel
::ERROR
, 'Session X: Invalid ID' ),
1337 ), $logger->getBuffer() );
1338 $logger->clearBuffer();
1340 $this->store
->setSessionMeta(
1341 $id, array( 'userId' => 0, 'userName' => '<X>', 'userToken' => null ) +
$metadata
1343 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1344 'provider' => $provider,
1347 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1348 $this->assertSame( array(
1349 array( LogLevel
::ERROR
, 'Session X: Invalid user name' ),
1350 ), $logger->getBuffer() );
1351 $logger->clearBuffer();
1353 // Mismatched user by ID
1354 $this->store
->setSessionMeta(
1355 $id, array( 'userId' => $userInfo->getId() +
1, 'userToken' => null ) +
$metadata
1357 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1358 'provider' => $provider,
1360 'userInfo' => $userInfo
1362 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1363 $this->assertSame( array(
1364 array( LogLevel
::WARNING
, 'Session X: User ID mismatch, 2 !== 1' ),
1365 ), $logger->getBuffer() );
1366 $logger->clearBuffer();
1368 // Mismatched user by name
1369 $this->store
->setSessionMeta(
1370 $id, array( 'userId' => 0, 'userName' => 'X', 'userToken' => null ) +
$metadata
1372 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1373 'provider' => $provider,
1375 'userInfo' => $userInfo
1377 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1378 $this->assertSame( array(
1379 array( LogLevel
::WARNING
, 'Session X: User name mismatch, X !== UTSysop' ),
1380 ), $logger->getBuffer() );
1381 $logger->clearBuffer();
1383 // ID matches, name doesn't
1384 $this->store
->setSessionMeta(
1385 $id, array( 'userId' => $userInfo->getId(), 'userName' => 'X', 'userToken' => null ) +
$metadata
1387 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1388 'provider' => $provider,
1390 'userInfo' => $userInfo
1392 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1393 $this->assertSame( array(
1395 LogLevel
::WARNING
, 'Session X: User ID matched but name didn\'t (rename?), X !== UTSysop'
1397 ), $logger->getBuffer() );
1398 $logger->clearBuffer();
1400 // Mismatched anon user
1401 $this->store
->setSessionMeta(
1402 $id, array( 'userId' => 0, 'userName' => null, 'userToken' => null ) +
$metadata
1404 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1405 'provider' => $provider,
1407 'userInfo' => $userInfo
1409 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1410 $this->assertSame( array(
1412 LogLevel
::WARNING
, 'Session X: Metadata has an anonymous user, but a non-anon user was provided'
1414 ), $logger->getBuffer() );
1415 $logger->clearBuffer();
1417 // Lookup user by ID
1418 $this->store
->setSessionMeta( $id, array( 'userToken' => null ) +
$metadata );
1419 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1420 'provider' => $provider,
1423 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1424 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1425 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1426 $this->assertTrue( $info->isIdSafe() );
1427 $this->assertSame( array(), $logger->getBuffer() );
1429 // Lookup user by name
1430 $this->store
->setSessionMeta(
1431 $id, array( 'userId' => 0, 'userName' => 'UTSysop', 'userToken' => null ) +
$metadata
1433 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1434 'provider' => $provider,
1437 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1438 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1439 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1440 $this->assertTrue( $info->isIdSafe() );
1441 $this->assertSame( array(), $logger->getBuffer() );
1443 // Lookup anonymous user
1444 $this->store
->setSessionMeta(
1445 $id, array( 'userId' => 0, 'userName' => null, 'userToken' => null ) +
$metadata
1447 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1448 'provider' => $provider,
1451 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1452 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1453 $this->assertTrue( $info->getUserInfo()->isAnon() );
1454 $this->assertTrue( $info->isIdSafe() );
1455 $this->assertSame( array(), $logger->getBuffer() );
1457 // Unverified user with metadata
1458 $this->store
->setSessionMeta( $id, $metadata );
1459 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1460 'provider' => $provider,
1462 'userInfo' => $unverifiedUserInfo
1464 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1465 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1466 $this->assertTrue( $info->getUserInfo()->isVerified() );
1467 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1468 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1469 $this->assertTrue( $info->isIdSafe() );
1470 $this->assertSame( array(), $logger->getBuffer() );
1472 // Unverified user with metadata
1473 $this->store
->setSessionMeta( $id, $metadata );
1474 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1475 'provider' => $provider,
1477 'userInfo' => $unverifiedUserInfo
1479 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1480 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1481 $this->assertTrue( $info->getUserInfo()->isVerified() );
1482 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1483 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1484 $this->assertTrue( $info->isIdSafe() );
1485 $this->assertSame( array(), $logger->getBuffer() );
1488 $this->store
->setSessionMeta( $id, array( 'userToken' => 'Bad' ) +
$metadata );
1489 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1490 'provider' => $provider,
1492 'userInfo' => $userInfo
1494 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1495 $this->assertSame( array(
1496 array( LogLevel
::WARNING
, 'Session X: User token mismatch' ),
1497 ), $logger->getBuffer() );
1498 $logger->clearBuffer();
1500 // Provider metadata
1501 $this->store
->setSessionMeta( $id, array( 'provider' => 'Mock2' ) +
$metadata );
1502 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1503 'provider' => $provider2,
1505 'userInfo' => $userInfo,
1506 'metadata' => array( 'Info' ),
1508 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1509 $this->assertSame( array( 'Info', 'changed' => true ), $info->getProviderMetadata() );
1510 $this->assertSame( array(), $logger->getBuffer() );
1512 $this->store
->setSessionMeta( $id, array( 'providerMetadata' => array( 'Saved' ) ) +
$metadata );
1513 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1514 'provider' => $provider,
1516 'userInfo' => $userInfo,
1518 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1519 $this->assertSame( array( 'Saved' ), $info->getProviderMetadata() );
1520 $this->assertSame( array(), $logger->getBuffer() );
1522 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1523 'provider' => $provider,
1525 'userInfo' => $userInfo,
1526 'metadata' => array( 'Info' ),
1528 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1529 $this->assertSame( array( 'Merged' ), $info->getProviderMetadata() );
1530 $this->assertSame( array(), $logger->getBuffer() );
1532 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1533 'provider' => $provider,
1535 'userInfo' => $userInfo,
1536 'metadata' => array( 'Throw' ),
1538 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1539 $this->assertSame( array(
1540 array( LogLevel
::WARNING
, 'Session X: Metadata merge failed: no merge!' ),
1541 ), $logger->getBuffer() );
1542 $logger->clearBuffer();
1544 // Remember from session
1545 $this->store
->setSessionMeta( $id, $metadata );
1546 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1547 'provider' => $provider,
1550 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1551 $this->assertFalse( $info->wasRemembered() );
1552 $this->assertSame( array(), $logger->getBuffer() );
1554 $this->store
->setSessionMeta( $id, array( 'remember' => true ) +
$metadata );
1555 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1556 'provider' => $provider,
1559 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1560 $this->assertTrue( $info->wasRemembered() );
1561 $this->assertSame( array(), $logger->getBuffer() );
1563 $this->store
->setSessionMeta( $id, array( 'remember' => false ) +
$metadata );
1564 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1565 'provider' => $provider,
1567 'userInfo' => $userInfo
1569 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1570 $this->assertTrue( $info->wasRemembered() );
1571 $this->assertSame( array(), $logger->getBuffer() );
1573 // forceHTTPS from session
1574 $this->store
->setSessionMeta( $id, $metadata );
1575 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1576 'provider' => $provider,
1578 'userInfo' => $userInfo
1580 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1581 $this->assertFalse( $info->forceHTTPS() );
1582 $this->assertSame( array(), $logger->getBuffer() );
1584 $this->store
->setSessionMeta( $id, array( 'forceHTTPS' => true ) +
$metadata );
1585 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1586 'provider' => $provider,
1588 'userInfo' => $userInfo
1590 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1591 $this->assertTrue( $info->forceHTTPS() );
1592 $this->assertSame( array(), $logger->getBuffer() );
1594 $this->store
->setSessionMeta( $id, array( 'forceHTTPS' => false ) +
$metadata );
1595 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1596 'provider' => $provider,
1598 'userInfo' => $userInfo,
1599 'forceHTTPS' => true
1601 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1602 $this->assertTrue( $info->forceHTTPS() );
1603 $this->assertSame( array(), $logger->getBuffer() );
1605 // "Persist" flag from session
1606 $this->store
->setSessionMeta( $id, $metadata );
1607 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1608 'provider' => $provider,
1610 'userInfo' => $userInfo
1612 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1613 $this->assertFalse( $info->wasPersisted() );
1614 $this->assertSame( array(), $logger->getBuffer() );
1616 $this->store
->setSessionMeta( $id, array( 'persisted' => true ) +
$metadata );
1617 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1618 'provider' => $provider,
1620 'userInfo' => $userInfo
1622 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1623 $this->assertTrue( $info->wasPersisted() );
1624 $this->assertSame( array(), $logger->getBuffer() );
1626 $this->store
->setSessionMeta( $id, array( 'persisted' => false ) +
$metadata );
1627 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1628 'provider' => $provider,
1630 'userInfo' => $userInfo,
1633 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1634 $this->assertTrue( $info->wasPersisted() );
1635 $this->assertSame( array(), $logger->getBuffer() );
1637 // Provider refreshSessionInfo() returning false
1638 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1639 'provider' => $provider3,
1641 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1642 $this->assertSame( array(), $logger->getBuffer() );
1647 $data = array( 'foo' => 1 );
1648 $this->store
->setSession( $id, array( 'metadata' => $metadata, 'data' => $data ) );
1649 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1650 'provider' => $provider,
1652 'userInfo' => $userInfo
1654 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1655 'SessionCheckInfo' => array( function ( &$reason, $i, $r, $m, $d ) use (
1656 $that, $info, $metadata, $data, $request, &$called
1658 $that->assertSame( $info->getId(), $i->getId() );
1659 $that->assertSame( $info->getProvider(), $i->getProvider() );
1660 $that->assertSame( $info->getUserInfo(), $i->getUserInfo() );
1661 $that->assertSame( $request, $r );
1662 $that->assertEquals( $metadata, $m );
1663 $that->assertEquals( $data, $d );
1668 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1669 $this->assertTrue( $called );
1670 $this->assertSame( array(
1671 array( LogLevel
::WARNING
, 'Session X: Hook aborted' ),
1672 ), $logger->getBuffer() );
1673 $logger->clearBuffer();