3 namespace MediaWiki\Session
;
12 * @covers MediaWiki\Session\SessionManager
14 class SessionManagerTest
extends MediaWikiTestCase
{
16 protected $config, $logger, $store;
18 protected function getManager() {
19 \ObjectCache
::$instances['testSessionStore'] = new TestBagOStuff();
20 $this->config
= new \
HashConfig( array(
21 'LanguageCode' => 'en',
22 'SessionCacheType' => 'testSessionStore',
23 'ObjectCacheSessionExpiry' => 100,
24 'SessionProviders' => array(
25 array( 'class' => 'DummySessionProvider' ),
28 $this->logger
= new \
TestLogger( false, function ( $m ) {
29 return substr( $m, 0, 15 ) === 'SessionBackend ' ?
null : $m;
31 $this->store
= new TestBagOStuff();
33 return new SessionManager( array(
34 'config' => $this->config
,
35 'logger' => $this->logger
,
36 'store' => $this->store
,
40 protected function objectCacheDef( $object ) {
41 return array( 'factory' => function () use ( $object ) {
46 public function testSingleton() {
47 $reset = TestUtils
::setSessionManagerSingleton( null );
49 $singleton = SessionManager
::singleton();
50 $this->assertInstanceOf( 'MediaWiki\\Session\\SessionManager', $singleton );
51 $this->assertSame( $singleton, SessionManager
::singleton() );
54 public function testGetGlobalSession() {
55 $context = \RequestContext
::getMain();
57 if ( !PHPSessionHandler
::isInstalled() ) {
58 PHPSessionHandler
::install( SessionManager
::singleton() );
60 $rProp = new \
ReflectionProperty( 'MediaWiki\\Session\\PHPSessionHandler', 'instance' );
61 $rProp->setAccessible( true );
62 $handler = \TestingAccessWrapper
::newFromObject( $rProp->getValue() );
63 $oldEnable = $handler->enable
;
64 $reset[] = new \
ScopedCallback( function () use ( $handler, $oldEnable ) {
65 if ( $handler->enable
) {
66 session_write_close();
68 $handler->enable
= $oldEnable;
70 $reset[] = TestUtils
::setSessionManagerSingleton( $this->getManager() );
72 $handler->enable
= true;
73 $request = new \
FauxRequest();
74 $context->setRequest( $request );
75 $id = $request->getSession()->getId();
78 $session = SessionManager
::getGlobalSession();
79 $this->assertSame( $id, $session->getId() );
81 session_id( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
82 $session = SessionManager
::getGlobalSession();
83 $this->assertSame( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', $session->getId() );
84 $this->assertSame( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', $request->getSession()->getId() );
86 session_write_close();
87 $handler->enable
= false;
88 $request = new \
FauxRequest();
89 $context->setRequest( $request );
90 $id = $request->getSession()->getId();
93 $session = SessionManager
::getGlobalSession();
94 $this->assertSame( $id, $session->getId() );
96 session_id( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
97 $session = SessionManager
::getGlobalSession();
98 $this->assertSame( $id, $session->getId() );
99 $this->assertSame( $id, $request->getSession()->getId() );
102 public function testConstructor() {
103 $manager = \TestingAccessWrapper
::newFromObject( $this->getManager() );
104 $this->assertSame( $this->config
, $manager->config
);
105 $this->assertSame( $this->logger
, $manager->logger
);
106 $this->assertSame( $this->store
, $manager->store
);
108 $manager = \TestingAccessWrapper
::newFromObject( new SessionManager() );
109 $this->assertSame( \RequestContext
::getMain()->getConfig(), $manager->config
);
111 $manager = \TestingAccessWrapper
::newFromObject( new SessionManager( array(
112 'config' => $this->config
,
114 $this->assertSame( \ObjectCache
::$instances['testSessionStore'], $manager->store
);
117 'config' => '$options[\'config\'] must be an instance of Config',
118 'logger' => '$options[\'logger\'] must be an instance of LoggerInterface',
119 'store' => '$options[\'store\'] must be an instance of BagOStuff',
120 ) as $key => $error ) {
122 new SessionManager( array( $key => new \stdClass
) );
123 $this->fail( 'Expected exception not thrown' );
124 } catch ( \InvalidArgumentException
$ex ) {
125 $this->assertSame( $error, $ex->getMessage() );
130 public function testGetSessionForRequest() {
131 $manager = $this->getManager();
132 $request = new \
FauxRequest();
136 $idEmpty = 'empty-session-------------------';
138 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
140 array( 'provideSessionInfo', 'newSessionInfo', '__toString', 'describe' )
143 $provider1 = $providerBuilder->getMock();
144 $provider1->expects( $this->any() )->method( 'provideSessionInfo' )
145 ->with( $this->identicalTo( $request ) )
146 ->will( $this->returnCallback( function ( $request ) {
147 return $request->info1
;
149 $provider1->expects( $this->any() )->method( 'newSessionInfo' )
150 ->will( $this->returnCallback( function () use ( $idEmpty, $provider1 ) {
151 return new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
152 'provider' => $provider1,
158 $provider1->expects( $this->any() )->method( '__toString' )
159 ->will( $this->returnValue( 'Provider1' ) );
160 $provider1->expects( $this->any() )->method( 'describe' )
161 ->will( $this->returnValue( '#1 sessions' ) );
163 $provider2 = $providerBuilder->getMock();
164 $provider2->expects( $this->any() )->method( 'provideSessionInfo' )
165 ->with( $this->identicalTo( $request ) )
166 ->will( $this->returnCallback( function ( $request ) {
167 return $request->info2
;
169 $provider2->expects( $this->any() )->method( '__toString' )
170 ->will( $this->returnValue( 'Provider2' ) );
171 $provider2->expects( $this->any() )->method( 'describe' )
172 ->will( $this->returnValue( '#2 sessions' ) );
174 $this->config
->set( 'SessionProviders', array(
175 $this->objectCacheDef( $provider1 ),
176 $this->objectCacheDef( $provider2 ),
179 // No provider returns info
180 $request->info1
= null;
181 $request->info2
= null;
182 $session = $manager->getSessionForRequest( $request );
183 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
184 $this->assertSame( $idEmpty, $session->getId() );
185 $this->assertNull( $manager->getPersistedSessionId( $request ) );
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() );
203 $this->assertSame( $id2, $manager->getPersistedSessionId( $request ) );
205 $request->info1
= new SessionInfo( SessionInfo
::MIN_PRIORITY +
2, array(
206 'provider' => $provider1,
207 'id' => ( $id1 = $manager->generateSessionId() ),
211 $request->info2
= new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, array(
212 'provider' => $provider2,
213 'id' => ( $id2 = $manager->generateSessionId() ),
217 $session = $manager->getSessionForRequest( $request );
218 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
219 $this->assertSame( $id1, $session->getId() );
220 $this->assertSame( $id1, $manager->getPersistedSessionId( $request ) );
223 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, array(
224 'provider' => $provider1,
225 'id' => ( $id1 = $manager->generateSessionId() ),
227 'userInfo' => UserInfo
::newAnonymous(),
230 $request->info2
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, array(
231 'provider' => $provider2,
232 'id' => ( $id2 = $manager->generateSessionId() ),
234 'userInfo' => UserInfo
::newAnonymous(),
238 $manager->getSessionForRequest( $request );
239 $this->fail( 'Expcected exception not thrown' );
240 } catch ( \OverFlowException
$ex ) {
241 $this->assertStringStartsWith(
242 'Multiple sessions for this request tied for top priority: ',
245 $this->assertCount( 2, $ex->sessionInfos
);
246 $this->assertContains( $request->info1
, $ex->sessionInfos
);
247 $this->assertContains( $request->info2
, $ex->sessionInfos
);
250 $manager->getPersistedSessionId( $request );
251 $this->fail( 'Expcected exception not thrown' );
252 } catch ( \OverFlowException
$ex ) {
253 $this->assertStringStartsWith(
254 'Multiple sessions for this request tied for top priority: ',
257 $this->assertCount( 2, $ex->sessionInfos
);
258 $this->assertContains( $request->info1
, $ex->sessionInfos
);
259 $this->assertContains( $request->info2
, $ex->sessionInfos
);
263 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, array(
264 'provider' => $provider2,
265 'id' => ( $id1 = $manager->generateSessionId() ),
269 $request->info2
= null;
271 $manager->getSessionForRequest( $request );
272 $this->fail( 'Expcected exception not thrown' );
273 } catch ( \UnexpectedValueException
$ex ) {
275 'Provider1 returned session info for a different provider: ' . $request->info1
,
280 $manager->getPersistedSessionId( $request );
281 $this->fail( 'Expcected exception not thrown' );
282 } catch ( \UnexpectedValueException
$ex ) {
284 'Provider1 returned session info for a different provider: ' . $request->info1
,
289 // Unusable session info
290 $this->logger
->setCollect( true );
291 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, array(
292 'provider' => $provider1,
293 'id' => ( $id1 = $manager->generateSessionId() ),
295 'userInfo' => UserInfo
::newFromName( 'UTSysop', false ),
298 $request->info2
= new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
299 'provider' => $provider2,
300 'id' => ( $id2 = $manager->generateSessionId() ),
304 $session = $manager->getSessionForRequest( $request );
305 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
306 $this->assertSame( $id2, $session->getId() );
307 $this->assertSame( $id2, $manager->getPersistedSessionId( $request ) );
308 $this->logger
->setCollect( false );
310 // Unpersisted session ID
311 $request->info1
= new SessionInfo( SessionInfo
::MAX_PRIORITY
, array(
312 'provider' => $provider1,
313 'id' => ( $id1 = $manager->generateSessionId() ),
314 'persisted' => false,
315 'userInfo' => UserInfo
::newFromName( 'UTSysop', true ),
318 $request->info2
= null;
319 $session = $manager->getSessionForRequest( $request );
320 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
321 $this->assertSame( $id1, $session->getId() );
323 $this->assertTrue( $session->isPersistent(), 'sanity check' );
324 $this->assertNull( $manager->getPersistedSessionId( $request ) );
327 public function testGetSessionById() {
328 $manager = $this->getManager();
331 $manager->getSessionById( 'bad' );
332 $this->fail( 'Expected exception not thrown' );
333 } catch ( \InvalidArgumentException
$ex ) {
334 $this->assertSame( 'Invalid session ID', $ex->getMessage() );
337 // Unknown session ID
338 $id = $manager->generateSessionId();
339 $session = $manager->getSessionById( $id, true );
340 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
341 $this->assertSame( $id, $session->getId() );
343 $id = $manager->generateSessionId();
344 $this->assertNull( $manager->getSessionById( $id, false ) );
346 // Known but unloadable session ID
347 $this->logger
->setCollect( true );
348 $id = $manager->generateSessionId();
349 $this->store
->setSession( $id, array( 'metadata' => array(
350 'userId' => User
::idFromName( 'UTSysop' ),
351 'userToken' => 'bad',
354 $this->assertNull( $manager->getSessionById( $id, true ) );
355 $this->assertNull( $manager->getSessionById( $id, false ) );
356 $this->logger
->setCollect( false );
359 $this->store
->setSession( $id, array() );
360 $session = $manager->getSessionById( $id, false );
361 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
362 $this->assertSame( $id, $session->getId() );
365 public function testGetEmptySession() {
366 $manager = $this->getManager();
367 $pmanager = \TestingAccessWrapper
::newFromObject( $manager );
368 $request = new \
FauxRequest();
370 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
371 ->setMethods( array( 'provideSessionInfo', 'newSessionInfo', '__toString' ) );
377 $provider1 = $providerBuilder->getMock();
378 $provider1->expects( $this->any() )->method( 'provideSessionInfo' )
379 ->will( $this->returnValue( null ) );
380 $provider1->expects( $this->any() )->method( 'newSessionInfo' )
381 ->with( $this->callback( function ( $id ) use ( &$expectId ) {
382 return $id === $expectId;
384 ->will( $this->returnCallback( function () use ( &$info1 ) {
387 $provider1->expects( $this->any() )->method( '__toString' )
388 ->will( $this->returnValue( 'MockProvider1' ) );
390 $provider2 = $providerBuilder->getMock();
391 $provider2->expects( $this->any() )->method( 'provideSessionInfo' )
392 ->will( $this->returnValue( null ) );
393 $provider2->expects( $this->any() )->method( 'newSessionInfo' )
394 ->with( $this->callback( function ( $id ) use ( &$expectId ) {
395 return $id === $expectId;
397 ->will( $this->returnCallback( function () use ( &$info2 ) {
400 $provider1->expects( $this->any() )->method( '__toString' )
401 ->will( $this->returnValue( 'MockProvider2' ) );
403 $this->config
->set( 'SessionProviders', array(
404 $this->objectCacheDef( $provider1 ),
405 $this->objectCacheDef( $provider2 ),
413 $manager->getEmptySession();
414 $this->fail( 'Expected exception not thrown' );
415 } catch ( \UnexpectedValueException
$ex ) {
417 'No provider could provide an empty session!',
424 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
425 'provider' => $provider1,
426 'id' => 'empty---------------------------',
431 $session = $manager->getEmptySession();
432 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
433 $this->assertSame( 'empty---------------------------', $session->getId() );
436 $expectId = 'expected------------------------';
437 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
438 'provider' => $provider1,
444 $session = $pmanager->getEmptySessionInternal( null, $expectId );
445 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
446 $this->assertSame( $expectId, $session->getId() );
449 $expectId = 'expected-----------------------2';
450 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
451 'provider' => $provider1,
452 'id' => "un$expectId",
458 $pmanager->getEmptySessionInternal( null, $expectId );
459 $this->fail( 'Expected exception not thrown' );
460 } catch ( \UnexpectedValueException
$ex ) {
462 'MockProvider1 returned empty session info with a wrong id: ' .
463 "un$expectId != $expectId",
469 $expectId = 'expected-----------------------2';
470 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
471 'provider' => $provider1,
477 $pmanager->getEmptySessionInternal( null, $expectId );
478 $this->fail( 'Expected exception not thrown' );
479 } catch ( \UnexpectedValueException
$ex ) {
481 'MockProvider1 returned empty session info with id flagged unsafe',
488 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
489 'provider' => $provider2,
490 'id' => 'empty---------------------------',
496 $manager->getEmptySession();
497 $this->fail( 'Expected exception not thrown' );
498 } catch ( \UnexpectedValueException
$ex ) {
500 'MockProvider1 returned an empty session info for a different provider: ' . $info1,
505 // Highest priority wins
507 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, array(
508 'provider' => $provider1,
509 'id' => 'empty1--------------------------',
513 $info2 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
514 'provider' => $provider2,
515 'id' => 'empty2--------------------------',
519 $session = $manager->getEmptySession();
520 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
521 $this->assertSame( 'empty1--------------------------', $session->getId() );
524 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, array(
525 'provider' => $provider1,
526 'id' => 'empty1--------------------------',
530 $info2 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
2, array(
531 'provider' => $provider2,
532 'id' => 'empty2--------------------------',
536 $session = $manager->getEmptySession();
537 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
538 $this->assertSame( 'empty2--------------------------', $session->getId() );
540 // Tied priorities throw an exception
542 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
543 'provider' => $provider1,
544 'id' => 'empty1--------------------------',
546 'userInfo' => UserInfo
::newAnonymous(),
549 $info2 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
550 'provider' => $provider2,
551 'id' => 'empty2--------------------------',
553 'userInfo' => UserInfo
::newAnonymous(),
557 $manager->getEmptySession();
558 $this->fail( 'Expected exception not thrown' );
559 } catch ( \UnexpectedValueException
$ex ) {
560 $this->assertStringStartsWith(
561 'Multiple empty sessions tied for top priority: ',
568 $pmanager->getEmptySessionInternal( null, 'bad' );
569 $this->fail( 'Expected exception not thrown' );
570 } catch ( \InvalidArgumentException
$ex ) {
571 $this->assertSame( 'Invalid session ID', $ex->getMessage() );
574 // Session already exists
575 $expectId = 'expected-----------------------3';
576 $this->store
->setSessionMeta( $expectId, array(
577 'provider' => 'MockProvider2',
583 $pmanager->getEmptySessionInternal( null, $expectId );
584 $this->fail( 'Expected exception not thrown' );
585 } catch ( \InvalidArgumentException
$ex ) {
586 $this->assertSame( 'Session ID already exists', $ex->getMessage() );
590 public function testGetVaryHeaders() {
591 $manager = $this->getManager();
593 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
594 ->setMethods( array( 'getVaryHeaders', '__toString' ) );
596 $provider1 = $providerBuilder->getMock();
597 $provider1->expects( $this->once() )->method( 'getVaryHeaders' )
598 ->will( $this->returnValue( array(
600 'Bar' => array( 'X', 'Bar1' ),
603 $provider1->expects( $this->any() )->method( '__toString' )
604 ->will( $this->returnValue( 'MockProvider1' ) );
606 $provider2 = $providerBuilder->getMock();
607 $provider2->expects( $this->once() )->method( 'getVaryHeaders' )
608 ->will( $this->returnValue( array(
610 'Bar' => array( 'X', 'Bar2' ),
611 'Quux' => array( 'Quux' ),
613 $provider2->expects( $this->any() )->method( '__toString' )
614 ->will( $this->returnValue( 'MockProvider2' ) );
616 $this->config
->set( 'SessionProviders', array(
617 $this->objectCacheDef( $provider1 ),
618 $this->objectCacheDef( $provider2 ),
623 'Bar' => array( 'X', 'Bar1', 3 => 'Bar2' ),
624 'Quux' => array( 'Quux' ),
626 'Quux' => array( 'Quux' ),
629 $this->assertEquals( $expect, $manager->getVaryHeaders() );
631 // Again, to ensure it's cached
632 $this->assertEquals( $expect, $manager->getVaryHeaders() );
635 public function testGetVaryCookies() {
636 $manager = $this->getManager();
638 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
639 ->setMethods( array( 'getVaryCookies', '__toString' ) );
641 $provider1 = $providerBuilder->getMock();
642 $provider1->expects( $this->once() )->method( 'getVaryCookies' )
643 ->will( $this->returnValue( array( 'Foo', 'Bar' ) ) );
644 $provider1->expects( $this->any() )->method( '__toString' )
645 ->will( $this->returnValue( 'MockProvider1' ) );
647 $provider2 = $providerBuilder->getMock();
648 $provider2->expects( $this->once() )->method( 'getVaryCookies' )
649 ->will( $this->returnValue( array( 'Foo', 'Baz' ) ) );
650 $provider2->expects( $this->any() )->method( '__toString' )
651 ->will( $this->returnValue( 'MockProvider2' ) );
653 $this->config
->set( 'SessionProviders', array(
654 $this->objectCacheDef( $provider1 ),
655 $this->objectCacheDef( $provider2 ),
658 $expect = array( 'Foo', 'Bar', 'Baz' );
660 $this->assertEquals( $expect, $manager->getVaryCookies() );
662 // Again, to ensure it's cached
663 $this->assertEquals( $expect, $manager->getVaryCookies() );
666 public function testGetProviders() {
667 $realManager = $this->getManager();
668 $manager = \TestingAccessWrapper
::newFromObject( $realManager );
670 $this->config
->set( 'SessionProviders', array(
671 array( 'class' => 'DummySessionProvider' ),
673 $providers = $manager->getProviders();
674 $this->assertArrayHasKey( 'DummySessionProvider', $providers );
675 $provider = \TestingAccessWrapper
::newFromObject( $providers['DummySessionProvider'] );
676 $this->assertSame( $manager->logger
, $provider->logger
);
677 $this->assertSame( $manager->config
, $provider->config
);
678 $this->assertSame( $realManager, $provider->getManager() );
680 $this->config
->set( 'SessionProviders', array(
681 array( 'class' => 'DummySessionProvider' ),
682 array( 'class' => 'DummySessionProvider' ),
684 $manager->sessionProviders
= null;
686 $manager->getProviders();
687 $this->fail( 'Expected exception not thrown' );
688 } catch ( \UnexpectedValueException
$ex ) {
690 'Duplicate provider name "DummySessionProvider"',
696 public function testShutdown() {
697 $manager = \TestingAccessWrapper
::newFromObject( $this->getManager() );
698 $manager->setLogger( new \Psr\Log\
NullLogger() );
700 $mock = $this->getMock( 'stdClass', array( 'save' ) );
701 $mock->expects( $this->once() )->method( 'save' );
703 $manager->allSessionBackends
= array( $mock );
704 $manager->shutdown();
707 public function testGetSessionFromInfo() {
708 $manager = \TestingAccessWrapper
::newFromObject( $this->getManager() );
709 $request = new \
FauxRequest();
711 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
713 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
714 'provider' => $manager->getProvider( 'DummySessionProvider' ),
717 'userInfo' => UserInfo
::newFromName( 'UTSysop', true ),
720 \TestingAccessWrapper
::newFromObject( $info )->idIsSafe
= true;
721 $session1 = \TestingAccessWrapper
::newFromObject(
722 $manager->getSessionFromInfo( $info, $request )
724 $session2 = \TestingAccessWrapper
::newFromObject(
725 $manager->getSessionFromInfo( $info, $request )
728 $this->assertSame( $session1->backend
, $session2->backend
);
729 $this->assertNotEquals( $session1->index
, $session2->index
);
730 $this->assertSame( $session1->getSessionId(), $session2->getSessionId() );
731 $this->assertSame( $id, $session1->getId() );
733 \TestingAccessWrapper
::newFromObject( $info )->idIsSafe
= false;
734 $session3 = $manager->getSessionFromInfo( $info, $request );
735 $this->assertNotSame( $id, $session3->getId() );
738 public function testBackendRegistration() {
739 $manager = $this->getManager();
741 $session = $manager->getSessionForRequest( new \FauxRequest
);
742 $backend = \TestingAccessWrapper
::newFromObject( $session )->backend
;
743 $sessionId = $session->getSessionId();
744 $id = (string)$sessionId;
746 $this->assertSame( $sessionId, $manager->getSessionById( $id, true )->getSessionId() );
748 $manager->changeBackendId( $backend );
749 $this->assertSame( $sessionId, $session->getSessionId() );
750 $this->assertNotEquals( $id, (string)$sessionId );
751 $id = (string)$sessionId;
753 $this->assertSame( $sessionId, $manager->getSessionById( $id, true )->getSessionId() );
755 // Destruction of the session here causes the backend to be deregistered
759 $manager->changeBackendId( $backend );
760 $this->fail( 'Expected exception not thrown' );
761 } catch ( \InvalidArgumentException
$ex ) {
763 'Backend was not registered with this SessionManager', $ex->getMessage()
768 $manager->deregisterSessionBackend( $backend );
769 $this->fail( 'Expected exception not thrown' );
770 } catch ( \InvalidArgumentException
$ex ) {
772 'Backend was not registered with this SessionManager', $ex->getMessage()
776 $session = $manager->getSessionById( $id, true );
777 $this->assertSame( $sessionId, $session->getSessionId() );
780 public function testGenerateSessionId() {
781 $manager = $this->getManager();
783 $id = $manager->generateSessionId();
784 $this->assertTrue( SessionManager
::validateSessionId( $id ), "Generated ID: $id" );
787 public function testAutoCreateUser() {
788 global $wgGroupPermissions;
792 \ObjectCache
::$instances[__METHOD__
] = new \
HashBagOStuff();
793 $this->setMwGlobals( array( 'wgMainCacheType' => __METHOD__
) );
795 $this->stashMwGlobals( array( 'wgGroupPermissions' ) );
796 $wgGroupPermissions['*']['createaccount'] = true;
797 $wgGroupPermissions['*']['autocreateaccount'] = false;
799 // Replace the global singleton with one configured for testing
800 $manager = $this->getManager();
801 $reset = TestUtils
::setSessionManagerSingleton( $manager );
803 $logger = new \
TestLogger( true, function ( $m ) {
804 if ( substr( $m, 0, 15 ) === 'SessionBackend ' ) {
808 $m = str_replace( 'MediaWiki\Session\SessionManager::autoCreateUser: ', '', $m );
809 $m = preg_replace( '/ - from: .*$/', ' - from: XXX', $m );
812 $manager->setLogger( $logger );
814 $session = SessionManager
::getGlobalSession();
816 // Can't create an already-existing user
817 $user = User
::newFromName( 'UTSysop' );
818 $id = $user->getId();
819 $this->assertFalse( $manager->autoCreateUser( $user ) );
820 $this->assertSame( $id, $user->getId() );
821 $this->assertSame( 'UTSysop', $user->getName() );
822 $this->assertSame( array(), $logger->getBuffer() );
823 $logger->clearBuffer();
825 // Sanity check that creation works at all
826 $user = User
::newFromName( 'UTSessionAutoCreate1' );
827 $this->assertSame( 0, $user->getId(), 'sanity check' );
828 $this->assertTrue( $manager->autoCreateUser( $user ) );
829 $this->assertNotEquals( 0, $user->getId() );
830 $this->assertSame( 'UTSessionAutoCreate1', $user->getName() );
832 $user->getId(), User
::idFromName( 'UTSessionAutoCreate1', User
::READ_LATEST
)
834 $this->assertSame( array(
835 array( LogLevel
::INFO
, 'creating new user (UTSessionAutoCreate1) - from: XXX' ),
836 ), $logger->getBuffer() );
837 $logger->clearBuffer();
839 // Check lack of permissions
840 $wgGroupPermissions['*']['createaccount'] = false;
841 $wgGroupPermissions['*']['autocreateaccount'] = false;
842 $user = User
::newFromName( 'UTDoesNotExist' );
843 $this->assertFalse( $manager->autoCreateUser( $user ) );
844 $this->assertSame( 0, $user->getId() );
845 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
846 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
848 $this->assertSame( array(
849 array( LogLevel
::DEBUG
, 'user is blocked from this wiki, blacklisting' ),
850 ), $logger->getBuffer() );
851 $logger->clearBuffer();
853 // Check other permission
854 $wgGroupPermissions['*']['createaccount'] = false;
855 $wgGroupPermissions['*']['autocreateaccount'] = true;
856 $user = User
::newFromName( 'UTSessionAutoCreate2' );
857 $this->assertSame( 0, $user->getId(), 'sanity check' );
858 $this->assertTrue( $manager->autoCreateUser( $user ) );
859 $this->assertNotEquals( 0, $user->getId() );
860 $this->assertSame( 'UTSessionAutoCreate2', $user->getName() );
862 $user->getId(), User
::idFromName( 'UTSessionAutoCreate2', User
::READ_LATEST
)
864 $this->assertSame( array(
865 array( LogLevel
::INFO
, 'creating new user (UTSessionAutoCreate2) - from: XXX' ),
866 ), $logger->getBuffer() );
867 $logger->clearBuffer();
869 // Test account-creation block
871 $block = new \
Block( array(
872 'address' => $anon->getName(),
874 'reason' => __METHOD__
,
875 'expiry' => time() +
100500,
876 'createAccount' => true,
879 $this->assertInstanceOf( 'Block', $anon->isBlockedFromCreateAccount(), 'sanity check' );
880 $reset2 = new \
ScopedCallback( array( $block, 'delete' ) );
881 $user = User
::newFromName( 'UTDoesNotExist' );
882 $this->assertFalse( $manager->autoCreateUser( $user ) );
883 $this->assertSame( 0, $user->getId() );
884 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
885 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
886 \ScopedCallback
::consume( $reset2 );
888 $this->assertSame( array(
889 array( LogLevel
::DEBUG
, 'user is blocked from this wiki, blacklisting' ),
890 ), $logger->getBuffer() );
891 $logger->clearBuffer();
893 // Sanity check that creation still works
894 $user = User
::newFromName( 'UTSessionAutoCreate3' );
895 $this->assertSame( 0, $user->getId(), 'sanity check' );
896 $this->assertTrue( $manager->autoCreateUser( $user ) );
897 $this->assertNotEquals( 0, $user->getId() );
898 $this->assertSame( 'UTSessionAutoCreate3', $user->getName() );
900 $user->getId(), User
::idFromName( 'UTSessionAutoCreate3', User
::READ_LATEST
)
902 $this->assertSame( array(
903 array( LogLevel
::INFO
, 'creating new user (UTSessionAutoCreate3) - from: XXX' ),
904 ), $logger->getBuffer() );
905 $logger->clearBuffer();
907 // Test prevention by AuthPlugin
909 $oldWgAuth = $wgAuth;
910 $mockWgAuth = $this->getMock( 'AuthPlugin', array( 'autoCreate' ) );
911 $mockWgAuth->expects( $this->once() )->method( 'autoCreate' )
912 ->will( $this->returnValue( false ) );
913 $this->setMwGlobals( array(
914 'wgAuth' => $mockWgAuth,
916 $user = User
::newFromName( 'UTDoesNotExist' );
917 $this->assertFalse( $manager->autoCreateUser( $user ) );
918 $this->assertSame( 0, $user->getId() );
919 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
920 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
921 $this->setMwGlobals( array(
922 'wgAuth' => $oldWgAuth,
925 $this->assertSame( array(
926 array( LogLevel
::DEBUG
, 'denied by AuthPlugin' ),
927 ), $logger->getBuffer() );
928 $logger->clearBuffer();
930 // Test prevention by wfReadOnly()
931 $this->setMwGlobals( array(
932 'wgReadOnly' => 'Because',
934 $user = User
::newFromName( 'UTDoesNotExist' );
935 $this->assertFalse( $manager->autoCreateUser( $user ) );
936 $this->assertSame( 0, $user->getId() );
937 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
938 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
939 $this->setMwGlobals( array(
940 'wgReadOnly' => false,
943 $this->assertSame( array(
944 array( LogLevel
::DEBUG
, 'denied by wfReadOnly()' ),
945 ), $logger->getBuffer() );
946 $logger->clearBuffer();
948 // Test prevention by a previous session
949 $session->set( 'MWSession::AutoCreateBlacklist', 'test' );
950 $user = User
::newFromName( 'UTDoesNotExist' );
951 $this->assertFalse( $manager->autoCreateUser( $user ) );
952 $this->assertSame( 0, $user->getId() );
953 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
954 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
956 $this->assertSame( array(
957 array( LogLevel
::DEBUG
, 'blacklisted in session (test)' ),
958 ), $logger->getBuffer() );
959 $logger->clearBuffer();
961 // Test uncreatable name
962 $user = User
::newFromName( 'UTDoesNotExist@' );
963 $this->assertFalse( $manager->autoCreateUser( $user ) );
964 $this->assertSame( 0, $user->getId() );
965 $this->assertNotSame( 'UTDoesNotExist@', $user->getName() );
966 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
968 $this->assertSame( array(
969 array( LogLevel
::DEBUG
, 'Invalid username, blacklisting' ),
970 ), $logger->getBuffer() );
971 $logger->clearBuffer();
973 // Test AbortAutoAccount hook
974 $mock = $this->getMock( __CLASS__
, array( 'onAbortAutoAccount' ) );
975 $mock->expects( $this->once() )->method( 'onAbortAutoAccount' )
976 ->will( $this->returnCallback( function ( User
$user, &$msg ) {
980 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array( $mock ) ) );
981 $user = User
::newFromName( 'UTDoesNotExist' );
982 $this->assertFalse( $manager->autoCreateUser( $user ) );
983 $this->assertSame( 0, $user->getId() );
984 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
985 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
986 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array() ) );
988 $this->assertSame( array(
989 array( LogLevel
::DEBUG
, 'denied by hook: No way!' ),
990 ), $logger->getBuffer() );
991 $logger->clearBuffer();
993 // Test AbortAutoAccount hook screwing up the name
994 $mock = $this->getMock( 'stdClass', array( 'onAbortAutoAccount' ) );
995 $mock->expects( $this->once() )->method( 'onAbortAutoAccount' )
996 ->will( $this->returnCallback( function ( User
$user ) {
997 $user->setName( 'UTDoesNotExistEither' );
999 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array( $mock ) ) );
1001 $user = User
::newFromName( 'UTDoesNotExist' );
1002 $manager->autoCreateUser( $user );
1003 $this->fail( 'Expected exception not thrown' );
1004 } catch ( \UnexpectedValueException
$ex ) {
1006 'AbortAutoAccount hook tried to change the user name',
1010 $this->assertSame( 0, $user->getId() );
1011 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
1012 $this->assertNotSame( 'UTDoesNotExistEither', $user->getName() );
1013 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
1014 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExistEither', User
::READ_LATEST
) );
1015 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array() ) );
1017 $this->assertSame( array(), $logger->getBuffer() );
1018 $logger->clearBuffer();
1020 // Test for "exception backoff"
1021 $user = User
::newFromName( 'UTDoesNotExist' );
1022 $cache = \ObjectCache
::getLocalClusterInstance();
1023 $backoffKey = wfMemcKey( 'MWSession', 'autocreate-failed', md5( $user->getName() ) );
1024 $cache->set( $backoffKey, 1, 60 * 10 );
1025 $this->assertFalse( $manager->autoCreateUser( $user ) );
1026 $this->assertSame( 0, $user->getId() );
1027 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
1028 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
1029 $cache->delete( $backoffKey );
1031 $this->assertSame( array(
1032 array( LogLevel
::DEBUG
, 'denied by prior creation attempt failures' ),
1033 ), $logger->getBuffer() );
1034 $logger->clearBuffer();
1036 // Sanity check that creation still works, and test completion hook
1037 $cb = $this->callback( function ( User
$user ) use ( $that ) {
1038 $that->assertNotEquals( 0, $user->getId() );
1039 $that->assertSame( 'UTSessionAutoCreate4', $user->getName() );
1040 $that->assertEquals(
1041 $user->getId(), User
::idFromName( 'UTSessionAutoCreate4', User
::READ_LATEST
)
1045 $mock = $this->getMock( 'stdClass',
1046 array( 'onAuthPluginAutoCreate', 'onLocalUserCreated' ) );
1047 $mock->expects( $this->once() )->method( 'onAuthPluginAutoCreate' )
1049 $mock->expects( $this->once() )->method( 'onLocalUserCreated' )
1050 ->with( $cb, $this->identicalTo( true ) );
1051 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1052 'AuthPluginAutoCreate' => array( $mock ),
1053 'LocalUserCreated' => array( $mock ),
1055 $user = User
::newFromName( 'UTSessionAutoCreate4' );
1056 $this->assertSame( 0, $user->getId(), 'sanity check' );
1057 $this->assertTrue( $manager->autoCreateUser( $user ) );
1058 $this->assertNotEquals( 0, $user->getId() );
1059 $this->assertSame( 'UTSessionAutoCreate4', $user->getName() );
1060 $this->assertEquals(
1062 User
::idFromName( 'UTSessionAutoCreate4', User
::READ_LATEST
)
1064 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1065 'AuthPluginAutoCreate' => array(),
1066 'LocalUserCreated' => array(),
1068 $this->assertSame( array(
1069 array( LogLevel
::INFO
, 'creating new user (UTSessionAutoCreate4) - from: XXX' ),
1070 ), $logger->getBuffer() );
1071 $logger->clearBuffer();
1074 public function onAbortAutoAccount( User
$user, &$msg ) {
1077 public function testPreventSessionsForUser() {
1078 $manager = $this->getManager();
1080 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
1081 ->setMethods( array( 'preventSessionsForUser', '__toString' ) );
1083 $provider1 = $providerBuilder->getMock();
1084 $provider1->expects( $this->once() )->method( 'preventSessionsForUser' )
1085 ->with( $this->equalTo( 'UTSysop' ) );
1086 $provider1->expects( $this->any() )->method( '__toString' )
1087 ->will( $this->returnValue( 'MockProvider1' ) );
1089 $this->config
->set( 'SessionProviders', array(
1090 $this->objectCacheDef( $provider1 ),
1093 $user = User
::newFromName( 'UTSysop' );
1094 $token = $user->getToken( true );
1096 $this->assertFalse( $manager->isUserSessionPrevented( 'UTSysop' ) );
1097 $manager->preventSessionsForUser( 'UTSysop' );
1098 $this->assertNotEquals( $token, User
::newFromName( 'UTSysop' )->getToken() );
1099 $this->assertTrue( $manager->isUserSessionPrevented( 'UTSysop' ) );
1102 public function testLoadSessionInfoFromStore() {
1103 $manager = $this->getManager();
1104 $logger = new \
TestLogger( true, function ( $m ) {
1105 return preg_replace(
1106 '/^Session \[\d+\]\w+<(?:null|anon|[+-]:\d+:\w+)>\w+: /', 'Session X: ', $m
1109 $manager->setLogger( $logger );
1110 $request = new \
FauxRequest();
1112 // TestingAccessWrapper can't handle methods with reference arguments, sigh.
1113 $rClass = new \
ReflectionClass( $manager );
1114 $rMethod = $rClass->getMethod( 'loadSessionInfoFromStore' );
1115 $rMethod->setAccessible( true );
1116 $loadSessionInfoFromStore = function ( &$info ) use ( $rMethod, $manager, $request ) {
1117 return $rMethod->invokeArgs( $manager, array( &$info, $request ) );
1120 $userInfo = UserInfo
::newFromName( 'UTSysop', true );
1121 $unverifiedUserInfo = UserInfo
::newFromName( 'UTSysop', false );
1123 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
1125 'userId' => $userInfo->getId(),
1126 'userName' => $userInfo->getName(),
1127 'userToken' => $userInfo->getToken( true ),
1128 'provider' => 'Mock',
1131 $builder = $this->getMockBuilder( 'MediaWiki\\Session\\SessionProvider' )
1132 ->setMethods( array( '__toString', 'mergeMetadata', 'refreshSessionInfo' ) );
1134 $provider = $builder->getMockForAbstractClass();
1135 $provider->setManager( $manager );
1136 $provider->expects( $this->any() )->method( 'persistsSessionId' )
1137 ->will( $this->returnValue( true ) );
1138 $provider->expects( $this->any() )->method( 'canChangeUser' )
1139 ->will( $this->returnValue( true ) );
1140 $provider->expects( $this->any() )->method( 'refreshSessionInfo' )
1141 ->will( $this->returnValue( true ) );
1142 $provider->expects( $this->any() )->method( '__toString' )
1143 ->will( $this->returnValue( 'Mock' ) );
1144 $provider->expects( $this->any() )->method( 'mergeMetadata' )
1145 ->will( $this->returnCallback( function ( $a, $b ) {
1146 if ( $b === array( 'Throw' ) ) {
1147 throw new \
UnexpectedValueException( 'no merge!' );
1149 return array( 'Merged' );
1152 $provider2 = $builder->getMockForAbstractClass();
1153 $provider2->setManager( $manager );
1154 $provider2->expects( $this->any() )->method( 'persistsSessionId' )
1155 ->will( $this->returnValue( false ) );
1156 $provider2->expects( $this->any() )->method( 'canChangeUser' )
1157 ->will( $this->returnValue( false ) );
1158 $provider2->expects( $this->any() )->method( '__toString' )
1159 ->will( $this->returnValue( 'Mock2' ) );
1160 $provider2->expects( $this->any() )->method( 'refreshSessionInfo' )
1161 ->will( $this->returnCallback( function ( $info, $request, &$metadata ) {
1162 $metadata['changed'] = true;
1166 $provider3 = $builder->getMockForAbstractClass();
1167 $provider3->setManager( $manager );
1168 $provider3->expects( $this->any() )->method( 'persistsSessionId' )
1169 ->will( $this->returnValue( true ) );
1170 $provider3->expects( $this->any() )->method( 'canChangeUser' )
1171 ->will( $this->returnValue( true ) );
1172 $provider3->expects( $this->once() )->method( 'refreshSessionInfo' )
1173 ->will( $this->returnValue( false ) );
1174 $provider3->expects( $this->any() )->method( '__toString' )
1175 ->will( $this->returnValue( 'Mock3' ) );
1177 \TestingAccessWrapper
::newFromObject( $manager )->sessionProviders
= array(
1178 (string)$provider => $provider,
1179 (string)$provider2 => $provider2,
1180 (string)$provider3 => $provider3,
1183 // No metadata, basic usage
1184 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1185 'provider' => $provider,
1187 'userInfo' => $userInfo
1189 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1190 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1191 $this->assertFalse( $info->isIdSafe() );
1192 $this->assertSame( array(), $logger->getBuffer() );
1194 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1195 'provider' => $provider,
1196 'userInfo' => $userInfo
1198 $this->assertTrue( $info->isIdSafe(), 'sanity check' );
1199 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1200 $this->assertTrue( $info->isIdSafe() );
1201 $this->assertSame( array(), $logger->getBuffer() );
1203 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1204 'provider' => $provider2,
1206 'userInfo' => $userInfo
1208 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1209 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1210 $this->assertTrue( $info->isIdSafe() );
1211 $this->assertSame( array(), $logger->getBuffer() );
1213 // Unverified user, no metadata
1214 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1215 'provider' => $provider,
1217 'userInfo' => $unverifiedUserInfo
1219 $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
1220 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1221 $this->assertSame( array(
1222 array( LogLevel
::WARNING
, 'Session X: Unverified user provided and no metadata to auth it' )
1223 ), $logger->getBuffer() );
1224 $logger->clearBuffer();
1226 // No metadata, missing data
1227 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1229 'userInfo' => $userInfo
1231 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1232 $this->assertSame( array(
1233 array( LogLevel
::WARNING
, 'Session X: Null provider and no metadata' ),
1234 ), $logger->getBuffer() );
1235 $logger->clearBuffer();
1237 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1238 'provider' => $provider,
1241 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1242 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1243 $this->assertInstanceOf( 'MediaWiki\\Session\\UserInfo', $info->getUserInfo() );
1244 $this->assertTrue( $info->getUserInfo()->isVerified() );
1245 $this->assertTrue( $info->getUserInfo()->isAnon() );
1246 $this->assertFalse( $info->isIdSafe() );
1247 $this->assertSame( array(), $logger->getBuffer() );
1249 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1250 'provider' => $provider2,
1253 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1254 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1255 $this->assertSame( array(
1256 array( LogLevel
::INFO
, 'Session X: No user provided and provider cannot set user' )
1257 ), $logger->getBuffer() );
1258 $logger->clearBuffer();
1260 // Incomplete/bad metadata
1261 $this->store
->setRawSession( $id, true );
1262 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1263 $this->assertSame( array(
1264 array( LogLevel
::WARNING
, 'Session X: Bad data' ),
1265 ), $logger->getBuffer() );
1266 $logger->clearBuffer();
1268 $this->store
->setRawSession( $id, array( 'data' => array() ) );
1269 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1270 $this->assertSame( array(
1271 array( LogLevel
::WARNING
, 'Session X: Bad data structure' ),
1272 ), $logger->getBuffer() );
1273 $logger->clearBuffer();
1275 $this->store
->deleteSession( $id );
1276 $this->store
->setRawSession( $id, array( 'metadata' => $metadata ) );
1277 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1278 $this->assertSame( array(
1279 array( LogLevel
::WARNING
, 'Session X: Bad data structure' ),
1280 ), $logger->getBuffer() );
1281 $logger->clearBuffer();
1283 $this->store
->setRawSession( $id, array( 'metadata' => $metadata, 'data' => true ) );
1284 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1285 $this->assertSame( array(
1286 array( LogLevel
::WARNING
, 'Session X: Bad data structure' ),
1287 ), $logger->getBuffer() );
1288 $logger->clearBuffer();
1290 $this->store
->setRawSession( $id, array( 'metadata' => true, 'data' => array() ) );
1291 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1292 $this->assertSame( array(
1293 array( LogLevel
::WARNING
, 'Session X: Bad data structure' ),
1294 ), $logger->getBuffer() );
1295 $logger->clearBuffer();
1297 foreach ( $metadata as $key => $dummy ) {
1299 unset( $tmp[$key] );
1300 $this->store
->setRawSession( $id, array( 'metadata' => $tmp, 'data' => array() ) );
1301 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1302 $this->assertSame( array(
1303 array( LogLevel
::WARNING
, 'Session X: Bad metadata' ),
1304 ), $logger->getBuffer() );
1305 $logger->clearBuffer();
1308 // Basic usage with metadata
1309 $this->store
->setRawSession( $id, array( 'metadata' => $metadata, 'data' => array() ) );
1310 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1311 'provider' => $provider,
1313 'userInfo' => $userInfo
1315 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1316 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1317 $this->assertTrue( $info->isIdSafe() );
1318 $this->assertSame( array(), $logger->getBuffer() );
1320 // Mismatched provider
1321 $this->store
->setSessionMeta( $id, array( 'provider' => 'Bad' ) +
$metadata );
1322 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1323 'provider' => $provider,
1325 'userInfo' => $userInfo
1327 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1328 $this->assertSame( array(
1329 array( LogLevel
::WARNING
, 'Session X: Wrong provider, Bad !== Mock' ),
1330 ), $logger->getBuffer() );
1331 $logger->clearBuffer();
1334 $this->store
->setSessionMeta( $id, array( 'provider' => 'Bad' ) +
$metadata );
1335 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1337 'userInfo' => $userInfo
1339 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1340 $this->assertSame( array(
1341 array( LogLevel
::WARNING
, 'Session X: Unknown provider, Bad' ),
1342 ), $logger->getBuffer() );
1343 $logger->clearBuffer();
1346 $this->store
->setSessionMeta( $id, $metadata );
1347 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1349 'userInfo' => $userInfo
1351 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1352 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1353 $this->assertTrue( $info->isIdSafe() );
1354 $this->assertSame( array(), $logger->getBuffer() );
1356 // Bad user metadata
1357 $this->store
->setSessionMeta( $id, array( 'userId' => -1, 'userToken' => null ) +
$metadata );
1358 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1359 'provider' => $provider,
1362 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1363 $this->assertSame( array(
1364 array( LogLevel
::ERROR
, 'Session X: Invalid ID' ),
1365 ), $logger->getBuffer() );
1366 $logger->clearBuffer();
1368 $this->store
->setSessionMeta(
1369 $id, array( 'userId' => 0, 'userName' => '<X>', 'userToken' => null ) +
$metadata
1371 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1372 'provider' => $provider,
1375 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1376 $this->assertSame( array(
1377 array( LogLevel
::ERROR
, 'Session X: Invalid user name' ),
1378 ), $logger->getBuffer() );
1379 $logger->clearBuffer();
1381 // Mismatched user by ID
1382 $this->store
->setSessionMeta(
1383 $id, array( 'userId' => $userInfo->getId() +
1, 'userToken' => null ) +
$metadata
1385 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1386 'provider' => $provider,
1388 'userInfo' => $userInfo
1390 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1391 $this->assertSame( array(
1392 array( LogLevel
::WARNING
, 'Session X: User ID mismatch, 2 !== 1' ),
1393 ), $logger->getBuffer() );
1394 $logger->clearBuffer();
1396 // Mismatched user by name
1397 $this->store
->setSessionMeta(
1398 $id, array( 'userId' => 0, 'userName' => 'X', 'userToken' => null ) +
$metadata
1400 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1401 'provider' => $provider,
1403 'userInfo' => $userInfo
1405 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1406 $this->assertSame( array(
1407 array( LogLevel
::WARNING
, 'Session X: User name mismatch, X !== UTSysop' ),
1408 ), $logger->getBuffer() );
1409 $logger->clearBuffer();
1411 // ID matches, name doesn't
1412 $this->store
->setSessionMeta(
1413 $id, array( 'userId' => $userInfo->getId(), 'userName' => 'X', 'userToken' => null ) +
$metadata
1415 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1416 'provider' => $provider,
1418 'userInfo' => $userInfo
1420 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1421 $this->assertSame( array(
1423 LogLevel
::WARNING
, 'Session X: User ID matched but name didn\'t (rename?), X !== UTSysop'
1425 ), $logger->getBuffer() );
1426 $logger->clearBuffer();
1428 // Mismatched anon user
1429 $this->store
->setSessionMeta(
1430 $id, array( 'userId' => 0, 'userName' => null, 'userToken' => null ) +
$metadata
1432 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1433 'provider' => $provider,
1435 'userInfo' => $userInfo
1437 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1438 $this->assertSame( array(
1440 LogLevel
::WARNING
, 'Session X: Metadata has an anonymous user, but a non-anon user was provided'
1442 ), $logger->getBuffer() );
1443 $logger->clearBuffer();
1445 // Lookup user by ID
1446 $this->store
->setSessionMeta( $id, array( 'userToken' => null ) +
$metadata );
1447 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1448 'provider' => $provider,
1451 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1452 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1453 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1454 $this->assertTrue( $info->isIdSafe() );
1455 $this->assertSame( array(), $logger->getBuffer() );
1457 // Lookup user by name
1458 $this->store
->setSessionMeta(
1459 $id, array( 'userId' => 0, 'userName' => 'UTSysop', 'userToken' => null ) +
$metadata
1461 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1462 'provider' => $provider,
1465 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1466 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1467 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1468 $this->assertTrue( $info->isIdSafe() );
1469 $this->assertSame( array(), $logger->getBuffer() );
1471 // Lookup anonymous user
1472 $this->store
->setSessionMeta(
1473 $id, array( 'userId' => 0, 'userName' => null, 'userToken' => null ) +
$metadata
1475 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1476 'provider' => $provider,
1479 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1480 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1481 $this->assertTrue( $info->getUserInfo()->isAnon() );
1482 $this->assertTrue( $info->isIdSafe() );
1483 $this->assertSame( array(), $logger->getBuffer() );
1485 // Unverified user with metadata
1486 $this->store
->setSessionMeta( $id, $metadata );
1487 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1488 'provider' => $provider,
1490 'userInfo' => $unverifiedUserInfo
1492 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1493 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1494 $this->assertTrue( $info->getUserInfo()->isVerified() );
1495 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1496 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1497 $this->assertTrue( $info->isIdSafe() );
1498 $this->assertSame( array(), $logger->getBuffer() );
1500 // Unverified user with metadata
1501 $this->store
->setSessionMeta( $id, $metadata );
1502 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1503 'provider' => $provider,
1505 'userInfo' => $unverifiedUserInfo
1507 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1508 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1509 $this->assertTrue( $info->getUserInfo()->isVerified() );
1510 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1511 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1512 $this->assertTrue( $info->isIdSafe() );
1513 $this->assertSame( array(), $logger->getBuffer() );
1516 $this->store
->setSessionMeta( $id, array( 'userToken' => 'Bad' ) +
$metadata );
1517 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1518 'provider' => $provider,
1520 'userInfo' => $userInfo
1522 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1523 $this->assertSame( array(
1524 array( LogLevel
::WARNING
, 'Session X: User token mismatch' ),
1525 ), $logger->getBuffer() );
1526 $logger->clearBuffer();
1528 // Provider metadata
1529 $this->store
->setSessionMeta( $id, array( 'provider' => 'Mock2' ) +
$metadata );
1530 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1531 'provider' => $provider2,
1533 'userInfo' => $userInfo,
1534 'metadata' => array( 'Info' ),
1536 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1537 $this->assertSame( array( 'Info', 'changed' => true ), $info->getProviderMetadata() );
1538 $this->assertSame( array(), $logger->getBuffer() );
1540 $this->store
->setSessionMeta( $id, array( 'providerMetadata' => array( 'Saved' ) ) +
$metadata );
1541 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1542 'provider' => $provider,
1544 'userInfo' => $userInfo,
1546 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1547 $this->assertSame( array( 'Saved' ), $info->getProviderMetadata() );
1548 $this->assertSame( array(), $logger->getBuffer() );
1550 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1551 'provider' => $provider,
1553 'userInfo' => $userInfo,
1554 'metadata' => array( 'Info' ),
1556 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1557 $this->assertSame( array( 'Merged' ), $info->getProviderMetadata() );
1558 $this->assertSame( array(), $logger->getBuffer() );
1560 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1561 'provider' => $provider,
1563 'userInfo' => $userInfo,
1564 'metadata' => array( 'Throw' ),
1566 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1567 $this->assertSame( array(
1568 array( LogLevel
::WARNING
, 'Session X: Metadata merge failed: no merge!' ),
1569 ), $logger->getBuffer() );
1570 $logger->clearBuffer();
1572 // Remember from session
1573 $this->store
->setSessionMeta( $id, $metadata );
1574 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1575 'provider' => $provider,
1578 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1579 $this->assertFalse( $info->wasRemembered() );
1580 $this->assertSame( array(), $logger->getBuffer() );
1582 $this->store
->setSessionMeta( $id, array( 'remember' => true ) +
$metadata );
1583 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1584 'provider' => $provider,
1587 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1588 $this->assertTrue( $info->wasRemembered() );
1589 $this->assertSame( array(), $logger->getBuffer() );
1591 $this->store
->setSessionMeta( $id, array( 'remember' => false ) +
$metadata );
1592 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1593 'provider' => $provider,
1595 'userInfo' => $userInfo
1597 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1598 $this->assertTrue( $info->wasRemembered() );
1599 $this->assertSame( array(), $logger->getBuffer() );
1601 // forceHTTPS from session
1602 $this->store
->setSessionMeta( $id, $metadata );
1603 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1604 'provider' => $provider,
1606 'userInfo' => $userInfo
1608 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1609 $this->assertFalse( $info->forceHTTPS() );
1610 $this->assertSame( array(), $logger->getBuffer() );
1612 $this->store
->setSessionMeta( $id, array( 'forceHTTPS' => true ) +
$metadata );
1613 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1614 'provider' => $provider,
1616 'userInfo' => $userInfo
1618 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1619 $this->assertTrue( $info->forceHTTPS() );
1620 $this->assertSame( array(), $logger->getBuffer() );
1622 $this->store
->setSessionMeta( $id, array( 'forceHTTPS' => false ) +
$metadata );
1623 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1624 'provider' => $provider,
1626 'userInfo' => $userInfo,
1627 'forceHTTPS' => true
1629 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1630 $this->assertTrue( $info->forceHTTPS() );
1631 $this->assertSame( array(), $logger->getBuffer() );
1633 // Provider refreshSessionInfo() returning false
1634 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1635 'provider' => $provider3,
1637 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1638 $this->assertSame( array(), $logger->getBuffer() );
1643 $data = array( 'foo' => 1 );
1644 $this->store
->setSession( $id, array( 'metadata' => $metadata, 'data' => $data ) );
1645 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1646 'provider' => $provider,
1648 'userInfo' => $userInfo
1650 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1651 'SessionCheckInfo' => array( function ( &$reason, $i, $r, $m, $d ) use (
1652 $that, $info, $metadata, $data, $request, &$called
1654 $that->assertSame( $info->getId(), $i->getId() );
1655 $that->assertSame( $info->getProvider(), $i->getProvider() );
1656 $that->assertSame( $info->getUserInfo(), $i->getUserInfo() );
1657 $that->assertSame( $request, $r );
1658 $that->assertEquals( $metadata, $m );
1659 $that->assertEquals( $data, $d );
1664 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1665 $this->assertTrue( $called );
1666 $this->assertSame( array(
1667 array( LogLevel
::WARNING
, 'Session X: Hook aborted' ),
1668 ), $logger->getBuffer() );
1669 $logger->clearBuffer();