8 use Wikimedia\TestingAccessWrapper;
21 $this->config = new \HashConfig( [
22 'LanguageCode' =>
'en',
23 'SessionCacheType' =>
'testSessionStore',
24 'ObjectCacheSessionExpiry' => 100,
25 'SessionProviders' => [
26 [
'class' =>
'DummySessionProvider' ],
29 $this->logger = new \TestLogger(
false,
function ( $m ) {
30 return substr( $m, 0, 15 ) ===
'SessionBackend ' ? null : $m;
35 'config' => $this->config,
36 'logger' => $this->logger,
37 'store' => $this->store,
42 return [
'factory' =>
function ()
use ( $object ) {
62 $rProp->setAccessible(
true );
63 $handler = TestingAccessWrapper::newFromObject( $rProp->getValue() );
65 $reset[] = new \Wikimedia\ScopedCallback(
function ()
use (
$handler, $oldEnable ) {
67 session_write_close();
76 $id =
$request->getSession()->getId();
80 $this->assertSame( $id, $session->getId() );
82 session_id(
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
84 $this->assertSame(
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', $session->getId() );
85 $this->assertSame(
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
$request->getSession()->getId() );
87 session_write_close();
91 $id =
$request->getSession()->getId();
95 $this->assertSame( $id, $session->getId() );
97 session_id(
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
99 $this->assertSame( $id, $session->getId() );
100 $this->assertSame( $id,
$request->getSession()->getId() );
104 $manager = TestingAccessWrapper::newFromObject( $this->
getManager() );
105 $this->assertSame( $this->config, $manager->config );
106 $this->assertSame( $this->logger, $manager->logger );
107 $this->assertSame( $this->
store, $manager->store );
109 $manager = TestingAccessWrapper::newFromObject(
new SessionManager() );
112 $manager = TestingAccessWrapper::newFromObject(
new SessionManager( [
113 'config' => $this->config,
118 'config' =>
'$options[\'config\'] must be an instance of Config',
119 'logger' =>
'$options[\'logger\'] must be an instance of LoggerInterface',
120 'store' =>
'$options[\'store\'] must be an instance of BagOStuff',
121 ]
as $key => $error ) {
124 $this->fail(
'Expected exception not thrown' );
125 }
catch ( \InvalidArgumentException $ex ) {
126 $this->assertSame( $error, $ex->getMessage() );
139 $idEmpty =
'empty-session-------------------';
141 $providerBuilder = $this->getMockBuilder(
'DummySessionProvider' )
143 [
'provideSessionInfo',
'newSessionInfo',
'__toString',
'describe',
'unpersistSession' ]
146 $provider1 = $providerBuilder->getMock();
147 $provider1->expects( $this->
any() )->method(
'provideSessionInfo' )
148 ->with( $this->identicalTo(
$request ) )
149 ->will( $this->returnCallback(
function (
$request ) {
152 $provider1->expects( $this->
any() )->method(
'newSessionInfo' )
153 ->will( $this->returnCallback(
function ()
use ( $idEmpty, $provider1 ) {
155 'provider' => $provider1,
161 $provider1->expects( $this->
any() )->method(
'__toString' )
162 ->will( $this->returnValue(
'Provider1' ) );
163 $provider1->expects( $this->
any() )->method(
'describe' )
164 ->will( $this->returnValue(
'#1 sessions' ) );
165 $provider1->expects( $this->
any() )->method(
'unpersistSession' )
166 ->will( $this->returnCallback(
function (
$request ) {
170 $provider2 = $providerBuilder->getMock();
171 $provider2->expects( $this->
any() )->method(
'provideSessionInfo' )
172 ->with( $this->identicalTo(
$request ) )
173 ->will( $this->returnCallback(
function (
$request ) {
176 $provider2->expects( $this->
any() )->method(
'__toString' )
177 ->will( $this->returnValue(
'Provider2' ) );
178 $provider2->expects( $this->
any() )->method(
'describe' )
179 ->will( $this->returnValue(
'#2 sessions' ) );
180 $provider2->expects( $this->
any() )->method(
'unpersistSession' )
181 ->will( $this->returnCallback(
function (
$request ) {
185 $this->config->set(
'SessionProviders', [
193 $session = $manager->getSessionForRequest(
$request );
195 $this->assertSame( $idEmpty, $session->getId() );
196 $this->assertFalse(
$request->unpersist1 );
197 $this->assertFalse(
$request->unpersist2 );
201 'provider' => $provider1,
202 'id' => ( $id1 = $manager->generateSessionId() ),
207 'provider' => $provider2,
208 'id' => ( $id2 = $manager->generateSessionId() ),
212 $session = $manager->getSessionForRequest(
$request );
214 $this->assertSame( $id2, $session->getId() );
215 $this->assertFalse(
$request->unpersist1 );
216 $this->assertFalse(
$request->unpersist2 );
219 'provider' => $provider1,
220 'id' => ( $id1 = $manager->generateSessionId() ),
225 'provider' => $provider2,
226 'id' => ( $id2 = $manager->generateSessionId() ),
230 $session = $manager->getSessionForRequest(
$request );
232 $this->assertSame( $id1, $session->getId() );
233 $this->assertFalse(
$request->unpersist1 );
234 $this->assertFalse(
$request->unpersist2 );
238 'provider' => $provider1,
239 'id' => ( $id1 = $manager->generateSessionId() ),
245 'provider' => $provider2,
246 'id' => ( $id2 = $manager->generateSessionId() ),
252 $manager->getSessionForRequest(
$request );
253 $this->fail(
'Expcected exception not thrown' );
254 }
catch ( \OverflowException $ex ) {
255 $this->assertStringStartsWith(
256 'Multiple sessions for this request tied for top priority: ',
259 $this->assertCount( 2, $ex->sessionInfos );
260 $this->assertContains(
$request->info1, $ex->sessionInfos );
261 $this->assertContains(
$request->info2, $ex->sessionInfos );
263 $this->assertFalse(
$request->unpersist1 );
264 $this->assertFalse(
$request->unpersist2 );
268 'provider' => $provider2,
269 'id' => ( $id1 = $manager->generateSessionId() ),
275 $manager->getSessionForRequest(
$request );
276 $this->fail(
'Expcected exception not thrown' );
277 }
catch ( \UnexpectedValueException $ex ) {
279 'Provider1 returned session info for a different provider: ' .
$request->info1,
283 $this->assertFalse(
$request->unpersist1 );
284 $this->assertFalse(
$request->unpersist2 );
287 $this->logger->setCollect(
true );
289 'provider' => $provider1,
290 'id' => ( $id1 = $manager->generateSessionId() ),
296 'provider' => $provider2,
297 'id' => ( $id2 = $manager->generateSessionId() ),
301 $session = $manager->getSessionForRequest(
$request );
303 $this->assertSame( $id2, $session->getId() );
304 $this->logger->setCollect(
false );
305 $this->assertTrue(
$request->unpersist1 );
306 $this->assertFalse(
$request->unpersist2 );
309 $this->logger->setCollect(
true );
311 'provider' => $provider1,
312 'id' => ( $id1 = $manager->generateSessionId() ),
317 'provider' => $provider2,
318 'id' => ( $id2 = $manager->generateSessionId() ),
323 $session = $manager->getSessionForRequest(
$request );
325 $this->assertSame( $id1, $session->
getId() );
326 $this->logger->setCollect(
false );
327 $this->assertFalse(
$request->unpersist1 );
328 $this->assertTrue(
$request->unpersist2 );
333 'provider' => $provider1,
334 'id' => ( $id1 = $manager->generateSessionId() ),
335 'persisted' =>
false,
340 $session = $manager->getSessionForRequest(
$request );
342 $this->assertSame( $id1, $session->
getId() );
343 $this->assertTrue(
$request->unpersist1 );
344 $this->assertFalse(
$request->unpersist2 );
346 $this->assertTrue( $session->isPersistent(),
'sanity check' );
352 $manager->getSessionById(
'bad' );
353 $this->fail(
'Expected exception not thrown' );
354 }
catch ( \InvalidArgumentException $ex ) {
355 $this->assertSame(
'Invalid session ID', $ex->getMessage() );
359 $id = $manager->generateSessionId();
360 $session = $manager->getSessionById( $id,
true );
362 $this->assertSame( $id, $session->getId() );
364 $id = $manager->generateSessionId();
365 $this->assertNull( $manager->getSessionById( $id,
false ) );
368 $this->logger->setCollect(
true );
369 $id = $manager->generateSessionId();
370 $this->
store->setSession( $id, [
'metadata' => [
372 'userToken' =>
'bad',
375 $this->assertNull( $manager->getSessionById( $id,
true ) );
376 $this->assertNull( $manager->getSessionById( $id,
false ) );
377 $this->logger->setCollect(
false );
380 $this->
store->setSession( $id, [] );
381 $session = $manager->getSessionById( $id,
false );
383 $this->assertSame( $id, $session->getId() );
386 $this->
store->setSession( $id, [
'metadata' => [
388 'userToken' =>
'bad',
390 $session2 = $manager->getSessionById( $id,
false );
392 $this->assertSame( $id, $session2->getId() );
393 unset( $session, $session2 );
394 $this->logger->setCollect(
true );
395 $this->assertNull( $manager->getSessionById( $id,
true ) );
396 $this->logger->setCollect(
false );
400 $provider = $this->getMockBuilder(
'DummySessionProvider' )
401 ->setMethods( [
'provideSessionInfo',
'newSessionInfo',
'__toString' ] )
403 $provider->expects( $this->
any() )->method(
'provideSessionInfo' )
404 ->will( $this->returnValue(
null ) );
405 $provider->expects( $this->
any() )->method(
'newSessionInfo' )
406 ->will( $this->returnValue(
null ) );
407 $provider->expects( $this->
any() )->method(
'__toString' )
408 ->will( $this->returnValue(
'MockProvider' ) );
409 $this->config->set(
'SessionProviders', [
412 $this->logger->setCollect(
true );
413 $this->assertNull( $manager->getSessionById( $id,
true ) );
414 $this->logger->setCollect(
false );
416 [ LogLevel::ERROR,
'Failed to create empty session: {exception}' ]
417 ], $this->logger->getBuffer() );
422 $pmanager = TestingAccessWrapper::newFromObject( $manager );
425 $providerBuilder = $this->getMockBuilder(
'DummySessionProvider' )
426 ->setMethods( [
'provideSessionInfo',
'newSessionInfo',
'__toString' ] );
432 $provider1 = $providerBuilder->getMock();
433 $provider1->expects( $this->
any() )->method(
'provideSessionInfo' )
434 ->will( $this->returnValue(
null ) );
435 $provider1->expects( $this->
any() )->method(
'newSessionInfo' )
436 ->with( $this->callback(
function ( $id )
use ( &$expectId ) {
437 return $id === $expectId;
439 ->will( $this->returnCallback(
function ()
use ( &$info1 ) {
442 $provider1->expects( $this->
any() )->method(
'__toString' )
443 ->will( $this->returnValue(
'MockProvider1' ) );
445 $provider2 = $providerBuilder->getMock();
446 $provider2->expects( $this->
any() )->method(
'provideSessionInfo' )
447 ->will( $this->returnValue(
null ) );
448 $provider2->expects( $this->
any() )->method(
'newSessionInfo' )
449 ->with( $this->callback(
function ( $id )
use ( &$expectId ) {
450 return $id === $expectId;
452 ->will( $this->returnCallback(
function ()
use ( &$info2 ) {
455 $provider1->expects( $this->
any() )->method(
'__toString' )
456 ->will( $this->returnValue(
'MockProvider2' ) );
458 $this->config->set(
'SessionProviders', [
468 $manager->getEmptySession();
469 $this->fail(
'Expected exception not thrown' );
470 }
catch ( \UnexpectedValueException $ex ) {
472 'No provider could provide an empty session!',
480 'provider' => $provider1,
481 'id' =>
'empty---------------------------',
486 $session = $manager->getEmptySession();
488 $this->assertSame(
'empty---------------------------', $session->getId() );
491 $expectId =
'expected------------------------';
493 'provider' => $provider1,
499 $session = $pmanager->getEmptySessionInternal(
null, $expectId );
501 $this->assertSame( $expectId, $session->getId() );
504 $expectId =
'expected-----------------------2';
506 'provider' => $provider1,
507 'id' =>
"un$expectId",
513 $pmanager->getEmptySessionInternal(
null, $expectId );
514 $this->fail(
'Expected exception not thrown' );
515 }
catch ( \UnexpectedValueException $ex ) {
517 'MockProvider1 returned empty session info with a wrong id: ' .
518 "un$expectId != $expectId",
524 $expectId =
'expected-----------------------2';
526 'provider' => $provider1,
532 $pmanager->getEmptySessionInternal(
null, $expectId );
533 $this->fail(
'Expected exception not thrown' );
534 }
catch ( \UnexpectedValueException $ex ) {
536 'MockProvider1 returned empty session info with id flagged unsafe',
544 'provider' => $provider2,
545 'id' =>
'empty---------------------------',
551 $manager->getEmptySession();
552 $this->fail(
'Expected exception not thrown' );
553 }
catch ( \UnexpectedValueException $ex ) {
555 'MockProvider1 returned an empty session info for a different provider: ' . $info1,
563 'provider' => $provider1,
564 'id' =>
'empty1--------------------------',
569 'provider' => $provider2,
570 'id' =>
'empty2--------------------------',
574 $session = $manager->getEmptySession();
576 $this->assertSame(
'empty1--------------------------', $session->getId() );
580 'provider' => $provider1,
581 'id' =>
'empty1--------------------------',
586 'provider' => $provider2,
587 'id' =>
'empty2--------------------------',
591 $session = $manager->getEmptySession();
593 $this->assertSame(
'empty2--------------------------', $session->getId() );
598 'provider' => $provider1,
599 'id' =>
'empty1--------------------------',
605 'provider' => $provider2,
606 'id' =>
'empty2--------------------------',
612 $manager->getEmptySession();
613 $this->fail(
'Expected exception not thrown' );
614 }
catch ( \UnexpectedValueException $ex ) {
615 $this->assertStringStartsWith(
616 'Multiple empty sessions tied for top priority: ',
623 $pmanager->getEmptySessionInternal(
null,
'bad' );
624 $this->fail(
'Expected exception not thrown' );
625 }
catch ( \InvalidArgumentException $ex ) {
626 $this->assertSame(
'Invalid session ID', $ex->getMessage() );
630 $expectId =
'expected-----------------------3';
631 $this->
store->setSessionMeta( $expectId, [
632 'provider' =>
'MockProvider2',
638 $pmanager->getEmptySessionInternal(
null, $expectId );
639 $this->fail(
'Expected exception not thrown' );
640 }
catch ( \InvalidArgumentException $ex ) {
641 $this->assertSame(
'Session ID already exists', $ex->getMessage() );
649 $providerBuilder = $this->getMockBuilder(
'DummySessionProvider' )
650 ->setMethods( [
'invalidateSessionsForUser',
'__toString' ] );
652 $provider1 = $providerBuilder->getMock();
653 $provider1->expects( $this->once() )->method(
'invalidateSessionsForUser' )
654 ->with( $this->identicalTo(
$user ) );
655 $provider1->expects( $this->
any() )->method(
'__toString' )
656 ->will( $this->returnValue(
'MockProvider1' ) );
658 $provider2 = $providerBuilder->getMock();
659 $provider2->expects( $this->once() )->method(
'invalidateSessionsForUser' )
660 ->with( $this->identicalTo(
$user ) );
661 $provider2->expects( $this->
any() )->method(
'__toString' )
662 ->will( $this->returnValue(
'MockProvider2' ) );
664 $this->config->set(
'SessionProviders', [
669 $oldToken =
$user->getToken(
true );
670 $manager->invalidateSessionsForUser(
$user );
671 $this->assertNotEquals( $oldToken,
$user->getToken() );
677 $providerBuilder = $this->getMockBuilder(
'DummySessionProvider' )
678 ->setMethods( [
'getVaryHeaders',
'__toString' ] );
680 $provider1 = $providerBuilder->getMock();
681 $provider1->expects( $this->once() )->method(
'getVaryHeaders' )
682 ->will( $this->returnValue( [
684 'Bar' => [
'X',
'Bar1' ],
687 $provider1->expects( $this->
any() )->method(
'__toString' )
688 ->will( $this->returnValue(
'MockProvider1' ) );
690 $provider2 = $providerBuilder->getMock();
691 $provider2->expects( $this->once() )->method(
'getVaryHeaders' )
692 ->will( $this->returnValue( [
694 'Bar' => [
'X',
'Bar2' ],
695 'Quux' => [
'Quux' ],
697 $provider2->expects( $this->
any() )->method(
'__toString' )
698 ->will( $this->returnValue(
'MockProvider2' ) );
700 $this->config->set(
'SessionProviders', [
707 'Bar' => [
'X',
'Bar1', 3 =>
'Bar2' ],
708 'Quux' => [
'Quux' ],
712 $this->assertEquals( $expect, $manager->getVaryHeaders() );
715 $this->assertEquals( $expect, $manager->getVaryHeaders() );
721 $providerBuilder = $this->getMockBuilder(
'DummySessionProvider' )
722 ->setMethods( [
'getVaryCookies',
'__toString' ] );
724 $provider1 = $providerBuilder->getMock();
725 $provider1->expects( $this->once() )->method(
'getVaryCookies' )
726 ->will( $this->returnValue( [
'Foo',
'Bar' ] ) );
727 $provider1->expects( $this->
any() )->method(
'__toString' )
728 ->will( $this->returnValue(
'MockProvider1' ) );
730 $provider2 = $providerBuilder->getMock();
731 $provider2->expects( $this->once() )->method(
'getVaryCookies' )
732 ->will( $this->returnValue( [
'Foo',
'Baz' ] ) );
733 $provider2->expects( $this->
any() )->method(
'__toString' )
734 ->will( $this->returnValue(
'MockProvider2' ) );
736 $this->config->set(
'SessionProviders', [
741 $expect = [
'Foo',
'Bar',
'Baz' ];
743 $this->assertEquals( $expect, $manager->getVaryCookies() );
746 $this->assertEquals( $expect, $manager->getVaryCookies() );
751 $manager = TestingAccessWrapper::newFromObject( $realManager );
753 $this->config->set(
'SessionProviders', [
754 [
'class' =>
'DummySessionProvider' ],
756 $providers = $manager->getProviders();
757 $this->assertArrayHasKey(
'DummySessionProvider', $providers );
758 $provider = TestingAccessWrapper::newFromObject( $providers[
'DummySessionProvider'] );
759 $this->assertSame( $manager->logger, $provider->logger );
760 $this->assertSame( $manager->config, $provider->config );
761 $this->assertSame( $realManager, $provider->getManager() );
763 $this->config->set(
'SessionProviders', [
764 [
'class' =>
'DummySessionProvider' ],
765 [
'class' =>
'DummySessionProvider' ],
767 $manager->sessionProviders =
null;
769 $manager->getProviders();
770 $this->fail(
'Expected exception not thrown' );
771 }
catch ( \UnexpectedValueException $ex ) {
773 'Duplicate provider name "DummySessionProvider"',
780 $manager = TestingAccessWrapper::newFromObject( $this->
getManager() );
781 $manager->setLogger(
new \Psr\Log\NullLogger() );
783 $mock = $this->getMockBuilder(
'stdClass' )
784 ->setMethods( [
'shutdown' ] )->getMock();
785 $mock->expects( $this->once() )->method(
'shutdown' );
787 $manager->allSessionBackends = [ $mock ];
788 $manager->shutdown();
792 $manager = TestingAccessWrapper::newFromObject( $this->
getManager() );
795 $id =
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
798 'provider' => $manager->getProvider(
'DummySessionProvider' ),
804 TestingAccessWrapper::newFromObject( $info )->idIsSafe =
true;
805 $session1 = TestingAccessWrapper::newFromObject(
806 $manager->getSessionFromInfo( $info,
$request )
808 $session2 = TestingAccessWrapper::newFromObject(
809 $manager->getSessionFromInfo( $info,
$request )
812 $this->assertSame( $session1->backend, $session2->backend );
813 $this->assertNotEquals( $session1->index, $session2->index );
814 $this->assertSame( $session1->getSessionId(), $session2->getSessionId() );
815 $this->assertSame( $id, $session1->getId() );
817 TestingAccessWrapper::newFromObject( $info )->idIsSafe =
false;
818 $session3 = $manager->getSessionFromInfo( $info,
$request );
819 $this->assertNotSame( $id, $session3->getId() );
825 $session = $manager->getSessionForRequest(
new \
FauxRequest );
826 $backend = TestingAccessWrapper::newFromObject( $session )->backend;
827 $sessionId = $session->getSessionId();
830 $this->assertSame( $sessionId, $manager->getSessionById( $id,
true )->getSessionId() );
832 $manager->changeBackendId( $backend );
833 $this->assertSame( $sessionId, $session->getSessionId() );
834 $this->assertNotEquals( $id, (
string)$sessionId );
837 $this->assertSame( $sessionId, $manager->getSessionById( $id,
true )->getSessionId() );
843 $manager->changeBackendId( $backend );
844 $this->fail(
'Expected exception not thrown' );
845 }
catch ( \InvalidArgumentException $ex ) {
847 'Backend was not registered with this SessionManager', $ex->getMessage()
852 $manager->deregisterSessionBackend( $backend );
853 $this->fail(
'Expected exception not thrown' );
854 }
catch ( \InvalidArgumentException $ex ) {
856 'Backend was not registered with this SessionManager', $ex->getMessage()
860 $session = $manager->getSessionById( $id,
true );
861 $this->assertSame( $sessionId, $session->getSessionId() );
867 $id = $manager->generateSessionId();
874 $providerBuilder = $this->getMockBuilder(
'DummySessionProvider' )
875 ->setMethods( [
'preventSessionsForUser',
'__toString' ] );
877 $provider1 = $providerBuilder->getMock();
878 $provider1->expects( $this->once() )->method(
'preventSessionsForUser' )
879 ->with( $this->equalTo(
'UTSysop' ) );
880 $provider1->expects( $this->
any() )->method(
'__toString' )
881 ->will( $this->returnValue(
'MockProvider1' ) );
883 $this->config->set(
'SessionProviders', [
887 $this->assertFalse( $manager->isUserSessionPrevented(
'UTSysop' ) );
888 $manager->preventSessionsForUser(
'UTSysop' );
889 $this->assertTrue( $manager->isUserSessionPrevented(
'UTSysop' ) );
894 $logger = new \TestLogger(
true );
895 $manager->setLogger(
$logger );
899 $rClass = new \ReflectionClass( $manager );
900 $rMethod = $rClass->getMethod(
'loadSessionInfoFromStore' );
901 $rMethod->setAccessible(
true );
902 $loadSessionInfoFromStore =
function ( &$info )
use ( $rMethod, $manager,
$request ) {
903 return $rMethod->invokeArgs( $manager, [ &$info,
$request ] );
909 $id =
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
911 'userId' => $userInfo->getId(),
912 'userName' => $userInfo->getName(),
913 'userToken' => $userInfo->getToken(
true ),
914 'provider' =>
'Mock',
918 ->setMethods( [
'__toString',
'mergeMetadata',
'refreshSessionInfo' ] );
920 $provider = $builder->getMockForAbstractClass();
921 $provider->setManager( $manager );
922 $provider->expects( $this->
any() )->method(
'persistsSessionId' )
923 ->will( $this->returnValue(
true ) );
924 $provider->expects( $this->
any() )->method(
'canChangeUser' )
925 ->will( $this->returnValue(
true ) );
926 $provider->expects( $this->
any() )->method(
'refreshSessionInfo' )
927 ->will( $this->returnValue(
true ) );
928 $provider->expects( $this->
any() )->method(
'__toString' )
929 ->will( $this->returnValue(
'Mock' ) );
930 $provider->expects( $this->
any() )->method(
'mergeMetadata' )
931 ->will( $this->returnCallback(
function ( $a, $b ) {
932 if ( $b === [
'Throw' ] ) {
938 $provider2 = $builder->getMockForAbstractClass();
939 $provider2->setManager( $manager );
940 $provider2->expects( $this->
any() )->method(
'persistsSessionId' )
941 ->will( $this->returnValue(
false ) );
942 $provider2->expects( $this->
any() )->method(
'canChangeUser' )
943 ->will( $this->returnValue(
false ) );
944 $provider2->expects( $this->
any() )->method(
'__toString' )
945 ->will( $this->returnValue(
'Mock2' ) );
946 $provider2->expects( $this->
any() )->method(
'refreshSessionInfo' )
947 ->will( $this->returnCallback(
function ( $info,
$request, &$metadata ) {
948 $metadata[
'changed'] =
true;
952 $provider3 = $builder->getMockForAbstractClass();
953 $provider3->setManager( $manager );
954 $provider3->expects( $this->
any() )->method(
'persistsSessionId' )
955 ->will( $this->returnValue(
true ) );
956 $provider3->expects( $this->
any() )->method(
'canChangeUser' )
957 ->will( $this->returnValue(
true ) );
958 $provider3->expects( $this->once() )->method(
'refreshSessionInfo' )
959 ->will( $this->returnValue(
false ) );
960 $provider3->expects( $this->
any() )->method(
'__toString' )
961 ->will( $this->returnValue(
'Mock3' ) );
963 TestingAccessWrapper::newFromObject( $manager )->sessionProviders = [
964 (
string)$provider => $provider,
965 (
string)$provider2 => $provider2,
966 (
string)$provider3 => $provider3,
971 'provider' => $provider,
973 'userInfo' => $userInfo
975 $this->assertFalse( $info->isIdSafe(),
'sanity check' );
976 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
977 $this->assertFalse( $info->isIdSafe() );
978 $this->assertSame( [],
$logger->getBuffer() );
981 'provider' => $provider,
982 'userInfo' => $userInfo
984 $this->assertTrue( $info->isIdSafe(),
'sanity check' );
985 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
986 $this->assertTrue( $info->isIdSafe() );
987 $this->assertSame( [],
$logger->getBuffer() );
990 'provider' => $provider2,
992 'userInfo' => $userInfo
994 $this->assertFalse( $info->isIdSafe(),
'sanity check' );
995 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
996 $this->assertTrue( $info->isIdSafe() );
997 $this->assertSame( [],
$logger->getBuffer() );
1001 'provider' => $provider,
1003 'userInfo' => $unverifiedUserInfo
1005 $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
1006 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1007 $this->assertSame( [
1010 'Session "{session}": Unverified user provided and no metadata to auth it',
1018 'userInfo' => $userInfo
1020 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1021 $this->assertSame( [
1022 [ LogLevel::WARNING,
'Session "{session}": Null provider and no metadata' ],
1027 'provider' => $provider,
1030 $this->assertFalse( $info->isIdSafe(),
'sanity check' );
1031 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1033 $this->assertTrue( $info->getUserInfo()->isVerified() );
1034 $this->assertTrue( $info->getUserInfo()->isAnon() );
1035 $this->assertFalse( $info->isIdSafe() );
1036 $this->assertSame( [],
$logger->getBuffer() );
1039 'provider' => $provider2,
1042 $this->assertFalse( $info->isIdSafe(),
'sanity check' );
1043 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1044 $this->assertSame( [
1045 [ LogLevel::INFO,
'Session "{session}": No user provided and provider cannot set user' ]
1050 $this->
store->setRawSession( $id,
true );
1051 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1052 $this->assertSame( [
1053 [ LogLevel::WARNING,
'Session "{session}": Bad data' ],
1057 $this->
store->setRawSession( $id, [
'data' => [] ] );
1058 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1059 $this->assertSame( [
1060 [ LogLevel::WARNING,
'Session "{session}": Bad data structure' ],
1064 $this->
store->deleteSession( $id );
1065 $this->
store->setRawSession( $id, [
'metadata' => $metadata ] );
1066 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1067 $this->assertSame( [
1068 [ LogLevel::WARNING,
'Session "{session}": Bad data structure' ],
1072 $this->
store->setRawSession( $id, [
'metadata' => $metadata,
'data' =>
true ] );
1073 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1074 $this->assertSame( [
1075 [ LogLevel::WARNING,
'Session "{session}": Bad data structure' ],
1079 $this->
store->setRawSession( $id, [
'metadata' =>
true,
'data' => [] ] );
1080 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1081 $this->assertSame( [
1082 [ LogLevel::WARNING,
'Session "{session}": Bad data structure' ],
1086 foreach ( $metadata
as $key => $dummy ) {
1088 unset( $tmp[$key] );
1089 $this->
store->setRawSession( $id, [
'metadata' => $tmp,
'data' => [] ] );
1090 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1091 $this->assertSame( [
1092 [ LogLevel::WARNING,
'Session "{session}": Bad metadata' ],
1098 $this->
store->setRawSession( $id, [
'metadata' => $metadata,
'data' => [] ] );
1100 'provider' => $provider,
1102 'userInfo' => $userInfo
1104 $this->assertFalse( $info->isIdSafe(),
'sanity check' );
1105 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1106 $this->assertTrue( $info->isIdSafe() );
1107 $this->assertSame( [],
$logger->getBuffer() );
1110 $this->
store->setSessionMeta( $id, [
'provider' =>
'Bad' ] + $metadata );
1112 'provider' => $provider,
1114 'userInfo' => $userInfo
1116 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1117 $this->assertSame( [
1118 [ LogLevel::WARNING,
'Session "{session}": Wrong provider Bad !== Mock' ],
1123 $this->
store->setSessionMeta( $id, [
'provider' =>
'Bad' ] + $metadata );
1126 'userInfo' => $userInfo
1128 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1129 $this->assertSame( [
1130 [ LogLevel::WARNING,
'Session "{session}": Unknown provider Bad' ],
1135 $this->
store->setSessionMeta( $id, $metadata );
1138 'userInfo' => $userInfo
1140 $this->assertFalse( $info->isIdSafe(),
'sanity check' );
1141 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1142 $this->assertTrue( $info->isIdSafe() );
1143 $this->assertSame( [],
$logger->getBuffer() );
1146 $this->
store->setSessionMeta( $id, [
'userId' => -1,
'userToken' =>
null ] + $metadata );
1148 'provider' => $provider,
1151 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1152 $this->assertSame( [
1153 [ LogLevel::ERROR,
'Session "{session}": {exception}' ],
1157 $this->
store->setSessionMeta(
1158 $id, [
'userId' => 0,
'userName' =>
'<X>',
'userToken' =>
null ] + $metadata
1161 'provider' => $provider,
1164 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1165 $this->assertSame( [
1166 [ LogLevel::ERROR,
'Session "{session}": {exception}', ],
1171 $this->
store->setSessionMeta(
1172 $id, [
'userId' => $userInfo->getId() + 1,
'userToken' => null ] + $metadata
1175 'provider' => $provider,
1177 'userInfo' => $userInfo
1179 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1180 $this->assertSame( [
1181 [ LogLevel::WARNING,
'Session "{session}": User ID mismatch, {uid_a} !== {uid_b}' ],
1186 $this->
store->setSessionMeta(
1187 $id, [
'userId' => 0,
'userName' =>
'X',
'userToken' =>
null ] + $metadata
1190 'provider' => $provider,
1192 'userInfo' => $userInfo
1194 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1195 $this->assertSame( [
1196 [ LogLevel::WARNING,
'Session "{session}": User name mismatch, {uname_a} !== {uname_b}' ],
1201 $this->
store->setSessionMeta(
1202 $id, [
'userId' => $userInfo->getId(),
'userName' =>
'X',
'userToken' => null ] + $metadata
1205 'provider' => $provider,
1207 'userInfo' => $userInfo
1209 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1210 $this->assertSame( [
1213 'Session "{session}": User ID matched but name didn\'t (rename?), {uname_a} !== {uname_b}'
1219 $this->
store->setSessionMeta(
1220 $id, [
'userId' => 0,
'userName' =>
null,
'userToken' =>
null ] + $metadata
1223 'provider' => $provider,
1225 'userInfo' => $userInfo
1227 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1228 $this->assertSame( [
1231 'Session "{session}": Metadata has an anonymous user, ' .
1232 'but a non-anon user was provided',
1238 $this->
store->setSessionMeta( $id, [
'userToken' =>
null ] + $metadata );
1240 'provider' => $provider,
1243 $this->assertFalse( $info->isIdSafe(),
'sanity check' );
1244 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1245 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1246 $this->assertTrue( $info->isIdSafe() );
1247 $this->assertSame( [],
$logger->getBuffer() );
1250 $this->
store->setSessionMeta(
1251 $id, [
'userId' => 0,
'userName' =>
'UTSysop',
'userToken' =>
null ] + $metadata
1254 'provider' => $provider,
1257 $this->assertFalse( $info->isIdSafe(),
'sanity check' );
1258 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1259 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1260 $this->assertTrue( $info->isIdSafe() );
1261 $this->assertSame( [],
$logger->getBuffer() );
1264 $this->
store->setSessionMeta(
1265 $id, [
'userId' => 0,
'userName' =>
null,
'userToken' =>
null ] + $metadata
1268 'provider' => $provider,
1271 $this->assertFalse( $info->isIdSafe(),
'sanity check' );
1272 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1273 $this->assertTrue( $info->getUserInfo()->isAnon() );
1274 $this->assertTrue( $info->isIdSafe() );
1275 $this->assertSame( [],
$logger->getBuffer() );
1278 $this->
store->setSessionMeta( $id, $metadata );
1280 'provider' => $provider,
1282 'userInfo' => $unverifiedUserInfo
1284 $this->assertFalse( $info->isIdSafe(),
'sanity check' );
1285 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1286 $this->assertTrue( $info->getUserInfo()->isVerified() );
1287 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1288 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1289 $this->assertTrue( $info->isIdSafe() );
1290 $this->assertSame( [],
$logger->getBuffer() );
1293 $this->
store->setSessionMeta( $id, $metadata );
1295 'provider' => $provider,
1297 'userInfo' => $unverifiedUserInfo
1299 $this->assertFalse( $info->isIdSafe(),
'sanity check' );
1300 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1301 $this->assertTrue( $info->getUserInfo()->isVerified() );
1302 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1303 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1304 $this->assertTrue( $info->isIdSafe() );
1305 $this->assertSame( [],
$logger->getBuffer() );
1308 $this->
store->setSessionMeta( $id, [
'userToken' =>
'Bad' ] + $metadata );
1310 'provider' => $provider,
1312 'userInfo' => $userInfo
1314 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1315 $this->assertSame( [
1316 [ LogLevel::WARNING,
'Session "{session}": User token mismatch' ],
1321 $this->
store->setSessionMeta( $id, [
'provider' =>
'Mock2' ] + $metadata );
1323 'provider' => $provider2,
1325 'userInfo' => $userInfo,
1326 'metadata' => [
'Info' ],
1328 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1329 $this->assertSame( [
'Info',
'changed' =>
true ], $info->getProviderMetadata() );
1330 $this->assertSame( [],
$logger->getBuffer() );
1332 $this->
store->setSessionMeta( $id, [
'providerMetadata' => [
'Saved' ] ] + $metadata );
1334 'provider' => $provider,
1336 'userInfo' => $userInfo,
1338 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1339 $this->assertSame( [
'Saved' ], $info->getProviderMetadata() );
1340 $this->assertSame( [],
$logger->getBuffer() );
1343 'provider' => $provider,
1345 'userInfo' => $userInfo,
1346 'metadata' => [
'Info' ],
1348 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1349 $this->assertSame( [
'Merged' ], $info->getProviderMetadata() );
1350 $this->assertSame( [],
$logger->getBuffer() );
1353 'provider' => $provider,
1355 'userInfo' => $userInfo,
1356 'metadata' => [
'Throw' ],
1358 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1359 $this->assertSame( [
1362 'Session "{session}": Metadata merge failed: {exception}',
1368 $this->
store->setSessionMeta( $id, $metadata );
1370 'provider' => $provider,
1373 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1374 $this->assertFalse( $info->wasRemembered() );
1375 $this->assertSame( [],
$logger->getBuffer() );
1377 $this->
store->setSessionMeta( $id, [
'remember' =>
true ] + $metadata );
1379 'provider' => $provider,
1382 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1383 $this->assertTrue( $info->wasRemembered() );
1384 $this->assertSame( [],
$logger->getBuffer() );
1386 $this->
store->setSessionMeta( $id, [
'remember' =>
false ] + $metadata );
1388 'provider' => $provider,
1390 'userInfo' => $userInfo
1392 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1393 $this->assertTrue( $info->wasRemembered() );
1394 $this->assertSame( [],
$logger->getBuffer() );
1397 $this->
store->setSessionMeta( $id, $metadata );
1399 'provider' => $provider,
1401 'userInfo' => $userInfo
1403 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1404 $this->assertFalse( $info->forceHTTPS() );
1405 $this->assertSame( [],
$logger->getBuffer() );
1407 $this->
store->setSessionMeta( $id, [
'forceHTTPS' =>
true ] + $metadata );
1409 'provider' => $provider,
1411 'userInfo' => $userInfo
1413 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1414 $this->assertTrue( $info->forceHTTPS() );
1415 $this->assertSame( [],
$logger->getBuffer() );
1417 $this->
store->setSessionMeta( $id, [
'forceHTTPS' =>
false ] + $metadata );
1419 'provider' => $provider,
1421 'userInfo' => $userInfo,
1422 'forceHTTPS' =>
true
1424 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1425 $this->assertTrue( $info->forceHTTPS() );
1426 $this->assertSame( [],
$logger->getBuffer() );
1429 $this->
store->setSessionMeta( $id, $metadata );
1431 'provider' => $provider,
1433 'userInfo' => $userInfo
1435 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1436 $this->assertFalse( $info->wasPersisted() );
1437 $this->assertSame( [],
$logger->getBuffer() );
1439 $this->
store->setSessionMeta( $id, [
'persisted' =>
true ] + $metadata );
1441 'provider' => $provider,
1443 'userInfo' => $userInfo
1445 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1446 $this->assertTrue( $info->wasPersisted() );
1447 $this->assertSame( [],
$logger->getBuffer() );
1449 $this->
store->setSessionMeta( $id, [
'persisted' =>
false ] + $metadata );
1451 'provider' => $provider,
1453 'userInfo' => $userInfo,
1456 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1457 $this->assertTrue( $info->wasPersisted() );
1458 $this->assertSame( [],
$logger->getBuffer() );
1462 'provider' => $provider3,
1464 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1465 $this->assertSame( [],
$logger->getBuffer() );
1469 $data = [
'foo' => 1 ];
1470 $this->
store->setSession( $id, [
'metadata' => $metadata,
'data' => $data ] );
1472 'provider' => $provider,
1474 'userInfo' => $userInfo
1477 'SessionCheckInfo' => [
function ( &$reason, $i, $r, $m, $d )
use (
1480 $this->assertSame( $info->getId(), $i->getId() );
1481 $this->assertSame( $info->getProvider(), $i->getProvider() );
1482 $this->assertSame( $info->getUserInfo(), $i->getUserInfo() );
1484 $this->assertEquals( $metadata, $m );
1485 $this->assertEquals( $data, $d );
1490 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1492 $this->assertSame( [
1493 [ LogLevel::WARNING,
'Session "{session}": Hook aborted' ],
1499 $this->
store->setSessionMeta( $id, [
'userToken' =>
'Bad' ] + $metadata );
1501 'provider' => $provider,
1503 'userInfo' => $userInfo,
1506 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1507 $this->assertFalse( $this->
store->getSession( $id ) );
1508 $this->assertSame( [
1509 [ LogLevel::WARNING,
'Session "{session}": User token mismatch' ],