3 namespace MediaWiki\Session
;
10 * @covers MediaWiki\Session\SessionProvider
12 class SessionProviderTest
extends MediaWikiTestCase
{
14 public function testBasics() {
15 $manager = new SessionManager();
16 $logger = new \
TestLogger();
17 $config = new \
HashConfig();
19 $provider = $this->getMockForAbstractClass( SessionProvider
::class );
20 $priv = \TestingAccessWrapper
::newFromObject( $provider );
22 $provider->setConfig( $config );
23 $this->assertSame( $config, $priv->config
);
24 $provider->setLogger( $logger );
25 $this->assertSame( $logger, $priv->logger
);
26 $provider->setManager( $manager );
27 $this->assertSame( $manager, $priv->manager
);
28 $this->assertSame( $manager, $provider->getManager() );
30 $provider->invalidateSessionsForUser( new \User
);
32 $this->assertSame( [], $provider->getVaryHeaders() );
33 $this->assertSame( [], $provider->getVaryCookies() );
34 $this->assertSame( null, $provider->suggestLoginUsername( new \FauxRequest
) );
36 $this->assertSame( get_class( $provider ), (string)$provider );
38 $this->assertNull( $provider->whyNoSession() );
40 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
41 'id' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
42 'provider' => $provider,
44 $metadata = [ 'foo' ];
45 $this->assertTrue( $provider->refreshSessionInfo( $info, new \FauxRequest
, $metadata ) );
46 $this->assertSame( [ 'foo' ], $metadata );
50 * @dataProvider provideNewSessionInfo
51 * @param bool $persistId Return value for ->persistsSessionId()
52 * @param bool $persistUser Return value for ->persistsSessionUser()
53 * @param bool $ok Whether a SessionInfo is provided
55 public function testNewSessionInfo( $persistId, $persistUser, $ok ) {
56 $manager = new SessionManager();
58 $provider = $this->getMockBuilder( SessionProvider
::class )
59 ->setMethods( [ 'canChangeUser', 'persistsSessionId' ] )
60 ->getMockForAbstractClass();
61 $provider->expects( $this->any() )->method( 'persistsSessionId' )
62 ->will( $this->returnValue( $persistId ) );
63 $provider->expects( $this->any() )->method( 'canChangeUser' )
64 ->will( $this->returnValue( $persistUser ) );
65 $provider->setManager( $manager );
68 $info = $provider->newSessionInfo();
69 $this->assertNotNull( $info );
70 $this->assertFalse( $info->wasPersisted() );
71 $this->assertTrue( $info->isIdSafe() );
73 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
74 $info = $provider->newSessionInfo( $id );
75 $this->assertNotNull( $info );
76 $this->assertSame( $id, $info->getId() );
77 $this->assertFalse( $info->wasPersisted() );
78 $this->assertTrue( $info->isIdSafe() );
80 $this->assertNull( $provider->newSessionInfo() );
84 public function testMergeMetadata() {
85 $provider = $this->getMockBuilder( SessionProvider
::class )
86 ->getMockForAbstractClass();
89 $provider->mergeMetadata(
90 [ 'foo' => 1, 'baz' => 3 ],
91 [ 'bar' => 2, 'baz' => '3' ]
93 $this->fail( 'Expected exception not thrown' );
94 } catch ( MetadataMergeException
$ex ) {
95 $this->assertSame( 'Key "baz" changed', $ex->getMessage() );
97 [ 'old_value' => 3, 'new_value' => '3' ], $ex->getContext() );
100 $res = $provider->mergeMetadata(
101 [ 'foo' => 1, 'baz' => 3 ],
102 [ 'bar' => 2, 'baz' => 3 ]
104 $this->assertSame( [ 'bar' => 2, 'baz' => 3 ], $res );
107 public static function provideNewSessionInfo() {
109 [ false, false, false ],
110 [ true, false, false ],
111 [ false, true, false ],
112 [ true, true, true ],
116 public function testImmutableSessions() {
117 $provider = $this->getMockBuilder( SessionProvider
::class )
118 ->setMethods( [ 'canChangeUser', 'persistsSessionId' ] )
119 ->getMockForAbstractClass();
120 $provider->expects( $this->any() )->method( 'canChangeUser' )
121 ->will( $this->returnValue( true ) );
122 $provider->preventSessionsForUser( 'Foo' );
124 $provider = $this->getMockBuilder( SessionProvider
::class )
125 ->setMethods( [ 'canChangeUser', 'persistsSessionId' ] )
126 ->getMockForAbstractClass();
127 $provider->expects( $this->any() )->method( 'canChangeUser' )
128 ->will( $this->returnValue( false ) );
130 $provider->preventSessionsForUser( 'Foo' );
131 $this->fail( 'Expected exception not thrown' );
132 } catch ( \BadMethodCallException
$ex ) {
134 'MediaWiki\\Session\\SessionProvider::preventSessionsForUser must be implmented ' .
135 'when canChangeUser() is false',
142 public function testHashToSessionId() {
143 $config = new \
HashConfig( [
144 'SecretKey' => 'Shhh!',
147 $provider = $this->getMockForAbstractClass( SessionProvider
::class,
148 [], 'MockSessionProvider' );
149 $provider->setConfig( $config );
150 $priv = \TestingAccessWrapper
::newFromObject( $provider );
152 $this->assertSame( 'eoq8cb1mg7j30ui5qolafps4hg29k5bb', $priv->hashToSessionId( 'foobar' ) );
153 $this->assertSame( '4do8j7tfld1g8tte9jqp3csfgmulaun9',
154 $priv->hashToSessionId( 'foobar', 'secret' ) );
157 $priv->hashToSessionId( [] );
158 $this->fail( 'Expected exception not thrown' );
159 } catch ( \InvalidArgumentException
$ex ) {
161 '$data must be a string, array was passed',
166 $priv->hashToSessionId( '', false );
167 $this->fail( 'Expected exception not thrown' );
168 } catch ( \InvalidArgumentException
$ex ) {
170 '$key must be a string or null, boolean was passed',
176 public function testDescribe() {
177 $provider = $this->getMockForAbstractClass( SessionProvider
::class,
178 [], 'MockSessionProvider' );
181 'MockSessionProvider sessions',
182 $provider->describe( \Language
::factory( 'en' ) )
186 public function testGetAllowedUserRights() {
187 $provider = $this->getMockForAbstractClass( SessionProvider
::class );
188 $backend = TestUtils
::getDummySessionBackend();
191 $provider->getAllowedUserRights( $backend );
192 $this->fail( 'Expected exception not thrown' );
193 } catch ( \InvalidArgumentException
$ex ) {
195 'Backend\'s provider isn\'t $this',
200 \TestingAccessWrapper
::newFromObject( $backend )->provider
= $provider;
201 $this->assertNull( $provider->getAllowedUserRights( $backend ) );