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( array(), $provider->getVaryHeaders() );
31 $this->assertSame( array(), $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
, array(
39 'id' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
40 'provider' => $provider,
42 $metadata = array( 'foo' );
43 $this->assertTrue( $provider->refreshSessionInfo( $info, new \FauxRequest
, $metadata ) );
44 $this->assertSame( array( '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( array( '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 array( 'foo' => 1, 'baz' => 3 ),
89 array( 'bar' => 2, 'baz' => '3' )
91 $this->fail( 'Expected exception not thrown' );
92 } catch ( \UnexpectedValueException
$ex ) {
93 $this->assertSame( 'Key "baz" changed', $ex->getMessage() );
96 $res = $provider->mergeMetadata(
97 array( 'foo' => 1, 'baz' => 3 ),
98 array( 'bar' => 2, 'baz' => 3 )
100 $this->assertSame( array( 'bar' => 2, 'baz' => 3 ), $res );
103 public static function provideNewSessionInfo() {
105 array( false, false, false ),
106 array( true, false, false ),
107 array( false, true, false ),
108 array( true, true, true ),
112 public function testImmutableSessions() {
113 $provider = $this->getMockBuilder( 'MediaWiki\\Session\\SessionProvider' )
114 ->setMethods( array( 'canChangeUser', 'persistsSessionId' ) )
115 ->getMockForAbstractClass();
116 $provider->expects( $this->any() )->method( 'canChangeUser' )
117 ->will( $this->returnValue( true ) );
118 $provider->preventSessionsForUser( 'Foo' );
120 $provider = $this->getMockBuilder( 'MediaWiki\\Session\\SessionProvider' )
121 ->setMethods( array( 'canChangeUser', 'persistsSessionId' ) )
122 ->getMockForAbstractClass();
123 $provider->expects( $this->any() )->method( 'canChangeUser' )
124 ->will( $this->returnValue( false ) );
126 $provider->preventSessionsForUser( 'Foo' );
127 $this->fail( 'Expected exception not thrown' );
128 } catch ( \BadMethodCallException
$ex ) {
133 public function testHashToSessionId() {
134 $config = new \
HashConfig( array(
135 'SecretKey' => 'Shhh!',
138 $provider = $this->getMockForAbstractClass( 'MediaWiki\\Session\\SessionProvider',
139 array(), 'MockSessionProvider' );
140 $provider->setConfig( $config );
141 $priv = \TestingAccessWrapper
::newFromObject( $provider );
143 $this->assertSame( 'eoq8cb1mg7j30ui5qolafps4hg29k5bb', $priv->hashToSessionId( 'foobar' ) );
144 $this->assertSame( '4do8j7tfld1g8tte9jqp3csfgmulaun9',
145 $priv->hashToSessionId( 'foobar', 'secret' ) );
148 $priv->hashToSessionId( array() );
149 $this->fail( 'Expected exception not thrown' );
150 } catch ( \InvalidArgumentException
$ex ) {
152 '$data must be a string, array was passed',
157 $priv->hashToSessionId( '', false );
158 $this->fail( 'Expected exception not thrown' );
159 } catch ( \InvalidArgumentException
$ex ) {
161 '$key must be a string or null, boolean was passed',
167 public function testDescribe() {
168 $provider = $this->getMockForAbstractClass( 'MediaWiki\\Session\\SessionProvider',
169 array(), 'MockSessionProvider' );
172 'MockSessionProvider sessions',
173 $provider->describe( \Language
::factory( 'en' ) )
177 public function testGetAllowedUserRights() {
178 $provider = $this->getMockForAbstractClass( 'MediaWiki\\Session\\SessionProvider' );
179 $backend = TestUtils
::getDummySessionBackend();
182 $provider->getAllowedUserRights( $backend );
183 $this->fail( 'Expected exception not thrown' );
184 } catch ( \InvalidArgumentException
$ex ) {
186 'Backend\'s provider isn\'t $this',
191 \TestingAccessWrapper
::newFromObject( $backend )->provider
= $provider;
192 $this->assertNull( $provider->getAllowedUserRights( $backend ) );