3 namespace MediaWiki\Tests\Session
;
5 use InvalidArgumentException
;
6 use MediaWiki\Session\SessionInfo
;
7 use MediaWiki\Session\SessionManager
;
8 use MediaWiki\Session\SessionProvider
;
9 use MediaWiki\Session\UserInfo
;
10 use MediaWikiIntegrationTestCase
;
16 * @covers \MediaWiki\Session\SessionInfo
18 class SessionInfoTest
extends MediaWikiIntegrationTestCase
{
19 use SessionProviderTestTrait
;
21 public function testBasics() {
22 $anonInfo = UserInfo
::newAnonymous();
23 $username = 'SessionInfoTestTestBasics';
24 $userInfo = UserInfo
::newFromName( $username, true );
25 $unverifiedUserInfo = UserInfo
::newFromName( $username, false );
28 new SessionInfo( SessionInfo
::MIN_PRIORITY
- 1, [] );
29 $this->fail( 'Expected exception not thrown', 'priority < min' );
30 } catch ( InvalidArgumentException
$ex ) {
31 $this->assertSame( 'Invalid priority', $ex->getMessage(), 'priority < min' );
35 new SessionInfo( SessionInfo
::MAX_PRIORITY +
1, [] );
36 $this->fail( 'Expected exception not thrown', 'priority > max' );
37 } catch ( InvalidArgumentException
$ex ) {
38 $this->assertSame( 'Invalid priority', $ex->getMessage(), 'priority > max' );
42 new SessionInfo( SessionInfo
::MIN_PRIORITY
, [ 'id' => 'ABC?' ] );
43 $this->fail( 'Expected exception not thrown', 'bad session ID' );
44 } catch ( InvalidArgumentException
$ex ) {
45 $this->assertSame( 'Invalid session ID', $ex->getMessage(), 'bad session ID' );
49 new SessionInfo( SessionInfo
::MIN_PRIORITY
, [ 'userInfo' => new stdClass
] );
50 $this->fail( 'Expected exception not thrown', 'bad userInfo' );
51 } catch ( InvalidArgumentException
$ex ) {
52 $this->assertSame( 'Invalid userInfo', $ex->getMessage(), 'bad userInfo' );
56 new SessionInfo( SessionInfo
::MIN_PRIORITY
, [] );
57 $this->fail( 'Expected exception not thrown', 'no provider, no id' );
58 } catch ( InvalidArgumentException
$ex ) {
59 $this->assertSame( 'Must supply an ID when no provider is given', $ex->getMessage(),
60 'no provider, no id' );
64 new SessionInfo( SessionInfo
::MIN_PRIORITY
, [ 'copyFrom' => new stdClass
] );
65 $this->fail( 'Expected exception not thrown', 'bad copyFrom' );
66 } catch ( InvalidArgumentException
$ex ) {
67 $this->assertSame( 'Invalid copyFrom', $ex->getMessage(),
71 $manager = new SessionManager();
72 $provider = $this->getMockBuilder( SessionProvider
::class )
73 ->onlyMethods( [ 'persistsSessionId', 'canChangeUser', '__toString' ] )
74 ->getMockForAbstractClass();
75 $this->initProvider( $provider, null, null, $manager );
76 $provider->method( 'persistsSessionId' )
78 $provider->method( 'canChangeUser' )
80 $provider->method( '__toString' )
81 ->willReturn( 'Mock' );
83 $provider2 = $this->getMockBuilder( SessionProvider
::class )
84 ->onlyMethods( [ 'persistsSessionId', 'canChangeUser', '__toString' ] )
85 ->getMockForAbstractClass();
86 $this->initProvider( $provider2, null, null, $manager );
87 $provider2->method( 'persistsSessionId' )
89 $provider2->method( 'canChangeUser' )
91 $provider2->method( '__toString' )
92 ->willReturn( 'Mock2' );
95 new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
96 'provider' => $provider,
97 'userInfo' => $anonInfo,
100 $this->fail( 'Expected exception not thrown', 'bad metadata' );
101 } catch ( InvalidArgumentException
$ex ) {
102 $this->assertSame( 'Invalid metadata', $ex->getMessage(), 'bad metadata' );
105 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
106 'provider' => $provider,
107 'userInfo' => $anonInfo
109 $this->assertSame( $provider, $info->getProvider() );
110 $this->assertNotNull( $info->getId() );
111 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
112 $this->assertSame( $anonInfo, $info->getUserInfo() );
113 $this->assertTrue( $info->isIdSafe() );
114 $this->assertFalse( $info->forceUse() );
115 $this->assertFalse( $info->wasPersisted() );
116 $this->assertFalse( $info->wasRemembered() );
117 $this->assertFalse( $info->forceHTTPS() );
118 $this->assertNull( $info->getProviderMetadata() );
120 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
121 'provider' => $provider,
122 'userInfo' => $unverifiedUserInfo,
123 'metadata' => [ 'Foo' ],
125 $this->assertSame( $provider, $info->getProvider() );
126 $this->assertNotNull( $info->getId() );
127 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
128 $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
129 $this->assertTrue( $info->isIdSafe() );
130 $this->assertFalse( $info->forceUse() );
131 $this->assertFalse( $info->wasPersisted() );
132 $this->assertFalse( $info->wasRemembered() );
133 $this->assertFalse( $info->forceHTTPS() );
134 $this->assertSame( [ 'Foo' ], $info->getProviderMetadata() );
136 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
137 'provider' => $provider,
138 'userInfo' => $userInfo
140 $this->assertSame( $provider, $info->getProvider() );
141 $this->assertNotNull( $info->getId() );
142 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
143 $this->assertSame( $userInfo, $info->getUserInfo() );
144 $this->assertTrue( $info->isIdSafe() );
145 $this->assertFalse( $info->forceUse() );
146 $this->assertFalse( $info->wasPersisted() );
147 $this->assertTrue( $info->wasRemembered() );
148 $this->assertFalse( $info->forceHTTPS() );
149 $this->assertNull( $info->getProviderMetadata() );
151 $id = $manager->generateSessionId();
153 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
154 'provider' => $provider,
157 'userInfo' => $anonInfo
159 $this->assertSame( $provider, $info->getProvider() );
160 $this->assertSame( $id, $info->getId() );
161 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
162 $this->assertSame( $anonInfo, $info->getUserInfo() );
163 $this->assertFalse( $info->isIdSafe() );
164 $this->assertFalse( $info->forceUse() );
165 $this->assertTrue( $info->wasPersisted() );
166 $this->assertFalse( $info->wasRemembered() );
167 $this->assertFalse( $info->forceHTTPS() );
168 $this->assertNull( $info->getProviderMetadata() );
170 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
171 'provider' => $provider,
173 'userInfo' => $userInfo
175 $this->assertSame( $provider, $info->getProvider() );
176 $this->assertSame( $id, $info->getId() );
177 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
178 $this->assertSame( $userInfo, $info->getUserInfo() );
179 $this->assertFalse( $info->isIdSafe() );
180 $this->assertFalse( $info->forceUse() );
181 $this->assertFalse( $info->wasPersisted() );
182 $this->assertTrue( $info->wasRemembered() );
183 $this->assertFalse( $info->forceHTTPS() );
184 $this->assertNull( $info->getProviderMetadata() );
186 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
189 'userInfo' => $userInfo,
190 'metadata' => [ 'Foo' ],
192 $this->assertSame( $id, $info->getId() );
193 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
194 $this->assertSame( $userInfo, $info->getUserInfo() );
195 $this->assertFalse( $info->isIdSafe() );
196 $this->assertFalse( $info->forceUse() );
197 $this->assertTrue( $info->wasPersisted() );
198 $this->assertFalse( $info->wasRemembered() );
199 $this->assertFalse( $info->forceHTTPS() );
200 $this->assertNull( $info->getProviderMetadata() );
202 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
204 'remembered' => true,
205 'userInfo' => $userInfo,
207 $this->assertFalse( $info->wasRemembered(), 'no provider' );
209 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
210 'provider' => $provider,
212 'remembered' => true,
214 $this->assertFalse( $info->wasRemembered(), 'no user' );
216 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
217 'provider' => $provider,
219 'remembered' => true,
220 'userInfo' => $anonInfo,
222 $this->assertFalse( $info->wasRemembered(), 'anonymous user' );
224 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
225 'provider' => $provider,
227 'remembered' => true,
228 'userInfo' => $unverifiedUserInfo,
230 $this->assertFalse( $info->wasRemembered(), 'unverified user' );
232 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
233 'provider' => $provider,
235 'remembered' => false,
236 'userInfo' => $userInfo,
238 $this->assertFalse( $info->wasRemembered(), 'specific override' );
240 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
244 $this->assertSame( $id, $info->getId() );
245 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
246 $this->assertTrue( $info->isIdSafe() );
248 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
252 $this->assertFalse( $info->forceUse(), 'no provider' );
254 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
255 'provider' => $provider,
258 $this->assertFalse( $info->forceUse(), 'no id' );
260 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
261 'provider' => $provider,
265 $this->assertTrue( $info->forceUse(), 'correct use' );
267 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
271 $this->assertTrue( $info->forceHTTPS() );
273 $fromInfo = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
275 'provider' => $provider,
276 'userInfo' => $userInfo,
280 'remembered' => true,
281 'forceHTTPS' => true,
282 'metadata' => [ 'foo!' ],
284 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
4, [
285 'copyFrom' => $fromInfo,
287 $this->assertSame( $id . 'A', $info->getId() );
288 $this->assertSame( SessionInfo
::MIN_PRIORITY +
4, $info->getPriority() );
289 $this->assertSame( $provider, $info->getProvider() );
290 $this->assertSame( $userInfo, $info->getUserInfo() );
291 $this->assertTrue( $info->isIdSafe() );
292 $this->assertTrue( $info->forceUse() );
293 $this->assertTrue( $info->wasPersisted() );
294 $this->assertTrue( $info->wasRemembered() );
295 $this->assertTrue( $info->forceHTTPS() );
296 $this->assertSame( [ 'foo!' ], $info->getProviderMetadata() );
298 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
4, [
300 'provider' => $provider2,
301 'userInfo' => $unverifiedUserInfo,
304 'persisted' => false,
305 'remembered' => false,
306 'forceHTTPS' => false,
308 'copyFrom' => $fromInfo,
310 $this->assertSame( $id . 'X', $info->getId() );
311 $this->assertSame( SessionInfo
::MIN_PRIORITY +
4, $info->getPriority() );
312 $this->assertSame( $provider2, $info->getProvider() );
313 $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
314 $this->assertFalse( $info->isIdSafe() );
315 $this->assertFalse( $info->forceUse() );
316 $this->assertFalse( $info->wasPersisted() );
317 $this->assertFalse( $info->wasRemembered() );
318 $this->assertFalse( $info->forceHTTPS() );
319 $this->assertNull( $info->getProviderMetadata() );
321 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
325 '[' . SessionInfo
::MIN_PRIORITY
. "]null<null>$id",
330 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
331 'provider' => $provider,
334 'userInfo' => $userInfo
337 '[' . SessionInfo
::MIN_PRIORITY
. "]Mock<+:{$userInfo->getId()}:$username>$id",
342 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
343 'provider' => $provider,
346 'userInfo' => $unverifiedUserInfo
349 '[' . SessionInfo
::MIN_PRIORITY
. "]Mock<-:{$userInfo->getId()}:$username>$id",
355 public function testCompare() {
356 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
357 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, [ 'id' => $id ] );
358 $info2 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
2, [ 'id' => $id ] );
360 $this->assertLessThan( 0, SessionInfo
::compare( $info1, $info2 ), '<' );
361 $this->assertGreaterThan( 0, SessionInfo
::compare( $info2, $info1 ), '>' );
362 $this->assertSame( 0, SessionInfo
::compare( $info1, $info1 ), '==' );