3 namespace MediaWiki\Session
;
13 * @covers MediaWiki\Session\SessionManager
15 class SessionManagerTest
extends MediaWikiTestCase
{
17 protected $config, $logger, $store;
19 protected function getManager() {
20 \ObjectCache
::$instances['testSessionStore'] = new TestBagOStuff();
21 $this->config
= new \
HashConfig( array(
22 'LanguageCode' => 'en',
23 'SessionCacheType' => 'testSessionStore',
24 'ObjectCacheSessionExpiry' => 100,
25 'SessionProviders' => array(
26 array( 'class' => 'DummySessionProvider' ),
29 $this->logger
= new \
TestLogger( false, function ( $m ) {
30 return substr( $m, 0, 15 ) === 'SessionBackend ' ?
null : $m;
32 $this->store
= new TestBagOStuff();
34 return new SessionManager( array(
35 'config' => $this->config
,
36 'logger' => $this->logger
,
37 'store' => $this->store
,
41 protected function objectCacheDef( $object ) {
42 return array( 'factory' => function () use ( $object ) {
47 public function testSingleton() {
48 $reset = TestUtils
::setSessionManagerSingleton( null );
50 $singleton = SessionManager
::singleton();
51 $this->assertInstanceOf( 'MediaWiki\\Session\\SessionManager', $singleton );
52 $this->assertSame( $singleton, SessionManager
::singleton() );
55 public function testGetGlobalSession() {
56 $context = \RequestContext
::getMain();
58 if ( !PHPSessionHandler
::isInstalled() ) {
59 PHPSessionHandler
::install( SessionManager
::singleton() );
61 $rProp = new \
ReflectionProperty( 'MediaWiki\\Session\\PHPSessionHandler', 'instance' );
62 $rProp->setAccessible( true );
63 $handler = \TestingAccessWrapper
::newFromObject( $rProp->getValue() );
64 $oldEnable = $handler->enable
;
65 $reset[] = new \
ScopedCallback( function () use ( $handler, $oldEnable ) {
66 if ( $handler->enable
) {
67 session_write_close();
69 $handler->enable
= $oldEnable;
71 $reset[] = TestUtils
::setSessionManagerSingleton( $this->getManager() );
73 $handler->enable
= true;
74 $request = new \
FauxRequest();
75 $context->setRequest( $request );
76 $id = $request->getSession()->getId();
79 $session = SessionManager
::getGlobalSession();
80 $this->assertSame( $id, $session->getId() );
82 session_id( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
83 $session = SessionManager
::getGlobalSession();
84 $this->assertSame( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', $session->getId() );
85 $this->assertSame( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', $request->getSession()->getId() );
87 session_write_close();
88 $handler->enable
= false;
89 $request = new \
FauxRequest();
90 $context->setRequest( $request );
91 $id = $request->getSession()->getId();
94 $session = SessionManager
::getGlobalSession();
95 $this->assertSame( $id, $session->getId() );
97 session_id( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
98 $session = SessionManager
::getGlobalSession();
99 $this->assertSame( $id, $session->getId() );
100 $this->assertSame( $id, $request->getSession()->getId() );
103 public function testConstructor() {
104 $manager = \TestingAccessWrapper
::newFromObject( $this->getManager() );
105 $this->assertSame( $this->config
, $manager->config
);
106 $this->assertSame( $this->logger
, $manager->logger
);
107 $this->assertSame( $this->store
, $manager->store
);
109 $manager = \TestingAccessWrapper
::newFromObject( new SessionManager() );
110 $this->assertSame( \RequestContext
::getMain()->getConfig(), $manager->config
);
112 $manager = \TestingAccessWrapper
::newFromObject( new SessionManager( array(
113 'config' => $this->config
,
115 $this->assertSame( \ObjectCache
::$instances['testSessionStore'], $manager->store
);
118 'config' => '$options[\'config\'] must be an instance of Config',
119 'logger' => '$options[\'logger\'] must be an instance of LoggerInterface',
120 'store' => '$options[\'store\'] must be an instance of BagOStuff',
121 ) as $key => $error ) {
123 new SessionManager( array( $key => new \stdClass
) );
124 $this->fail( 'Expected exception not thrown' );
125 } catch ( \InvalidArgumentException
$ex ) {
126 $this->assertSame( $error, $ex->getMessage() );
131 public function testGetSessionForRequest() {
132 $manager = $this->getManager();
133 $request = new \
FauxRequest();
137 $idEmpty = 'empty-session-------------------';
139 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
141 array( 'provideSessionInfo', 'newSessionInfo', '__toString', 'describe' )
144 $provider1 = $providerBuilder->getMock();
145 $provider1->expects( $this->any() )->method( 'provideSessionInfo' )
146 ->with( $this->identicalTo( $request ) )
147 ->will( $this->returnCallback( function ( $request ) {
148 return $request->info1
;
150 $provider1->expects( $this->any() )->method( 'newSessionInfo' )
151 ->will( $this->returnCallback( function () use ( $idEmpty, $provider1 ) {
152 return new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
153 'provider' => $provider1,
159 $provider1->expects( $this->any() )->method( '__toString' )
160 ->will( $this->returnValue( 'Provider1' ) );
161 $provider1->expects( $this->any() )->method( 'describe' )
162 ->will( $this->returnValue( '#1 sessions' ) );
164 $provider2 = $providerBuilder->getMock();
165 $provider2->expects( $this->any() )->method( 'provideSessionInfo' )
166 ->with( $this->identicalTo( $request ) )
167 ->will( $this->returnCallback( function ( $request ) {
168 return $request->info2
;
170 $provider2->expects( $this->any() )->method( '__toString' )
171 ->will( $this->returnValue( 'Provider2' ) );
172 $provider2->expects( $this->any() )->method( 'describe' )
173 ->will( $this->returnValue( '#2 sessions' ) );
175 $this->config
->set( 'SessionProviders', array(
176 $this->objectCacheDef( $provider1 ),
177 $this->objectCacheDef( $provider2 ),
180 // No provider returns info
181 $request->info1
= null;
182 $request->info2
= null;
183 $session = $manager->getSessionForRequest( $request );
184 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
185 $this->assertSame( $idEmpty, $session->getId() );
187 // Both providers return info, picks best one
188 $request->info1
= new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, array(
189 'provider' => $provider1,
190 'id' => ( $id1 = $manager->generateSessionId() ),
194 $request->info2
= new SessionInfo( SessionInfo
::MIN_PRIORITY +
2, array(
195 'provider' => $provider2,
196 'id' => ( $id2 = $manager->generateSessionId() ),
200 $session = $manager->getSessionForRequest( $request );
201 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
202 $this->assertSame( $id2, $session->getId() );
204 $request->info1
= new SessionInfo( SessionInfo
::MIN_PRIORITY +
2, array(
205 'provider' => $provider1,
206 'id' => ( $id1 = $manager->generateSessionId() ),
210 $request->info2
= new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, array(
211 'provider' => $provider2,
212 'id' => ( $id2 = $manager->generateSessionId() ),
216 $session = $manager->getSessionForRequest( $request );
217 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
218 $this->assertSame( $id1, $session->getId() );
221 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, array(
222 'provider' => $provider1,
223 'id' => ( $id1 = $manager->generateSessionId() ),
225 'userInfo' => UserInfo
::newAnonymous(),
228 $request->info2
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, array(
229 'provider' => $provider2,
230 'id' => ( $id2 = $manager->generateSessionId() ),
232 'userInfo' => UserInfo
::newAnonymous(),
236 $manager->getSessionForRequest( $request );
237 $this->fail( 'Expcected exception not thrown' );
238 } catch ( \OverFlowException
$ex ) {
239 $this->assertStringStartsWith(
240 'Multiple sessions for this request tied for top priority: ',
243 $this->assertCount( 2, $ex->sessionInfos
);
244 $this->assertContains( $request->info1
, $ex->sessionInfos
);
245 $this->assertContains( $request->info2
, $ex->sessionInfos
);
249 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, array(
250 'provider' => $provider2,
251 'id' => ( $id1 = $manager->generateSessionId() ),
255 $request->info2
= null;
257 $manager->getSessionForRequest( $request );
258 $this->fail( 'Expcected exception not thrown' );
259 } catch ( \UnexpectedValueException
$ex ) {
261 'Provider1 returned session info for a different provider: ' . $request->info1
,
266 // Unusable session info
267 $this->logger
->setCollect( true );
268 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, array(
269 'provider' => $provider1,
270 'id' => ( $id1 = $manager->generateSessionId() ),
272 'userInfo' => UserInfo
::newFromName( 'UTSysop', false ),
275 $request->info2
= new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
276 'provider' => $provider2,
277 'id' => ( $id2 = $manager->generateSessionId() ),
281 $session = $manager->getSessionForRequest( $request );
282 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
283 $this->assertSame( $id2, $session->getId() );
284 $this->logger
->setCollect( false );
286 // Unpersisted session ID
287 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, array(
288 'provider' => $provider1,
289 'id' => ( $id1 = $manager->generateSessionId() ),
290 'persisted' => false,
291 'userInfo' => UserInfo
::newFromName( 'UTSysop', true ),
294 $request->info2
= null;
295 $session = $manager->getSessionForRequest( $request );
296 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
297 $this->assertSame( $id1, $session->getId() );
299 $this->assertTrue( $session->isPersistent(), 'sanity check' );
302 public function testGetSessionById() {
303 $manager = $this->getManager();
305 $manager->getSessionById( 'bad' );
306 $this->fail( 'Expected exception not thrown' );
307 } catch ( \InvalidArgumentException
$ex ) {
308 $this->assertSame( 'Invalid session ID', $ex->getMessage() );
311 // Unknown session ID
312 $id = $manager->generateSessionId();
313 $session = $manager->getSessionById( $id, true );
314 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
315 $this->assertSame( $id, $session->getId() );
317 $id = $manager->generateSessionId();
318 $this->assertNull( $manager->getSessionById( $id, false ) );
320 // Known but unloadable session ID
321 $this->logger
->setCollect( true );
322 $id = $manager->generateSessionId();
323 $this->store
->setSession( $id, array( 'metadata' => array(
324 'userId' => User
::idFromName( 'UTSysop' ),
325 'userToken' => 'bad',
328 $this->assertNull( $manager->getSessionById( $id, true ) );
329 $this->assertNull( $manager->getSessionById( $id, false ) );
330 $this->logger
->setCollect( false );
333 $this->store
->setSession( $id, array() );
334 $session = $manager->getSessionById( $id, false );
335 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
336 $this->assertSame( $id, $session->getId() );
339 public function testGetEmptySession() {
340 $manager = $this->getManager();
341 $pmanager = \TestingAccessWrapper
::newFromObject( $manager );
342 $request = new \
FauxRequest();
344 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
345 ->setMethods( array( 'provideSessionInfo', 'newSessionInfo', '__toString' ) );
351 $provider1 = $providerBuilder->getMock();
352 $provider1->expects( $this->any() )->method( 'provideSessionInfo' )
353 ->will( $this->returnValue( null ) );
354 $provider1->expects( $this->any() )->method( 'newSessionInfo' )
355 ->with( $this->callback( function ( $id ) use ( &$expectId ) {
356 return $id === $expectId;
358 ->will( $this->returnCallback( function () use ( &$info1 ) {
361 $provider1->expects( $this->any() )->method( '__toString' )
362 ->will( $this->returnValue( 'MockProvider1' ) );
364 $provider2 = $providerBuilder->getMock();
365 $provider2->expects( $this->any() )->method( 'provideSessionInfo' )
366 ->will( $this->returnValue( null ) );
367 $provider2->expects( $this->any() )->method( 'newSessionInfo' )
368 ->with( $this->callback( function ( $id ) use ( &$expectId ) {
369 return $id === $expectId;
371 ->will( $this->returnCallback( function () use ( &$info2 ) {
374 $provider1->expects( $this->any() )->method( '__toString' )
375 ->will( $this->returnValue( 'MockProvider2' ) );
377 $this->config
->set( 'SessionProviders', array(
378 $this->objectCacheDef( $provider1 ),
379 $this->objectCacheDef( $provider2 ),
387 $manager->getEmptySession();
388 $this->fail( 'Expected exception not thrown' );
389 } catch ( \UnexpectedValueException
$ex ) {
391 'No provider could provide an empty session!',
398 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
399 'provider' => $provider1,
400 'id' => 'empty---------------------------',
405 $session = $manager->getEmptySession();
406 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
407 $this->assertSame( 'empty---------------------------', $session->getId() );
410 $expectId = 'expected------------------------';
411 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
412 'provider' => $provider1,
418 $session = $pmanager->getEmptySessionInternal( null, $expectId );
419 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
420 $this->assertSame( $expectId, $session->getId() );
423 $expectId = 'expected-----------------------2';
424 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
425 'provider' => $provider1,
426 'id' => "un$expectId",
432 $pmanager->getEmptySessionInternal( null, $expectId );
433 $this->fail( 'Expected exception not thrown' );
434 } catch ( \UnexpectedValueException
$ex ) {
436 'MockProvider1 returned empty session info with a wrong id: ' .
437 "un$expectId != $expectId",
443 $expectId = 'expected-----------------------2';
444 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
445 'provider' => $provider1,
451 $pmanager->getEmptySessionInternal( null, $expectId );
452 $this->fail( 'Expected exception not thrown' );
453 } catch ( \UnexpectedValueException
$ex ) {
455 'MockProvider1 returned empty session info with id flagged unsafe',
462 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
463 'provider' => $provider2,
464 'id' => 'empty---------------------------',
470 $manager->getEmptySession();
471 $this->fail( 'Expected exception not thrown' );
472 } catch ( \UnexpectedValueException
$ex ) {
474 'MockProvider1 returned an empty session info for a different provider: ' . $info1,
479 // Highest priority wins
481 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, array(
482 'provider' => $provider1,
483 'id' => 'empty1--------------------------',
487 $info2 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
488 'provider' => $provider2,
489 'id' => 'empty2--------------------------',
493 $session = $manager->getEmptySession();
494 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
495 $this->assertSame( 'empty1--------------------------', $session->getId() );
498 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, array(
499 'provider' => $provider1,
500 'id' => 'empty1--------------------------',
504 $info2 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
2, array(
505 'provider' => $provider2,
506 'id' => 'empty2--------------------------',
510 $session = $manager->getEmptySession();
511 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
512 $this->assertSame( 'empty2--------------------------', $session->getId() );
514 // Tied priorities throw an exception
516 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
517 'provider' => $provider1,
518 'id' => 'empty1--------------------------',
520 'userInfo' => UserInfo
::newAnonymous(),
523 $info2 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
524 'provider' => $provider2,
525 'id' => 'empty2--------------------------',
527 'userInfo' => UserInfo
::newAnonymous(),
531 $manager->getEmptySession();
532 $this->fail( 'Expected exception not thrown' );
533 } catch ( \UnexpectedValueException
$ex ) {
534 $this->assertStringStartsWith(
535 'Multiple empty sessions tied for top priority: ',
542 $pmanager->getEmptySessionInternal( null, 'bad' );
543 $this->fail( 'Expected exception not thrown' );
544 } catch ( \InvalidArgumentException
$ex ) {
545 $this->assertSame( 'Invalid session ID', $ex->getMessage() );
548 // Session already exists
549 $expectId = 'expected-----------------------3';
550 $this->store
->setSessionMeta( $expectId, array(
551 'provider' => 'MockProvider2',
557 $pmanager->getEmptySessionInternal( null, $expectId );
558 $this->fail( 'Expected exception not thrown' );
559 } catch ( \InvalidArgumentException
$ex ) {
560 $this->assertSame( 'Session ID already exists', $ex->getMessage() );
564 public function testGetVaryHeaders() {
565 $manager = $this->getManager();
567 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
568 ->setMethods( array( 'getVaryHeaders', '__toString' ) );
570 $provider1 = $providerBuilder->getMock();
571 $provider1->expects( $this->once() )->method( 'getVaryHeaders' )
572 ->will( $this->returnValue( array(
574 'Bar' => array( 'X', 'Bar1' ),
577 $provider1->expects( $this->any() )->method( '__toString' )
578 ->will( $this->returnValue( 'MockProvider1' ) );
580 $provider2 = $providerBuilder->getMock();
581 $provider2->expects( $this->once() )->method( 'getVaryHeaders' )
582 ->will( $this->returnValue( array(
584 'Bar' => array( 'X', 'Bar2' ),
585 'Quux' => array( 'Quux' ),
587 $provider2->expects( $this->any() )->method( '__toString' )
588 ->will( $this->returnValue( 'MockProvider2' ) );
590 $this->config
->set( 'SessionProviders', array(
591 $this->objectCacheDef( $provider1 ),
592 $this->objectCacheDef( $provider2 ),
597 'Bar' => array( 'X', 'Bar1', 3 => 'Bar2' ),
598 'Quux' => array( 'Quux' ),
602 $this->assertEquals( $expect, $manager->getVaryHeaders() );
604 // Again, to ensure it's cached
605 $this->assertEquals( $expect, $manager->getVaryHeaders() );
608 public function testGetVaryCookies() {
609 $manager = $this->getManager();
611 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
612 ->setMethods( array( 'getVaryCookies', '__toString' ) );
614 $provider1 = $providerBuilder->getMock();
615 $provider1->expects( $this->once() )->method( 'getVaryCookies' )
616 ->will( $this->returnValue( array( 'Foo', 'Bar' ) ) );
617 $provider1->expects( $this->any() )->method( '__toString' )
618 ->will( $this->returnValue( 'MockProvider1' ) );
620 $provider2 = $providerBuilder->getMock();
621 $provider2->expects( $this->once() )->method( 'getVaryCookies' )
622 ->will( $this->returnValue( array( 'Foo', 'Baz' ) ) );
623 $provider2->expects( $this->any() )->method( '__toString' )
624 ->will( $this->returnValue( 'MockProvider2' ) );
626 $this->config
->set( 'SessionProviders', array(
627 $this->objectCacheDef( $provider1 ),
628 $this->objectCacheDef( $provider2 ),
631 $expect = array( 'Foo', 'Bar', 'Baz' );
633 $this->assertEquals( $expect, $manager->getVaryCookies() );
635 // Again, to ensure it's cached
636 $this->assertEquals( $expect, $manager->getVaryCookies() );
639 public function testGetProviders() {
640 $realManager = $this->getManager();
641 $manager = \TestingAccessWrapper
::newFromObject( $realManager );
643 $this->config
->set( 'SessionProviders', array(
644 array( 'class' => 'DummySessionProvider' ),
646 $providers = $manager->getProviders();
647 $this->assertArrayHasKey( 'DummySessionProvider', $providers );
648 $provider = \TestingAccessWrapper
::newFromObject( $providers['DummySessionProvider'] );
649 $this->assertSame( $manager->logger
, $provider->logger
);
650 $this->assertSame( $manager->config
, $provider->config
);
651 $this->assertSame( $realManager, $provider->getManager() );
653 $this->config
->set( 'SessionProviders', array(
654 array( 'class' => 'DummySessionProvider' ),
655 array( 'class' => 'DummySessionProvider' ),
657 $manager->sessionProviders
= null;
659 $manager->getProviders();
660 $this->fail( 'Expected exception not thrown' );
661 } catch ( \UnexpectedValueException
$ex ) {
663 'Duplicate provider name "DummySessionProvider"',
669 public function testShutdown() {
670 $manager = \TestingAccessWrapper
::newFromObject( $this->getManager() );
671 $manager->setLogger( new \Psr\Log\
NullLogger() );
673 $mock = $this->getMock( 'stdClass', array( 'save' ) );
674 $mock->expects( $this->once() )->method( 'save' );
676 $manager->allSessionBackends
= array( $mock );
677 $manager->shutdown();
680 public function testGetSessionFromInfo() {
681 $manager = \TestingAccessWrapper
::newFromObject( $this->getManager() );
682 $request = new \
FauxRequest();
684 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
686 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
687 'provider' => $manager->getProvider( 'DummySessionProvider' ),
690 'userInfo' => UserInfo
::newFromName( 'UTSysop', true ),
693 \TestingAccessWrapper
::newFromObject( $info )->idIsSafe
= true;
694 $session1 = \TestingAccessWrapper
::newFromObject(
695 $manager->getSessionFromInfo( $info, $request )
697 $session2 = \TestingAccessWrapper
::newFromObject(
698 $manager->getSessionFromInfo( $info, $request )
701 $this->assertSame( $session1->backend
, $session2->backend
);
702 $this->assertNotEquals( $session1->index
, $session2->index
);
703 $this->assertSame( $session1->getSessionId(), $session2->getSessionId() );
704 $this->assertSame( $id, $session1->getId() );
706 \TestingAccessWrapper
::newFromObject( $info )->idIsSafe
= false;
707 $session3 = $manager->getSessionFromInfo( $info, $request );
708 $this->assertNotSame( $id, $session3->getId() );
711 public function testBackendRegistration() {
712 $manager = $this->getManager();
714 $session = $manager->getSessionForRequest( new \FauxRequest
);
715 $backend = \TestingAccessWrapper
::newFromObject( $session )->backend
;
716 $sessionId = $session->getSessionId();
717 $id = (string)$sessionId;
719 $this->assertSame( $sessionId, $manager->getSessionById( $id, true )->getSessionId() );
721 $manager->changeBackendId( $backend );
722 $this->assertSame( $sessionId, $session->getSessionId() );
723 $this->assertNotEquals( $id, (string)$sessionId );
724 $id = (string)$sessionId;
726 $this->assertSame( $sessionId, $manager->getSessionById( $id, true )->getSessionId() );
728 // Destruction of the session here causes the backend to be deregistered
732 $manager->changeBackendId( $backend );
733 $this->fail( 'Expected exception not thrown' );
734 } catch ( \InvalidArgumentException
$ex ) {
736 'Backend was not registered with this SessionManager', $ex->getMessage()
741 $manager->deregisterSessionBackend( $backend );
742 $this->fail( 'Expected exception not thrown' );
743 } catch ( \InvalidArgumentException
$ex ) {
745 'Backend was not registered with this SessionManager', $ex->getMessage()
749 $session = $manager->getSessionById( $id, true );
750 $this->assertSame( $sessionId, $session->getSessionId() );
753 public function testGenerateSessionId() {
754 $manager = $this->getManager();
756 $id = $manager->generateSessionId();
757 $this->assertTrue( SessionManager
::validateSessionId( $id ), "Generated ID: $id" );
760 public function testAutoCreateUser() {
761 global $wgGroupPermissions;
763 \ObjectCache
::$instances[__METHOD__
] = new TestBagOStuff();
764 $this->setMwGlobals( array( 'wgMainCacheType' => __METHOD__
) );
765 $this->setMWGlobals( array(
766 'wgAuth' => new AuthPlugin
,
769 $this->stashMwGlobals( array( 'wgGroupPermissions' ) );
770 $wgGroupPermissions['*']['createaccount'] = true;
771 $wgGroupPermissions['*']['autocreateaccount'] = false;
773 // Replace the global singleton with one configured for testing
774 $manager = $this->getManager();
775 $reset = TestUtils
::setSessionManagerSingleton( $manager );
777 $logger = new \
TestLogger( true, function ( $m ) {
778 if ( substr( $m, 0, 15 ) === 'SessionBackend ' ) {
782 $m = str_replace( 'MediaWiki\Session\SessionManager::autoCreateUser: ', '', $m );
785 $manager->setLogger( $logger );
787 $session = SessionManager
::getGlobalSession();
789 // Can't create an already-existing user
790 $user = User
::newFromName( 'UTSysop' );
791 $id = $user->getId();
792 $this->assertFalse( $manager->autoCreateUser( $user ) );
793 $this->assertSame( $id, $user->getId() );
794 $this->assertSame( 'UTSysop', $user->getName() );
795 $this->assertSame( array(), $logger->getBuffer() );
796 $logger->clearBuffer();
798 // Sanity check that creation works at all
799 $user = User
::newFromName( 'UTSessionAutoCreate1' );
800 $this->assertSame( 0, $user->getId(), 'sanity check' );
801 $this->assertTrue( $manager->autoCreateUser( $user ) );
802 $this->assertNotEquals( 0, $user->getId() );
803 $this->assertSame( 'UTSessionAutoCreate1', $user->getName() );
805 $user->getId(), User
::idFromName( 'UTSessionAutoCreate1', User
::READ_LATEST
)
807 $this->assertSame( array(
808 array( LogLevel
::INFO
, 'creating new user ({username}) - from: {url}' ),
809 ), $logger->getBuffer() );
810 $logger->clearBuffer();
812 // Check lack of permissions
813 $wgGroupPermissions['*']['createaccount'] = false;
814 $wgGroupPermissions['*']['autocreateaccount'] = false;
815 $user = User
::newFromName( 'UTDoesNotExist' );
816 $this->assertFalse( $manager->autoCreateUser( $user ) );
817 $this->assertSame( 0, $user->getId() );
818 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
819 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
821 $this->assertSame( array(
824 'user is blocked from this wiki, blacklisting',
826 ), $logger->getBuffer() );
827 $logger->clearBuffer();
829 // Check other permission
830 $wgGroupPermissions['*']['createaccount'] = false;
831 $wgGroupPermissions['*']['autocreateaccount'] = true;
832 $user = User
::newFromName( 'UTSessionAutoCreate2' );
833 $this->assertSame( 0, $user->getId(), 'sanity check' );
834 $this->assertTrue( $manager->autoCreateUser( $user ) );
835 $this->assertNotEquals( 0, $user->getId() );
836 $this->assertSame( 'UTSessionAutoCreate2', $user->getName() );
838 $user->getId(), User
::idFromName( 'UTSessionAutoCreate2', User
::READ_LATEST
)
840 $this->assertSame( array(
841 array( LogLevel
::INFO
, 'creating new user ({username}) - from: {url}' ),
842 ), $logger->getBuffer() );
843 $logger->clearBuffer();
845 // Test account-creation block
847 $block = new \
Block( array(
848 'address' => $anon->getName(),
850 'reason' => __METHOD__
,
851 'expiry' => time() +
100500,
852 'createAccount' => true,
855 $this->assertInstanceOf( 'Block', $anon->isBlockedFromCreateAccount(), 'sanity check' );
856 $reset2 = new \
ScopedCallback( array( $block, 'delete' ) );
857 $user = User
::newFromName( 'UTDoesNotExist' );
858 $this->assertFalse( $manager->autoCreateUser( $user ) );
859 $this->assertSame( 0, $user->getId() );
860 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
861 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
862 \ScopedCallback
::consume( $reset2 );
864 $this->assertSame( array(
865 array( LogLevel
::DEBUG
, 'user is blocked from this wiki, blacklisting' ),
866 ), $logger->getBuffer() );
867 $logger->clearBuffer();
869 // Sanity check that creation still works
870 $user = User
::newFromName( 'UTSessionAutoCreate3' );
871 $this->assertSame( 0, $user->getId(), 'sanity check' );
872 $this->assertTrue( $manager->autoCreateUser( $user ) );
873 $this->assertNotEquals( 0, $user->getId() );
874 $this->assertSame( 'UTSessionAutoCreate3', $user->getName() );
876 $user->getId(), User
::idFromName( 'UTSessionAutoCreate3', User
::READ_LATEST
)
878 $this->assertSame( array(
879 array( LogLevel
::INFO
, 'creating new user ({username}) - from: {url}' ),
880 ), $logger->getBuffer() );
881 $logger->clearBuffer();
883 // Test prevention by AuthPlugin
885 $oldWgAuth = $wgAuth;
886 $mockWgAuth = $this->getMock( 'AuthPlugin', array( 'autoCreate' ) );
887 $mockWgAuth->expects( $this->once() )->method( 'autoCreate' )
888 ->will( $this->returnValue( false ) );
889 $this->setMwGlobals( array(
890 'wgAuth' => $mockWgAuth,
892 $user = User
::newFromName( 'UTDoesNotExist' );
893 $this->assertFalse( $manager->autoCreateUser( $user ) );
894 $this->assertSame( 0, $user->getId() );
895 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
896 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
897 $this->setMwGlobals( array(
898 'wgAuth' => $oldWgAuth,
901 $this->assertSame( array(
902 array( LogLevel
::DEBUG
, 'denied by AuthPlugin' ),
903 ), $logger->getBuffer() );
904 $logger->clearBuffer();
906 // Test prevention by wfReadOnly()
907 $this->setMwGlobals( array(
908 'wgReadOnly' => 'Because',
910 $user = User
::newFromName( 'UTDoesNotExist' );
911 $this->assertFalse( $manager->autoCreateUser( $user ) );
912 $this->assertSame( 0, $user->getId() );
913 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
914 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
915 $this->setMwGlobals( array(
916 'wgReadOnly' => false,
919 $this->assertSame( array(
920 array( LogLevel
::DEBUG
, 'denied by wfReadOnly()' ),
921 ), $logger->getBuffer() );
922 $logger->clearBuffer();
924 // Test prevention by a previous session
925 $session->set( 'MWSession::AutoCreateBlacklist', 'test' );
926 $user = User
::newFromName( 'UTDoesNotExist' );
927 $this->assertFalse( $manager->autoCreateUser( $user ) );
928 $this->assertSame( 0, $user->getId() );
929 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
930 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
932 $this->assertSame( array(
933 array( LogLevel
::DEBUG
, 'blacklisted in session (test)' ),
934 ), $logger->getBuffer() );
935 $logger->clearBuffer();
937 // Test uncreatable name
938 $user = User
::newFromName( 'UTDoesNotExist@' );
939 $this->assertFalse( $manager->autoCreateUser( $user ) );
940 $this->assertSame( 0, $user->getId() );
941 $this->assertNotSame( 'UTDoesNotExist@', $user->getName() );
942 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
944 $this->assertSame( array(
945 array( LogLevel
::DEBUG
, 'Invalid username, blacklisting' ),
946 ), $logger->getBuffer() );
947 $logger->clearBuffer();
949 // Test AbortAutoAccount hook
950 $mock = $this->getMock( __CLASS__
, array( 'onAbortAutoAccount' ) );
951 $mock->expects( $this->once() )->method( 'onAbortAutoAccount' )
952 ->will( $this->returnCallback( function ( User
$user, &$msg ) {
956 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array( $mock ) ) );
957 $user = User
::newFromName( 'UTDoesNotExist' );
958 $this->assertFalse( $manager->autoCreateUser( $user ) );
959 $this->assertSame( 0, $user->getId() );
960 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
961 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
962 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array() ) );
964 $this->assertSame( array(
965 array( LogLevel
::DEBUG
, 'denied by hook: No way!' ),
966 ), $logger->getBuffer() );
967 $logger->clearBuffer();
969 // Test AbortAutoAccount hook screwing up the name
970 $mock = $this->getMock( 'stdClass', array( 'onAbortAutoAccount' ) );
971 $mock->expects( $this->once() )->method( 'onAbortAutoAccount' )
972 ->will( $this->returnCallback( function ( User
$user ) {
973 $user->setName( 'UTDoesNotExistEither' );
975 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array( $mock ) ) );
977 $user = User
::newFromName( 'UTDoesNotExist' );
978 $manager->autoCreateUser( $user );
979 $this->fail( 'Expected exception not thrown' );
980 } catch ( \UnexpectedValueException
$ex ) {
982 'AbortAutoAccount hook tried to change the user name',
986 $this->assertSame( 0, $user->getId() );
987 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
988 $this->assertNotSame( 'UTDoesNotExistEither', $user->getName() );
989 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
990 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExistEither', User
::READ_LATEST
) );
991 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array() ) );
993 $this->assertSame( array(), $logger->getBuffer() );
994 $logger->clearBuffer();
996 // Test for "exception backoff"
997 $user = User
::newFromName( 'UTDoesNotExist' );
998 $cache = \ObjectCache
::getLocalClusterInstance();
999 $backoffKey = wfMemcKey( 'MWSession', 'autocreate-failed', md5( $user->getName() ) );
1000 $cache->set( $backoffKey, 1, 60 * 10 );
1001 $this->assertFalse( $manager->autoCreateUser( $user ) );
1002 $this->assertSame( 0, $user->getId() );
1003 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
1004 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
1005 $cache->delete( $backoffKey );
1007 $this->assertSame( array(
1008 array( LogLevel
::DEBUG
, 'denied by prior creation attempt failures' ),
1009 ), $logger->getBuffer() );
1010 $logger->clearBuffer();
1012 // Sanity check that creation still works, and test completion hook
1013 $cb = $this->callback( function ( User
$user ) {
1014 $this->assertNotEquals( 0, $user->getId() );
1015 $this->assertSame( 'UTSessionAutoCreate4', $user->getName() );
1016 $this->assertEquals(
1017 $user->getId(), User
::idFromName( 'UTSessionAutoCreate4', User
::READ_LATEST
)
1021 $mock = $this->getMock( 'stdClass',
1022 array( 'onAuthPluginAutoCreate', 'onLocalUserCreated' ) );
1023 $mock->expects( $this->once() )->method( 'onAuthPluginAutoCreate' )
1025 $mock->expects( $this->once() )->method( 'onLocalUserCreated' )
1026 ->with( $cb, $this->identicalTo( true ) );
1027 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1028 'AuthPluginAutoCreate' => array( $mock ),
1029 'LocalUserCreated' => array( $mock ),
1031 $user = User
::newFromName( 'UTSessionAutoCreate4' );
1032 $this->assertSame( 0, $user->getId(), 'sanity check' );
1033 $this->assertTrue( $manager->autoCreateUser( $user ) );
1034 $this->assertNotEquals( 0, $user->getId() );
1035 $this->assertSame( 'UTSessionAutoCreate4', $user->getName() );
1036 $this->assertEquals(
1038 User
::idFromName( 'UTSessionAutoCreate4', User
::READ_LATEST
)
1040 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1041 'AuthPluginAutoCreate' => array(),
1042 'LocalUserCreated' => array(),
1044 $this->assertSame( array(
1045 array( LogLevel
::INFO
, 'creating new user ({username}) - from: {url}' ),
1046 ), $logger->getBuffer() );
1047 $logger->clearBuffer();
1050 public function onAbortAutoAccount( User
$user, &$msg ) {
1053 public function testPreventSessionsForUser() {
1054 $manager = $this->getManager();
1056 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
1057 ->setMethods( array( 'preventSessionsForUser', '__toString' ) );
1059 $provider1 = $providerBuilder->getMock();
1060 $provider1->expects( $this->once() )->method( 'preventSessionsForUser' )
1061 ->with( $this->equalTo( 'UTSysop' ) );
1062 $provider1->expects( $this->any() )->method( '__toString' )
1063 ->will( $this->returnValue( 'MockProvider1' ) );
1065 $this->config
->set( 'SessionProviders', array(
1066 $this->objectCacheDef( $provider1 ),
1069 $this->assertFalse( $manager->isUserSessionPrevented( 'UTSysop' ) );
1070 $manager->preventSessionsForUser( 'UTSysop' );
1071 $this->assertTrue( $manager->isUserSessionPrevented( 'UTSysop' ) );
1074 public function testLoadSessionInfoFromStore() {
1075 $manager = $this->getManager();
1076 $logger = new \
TestLogger( true );
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(
1192 'Session "{session}": Unverified user provided and no metadata to auth it',
1194 ), $logger->getBuffer() );
1195 $logger->clearBuffer();
1197 // No metadata, missing data
1198 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1200 'userInfo' => $userInfo
1202 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1203 $this->assertSame( array(
1204 array( LogLevel
::WARNING
, 'Session "{session}": Null provider and no metadata' ),
1205 ), $logger->getBuffer() );
1206 $logger->clearBuffer();
1208 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1209 'provider' => $provider,
1212 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1213 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1214 $this->assertInstanceOf( 'MediaWiki\\Session\\UserInfo', $info->getUserInfo() );
1215 $this->assertTrue( $info->getUserInfo()->isVerified() );
1216 $this->assertTrue( $info->getUserInfo()->isAnon() );
1217 $this->assertFalse( $info->isIdSafe() );
1218 $this->assertSame( array(), $logger->getBuffer() );
1220 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1221 'provider' => $provider2,
1224 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1225 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1226 $this->assertSame( array(
1227 array( LogLevel
::INFO
, 'Session "{session}": No user provided and provider cannot set user' )
1228 ), $logger->getBuffer() );
1229 $logger->clearBuffer();
1231 // Incomplete/bad metadata
1232 $this->store
->setRawSession( $id, true );
1233 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1234 $this->assertSame( array(
1235 array( LogLevel
::WARNING
, 'Session "{session}": Bad data' ),
1236 ), $logger->getBuffer() );
1237 $logger->clearBuffer();
1239 $this->store
->setRawSession( $id, array( 'data' => array() ) );
1240 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1241 $this->assertSame( array(
1242 array( LogLevel
::WARNING
, 'Session "{session}": Bad data structure' ),
1243 ), $logger->getBuffer() );
1244 $logger->clearBuffer();
1246 $this->store
->deleteSession( $id );
1247 $this->store
->setRawSession( $id, array( 'metadata' => $metadata ) );
1248 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1249 $this->assertSame( array(
1250 array( LogLevel
::WARNING
, 'Session "{session}": Bad data structure' ),
1251 ), $logger->getBuffer() );
1252 $logger->clearBuffer();
1254 $this->store
->setRawSession( $id, array( 'metadata' => $metadata, 'data' => true ) );
1255 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1256 $this->assertSame( array(
1257 array( LogLevel
::WARNING
, 'Session "{session}": Bad data structure' ),
1258 ), $logger->getBuffer() );
1259 $logger->clearBuffer();
1261 $this->store
->setRawSession( $id, array( 'metadata' => true, 'data' => array() ) );
1262 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1263 $this->assertSame( array(
1264 array( LogLevel
::WARNING
, 'Session "{session}": Bad data structure' ),
1265 ), $logger->getBuffer() );
1266 $logger->clearBuffer();
1268 foreach ( $metadata as $key => $dummy ) {
1270 unset( $tmp[$key] );
1271 $this->store
->setRawSession( $id, array( 'metadata' => $tmp, 'data' => array() ) );
1272 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1273 $this->assertSame( array(
1274 array( LogLevel
::WARNING
, 'Session "{session}": Bad metadata' ),
1275 ), $logger->getBuffer() );
1276 $logger->clearBuffer();
1279 // Basic usage with metadata
1280 $this->store
->setRawSession( $id, array( 'metadata' => $metadata, 'data' => array() ) );
1281 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1282 'provider' => $provider,
1284 'userInfo' => $userInfo
1286 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1287 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1288 $this->assertTrue( $info->isIdSafe() );
1289 $this->assertSame( array(), $logger->getBuffer() );
1291 // Mismatched provider
1292 $this->store
->setSessionMeta( $id, array( 'provider' => 'Bad' ) +
$metadata );
1293 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1294 'provider' => $provider,
1296 'userInfo' => $userInfo
1298 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1299 $this->assertSame( array(
1300 array( LogLevel
::WARNING
, 'Session "{session}": Wrong provider Bad !== Mock' ),
1301 ), $logger->getBuffer() );
1302 $logger->clearBuffer();
1305 $this->store
->setSessionMeta( $id, array( 'provider' => 'Bad' ) +
$metadata );
1306 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1308 'userInfo' => $userInfo
1310 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1311 $this->assertSame( array(
1312 array( LogLevel
::WARNING
, 'Session "{session}": Unknown provider Bad' ),
1313 ), $logger->getBuffer() );
1314 $logger->clearBuffer();
1317 $this->store
->setSessionMeta( $id, $metadata );
1318 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1320 'userInfo' => $userInfo
1322 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1323 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1324 $this->assertTrue( $info->isIdSafe() );
1325 $this->assertSame( array(), $logger->getBuffer() );
1327 // Bad user metadata
1328 $this->store
->setSessionMeta( $id, array( 'userId' => -1, 'userToken' => null ) +
$metadata );
1329 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1330 'provider' => $provider,
1333 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1334 $this->assertSame( array(
1335 array( LogLevel
::ERROR
, 'Session "{session}": {exception}' ),
1336 ), $logger->getBuffer() );
1337 $logger->clearBuffer();
1339 $this->store
->setSessionMeta(
1340 $id, array( 'userId' => 0, 'userName' => '<X>', 'userToken' => null ) +
$metadata
1342 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1343 'provider' => $provider,
1346 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1347 $this->assertSame( array(
1348 array( LogLevel
::ERROR
, 'Session "{session}": {exception}', ),
1349 ), $logger->getBuffer() );
1350 $logger->clearBuffer();
1352 // Mismatched user by ID
1353 $this->store
->setSessionMeta(
1354 $id, array( 'userId' => $userInfo->getId() +
1, 'userToken' => null ) +
$metadata
1356 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1357 'provider' => $provider,
1359 'userInfo' => $userInfo
1361 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1362 $this->assertSame( array(
1363 array( LogLevel
::WARNING
, 'Session "{session}": User ID mismatch, {uid_a} !== {uid_b}' ),
1364 ), $logger->getBuffer() );
1365 $logger->clearBuffer();
1367 // Mismatched user by name
1368 $this->store
->setSessionMeta(
1369 $id, array( 'userId' => 0, 'userName' => 'X', 'userToken' => null ) +
$metadata
1371 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1372 'provider' => $provider,
1374 'userInfo' => $userInfo
1376 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1377 $this->assertSame( array(
1378 array( LogLevel
::WARNING
, 'Session "{session}": User name mismatch, {uname_a} !== {uname_b}' ),
1379 ), $logger->getBuffer() );
1380 $logger->clearBuffer();
1382 // ID matches, name doesn't
1383 $this->store
->setSessionMeta(
1384 $id, array( 'userId' => $userInfo->getId(), 'userName' => 'X', 'userToken' => null ) +
$metadata
1386 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1387 'provider' => $provider,
1389 'userInfo' => $userInfo
1391 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1392 $this->assertSame( array(
1395 'Session "{session}": User ID matched but name didn\'t (rename?), {uname_a} !== {uname_b}'
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(
1413 'Session "{session}": Metadata has an anonymous user, ' .
1414 'but a non-anon user was provided',
1416 ), $logger->getBuffer() );
1417 $logger->clearBuffer();
1419 // Lookup user by ID
1420 $this->store
->setSessionMeta( $id, array( 'userToken' => null ) +
$metadata );
1421 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1422 'provider' => $provider,
1425 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1426 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1427 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1428 $this->assertTrue( $info->isIdSafe() );
1429 $this->assertSame( array(), $logger->getBuffer() );
1431 // Lookup user by name
1432 $this->store
->setSessionMeta(
1433 $id, array( 'userId' => 0, 'userName' => 'UTSysop', 'userToken' => null ) +
$metadata
1435 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1436 'provider' => $provider,
1439 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1440 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1441 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1442 $this->assertTrue( $info->isIdSafe() );
1443 $this->assertSame( array(), $logger->getBuffer() );
1445 // Lookup anonymous user
1446 $this->store
->setSessionMeta(
1447 $id, array( 'userId' => 0, 'userName' => null, 'userToken' => null ) +
$metadata
1449 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1450 'provider' => $provider,
1453 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1454 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1455 $this->assertTrue( $info->getUserInfo()->isAnon() );
1456 $this->assertTrue( $info->isIdSafe() );
1457 $this->assertSame( array(), $logger->getBuffer() );
1459 // Unverified user with metadata
1460 $this->store
->setSessionMeta( $id, $metadata );
1461 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1462 'provider' => $provider,
1464 'userInfo' => $unverifiedUserInfo
1466 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1467 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1468 $this->assertTrue( $info->getUserInfo()->isVerified() );
1469 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1470 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1471 $this->assertTrue( $info->isIdSafe() );
1472 $this->assertSame( array(), $logger->getBuffer() );
1474 // Unverified user with metadata
1475 $this->store
->setSessionMeta( $id, $metadata );
1476 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1477 'provider' => $provider,
1479 'userInfo' => $unverifiedUserInfo
1481 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1482 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1483 $this->assertTrue( $info->getUserInfo()->isVerified() );
1484 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1485 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1486 $this->assertTrue( $info->isIdSafe() );
1487 $this->assertSame( array(), $logger->getBuffer() );
1490 $this->store
->setSessionMeta( $id, array( 'userToken' => 'Bad' ) +
$metadata );
1491 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1492 'provider' => $provider,
1494 'userInfo' => $userInfo
1496 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1497 $this->assertSame( array(
1498 array( LogLevel
::WARNING
, 'Session "{session}": User token mismatch' ),
1499 ), $logger->getBuffer() );
1500 $logger->clearBuffer();
1502 // Provider metadata
1503 $this->store
->setSessionMeta( $id, array( 'provider' => 'Mock2' ) +
$metadata );
1504 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1505 'provider' => $provider2,
1507 'userInfo' => $userInfo,
1508 'metadata' => array( 'Info' ),
1510 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1511 $this->assertSame( array( 'Info', 'changed' => true ), $info->getProviderMetadata() );
1512 $this->assertSame( array(), $logger->getBuffer() );
1514 $this->store
->setSessionMeta( $id, array( 'providerMetadata' => array( 'Saved' ) ) +
$metadata );
1515 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1516 'provider' => $provider,
1518 'userInfo' => $userInfo,
1520 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1521 $this->assertSame( array( 'Saved' ), $info->getProviderMetadata() );
1522 $this->assertSame( array(), $logger->getBuffer() );
1524 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1525 'provider' => $provider,
1527 'userInfo' => $userInfo,
1528 'metadata' => array( 'Info' ),
1530 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1531 $this->assertSame( array( 'Merged' ), $info->getProviderMetadata() );
1532 $this->assertSame( array(), $logger->getBuffer() );
1534 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1535 'provider' => $provider,
1537 'userInfo' => $userInfo,
1538 'metadata' => array( 'Throw' ),
1540 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1541 $this->assertSame( array(
1544 'Session "{session}": Metadata merge failed: {exception}',
1546 ), $logger->getBuffer() );
1547 $logger->clearBuffer();
1549 // Remember from session
1550 $this->store
->setSessionMeta( $id, $metadata );
1551 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1552 'provider' => $provider,
1555 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1556 $this->assertFalse( $info->wasRemembered() );
1557 $this->assertSame( array(), $logger->getBuffer() );
1559 $this->store
->setSessionMeta( $id, array( 'remember' => true ) +
$metadata );
1560 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1561 'provider' => $provider,
1564 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1565 $this->assertTrue( $info->wasRemembered() );
1566 $this->assertSame( array(), $logger->getBuffer() );
1568 $this->store
->setSessionMeta( $id, array( 'remember' => false ) +
$metadata );
1569 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1570 'provider' => $provider,
1572 'userInfo' => $userInfo
1574 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1575 $this->assertTrue( $info->wasRemembered() );
1576 $this->assertSame( array(), $logger->getBuffer() );
1578 // forceHTTPS from session
1579 $this->store
->setSessionMeta( $id, $metadata );
1580 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1581 'provider' => $provider,
1583 'userInfo' => $userInfo
1585 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1586 $this->assertFalse( $info->forceHTTPS() );
1587 $this->assertSame( array(), $logger->getBuffer() );
1589 $this->store
->setSessionMeta( $id, array( 'forceHTTPS' => true ) +
$metadata );
1590 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1591 'provider' => $provider,
1593 'userInfo' => $userInfo
1595 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1596 $this->assertTrue( $info->forceHTTPS() );
1597 $this->assertSame( array(), $logger->getBuffer() );
1599 $this->store
->setSessionMeta( $id, array( 'forceHTTPS' => false ) +
$metadata );
1600 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1601 'provider' => $provider,
1603 'userInfo' => $userInfo,
1604 'forceHTTPS' => true
1606 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1607 $this->assertTrue( $info->forceHTTPS() );
1608 $this->assertSame( array(), $logger->getBuffer() );
1610 // "Persist" flag from session
1611 $this->store
->setSessionMeta( $id, $metadata );
1612 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1613 'provider' => $provider,
1615 'userInfo' => $userInfo
1617 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1618 $this->assertFalse( $info->wasPersisted() );
1619 $this->assertSame( array(), $logger->getBuffer() );
1621 $this->store
->setSessionMeta( $id, array( 'persisted' => true ) +
$metadata );
1622 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1623 'provider' => $provider,
1625 'userInfo' => $userInfo
1627 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1628 $this->assertTrue( $info->wasPersisted() );
1629 $this->assertSame( array(), $logger->getBuffer() );
1631 $this->store
->setSessionMeta( $id, array( 'persisted' => false ) +
$metadata );
1632 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1633 'provider' => $provider,
1635 'userInfo' => $userInfo,
1638 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1639 $this->assertTrue( $info->wasPersisted() );
1640 $this->assertSame( array(), $logger->getBuffer() );
1642 // Provider refreshSessionInfo() returning false
1643 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1644 'provider' => $provider3,
1646 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1647 $this->assertSame( array(), $logger->getBuffer() );
1651 $data = array( 'foo' => 1 );
1652 $this->store
->setSession( $id, array( 'metadata' => $metadata, 'data' => $data ) );
1653 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1654 'provider' => $provider,
1656 'userInfo' => $userInfo
1658 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1659 'SessionCheckInfo' => array( function ( &$reason, $i, $r, $m, $d ) use (
1660 $info, $metadata, $data, $request, &$called
1662 $this->assertSame( $info->getId(), $i->getId() );
1663 $this->assertSame( $info->getProvider(), $i->getProvider() );
1664 $this->assertSame( $info->getUserInfo(), $i->getUserInfo() );
1665 $this->assertSame( $request, $r );
1666 $this->assertEquals( $metadata, $m );
1667 $this->assertEquals( $data, $d );
1672 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1673 $this->assertTrue( $called );
1674 $this->assertSame( array(
1675 array( LogLevel
::WARNING
, 'Session "{session}": Hook aborted' ),
1676 ), $logger->getBuffer() );
1677 $logger->clearBuffer();