3 namespace MediaWiki\Session
;
10 * @covers MediaWiki\Session\SessionInfo
12 class SessionInfoTest
extends MediaWikiTestCase
{
14 public function testBasics() {
15 $anonInfo = UserInfo
::newAnonymous();
16 $userInfo = UserInfo
::newFromName( 'UTSysop', true );
17 $unverifiedUserInfo = UserInfo
::newFromName( 'UTSysop', false );
20 new SessionInfo( SessionInfo
::MIN_PRIORITY
- 1, [] );
21 $this->fail( 'Expected exception not thrown', 'priority < min' );
22 } catch ( \InvalidArgumentException
$ex ) {
23 $this->assertSame( 'Invalid priority', $ex->getMessage(), 'priority < min' );
27 new SessionInfo( SessionInfo
::MAX_PRIORITY +
1, [] );
28 $this->fail( 'Expected exception not thrown', 'priority > max' );
29 } catch ( \InvalidArgumentException
$ex ) {
30 $this->assertSame( 'Invalid priority', $ex->getMessage(), 'priority > max' );
34 new SessionInfo( SessionInfo
::MIN_PRIORITY
, [ 'id' => 'ABC?' ] );
35 $this->fail( 'Expected exception not thrown', 'bad session ID' );
36 } catch ( \InvalidArgumentException
$ex ) {
37 $this->assertSame( 'Invalid session ID', $ex->getMessage(), 'bad session ID' );
41 new SessionInfo( SessionInfo
::MIN_PRIORITY
, [ 'userInfo' => new \stdClass
] );
42 $this->fail( 'Expected exception not thrown', 'bad userInfo' );
43 } catch ( \InvalidArgumentException
$ex ) {
44 $this->assertSame( 'Invalid userInfo', $ex->getMessage(), 'bad userInfo' );
48 new SessionInfo( SessionInfo
::MIN_PRIORITY
, [] );
49 $this->fail( 'Expected exception not thrown', 'no provider, no id' );
50 } catch ( \InvalidArgumentException
$ex ) {
51 $this->assertSame( 'Must supply an ID when no provider is given', $ex->getMessage(),
52 'no provider, no id' );
56 new SessionInfo( SessionInfo
::MIN_PRIORITY
, [ 'copyFrom' => new \stdClass
] );
57 $this->fail( 'Expected exception not thrown', 'bad copyFrom' );
58 } catch ( \InvalidArgumentException
$ex ) {
59 $this->assertSame( 'Invalid copyFrom', $ex->getMessage(),
63 $manager = new SessionManager();
64 $provider = $this->getMockBuilder( SessionProvider
::class )
65 ->setMethods( [ 'persistsSessionId', 'canChangeUser', '__toString' ] )
66 ->getMockForAbstractClass();
67 $provider->setManager( $manager );
68 $provider->expects( $this->any() )->method( 'persistsSessionId' )
69 ->will( $this->returnValue( true ) );
70 $provider->expects( $this->any() )->method( 'canChangeUser' )
71 ->will( $this->returnValue( true ) );
72 $provider->expects( $this->any() )->method( '__toString' )
73 ->will( $this->returnValue( 'Mock' ) );
75 $provider2 = $this->getMockBuilder( SessionProvider
::class )
76 ->setMethods( [ 'persistsSessionId', 'canChangeUser', '__toString' ] )
77 ->getMockForAbstractClass();
78 $provider2->setManager( $manager );
79 $provider2->expects( $this->any() )->method( 'persistsSessionId' )
80 ->will( $this->returnValue( true ) );
81 $provider2->expects( $this->any() )->method( 'canChangeUser' )
82 ->will( $this->returnValue( true ) );
83 $provider2->expects( $this->any() )->method( '__toString' )
84 ->will( $this->returnValue( 'Mock2' ) );
87 new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
88 'provider' => $provider,
89 'userInfo' => $anonInfo,
92 $this->fail( 'Expected exception not thrown', 'bad metadata' );
93 } catch ( \InvalidArgumentException
$ex ) {
94 $this->assertSame( 'Invalid metadata', $ex->getMessage(), 'bad metadata' );
97 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
98 'provider' => $provider,
99 'userInfo' => $anonInfo
101 $this->assertSame( $provider, $info->getProvider() );
102 $this->assertNotNull( $info->getId() );
103 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
104 $this->assertSame( $anonInfo, $info->getUserInfo() );
105 $this->assertTrue( $info->isIdSafe() );
106 $this->assertFalse( $info->forceUse() );
107 $this->assertFalse( $info->wasPersisted() );
108 $this->assertFalse( $info->wasRemembered() );
109 $this->assertFalse( $info->forceHTTPS() );
110 $this->assertNull( $info->getProviderMetadata() );
112 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
113 'provider' => $provider,
114 'userInfo' => $unverifiedUserInfo,
115 'metadata' => [ 'Foo' ],
117 $this->assertSame( $provider, $info->getProvider() );
118 $this->assertNotNull( $info->getId() );
119 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
120 $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
121 $this->assertTrue( $info->isIdSafe() );
122 $this->assertFalse( $info->forceUse() );
123 $this->assertFalse( $info->wasPersisted() );
124 $this->assertFalse( $info->wasRemembered() );
125 $this->assertFalse( $info->forceHTTPS() );
126 $this->assertSame( [ 'Foo' ], $info->getProviderMetadata() );
128 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
129 'provider' => $provider,
130 'userInfo' => $userInfo
132 $this->assertSame( $provider, $info->getProvider() );
133 $this->assertNotNull( $info->getId() );
134 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
135 $this->assertSame( $userInfo, $info->getUserInfo() );
136 $this->assertTrue( $info->isIdSafe() );
137 $this->assertFalse( $info->forceUse() );
138 $this->assertFalse( $info->wasPersisted() );
139 $this->assertTrue( $info->wasRemembered() );
140 $this->assertFalse( $info->forceHTTPS() );
141 $this->assertNull( $info->getProviderMetadata() );
143 $id = $manager->generateSessionId();
145 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
146 'provider' => $provider,
149 'userInfo' => $anonInfo
151 $this->assertSame( $provider, $info->getProvider() );
152 $this->assertSame( $id, $info->getId() );
153 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
154 $this->assertSame( $anonInfo, $info->getUserInfo() );
155 $this->assertFalse( $info->isIdSafe() );
156 $this->assertFalse( $info->forceUse() );
157 $this->assertTrue( $info->wasPersisted() );
158 $this->assertFalse( $info->wasRemembered() );
159 $this->assertFalse( $info->forceHTTPS() );
160 $this->assertNull( $info->getProviderMetadata() );
162 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
163 'provider' => $provider,
165 'userInfo' => $userInfo
167 $this->assertSame( $provider, $info->getProvider() );
168 $this->assertSame( $id, $info->getId() );
169 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
170 $this->assertSame( $userInfo, $info->getUserInfo() );
171 $this->assertFalse( $info->isIdSafe() );
172 $this->assertFalse( $info->forceUse() );
173 $this->assertFalse( $info->wasPersisted() );
174 $this->assertTrue( $info->wasRemembered() );
175 $this->assertFalse( $info->forceHTTPS() );
176 $this->assertNull( $info->getProviderMetadata() );
178 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
181 'userInfo' => $userInfo,
182 'metadata' => [ 'Foo' ],
184 $this->assertSame( $id, $info->getId() );
185 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
186 $this->assertSame( $userInfo, $info->getUserInfo() );
187 $this->assertFalse( $info->isIdSafe() );
188 $this->assertFalse( $info->forceUse() );
189 $this->assertTrue( $info->wasPersisted() );
190 $this->assertFalse( $info->wasRemembered() );
191 $this->assertFalse( $info->forceHTTPS() );
192 $this->assertNull( $info->getProviderMetadata() );
194 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
196 'remembered' => true,
197 'userInfo' => $userInfo,
199 $this->assertFalse( $info->wasRemembered(), 'no provider' );
201 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
202 'provider' => $provider,
204 'remembered' => true,
206 $this->assertFalse( $info->wasRemembered(), 'no user' );
208 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
209 'provider' => $provider,
211 'remembered' => true,
212 'userInfo' => $anonInfo,
214 $this->assertFalse( $info->wasRemembered(), 'anonymous user' );
216 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
217 'provider' => $provider,
219 'remembered' => true,
220 'userInfo' => $unverifiedUserInfo,
222 $this->assertFalse( $info->wasRemembered(), 'unverified user' );
224 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
225 'provider' => $provider,
227 'remembered' => false,
228 'userInfo' => $userInfo,
230 $this->assertFalse( $info->wasRemembered(), 'specific override' );
232 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
236 $this->assertSame( $id, $info->getId() );
237 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
238 $this->assertTrue( $info->isIdSafe() );
240 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
244 $this->assertFalse( $info->forceUse(), 'no provider' );
246 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
247 'provider' => $provider,
250 $this->assertFalse( $info->forceUse(), 'no id' );
252 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
253 'provider' => $provider,
257 $this->assertTrue( $info->forceUse(), 'correct use' );
259 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
263 $this->assertTrue( $info->forceHTTPS() );
265 $fromInfo = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
267 'provider' => $provider,
268 'userInfo' => $userInfo,
272 'remembered' => true,
273 'forceHTTPS' => true,
274 'metadata' => [ 'foo!' ],
276 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
4, [
277 'copyFrom' => $fromInfo,
279 $this->assertSame( $id . 'A', $info->getId() );
280 $this->assertSame( SessionInfo
::MIN_PRIORITY +
4, $info->getPriority() );
281 $this->assertSame( $provider, $info->getProvider() );
282 $this->assertSame( $userInfo, $info->getUserInfo() );
283 $this->assertTrue( $info->isIdSafe() );
284 $this->assertTrue( $info->forceUse() );
285 $this->assertTrue( $info->wasPersisted() );
286 $this->assertTrue( $info->wasRemembered() );
287 $this->assertTrue( $info->forceHTTPS() );
288 $this->assertSame( [ 'foo!' ], $info->getProviderMetadata() );
290 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
4, [
292 'provider' => $provider2,
293 'userInfo' => $unverifiedUserInfo,
296 'persisted' => false,
297 'remembered' => false,
298 'forceHTTPS' => false,
300 'copyFrom' => $fromInfo,
302 $this->assertSame( $id . 'X', $info->getId() );
303 $this->assertSame( SessionInfo
::MIN_PRIORITY +
4, $info->getPriority() );
304 $this->assertSame( $provider2, $info->getProvider() );
305 $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
306 $this->assertFalse( $info->isIdSafe() );
307 $this->assertFalse( $info->forceUse() );
308 $this->assertFalse( $info->wasPersisted() );
309 $this->assertFalse( $info->wasRemembered() );
310 $this->assertFalse( $info->forceHTTPS() );
311 $this->assertNull( $info->getProviderMetadata() );
313 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
317 '[' . SessionInfo
::MIN_PRIORITY
. "]null<null>$id",
322 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
323 'provider' => $provider,
326 'userInfo' => $userInfo
329 '[' . SessionInfo
::MIN_PRIORITY
. "]Mock<+:{$userInfo->getId()}:UTSysop>$id",
334 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
335 'provider' => $provider,
338 'userInfo' => $unverifiedUserInfo
341 '[' . SessionInfo
::MIN_PRIORITY
. "]Mock<-:{$userInfo->getId()}:UTSysop>$id",
347 public function testCompare() {
348 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
349 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, [ 'id' => $id ] );
350 $info2 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
2, [ 'id' => $id ] );
352 $this->assertTrue( SessionInfo
::compare( $info1, $info2 ) < 0, '<' );
353 $this->assertTrue( SessionInfo
::compare( $info2, $info1 ) > 0, '>' );
354 $this->assertTrue( SessionInfo
::compare( $info1, $info1 ) === 0, '==' );