tests: More use statements in session tests

Change-Id: I562236f45c0fef5ad2b8fba5d3584b4ddd38a17a
This commit is contained in:
Reedy 2024-02-16 21:57:59 +00:00
parent 2c0790333d
commit c4527ef115
14 changed files with 214 additions and 162 deletions

View file

@ -2,9 +2,11 @@
namespace MediaWiki\Tests\Session;
use InvalidArgumentException;
use MediaWiki\Config\HashConfig;
use MediaWiki\Config\MultiConfig;
use MediaWiki\MainConfigNames;
use MediaWiki\Request\FauxRequest;
use MediaWiki\Session\BotPasswordSessionProvider;
use MediaWiki\Session\Session;
use MediaWiki\Session\SessionInfo;
@ -13,6 +15,8 @@ use MediaWiki\Session\UserInfo;
use MediaWiki\User\BotPassword;
use MediaWikiIntegrationTestCase;
use Psr\Log\LogLevel;
use Psr\Log\NullLogger;
use TestLogger;
use Wikimedia\TestingAccessWrapper;
/**
@ -56,7 +60,7 @@ class BotPasswordSessionProviderTest extends MediaWikiIntegrationTestCase {
}
$manager = new SessionManager( [
'config' => new MultiConfig( [ $this->config, $this->getServiceContainer()->getMainConfig() ] ),
'logger' => new \Psr\Log\NullLogger,
'logger' => new NullLogger,
'store' => new TestBagOStuff,
] );
@ -110,7 +114,7 @@ class BotPasswordSessionProviderTest extends MediaWikiIntegrationTestCase {
try {
$provider = new BotPasswordSessionProvider( $grantsInfo );
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame(
'MediaWiki\\Session\\BotPasswordSessionProvider::__construct: priority must be specified',
$ex->getMessage()
@ -125,7 +129,7 @@ class BotPasswordSessionProviderTest extends MediaWikiIntegrationTestCase {
]
);
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame(
'MediaWiki\\Session\\BotPasswordSessionProvider::__construct: Invalid priority',
$ex->getMessage()
@ -140,7 +144,7 @@ class BotPasswordSessionProviderTest extends MediaWikiIntegrationTestCase {
]
);
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame(
'MediaWiki\\Session\\BotPasswordSessionProvider::__construct: Invalid priority',
$ex->getMessage()
@ -190,7 +194,7 @@ class BotPasswordSessionProviderTest extends MediaWikiIntegrationTestCase {
}
public function testProvideSessionInfo() {
$request = new \MediaWiki\Request\FauxRequest;
$request = new FauxRequest;
$request->setCookie( '_BPsession', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'wgCookiePrefix' );
$provider = $this->getProvider( null, null, false );
@ -206,13 +210,13 @@ class BotPasswordSessionProviderTest extends MediaWikiIntegrationTestCase {
$this->assertNull( $provider->provideSessionInfo( $request ) );
$this->config->set( 'EnableBotPasswords', true );
$this->assertNull( $provider->provideSessionInfo( new \MediaWiki\Request\FauxRequest ) );
$this->assertNull( $provider->provideSessionInfo( new FauxRequest ) );
}
public function testNewSessionInfoForRequest() {
$provider = $this->getProvider();
$user = static::getTestSysop()->getUser();
$request = $this->getMockBuilder( \MediaWiki\Request\FauxRequest::class )
$request = $this->getMockBuilder( FauxRequest::class )
->onlyMethods( [ 'getIP' ] )->getMock();
$request->method( 'getIP' )
->willReturn( '127.0.0.1' );
@ -236,12 +240,12 @@ class BotPasswordSessionProviderTest extends MediaWikiIntegrationTestCase {
}
public function testCheckSessionInfo() {
$logger = new \TestLogger( true );
$logger = new TestLogger( true );
$provider = $this->getProvider();
$this->initProvider( $provider, $logger );
$user = static::getTestSysop()->getUser();
$request = $this->getMockBuilder( \MediaWiki\Request\FauxRequest::class )
$request = $this->getMockBuilder( FauxRequest::class )
->onlyMethods( [ 'getIP' ] )->getMock();
$request->method( 'getIP' )
->willReturn( '127.0.0.1' );
@ -293,7 +297,7 @@ class BotPasswordSessionProviderTest extends MediaWikiIntegrationTestCase {
], $logger->getBuffer() );
$logger->clearBuffer();
$request2 = $this->getMockBuilder( \MediaWiki\Request\FauxRequest::class )
$request2 = $this->getMockBuilder( FauxRequest::class )
->onlyMethods( [ 'getIP' ] )->getMock();
$request2->method( 'getIP' )
->willReturn( '10.0.0.1' );
@ -314,7 +318,7 @@ class BotPasswordSessionProviderTest extends MediaWikiIntegrationTestCase {
}
public function testGetAllowedUserRights() {
$logger = new \TestLogger( true );
$logger = new TestLogger( true );
$provider = $this->getProvider();
$this->initProvider( $provider, $logger );
@ -324,7 +328,7 @@ class BotPasswordSessionProviderTest extends MediaWikiIntegrationTestCase {
try {
$provider->getAllowedUserRights( $backend );
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame( 'Backend\'s provider isn\'t $this', $ex->getMessage() );
}

View file

@ -2,8 +2,12 @@
namespace MediaWiki\Tests\Session;
use ArrayUtils;
use InvalidArgumentException;
use MediaWiki\Config\HashConfig;
use MediaWiki\MainConfigNames;
use MediaWiki\Request\FauxRequest;
use MediaWiki\Request\FauxResponse;
use MediaWiki\Session\CookieSessionProvider;
use MediaWiki\Session\SessionBackend;
use MediaWiki\Session\SessionId;
@ -11,6 +15,7 @@ use MediaWiki\Session\SessionInfo;
use MediaWiki\Session\SessionManager;
use MediaWiki\User\User;
use MediaWikiIntegrationTestCase;
use Message;
use Psr\Log\LogLevel;
use Psr\Log\NullLogger;
use TestLogger;
@ -53,7 +58,7 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
try {
new CookieSessionProvider();
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame(
'MediaWiki\\Session\\CookieSessionProvider::__construct: priority must be specified',
$ex->getMessage()
@ -63,7 +68,7 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
try {
new CookieSessionProvider( [ 'priority' => 'foo' ] );
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame(
'MediaWiki\\Session\\CookieSessionProvider::__construct: Invalid priority',
$ex->getMessage()
@ -72,7 +77,7 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
try {
new CookieSessionProvider( [ 'priority' => SessionInfo::MIN_PRIORITY - 1 ] );
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame(
'MediaWiki\\Session\\CookieSessionProvider::__construct: Invalid priority',
$ex->getMessage()
@ -81,7 +86,7 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
try {
new CookieSessionProvider( [ 'priority' => SessionInfo::MAX_PRIORITY + 1 ] );
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame(
'MediaWiki\\Session\\CookieSessionProvider::__construct: Invalid priority',
$ex->getMessage()
@ -91,7 +96,7 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
try {
new CookieSessionProvider( [ 'priority' => 1, 'cookieOptions' => null ] );
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame(
'MediaWiki\\Session\\CookieSessionProvider::__construct: cookieOptions must be an array',
$ex->getMessage()
@ -175,7 +180,7 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
);
$msg = $provider->whyNoSession();
$this->assertInstanceOf( \Message::class, $msg );
$this->assertInstanceOf( Message::class, $msg );
$this->assertSame( 'sessionprovider-nocookies', $msg->getKey() );
}
@ -196,14 +201,14 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
$sessionId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
// No data
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$info = $provider->provideSessionInfo( $request );
$this->assertNull( $info );
$this->assertSame( [], $logger->getBuffer() );
$logger->clearBuffer();
// Session key only
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$request->setCookies( [
'session' => $sessionId,
], '' );
@ -224,7 +229,7 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
$logger->clearBuffer();
// User, no session key
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$request->setCookies( [
'xUserID' => $id,
'xToken' => $token,
@ -241,7 +246,7 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
$logger->clearBuffer();
// User and session key
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$request->setCookies( [
'session' => $sessionId,
'xUserID' => $id,
@ -259,7 +264,7 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
$logger->clearBuffer();
// User with bad token
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$request->setCookies( [
'session' => $sessionId,
'xUserID' => $id,
@ -276,7 +281,7 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
$logger->clearBuffer();
// User id with no token
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$request->setCookies( [
'session' => $sessionId,
'xUserID' => $id,
@ -293,7 +298,7 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
$this->assertSame( [], $logger->getBuffer() );
$logger->clearBuffer();
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$request->setCookies( [
'xUserID' => $id,
], '' );
@ -303,7 +308,7 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
$logger->clearBuffer();
// User and session key, with forceHTTPS flag
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$request->setCookies( [
'session' => $sessionId,
'xUserID' => $id,
@ -322,7 +327,7 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
$logger->clearBuffer();
// Invalid user id
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$request->setCookies( [
'session' => $sessionId,
'xUserID' => '-1',
@ -333,7 +338,7 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
$logger->clearBuffer();
// User id with matching name
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$request->setCookies( [
'session' => $sessionId,
'xUserID' => $id,
@ -352,7 +357,7 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
$logger->clearBuffer();
// User id with wrong name
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$request->setCookies( [
'session' => $sessionId,
'xUserID' => $id,
@ -393,7 +398,7 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
$provider, null, $this->getConfig(), null, null, $this->getServiceContainer()->getUserNameUtils()
);
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$this->assertNull( $provider->suggestLoginUsername( $request ) );
$request->setCookies( [
@ -442,7 +447,7 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
$backend->setUser( $anon );
$backend->setRememberUser( true );
$backend->setForceHTTPS( false );
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$provider->persistSession( $backend, $request );
$this->assertSame( $sessionId, $request->response()->getCookie( 'MySessionName' ) );
$this->assertSame( '', $request->response()->getCookie( 'xUserID' ) );
@ -459,7 +464,7 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
$backend->setUser( $user );
$backend->setRememberUser( false );
$backend->setForceHTTPS( false );
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$provider->persistSession( $backend, $request );
$this->assertSame( $sessionId, $request->response()->getCookie( 'MySessionName' ) );
$this->assertSame( (string)$user->getId(), $request->response()->getCookie( 'xUserID' ) );
@ -476,7 +481,7 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
$backend->setUser( $user );
$backend->setRememberUser( true );
$backend->setForceHTTPS( true );
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$provider->persistSession( $backend, $request );
$this->assertSame( $sessionId, $request->response()->getCookie( 'MySessionName' ) );
$this->assertSame( (string)$user->getId(), $request->response()->getCookie( 'xUserID' ) );
@ -534,7 +539,7 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
$backend->setUser( $user );
$backend->setRememberUser( $remember );
$backend->setForceHTTPS( $secure );
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$time = time();
$provider->persistSession( $backend, $request );
@ -586,7 +591,7 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
}
public static function provideCookieData() {
return \ArrayUtils::cartesianProduct(
return ArrayUtils::cartesianProduct(
[ false, true ], // $secure
[ false, true ], // $remember
[ false, true ] // $forceHTTPS
@ -594,14 +599,14 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
}
protected function getSentRequest() {
$sentResponse = $this->getMockBuilder( \MediaWiki\Request\FauxResponse::class )
$sentResponse = $this->getMockBuilder( FauxResponse::class )
->onlyMethods( [ 'headersSent', 'setCookie', 'header' ] )->getMock();
$sentResponse->method( 'headersSent' )
->willReturn( true );
$sentResponse->expects( $this->never() )->method( 'setCookie' );
$sentResponse->expects( $this->never() )->method( 'header' );
$sentRequest = $this->getMockBuilder( \MediaWiki\Request\FauxRequest::class )
$sentRequest = $this->getMockBuilder( FauxRequest::class )
->onlyMethods( [ 'response' ] )->getMock();
$sentRequest->method( 'response' )
->willReturn( $sentResponse );
@ -618,7 +623,7 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
$provider, null, $this->getConfig(), SessionManager::singleton(), $this->createHookContainer()
);
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$provider->unpersistSession( $request );
$this->assertSame( '', $request->response()->getCookie( 'MySessionName' ) );
$this->assertSame( '', $request->response()->getCookie( 'xUserID' ) );
@ -644,17 +649,17 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
$t2 = time() - 86400 * 2;
// Set it
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$providerPriv->setLoggedOutCookie( $t1, $request );
$this->assertSame( (string)$t1, $request->response()->getCookie( 'xLoggedOut' ) );
// Too old
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$providerPriv->setLoggedOutCookie( $t2, $request );
$this->assertSame( null, $request->response()->getCookie( 'xLoggedOut' ) );
// Don't reset if it's already set
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$request->setCookies( [
'xLoggedOut' => $t1,
], '' );
@ -673,7 +678,7 @@ class CookieSessionProviderTest extends MediaWikiIntegrationTestCase {
);
$provider = TestingAccessWrapper::newFromObject( $provider );
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$request->setCookies( [
'xFoo' => 'foo!',
'xBar' => 'deleted',

View file

@ -2,8 +2,13 @@
namespace MediaWiki\Tests\Session;
use ArrayUtils;
use BadMethodCallException;
use InvalidArgumentException;
use MediaWiki\Config\HashConfig;
use MediaWiki\MainConfigNames;
use MediaWiki\Request\FauxRequest;
use MediaWiki\Request\FauxResponse;
use MediaWiki\Session\ImmutableSessionProviderWithCookie;
use MediaWiki\Session\SessionBackend;
use MediaWiki\Session\SessionId;
@ -11,6 +16,7 @@ use MediaWiki\Session\SessionInfo;
use MediaWiki\Session\SessionManager;
use MediaWiki\Session\UserInfo;
use MediaWikiIntegrationTestCase;
use Message;
use Psr\Log\NullLogger;
use TestLogger;
use Wikimedia\TestingAccessWrapper;
@ -68,7 +74,7 @@ class ImmutableSessionProviderWithCookieTest extends MediaWikiIntegrationTestCas
] ] )
->getMockForAbstractClass();
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame(
'sessionCookieName must be a string',
$ex->getMessage()
@ -82,7 +88,7 @@ class ImmutableSessionProviderWithCookieTest extends MediaWikiIntegrationTestCas
] ] )
->getMockForAbstractClass();
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame(
'sessionCookieOptions must be an array',
$ex->getMessage()
@ -100,7 +106,7 @@ class ImmutableSessionProviderWithCookieTest extends MediaWikiIntegrationTestCas
$this->assertFalse( $provider->canChangeUser() );
$msg = $provider->whyNoSession();
$this->assertInstanceOf( \Message::class, $msg );
$this->assertInstanceOf( Message::class, $msg );
$this->assertSame( 'sessionprovider-nocookies', $msg->getKey() );
}
@ -120,7 +126,7 @@ class ImmutableSessionProviderWithCookieTest extends MediaWikiIntegrationTestCas
public function testGetSessionIdFromCookie() {
$this->overrideConfigValue( MainConfigNames::CookiePrefix, 'wgCookiePrefix' );
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$request->setCookies( [
'' => 'empty---------------------------',
'Foo' => 'foo-----------------------------',
@ -133,7 +139,7 @@ class ImmutableSessionProviderWithCookieTest extends MediaWikiIntegrationTestCas
try {
$provider->getSessionIdFromCookie( $request );
$this->fail( 'Expected exception not thrown' );
} catch ( \BadMethodCallException $ex ) {
} catch ( BadMethodCallException $ex ) {
$this->assertSame(
'MediaWiki\\Session\\ImmutableSessionProviderWithCookie::getSessionIdFromCookie ' .
'may not be called when $this->sessionCookieName === null',
@ -167,7 +173,7 @@ class ImmutableSessionProviderWithCookieTest extends MediaWikiIntegrationTestCas
}
protected function getSentRequest() {
$sentResponse = $this->getMockBuilder( \MediaWiki\Request\FauxResponse::class )
$sentResponse = $this->getMockBuilder( FauxResponse::class )
->onlyMethods( [ 'headersSent', 'setCookie', 'header' ] )
->getMock();
$sentResponse->method( 'headersSent' )
@ -175,7 +181,7 @@ class ImmutableSessionProviderWithCookieTest extends MediaWikiIntegrationTestCas
$sentResponse->expects( $this->never() )->method( 'setCookie' );
$sentResponse->expects( $this->never() )->method( 'header' );
$sentRequest = $this->getMockBuilder( \MediaWiki\Request\FauxRequest::class )
$sentRequest = $this->getMockBuilder( FauxRequest::class )
->onlyMethods( [ 'response' ] )->getMock();
$sentRequest->method( 'response' )
->willReturn( $sentResponse );
@ -229,13 +235,13 @@ class ImmutableSessionProviderWithCookieTest extends MediaWikiIntegrationTestCas
// No cookie
$priv->sessionCookieName = null;
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$provider->persistSession( $backend, $request );
$this->assertSame( [], $request->response()->getCookies() );
// Cookie
$priv->sessionCookieName = 'session';
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$time = time();
$provider->persistSession( $backend, $request );
@ -282,7 +288,7 @@ class ImmutableSessionProviderWithCookieTest extends MediaWikiIntegrationTestCas
}
public static function providePersistSession() {
return \ArrayUtils::cartesianProduct(
return ArrayUtils::cartesianProduct(
[ false, true ], // $secure
[ false, true ], // $remember
[ false, true ] // $forceHTTPS
@ -295,13 +301,13 @@ class ImmutableSessionProviderWithCookieTest extends MediaWikiIntegrationTestCas
// No cookie
$priv->sessionCookieName = null;
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$provider->unpersistSession( $request );
$this->assertSame( null, $request->response()->getCookie( 'session', '' ) );
// Cookie
$priv->sessionCookieName = 'session';
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$provider->unpersistSession( $request );
$this->assertSame( '', $request->response()->getCookie( 'session', '' ) );

View file

@ -3,12 +3,15 @@
namespace MediaWiki\Tests\Session;
use BadMethodCallException;
use DummySessionProvider;
use MediaWiki\MainConfigNames;
use MediaWiki\Session\PHPSessionHandler;
use MediaWiki\Session\SessionManager;
use MediaWikiIntegrationTestCase;
use Psr\Log\LogLevel;
use TestLogger;
use UnexpectedValueException;
use Wikimedia\ScopedCallback;
use Wikimedia\TestingAccessWrapper;
/**
@ -26,7 +29,7 @@ class PHPSessionHandlerTest extends MediaWikiIntegrationTestCase {
$oldManager = $old->manager;
$oldStore = $old->store;
$oldLogger = $old->logger;
$reset[] = new \Wikimedia\ScopedCallback(
$reset[] = new ScopedCallback(
[ PHPSessionHandler::class, 'install' ],
[ $oldManager, $oldStore, $oldLogger ]
);
@ -42,7 +45,7 @@ class PHPSessionHandlerTest extends MediaWikiIntegrationTestCase {
$staticAccess = TestingAccessWrapper::newFromClass( PHPSessionHandler::class );
$oldValue = $staticAccess->instance;
$reset = new \Wikimedia\ScopedCallback( static function () use ( $staticAccess, $oldValue ) {
$reset = new ScopedCallback( static function () use ( $staticAccess, $oldValue ) {
$staticAccess->instance = $oldValue;
} );
$staticAccess->instance = $handler;
@ -75,7 +78,7 @@ class PHPSessionHandlerTest extends MediaWikiIntegrationTestCase {
$store = new TestBagOStuff();
// Tolerate debug message, anything else is unexpected
$logger = new \TestLogger( false, static function ( $m ) {
$logger = new TestLogger( false, static function ( $m ) {
return preg_match( '/^SessionManager using store/', $m ) ? null : $m;
} );
$manager = new SessionManager( [
@ -109,12 +112,12 @@ class PHPSessionHandlerTest extends MediaWikiIntegrationTestCase {
$reset = $this->getResetter( $staticAccess );
$this->overrideConfigValues( [
MainConfigNames::SessionProviders => [ [ 'class' => \DummySessionProvider::class ] ],
MainConfigNames::SessionProviders => [ [ 'class' => DummySessionProvider::class ] ],
MainConfigNames::ObjectCacheSessionExpiry => 2,
] );
$store = new TestBagOStuff();
$logger = new \TestLogger( true, static function ( $m ) {
$logger = new TestLogger( true, static function ( $m ) {
return (
// Discard all log events starting with expected prefix
preg_match( '/^SessionBackend "\{session\}" /', $m )
@ -128,7 +131,7 @@ class PHPSessionHandlerTest extends MediaWikiIntegrationTestCase {
] );
PHPSessionHandler::install( $manager );
$wrap = TestingAccessWrapper::newFromObject( $staticAccess->instance );
$reset[] = new \Wikimedia\ScopedCallback(
$reset[] = new ScopedCallback(
[ $wrap, 'setEnableFlags' ],
[ $wrap->enable ? ( $wrap->warn ? 'warn' : 'enable' ) : 'disable' ]
);
@ -321,7 +324,7 @@ class PHPSessionHandlerTest extends MediaWikiIntegrationTestCase {
TestingAccessWrapper::newFromObject( $handler )->setEnableFlags( 'disable' );
$oldValue = $staticAccess->instance;
$staticAccess->instance = $handler;
$reset = new \Wikimedia\ScopedCallback( static function () use ( $staticAccess, $oldValue ) {
$reset = new ScopedCallback( static function () use ( $staticAccess, $oldValue ) {
$staticAccess->instance = $oldValue;
} );

View file

@ -2,6 +2,10 @@
namespace MediaWiki\Tests\Session;
use BadMethodCallException;
use CachedBagOStuff;
use DummySessionProvider;
use InvalidArgumentException;
use MediaWiki\Config\Config;
use MediaWiki\Config\HashConfig;
use MediaWiki\Context\RequestContext;
@ -17,6 +21,9 @@ use MediaWiki\Session\SessionProvider;
use MediaWiki\Session\UserInfo;
use MediaWiki\User\User;
use MediaWikiIntegrationTestCase;
use Psr\Log\NullLogger;
use UnexpectedValueException;
use Wikimedia\ScopedCallback;
use Wikimedia\TestingAccessWrapper;
/**
@ -67,7 +74,7 @@ class SessionBackendTest extends MediaWikiIntegrationTestCase {
$this->manager = null;
}
$logger = new \Psr\Log\NullLogger();
$logger = new NullLogger();
if ( !$this->manager ) {
$this->manager = new SessionManager( [
'store' => $this->store,
@ -79,7 +86,7 @@ class SessionBackendTest extends MediaWikiIntegrationTestCase {
$hookContainer = $this->getHookContainer();
if ( !$this->provider ) {
$this->provider = new \DummySessionProvider();
$this->provider = new DummySessionProvider();
}
$this->initProvider( $this->provider, null, $this->config, $this->manager, $hookContainer );
@ -120,12 +127,12 @@ class SessionBackendTest extends MediaWikiIntegrationTestCase {
'idIsSafe' => true,
] );
$id = new SessionId( $info->getId() );
$logger = new \Psr\Log\NullLogger();
$logger = new NullLogger();
$hookContainer = $this->getHookContainer();
try {
new SessionBackend( $id, $info, $this->store, $logger, $hookContainer, 10 );
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame(
'Refusing to create session for unverified user ' . $info->getUserInfo(),
$ex->getMessage()
@ -141,7 +148,7 @@ class SessionBackendTest extends MediaWikiIntegrationTestCase {
try {
new SessionBackend( $id, $info, $this->store, $logger, $hookContainer, 10 );
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame( 'Cannot create session without a provider', $ex->getMessage() );
}
@ -156,7 +163,7 @@ class SessionBackendTest extends MediaWikiIntegrationTestCase {
try {
new SessionBackend( $id, $info, $this->store, $logger, $hookContainer, 10 );
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame(
'SessionId and SessionInfo don\'t match',
$ex->getMessage()
@ -236,13 +243,13 @@ class SessionBackendTest extends MediaWikiIntegrationTestCase {
try {
$backend->getRequest( $index );
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame( 'Invalid session index', $ex->getMessage() );
}
try {
$backend->suggestLoginUsername( $index );
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame( 'Invalid session index', $ex->getMessage() );
}
@ -260,14 +267,14 @@ class SessionBackendTest extends MediaWikiIntegrationTestCase {
try {
$backend->setProviderMetadata( 'foo' );
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame( '$metadata must be an array or null', $ex->getMessage() );
}
try {
$backend->setProviderMetadata( (object)[] );
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame( '$metadata must be an array or null', $ex->getMessage() );
}
@ -291,7 +298,7 @@ class SessionBackendTest extends MediaWikiIntegrationTestCase {
public function testResetId() {
$id = session_id();
$builder = $this->getMockBuilder( \DummySessionProvider::class )
$builder = $this->getMockBuilder( DummySessionProvider::class )
->onlyMethods( [ 'persistsSessionId', 'sessionIdWasReset' ] );
$this->provider = $builder->getMock();
@ -327,7 +334,7 @@ class SessionBackendTest extends MediaWikiIntegrationTestCase {
}
public function testPersist() {
$this->provider = $this->getMockBuilder( \DummySessionProvider::class )
$this->provider = $this->getMockBuilder( DummySessionProvider::class )
->onlyMethods( [ 'persistSession' ] )->getMock();
$this->provider->expects( $this->once() )->method( 'persistSession' );
$backend = $this->getBackend();
@ -347,12 +354,12 @@ class SessionBackendTest extends MediaWikiIntegrationTestCase {
}
public function testUnpersist() {
$this->provider = $this->getMockBuilder( \DummySessionProvider::class )
$this->provider = $this->getMockBuilder( DummySessionProvider::class )
->onlyMethods( [ 'unpersistSession' ] )->getMock();
$this->provider->expects( $this->once() )->method( 'unpersistSession' );
$backend = $this->getBackend();
$wrap = TestingAccessWrapper::newFromObject( $backend );
$wrap->store = new \CachedBagOStuff( $this->store );
$wrap->store = new CachedBagOStuff( $this->store );
$wrap->persist = true;
$wrap->dataDirty = true;
@ -400,7 +407,7 @@ class SessionBackendTest extends MediaWikiIntegrationTestCase {
public function testSetUser() {
$user = static::getTestSysop()->getUser();
$this->provider = $this->getMockBuilder( \DummySessionProvider::class )
$this->provider = $this->getMockBuilder( DummySessionProvider::class )
->onlyMethods( [ 'canChangeUser' ] )->getMock();
$this->provider->method( 'canChangeUser' )
->willReturn( false );
@ -409,7 +416,7 @@ class SessionBackendTest extends MediaWikiIntegrationTestCase {
try {
$backend->setUser( $user );
$this->fail( 'Expected exception not thrown' );
} catch ( \BadMethodCallException $ex ) {
} catch ( BadMethodCallException $ex ) {
$this->assertSame(
'Cannot set user on this session; check $session->canSetUser() first',
$ex->getMessage()
@ -503,7 +510,7 @@ class SessionBackendTest extends MediaWikiIntegrationTestCase {
// Save happens when delay is consumed
$this->onSessionMetadataCalled = false;
$priv->metaDirty = true;
\Wikimedia\ScopedCallback::consume( $delay );
ScopedCallback::consume( $delay );
$this->assertTrue( $this->onSessionMetadataCalled );
// Test multiple delays
@ -514,11 +521,11 @@ class SessionBackendTest extends MediaWikiIntegrationTestCase {
$priv->metaDirty = true;
$priv->autosave();
$this->assertFalse( $this->onSessionMetadataCalled );
\Wikimedia\ScopedCallback::consume( $delay3 );
ScopedCallback::consume( $delay3 );
$this->assertFalse( $this->onSessionMetadataCalled );
\Wikimedia\ScopedCallback::consume( $delay1 );
ScopedCallback::consume( $delay1 );
$this->assertFalse( $this->onSessionMetadataCalled );
\Wikimedia\ScopedCallback::consume( $delay2 );
ScopedCallback::consume( $delay2 );
$this->assertTrue( $this->onSessionMetadataCalled );
}
@ -531,7 +538,7 @@ class SessionBackendTest extends MediaWikiIntegrationTestCase {
->onlyMethods( [ 'onSessionMetadata' ] )->getMock();
$neverHook->expects( $this->never() )->method( 'onSessionMetadata' );
$builder = $this->getMockBuilder( \DummySessionProvider::class )
$builder = $this->getMockBuilder( DummySessionProvider::class )
->onlyMethods( [ 'persistSession', 'unpersistSession' ] );
$neverProvider = $builder->getMock();
@ -749,7 +756,7 @@ class SessionBackendTest extends MediaWikiIntegrationTestCase {
try {
$backend->save();
$this->fail( 'Expected exception not thrown' );
} catch ( \UnexpectedValueException $ex ) {
} catch ( UnexpectedValueException $ex ) {
$this->assertSame(
'SessionMetadata hook changed metadata key "userId"',
$ex->getMessage()
@ -779,7 +786,7 @@ class SessionBackendTest extends MediaWikiIntegrationTestCase {
$testData = [ 'foo' => 'foo!', 'bar', [ 'baz', null ] ];
// Not persistent
$this->provider = $this->getMockBuilder( \DummySessionProvider::class )
$this->provider = $this->getMockBuilder( DummySessionProvider::class )
->onlyMethods( [ 'persistSession' ] )->getMock();
$this->provider->expects( $this->never() )->method( 'persistSession' );
$this->onSessionMetadataCalled = false;
@ -805,7 +812,7 @@ class SessionBackendTest extends MediaWikiIntegrationTestCase {
$this->assertNotEquals( 0, $wrap->expires );
// Persistent
$this->provider = $this->getMockBuilder( \DummySessionProvider::class )
$this->provider = $this->getMockBuilder( DummySessionProvider::class )
->onlyMethods( [ 'persistSession' ] )->getMock();
$this->provider->expects( $this->atLeastOnce() )->method( 'persistSession' );
$this->onSessionMetadataCalled = false;
@ -832,7 +839,7 @@ class SessionBackendTest extends MediaWikiIntegrationTestCase {
$this->assertNotEquals( 0, $wrap->expires );
// Not persistent, not expiring
$this->provider = $this->getMockBuilder( \DummySessionProvider::class )
$this->provider = $this->getMockBuilder( DummySessionProvider::class )
->onlyMethods( [ 'persistSession' ] )->getMock();
$this->provider->expects( $this->never() )->method( 'persistSession' );
$this->onSessionMetadataCalled = false;
@ -865,7 +872,7 @@ class SessionBackendTest extends MediaWikiIntegrationTestCase {
if ( !PHPSessionHandler::isEnabled() ) {
$staticAccess = TestingAccessWrapper::newFromClass( PHPSessionHandler::class );
$handler = TestingAccessWrapper::newFromObject( $staticAccess->instance );
$resetHandler = new \Wikimedia\ScopedCallback( static function () use ( $handler ) {
$resetHandler = new ScopedCallback( static function () use ( $handler ) {
session_write_close();
$handler->enable = false;
} );
@ -904,7 +911,7 @@ class SessionBackendTest extends MediaWikiIntegrationTestCase {
if ( !PHPSessionHandler::isEnabled() ) {
$staticAccess = TestingAccessWrapper::newFromClass( PHPSessionHandler::class );
$handler = TestingAccessWrapper::newFromObject( $staticAccess->instance );
$resetHandler = new \Wikimedia\ScopedCallback( static function () use ( $handler ) {
$resetHandler = new ScopedCallback( static function () use ( $handler ) {
session_write_close();
$handler->enable = false;
} );
@ -939,7 +946,7 @@ class SessionBackendTest extends MediaWikiIntegrationTestCase {
if ( !PHPSessionHandler::isEnabled() ) {
$staticAccess = TestingAccessWrapper::newFromClass( PHPSessionHandler::class );
$handler = TestingAccessWrapper::newFromObject( $staticAccess->instance );
$resetHandler = new \Wikimedia\ScopedCallback( static function () use ( $handler ) {
$resetHandler = new ScopedCallback( static function () use ( $handler ) {
session_write_close();
$handler->enable = false;
} );
@ -971,7 +978,7 @@ class SessionBackendTest extends MediaWikiIntegrationTestCase {
}
public function testGetAllowedUserRights() {
$this->provider = $this->getMockBuilder( \DummySessionProvider::class )
$this->provider = $this->getMockBuilder( DummySessionProvider::class )
->onlyMethods( [ 'getAllowedUserRights' ] )
->getMock();
$this->provider->method( 'getAllowedUserRights' )

View file

@ -2,11 +2,13 @@
namespace MediaWiki\Tests\Session;
use InvalidArgumentException;
use MediaWiki\Session\SessionInfo;
use MediaWiki\Session\SessionManager;
use MediaWiki\Session\SessionProvider;
use MediaWiki\Session\UserInfo;
use MediaWikiIntegrationTestCase;
use stdClass;
/**
* @group Session
@ -25,43 +27,43 @@ class SessionInfoTest extends MediaWikiIntegrationTestCase {
try {
new SessionInfo( SessionInfo::MIN_PRIORITY - 1, [] );
$this->fail( 'Expected exception not thrown', 'priority < min' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame( 'Invalid priority', $ex->getMessage(), 'priority < min' );
}
try {
new SessionInfo( SessionInfo::MAX_PRIORITY + 1, [] );
$this->fail( 'Expected exception not thrown', 'priority > max' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame( 'Invalid priority', $ex->getMessage(), 'priority > max' );
}
try {
new SessionInfo( SessionInfo::MIN_PRIORITY, [ 'id' => 'ABC?' ] );
$this->fail( 'Expected exception not thrown', 'bad session ID' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame( 'Invalid session ID', $ex->getMessage(), 'bad session ID' );
}
try {
new SessionInfo( SessionInfo::MIN_PRIORITY, [ 'userInfo' => new \stdClass ] );
new SessionInfo( SessionInfo::MIN_PRIORITY, [ 'userInfo' => new stdClass ] );
$this->fail( 'Expected exception not thrown', 'bad userInfo' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame( 'Invalid userInfo', $ex->getMessage(), 'bad userInfo' );
}
try {
new SessionInfo( SessionInfo::MIN_PRIORITY, [] );
$this->fail( 'Expected exception not thrown', 'no provider, no id' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame( 'Must supply an ID when no provider is given', $ex->getMessage(),
'no provider, no id' );
}
try {
new SessionInfo( SessionInfo::MIN_PRIORITY, [ 'copyFrom' => new \stdClass ] );
new SessionInfo( SessionInfo::MIN_PRIORITY, [ 'copyFrom' => new stdClass ] );
$this->fail( 'Expected exception not thrown', 'bad copyFrom' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame( 'Invalid copyFrom', $ex->getMessage(),
'bad copyFrom' );
}
@ -96,7 +98,7 @@ class SessionInfoTest extends MediaWikiIntegrationTestCase {
'metadata' => 'foo',
] );
$this->fail( 'Expected exception not thrown', 'bad metadata' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame( 'Invalid metadata', $ex->getMessage(), 'bad metadata' );
}

View file

@ -2,9 +2,13 @@
namespace MediaWiki\Tests\Session;
use DummySessionProvider;
use InvalidArgumentException;
use MediaWiki\Config\HashConfig;
use MediaWiki\Context\RequestContext;
use MediaWiki\MainConfigNames;
use MediaWiki\Request\FauxRequest;
use MediaWiki\Request\ProxyLookup;
use MediaWiki\Session\CookieSessionProvider;
use MediaWiki\Session\MetadataMergeException;
use MediaWiki\Session\PHPSessionHandler;
@ -14,9 +18,16 @@ use MediaWiki\Session\SessionManager;
use MediaWiki\Session\SessionOverflowException;
use MediaWiki\Session\SessionProvider;
use MediaWiki\Session\UserInfo;
use MediaWiki\Utils\MWTimestamp;
use MediaWikiIntegrationTestCase;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Psr\Log\NullLogger;
use ReflectionClass;
use stdClass;
use TestLogger;
use UnexpectedValueException;
use Wikimedia\ScopedCallback;
use Wikimedia\TestingAccessWrapper;
/**
@ -30,7 +41,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
/** @var HashConfig */
private $config;
/** @var \TestLogger */
/** @var TestLogger */
private $logger;
/** @var TestBagOStuff */
@ -45,10 +56,10 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
MainConfigNames::SessionCacheType => $cacheType,
MainConfigNames::ObjectCacheSessionExpiry => 100,
MainConfigNames::SessionProviders => [
[ 'class' => \DummySessionProvider::class ],
[ 'class' => DummySessionProvider::class ],
]
] );
$this->logger = new \TestLogger( false, static function ( $m ) {
$this->logger = new TestLogger( false, static function ( $m ) {
return ( str_starts_with( $m, 'SessionBackend ' )
|| str_starts_with( $m, 'SessionManager using store ' )
// These were added for T264793 and behave somewhat erratically, not worth testing
@ -87,7 +98,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
$staticAccess = TestingAccessWrapper::newFromClass( PHPSessionHandler::class );
$handler = TestingAccessWrapper::newFromObject( $staticAccess->instance );
$oldEnable = $handler->enable;
$reset[] = new \Wikimedia\ScopedCallback( static function () use ( $handler, $oldEnable ) {
$reset[] = new ScopedCallback( static function () use ( $handler, $oldEnable ) {
if ( $handler->enable ) {
session_write_close();
}
@ -96,7 +107,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
$reset[] = TestUtils::setSessionManagerSingleton( $this->getManager() );
$handler->enable = true;
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$context->setRequest( $request );
$id = $request->getSession()->getId();
@ -112,7 +123,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
session_write_close();
$handler->enable = false;
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$context->setRequest( $request );
$id = $request->getSession()->getId();
@ -146,9 +157,9 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
'store' => '$options[\'store\'] must be an instance of BagOStuff',
] as $key => $error ) {
try {
new SessionManager( [ $key => new \stdClass ] );
new SessionManager( [ $key => new stdClass ] );
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame( $error, $ex->getMessage() );
}
}
@ -156,7 +167,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
public function testGetSessionForRequest() {
$manager = $this->getManager();
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$requestUnpersist1 = false;
$requestUnpersist2 = false;
$requestInfo1 = null;
@ -166,7 +177,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
$id2 = '';
$idEmpty = 'empty-session-------------------';
$providerBuilder = $this->getMockBuilder( \DummySessionProvider::class )
$providerBuilder = $this->getMockBuilder( DummySessionProvider::class )
->onlyMethods(
[ 'provideSessionInfo', 'newSessionInfo', '__toString', 'describe', 'unpersistSession' ]
);
@ -300,7 +311,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
try {
$manager->getSessionForRequest( $request );
$this->fail( 'Expcected exception not thrown' );
} catch ( \UnexpectedValueException $ex ) {
} catch ( UnexpectedValueException $ex ) {
$this->assertSame(
'Provider1 returned session info for a different provider: ' . $requestInfo1,
$ex->getMessage()
@ -377,7 +388,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
try {
$manager->getSessionById( 'bad' );
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame( 'Invalid session ID', $ex->getMessage() );
}
@ -424,7 +435,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
// Failure to create an empty session
$manager = $this->getManager();
$provider = $this->getMockBuilder( \DummySessionProvider::class )
$provider = $this->getMockBuilder( DummySessionProvider::class )
->onlyMethods( [ 'provideSessionInfo', 'newSessionInfo', '__toString' ] )
->getMock();
$provider->method( 'provideSessionInfo' )
@ -448,7 +459,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
$manager = $this->getManager();
$pmanager = TestingAccessWrapper::newFromObject( $manager );
$providerBuilder = $this->getMockBuilder( \DummySessionProvider::class )
$providerBuilder = $this->getMockBuilder( DummySessionProvider::class )
->onlyMethods( [ 'provideSessionInfo', 'newSessionInfo', '__toString' ] );
$expectId = null;
@ -493,7 +504,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
try {
$manager->getEmptySession();
$this->fail( 'Expected exception not thrown' );
} catch ( \UnexpectedValueException $ex ) {
} catch ( UnexpectedValueException $ex ) {
$this->assertSame(
'No provider could provide an empty session!',
$ex->getMessage()
@ -538,7 +549,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
try {
$pmanager->getEmptySessionInternal( null, $expectId );
$this->fail( 'Expected exception not thrown' );
} catch ( \UnexpectedValueException $ex ) {
} catch ( UnexpectedValueException $ex ) {
$this->assertSame(
'MockProvider1 returned empty session info with a wrong id: ' .
"un$expectId != $expectId",
@ -557,7 +568,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
try {
$pmanager->getEmptySessionInternal( null, $expectId );
$this->fail( 'Expected exception not thrown' );
} catch ( \UnexpectedValueException $ex ) {
} catch ( UnexpectedValueException $ex ) {
$this->assertSame(
'MockProvider1 returned empty session info with id flagged unsafe',
$ex->getMessage()
@ -576,7 +587,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
try {
$manager->getEmptySession();
$this->fail( 'Expected exception not thrown' );
} catch ( \UnexpectedValueException $ex ) {
} catch ( UnexpectedValueException $ex ) {
$this->assertSame(
'MockProvider1 returned an empty session info for a different provider: ' . $info1,
$ex->getMessage()
@ -637,7 +648,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
try {
$manager->getEmptySession();
$this->fail( 'Expected exception not thrown' );
} catch ( \UnexpectedValueException $ex ) {
} catch ( UnexpectedValueException $ex ) {
$this->assertStringStartsWith(
'Multiple empty sessions tied for top priority: ',
$ex->getMessage()
@ -648,7 +659,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
try {
$pmanager->getEmptySessionInternal( null, 'bad' );
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame( 'Invalid session ID', $ex->getMessage() );
}
@ -663,7 +674,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
try {
$pmanager->getEmptySessionInternal( null, $expectId );
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame( 'Session ID already exists', $ex->getMessage() );
}
}
@ -672,7 +683,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
$user = $this->getTestSysop()->getUser();
$manager = $this->getManager();
$providerBuilder = $this->getMockBuilder( \DummySessionProvider::class )
$providerBuilder = $this->getMockBuilder( DummySessionProvider::class )
->onlyMethods( [ 'invalidateSessionsForUser', '__toString' ] );
$provider1 = $providerBuilder->getMock();
@ -700,7 +711,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
public function testGetVaryHeaders() {
$manager = $this->getManager();
$providerBuilder = $this->getMockBuilder( \DummySessionProvider::class )
$providerBuilder = $this->getMockBuilder( DummySessionProvider::class )
->onlyMethods( [ 'getVaryHeaders', '__toString' ] );
$provider1 = $providerBuilder->getMock();
@ -744,7 +755,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
public function testGetVaryCookies() {
$manager = $this->getManager();
$providerBuilder = $this->getMockBuilder( \DummySessionProvider::class )
$providerBuilder = $this->getMockBuilder( DummySessionProvider::class )
->onlyMethods( [ 'getVaryCookies', '__toString' ] );
$provider1 = $providerBuilder->getMock();
@ -777,7 +788,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
$manager = TestingAccessWrapper::newFromObject( $realManager );
$this->config->set( 'SessionProviders', [
[ 'class' => \DummySessionProvider::class ],
[ 'class' => DummySessionProvider::class ],
] );
$providers = $manager->getProviders();
$this->assertArrayHasKey( 'DummySessionProvider', $providers );
@ -787,14 +798,14 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
$this->assertSame( $realManager, $provider->getManager() );
$this->config->set( 'SessionProviders', [
[ 'class' => \DummySessionProvider::class ],
[ 'class' => \DummySessionProvider::class ],
[ 'class' => DummySessionProvider::class ],
[ 'class' => DummySessionProvider::class ],
] );
$manager->sessionProviders = null;
try {
$manager->getProviders();
$this->fail( 'Expected exception not thrown' );
} catch ( \UnexpectedValueException $ex ) {
} catch ( UnexpectedValueException $ex ) {
$this->assertSame(
'Duplicate provider name "DummySessionProvider"',
$ex->getMessage()
@ -804,9 +815,9 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
public function testShutdown() {
$manager = TestingAccessWrapper::newFromObject( $this->getManager() );
$manager->setLogger( new \Psr\Log\NullLogger() );
$manager->setLogger( new NullLogger() );
$mock = $this->getMockBuilder( \stdClass::class )
$mock = $this->getMockBuilder( stdClass::class )
->addMethods( [ 'shutdown' ] )->getMock();
$mock->expects( $this->once() )->method( 'shutdown' );
@ -816,7 +827,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
public function testGetSessionFromInfo() {
$manager = TestingAccessWrapper::newFromObject( $this->getManager() );
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
@ -848,7 +859,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
public function testBackendRegistration() {
$manager = $this->getManager();
$session = $manager->getSessionForRequest( new \MediaWiki\Request\FauxRequest );
$session = $manager->getSessionForRequest( new FauxRequest );
$backend = TestingAccessWrapper::newFromObject( $session )->backend;
$sessionId = $session->getSessionId();
$id = (string)$sessionId;
@ -868,7 +879,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
try {
$manager->changeBackendId( $backend );
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame(
'Backend was not registered with this SessionManager', $ex->getMessage()
);
@ -877,7 +888,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
try {
$manager->deregisterSessionBackend( $backend );
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame(
'Backend was not registered with this SessionManager', $ex->getMessage()
);
@ -897,7 +908,7 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
public function testPreventSessionsForUser() {
$manager = $this->getManager();
$providerBuilder = $this->getMockBuilder( \DummySessionProvider::class )
$providerBuilder = $this->getMockBuilder( DummySessionProvider::class )
->onlyMethods( [ 'preventSessionsForUser', '__toString' ] );
$username = 'TestPreventSessionsForUser';
@ -918,12 +929,12 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
public function testLoadSessionInfoFromStore() {
$manager = $this->getManager();
$logger = new \TestLogger( true );
$logger = new TestLogger( true );
$manager->setLogger( $logger );
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
// TestingAccessWrapper can't handle methods with reference arguments, sigh.
$rClass = new \ReflectionClass( $manager );
$rClass = new ReflectionClass( $manager );
$rMethod = $rClass->getMethod( 'loadSessionInfoFromStore' );
$rMethod->setAccessible( true );
$loadSessionInfoFromStore = static function ( &$info ) use ( $rMethod, $manager, $request ) {
@ -1545,16 +1556,16 @@ class SessionManagerTest extends MediaWikiIntegrationTestCase {
public function testLogPotentialSessionLeakage(
$ip, $mwuser, $sessionData, $expectedSessionData, $expectedLogLevel
) {
\MediaWiki\Utils\MWTimestamp::setFakeTime( 1234567 );
MWTimestamp::setFakeTime( 1234567 );
$this->overrideConfigValue( MainConfigNames::SuspiciousIpExpiry, 600 );
$manager = new SessionManager();
$logger = $this->createMock( LoggerInterface::class );
$this->setLogger( 'session-ip', $logger );
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$request->setIP( $ip );
$request->setCookie( 'mwuser-sessionId', $mwuser );
$proxyLookup = $this->createMock( \MediaWiki\Request\ProxyLookup::class );
$proxyLookup = $this->createMock( ProxyLookup::class );
$proxyLookup->method( 'isConfiguredProxy' )->willReturnCallback( static function ( $ip ) {
return $ip === '11.22.33.44';
} );

View file

@ -2,8 +2,11 @@
namespace MediaWiki\Tests\Session;
use BadMethodCallException;
use InvalidArgumentException;
use MediaWiki\Config\HashConfig;
use MediaWiki\MainConfigNames;
use MediaWiki\Request\FauxRequest;
use MediaWiki\Session\MetadataMergeException;
use MediaWiki\Session\SessionInfo;
use MediaWiki\Session\SessionManager;
@ -58,7 +61,7 @@ class SessionProviderTest extends MediaWikiIntegrationTestCase {
$this->assertSame( [], $provider->getVaryHeaders() );
$this->assertSame( [], $provider->getVaryCookies() );
$this->assertSame( null, $provider->suggestLoginUsername( new \MediaWiki\Request\FauxRequest ) );
$this->assertSame( null, $provider->suggestLoginUsername( new FauxRequest ) );
$this->assertSame( get_class( $provider ), (string)$provider );
@ -72,7 +75,7 @@ class SessionProviderTest extends MediaWikiIntegrationTestCase {
'provider' => $provider,
] );
$metadata = [ 'foo' ];
$this->assertTrue( $provider->refreshSessionInfo( $info, new \MediaWiki\Request\FauxRequest, $metadata ) );
$this->assertTrue( $provider->refreshSessionInfo( $info, new FauxRequest, $metadata ) );
$this->assertSame( [ 'foo' ], $metadata );
}
@ -159,7 +162,7 @@ class SessionProviderTest extends MediaWikiIntegrationTestCase {
try {
$provider->preventSessionsForUser( 'Foo' );
$this->fail( 'Expected exception not thrown' );
} catch ( \BadMethodCallException $ex ) {
} catch ( BadMethodCallException $ex ) {
$this->assertSame(
'MediaWiki\\Session\\SessionProvider::preventSessionsForUser must be implemented ' .
'when canChangeUser() is false',
@ -185,7 +188,7 @@ class SessionProviderTest extends MediaWikiIntegrationTestCase {
try {
$priv->hashToSessionId( [] );
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame(
'$data must be a string, array was passed',
$ex->getMessage()
@ -194,7 +197,7 @@ class SessionProviderTest extends MediaWikiIntegrationTestCase {
try {
$priv->hashToSessionId( '', false );
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame(
'$key must be a string or null, boolean was passed',
$ex->getMessage()
@ -220,7 +223,7 @@ class SessionProviderTest extends MediaWikiIntegrationTestCase {
try {
$provider->getAllowedUserRights( $backend );
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame(
'Backend\'s provider isn\'t $this',
$ex->getMessage()

View file

@ -5,6 +5,7 @@ namespace MediaWiki\Tests\Session;
use MediaWiki\User\User;
use MediaWikiIntegrationTestCase;
use Psr\Log\LogLevel;
use TestLogger;
use Wikimedia\TestingAccessWrapper;
/**
@ -61,7 +62,7 @@ class SessionTest extends MediaWikiIntegrationTestCase {
}
public function testSecrets() {
$logger = new \TestLogger;
$logger = new TestLogger;
$session = TestUtils::getDummySession( null, -1, $logger );
// Simple defaulting

View file

@ -8,6 +8,9 @@ use MediaWiki\Session\SessionBackend;
use MediaWiki\Session\SessionManager;
use PHPUnit\Framework\Assert;
use Psr\Log\LoggerInterface;
use ReflectionClass;
use TestLogger;
use Wikimedia\ScopedCallback;
use Wikimedia\TestingAccessWrapper;
/**
@ -18,7 +21,7 @@ class TestUtils {
/**
* Override the singleton for unit testing
* @param SessionManager|null $manager
* @return \Wikimedia\ScopedCallback|null
* @return ScopedCallback|null
*/
public static function setSessionManagerSingleton( SessionManager $manager = null ) {
session_write_close();
@ -40,7 +43,7 @@ class TestUtils {
PHPSessionHandler::install( $manager );
}
return new \Wikimedia\ScopedCallback( static function () use ( $reset, $staticAccess, $oldInstance ) {
return new ScopedCallback( static function () use ( $reset, $staticAccess, $oldInstance ) {
foreach ( $reset as [ $property, $oldValue ] ) {
$staticAccess->$property = $oldValue;
}
@ -57,7 +60,7 @@ class TestUtils {
* fields necessary.
*/
public static function getDummySessionBackend() {
$rc = new \ReflectionClass( SessionBackend::class );
$rc = new ReflectionClass( SessionBackend::class );
if ( !method_exists( $rc, 'newInstanceWithoutConstructor' ) ) {
Assert::markTestSkipped(
'ReflectionClass::newInstanceWithoutConstructor isn\'t available'
@ -65,7 +68,7 @@ class TestUtils {
}
$ret = $rc->newInstanceWithoutConstructor();
TestingAccessWrapper::newFromObject( $ret )->logger = new \TestLogger;
TestingAccessWrapper::newFromObject( $ret )->logger = new TestLogger;
return $ret;
}
@ -79,13 +82,13 @@ class TestUtils {
* @return Session
*/
public static function getDummySession( $backend = null, $index = -1, $logger = null ) {
$rc = new \ReflectionClass( Session::class );
$rc = new ReflectionClass( Session::class );
$session = $rc->newInstanceWithoutConstructor();
$priv = TestingAccessWrapper::newFromObject( $session );
$priv->backend = $backend ?? new DummySessionBackend();
$priv->index = $index;
$priv->logger = $logger ?? new \TestLogger();
$priv->logger = $logger ?? new TestLogger();
return $session;
}

View file

@ -2,6 +2,7 @@
namespace MediaWiki\Tests\Session;
use InvalidArgumentException;
use MediaWiki\Session\UserInfo;
use MediaWiki\User\User;
use MediaWikiIntegrationTestCase;
@ -34,7 +35,7 @@ class UserInfoTest extends MediaWikiIntegrationTestCase {
try {
UserInfo::newFromId( $id );
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame( 'Invalid ID', $ex->getMessage() );
}
@ -68,7 +69,7 @@ class UserInfoTest extends MediaWikiIntegrationTestCase {
try {
UserInfo::newFromName( '<bad name>' );
$this->fail( 'Expected exception not thrown' );
} catch ( \InvalidArgumentException $ex ) {
} catch ( InvalidArgumentException $ex ) {
$this->assertSame( 'Invalid user name', $ex->getMessage() );
}

View file

@ -3,18 +3,20 @@
namespace MediaWiki\Tests\Session;
use MediaWiki\Session\MetadataMergeException;
use MediaWikiUnitTestCase;
use UnexpectedValueException;
/**
* @group Session
* @covers MediaWiki\Session\MetadataMergeException
*/
class MetadataMergeExceptionTest extends \MediaWikiUnitTestCase {
class MetadataMergeExceptionTest extends MediaWikiUnitTestCase {
public function testBasics() {
$data = [ 'foo' => 'bar' ];
$ex = new MetadataMergeException();
$this->assertInstanceOf( \UnexpectedValueException::class, $ex );
$this->assertInstanceOf( UnexpectedValueException::class, $ex );
$this->assertSame( [], $ex->getContext() );
$ex2 = new MetadataMergeException( 'Message', 42, $ex, $data );

View file

@ -3,12 +3,13 @@
namespace MediaWiki\Tests\Session;
use MediaWiki\Session\SessionId;
use MediaWikiUnitTestCase;
/**
* @group Session
* @covers MediaWiki\Session\SessionId
*/
class SessionIdTest extends \MediaWikiUnitTestCase {
class SessionIdTest extends MediaWikiUnitTestCase {
public function testEverything() {
$id = new SessionId( 'foo' );

View file

@ -2,10 +2,13 @@
namespace MediaWiki\Tests\Session;
use MediaWiki\Request\FauxRequest;
use MediaWiki\Session\Session;
use MediaWiki\Session\SessionId;
use MediaWikiUnitTestCase;
use Psr\Log\LogLevel;
use stdClass;
use TestLogger;
use Wikimedia\TestingAccessWrapper;
/**
@ -19,12 +22,12 @@ class SessionUnitTest extends MediaWikiUnitTestCase {
TestingAccessWrapper::newFromObject( $backend )->requests = [ -1 => 'dummy' ];
TestingAccessWrapper::newFromObject( $backend )->id = new SessionId( 'abc' );
$session = new Session( $backend, 42, new \TestLogger );
$session = new Session( $backend, 42, new TestLogger );
$priv = TestingAccessWrapper::newFromObject( $session );
$this->assertSame( $backend, $priv->backend );
$this->assertSame( 42, $priv->index );
$request = new \MediaWiki\Request\FauxRequest();
$request = new FauxRequest();
$priv2 = TestingAccessWrapper::newFromObject( $session->sessionWithRequest( $request ) );
$this->assertSame( $backend, $priv2->backend );
$this->assertNotSame( $priv->index, $priv2->index );
@ -77,7 +80,7 @@ class SessionUnitTest extends MediaWikiUnitTestCase {
[ 'getRequest', [], true ],
[ 'getAllowedUserRights', [], false ],
[ 'canSetUser', [], false ],
[ 'setUser', [ new \stdClass ], false ],
[ 'setUser', [ new stdClass ], false ],
[ 'suggestLoginUsername', [], true ],
[ 'shouldForceHTTPS', [], false ],
[ 'setForceHTTPS', [ true ], false ],
@ -150,7 +153,7 @@ class SessionUnitTest extends MediaWikiUnitTestCase {
}
public function testArrayAccess() {
$logger = new \TestLogger;
$logger = new TestLogger;
$session = TestUtils::getDummySession( null, -1, $logger );
$backend = TestingAccessWrapper::newFromObject( $session )->backend;