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;
765 \ObjectCache
::$instances[__METHOD__
] = new TestBagOStuff();
766 $this->setMwGlobals( array( 'wgMainCacheType' => __METHOD__
) );
767 $this->setMWGlobals( array(
768 'wgAuth' => new AuthPlugin
,
771 $this->stashMwGlobals( array( 'wgGroupPermissions' ) );
772 $wgGroupPermissions['*']['createaccount'] = true;
773 $wgGroupPermissions['*']['autocreateaccount'] = false;
775 // Replace the global singleton with one configured for testing
776 $manager = $this->getManager();
777 $reset = TestUtils
::setSessionManagerSingleton( $manager );
779 $logger = new \
TestLogger( true, function ( $m ) {
780 if ( substr( $m, 0, 15 ) === 'SessionBackend ' ) {
784 $m = str_replace( 'MediaWiki\Session\SessionManager::autoCreateUser: ', '', $m );
787 $manager->setLogger( $logger );
789 $session = SessionManager
::getGlobalSession();
791 // Can't create an already-existing user
792 $user = User
::newFromName( 'UTSysop' );
793 $id = $user->getId();
794 $this->assertFalse( $manager->autoCreateUser( $user ) );
795 $this->assertSame( $id, $user->getId() );
796 $this->assertSame( 'UTSysop', $user->getName() );
797 $this->assertSame( array(), $logger->getBuffer() );
798 $logger->clearBuffer();
800 // Sanity check that creation works at all
801 $user = User
::newFromName( 'UTSessionAutoCreate1' );
802 $this->assertSame( 0, $user->getId(), 'sanity check' );
803 $this->assertTrue( $manager->autoCreateUser( $user ) );
804 $this->assertNotEquals( 0, $user->getId() );
805 $this->assertSame( 'UTSessionAutoCreate1', $user->getName() );
807 $user->getId(), User
::idFromName( 'UTSessionAutoCreate1', User
::READ_LATEST
)
809 $this->assertSame( array(
810 array( LogLevel
::INFO
, 'creating new user ({username}) - from: {url}' ),
811 ), $logger->getBuffer() );
812 $logger->clearBuffer();
814 // Check lack of permissions
815 $wgGroupPermissions['*']['createaccount'] = false;
816 $wgGroupPermissions['*']['autocreateaccount'] = false;
817 $user = User
::newFromName( 'UTDoesNotExist' );
818 $this->assertFalse( $manager->autoCreateUser( $user ) );
819 $this->assertSame( 0, $user->getId() );
820 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
821 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
823 $this->assertSame( array(
826 'user is blocked from this wiki, blacklisting',
828 ), $logger->getBuffer() );
829 $logger->clearBuffer();
831 // Check other permission
832 $wgGroupPermissions['*']['createaccount'] = false;
833 $wgGroupPermissions['*']['autocreateaccount'] = true;
834 $user = User
::newFromName( 'UTSessionAutoCreate2' );
835 $this->assertSame( 0, $user->getId(), 'sanity check' );
836 $this->assertTrue( $manager->autoCreateUser( $user ) );
837 $this->assertNotEquals( 0, $user->getId() );
838 $this->assertSame( 'UTSessionAutoCreate2', $user->getName() );
840 $user->getId(), User
::idFromName( 'UTSessionAutoCreate2', User
::READ_LATEST
)
842 $this->assertSame( array(
843 array( LogLevel
::INFO
, 'creating new user ({username}) - from: {url}' ),
844 ), $logger->getBuffer() );
845 $logger->clearBuffer();
847 // Test account-creation block
849 $block = new \
Block( array(
850 'address' => $anon->getName(),
852 'reason' => __METHOD__
,
853 'expiry' => time() +
100500,
854 'createAccount' => true,
857 $this->assertInstanceOf( 'Block', $anon->isBlockedFromCreateAccount(), 'sanity check' );
858 $reset2 = new \
ScopedCallback( array( $block, 'delete' ) );
859 $user = User
::newFromName( 'UTDoesNotExist' );
860 $this->assertFalse( $manager->autoCreateUser( $user ) );
861 $this->assertSame( 0, $user->getId() );
862 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
863 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
864 \ScopedCallback
::consume( $reset2 );
866 $this->assertSame( array(
867 array( LogLevel
::DEBUG
, 'user is blocked from this wiki, blacklisting' ),
868 ), $logger->getBuffer() );
869 $logger->clearBuffer();
871 // Sanity check that creation still works
872 $user = User
::newFromName( 'UTSessionAutoCreate3' );
873 $this->assertSame( 0, $user->getId(), 'sanity check' );
874 $this->assertTrue( $manager->autoCreateUser( $user ) );
875 $this->assertNotEquals( 0, $user->getId() );
876 $this->assertSame( 'UTSessionAutoCreate3', $user->getName() );
878 $user->getId(), User
::idFromName( 'UTSessionAutoCreate3', User
::READ_LATEST
)
880 $this->assertSame( array(
881 array( LogLevel
::INFO
, 'creating new user ({username}) - from: {url}' ),
882 ), $logger->getBuffer() );
883 $logger->clearBuffer();
885 // Test prevention by AuthPlugin
887 $oldWgAuth = $wgAuth;
888 $mockWgAuth = $this->getMock( 'AuthPlugin', array( 'autoCreate' ) );
889 $mockWgAuth->expects( $this->once() )->method( 'autoCreate' )
890 ->will( $this->returnValue( false ) );
891 $this->setMwGlobals( array(
892 'wgAuth' => $mockWgAuth,
894 $user = User
::newFromName( 'UTDoesNotExist' );
895 $this->assertFalse( $manager->autoCreateUser( $user ) );
896 $this->assertSame( 0, $user->getId() );
897 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
898 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
899 $this->setMwGlobals( array(
900 'wgAuth' => $oldWgAuth,
903 $this->assertSame( array(
904 array( LogLevel
::DEBUG
, 'denied by AuthPlugin' ),
905 ), $logger->getBuffer() );
906 $logger->clearBuffer();
908 // Test prevention by wfReadOnly()
909 $this->setMwGlobals( array(
910 'wgReadOnly' => 'Because',
912 $user = User
::newFromName( 'UTDoesNotExist' );
913 $this->assertFalse( $manager->autoCreateUser( $user ) );
914 $this->assertSame( 0, $user->getId() );
915 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
916 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
917 $this->setMwGlobals( array(
918 'wgReadOnly' => false,
921 $this->assertSame( array(
922 array( LogLevel
::DEBUG
, 'denied by wfReadOnly()' ),
923 ), $logger->getBuffer() );
924 $logger->clearBuffer();
926 // Test prevention by a previous session
927 $session->set( 'MWSession::AutoCreateBlacklist', 'test' );
928 $user = User
::newFromName( 'UTDoesNotExist' );
929 $this->assertFalse( $manager->autoCreateUser( $user ) );
930 $this->assertSame( 0, $user->getId() );
931 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
932 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
934 $this->assertSame( array(
935 array( LogLevel
::DEBUG
, 'blacklisted in session (test)' ),
936 ), $logger->getBuffer() );
937 $logger->clearBuffer();
939 // Test uncreatable name
940 $user = User
::newFromName( 'UTDoesNotExist@' );
941 $this->assertFalse( $manager->autoCreateUser( $user ) );
942 $this->assertSame( 0, $user->getId() );
943 $this->assertNotSame( 'UTDoesNotExist@', $user->getName() );
944 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
946 $this->assertSame( array(
947 array( LogLevel
::DEBUG
, 'Invalid username, blacklisting' ),
948 ), $logger->getBuffer() );
949 $logger->clearBuffer();
951 // Test AbortAutoAccount hook
952 $mock = $this->getMock( __CLASS__
, array( 'onAbortAutoAccount' ) );
953 $mock->expects( $this->once() )->method( 'onAbortAutoAccount' )
954 ->will( $this->returnCallback( function ( User
$user, &$msg ) {
958 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array( $mock ) ) );
959 $user = User
::newFromName( 'UTDoesNotExist' );
960 $this->assertFalse( $manager->autoCreateUser( $user ) );
961 $this->assertSame( 0, $user->getId() );
962 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
963 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
964 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array() ) );
966 $this->assertSame( array(
967 array( LogLevel
::DEBUG
, 'denied by hook: No way!' ),
968 ), $logger->getBuffer() );
969 $logger->clearBuffer();
971 // Test AbortAutoAccount hook screwing up the name
972 $mock = $this->getMock( 'stdClass', array( 'onAbortAutoAccount' ) );
973 $mock->expects( $this->once() )->method( 'onAbortAutoAccount' )
974 ->will( $this->returnCallback( function ( User
$user ) {
975 $user->setName( 'UTDoesNotExistEither' );
977 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array( $mock ) ) );
979 $user = User
::newFromName( 'UTDoesNotExist' );
980 $manager->autoCreateUser( $user );
981 $this->fail( 'Expected exception not thrown' );
982 } catch ( \UnexpectedValueException
$ex ) {
984 'AbortAutoAccount hook tried to change the user name',
988 $this->assertSame( 0, $user->getId() );
989 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
990 $this->assertNotSame( 'UTDoesNotExistEither', $user->getName() );
991 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
992 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExistEither', User
::READ_LATEST
) );
993 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array() ) );
995 $this->assertSame( array(), $logger->getBuffer() );
996 $logger->clearBuffer();
998 // Test for "exception backoff"
999 $user = User
::newFromName( 'UTDoesNotExist' );
1000 $cache = \ObjectCache
::getLocalClusterInstance();
1001 $backoffKey = wfMemcKey( 'MWSession', 'autocreate-failed', md5( $user->getName() ) );
1002 $cache->set( $backoffKey, 1, 60 * 10 );
1003 $this->assertFalse( $manager->autoCreateUser( $user ) );
1004 $this->assertSame( 0, $user->getId() );
1005 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
1006 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
1007 $cache->delete( $backoffKey );
1009 $this->assertSame( array(
1010 array( LogLevel
::DEBUG
, 'denied by prior creation attempt failures' ),
1011 ), $logger->getBuffer() );
1012 $logger->clearBuffer();
1014 // Sanity check that creation still works, and test completion hook
1015 $cb = $this->callback( function ( User
$user ) use ( $that ) {
1016 $that->assertNotEquals( 0, $user->getId() );
1017 $that->assertSame( 'UTSessionAutoCreate4', $user->getName() );
1018 $that->assertEquals(
1019 $user->getId(), User
::idFromName( 'UTSessionAutoCreate4', User
::READ_LATEST
)
1023 $mock = $this->getMock( 'stdClass',
1024 array( 'onAuthPluginAutoCreate', 'onLocalUserCreated' ) );
1025 $mock->expects( $this->once() )->method( 'onAuthPluginAutoCreate' )
1027 $mock->expects( $this->once() )->method( 'onLocalUserCreated' )
1028 ->with( $cb, $this->identicalTo( true ) );
1029 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1030 'AuthPluginAutoCreate' => array( $mock ),
1031 'LocalUserCreated' => array( $mock ),
1033 $user = User
::newFromName( 'UTSessionAutoCreate4' );
1034 $this->assertSame( 0, $user->getId(), 'sanity check' );
1035 $this->assertTrue( $manager->autoCreateUser( $user ) );
1036 $this->assertNotEquals( 0, $user->getId() );
1037 $this->assertSame( 'UTSessionAutoCreate4', $user->getName() );
1038 $this->assertEquals(
1040 User
::idFromName( 'UTSessionAutoCreate4', User
::READ_LATEST
)
1042 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1043 'AuthPluginAutoCreate' => array(),
1044 'LocalUserCreated' => array(),
1046 $this->assertSame( array(
1047 array( LogLevel
::INFO
, 'creating new user ({username}) - from: {url}' ),
1048 ), $logger->getBuffer() );
1049 $logger->clearBuffer();
1052 public function onAbortAutoAccount( User
$user, &$msg ) {
1055 public function testPreventSessionsForUser() {
1056 $manager = $this->getManager();
1058 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
1059 ->setMethods( array( 'preventSessionsForUser', '__toString' ) );
1061 $provider1 = $providerBuilder->getMock();
1062 $provider1->expects( $this->once() )->method( 'preventSessionsForUser' )
1063 ->with( $this->equalTo( 'UTSysop' ) );
1064 $provider1->expects( $this->any() )->method( '__toString' )
1065 ->will( $this->returnValue( 'MockProvider1' ) );
1067 $this->config
->set( 'SessionProviders', array(
1068 $this->objectCacheDef( $provider1 ),
1071 $this->assertFalse( $manager->isUserSessionPrevented( 'UTSysop' ) );
1072 $manager->preventSessionsForUser( 'UTSysop' );
1073 $this->assertTrue( $manager->isUserSessionPrevented( 'UTSysop' ) );
1076 public function testLoadSessionInfoFromStore() {
1077 $manager = $this->getManager();
1078 $logger = new \
TestLogger( true );
1079 $manager->setLogger( $logger );
1080 $request = new \
FauxRequest();
1082 // TestingAccessWrapper can't handle methods with reference arguments, sigh.
1083 $rClass = new \
ReflectionClass( $manager );
1084 $rMethod = $rClass->getMethod( 'loadSessionInfoFromStore' );
1085 $rMethod->setAccessible( true );
1086 $loadSessionInfoFromStore = function ( &$info ) use ( $rMethod, $manager, $request ) {
1087 return $rMethod->invokeArgs( $manager, array( &$info, $request ) );
1090 $userInfo = UserInfo
::newFromName( 'UTSysop', true );
1091 $unverifiedUserInfo = UserInfo
::newFromName( 'UTSysop', false );
1093 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
1095 'userId' => $userInfo->getId(),
1096 'userName' => $userInfo->getName(),
1097 'userToken' => $userInfo->getToken( true ),
1098 'provider' => 'Mock',
1101 $builder = $this->getMockBuilder( 'MediaWiki\\Session\\SessionProvider' )
1102 ->setMethods( array( '__toString', 'mergeMetadata', 'refreshSessionInfo' ) );
1104 $provider = $builder->getMockForAbstractClass();
1105 $provider->setManager( $manager );
1106 $provider->expects( $this->any() )->method( 'persistsSessionId' )
1107 ->will( $this->returnValue( true ) );
1108 $provider->expects( $this->any() )->method( 'canChangeUser' )
1109 ->will( $this->returnValue( true ) );
1110 $provider->expects( $this->any() )->method( 'refreshSessionInfo' )
1111 ->will( $this->returnValue( true ) );
1112 $provider->expects( $this->any() )->method( '__toString' )
1113 ->will( $this->returnValue( 'Mock' ) );
1114 $provider->expects( $this->any() )->method( 'mergeMetadata' )
1115 ->will( $this->returnCallback( function ( $a, $b ) {
1116 if ( $b === array( 'Throw' ) ) {
1117 throw new \
UnexpectedValueException( 'no merge!' );
1119 return array( 'Merged' );
1122 $provider2 = $builder->getMockForAbstractClass();
1123 $provider2->setManager( $manager );
1124 $provider2->expects( $this->any() )->method( 'persistsSessionId' )
1125 ->will( $this->returnValue( false ) );
1126 $provider2->expects( $this->any() )->method( 'canChangeUser' )
1127 ->will( $this->returnValue( false ) );
1128 $provider2->expects( $this->any() )->method( '__toString' )
1129 ->will( $this->returnValue( 'Mock2' ) );
1130 $provider2->expects( $this->any() )->method( 'refreshSessionInfo' )
1131 ->will( $this->returnCallback( function ( $info, $request, &$metadata ) {
1132 $metadata['changed'] = true;
1136 $provider3 = $builder->getMockForAbstractClass();
1137 $provider3->setManager( $manager );
1138 $provider3->expects( $this->any() )->method( 'persistsSessionId' )
1139 ->will( $this->returnValue( true ) );
1140 $provider3->expects( $this->any() )->method( 'canChangeUser' )
1141 ->will( $this->returnValue( true ) );
1142 $provider3->expects( $this->once() )->method( 'refreshSessionInfo' )
1143 ->will( $this->returnValue( false ) );
1144 $provider3->expects( $this->any() )->method( '__toString' )
1145 ->will( $this->returnValue( 'Mock3' ) );
1147 \TestingAccessWrapper
::newFromObject( $manager )->sessionProviders
= array(
1148 (string)$provider => $provider,
1149 (string)$provider2 => $provider2,
1150 (string)$provider3 => $provider3,
1153 // No metadata, basic usage
1154 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1155 'provider' => $provider,
1157 'userInfo' => $userInfo
1159 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1160 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1161 $this->assertFalse( $info->isIdSafe() );
1162 $this->assertSame( array(), $logger->getBuffer() );
1164 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1165 'provider' => $provider,
1166 'userInfo' => $userInfo
1168 $this->assertTrue( $info->isIdSafe(), 'sanity check' );
1169 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1170 $this->assertTrue( $info->isIdSafe() );
1171 $this->assertSame( array(), $logger->getBuffer() );
1173 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1174 'provider' => $provider2,
1176 'userInfo' => $userInfo
1178 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1179 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1180 $this->assertTrue( $info->isIdSafe() );
1181 $this->assertSame( array(), $logger->getBuffer() );
1183 // Unverified user, no metadata
1184 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1185 'provider' => $provider,
1187 'userInfo' => $unverifiedUserInfo
1189 $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
1190 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1191 $this->assertSame( array(
1194 'Session "{session}": Unverified user provided and no metadata to auth it',
1196 ), $logger->getBuffer() );
1197 $logger->clearBuffer();
1199 // No metadata, missing data
1200 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1202 'userInfo' => $userInfo
1204 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1205 $this->assertSame( array(
1206 array( LogLevel
::WARNING
, 'Session "{session}": Null provider and no metadata' ),
1207 ), $logger->getBuffer() );
1208 $logger->clearBuffer();
1210 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1211 'provider' => $provider,
1214 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1215 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1216 $this->assertInstanceOf( 'MediaWiki\\Session\\UserInfo', $info->getUserInfo() );
1217 $this->assertTrue( $info->getUserInfo()->isVerified() );
1218 $this->assertTrue( $info->getUserInfo()->isAnon() );
1219 $this->assertFalse( $info->isIdSafe() );
1220 $this->assertSame( array(), $logger->getBuffer() );
1222 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1223 'provider' => $provider2,
1226 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1227 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1228 $this->assertSame( array(
1229 array( LogLevel
::INFO
, 'Session "{session}": No user provided and provider cannot set user' )
1230 ), $logger->getBuffer() );
1231 $logger->clearBuffer();
1233 // Incomplete/bad metadata
1234 $this->store
->setRawSession( $id, true );
1235 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1236 $this->assertSame( array(
1237 array( LogLevel
::WARNING
, 'Session "{session}": Bad data' ),
1238 ), $logger->getBuffer() );
1239 $logger->clearBuffer();
1241 $this->store
->setRawSession( $id, array( 'data' => array() ) );
1242 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1243 $this->assertSame( array(
1244 array( LogLevel
::WARNING
, 'Session "{session}": Bad data structure' ),
1245 ), $logger->getBuffer() );
1246 $logger->clearBuffer();
1248 $this->store
->deleteSession( $id );
1249 $this->store
->setRawSession( $id, array( 'metadata' => $metadata ) );
1250 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1251 $this->assertSame( array(
1252 array( LogLevel
::WARNING
, 'Session "{session}": Bad data structure' ),
1253 ), $logger->getBuffer() );
1254 $logger->clearBuffer();
1256 $this->store
->setRawSession( $id, array( 'metadata' => $metadata, 'data' => true ) );
1257 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1258 $this->assertSame( array(
1259 array( LogLevel
::WARNING
, 'Session "{session}": Bad data structure' ),
1260 ), $logger->getBuffer() );
1261 $logger->clearBuffer();
1263 $this->store
->setRawSession( $id, array( 'metadata' => true, 'data' => array() ) );
1264 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1265 $this->assertSame( array(
1266 array( LogLevel
::WARNING
, 'Session "{session}": Bad data structure' ),
1267 ), $logger->getBuffer() );
1268 $logger->clearBuffer();
1270 foreach ( $metadata as $key => $dummy ) {
1272 unset( $tmp[$key] );
1273 $this->store
->setRawSession( $id, array( 'metadata' => $tmp, 'data' => array() ) );
1274 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1275 $this->assertSame( array(
1276 array( LogLevel
::WARNING
, 'Session "{session}": Bad metadata' ),
1277 ), $logger->getBuffer() );
1278 $logger->clearBuffer();
1281 // Basic usage with metadata
1282 $this->store
->setRawSession( $id, array( 'metadata' => $metadata, 'data' => array() ) );
1283 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1284 'provider' => $provider,
1286 'userInfo' => $userInfo
1288 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1289 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1290 $this->assertTrue( $info->isIdSafe() );
1291 $this->assertSame( array(), $logger->getBuffer() );
1293 // Mismatched provider
1294 $this->store
->setSessionMeta( $id, array( 'provider' => 'Bad' ) +
$metadata );
1295 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1296 'provider' => $provider,
1298 'userInfo' => $userInfo
1300 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1301 $this->assertSame( array(
1302 array( LogLevel
::WARNING
, 'Session "{session}": Wrong provider Bad !== Mock' ),
1303 ), $logger->getBuffer() );
1304 $logger->clearBuffer();
1307 $this->store
->setSessionMeta( $id, array( 'provider' => 'Bad' ) +
$metadata );
1308 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1310 'userInfo' => $userInfo
1312 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1313 $this->assertSame( array(
1314 array( LogLevel
::WARNING
, 'Session "{session}": Unknown provider Bad' ),
1315 ), $logger->getBuffer() );
1316 $logger->clearBuffer();
1319 $this->store
->setSessionMeta( $id, $metadata );
1320 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1322 'userInfo' => $userInfo
1324 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1325 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1326 $this->assertTrue( $info->isIdSafe() );
1327 $this->assertSame( array(), $logger->getBuffer() );
1329 // Bad user metadata
1330 $this->store
->setSessionMeta( $id, array( 'userId' => -1, 'userToken' => null ) +
$metadata );
1331 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1332 'provider' => $provider,
1335 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1336 $this->assertSame( array(
1337 array( LogLevel
::ERROR
, 'Session "{session}": {exception}' ),
1338 ), $logger->getBuffer() );
1339 $logger->clearBuffer();
1341 $this->store
->setSessionMeta(
1342 $id, array( 'userId' => 0, 'userName' => '<X>', 'userToken' => null ) +
$metadata
1344 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1345 'provider' => $provider,
1348 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1349 $this->assertSame( array(
1350 array( LogLevel
::ERROR
, 'Session "{session}": {exception}', ),
1351 ), $logger->getBuffer() );
1352 $logger->clearBuffer();
1354 // Mismatched user by ID
1355 $this->store
->setSessionMeta(
1356 $id, array( 'userId' => $userInfo->getId() +
1, 'userToken' => null ) +
$metadata
1358 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1359 'provider' => $provider,
1361 'userInfo' => $userInfo
1363 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1364 $this->assertSame( array(
1365 array( LogLevel
::WARNING
, 'Session "{session}": User ID mismatch, {uid_a} !== {uid_b}' ),
1366 ), $logger->getBuffer() );
1367 $logger->clearBuffer();
1369 // Mismatched user by name
1370 $this->store
->setSessionMeta(
1371 $id, array( 'userId' => 0, 'userName' => 'X', 'userToken' => null ) +
$metadata
1373 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1374 'provider' => $provider,
1376 'userInfo' => $userInfo
1378 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1379 $this->assertSame( array(
1380 array( LogLevel
::WARNING
, 'Session "{session}": User name mismatch, {uname_a} !== {uname_b}' ),
1381 ), $logger->getBuffer() );
1382 $logger->clearBuffer();
1384 // ID matches, name doesn't
1385 $this->store
->setSessionMeta(
1386 $id, array( 'userId' => $userInfo->getId(), 'userName' => 'X', 'userToken' => null ) +
$metadata
1388 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1389 'provider' => $provider,
1391 'userInfo' => $userInfo
1393 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1394 $this->assertSame( array(
1397 'Session "{session}": User ID matched but name didn\'t (rename?), {uname_a} !== {uname_b}'
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(
1415 'Session "{session}": Metadata has an anonymous user, ' .
1416 'but a non-anon user was provided',
1418 ), $logger->getBuffer() );
1419 $logger->clearBuffer();
1421 // Lookup user by ID
1422 $this->store
->setSessionMeta( $id, array( 'userToken' => null ) +
$metadata );
1423 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1424 'provider' => $provider,
1427 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1428 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1429 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1430 $this->assertTrue( $info->isIdSafe() );
1431 $this->assertSame( array(), $logger->getBuffer() );
1433 // Lookup user by name
1434 $this->store
->setSessionMeta(
1435 $id, array( 'userId' => 0, 'userName' => 'UTSysop', 'userToken' => null ) +
$metadata
1437 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1438 'provider' => $provider,
1441 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1442 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1443 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1444 $this->assertTrue( $info->isIdSafe() );
1445 $this->assertSame( array(), $logger->getBuffer() );
1447 // Lookup anonymous user
1448 $this->store
->setSessionMeta(
1449 $id, array( 'userId' => 0, 'userName' => null, 'userToken' => null ) +
$metadata
1451 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1452 'provider' => $provider,
1455 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1456 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1457 $this->assertTrue( $info->getUserInfo()->isAnon() );
1458 $this->assertTrue( $info->isIdSafe() );
1459 $this->assertSame( array(), $logger->getBuffer() );
1461 // Unverified user with metadata
1462 $this->store
->setSessionMeta( $id, $metadata );
1463 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1464 'provider' => $provider,
1466 'userInfo' => $unverifiedUserInfo
1468 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1469 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1470 $this->assertTrue( $info->getUserInfo()->isVerified() );
1471 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1472 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1473 $this->assertTrue( $info->isIdSafe() );
1474 $this->assertSame( array(), $logger->getBuffer() );
1476 // Unverified user with metadata
1477 $this->store
->setSessionMeta( $id, $metadata );
1478 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1479 'provider' => $provider,
1481 'userInfo' => $unverifiedUserInfo
1483 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1484 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1485 $this->assertTrue( $info->getUserInfo()->isVerified() );
1486 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1487 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1488 $this->assertTrue( $info->isIdSafe() );
1489 $this->assertSame( array(), $logger->getBuffer() );
1492 $this->store
->setSessionMeta( $id, array( 'userToken' => 'Bad' ) +
$metadata );
1493 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1494 'provider' => $provider,
1496 'userInfo' => $userInfo
1498 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1499 $this->assertSame( array(
1500 array( LogLevel
::WARNING
, 'Session "{session}": User token mismatch' ),
1501 ), $logger->getBuffer() );
1502 $logger->clearBuffer();
1504 // Provider metadata
1505 $this->store
->setSessionMeta( $id, array( 'provider' => 'Mock2' ) +
$metadata );
1506 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1507 'provider' => $provider2,
1509 'userInfo' => $userInfo,
1510 'metadata' => array( 'Info' ),
1512 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1513 $this->assertSame( array( 'Info', 'changed' => true ), $info->getProviderMetadata() );
1514 $this->assertSame( array(), $logger->getBuffer() );
1516 $this->store
->setSessionMeta( $id, array( 'providerMetadata' => array( 'Saved' ) ) +
$metadata );
1517 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1518 'provider' => $provider,
1520 'userInfo' => $userInfo,
1522 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1523 $this->assertSame( array( 'Saved' ), $info->getProviderMetadata() );
1524 $this->assertSame( array(), $logger->getBuffer() );
1526 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1527 'provider' => $provider,
1529 'userInfo' => $userInfo,
1530 'metadata' => array( 'Info' ),
1532 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1533 $this->assertSame( array( 'Merged' ), $info->getProviderMetadata() );
1534 $this->assertSame( array(), $logger->getBuffer() );
1536 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1537 'provider' => $provider,
1539 'userInfo' => $userInfo,
1540 'metadata' => array( 'Throw' ),
1542 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1543 $this->assertSame( array(
1546 'Session "{session}": Metadata merge failed: {exception}',
1548 ), $logger->getBuffer() );
1549 $logger->clearBuffer();
1551 // Remember from session
1552 $this->store
->setSessionMeta( $id, $metadata );
1553 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1554 'provider' => $provider,
1557 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1558 $this->assertFalse( $info->wasRemembered() );
1559 $this->assertSame( array(), $logger->getBuffer() );
1561 $this->store
->setSessionMeta( $id, array( 'remember' => true ) +
$metadata );
1562 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1563 'provider' => $provider,
1566 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1567 $this->assertTrue( $info->wasRemembered() );
1568 $this->assertSame( array(), $logger->getBuffer() );
1570 $this->store
->setSessionMeta( $id, array( 'remember' => false ) +
$metadata );
1571 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1572 'provider' => $provider,
1574 'userInfo' => $userInfo
1576 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1577 $this->assertTrue( $info->wasRemembered() );
1578 $this->assertSame( array(), $logger->getBuffer() );
1580 // forceHTTPS from session
1581 $this->store
->setSessionMeta( $id, $metadata );
1582 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1583 'provider' => $provider,
1585 'userInfo' => $userInfo
1587 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1588 $this->assertFalse( $info->forceHTTPS() );
1589 $this->assertSame( array(), $logger->getBuffer() );
1591 $this->store
->setSessionMeta( $id, array( 'forceHTTPS' => true ) +
$metadata );
1592 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1593 'provider' => $provider,
1595 'userInfo' => $userInfo
1597 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1598 $this->assertTrue( $info->forceHTTPS() );
1599 $this->assertSame( array(), $logger->getBuffer() );
1601 $this->store
->setSessionMeta( $id, array( 'forceHTTPS' => false ) +
$metadata );
1602 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1603 'provider' => $provider,
1605 'userInfo' => $userInfo,
1606 'forceHTTPS' => true
1608 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1609 $this->assertTrue( $info->forceHTTPS() );
1610 $this->assertSame( array(), $logger->getBuffer() );
1612 // "Persist" flag from session
1613 $this->store
->setSessionMeta( $id, $metadata );
1614 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1615 'provider' => $provider,
1617 'userInfo' => $userInfo
1619 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1620 $this->assertFalse( $info->wasPersisted() );
1621 $this->assertSame( array(), $logger->getBuffer() );
1623 $this->store
->setSessionMeta( $id, array( 'persisted' => true ) +
$metadata );
1624 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1625 'provider' => $provider,
1627 'userInfo' => $userInfo
1629 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1630 $this->assertTrue( $info->wasPersisted() );
1631 $this->assertSame( array(), $logger->getBuffer() );
1633 $this->store
->setSessionMeta( $id, array( 'persisted' => false ) +
$metadata );
1634 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1635 'provider' => $provider,
1637 'userInfo' => $userInfo,
1640 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1641 $this->assertTrue( $info->wasPersisted() );
1642 $this->assertSame( array(), $logger->getBuffer() );
1644 // Provider refreshSessionInfo() returning false
1645 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1646 'provider' => $provider3,
1648 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1649 $this->assertSame( array(), $logger->getBuffer() );
1654 $data = array( 'foo' => 1 );
1655 $this->store
->setSession( $id, array( 'metadata' => $metadata, 'data' => $data ) );
1656 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1657 'provider' => $provider,
1659 'userInfo' => $userInfo
1661 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1662 'SessionCheckInfo' => array( function ( &$reason, $i, $r, $m, $d ) use (
1663 $that, $info, $metadata, $data, $request, &$called
1665 $that->assertSame( $info->getId(), $i->getId() );
1666 $that->assertSame( $info->getProvider(), $i->getProvider() );
1667 $that->assertSame( $info->getUserInfo(), $i->getUserInfo() );
1668 $that->assertSame( $request, $r );
1669 $that->assertEquals( $metadata, $m );
1670 $that->assertEquals( $data, $d );
1675 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1676 $this->assertTrue( $called );
1677 $this->assertSame( array(
1678 array( LogLevel
::WARNING
, 'Session "{session}": Hook aborted' ),
1679 ), $logger->getBuffer() );
1680 $logger->clearBuffer();