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 );
340 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
341 $this->assertSame( $id, $session->getId() );
343 $id = $manager->generateSessionId();
344 $this->assertNull( $manager->getSessionById( $id, true ) );
346 // Known but unloadable session ID
347 $this->logger
->setCollect( true );
348 $id = $manager->generateSessionId();
349 $this->store
->setRawSession( $id, array( 'metadata' => array(
350 'provider' => 'DummySessionProvider',
357 $manager->getSessionById( $id );
358 $this->fail( 'Expected exception not thrown' );
359 } catch ( \UnexpectedValueException
$ex ) {
361 'Can neither load the session nor create an empty session',
366 $this->assertNull( $manager->getSessionById( $id, true ) );
367 $this->logger
->setCollect( false );
370 $this->store
->setSession( $id, array() );
371 $session = $manager->getSessionById( $id );
372 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
373 $this->assertSame( $id, $session->getId() );
376 public function testGetEmptySession() {
377 $manager = $this->getManager();
378 $pmanager = \TestingAccessWrapper
::newFromObject( $manager );
379 $request = new \
FauxRequest();
381 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
382 ->setMethods( array( 'provideSessionInfo', 'newSessionInfo', '__toString' ) );
388 $provider1 = $providerBuilder->getMock();
389 $provider1->expects( $this->any() )->method( 'provideSessionInfo' )
390 ->will( $this->returnValue( null ) );
391 $provider1->expects( $this->any() )->method( 'newSessionInfo' )
392 ->with( $this->callback( function ( $id ) use ( &$expectId ) {
393 return $id === $expectId;
395 ->will( $this->returnCallback( function () use ( &$info1 ) {
398 $provider1->expects( $this->any() )->method( '__toString' )
399 ->will( $this->returnValue( 'MockProvider1' ) );
401 $provider2 = $providerBuilder->getMock();
402 $provider2->expects( $this->any() )->method( 'provideSessionInfo' )
403 ->will( $this->returnValue( null ) );
404 $provider2->expects( $this->any() )->method( 'newSessionInfo' )
405 ->with( $this->callback( function ( $id ) use ( &$expectId ) {
406 return $id === $expectId;
408 ->will( $this->returnCallback( function () use ( &$info2 ) {
411 $provider1->expects( $this->any() )->method( '__toString' )
412 ->will( $this->returnValue( 'MockProvider2' ) );
414 $this->config
->set( 'SessionProviders', array(
415 $this->objectCacheDef( $provider1 ),
416 $this->objectCacheDef( $provider2 ),
424 $manager->getEmptySession();
425 $this->fail( 'Expected exception not thrown' );
426 } catch ( \UnexpectedValueException
$ex ) {
428 'No provider could provide an empty session!',
435 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
436 'provider' => $provider1,
437 'id' => 'empty---------------------------',
442 $session = $manager->getEmptySession();
443 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
444 $this->assertSame( 'empty---------------------------', $session->getId() );
447 $expectId = 'expected------------------------';
448 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
449 'provider' => $provider1,
455 $session = $pmanager->getEmptySessionInternal( null, $expectId );
456 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
457 $this->assertSame( $expectId, $session->getId() );
460 $expectId = 'expected-----------------------2';
461 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
462 'provider' => $provider1,
463 'id' => "un$expectId",
469 $pmanager->getEmptySessionInternal( null, $expectId );
470 $this->fail( 'Expected exception not thrown' );
471 } catch ( \UnexpectedValueException
$ex ) {
473 'MockProvider1 returned empty session info with a wrong id: ' .
474 "un$expectId != $expectId",
480 $expectId = 'expected-----------------------2';
481 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
482 'provider' => $provider1,
488 $pmanager->getEmptySessionInternal( null, $expectId );
489 $this->fail( 'Expected exception not thrown' );
490 } catch ( \UnexpectedValueException
$ex ) {
492 'MockProvider1 returned empty session info with id flagged unsafe',
499 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
500 'provider' => $provider2,
501 'id' => 'empty---------------------------',
507 $manager->getEmptySession();
508 $this->fail( 'Expected exception not thrown' );
509 } catch ( \UnexpectedValueException
$ex ) {
511 'MockProvider1 returned an empty session info for a different provider: ' . $info1,
516 // Highest priority wins
518 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, array(
519 'provider' => $provider1,
520 'id' => 'empty1--------------------------',
524 $info2 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
525 'provider' => $provider2,
526 'id' => 'empty2--------------------------',
530 $session = $manager->getEmptySession();
531 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
532 $this->assertSame( 'empty1--------------------------', $session->getId() );
535 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, array(
536 'provider' => $provider1,
537 'id' => 'empty1--------------------------',
541 $info2 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
2, array(
542 'provider' => $provider2,
543 'id' => 'empty2--------------------------',
547 $session = $manager->getEmptySession();
548 $this->assertInstanceOf( 'MediaWiki\\Session\\Session', $session );
549 $this->assertSame( 'empty2--------------------------', $session->getId() );
551 // Tied priorities throw an exception
553 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
554 'provider' => $provider1,
555 'id' => 'empty1--------------------------',
557 'userInfo' => UserInfo
::newAnonymous(),
560 $info2 = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
561 'provider' => $provider2,
562 'id' => 'empty2--------------------------',
564 'userInfo' => UserInfo
::newAnonymous(),
568 $manager->getEmptySession();
569 $this->fail( 'Expected exception not thrown' );
570 } catch ( \UnexpectedValueException
$ex ) {
571 $this->assertStringStartsWith(
572 'Multiple empty sessions tied for top priority: ',
579 $pmanager->getEmptySessionInternal( null, 'bad' );
580 $this->fail( 'Expected exception not thrown' );
581 } catch ( \InvalidArgumentException
$ex ) {
582 $this->assertSame( 'Invalid session ID', $ex->getMessage() );
585 // Session already exists
586 $expectId = 'expected-----------------------3';
587 $this->store
->setSessionMeta( $expectId, array(
588 'provider' => 'MockProvider2',
594 $pmanager->getEmptySessionInternal( null, $expectId );
595 $this->fail( 'Expected exception not thrown' );
596 } catch ( \InvalidArgumentException
$ex ) {
597 $this->assertSame( 'Session ID already exists', $ex->getMessage() );
601 public function testGetVaryHeaders() {
602 $manager = $this->getManager();
604 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
605 ->setMethods( array( 'getVaryHeaders', '__toString' ) );
607 $provider1 = $providerBuilder->getMock();
608 $provider1->expects( $this->once() )->method( 'getVaryHeaders' )
609 ->will( $this->returnValue( array(
611 'Bar' => array( 'X', 'Bar1' ),
614 $provider1->expects( $this->any() )->method( '__toString' )
615 ->will( $this->returnValue( 'MockProvider1' ) );
617 $provider2 = $providerBuilder->getMock();
618 $provider2->expects( $this->once() )->method( 'getVaryHeaders' )
619 ->will( $this->returnValue( array(
621 'Bar' => array( 'X', 'Bar2' ),
622 'Quux' => array( 'Quux' ),
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 ),
634 'Bar' => array( 'X', 'Bar1', 3 => 'Bar2' ),
635 'Quux' => array( 'Quux' ),
637 'Quux' => array( 'Quux' ),
640 $this->assertEquals( $expect, $manager->getVaryHeaders() );
642 // Again, to ensure it's cached
643 $this->assertEquals( $expect, $manager->getVaryHeaders() );
646 public function testGetVaryCookies() {
647 $manager = $this->getManager();
649 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
650 ->setMethods( array( 'getVaryCookies', '__toString' ) );
652 $provider1 = $providerBuilder->getMock();
653 $provider1->expects( $this->once() )->method( 'getVaryCookies' )
654 ->will( $this->returnValue( array( 'Foo', 'Bar' ) ) );
655 $provider1->expects( $this->any() )->method( '__toString' )
656 ->will( $this->returnValue( 'MockProvider1' ) );
658 $provider2 = $providerBuilder->getMock();
659 $provider2->expects( $this->once() )->method( 'getVaryCookies' )
660 ->will( $this->returnValue( array( 'Foo', 'Baz' ) ) );
661 $provider2->expects( $this->any() )->method( '__toString' )
662 ->will( $this->returnValue( 'MockProvider2' ) );
664 $this->config
->set( 'SessionProviders', array(
665 $this->objectCacheDef( $provider1 ),
666 $this->objectCacheDef( $provider2 ),
669 $expect = array( 'Foo', 'Bar', 'Baz' );
671 $this->assertEquals( $expect, $manager->getVaryCookies() );
673 // Again, to ensure it's cached
674 $this->assertEquals( $expect, $manager->getVaryCookies() );
677 public function testGetProviders() {
678 $realManager = $this->getManager();
679 $manager = \TestingAccessWrapper
::newFromObject( $realManager );
681 $this->config
->set( 'SessionProviders', array(
682 array( 'class' => 'DummySessionProvider' ),
684 $providers = $manager->getProviders();
685 $this->assertArrayHasKey( 'DummySessionProvider', $providers );
686 $provider = \TestingAccessWrapper
::newFromObject( $providers['DummySessionProvider'] );
687 $this->assertSame( $manager->logger
, $provider->logger
);
688 $this->assertSame( $manager->config
, $provider->config
);
689 $this->assertSame( $realManager, $provider->getManager() );
691 $this->config
->set( 'SessionProviders', array(
692 array( 'class' => 'DummySessionProvider' ),
693 array( 'class' => 'DummySessionProvider' ),
695 $manager->sessionProviders
= null;
697 $manager->getProviders();
698 $this->fail( 'Expected exception not thrown' );
699 } catch ( \UnexpectedValueException
$ex ) {
701 'Duplicate provider name "DummySessionProvider"',
707 public function testShutdown() {
708 $manager = \TestingAccessWrapper
::newFromObject( $this->getManager() );
709 $manager->setLogger( new \Psr\Log\
NullLogger() );
711 $mock = $this->getMock( 'stdClass', array( 'save' ) );
712 $mock->expects( $this->once() )->method( 'save' );
714 $manager->allSessionBackends
= array( $mock );
715 $manager->shutdown();
718 public function testGetSessionFromInfo() {
719 $manager = \TestingAccessWrapper
::newFromObject( $this->getManager() );
720 $request = new \
FauxRequest();
722 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
724 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
725 'provider' => $manager->getProvider( 'DummySessionProvider' ),
728 'userInfo' => UserInfo
::newFromName( 'UTSysop', true ),
731 \TestingAccessWrapper
::newFromObject( $info )->idIsSafe
= true;
732 $session1 = \TestingAccessWrapper
::newFromObject(
733 $manager->getSessionFromInfo( $info, $request )
735 $session2 = \TestingAccessWrapper
::newFromObject(
736 $manager->getSessionFromInfo( $info, $request )
739 $this->assertSame( $session1->backend
, $session2->backend
);
740 $this->assertNotEquals( $session1->index
, $session2->index
);
741 $this->assertSame( $session1->getSessionId(), $session2->getSessionId() );
742 $this->assertSame( $id, $session1->getId() );
744 \TestingAccessWrapper
::newFromObject( $info )->idIsSafe
= false;
745 $session3 = $manager->getSessionFromInfo( $info, $request );
746 $this->assertNotSame( $id, $session3->getId() );
749 public function testBackendRegistration() {
750 $manager = $this->getManager();
752 $session = $manager->getSessionForRequest( new \FauxRequest
);
753 $backend = \TestingAccessWrapper
::newFromObject( $session )->backend
;
754 $sessionId = $session->getSessionId();
755 $id = (string)$sessionId;
757 $this->assertSame( $sessionId, $manager->getSessionById( $id )->getSessionId() );
759 $manager->changeBackendId( $backend );
760 $this->assertSame( $sessionId, $session->getSessionId() );
761 $this->assertNotEquals( $id, (string)$sessionId );
762 $id = (string)$sessionId;
764 $this->assertSame( $sessionId, $manager->getSessionById( $id )->getSessionId() );
766 // Destruction of the session here causes the backend to be deregistered
770 $manager->changeBackendId( $backend );
771 $this->fail( 'Expected exception not thrown' );
772 } catch ( \InvalidArgumentException
$ex ) {
774 'Backend was not registered with this SessionManager', $ex->getMessage()
779 $manager->deregisterSessionBackend( $backend );
780 $this->fail( 'Expected exception not thrown' );
781 } catch ( \InvalidArgumentException
$ex ) {
783 'Backend was not registered with this SessionManager', $ex->getMessage()
787 $session = $manager->getSessionById( $id );
788 $this->assertSame( $sessionId, $session->getSessionId() );
791 public function testGenerateSessionId() {
792 $manager = $this->getManager();
794 $id = $manager->generateSessionId();
795 $this->assertTrue( SessionManager
::validateSessionId( $id ), "Generated ID: $id" );
798 public function testAutoCreateUser() {
799 global $wgGroupPermissions;
803 \ObjectCache
::$instances[__METHOD__
] = new \
HashBagOStuff();
804 $this->setMwGlobals( array( 'wgMainCacheType' => __METHOD__
) );
806 $this->stashMwGlobals( array( 'wgGroupPermissions' ) );
807 $wgGroupPermissions['*']['createaccount'] = true;
808 $wgGroupPermissions['*']['autocreateaccount'] = false;
810 // Replace the global singleton with one configured for testing
811 $manager = $this->getManager();
812 $reset = TestUtils
::setSessionManagerSingleton( $manager );
814 $logger = new \
TestLogger( true, function ( $m ) {
815 if ( substr( $m, 0, 15 ) === 'SessionBackend ' ) {
819 $m = str_replace( 'MediaWiki\Session\SessionManager::autoCreateUser: ', '', $m );
820 $m = preg_replace( '/ - from: .*$/', ' - from: XXX', $m );
823 $manager->setLogger( $logger );
825 $session = SessionManager
::getGlobalSession();
827 // Can't create an already-existing user
828 $user = User
::newFromName( 'UTSysop' );
829 $id = $user->getId();
830 $this->assertFalse( $manager->autoCreateUser( $user ) );
831 $this->assertSame( $id, $user->getId() );
832 $this->assertSame( 'UTSysop', $user->getName() );
833 $this->assertSame( array(), $logger->getBuffer() );
834 $logger->clearBuffer();
836 // Sanity check that creation works at all
837 $user = User
::newFromName( 'UTSessionAutoCreate1' );
838 $this->assertSame( 0, $user->getId(), 'sanity check' );
839 $this->assertTrue( $manager->autoCreateUser( $user ) );
840 $this->assertNotEquals( 0, $user->getId() );
841 $this->assertSame( 'UTSessionAutoCreate1', $user->getName() );
843 $user->getId(), User
::idFromName( 'UTSessionAutoCreate1', User
::READ_LATEST
)
845 $this->assertSame( array(
846 array( LogLevel
::INFO
, 'creating new user (UTSessionAutoCreate1) - from: XXX' ),
847 ), $logger->getBuffer() );
848 $logger->clearBuffer();
850 // Check lack of permissions
851 $wgGroupPermissions['*']['createaccount'] = false;
852 $wgGroupPermissions['*']['autocreateaccount'] = false;
853 $user = User
::newFromName( 'UTDoesNotExist' );
854 $this->assertFalse( $manager->autoCreateUser( $user ) );
855 $this->assertSame( 0, $user->getId() );
856 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
857 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
859 $this->assertSame( array(
860 array( LogLevel
::DEBUG
, 'user is blocked from this wiki, blacklisting' ),
861 ), $logger->getBuffer() );
862 $logger->clearBuffer();
864 // Check other permission
865 $wgGroupPermissions['*']['createaccount'] = false;
866 $wgGroupPermissions['*']['autocreateaccount'] = true;
867 $user = User
::newFromName( 'UTSessionAutoCreate2' );
868 $this->assertSame( 0, $user->getId(), 'sanity check' );
869 $this->assertTrue( $manager->autoCreateUser( $user ) );
870 $this->assertNotEquals( 0, $user->getId() );
871 $this->assertSame( 'UTSessionAutoCreate2', $user->getName() );
873 $user->getId(), User
::idFromName( 'UTSessionAutoCreate2', User
::READ_LATEST
)
875 $this->assertSame( array(
876 array( LogLevel
::INFO
, 'creating new user (UTSessionAutoCreate2) - from: XXX' ),
877 ), $logger->getBuffer() );
878 $logger->clearBuffer();
880 // Test account-creation block
882 $block = new \
Block( array(
883 'address' => $anon->getName(),
885 'reason' => __METHOD__
,
886 'expiry' => time() +
100500,
887 'createAccount' => true,
890 $this->assertInstanceOf( 'Block', $anon->isBlockedFromCreateAccount(), 'sanity check' );
891 $reset2 = new \
ScopedCallback( array( $block, 'delete' ) );
892 $user = User
::newFromName( 'UTDoesNotExist' );
893 $this->assertFalse( $manager->autoCreateUser( $user ) );
894 $this->assertSame( 0, $user->getId() );
895 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
896 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
897 \ScopedCallback
::consume( $reset2 );
899 $this->assertSame( array(
900 array( LogLevel
::DEBUG
, 'user is blocked from this wiki, blacklisting' ),
901 ), $logger->getBuffer() );
902 $logger->clearBuffer();
904 // Sanity check that creation still works
905 $user = User
::newFromName( 'UTSessionAutoCreate3' );
906 $this->assertSame( 0, $user->getId(), 'sanity check' );
907 $this->assertTrue( $manager->autoCreateUser( $user ) );
908 $this->assertNotEquals( 0, $user->getId() );
909 $this->assertSame( 'UTSessionAutoCreate3', $user->getName() );
911 $user->getId(), User
::idFromName( 'UTSessionAutoCreate3', User
::READ_LATEST
)
913 $this->assertSame( array(
914 array( LogLevel
::INFO
, 'creating new user (UTSessionAutoCreate3) - from: XXX' ),
915 ), $logger->getBuffer() );
916 $logger->clearBuffer();
918 // Test prevention by AuthPlugin
920 $oldWgAuth = $wgAuth;
921 $mockWgAuth = $this->getMock( 'AuthPlugin', array( 'autoCreate' ) );
922 $mockWgAuth->expects( $this->once() )->method( 'autoCreate' )
923 ->will( $this->returnValue( false ) );
924 $this->setMwGlobals( array(
925 'wgAuth' => $mockWgAuth,
927 $user = User
::newFromName( 'UTDoesNotExist' );
928 $this->assertFalse( $manager->autoCreateUser( $user ) );
929 $this->assertSame( 0, $user->getId() );
930 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
931 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
932 $this->setMwGlobals( array(
933 'wgAuth' => $oldWgAuth,
936 $this->assertSame( array(
937 array( LogLevel
::DEBUG
, 'denied by AuthPlugin' ),
938 ), $logger->getBuffer() );
939 $logger->clearBuffer();
941 // Test prevention by wfReadOnly()
942 $this->setMwGlobals( array(
943 'wgReadOnly' => 'Because',
945 $user = User
::newFromName( 'UTDoesNotExist' );
946 $this->assertFalse( $manager->autoCreateUser( $user ) );
947 $this->assertSame( 0, $user->getId() );
948 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
949 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
950 $this->setMwGlobals( array(
951 'wgReadOnly' => false,
954 $this->assertSame( array(
955 array( LogLevel
::DEBUG
, 'denied by wfReadOnly()' ),
956 ), $logger->getBuffer() );
957 $logger->clearBuffer();
959 // Test prevention by a previous session
960 $session->set( 'MWSession::AutoCreateBlacklist', 'test' );
961 $user = User
::newFromName( 'UTDoesNotExist' );
962 $this->assertFalse( $manager->autoCreateUser( $user ) );
963 $this->assertSame( 0, $user->getId() );
964 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
965 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
967 $this->assertSame( array(
968 array( LogLevel
::DEBUG
, 'blacklisted in session (test)' ),
969 ), $logger->getBuffer() );
970 $logger->clearBuffer();
972 // Test uncreatable name
973 $user = User
::newFromName( 'UTDoesNotExist@' );
974 $this->assertFalse( $manager->autoCreateUser( $user ) );
975 $this->assertSame( 0, $user->getId() );
976 $this->assertNotSame( 'UTDoesNotExist@', $user->getName() );
977 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
979 $this->assertSame( array(
980 array( LogLevel
::DEBUG
, 'Invalid username, blacklisting' ),
981 ), $logger->getBuffer() );
982 $logger->clearBuffer();
984 // Test AbortAutoAccount hook
985 $mock = $this->getMock( __CLASS__
, array( 'onAbortAutoAccount' ) );
986 $mock->expects( $this->once() )->method( 'onAbortAutoAccount' )
987 ->will( $this->returnCallback( function ( User
$user, &$msg ) {
991 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array( $mock ) ) );
992 $user = User
::newFromName( 'UTDoesNotExist' );
993 $this->assertFalse( $manager->autoCreateUser( $user ) );
994 $this->assertSame( 0, $user->getId() );
995 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
996 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
997 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array() ) );
999 $this->assertSame( array(
1000 array( LogLevel
::DEBUG
, 'denied by hook: No way!' ),
1001 ), $logger->getBuffer() );
1002 $logger->clearBuffer();
1004 // Test AbortAutoAccount hook screwing up the name
1005 $mock = $this->getMock( 'stdClass', array( 'onAbortAutoAccount' ) );
1006 $mock->expects( $this->once() )->method( 'onAbortAutoAccount' )
1007 ->will( $this->returnCallback( function ( User
$user ) {
1008 $user->setName( 'UTDoesNotExistEither' );
1010 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array( $mock ) ) );
1012 $user = User
::newFromName( 'UTDoesNotExist' );
1013 $manager->autoCreateUser( $user );
1014 $this->fail( 'Expected exception not thrown' );
1015 } catch ( \UnexpectedValueException
$ex ) {
1017 'AbortAutoAccount hook tried to change the user name',
1021 $this->assertSame( 0, $user->getId() );
1022 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
1023 $this->assertNotSame( 'UTDoesNotExistEither', $user->getName() );
1024 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
1025 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExistEither', User
::READ_LATEST
) );
1026 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array() ) );
1028 $this->assertSame( array(), $logger->getBuffer() );
1029 $logger->clearBuffer();
1031 // Test for "exception backoff"
1032 $user = User
::newFromName( 'UTDoesNotExist' );
1033 $cache = \ObjectCache
::getLocalClusterInstance();
1034 $backoffKey = wfMemcKey( 'MWSession', 'autocreate-failed', md5( $user->getName() ) );
1035 $cache->set( $backoffKey, 1, 60 * 10 );
1036 $this->assertFalse( $manager->autoCreateUser( $user ) );
1037 $this->assertSame( 0, $user->getId() );
1038 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
1039 $this->assertEquals( 0, User
::idFromName( 'UTDoesNotExist', User
::READ_LATEST
) );
1040 $cache->delete( $backoffKey );
1042 $this->assertSame( array(
1043 array( LogLevel
::DEBUG
, 'denied by prior creation attempt failures' ),
1044 ), $logger->getBuffer() );
1045 $logger->clearBuffer();
1047 // Sanity check that creation still works, and test completion hook
1048 $cb = $this->callback( function ( User
$user ) use ( $that ) {
1049 $that->assertNotEquals( 0, $user->getId() );
1050 $that->assertSame( 'UTSessionAutoCreate4', $user->getName() );
1051 $that->assertEquals(
1052 $user->getId(), User
::idFromName( 'UTSessionAutoCreate4', User
::READ_LATEST
)
1056 $mock = $this->getMock( 'stdClass',
1057 array( 'onAuthPluginAutoCreate', 'onLocalUserCreated' ) );
1058 $mock->expects( $this->once() )->method( 'onAuthPluginAutoCreate' )
1060 $mock->expects( $this->once() )->method( 'onLocalUserCreated' )
1061 ->with( $cb, $this->identicalTo( true ) );
1062 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1063 'AuthPluginAutoCreate' => array( $mock ),
1064 'LocalUserCreated' => array( $mock ),
1066 $user = User
::newFromName( 'UTSessionAutoCreate4' );
1067 $this->assertSame( 0, $user->getId(), 'sanity check' );
1068 $this->assertTrue( $manager->autoCreateUser( $user ) );
1069 $this->assertNotEquals( 0, $user->getId() );
1070 $this->assertSame( 'UTSessionAutoCreate4', $user->getName() );
1071 $this->assertEquals(
1073 User
::idFromName( 'UTSessionAutoCreate4', User
::READ_LATEST
)
1075 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1076 'AuthPluginAutoCreate' => array(),
1077 'LocalUserCreated' => array(),
1079 $this->assertSame( array(
1080 array( LogLevel
::INFO
, 'creating new user (UTSessionAutoCreate4) - from: XXX' ),
1081 ), $logger->getBuffer() );
1082 $logger->clearBuffer();
1085 public function onAbortAutoAccount( User
$user, &$msg ) {
1088 public function testPreventSessionsForUser() {
1089 $manager = $this->getManager();
1091 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
1092 ->setMethods( array( 'preventSessionsForUser', '__toString' ) );
1094 $provider1 = $providerBuilder->getMock();
1095 $provider1->expects( $this->once() )->method( 'preventSessionsForUser' )
1096 ->with( $this->equalTo( 'UTSysop' ) );
1097 $provider1->expects( $this->any() )->method( '__toString' )
1098 ->will( $this->returnValue( 'MockProvider1' ) );
1100 $this->config
->set( 'SessionProviders', array(
1101 $this->objectCacheDef( $provider1 ),
1104 $user = User
::newFromName( 'UTSysop' );
1105 $token = $user->getToken( true );
1107 $this->assertFalse( $manager->isUserSessionPrevented( 'UTSysop' ) );
1108 $manager->preventSessionsForUser( 'UTSysop' );
1109 $this->assertNotEquals( $token, User
::newFromName( 'UTSysop' )->getToken() );
1110 $this->assertTrue( $manager->isUserSessionPrevented( 'UTSysop' ) );
1113 public function testLoadSessionInfoFromStore() {
1114 $manager = $this->getManager();
1115 $logger = new \
TestLogger( true, function ( $m ) {
1116 return preg_replace(
1117 '/^Session \[\d+\]\w+<(?:null|anon|[+-]:\d+:\w+)>\w+: /', 'Session X: ', $m
1120 $manager->setLogger( $logger );
1121 $request = new \
FauxRequest();
1123 // TestingAccessWrapper can't handle methods with reference arguments, sigh.
1124 $rClass = new \
ReflectionClass( $manager );
1125 $rMethod = $rClass->getMethod( 'loadSessionInfoFromStore' );
1126 $rMethod->setAccessible( true );
1127 $loadSessionInfoFromStore = function ( &$info ) use ( $rMethod, $manager, $request ) {
1128 return $rMethod->invokeArgs( $manager, array( &$info, $request ) );
1131 $userInfo = UserInfo
::newFromName( 'UTSysop', true );
1132 $unverifiedUserInfo = UserInfo
::newFromName( 'UTSysop', false );
1134 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
1136 'userId' => $userInfo->getId(),
1137 'userName' => $userInfo->getName(),
1138 'userToken' => $userInfo->getToken( true ),
1139 'provider' => 'Mock',
1142 $builder = $this->getMockBuilder( 'MediaWiki\\Session\\SessionProvider' )
1143 ->setMethods( array( '__toString', 'mergeMetadata', 'refreshSessionInfo' ) );
1145 $provider = $builder->getMockForAbstractClass();
1146 $provider->setManager( $manager );
1147 $provider->expects( $this->any() )->method( 'persistsSessionId' )
1148 ->will( $this->returnValue( true ) );
1149 $provider->expects( $this->any() )->method( 'canChangeUser' )
1150 ->will( $this->returnValue( true ) );
1151 $provider->expects( $this->any() )->method( 'refreshSessionInfo' )
1152 ->will( $this->returnValue( true ) );
1153 $provider->expects( $this->any() )->method( '__toString' )
1154 ->will( $this->returnValue( 'Mock' ) );
1155 $provider->expects( $this->any() )->method( 'mergeMetadata' )
1156 ->will( $this->returnCallback( function ( $a, $b ) {
1157 if ( $b === array( 'Throw' ) ) {
1158 throw new \
UnexpectedValueException( 'no merge!' );
1160 return array( 'Merged' );
1163 $provider2 = $builder->getMockForAbstractClass();
1164 $provider2->setManager( $manager );
1165 $provider2->expects( $this->any() )->method( 'persistsSessionId' )
1166 ->will( $this->returnValue( false ) );
1167 $provider2->expects( $this->any() )->method( 'canChangeUser' )
1168 ->will( $this->returnValue( false ) );
1169 $provider2->expects( $this->any() )->method( '__toString' )
1170 ->will( $this->returnValue( 'Mock2' ) );
1171 $provider2->expects( $this->any() )->method( 'refreshSessionInfo' )
1172 ->will( $this->returnCallback( function ( $info, $request, &$metadata ) {
1173 $metadata['changed'] = true;
1177 $provider3 = $builder->getMockForAbstractClass();
1178 $provider3->setManager( $manager );
1179 $provider3->expects( $this->any() )->method( 'persistsSessionId' )
1180 ->will( $this->returnValue( true ) );
1181 $provider3->expects( $this->any() )->method( 'canChangeUser' )
1182 ->will( $this->returnValue( true ) );
1183 $provider3->expects( $this->once() )->method( 'refreshSessionInfo' )
1184 ->will( $this->returnValue( false ) );
1185 $provider3->expects( $this->any() )->method( '__toString' )
1186 ->will( $this->returnValue( 'Mock3' ) );
1188 \TestingAccessWrapper
::newFromObject( $manager )->sessionProviders
= array(
1189 (string)$provider => $provider,
1190 (string)$provider2 => $provider2,
1191 (string)$provider3 => $provider3,
1194 // No metadata, basic usage
1195 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1196 'provider' => $provider,
1198 'userInfo' => $userInfo
1200 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1201 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1202 $this->assertFalse( $info->isIdSafe() );
1203 $this->assertSame( array(), $logger->getBuffer() );
1205 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1206 'provider' => $provider,
1207 'userInfo' => $userInfo
1209 $this->assertTrue( $info->isIdSafe(), 'sanity check' );
1210 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1211 $this->assertTrue( $info->isIdSafe() );
1212 $this->assertSame( array(), $logger->getBuffer() );
1214 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1215 'provider' => $provider2,
1217 'userInfo' => $userInfo
1219 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1220 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1221 $this->assertTrue( $info->isIdSafe() );
1222 $this->assertSame( array(), $logger->getBuffer() );
1224 // Unverified user, no metadata
1225 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1226 'provider' => $provider,
1228 'userInfo' => $unverifiedUserInfo
1230 $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
1231 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1232 $this->assertSame( array(
1233 array( LogLevel
::WARNING
, 'Session X: Unverified user provided and no metadata to auth it' )
1234 ), $logger->getBuffer() );
1235 $logger->clearBuffer();
1237 // No metadata, missing data
1238 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1240 'userInfo' => $userInfo
1242 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1243 $this->assertSame( array(
1244 array( LogLevel
::WARNING
, 'Session X: Null provider and no metadata' ),
1245 ), $logger->getBuffer() );
1246 $logger->clearBuffer();
1248 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1249 'provider' => $provider,
1252 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1253 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1254 $this->assertInstanceOf( 'MediaWiki\\Session\\UserInfo', $info->getUserInfo() );
1255 $this->assertTrue( $info->getUserInfo()->isVerified() );
1256 $this->assertTrue( $info->getUserInfo()->isAnon() );
1257 $this->assertFalse( $info->isIdSafe() );
1258 $this->assertSame( array(), $logger->getBuffer() );
1260 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1261 'provider' => $provider2,
1264 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1265 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1266 $this->assertSame( array(
1267 array( LogLevel
::INFO
, 'Session X: No user provided and provider cannot set user' )
1268 ), $logger->getBuffer() );
1269 $logger->clearBuffer();
1271 // Incomplete/bad metadata
1272 $this->store
->setRawSession( $id, true );
1273 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1274 $this->assertSame( array(
1275 array( LogLevel
::WARNING
, 'Session X: Bad data' ),
1276 ), $logger->getBuffer() );
1277 $logger->clearBuffer();
1279 $this->store
->setRawSession( $id, array( 'data' => array() ) );
1280 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1281 $this->assertSame( array(
1282 array( LogLevel
::WARNING
, 'Session X: Bad data structure' ),
1283 ), $logger->getBuffer() );
1284 $logger->clearBuffer();
1286 $this->store
->deleteSession( $id );
1287 $this->store
->setRawSession( $id, array( 'metadata' => $metadata ) );
1288 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1289 $this->assertSame( array(
1290 array( LogLevel
::WARNING
, 'Session X: Bad data structure' ),
1291 ), $logger->getBuffer() );
1292 $logger->clearBuffer();
1294 $this->store
->setRawSession( $id, array( 'metadata' => $metadata, 'data' => true ) );
1295 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1296 $this->assertSame( array(
1297 array( LogLevel
::WARNING
, 'Session X: Bad data structure' ),
1298 ), $logger->getBuffer() );
1299 $logger->clearBuffer();
1301 $this->store
->setRawSession( $id, array( 'metadata' => true, 'data' => array() ) );
1302 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1303 $this->assertSame( array(
1304 array( LogLevel
::WARNING
, 'Session X: Bad data structure' ),
1305 ), $logger->getBuffer() );
1306 $logger->clearBuffer();
1308 foreach ( $metadata as $key => $dummy ) {
1310 unset( $tmp[$key] );
1311 $this->store
->setRawSession( $id, array( 'metadata' => $tmp, 'data' => array() ) );
1312 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1313 $this->assertSame( array(
1314 array( LogLevel
::WARNING
, 'Session X: Bad metadata' ),
1315 ), $logger->getBuffer() );
1316 $logger->clearBuffer();
1319 // Basic usage with metadata
1320 $this->store
->setRawSession( $id, array( 'metadata' => $metadata, 'data' => array() ) );
1321 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1322 'provider' => $provider,
1324 'userInfo' => $userInfo
1326 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1327 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1328 $this->assertTrue( $info->isIdSafe() );
1329 $this->assertSame( array(), $logger->getBuffer() );
1331 // Mismatched provider
1332 $this->store
->setSessionMeta( $id, array( 'provider' => 'Bad' ) +
$metadata );
1333 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1334 'provider' => $provider,
1336 'userInfo' => $userInfo
1338 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1339 $this->assertSame( array(
1340 array( LogLevel
::WARNING
, 'Session X: Wrong provider, Bad !== Mock' ),
1341 ), $logger->getBuffer() );
1342 $logger->clearBuffer();
1345 $this->store
->setSessionMeta( $id, array( 'provider' => 'Bad' ) +
$metadata );
1346 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1348 'userInfo' => $userInfo
1350 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1351 $this->assertSame( array(
1352 array( LogLevel
::WARNING
, 'Session X: Unknown provider, Bad' ),
1353 ), $logger->getBuffer() );
1354 $logger->clearBuffer();
1357 $this->store
->setSessionMeta( $id, $metadata );
1358 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1360 'userInfo' => $userInfo
1362 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1363 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1364 $this->assertTrue( $info->isIdSafe() );
1365 $this->assertSame( array(), $logger->getBuffer() );
1367 // Bad user metadata
1368 $this->store
->setSessionMeta( $id, array( 'userId' => -1, 'userToken' => null ) +
$metadata );
1369 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1370 'provider' => $provider,
1373 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1374 $this->assertSame( array(
1375 array( LogLevel
::ERROR
, 'Session X: Invalid ID' ),
1376 ), $logger->getBuffer() );
1377 $logger->clearBuffer();
1379 $this->store
->setSessionMeta(
1380 $id, array( 'userId' => 0, 'userName' => '<X>', 'userToken' => null ) +
$metadata
1382 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1383 'provider' => $provider,
1386 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1387 $this->assertSame( array(
1388 array( LogLevel
::ERROR
, 'Session X: Invalid user name' ),
1389 ), $logger->getBuffer() );
1390 $logger->clearBuffer();
1392 // Mismatched user by ID
1393 $this->store
->setSessionMeta(
1394 $id, array( 'userId' => $userInfo->getId() +
1, 'userToken' => null ) +
$metadata
1396 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1397 'provider' => $provider,
1399 'userInfo' => $userInfo
1401 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1402 $this->assertSame( array(
1403 array( LogLevel
::WARNING
, 'Session X: User ID mismatch, 2 !== 1' ),
1404 ), $logger->getBuffer() );
1405 $logger->clearBuffer();
1407 // Mismatched user by name
1408 $this->store
->setSessionMeta(
1409 $id, array( 'userId' => 0, 'userName' => 'X', '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(
1418 array( LogLevel
::WARNING
, 'Session X: User name mismatch, X !== UTSysop' ),
1419 ), $logger->getBuffer() );
1420 $logger->clearBuffer();
1422 // ID matches, name doesn't
1423 $this->store
->setSessionMeta(
1424 $id, array( 'userId' => $userInfo->getId(), 'userName' => 'X', 'userToken' => null ) +
$metadata
1426 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1427 'provider' => $provider,
1429 'userInfo' => $userInfo
1431 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1432 $this->assertSame( array(
1434 LogLevel
::WARNING
, 'Session X: User ID matched but name didn\'t (rename?), X !== UTSysop'
1436 ), $logger->getBuffer() );
1437 $logger->clearBuffer();
1439 // Mismatched anon user
1440 $this->store
->setSessionMeta(
1441 $id, array( 'userId' => 0, 'userName' => null, 'userToken' => null ) +
$metadata
1443 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1444 'provider' => $provider,
1446 'userInfo' => $userInfo
1448 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1449 $this->assertSame( array(
1451 LogLevel
::WARNING
, 'Session X: Metadata has an anonymous user, but a non-anon user was provided'
1453 ), $logger->getBuffer() );
1454 $logger->clearBuffer();
1456 // Lookup user by ID
1457 $this->store
->setSessionMeta( $id, array( 'userToken' => null ) +
$metadata );
1458 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1459 'provider' => $provider,
1462 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1463 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1464 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1465 $this->assertTrue( $info->isIdSafe() );
1466 $this->assertSame( array(), $logger->getBuffer() );
1468 // Lookup user by name
1469 $this->store
->setSessionMeta(
1470 $id, array( 'userId' => 0, 'userName' => 'UTSysop', 'userToken' => null ) +
$metadata
1472 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1473 'provider' => $provider,
1476 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1477 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1478 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1479 $this->assertTrue( $info->isIdSafe() );
1480 $this->assertSame( array(), $logger->getBuffer() );
1482 // Lookup anonymous user
1483 $this->store
->setSessionMeta(
1484 $id, array( 'userId' => 0, 'userName' => null, 'userToken' => null ) +
$metadata
1486 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1487 'provider' => $provider,
1490 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1491 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1492 $this->assertTrue( $info->getUserInfo()->isAnon() );
1493 $this->assertTrue( $info->isIdSafe() );
1494 $this->assertSame( array(), $logger->getBuffer() );
1496 // Unverified user with metadata
1497 $this->store
->setSessionMeta( $id, $metadata );
1498 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1499 'provider' => $provider,
1501 'userInfo' => $unverifiedUserInfo
1503 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1504 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1505 $this->assertTrue( $info->getUserInfo()->isVerified() );
1506 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1507 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1508 $this->assertTrue( $info->isIdSafe() );
1509 $this->assertSame( array(), $logger->getBuffer() );
1511 // Unverified user with metadata
1512 $this->store
->setSessionMeta( $id, $metadata );
1513 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1514 'provider' => $provider,
1516 'userInfo' => $unverifiedUserInfo
1518 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1519 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1520 $this->assertTrue( $info->getUserInfo()->isVerified() );
1521 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1522 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1523 $this->assertTrue( $info->isIdSafe() );
1524 $this->assertSame( array(), $logger->getBuffer() );
1527 $this->store
->setSessionMeta( $id, array( 'userToken' => 'Bad' ) +
$metadata );
1528 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1529 'provider' => $provider,
1531 'userInfo' => $userInfo
1533 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1534 $this->assertSame( array(
1535 array( LogLevel
::WARNING
, 'Session X: User token mismatch' ),
1536 ), $logger->getBuffer() );
1537 $logger->clearBuffer();
1539 // Provider metadata
1540 $this->store
->setSessionMeta( $id, array( 'provider' => 'Mock2' ) +
$metadata );
1541 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1542 'provider' => $provider2,
1544 'userInfo' => $userInfo,
1545 'metadata' => array( 'Info' ),
1547 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1548 $this->assertSame( array( 'Info', 'changed' => true ), $info->getProviderMetadata() );
1549 $this->assertSame( array(), $logger->getBuffer() );
1551 $this->store
->setSessionMeta( $id, array( 'providerMetadata' => array( 'Saved' ) ) +
$metadata );
1552 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1553 'provider' => $provider,
1555 'userInfo' => $userInfo,
1557 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1558 $this->assertSame( array( 'Saved' ), $info->getProviderMetadata() );
1559 $this->assertSame( array(), $logger->getBuffer() );
1561 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1562 'provider' => $provider,
1564 'userInfo' => $userInfo,
1565 'metadata' => array( 'Info' ),
1567 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1568 $this->assertSame( array( 'Merged' ), $info->getProviderMetadata() );
1569 $this->assertSame( array(), $logger->getBuffer() );
1571 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1572 'provider' => $provider,
1574 'userInfo' => $userInfo,
1575 'metadata' => array( 'Throw' ),
1577 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1578 $this->assertSame( array(
1579 array( LogLevel
::WARNING
, 'Session X: Metadata merge failed: no merge!' ),
1580 ), $logger->getBuffer() );
1581 $logger->clearBuffer();
1583 // Remember from session
1584 $this->store
->setSessionMeta( $id, $metadata );
1585 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1586 'provider' => $provider,
1589 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1590 $this->assertFalse( $info->wasRemembered() );
1591 $this->assertSame( array(), $logger->getBuffer() );
1593 $this->store
->setSessionMeta( $id, array( 'remember' => true ) +
$metadata );
1594 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1595 'provider' => $provider,
1598 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1599 $this->assertTrue( $info->wasRemembered() );
1600 $this->assertSame( array(), $logger->getBuffer() );
1602 $this->store
->setSessionMeta( $id, array( 'remember' => false ) +
$metadata );
1603 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1604 'provider' => $provider,
1606 'userInfo' => $userInfo
1608 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1609 $this->assertTrue( $info->wasRemembered() );
1610 $this->assertSame( array(), $logger->getBuffer() );
1612 // forceHTTPS from session
1613 $this->store
->setSessionMeta( $id, $metadata );
1614 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1615 'provider' => $provider,
1617 'userInfo' => $userInfo
1619 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1620 $this->assertFalse( $info->forceHTTPS() );
1621 $this->assertSame( array(), $logger->getBuffer() );
1623 $this->store
->setSessionMeta( $id, array( 'forceHTTPS' => true ) +
$metadata );
1624 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1625 'provider' => $provider,
1627 'userInfo' => $userInfo
1629 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1630 $this->assertTrue( $info->forceHTTPS() );
1631 $this->assertSame( array(), $logger->getBuffer() );
1633 $this->store
->setSessionMeta( $id, array( 'forceHTTPS' => false ) +
$metadata );
1634 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1635 'provider' => $provider,
1637 'userInfo' => $userInfo,
1638 'forceHTTPS' => true
1640 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1641 $this->assertTrue( $info->forceHTTPS() );
1642 $this->assertSame( array(), $logger->getBuffer() );
1644 // Provider refreshSessionInfo() returning false
1645 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1646 'provider' => $provider3,
1648 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1649 $this->assertSame( array(), $logger->getBuffer() );
1654 $data = array( 'foo' => 1 );
1655 $this->store
->setSession( $id, array( 'metadata' => $metadata, 'data' => $data ) );
1656 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, array(
1657 'provider' => $provider,
1659 'userInfo' => $userInfo
1661 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1662 'SessionCheckInfo' => array( function ( &$reason, $i, $r, $m, $d ) use (
1663 $that, $info, $metadata, $data, $request, &$called
1665 $that->assertSame( $info->getId(), $i->getId() );
1666 $that->assertSame( $info->getProvider(), $i->getProvider() );
1667 $that->assertSame( $info->getUserInfo(), $i->getUserInfo() );
1668 $that->assertSame( $request, $r );
1669 $that->assertEquals( $metadata, $m );
1670 $that->assertEquals( $data, $d );
1675 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1676 $this->assertTrue( $called );
1677 $this->assertSame( array(
1678 array( LogLevel
::WARNING
, 'Session X: Hook aborted' ),
1679 ), $logger->getBuffer() );
1680 $logger->clearBuffer();