return true;
}
+ /**
+ * @codeCoverageIgnore
+ */
public function preventSessionsForUser( $username ) {
BotPassword::removeAllPasswordsForUser( $username );
}
public function getAllowedUserRights( SessionBackend $backend ) {
if ( $backend->getProvider() !== $this ) {
- throw new InvalidArgumentException( 'Backend\'s provider isn\'t $this' );
+ throw new \InvalidArgumentException( 'Backend\'s provider isn\'t $this' );
}
$data = $backend->getProviderMetadata();
- if ( $data ) {
+ if ( $data && isset( $data['rights'] ) && is_array( $data['rights'] ) ) {
return $data['rights'];
}
namespace MediaWiki\Session;
+use Exception;
use UnexpectedValueException;
/**
return;
}
+ // @codeCoverageIgnoreStart
if ( defined( 'MW_NO_SESSION_HANDLER' ) ) {
throw new \BadMethodCallException( 'MW_NO_SESSION_HANDLER is defined' );
}
+ // @codeCoverageIgnoreEnd
self::$instance = new self( $manager );
}
public function getVaryHeaders() {
+ // @codeCoverageIgnoreStart
if ( defined( 'MW_NO_SESSION' ) && MW_NO_SESSION !== 'warn' ) {
return [];
}
+ // @codeCoverageIgnoreEnd
if ( $this->varyHeaders === null ) {
$headers = [];
foreach ( $this->getProviders() as $provider ) {
}
public function getVaryCookies() {
+ // @codeCoverageIgnoreStart
if ( defined( 'MW_NO_SESSION' ) && MW_NO_SESSION !== 'warn' ) {
return [];
}
+ // @codeCoverageIgnoreEnd
if ( $this->varyCookies === null ) {
$cookies = [];
foreach ( $this->getProviders() as $provider ) {
}
# Notify AuthPlugin
+ // @codeCoverageIgnoreStart
$tmpUser = $user;
$wgAuth->initUser( $tmpUser, true );
if ( $tmpUser !== $user ) {
$logger->warning( __METHOD__ . ': ' .
get_class( $wgAuth ) . '::initUser() replaced the user object' );
}
+ // @codeCoverageIgnoreEnd
# Notify hooks (e.g. Newuserlog)
\Hooks::run( 'AuthPluginAutoCreate', [ $user ] );
* @return Session
*/
public function getSessionFromInfo( SessionInfo $info, WebRequest $request ) {
+ // @codeCoverageIgnoreStart
if ( defined( 'MW_NO_SESSION' ) ) {
if ( MW_NO_SESSION === 'warn' ) {
// Undocumented safety case for converting existing entry points
throw new \BadMethodCallException( 'Sessions are disabled for this entry point' );
}
}
+ // @codeCoverageIgnoreEnd
$id = $info->getId();
$this->assertSame( [], $logger->getBuffer() );
$this->assertEquals( $dataMD + [ 'rights' => [ 'read' ] ], $metadata );
}
+
+ public function testGetAllowedUserRights() {
+ $logger = new \TestLogger( true );
+ $provider = $this->getProvider();
+ $provider->setLogger( $logger );
+
+ $backend = TestUtils::getDummySessionBackend();
+ $backendPriv = \TestingAccessWrapper::newFromObject( $backend );
+
+ try {
+ $provider->getAllowedUserRights( $backend );
+ $this->fail( 'Expected exception not thrown' );
+ } catch ( \InvalidArgumentException $ex ) {
+ $this->assertSame( 'Backend\'s provider isn\'t $this', $ex->getMessage() );
+ }
+
+ $backendPriv->provider = $provider;
+ $backendPriv->providerMetadata = [ 'rights' => [ 'foo', 'bar', 'baz' ] ];
+ $this->assertSame( [ 'foo', 'bar', 'baz' ], $provider->getAllowedUserRights( $backend ) );
+ $this->assertSame( [], $logger->getBuffer() );
+
+ $backendPriv->providerMetadata = [ 'foo' => 'bar' ];
+ $this->assertSame( [], $provider->getAllowedUserRights( $backend ) );
+ $this->assertSame( [
+ [
+ LogLevel::DEBUG,
+ 'MediaWiki\\Session\\BotPasswordSessionProvider::getAllowedUserRights: ' .
+ 'No provider metadata, returning no rights allowed'
+ ]
+ ], $logger->getBuffer() );
+ $logger->clearBuffer();
+
+ $backendPriv->providerMetadata = [ 'rights' => 'bar' ];
+ $this->assertSame( [], $provider->getAllowedUserRights( $backend ) );
+ $this->assertSame( [
+ [
+ LogLevel::DEBUG,
+ 'MediaWiki\\Session\\BotPasswordSessionProvider::getAllowedUserRights: ' .
+ 'No provider metadata, returning no rights allowed'
+ ]
+ ], $logger->getBuffer() );
+ $logger->clearBuffer();
+
+ $backendPriv->providerMetadata = null;
+ $this->assertSame( [], $provider->getAllowedUserRights( $backend ) );
+ $this->assertSame( [
+ [
+ LogLevel::DEBUG,
+ 'MediaWiki\\Session\\BotPasswordSessionProvider::getAllowedUserRights: ' .
+ 'No provider metadata, returning no rights allowed'
+ ]
+ ], $logger->getBuffer() );
+ $logger->clearBuffer();
+ }
}
public function onUserSetCookies( $user, &$sessionData, &$cookies ) {
}
+ public function testGetCookie() {
+ $provider = new CookieSessionProvider( [
+ 'priority' => 1,
+ 'sessionName' => 'MySessionName',
+ 'cookieOptions' => [ 'prefix' => 'x' ],
+ ] );
+ $provider->setLogger( new \Psr\Log\NullLogger() );
+ $provider->setConfig( $this->getConfig() );
+ $provider->setManager( SessionManager::singleton() );
+ $provider = \TestingAccessWrapper::newFromObject( $provider );
+
+ $request = new \FauxRequest();
+ $request->setCookies( [
+ 'xFoo' => 'foo!',
+ 'xBar' => 'deleted',
+ ], '' );
+ $this->assertSame( 'foo!', $provider->getCookie( $request, 'Foo', 'x' ) );
+ $this->assertNull( $provider->getCookie( $request, 'Bar', 'x' ) );
+ $this->assertNull( $provider->getCookie( $request, 'Baz', 'x' ) );
+ }
+
}
--- /dev/null
+<?php
+
+namespace MediaWiki\Session;
+
+use MediaWikiTestCase;
+
+/**
+ * @group Session
+ * @covers MediaWiki\Session\MetadataMergeException
+ */
+class MetadataMergeExceptionTest extends MediaWikiTestCase {
+
+ public function testBasics() {
+ $data = [ 'foo' => 'bar' ];
+
+ $ex = new MetadataMergeException();
+ $this->assertInstanceOf( 'UnexpectedValueException', $ex );
+ $this->assertSame( [], $ex->getContext() );
+
+ $ex2 = new MetadataMergeException( 'Message', 42, $ex, $data );
+ $this->assertSame( 'Message', $ex2->getMessage() );
+ $this->assertSame( 42, $ex2->getCode() );
+ $this->assertSame( $ex, $ex2->getPrevious() );
+ $this->assertSame( $data, $ex2->getContext() );
+
+ $ex->setContext( $data );
+ $this->assertSame( $data, $ex->getContext() );
+ }
+
+}
$this->assertArrayHasKey( $backend->getId(), $manager->allSessionIds );
}
+ public function testSetProviderMetadata() {
+ $backend = $this->getBackend();
+ $priv = \TestingAccessWrapper::newFromObject( $backend );
+ $priv->providerMetadata = [ 'dummy' ];
+
+ try {
+ $backend->setProviderMetadata( 'foo' );
+ $this->fail( 'Expected exception not thrown' );
+ } 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 ) {
+ $this->assertSame( '$metadata must be an array or null', $ex->getMessage() );
+ }
+
+ $this->assertFalse( $this->store->getSession( self::SESSIONID ), 'sanity check' );
+ $backend->setProviderMetadata( [ 'dummy' ] );
+ $this->assertFalse( $this->store->getSession( self::SESSIONID ) );
+
+ $this->assertFalse( $this->store->getSession( self::SESSIONID ), 'sanity check' );
+ $backend->setProviderMetadata( [ 'test' ] );
+ $this->assertNotFalse( $this->store->getSession( self::SESSIONID ) );
+ $this->assertSame( [ 'test' ], $backend->getProviderMetadata() );
+ $this->store->deleteSession( self::SESSIONID );
+
+ $this->assertFalse( $this->store->getSession( self::SESSIONID ), 'sanity check' );
+ $backend->setProviderMetadata( null );
+ $this->assertNotFalse( $this->store->getSession( self::SESSIONID ) );
+ $this->assertSame( null, $backend->getProviderMetadata() );
+ $this->store->deleteSession( self::SESSIONID );
+ }
+
public function testResetId() {
$id = session_id();
$provider->preventSessionsForUser( 'Foo' );
$this->fail( 'Expected exception not thrown' );
} catch ( \BadMethodCallException $ex ) {
+ $this->assertSame(
+ 'MediaWiki\\Session\\SessionProvider::preventSessionsForUser must be implmented ' .
+ 'when canChangeUser() is false',
+ $ex->getMessage()
+ );
}
}