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();
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' ),
600 'Quux' => array( 'Quux' ),
603 $this->assertEquals( $expect, $manager->getVaryHeaders() );
605 // Again, to ensure it's cached
606 $this->assertEquals( $expect, $manager->getVaryHeaders() );
609 public function testGetVaryCookies() {
610 $manager = $this->getManager();
612 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
613 ->setMethods( array( 'getVaryCookies', '__toString' ) );
615 $provider1 = $providerBuilder->getMock();
616 $provider1->expects( $this->once() )->method( 'getVaryCookies' )
617 ->will( $this->returnValue( array( 'Foo', 'Bar' ) ) );
618 $provider1->expects( $this->any() )->method( '__toString' )
619 ->will( $this->returnValue( 'MockProvider1' ) );
621 $provider2 = $providerBuilder->getMock();
622 $provider2->expects( $this->once() )->method( 'getVaryCookies' )
623 ->will( $this->returnValue( array( 'Foo', 'Baz' ) ) );
624 $provider2->expects( $this->any() )->method( '__toString' )
625 ->will( $this->returnValue( 'MockProvider2' ) );
627 $this->config
->set( 'SessionProviders', array(
628 $this->objectCacheDef( $provider1 ),
629 $this->objectCacheDef( $provider2 ),
632 $expect = array( 'Foo', 'Bar', 'Baz' );
634 $this->assertEquals( $expect, $manager->getVaryCookies() );
636 // Again, to ensure it's cached
637 $this->assertEquals( $expect, $manager->getVaryCookies() );
640 public function testGetProviders() {
641 $realManager = $this->getManager();
642 $manager = \TestingAccessWrapper
::newFromObject( $realManager );
644 $this->config
->set( 'SessionProviders', array(
645 array( 'class' => 'DummySessionProvider' ),
647 $providers = $manager->getProviders();
648 $this->assertArrayHasKey( 'DummySessionProvider', $providers );
649 $provider = \TestingAccessWrapper
::newFromObject( $providers['DummySessionProvider'] );
650 $this->assertSame( $manager->logger
, $provider->logger
);
651 $this->assertSame( $manager->config
, $provider->config
);
652 $this->assertSame( $realManager, $provider->getManager() );
654 $this->config
->set( 'SessionProviders', array(
655 array( 'class' => 'DummySessionProvider' ),
656 array( 'class' => 'DummySessionProvider' ),
658 $manager->sessionProviders
= null;
660 $manager->getProviders();
661 $this->fail( 'Expected exception not thrown' );
662 } catch ( \UnexpectedValueException
$ex ) {
664 'Duplicate provider name "DummySessionProvider"',
670 public function testShutdown() {
671 $manager = \TestingAccessWrapper
::newFromObject( $this->getManager() );
672 $manager->setLogger( new \Psr\Log\
NullLogger() );
674 $mock = $this->getMock( 'stdClass', array( 'save' ) );
675 $mock->expects( $this->once() )->method( 'save' );
677 $manager->allSessionBackends
= array( $mock );
678 $manager->shutdown();
681 public function testGetSessionFromInfo() {
682 $manager = \TestingAccessWrapper
::newFromObject( $this->getManager() );
683 $request = new \
FauxRequest();
685 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
687 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
688 'provider' => $manager->getProvider( 'DummySessionProvider' ),
691 'userInfo' => UserInfo
::newFromName( 'UTSysop', true ),
694 \TestingAccessWrapper
::newFromObject( $info )->idIsSafe
= true;
695 $session1 = \TestingAccessWrapper
::newFromObject(
696 $manager->getSessionFromInfo( $info, $request )
698 $session2 = \TestingAccessWrapper
::newFromObject(
699 $manager->getSessionFromInfo( $info, $request )
702 $this->assertSame( $session1->backend
, $session2->backend
);
703 $this->assertNotEquals( $session1->index
, $session2->index
);
704 $this->assertSame( $session1->getSessionId(), $session2->getSessionId() );
705 $this->assertSame( $id, $session1->getId() );
707 \TestingAccessWrapper
::newFromObject( $info )->idIsSafe
= false;
708 $session3 = $manager->getSessionFromInfo( $info, $request );
709 $this->assertNotSame( $id, $session3->getId() );
712 public function testBackendRegistration() {
713 $manager = $this->getManager();
715 $session = $manager->getSessionForRequest( new \FauxRequest
);
716 $backend = \TestingAccessWrapper
::newFromObject( $session )->backend
;
717 $sessionId = $session->getSessionId();
718 $id = (string)$sessionId;
720 $this->assertSame( $sessionId, $manager->getSessionById( $id, true )->getSessionId() );
722 $manager->changeBackendId( $backend );
723 $this->assertSame( $sessionId, $session->getSessionId() );
724 $this->assertNotEquals( $id, (string)$sessionId );
725 $id = (string)$sessionId;
727 $this->assertSame( $sessionId, $manager->getSessionById( $id, true )->getSessionId() );
729 // Destruction of the session here causes the backend to be deregistered
733 $manager->changeBackendId( $backend );
734 $this->fail( 'Expected exception not thrown' );
735 } catch ( \InvalidArgumentException
$ex ) {
737 'Backend was not registered with this SessionManager', $ex->getMessage()
742 $manager->deregisterSessionBackend( $backend );
743 $this->fail( 'Expected exception not thrown' );
744 } catch ( \InvalidArgumentException
$ex ) {
746 'Backend was not registered with this SessionManager', $ex->getMessage()
750 $session = $manager->getSessionById( $id, true );
751 $this->assertSame( $sessionId, $session->getSessionId() );
754 public function testGenerateSessionId() {
755 $manager = $this->getManager();
757 $id = $manager->generateSessionId();
758 $this->assertTrue( SessionManager
::validateSessionId( $id ), "Generated ID: $id" );
761 public function testAutoCreateUser() {
762 global $wgGroupPermissions;
766 \ObjectCache
::$instances[__METHOD__
] = new \
HashBagOStuff();
767 $this->setMwGlobals( array( 'wgMainCacheType' => __METHOD__
) );
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 );
783 $m = preg_replace( '/ - from: .*$/', ' - from: XXX', $m );
786 $manager->setLogger( $logger );
788 $session = SessionManager
::getGlobalSession();
790 // Can't create an already-existing user
791 $user = User
::newFromName( 'UTSysop' );
792 $id = $user->getId();
793 $this->assertFalse( $manager->autoCreateUser( $user ) );
794 $this->assertSame( $id, $user->getId() );
795 $this->assertSame( 'UTSysop', $user->getName() );
796 $this->assertSame( array(), $logger->getBuffer() );
797 $logger->clearBuffer();
799 // Sanity check that creation works at all
800 $user = User
::newFromName( 'UTSessionAutoCreate1' );
801 $this->assertSame( 0, $user->getId(), 'sanity check' );
802 $this->assertTrue( $manager->autoCreateUser( $user ) );
803 $this->assertNotEquals( 0, $user->getId() );
804 $this->assertSame( 'UTSessionAutoCreate1', $user->getName() );
806 $user->getId(), User
::idFromName( 'UTSessionAutoCreate1', User
::READ_LATEST
)
808 $this->assertSame( array(
809 array( LogLevel
::INFO
, 'creating new user (UTSessionAutoCreate1) - from: XXX' ),
810 ), $logger->getBuffer() );
811 $logger->clearBuffer();
813 // Check lack of permissions
814 $wgGroupPermissions['*']['createaccount'] = false;
815 $wgGroupPermissions['*']['autocreateaccount'] = false;
816 $user = User
::newFromName( 'UTDoesNotExist' );
817 $this->assertFalse( $manager->autoCreateUser( $user ) );
818 $this->assertSame( 0, $user->getId() );
819 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
820 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
822 $this->assertSame( array(
823 array( LogLevel
::DEBUG
, 'user is blocked from this wiki, blacklisting' ),
824 ), $logger->getBuffer() );
825 $logger->clearBuffer();
827 // Check other permission
828 $wgGroupPermissions['*']['createaccount'] = false;
829 $wgGroupPermissions['*']['autocreateaccount'] = true;
830 $user = User
::newFromName( 'UTSessionAutoCreate2' );
831 $this->assertSame( 0, $user->getId(), 'sanity check' );
832 $this->assertTrue( $manager->autoCreateUser( $user ) );
833 $this->assertNotEquals( 0, $user->getId() );
834 $this->assertSame( 'UTSessionAutoCreate2', $user->getName() );
836 $user->getId(), User
::idFromName( 'UTSessionAutoCreate2', User
::READ_LATEST
)
838 $this->assertSame( array(
839 array( LogLevel
::INFO
, 'creating new user (UTSessionAutoCreate2) - from: XXX' ),
840 ), $logger->getBuffer() );
841 $logger->clearBuffer();
843 // Test account-creation block
845 $block = new \
Block( array(
846 'address' => $anon->getName(),
848 'reason' => __METHOD__
,
849 'expiry' => time() +
100500,
850 'createAccount' => true,
853 $this->assertInstanceOf( 'Block', $anon->isBlockedFromCreateAccount(), 'sanity check' );
854 $reset2 = new \
ScopedCallback( array( $block, 'delete' ) );
855 $user = User
::newFromName( 'UTDoesNotExist' );
856 $this->assertFalse( $manager->autoCreateUser( $user ) );
857 $this->assertSame( 0, $user->getId() );
858 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
859 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
860 \ScopedCallback
::consume( $reset2 );
862 $this->assertSame( array(
863 array( LogLevel
::DEBUG
, 'user is blocked from this wiki, blacklisting' ),
864 ), $logger->getBuffer() );
865 $logger->clearBuffer();
867 // Sanity check that creation still works
868 $user = User
::newFromName( 'UTSessionAutoCreate3' );
869 $this->assertSame( 0, $user->getId(), 'sanity check' );
870 $this->assertTrue( $manager->autoCreateUser( $user ) );
871 $this->assertNotEquals( 0, $user->getId() );
872 $this->assertSame( 'UTSessionAutoCreate3', $user->getName() );
874 $user->getId(), User
::idFromName( 'UTSessionAutoCreate3', User
::READ_LATEST
)
876 $this->assertSame( array(
877 array( LogLevel
::INFO
, 'creating new user (UTSessionAutoCreate3) - from: XXX' ),
878 ), $logger->getBuffer() );
879 $logger->clearBuffer();
881 // Test prevention by AuthPlugin
883 $oldWgAuth = $wgAuth;
884 $mockWgAuth = $this->getMock( 'AuthPlugin', array( 'autoCreate' ) );
885 $mockWgAuth->expects( $this->once() )->method( 'autoCreate' )
886 ->will( $this->returnValue( false ) );
887 $this->setMwGlobals( array(
888 'wgAuth' => $mockWgAuth,
890 $user = User
::newFromName( 'UTDoesNotExist' );
891 $this->assertFalse( $manager->autoCreateUser( $user ) );
892 $this->assertSame( 0, $user->getId() );
893 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
894 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
895 $this->setMwGlobals( array(
896 'wgAuth' => $oldWgAuth,
899 $this->assertSame( array(
900 array( LogLevel
::DEBUG
, 'denied by AuthPlugin' ),
901 ), $logger->getBuffer() );
902 $logger->clearBuffer();
904 // Test prevention by wfReadOnly()
905 $this->setMwGlobals( array(
906 'wgReadOnly' => 'Because',
908 $user = User
::newFromName( 'UTDoesNotExist' );
909 $this->assertFalse( $manager->autoCreateUser( $user ) );
910 $this->assertSame( 0, $user->getId() );
911 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
912 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
913 $this->setMwGlobals( array(
914 'wgReadOnly' => false,
917 $this->assertSame( array(
918 array( LogLevel
::DEBUG
, 'denied by wfReadOnly()' ),
919 ), $logger->getBuffer() );
920 $logger->clearBuffer();
922 // Test prevention by a previous session
923 $session->set( 'MWSession::AutoCreateBlacklist', 'test' );
924 $user = User
::newFromName( 'UTDoesNotExist' );
925 $this->assertFalse( $manager->autoCreateUser( $user ) );
926 $this->assertSame( 0, $user->getId() );
927 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
928 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
930 $this->assertSame( array(
931 array( LogLevel
::DEBUG
, 'blacklisted in session (test)' ),
932 ), $logger->getBuffer() );
933 $logger->clearBuffer();
935 // Test uncreatable name
936 $user = User
::newFromName( 'UTDoesNotExist@' );
937 $this->assertFalse( $manager->autoCreateUser( $user ) );
938 $this->assertSame( 0, $user->getId() );
939 $this->assertNotSame( 'UTDoesNotExist@', $user->getName() );
940 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
942 $this->assertSame( array(
943 array( LogLevel
::DEBUG
, 'Invalid username, blacklisting' ),
944 ), $logger->getBuffer() );
945 $logger->clearBuffer();
947 // Test AbortAutoAccount hook
948 $mock = $this->getMock( __CLASS__
, array( 'onAbortAutoAccount' ) );
949 $mock->expects( $this->once() )->method( 'onAbortAutoAccount' )
950 ->will( $this->returnCallback( function ( User
$user, &$msg ) {
954 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array( $mock ) ) );
955 $user = User
::newFromName( 'UTDoesNotExist' );
956 $this->assertFalse( $manager->autoCreateUser( $user ) );
957 $this->assertSame( 0, $user->getId() );
958 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
959 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
960 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array() ) );
962 $this->assertSame( array(
963 array( LogLevel
::DEBUG
, 'denied by hook: No way!' ),
964 ), $logger->getBuffer() );
965 $logger->clearBuffer();
967 // Test AbortAutoAccount hook screwing up the name
968 $mock = $this->getMock( 'stdClass', array( 'onAbortAutoAccount' ) );
969 $mock->expects( $this->once() )->method( 'onAbortAutoAccount' )
970 ->will( $this->returnCallback( function ( User
$user ) {
971 $user->setName( 'UTDoesNotExistEither' );
973 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array( $mock ) ) );
975 $user = User
::newFromName( 'UTDoesNotExist' );
976 $manager->autoCreateUser( $user );
977 $this->fail( 'Expected exception not thrown' );
978 } catch ( \UnexpectedValueException
$ex ) {
980 'AbortAutoAccount hook tried to change the user name',
984 $this->assertSame( 0, $user->getId() );
985 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
986 $this->assertNotSame( 'UTDoesNotExistEither', $user->getName() );
987 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
988 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExistEither', User
::READ_LATEST
) );
989 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array() ) );
991 $this->assertSame( array(), $logger->getBuffer() );
992 $logger->clearBuffer();
994 // Test for "exception backoff"
995 $user = User
::newFromName( 'UTDoesNotExist' );
996 $cache = \ObjectCache
::getLocalClusterInstance();
997 $backoffKey = wfMemcKey( 'MWSession', 'autocreate-failed', md5( $user->getName() ) );
998 $cache->set( $backoffKey, 1, 60 * 10 );
999 $this->assertFalse( $manager->autoCreateUser( $user ) );
1000 $this->assertSame( 0, $user->getId() );
1001 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
1002 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
1003 $cache->delete( $backoffKey );
1005 $this->assertSame( array(
1006 array( LogLevel
::DEBUG
, 'denied by prior creation attempt failures' ),
1007 ), $logger->getBuffer() );
1008 $logger->clearBuffer();
1010 // Sanity check that creation still works, and test completion hook
1011 $cb = $this->callback( function ( User
$user ) use ( $that ) {
1012 $that->assertNotEquals( 0, $user->getId() );
1013 $that->assertSame( 'UTSessionAutoCreate4', $user->getName() );
1014 $that->assertEquals(
1015 $user->getId(), User
::idFromName( 'UTSessionAutoCreate4', User
::READ_LATEST
)
1019 $mock = $this->getMock( 'stdClass',
1020 array( 'onAuthPluginAutoCreate', 'onLocalUserCreated' ) );
1021 $mock->expects( $this->once() )->method( 'onAuthPluginAutoCreate' )
1023 $mock->expects( $this->once() )->method( 'onLocalUserCreated' )
1024 ->with( $cb, $this->identicalTo( true ) );
1025 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1026 'AuthPluginAutoCreate' => array( $mock ),
1027 'LocalUserCreated' => array( $mock ),
1029 $user = User
::newFromName( 'UTSessionAutoCreate4' );
1030 $this->assertSame( 0, $user->getId(), 'sanity check' );
1031 $this->assertTrue( $manager->autoCreateUser( $user ) );
1032 $this->assertNotEquals( 0, $user->getId() );
1033 $this->assertSame( 'UTSessionAutoCreate4', $user->getName() );
1034 $this->assertEquals(
1036 User
::idFromName( 'UTSessionAutoCreate4', User
::READ_LATEST
)
1038 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1039 'AuthPluginAutoCreate' => array(),
1040 'LocalUserCreated' => array(),
1042 $this->assertSame( array(
1043 array( LogLevel
::INFO
, 'creating new user (UTSessionAutoCreate4) - from: XXX' ),
1044 ), $logger->getBuffer() );
1045 $logger->clearBuffer();
1048 public function onAbortAutoAccount( User
$user, &$msg ) {
1051 public function testPreventSessionsForUser() {
1052 $manager = $this->getManager();
1054 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
1055 ->setMethods( array( 'preventSessionsForUser', '__toString' ) );
1057 $provider1 = $providerBuilder->getMock();
1058 $provider1->expects( $this->once() )->method( 'preventSessionsForUser' )
1059 ->with( $this->equalTo( 'UTSysop' ) );
1060 $provider1->expects( $this->any() )->method( '__toString' )
1061 ->will( $this->returnValue( 'MockProvider1' ) );
1063 $this->config
->set( 'SessionProviders', array(
1064 $this->objectCacheDef( $provider1 ),
1067 $user = User
::newFromName( 'UTSysop' );
1068 $token = $user->getToken( true );
1070 $this->assertFalse( $manager->isUserSessionPrevented( 'UTSysop' ) );
1071 $manager->preventSessionsForUser( 'UTSysop' );
1072 $this->assertNotEquals( $token, User
::newFromName( 'UTSysop' )->getToken() );
1073 $this->assertTrue( $manager->isUserSessionPrevented( 'UTSysop' ) );
1076 public function testLoadSessionInfoFromStore() {
1077 $manager = $this->getManager();
1078 $logger = new \
TestLogger( true, function ( $m ) {
1079 return preg_replace(
1080 '/^Session \[\d+\]\w+<(?:null|anon|[+-]:\d+:\w+)>\w+: /', 'Session X: ', $m
1083 $manager->setLogger( $logger );
1084 $request = new \
FauxRequest();
1086 // TestingAccessWrapper can't handle methods with reference arguments, sigh.
1087 $rClass = new \
ReflectionClass( $manager );
1088 $rMethod = $rClass->getMethod( 'loadSessionInfoFromStore' );
1089 $rMethod->setAccessible( true );
1090 $loadSessionInfoFromStore = function ( &$info ) use ( $rMethod, $manager, $request ) {
1091 return $rMethod->invokeArgs( $manager, array( &$info, $request ) );
1094 $userInfo = UserInfo
::newFromName( 'UTSysop', true );
1095 $unverifiedUserInfo = UserInfo
::newFromName( 'UTSysop', false );
1097 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
1099 'userId' => $userInfo->getId(),
1100 'userName' => $userInfo->getName(),
1101 'userToken' => $userInfo->getToken( true ),
1102 'provider' => 'Mock',
1105 $builder = $this->getMockBuilder( 'MediaWiki\\Session\\SessionProvider' )
1106 ->setMethods( array( '__toString', 'mergeMetadata', 'refreshSessionInfo' ) );
1108 $provider = $builder->getMockForAbstractClass();
1109 $provider->setManager( $manager );
1110 $provider->expects( $this->any() )->method( 'persistsSessionId' )
1111 ->will( $this->returnValue( true ) );
1112 $provider->expects( $this->any() )->method( 'canChangeUser' )
1113 ->will( $this->returnValue( true ) );
1114 $provider->expects( $this->any() )->method( 'refreshSessionInfo' )
1115 ->will( $this->returnValue( true ) );
1116 $provider->expects( $this->any() )->method( '__toString' )
1117 ->will( $this->returnValue( 'Mock' ) );
1118 $provider->expects( $this->any() )->method( 'mergeMetadata' )
1119 ->will( $this->returnCallback( function ( $a, $b ) {
1120 if ( $b === array( 'Throw' ) ) {
1121 throw new \
UnexpectedValueException( 'no merge!' );
1123 return array( 'Merged' );
1126 $provider2 = $builder->getMockForAbstractClass();
1127 $provider2->setManager( $manager );
1128 $provider2->expects( $this->any() )->method( 'persistsSessionId' )
1129 ->will( $this->returnValue( false ) );
1130 $provider2->expects( $this->any() )->method( 'canChangeUser' )
1131 ->will( $this->returnValue( false ) );
1132 $provider2->expects( $this->any() )->method( '__toString' )
1133 ->will( $this->returnValue( 'Mock2' ) );
1134 $provider2->expects( $this->any() )->method( 'refreshSessionInfo' )
1135 ->will( $this->returnCallback( function ( $info, $request, &$metadata ) {
1136 $metadata['changed'] = true;
1140 $provider3 = $builder->getMockForAbstractClass();
1141 $provider3->setManager( $manager );
1142 $provider3->expects( $this->any() )->method( 'persistsSessionId' )
1143 ->will( $this->returnValue( true ) );
1144 $provider3->expects( $this->any() )->method( 'canChangeUser' )
1145 ->will( $this->returnValue( true ) );
1146 $provider3->expects( $this->once() )->method( 'refreshSessionInfo' )
1147 ->will( $this->returnValue( false ) );
1148 $provider3->expects( $this->any() )->method( '__toString' )
1149 ->will( $this->returnValue( 'Mock3' ) );
1151 \TestingAccessWrapper
::newFromObject( $manager )->sessionProviders
= array(
1152 (string)$provider => $provider,
1153 (string)$provider2 => $provider2,
1154 (string)$provider3 => $provider3,
1157 // No metadata, basic usage
1158 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1159 'provider' => $provider,
1161 'userInfo' => $userInfo
1163 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1164 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1165 $this->assertFalse( $info->isIdSafe() );
1166 $this->assertSame( array(), $logger->getBuffer() );
1168 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1169 'provider' => $provider,
1170 'userInfo' => $userInfo
1172 $this->assertTrue( $info->isIdSafe(), 'sanity check' );
1173 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1174 $this->assertTrue( $info->isIdSafe() );
1175 $this->assertSame( array(), $logger->getBuffer() );
1177 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1178 'provider' => $provider2,
1180 'userInfo' => $userInfo
1182 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1183 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1184 $this->assertTrue( $info->isIdSafe() );
1185 $this->assertSame( array(), $logger->getBuffer() );
1187 // Unverified user, no metadata
1188 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1189 'provider' => $provider,
1191 'userInfo' => $unverifiedUserInfo
1193 $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
1194 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1195 $this->assertSame( array(
1196 array( LogLevel
::WARNING
, 'Session X: Unverified user provided and no metadata to auth it' )
1197 ), $logger->getBuffer() );
1198 $logger->clearBuffer();
1200 // No metadata, missing data
1201 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1203 'userInfo' => $userInfo
1205 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1206 $this->assertSame( array(
1207 array( LogLevel
::WARNING
, 'Session X: Null provider and no metadata' ),
1208 ), $logger->getBuffer() );
1209 $logger->clearBuffer();
1211 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1212 'provider' => $provider,
1215 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1216 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1217 $this->assertInstanceOf( 'MediaWiki\\Session\\UserInfo', $info->getUserInfo() );
1218 $this->assertTrue( $info->getUserInfo()->isVerified() );
1219 $this->assertTrue( $info->getUserInfo()->isAnon() );
1220 $this->assertFalse( $info->isIdSafe() );
1221 $this->assertSame( array(), $logger->getBuffer() );
1223 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1224 'provider' => $provider2,
1227 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1228 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1229 $this->assertSame( array(
1230 array( LogLevel
::INFO
, 'Session X: No user provided and provider cannot set user' )
1231 ), $logger->getBuffer() );
1232 $logger->clearBuffer();
1234 // Incomplete/bad metadata
1235 $this->store
->setRawSession( $id, true );
1236 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1237 $this->assertSame( array(
1238 array( LogLevel
::WARNING
, 'Session X: Bad data' ),
1239 ), $logger->getBuffer() );
1240 $logger->clearBuffer();
1242 $this->store
->setRawSession( $id, array( 'data' => array() ) );
1243 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1244 $this->assertSame( array(
1245 array( LogLevel
::WARNING
, 'Session X: Bad data structure' ),
1246 ), $logger->getBuffer() );
1247 $logger->clearBuffer();
1249 $this->store
->deleteSession( $id );
1250 $this->store
->setRawSession( $id, array( 'metadata' => $metadata ) );
1251 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1252 $this->assertSame( array(
1253 array( LogLevel
::WARNING
, 'Session X: Bad data structure' ),
1254 ), $logger->getBuffer() );
1255 $logger->clearBuffer();
1257 $this->store
->setRawSession( $id, array( 'metadata' => $metadata, 'data' => true ) );
1258 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1259 $this->assertSame( array(
1260 array( LogLevel
::WARNING
, 'Session X: Bad data structure' ),
1261 ), $logger->getBuffer() );
1262 $logger->clearBuffer();
1264 $this->store
->setRawSession( $id, array( 'metadata' => true, 'data' => array() ) );
1265 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1266 $this->assertSame( array(
1267 array( LogLevel
::WARNING
, 'Session X: Bad data structure' ),
1268 ), $logger->getBuffer() );
1269 $logger->clearBuffer();
1271 foreach ( $metadata as $key => $dummy ) {
1273 unset( $tmp[$key] );
1274 $this->store
->setRawSession( $id, array( 'metadata' => $tmp, 'data' => array() ) );
1275 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1276 $this->assertSame( array(
1277 array( LogLevel
::WARNING
, 'Session X: Bad metadata' ),
1278 ), $logger->getBuffer() );
1279 $logger->clearBuffer();
1282 // Basic usage with metadata
1283 $this->store
->setRawSession( $id, array( 'metadata' => $metadata, 'data' => array() ) );
1284 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1285 'provider' => $provider,
1287 'userInfo' => $userInfo
1289 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1290 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1291 $this->assertTrue( $info->isIdSafe() );
1292 $this->assertSame( array(), $logger->getBuffer() );
1294 // Mismatched provider
1295 $this->store
->setSessionMeta( $id, array( 'provider' => 'Bad' ) +
$metadata );
1296 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1297 'provider' => $provider,
1299 'userInfo' => $userInfo
1301 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1302 $this->assertSame( array(
1303 array( LogLevel
::WARNING
, 'Session X: Wrong provider, Bad !== Mock' ),
1304 ), $logger->getBuffer() );
1305 $logger->clearBuffer();
1308 $this->store
->setSessionMeta( $id, array( 'provider' => 'Bad' ) +
$metadata );
1309 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1311 'userInfo' => $userInfo
1313 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1314 $this->assertSame( array(
1315 array( LogLevel
::WARNING
, 'Session X: Unknown provider, Bad' ),
1316 ), $logger->getBuffer() );
1317 $logger->clearBuffer();
1320 $this->store
->setSessionMeta( $id, $metadata );
1321 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1323 'userInfo' => $userInfo
1325 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1326 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1327 $this->assertTrue( $info->isIdSafe() );
1328 $this->assertSame( array(), $logger->getBuffer() );
1330 // Bad user metadata
1331 $this->store
->setSessionMeta( $id, array( 'userId' => -1, 'userToken' => null ) +
$metadata );
1332 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1333 'provider' => $provider,
1336 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1337 $this->assertSame( array(
1338 array( LogLevel
::ERROR
, 'Session X: Invalid ID' ),
1339 ), $logger->getBuffer() );
1340 $logger->clearBuffer();
1342 $this->store
->setSessionMeta(
1343 $id, array( 'userId' => 0, 'userName' => '<X>', 'userToken' => null ) +
$metadata
1345 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1346 'provider' => $provider,
1349 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1350 $this->assertSame( array(
1351 array( LogLevel
::ERROR
, 'Session X: Invalid user name' ),
1352 ), $logger->getBuffer() );
1353 $logger->clearBuffer();
1355 // Mismatched user by ID
1356 $this->store
->setSessionMeta(
1357 $id, array( 'userId' => $userInfo->getId() +
1, 'userToken' => null ) +
$metadata
1359 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1360 'provider' => $provider,
1362 'userInfo' => $userInfo
1364 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1365 $this->assertSame( array(
1366 array( LogLevel
::WARNING
, 'Session X: User ID mismatch, 2 !== 1' ),
1367 ), $logger->getBuffer() );
1368 $logger->clearBuffer();
1370 // Mismatched user by name
1371 $this->store
->setSessionMeta(
1372 $id, array( 'userId' => 0, 'userName' => 'X', 'userToken' => null ) +
$metadata
1374 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1375 'provider' => $provider,
1377 'userInfo' => $userInfo
1379 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1380 $this->assertSame( array(
1381 array( LogLevel
::WARNING
, 'Session X: User name mismatch, X !== UTSysop' ),
1382 ), $logger->getBuffer() );
1383 $logger->clearBuffer();
1385 // ID matches, name doesn't
1386 $this->store
->setSessionMeta(
1387 $id, array( 'userId' => $userInfo->getId(), 'userName' => 'X', 'userToken' => null ) +
$metadata
1389 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1390 'provider' => $provider,
1392 'userInfo' => $userInfo
1394 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1395 $this->assertSame( array(
1397 LogLevel
::WARNING
, 'Session X: User ID matched but name didn\'t (rename?), X !== UTSysop'
1399 ), $logger->getBuffer() );
1400 $logger->clearBuffer();
1402 // Mismatched anon user
1403 $this->store
->setSessionMeta(
1404 $id, array( 'userId' => 0, 'userName' => null, 'userToken' => null ) +
$metadata
1406 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1407 'provider' => $provider,
1409 'userInfo' => $userInfo
1411 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1412 $this->assertSame( array(
1414 LogLevel
::WARNING
, 'Session X: Metadata has an anonymous user, 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 X: 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(
1542 array( LogLevel
::WARNING
, 'Session X: Metadata merge failed: no merge!' ),
1543 ), $logger->getBuffer() );
1544 $logger->clearBuffer();
1546 // Remember from session
1547 $this->store
->setSessionMeta( $id, $metadata );
1548 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1549 'provider' => $provider,
1552 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1553 $this->assertFalse( $info->wasRemembered() );
1554 $this->assertSame( array(), $logger->getBuffer() );
1556 $this->store
->setSessionMeta( $id, array( 'remember' => true ) +
$metadata );
1557 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1558 'provider' => $provider,
1561 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1562 $this->assertTrue( $info->wasRemembered() );
1563 $this->assertSame( array(), $logger->getBuffer() );
1565 $this->store
->setSessionMeta( $id, array( 'remember' => false ) +
$metadata );
1566 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1567 'provider' => $provider,
1569 'userInfo' => $userInfo
1571 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1572 $this->assertTrue( $info->wasRemembered() );
1573 $this->assertSame( array(), $logger->getBuffer() );
1575 // forceHTTPS from session
1576 $this->store
->setSessionMeta( $id, $metadata );
1577 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1578 'provider' => $provider,
1580 'userInfo' => $userInfo
1582 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1583 $this->assertFalse( $info->forceHTTPS() );
1584 $this->assertSame( array(), $logger->getBuffer() );
1586 $this->store
->setSessionMeta( $id, array( 'forceHTTPS' => true ) +
$metadata );
1587 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1588 'provider' => $provider,
1590 'userInfo' => $userInfo
1592 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1593 $this->assertTrue( $info->forceHTTPS() );
1594 $this->assertSame( array(), $logger->getBuffer() );
1596 $this->store
->setSessionMeta( $id, array( 'forceHTTPS' => false ) +
$metadata );
1597 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1598 'provider' => $provider,
1600 'userInfo' => $userInfo,
1601 'forceHTTPS' => true
1603 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1604 $this->assertTrue( $info->forceHTTPS() );
1605 $this->assertSame( array(), $logger->getBuffer() );
1607 // Provider refreshSessionInfo() returning false
1608 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1609 'provider' => $provider3,
1611 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1612 $this->assertSame( array(), $logger->getBuffer() );
1617 $data = array( 'foo' => 1 );
1618 $this->store
->setSession( $id, array( 'metadata' => $metadata, 'data' => $data ) );
1619 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1620 'provider' => $provider,
1622 'userInfo' => $userInfo
1624 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1625 'SessionCheckInfo' => array( function ( &$reason, $i, $r, $m, $d ) use (
1626 $that, $info, $metadata, $data, $request, &$called
1628 $that->assertSame( $info->getId(), $i->getId() );
1629 $that->assertSame( $info->getProvider(), $i->getProvider() );
1630 $that->assertSame( $info->getUserInfo(), $i->getUserInfo() );
1631 $that->assertSame( $request, $r );
1632 $that->assertEquals( $metadata, $m );
1633 $that->assertEquals( $data, $d );
1638 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1639 $this->assertTrue( $called );
1640 $this->assertSame( array(
1641 array( LogLevel
::WARNING
, 'Session X: Hook aborted' ),
1642 ), $logger->getBuffer() );
1643 $logger->clearBuffer();