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( 'MediaWiki\\Session\\SessionProvider' );
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 $this->assertSame( [], $provider->getVaryHeaders() );
31 $this->assertSame( [], $provider->getVaryCookies() );
32 $this->assertSame( null, $provider->suggestLoginUsername( new \FauxRequest
) );
34 $this->assertSame( get_class( $provider ), (string)$provider );
36 $this->assertNull( $provider->whyNoSession() );
38 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
39 'id' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
40 'provider' => $provider,
42 $metadata = [ 'foo' ];
43 $this->assertTrue( $provider->refreshSessionInfo( $info, new \FauxRequest
, $metadata ) );
44 $this->assertSame( [ 'foo' ], $metadata );
48 * @dataProvider provideNewSessionInfo
49 * @param bool $persistId Return value for ->persistsSessionId()
50 * @param bool $persistUser Return value for ->persistsSessionUser()
51 * @param bool $ok Whether a SessionInfo is provided
53 public function testNewSessionInfo( $persistId, $persistUser, $ok ) {
54 $manager = new SessionManager();
56 $provider = $this->getMockBuilder( 'MediaWiki\\Session\\SessionProvider' )
57 ->setMethods( [ 'canChangeUser', 'persistsSessionId' ] )
58 ->getMockForAbstractClass();
59 $provider->expects( $this->any() )->method( 'persistsSessionId' )
60 ->will( $this->returnValue( $persistId ) );
61 $provider->expects( $this->any() )->method( 'canChangeUser' )
62 ->will( $this->returnValue( $persistUser ) );
63 $provider->setManager( $manager );
66 $info = $provider->newSessionInfo();
67 $this->assertNotNull( $info );
68 $this->assertFalse( $info->wasPersisted() );
69 $this->assertTrue( $info->isIdSafe() );
71 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
72 $info = $provider->newSessionInfo( $id );
73 $this->assertNotNull( $info );
74 $this->assertSame( $id, $info->getId() );
75 $this->assertFalse( $info->wasPersisted() );
76 $this->assertTrue( $info->isIdSafe() );
78 $this->assertNull( $provider->newSessionInfo() );
82 public function testMergeMetadata() {
83 $provider = $this->getMockBuilder( 'MediaWiki\\Session\\SessionProvider' )
84 ->getMockForAbstractClass();
87 $provider->mergeMetadata(
88 [ 'foo' => 1, 'baz' => 3 ],
89 [ 'bar' => 2, 'baz' => '3' ]
91 $this->fail( 'Expected exception not thrown' );
92 } catch ( MetadataMergeException
$ex ) {
93 $this->assertSame( 'Key "baz" changed', $ex->getMessage() );
95 [ 'old_value' => 3, 'new_value' => '3' ], $ex->getContext() );
98 $res = $provider->mergeMetadata(
99 [ 'foo' => 1, 'baz' => 3 ],
100 [ 'bar' => 2, 'baz' => 3 ]
102 $this->assertSame( [ 'bar' => 2, 'baz' => 3 ], $res );
105 public static function provideNewSessionInfo() {
107 [ false, false, false ],
108 [ true, false, false ],
109 [ false, true, false ],
110 [ true, true, true ],
114 public function testImmutableSessions() {
115 $provider = $this->getMockBuilder( 'MediaWiki\\Session\\SessionProvider' )
116 ->setMethods( [ 'canChangeUser', 'persistsSessionId' ] )
117 ->getMockForAbstractClass();
118 $provider->expects( $this->any() )->method( 'canChangeUser' )
119 ->will( $this->returnValue( true ) );
120 $provider->preventSessionsForUser( 'Foo' );
122 $provider = $this->getMockBuilder( 'MediaWiki\\Session\\SessionProvider' )
123 ->setMethods( [ 'canChangeUser', 'persistsSessionId' ] )
124 ->getMockForAbstractClass();
125 $provider->expects( $this->any() )->method( 'canChangeUser' )
126 ->will( $this->returnValue( false ) );
128 $provider->preventSessionsForUser( 'Foo' );
129 $this->fail( 'Expected exception not thrown' );
130 } catch ( \BadMethodCallException
$ex ) {
135 public function testHashToSessionId() {
136 $config = new \
HashConfig( [
137 'SecretKey' => 'Shhh!',
140 $provider = $this->getMockForAbstractClass( 'MediaWiki\\Session\\SessionProvider',
141 [], 'MockSessionProvider' );
142 $provider->setConfig( $config );
143 $priv = \TestingAccessWrapper
::newFromObject( $provider );
145 $this->assertSame( 'eoq8cb1mg7j30ui5qolafps4hg29k5bb', $priv->hashToSessionId( 'foobar' ) );
146 $this->assertSame( '4do8j7tfld1g8tte9jqp3csfgmulaun9',
147 $priv->hashToSessionId( 'foobar', 'secret' ) );
150 $priv->hashToSessionId( [] );
151 $this->fail( 'Expected exception not thrown' );
152 } catch ( \InvalidArgumentException
$ex ) {
154 '$data must be a string, array was passed',
159 $priv->hashToSessionId( '', false );
160 $this->fail( 'Expected exception not thrown' );
161 } catch ( \InvalidArgumentException
$ex ) {
163 '$key must be a string or null, boolean was passed',
169 public function testDescribe() {
170 $provider = $this->getMockForAbstractClass( 'MediaWiki\\Session\\SessionProvider',
171 [], 'MockSessionProvider' );
174 'MockSessionProvider sessions',
175 $provider->describe( \Language
::factory( 'en' ) )
179 public function testGetAllowedUserRights() {
180 $provider = $this->getMockForAbstractClass( 'MediaWiki\\Session\\SessionProvider' );
181 $backend = TestUtils
::getDummySessionBackend();
184 $provider->getAllowedUserRights( $backend );
185 $this->fail( 'Expected exception not thrown' );
186 } catch ( \InvalidArgumentException
$ex ) {
188 'Backend\'s provider isn\'t $this',
193 \TestingAccessWrapper
::newFromObject( $backend )->provider
= $provider;
194 $this->assertNull( $provider->getAllowedUserRights( $backend ) );