4cbeeb954c15be9d1e6fd0b3987c3a4c8eb6a3e4
[lhc/web/wiklou.git] / tests / phpunit / includes / session / SessionProviderTest.php
1 <?php
2
3 namespace MediaWiki\Session;
4
5 use MediaWikiTestCase;
6
7 /**
8 * @group Session
9 * @group Database
10 * @covers MediaWiki\Session\SessionProvider
11 */
12 class SessionProviderTest extends MediaWikiTestCase {
13
14 public function testBasics() {
15 $manager = new SessionManager();
16 $logger = new \TestLogger();
17 $config = new \HashConfig();
18
19 $provider = $this->getMockForAbstractClass( SessionProvider::class );
20 $priv = \TestingAccessWrapper::newFromObject( $provider );
21
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() );
29
30 $provider->invalidateSessionsForUser( new \User );
31
32 $this->assertSame( [], $provider->getVaryHeaders() );
33 $this->assertSame( [], $provider->getVaryCookies() );
34 $this->assertSame( null, $provider->suggestLoginUsername( new \FauxRequest ) );
35
36 $this->assertSame( get_class( $provider ), (string)$provider );
37
38 $this->assertNull( $provider->getRememberUserDuration() );
39
40 $this->assertNull( $provider->whyNoSession() );
41
42 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
43 'id' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
44 'provider' => $provider,
45 ] );
46 $metadata = [ 'foo' ];
47 $this->assertTrue( $provider->refreshSessionInfo( $info, new \FauxRequest, $metadata ) );
48 $this->assertSame( [ 'foo' ], $metadata );
49 }
50
51 /**
52 * @dataProvider provideNewSessionInfo
53 * @param bool $persistId Return value for ->persistsSessionId()
54 * @param bool $persistUser Return value for ->persistsSessionUser()
55 * @param bool $ok Whether a SessionInfo is provided
56 */
57 public function testNewSessionInfo( $persistId, $persistUser, $ok ) {
58 $manager = new SessionManager();
59
60 $provider = $this->getMockBuilder( SessionProvider::class )
61 ->setMethods( [ 'canChangeUser', 'persistsSessionId' ] )
62 ->getMockForAbstractClass();
63 $provider->expects( $this->any() )->method( 'persistsSessionId' )
64 ->will( $this->returnValue( $persistId ) );
65 $provider->expects( $this->any() )->method( 'canChangeUser' )
66 ->will( $this->returnValue( $persistUser ) );
67 $provider->setManager( $manager );
68
69 if ( $ok ) {
70 $info = $provider->newSessionInfo();
71 $this->assertNotNull( $info );
72 $this->assertFalse( $info->wasPersisted() );
73 $this->assertTrue( $info->isIdSafe() );
74
75 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
76 $info = $provider->newSessionInfo( $id );
77 $this->assertNotNull( $info );
78 $this->assertSame( $id, $info->getId() );
79 $this->assertFalse( $info->wasPersisted() );
80 $this->assertTrue( $info->isIdSafe() );
81 } else {
82 $this->assertNull( $provider->newSessionInfo() );
83 }
84 }
85
86 public function testMergeMetadata() {
87 $provider = $this->getMockBuilder( SessionProvider::class )
88 ->getMockForAbstractClass();
89
90 try {
91 $provider->mergeMetadata(
92 [ 'foo' => 1, 'baz' => 3 ],
93 [ 'bar' => 2, 'baz' => '3' ]
94 );
95 $this->fail( 'Expected exception not thrown' );
96 } catch ( MetadataMergeException $ex ) {
97 $this->assertSame( 'Key "baz" changed', $ex->getMessage() );
98 $this->assertSame(
99 [ 'old_value' => 3, 'new_value' => '3' ], $ex->getContext() );
100 }
101
102 $res = $provider->mergeMetadata(
103 [ 'foo' => 1, 'baz' => 3 ],
104 [ 'bar' => 2, 'baz' => 3 ]
105 );
106 $this->assertSame( [ 'bar' => 2, 'baz' => 3 ], $res );
107 }
108
109 public static function provideNewSessionInfo() {
110 return [
111 [ false, false, false ],
112 [ true, false, false ],
113 [ false, true, false ],
114 [ true, true, true ],
115 ];
116 }
117
118 public function testImmutableSessions() {
119 $provider = $this->getMockBuilder( SessionProvider::class )
120 ->setMethods( [ 'canChangeUser', 'persistsSessionId' ] )
121 ->getMockForAbstractClass();
122 $provider->expects( $this->any() )->method( 'canChangeUser' )
123 ->will( $this->returnValue( true ) );
124 $provider->preventSessionsForUser( 'Foo' );
125
126 $provider = $this->getMockBuilder( SessionProvider::class )
127 ->setMethods( [ 'canChangeUser', 'persistsSessionId' ] )
128 ->getMockForAbstractClass();
129 $provider->expects( $this->any() )->method( 'canChangeUser' )
130 ->will( $this->returnValue( false ) );
131 try {
132 $provider->preventSessionsForUser( 'Foo' );
133 $this->fail( 'Expected exception not thrown' );
134 } catch ( \BadMethodCallException $ex ) {
135 $this->assertSame(
136 'MediaWiki\\Session\\SessionProvider::preventSessionsForUser must be implmented ' .
137 'when canChangeUser() is false',
138 $ex->getMessage()
139 );
140 }
141
142 }
143
144 public function testHashToSessionId() {
145 $config = new \HashConfig( [
146 'SecretKey' => 'Shhh!',
147 ] );
148
149 $provider = $this->getMockForAbstractClass( SessionProvider::class,
150 [], 'MockSessionProvider' );
151 $provider->setConfig( $config );
152 $priv = \TestingAccessWrapper::newFromObject( $provider );
153
154 $this->assertSame( 'eoq8cb1mg7j30ui5qolafps4hg29k5bb', $priv->hashToSessionId( 'foobar' ) );
155 $this->assertSame( '4do8j7tfld1g8tte9jqp3csfgmulaun9',
156 $priv->hashToSessionId( 'foobar', 'secret' ) );
157
158 try {
159 $priv->hashToSessionId( [] );
160 $this->fail( 'Expected exception not thrown' );
161 } catch ( \InvalidArgumentException $ex ) {
162 $this->assertSame(
163 '$data must be a string, array was passed',
164 $ex->getMessage()
165 );
166 }
167 try {
168 $priv->hashToSessionId( '', false );
169 $this->fail( 'Expected exception not thrown' );
170 } catch ( \InvalidArgumentException $ex ) {
171 $this->assertSame(
172 '$key must be a string or null, boolean was passed',
173 $ex->getMessage()
174 );
175 }
176 }
177
178 public function testDescribe() {
179 $provider = $this->getMockForAbstractClass( SessionProvider::class,
180 [], 'MockSessionProvider' );
181
182 $this->assertSame(
183 'MockSessionProvider sessions',
184 $provider->describe( \Language::factory( 'en' ) )
185 );
186 }
187
188 public function testGetAllowedUserRights() {
189 $provider = $this->getMockForAbstractClass( SessionProvider::class );
190 $backend = TestUtils::getDummySessionBackend();
191
192 try {
193 $provider->getAllowedUserRights( $backend );
194 $this->fail( 'Expected exception not thrown' );
195 } catch ( \InvalidArgumentException $ex ) {
196 $this->assertSame(
197 'Backend\'s provider isn\'t $this',
198 $ex->getMessage()
199 );
200 }
201
202 \TestingAccessWrapper::newFromObject( $backend )->provider = $provider;
203 $this->assertNull( $provider->getAllowedUserRights( $backend ) );
204 }
205
206 }