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' ),
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 TestBagOStuff();
767 $this->setMwGlobals( array( 'wgMainCacheType' => __METHOD__
) );
768 $this->setMWGlobals( array(
769 'wgAuth' => new AuthPlugin
,
772 $this->stashMwGlobals( array( 'wgGroupPermissions' ) );
773 $wgGroupPermissions['*']['createaccount'] = true;
774 $wgGroupPermissions['*']['autocreateaccount'] = false;
776 // Replace the global singleton with one configured for testing
777 $manager = $this->getManager();
778 $reset = TestUtils
::setSessionManagerSingleton( $manager );
780 $logger = new \
TestLogger( true, function ( $m ) {
781 if ( substr( $m, 0, 15 ) === 'SessionBackend ' ) {
785 $m = str_replace( 'MediaWiki\Session\SessionManager::autoCreateUser: ', '', $m );
788 $manager->setLogger( $logger );
790 $session = SessionManager
::getGlobalSession();
792 // Can't create an already-existing user
793 $user = User
::newFromName( 'UTSysop' );
794 $id = $user->getId();
795 $this->assertFalse( $manager->autoCreateUser( $user ) );
796 $this->assertSame( $id, $user->getId() );
797 $this->assertSame( 'UTSysop', $user->getName() );
798 $this->assertSame( array(), $logger->getBuffer() );
799 $logger->clearBuffer();
801 // Sanity check that creation works at all
802 $user = User
::newFromName( 'UTSessionAutoCreate1' );
803 $this->assertSame( 0, $user->getId(), 'sanity check' );
804 $this->assertTrue( $manager->autoCreateUser( $user ) );
805 $this->assertNotEquals( 0, $user->getId() );
806 $this->assertSame( 'UTSessionAutoCreate1', $user->getName() );
808 $user->getId(), User
::idFromName( 'UTSessionAutoCreate1', User
::READ_LATEST
)
810 $this->assertSame( array(
811 array( LogLevel
::INFO
, 'creating new user ({username}) - from: {url}' ),
812 ), $logger->getBuffer() );
813 $logger->clearBuffer();
815 // Check lack of permissions
816 $wgGroupPermissions['*']['createaccount'] = false;
817 $wgGroupPermissions['*']['autocreateaccount'] = false;
818 $user = User
::newFromName( 'UTDoesNotExist' );
819 $this->assertFalse( $manager->autoCreateUser( $user ) );
820 $this->assertSame( 0, $user->getId() );
821 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
822 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
824 $this->assertSame( array(
827 'user is blocked from this wiki, blacklisting',
829 ), $logger->getBuffer() );
830 $logger->clearBuffer();
832 // Check other permission
833 $wgGroupPermissions['*']['createaccount'] = false;
834 $wgGroupPermissions['*']['autocreateaccount'] = true;
835 $user = User
::newFromName( 'UTSessionAutoCreate2' );
836 $this->assertSame( 0, $user->getId(), 'sanity check' );
837 $this->assertTrue( $manager->autoCreateUser( $user ) );
838 $this->assertNotEquals( 0, $user->getId() );
839 $this->assertSame( 'UTSessionAutoCreate2', $user->getName() );
841 $user->getId(), User
::idFromName( 'UTSessionAutoCreate2', User
::READ_LATEST
)
843 $this->assertSame( array(
844 array( LogLevel
::INFO
, 'creating new user ({username}) - from: {url}' ),
845 ), $logger->getBuffer() );
846 $logger->clearBuffer();
848 // Test account-creation block
850 $block = new \
Block( array(
851 'address' => $anon->getName(),
853 'reason' => __METHOD__
,
854 'expiry' => time() +
100500,
855 'createAccount' => true,
858 $this->assertInstanceOf( 'Block', $anon->isBlockedFromCreateAccount(), 'sanity check' );
859 $reset2 = new \
ScopedCallback( array( $block, 'delete' ) );
860 $user = User
::newFromName( 'UTDoesNotExist' );
861 $this->assertFalse( $manager->autoCreateUser( $user ) );
862 $this->assertSame( 0, $user->getId() );
863 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
864 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
865 \ScopedCallback
::consume( $reset2 );
867 $this->assertSame( array(
868 array( LogLevel
::DEBUG
, 'user is blocked from this wiki, blacklisting' ),
869 ), $logger->getBuffer() );
870 $logger->clearBuffer();
872 // Sanity check that creation still works
873 $user = User
::newFromName( 'UTSessionAutoCreate3' );
874 $this->assertSame( 0, $user->getId(), 'sanity check' );
875 $this->assertTrue( $manager->autoCreateUser( $user ) );
876 $this->assertNotEquals( 0, $user->getId() );
877 $this->assertSame( 'UTSessionAutoCreate3', $user->getName() );
879 $user->getId(), User
::idFromName( 'UTSessionAutoCreate3', User
::READ_LATEST
)
881 $this->assertSame( array(
882 array( LogLevel
::INFO
, 'creating new user ({username}) - from: {url}' ),
883 ), $logger->getBuffer() );
884 $logger->clearBuffer();
886 // Test prevention by AuthPlugin
888 $oldWgAuth = $wgAuth;
889 $mockWgAuth = $this->getMock( 'AuthPlugin', array( 'autoCreate' ) );
890 $mockWgAuth->expects( $this->once() )->method( 'autoCreate' )
891 ->will( $this->returnValue( false ) );
892 $this->setMwGlobals( array(
893 'wgAuth' => $mockWgAuth,
895 $user = User
::newFromName( 'UTDoesNotExist' );
896 $this->assertFalse( $manager->autoCreateUser( $user ) );
897 $this->assertSame( 0, $user->getId() );
898 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
899 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
900 $this->setMwGlobals( array(
901 'wgAuth' => $oldWgAuth,
904 $this->assertSame( array(
905 array( LogLevel
::DEBUG
, 'denied by AuthPlugin' ),
906 ), $logger->getBuffer() );
907 $logger->clearBuffer();
909 // Test prevention by wfReadOnly()
910 $this->setMwGlobals( array(
911 'wgReadOnly' => 'Because',
913 $user = User
::newFromName( 'UTDoesNotExist' );
914 $this->assertFalse( $manager->autoCreateUser( $user ) );
915 $this->assertSame( 0, $user->getId() );
916 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
917 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
918 $this->setMwGlobals( array(
919 'wgReadOnly' => false,
922 $this->assertSame( array(
923 array( LogLevel
::DEBUG
, 'denied by wfReadOnly()' ),
924 ), $logger->getBuffer() );
925 $logger->clearBuffer();
927 // Test prevention by a previous session
928 $session->set( 'MWSession::AutoCreateBlacklist', 'test' );
929 $user = User
::newFromName( 'UTDoesNotExist' );
930 $this->assertFalse( $manager->autoCreateUser( $user ) );
931 $this->assertSame( 0, $user->getId() );
932 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
933 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
935 $this->assertSame( array(
936 array( LogLevel
::DEBUG
, 'blacklisted in session (test)' ),
937 ), $logger->getBuffer() );
938 $logger->clearBuffer();
940 // Test uncreatable name
941 $user = User
::newFromName( 'UTDoesNotExist@' );
942 $this->assertFalse( $manager->autoCreateUser( $user ) );
943 $this->assertSame( 0, $user->getId() );
944 $this->assertNotSame( 'UTDoesNotExist@', $user->getName() );
945 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
947 $this->assertSame( array(
948 array( LogLevel
::DEBUG
, 'Invalid username, blacklisting' ),
949 ), $logger->getBuffer() );
950 $logger->clearBuffer();
952 // Test AbortAutoAccount hook
953 $mock = $this->getMock( __CLASS__
, array( 'onAbortAutoAccount' ) );
954 $mock->expects( $this->once() )->method( 'onAbortAutoAccount' )
955 ->will( $this->returnCallback( function ( User
$user, &$msg ) {
959 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array( $mock ) ) );
960 $user = User
::newFromName( 'UTDoesNotExist' );
961 $this->assertFalse( $manager->autoCreateUser( $user ) );
962 $this->assertSame( 0, $user->getId() );
963 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
964 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
965 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array() ) );
967 $this->assertSame( array(
968 array( LogLevel
::DEBUG
, 'denied by hook: No way!' ),
969 ), $logger->getBuffer() );
970 $logger->clearBuffer();
972 // Test AbortAutoAccount hook screwing up the name
973 $mock = $this->getMock( 'stdClass', array( 'onAbortAutoAccount' ) );
974 $mock->expects( $this->once() )->method( 'onAbortAutoAccount' )
975 ->will( $this->returnCallback( function ( User
$user ) {
976 $user->setName( 'UTDoesNotExistEither' );
978 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array( $mock ) ) );
980 $user = User
::newFromName( 'UTDoesNotExist' );
981 $manager->autoCreateUser( $user );
982 $this->fail( 'Expected exception not thrown' );
983 } catch ( \UnexpectedValueException
$ex ) {
985 'AbortAutoAccount hook tried to change the user name',
989 $this->assertSame( 0, $user->getId() );
990 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
991 $this->assertNotSame( 'UTDoesNotExistEither', $user->getName() );
992 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
993 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExistEither', User
::READ_LATEST
) );
994 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array() ) );
996 $this->assertSame( array(), $logger->getBuffer() );
997 $logger->clearBuffer();
999 // Test for "exception backoff"
1000 $user = User
::newFromName( 'UTDoesNotExist' );
1001 $cache = \ObjectCache
::getLocalClusterInstance();
1002 $backoffKey = wfMemcKey( 'MWSession', 'autocreate-failed', md5( $user->getName() ) );
1003 $cache->set( $backoffKey, 1, 60 * 10 );
1004 $this->assertFalse( $manager->autoCreateUser( $user ) );
1005 $this->assertSame( 0, $user->getId() );
1006 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
1007 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
1008 $cache->delete( $backoffKey );
1010 $this->assertSame( array(
1011 array( LogLevel
::DEBUG
, 'denied by prior creation attempt failures' ),
1012 ), $logger->getBuffer() );
1013 $logger->clearBuffer();
1015 // Sanity check that creation still works, and test completion hook
1016 $cb = $this->callback( function ( User
$user ) use ( $that ) {
1017 $that->assertNotEquals( 0, $user->getId() );
1018 $that->assertSame( 'UTSessionAutoCreate4', $user->getName() );
1019 $that->assertEquals(
1020 $user->getId(), User
::idFromName( 'UTSessionAutoCreate4', User
::READ_LATEST
)
1024 $mock = $this->getMock( 'stdClass',
1025 array( 'onAuthPluginAutoCreate', 'onLocalUserCreated' ) );
1026 $mock->expects( $this->once() )->method( 'onAuthPluginAutoCreate' )
1028 $mock->expects( $this->once() )->method( 'onLocalUserCreated' )
1029 ->with( $cb, $this->identicalTo( true ) );
1030 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1031 'AuthPluginAutoCreate' => array( $mock ),
1032 'LocalUserCreated' => array( $mock ),
1034 $user = User
::newFromName( 'UTSessionAutoCreate4' );
1035 $this->assertSame( 0, $user->getId(), 'sanity check' );
1036 $this->assertTrue( $manager->autoCreateUser( $user ) );
1037 $this->assertNotEquals( 0, $user->getId() );
1038 $this->assertSame( 'UTSessionAutoCreate4', $user->getName() );
1039 $this->assertEquals(
1041 User
::idFromName( 'UTSessionAutoCreate4', User
::READ_LATEST
)
1043 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1044 'AuthPluginAutoCreate' => array(),
1045 'LocalUserCreated' => array(),
1047 $this->assertSame( array(
1048 array( LogLevel
::INFO
, 'creating new user ({username}) - from: {url}' ),
1049 ), $logger->getBuffer() );
1050 $logger->clearBuffer();
1053 public function onAbortAutoAccount( User
$user, &$msg ) {
1056 public function testPreventSessionsForUser() {
1057 $manager = $this->getManager();
1059 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
1060 ->setMethods( array( 'preventSessionsForUser', '__toString' ) );
1062 $provider1 = $providerBuilder->getMock();
1063 $provider1->expects( $this->once() )->method( 'preventSessionsForUser' )
1064 ->with( $this->equalTo( 'UTSysop' ) );
1065 $provider1->expects( $this->any() )->method( '__toString' )
1066 ->will( $this->returnValue( 'MockProvider1' ) );
1068 $this->config
->set( 'SessionProviders', array(
1069 $this->objectCacheDef( $provider1 ),
1072 $user = User
::newFromName( 'UTSysop' );
1073 $token = $user->getToken( true );
1075 $this->assertFalse( $manager->isUserSessionPrevented( 'UTSysop' ) );
1076 $manager->preventSessionsForUser( 'UTSysop' );
1077 $this->assertNotEquals( $token, User
::newFromName( 'UTSysop' )->getToken() );
1078 $this->assertTrue( $manager->isUserSessionPrevented( 'UTSysop' ) );
1081 public function testLoadSessionInfoFromStore() {
1082 $manager = $this->getManager();
1083 $logger = new \
TestLogger( true );
1084 $manager->setLogger( $logger );
1085 $request = new \
FauxRequest();
1087 // TestingAccessWrapper can't handle methods with reference arguments, sigh.
1088 $rClass = new \
ReflectionClass( $manager );
1089 $rMethod = $rClass->getMethod( 'loadSessionInfoFromStore' );
1090 $rMethod->setAccessible( true );
1091 $loadSessionInfoFromStore = function ( &$info ) use ( $rMethod, $manager, $request ) {
1092 return $rMethod->invokeArgs( $manager, array( &$info, $request ) );
1095 $userInfo = UserInfo
::newFromName( 'UTSysop', true );
1096 $unverifiedUserInfo = UserInfo
::newFromName( 'UTSysop', false );
1098 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
1100 'userId' => $userInfo->getId(),
1101 'userName' => $userInfo->getName(),
1102 'userToken' => $userInfo->getToken( true ),
1103 'provider' => 'Mock',
1106 $builder = $this->getMockBuilder( 'MediaWiki\\Session\\SessionProvider' )
1107 ->setMethods( array( '__toString', 'mergeMetadata', 'refreshSessionInfo' ) );
1109 $provider = $builder->getMockForAbstractClass();
1110 $provider->setManager( $manager );
1111 $provider->expects( $this->any() )->method( 'persistsSessionId' )
1112 ->will( $this->returnValue( true ) );
1113 $provider->expects( $this->any() )->method( 'canChangeUser' )
1114 ->will( $this->returnValue( true ) );
1115 $provider->expects( $this->any() )->method( 'refreshSessionInfo' )
1116 ->will( $this->returnValue( true ) );
1117 $provider->expects( $this->any() )->method( '__toString' )
1118 ->will( $this->returnValue( 'Mock' ) );
1119 $provider->expects( $this->any() )->method( 'mergeMetadata' )
1120 ->will( $this->returnCallback( function ( $a, $b ) {
1121 if ( $b === array( 'Throw' ) ) {
1122 throw new \
UnexpectedValueException( 'no merge!' );
1124 return array( 'Merged' );
1127 $provider2 = $builder->getMockForAbstractClass();
1128 $provider2->setManager( $manager );
1129 $provider2->expects( $this->any() )->method( 'persistsSessionId' )
1130 ->will( $this->returnValue( false ) );
1131 $provider2->expects( $this->any() )->method( 'canChangeUser' )
1132 ->will( $this->returnValue( false ) );
1133 $provider2->expects( $this->any() )->method( '__toString' )
1134 ->will( $this->returnValue( 'Mock2' ) );
1135 $provider2->expects( $this->any() )->method( 'refreshSessionInfo' )
1136 ->will( $this->returnCallback( function ( $info, $request, &$metadata ) {
1137 $metadata['changed'] = true;
1141 $provider3 = $builder->getMockForAbstractClass();
1142 $provider3->setManager( $manager );
1143 $provider3->expects( $this->any() )->method( 'persistsSessionId' )
1144 ->will( $this->returnValue( true ) );
1145 $provider3->expects( $this->any() )->method( 'canChangeUser' )
1146 ->will( $this->returnValue( true ) );
1147 $provider3->expects( $this->once() )->method( 'refreshSessionInfo' )
1148 ->will( $this->returnValue( false ) );
1149 $provider3->expects( $this->any() )->method( '__toString' )
1150 ->will( $this->returnValue( 'Mock3' ) );
1152 \TestingAccessWrapper
::newFromObject( $manager )->sessionProviders
= array(
1153 (string)$provider => $provider,
1154 (string)$provider2 => $provider2,
1155 (string)$provider3 => $provider3,
1158 // No metadata, basic usage
1159 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1160 'provider' => $provider,
1162 'userInfo' => $userInfo
1164 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1165 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1166 $this->assertFalse( $info->isIdSafe() );
1167 $this->assertSame( array(), $logger->getBuffer() );
1169 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1170 'provider' => $provider,
1171 'userInfo' => $userInfo
1173 $this->assertTrue( $info->isIdSafe(), 'sanity check' );
1174 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1175 $this->assertTrue( $info->isIdSafe() );
1176 $this->assertSame( array(), $logger->getBuffer() );
1178 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1179 'provider' => $provider2,
1181 'userInfo' => $userInfo
1183 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1184 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1185 $this->assertTrue( $info->isIdSafe() );
1186 $this->assertSame( array(), $logger->getBuffer() );
1188 // Unverified user, no metadata
1189 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1190 'provider' => $provider,
1192 'userInfo' => $unverifiedUserInfo
1194 $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
1195 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1196 $this->assertSame( array(
1199 'Session "{session}": Unverified user provided and no metadata to auth it',
1201 ), $logger->getBuffer() );
1202 $logger->clearBuffer();
1204 // No metadata, missing data
1205 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1207 'userInfo' => $userInfo
1209 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1210 $this->assertSame( array(
1211 array( LogLevel
::WARNING
, 'Session "{session}": Null provider and no metadata' ),
1212 ), $logger->getBuffer() );
1213 $logger->clearBuffer();
1215 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1216 'provider' => $provider,
1219 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1220 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1221 $this->assertInstanceOf( 'MediaWiki\\Session\\UserInfo', $info->getUserInfo() );
1222 $this->assertTrue( $info->getUserInfo()->isVerified() );
1223 $this->assertTrue( $info->getUserInfo()->isAnon() );
1224 $this->assertFalse( $info->isIdSafe() );
1225 $this->assertSame( array(), $logger->getBuffer() );
1227 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1228 'provider' => $provider2,
1231 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1232 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1233 $this->assertSame( array(
1234 array( LogLevel
::INFO
, 'Session "{session}": No user provided and provider cannot set user' )
1235 ), $logger->getBuffer() );
1236 $logger->clearBuffer();
1238 // Incomplete/bad metadata
1239 $this->store
->setRawSession( $id, true );
1240 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1241 $this->assertSame( array(
1242 array( LogLevel
::WARNING
, 'Session "{session}": Bad data' ),
1243 ), $logger->getBuffer() );
1244 $logger->clearBuffer();
1246 $this->store
->setRawSession( $id, array( 'data' => array() ) );
1247 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1248 $this->assertSame( array(
1249 array( LogLevel
::WARNING
, 'Session "{session}": Bad data structure' ),
1250 ), $logger->getBuffer() );
1251 $logger->clearBuffer();
1253 $this->store
->deleteSession( $id );
1254 $this->store
->setRawSession( $id, array( 'metadata' => $metadata ) );
1255 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1256 $this->assertSame( array(
1257 array( LogLevel
::WARNING
, 'Session "{session}": Bad data structure' ),
1258 ), $logger->getBuffer() );
1259 $logger->clearBuffer();
1261 $this->store
->setRawSession( $id, array( 'metadata' => $metadata, 'data' => true ) );
1262 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1263 $this->assertSame( array(
1264 array( LogLevel
::WARNING
, 'Session "{session}": Bad data structure' ),
1265 ), $logger->getBuffer() );
1266 $logger->clearBuffer();
1268 $this->store
->setRawSession( $id, array( 'metadata' => true, 'data' => array() ) );
1269 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1270 $this->assertSame( array(
1271 array( LogLevel
::WARNING
, 'Session "{session}": Bad data structure' ),
1272 ), $logger->getBuffer() );
1273 $logger->clearBuffer();
1275 foreach ( $metadata as $key => $dummy ) {
1277 unset( $tmp[$key] );
1278 $this->store
->setRawSession( $id, array( 'metadata' => $tmp, 'data' => array() ) );
1279 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1280 $this->assertSame( array(
1281 array( LogLevel
::WARNING
, 'Session "{session}": Bad metadata' ),
1282 ), $logger->getBuffer() );
1283 $logger->clearBuffer();
1286 // Basic usage with metadata
1287 $this->store
->setRawSession( $id, array( 'metadata' => $metadata, 'data' => array() ) );
1288 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1289 'provider' => $provider,
1291 'userInfo' => $userInfo
1293 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1294 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1295 $this->assertTrue( $info->isIdSafe() );
1296 $this->assertSame( array(), $logger->getBuffer() );
1298 // Mismatched provider
1299 $this->store
->setSessionMeta( $id, array( 'provider' => 'Bad' ) +
$metadata );
1300 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1301 'provider' => $provider,
1303 'userInfo' => $userInfo
1305 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1306 $this->assertSame( array(
1307 array( LogLevel
::WARNING
, 'Session "{session}": Wrong provider Bad !== Mock' ),
1308 ), $logger->getBuffer() );
1309 $logger->clearBuffer();
1312 $this->store
->setSessionMeta( $id, array( 'provider' => 'Bad' ) +
$metadata );
1313 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1315 'userInfo' => $userInfo
1317 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1318 $this->assertSame( array(
1319 array( LogLevel
::WARNING
, 'Session "{session}": Unknown provider Bad' ),
1320 ), $logger->getBuffer() );
1321 $logger->clearBuffer();
1324 $this->store
->setSessionMeta( $id, $metadata );
1325 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1327 'userInfo' => $userInfo
1329 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1330 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1331 $this->assertTrue( $info->isIdSafe() );
1332 $this->assertSame( array(), $logger->getBuffer() );
1334 // Bad user metadata
1335 $this->store
->setSessionMeta( $id, array( 'userId' => -1, 'userToken' => null ) +
$metadata );
1336 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1337 'provider' => $provider,
1340 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1341 $this->assertSame( array(
1342 array( LogLevel
::ERROR
, 'Session "{session}": {exception}' ),
1343 ), $logger->getBuffer() );
1344 $logger->clearBuffer();
1346 $this->store
->setSessionMeta(
1347 $id, array( 'userId' => 0, 'userName' => '<X>', 'userToken' => null ) +
$metadata
1349 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1350 'provider' => $provider,
1353 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1354 $this->assertSame( array(
1355 array( LogLevel
::ERROR
, 'Session "{session}": {exception}', ),
1356 ), $logger->getBuffer() );
1357 $logger->clearBuffer();
1359 // Mismatched user by ID
1360 $this->store
->setSessionMeta(
1361 $id, array( 'userId' => $userInfo->getId() +
1, 'userToken' => null ) +
$metadata
1363 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1364 'provider' => $provider,
1366 'userInfo' => $userInfo
1368 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1369 $this->assertSame( array(
1370 array( LogLevel
::WARNING
, 'Session "{session}": User ID mismatch, {uid_a} !== {uid_b}' ),
1371 ), $logger->getBuffer() );
1372 $logger->clearBuffer();
1374 // Mismatched user by name
1375 $this->store
->setSessionMeta(
1376 $id, array( 'userId' => 0, 'userName' => 'X', 'userToken' => null ) +
$metadata
1378 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1379 'provider' => $provider,
1381 'userInfo' => $userInfo
1383 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1384 $this->assertSame( array(
1385 array( LogLevel
::WARNING
, 'Session "{session}": User name mismatch, {uname_a} !== {uname_b}' ),
1386 ), $logger->getBuffer() );
1387 $logger->clearBuffer();
1389 // ID matches, name doesn't
1390 $this->store
->setSessionMeta(
1391 $id, array( 'userId' => $userInfo->getId(), 'userName' => 'X', 'userToken' => null ) +
$metadata
1393 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1394 'provider' => $provider,
1396 'userInfo' => $userInfo
1398 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1399 $this->assertSame( array(
1402 'Session "{session}": User ID matched but name didn\'t (rename?), {uname_a} !== {uname_b}'
1404 ), $logger->getBuffer() );
1405 $logger->clearBuffer();
1407 // Mismatched anon user
1408 $this->store
->setSessionMeta(
1409 $id, array( 'userId' => 0, 'userName' => null, 'userToken' => null ) +
$metadata
1411 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1412 'provider' => $provider,
1414 'userInfo' => $userInfo
1416 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1417 $this->assertSame( array(
1420 'Session "{session}": Metadata has an anonymous user, ' .
1421 'but a non-anon user was provided',
1423 ), $logger->getBuffer() );
1424 $logger->clearBuffer();
1426 // Lookup user by ID
1427 $this->store
->setSessionMeta( $id, array( 'userToken' => null ) +
$metadata );
1428 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1429 'provider' => $provider,
1432 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1433 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1434 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1435 $this->assertTrue( $info->isIdSafe() );
1436 $this->assertSame( array(), $logger->getBuffer() );
1438 // Lookup user by name
1439 $this->store
->setSessionMeta(
1440 $id, array( 'userId' => 0, 'userName' => 'UTSysop', 'userToken' => null ) +
$metadata
1442 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1443 'provider' => $provider,
1446 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1447 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1448 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1449 $this->assertTrue( $info->isIdSafe() );
1450 $this->assertSame( array(), $logger->getBuffer() );
1452 // Lookup anonymous user
1453 $this->store
->setSessionMeta(
1454 $id, array( 'userId' => 0, 'userName' => null, 'userToken' => null ) +
$metadata
1456 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1457 'provider' => $provider,
1460 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1461 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1462 $this->assertTrue( $info->getUserInfo()->isAnon() );
1463 $this->assertTrue( $info->isIdSafe() );
1464 $this->assertSame( array(), $logger->getBuffer() );
1466 // Unverified user with metadata
1467 $this->store
->setSessionMeta( $id, $metadata );
1468 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1469 'provider' => $provider,
1471 'userInfo' => $unverifiedUserInfo
1473 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1474 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1475 $this->assertTrue( $info->getUserInfo()->isVerified() );
1476 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1477 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1478 $this->assertTrue( $info->isIdSafe() );
1479 $this->assertSame( array(), $logger->getBuffer() );
1481 // Unverified user with metadata
1482 $this->store
->setSessionMeta( $id, $metadata );
1483 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1484 'provider' => $provider,
1486 'userInfo' => $unverifiedUserInfo
1488 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1489 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1490 $this->assertTrue( $info->getUserInfo()->isVerified() );
1491 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1492 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1493 $this->assertTrue( $info->isIdSafe() );
1494 $this->assertSame( array(), $logger->getBuffer() );
1497 $this->store
->setSessionMeta( $id, array( 'userToken' => 'Bad' ) +
$metadata );
1498 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1499 'provider' => $provider,
1501 'userInfo' => $userInfo
1503 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1504 $this->assertSame( array(
1505 array( LogLevel
::WARNING
, 'Session "{session}": User token mismatch' ),
1506 ), $logger->getBuffer() );
1507 $logger->clearBuffer();
1509 // Provider metadata
1510 $this->store
->setSessionMeta( $id, array( 'provider' => 'Mock2' ) +
$metadata );
1511 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1512 'provider' => $provider2,
1514 'userInfo' => $userInfo,
1515 'metadata' => array( 'Info' ),
1517 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1518 $this->assertSame( array( 'Info', 'changed' => true ), $info->getProviderMetadata() );
1519 $this->assertSame( array(), $logger->getBuffer() );
1521 $this->store
->setSessionMeta( $id, array( 'providerMetadata' => array( 'Saved' ) ) +
$metadata );
1522 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1523 'provider' => $provider,
1525 'userInfo' => $userInfo,
1527 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1528 $this->assertSame( array( 'Saved' ), $info->getProviderMetadata() );
1529 $this->assertSame( array(), $logger->getBuffer() );
1531 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1532 'provider' => $provider,
1534 'userInfo' => $userInfo,
1535 'metadata' => array( 'Info' ),
1537 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1538 $this->assertSame( array( 'Merged' ), $info->getProviderMetadata() );
1539 $this->assertSame( array(), $logger->getBuffer() );
1541 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1542 'provider' => $provider,
1544 'userInfo' => $userInfo,
1545 'metadata' => array( 'Throw' ),
1547 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1548 $this->assertSame( array(
1551 'Session "{session}": Metadata merge failed: {exception}',
1553 ), $logger->getBuffer() );
1554 $logger->clearBuffer();
1556 // Remember from session
1557 $this->store
->setSessionMeta( $id, $metadata );
1558 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1559 'provider' => $provider,
1562 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1563 $this->assertFalse( $info->wasRemembered() );
1564 $this->assertSame( array(), $logger->getBuffer() );
1566 $this->store
->setSessionMeta( $id, array( 'remember' => true ) +
$metadata );
1567 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1568 'provider' => $provider,
1571 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1572 $this->assertTrue( $info->wasRemembered() );
1573 $this->assertSame( array(), $logger->getBuffer() );
1575 $this->store
->setSessionMeta( $id, array( 'remember' => false ) +
$metadata );
1576 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1577 'provider' => $provider,
1579 'userInfo' => $userInfo
1581 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1582 $this->assertTrue( $info->wasRemembered() );
1583 $this->assertSame( array(), $logger->getBuffer() );
1585 // forceHTTPS from session
1586 $this->store
->setSessionMeta( $id, $metadata );
1587 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1588 'provider' => $provider,
1590 'userInfo' => $userInfo
1592 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1593 $this->assertFalse( $info->forceHTTPS() );
1594 $this->assertSame( array(), $logger->getBuffer() );
1596 $this->store
->setSessionMeta( $id, array( 'forceHTTPS' => true ) +
$metadata );
1597 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1598 'provider' => $provider,
1600 'userInfo' => $userInfo
1602 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1603 $this->assertTrue( $info->forceHTTPS() );
1604 $this->assertSame( array(), $logger->getBuffer() );
1606 $this->store
->setSessionMeta( $id, array( 'forceHTTPS' => false ) +
$metadata );
1607 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1608 'provider' => $provider,
1610 'userInfo' => $userInfo,
1611 'forceHTTPS' => true
1613 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1614 $this->assertTrue( $info->forceHTTPS() );
1615 $this->assertSame( array(), $logger->getBuffer() );
1617 // "Persist" flag from session
1618 $this->store
->setSessionMeta( $id, $metadata );
1619 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1620 'provider' => $provider,
1622 'userInfo' => $userInfo
1624 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1625 $this->assertFalse( $info->wasPersisted() );
1626 $this->assertSame( array(), $logger->getBuffer() );
1628 $this->store
->setSessionMeta( $id, array( 'persisted' => true ) +
$metadata );
1629 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1630 'provider' => $provider,
1632 'userInfo' => $userInfo
1634 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1635 $this->assertTrue( $info->wasPersisted() );
1636 $this->assertSame( array(), $logger->getBuffer() );
1638 $this->store
->setSessionMeta( $id, array( 'persisted' => false ) +
$metadata );
1639 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1640 'provider' => $provider,
1642 'userInfo' => $userInfo,
1645 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1646 $this->assertTrue( $info->wasPersisted() );
1647 $this->assertSame( array(), $logger->getBuffer() );
1649 // Provider refreshSessionInfo() returning false
1650 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1651 'provider' => $provider3,
1653 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1654 $this->assertSame( array(), $logger->getBuffer() );
1659 $data = array( 'foo' => 1 );
1660 $this->store
->setSession( $id, array( 'metadata' => $metadata, 'data' => $data ) );
1661 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1662 'provider' => $provider,
1664 'userInfo' => $userInfo
1666 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1667 'SessionCheckInfo' => array( function ( &$reason, $i, $r, $m, $d ) use (
1668 $that, $info, $metadata, $data, $request, &$called
1670 $that->assertSame( $info->getId(), $i->getId() );
1671 $that->assertSame( $info->getProvider(), $i->getProvider() );
1672 $that->assertSame( $info->getUserInfo(), $i->getUserInfo() );
1673 $that->assertSame( $request, $r );
1674 $that->assertEquals( $metadata, $m );
1675 $that->assertEquals( $data, $d );
1680 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1681 $this->assertTrue( $called );
1682 $this->assertSame( array(
1683 array( LogLevel
::WARNING
, 'Session "{session}": Hook aborted' ),
1684 ), $logger->getBuffer() );
1685 $logger->clearBuffer();