3 namespace MediaWiki\Session
;
8 * @covers MediaWiki\Session\SessionInfo
10 class SessionInfoTest
extends \MediaWikiUnitTestCase
{
12 public function testBasics() {
13 $sysopUser = new \
User();
14 $sysopUser->setName( 'UTSysop' );
16 $anonInfo = UserInfo
::newAnonymous();
17 $userInfo = UserInfo
::newFromUser( $sysopUser, true );
18 $unverifiedUserInfo = UserInfo
::newFromUser( $sysopUser, false );
21 new SessionInfo( SessionInfo
::MIN_PRIORITY
- 1, [] );
22 $this->fail( 'Expected exception not thrown', 'priority < min' );
23 } catch ( \InvalidArgumentException
$ex ) {
24 $this->assertSame( 'Invalid priority', $ex->getMessage(), 'priority < min' );
28 new SessionInfo( SessionInfo
::MAX_PRIORITY +
1, [] );
29 $this->fail( 'Expected exception not thrown', 'priority > max' );
30 } catch ( \InvalidArgumentException
$ex ) {
31 $this->assertSame( 'Invalid priority', $ex->getMessage(), 'priority > max' );
35 new SessionInfo( SessionInfo
::MIN_PRIORITY
, [ 'id' => 'ABC?' ] );
36 $this->fail( 'Expected exception not thrown', 'bad session ID' );
37 } catch ( \InvalidArgumentException
$ex ) {
38 $this->assertSame( 'Invalid session ID', $ex->getMessage(), 'bad session ID' );
42 new SessionInfo( SessionInfo
::MIN_PRIORITY
, [ 'userInfo' => new \stdClass
] );
43 $this->fail( 'Expected exception not thrown', 'bad userInfo' );
44 } catch ( \InvalidArgumentException
$ex ) {
45 $this->assertSame( 'Invalid userInfo', $ex->getMessage(), 'bad userInfo' );
49 new SessionInfo( SessionInfo
::MIN_PRIORITY
, [] );
50 $this->fail( 'Expected exception not thrown', 'no provider, no id' );
51 } catch ( \InvalidArgumentException
$ex ) {
52 $this->assertSame( 'Must supply an ID when no provider is given', $ex->getMessage(),
53 'no provider, no id' );
57 new SessionInfo( SessionInfo
::MIN_PRIORITY
, [ 'copyFrom' => new \stdClass
] );
58 $this->fail( 'Expected exception not thrown', 'bad copyFrom' );
59 } catch ( \InvalidArgumentException
$ex ) {
60 $this->assertSame( 'Invalid copyFrom', $ex->getMessage(),
64 $manager = new SessionManager();
65 $provider = $this->getMockBuilder( SessionProvider
::class )
66 ->setMethods( [ 'persistsSessionId', 'canChangeUser', '__toString' ] )
67 ->getMockForAbstractClass();
68 $provider->setManager( $manager );
69 $provider->expects( $this->any() )->method( 'persistsSessionId' )
70 ->will( $this->returnValue( true ) );
71 $provider->expects( $this->any() )->method( 'canChangeUser' )
72 ->will( $this->returnValue( true ) );
73 $provider->expects( $this->any() )->method( '__toString' )
74 ->will( $this->returnValue( 'Mock' ) );
76 $provider2 = $this->getMockBuilder( SessionProvider
::class )
77 ->setMethods( [ 'persistsSessionId', 'canChangeUser', '__toString' ] )
78 ->getMockForAbstractClass();
79 $provider2->setManager( $manager );
80 $provider2->expects( $this->any() )->method( 'persistsSessionId' )
81 ->will( $this->returnValue( true ) );
82 $provider2->expects( $this->any() )->method( 'canChangeUser' )
83 ->will( $this->returnValue( true ) );
84 $provider2->expects( $this->any() )->method( '__toString' )
85 ->will( $this->returnValue( 'Mock2' ) );
88 new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
89 'provider' => $provider,
90 'userInfo' => $anonInfo,
93 $this->fail( 'Expected exception not thrown', 'bad metadata' );
94 } catch ( \InvalidArgumentException
$ex ) {
95 $this->assertSame( 'Invalid metadata', $ex->getMessage(), 'bad metadata' );
98 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
99 'provider' => $provider,
100 'userInfo' => $anonInfo
102 $this->assertSame( $provider, $info->getProvider() );
103 $this->assertNotNull( $info->getId() );
104 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
105 $this->assertSame( $anonInfo, $info->getUserInfo() );
106 $this->assertTrue( $info->isIdSafe() );
107 $this->assertFalse( $info->forceUse() );
108 $this->assertFalse( $info->wasPersisted() );
109 $this->assertFalse( $info->wasRemembered() );
110 $this->assertFalse( $info->forceHTTPS() );
111 $this->assertNull( $info->getProviderMetadata() );
113 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
114 'provider' => $provider,
115 'userInfo' => $unverifiedUserInfo,
116 'metadata' => [ 'Foo' ],
118 $this->assertSame( $provider, $info->getProvider() );
119 $this->assertNotNull( $info->getId() );
120 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
121 $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
122 $this->assertTrue( $info->isIdSafe() );
123 $this->assertFalse( $info->forceUse() );
124 $this->assertFalse( $info->wasPersisted() );
125 $this->assertFalse( $info->wasRemembered() );
126 $this->assertFalse( $info->forceHTTPS() );
127 $this->assertSame( [ 'Foo' ], $info->getProviderMetadata() );
129 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
130 'provider' => $provider,
131 'userInfo' => $userInfo
133 $this->assertSame( $provider, $info->getProvider() );
134 $this->assertNotNull( $info->getId() );
135 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
136 $this->assertSame( $userInfo, $info->getUserInfo() );
137 $this->assertTrue( $info->isIdSafe() );
138 $this->assertFalse( $info->forceUse() );
139 $this->assertFalse( $info->wasPersisted() );
140 $this->assertTrue( $info->wasRemembered() );
141 $this->assertFalse( $info->forceHTTPS() );
142 $this->assertNull( $info->getProviderMetadata() );
144 $id = $manager->generateSessionId();
146 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
147 'provider' => $provider,
150 'userInfo' => $anonInfo
152 $this->assertSame( $provider, $info->getProvider() );
153 $this->assertSame( $id, $info->getId() );
154 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
155 $this->assertSame( $anonInfo, $info->getUserInfo() );
156 $this->assertFalse( $info->isIdSafe() );
157 $this->assertFalse( $info->forceUse() );
158 $this->assertTrue( $info->wasPersisted() );
159 $this->assertFalse( $info->wasRemembered() );
160 $this->assertFalse( $info->forceHTTPS() );
161 $this->assertNull( $info->getProviderMetadata() );
163 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
164 'provider' => $provider,
166 'userInfo' => $userInfo
168 $this->assertSame( $provider, $info->getProvider() );
169 $this->assertSame( $id, $info->getId() );
170 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
171 $this->assertSame( $userInfo, $info->getUserInfo() );
172 $this->assertFalse( $info->isIdSafe() );
173 $this->assertFalse( $info->forceUse() );
174 $this->assertFalse( $info->wasPersisted() );
175 $this->assertTrue( $info->wasRemembered() );
176 $this->assertFalse( $info->forceHTTPS() );
177 $this->assertNull( $info->getProviderMetadata() );
179 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
182 'userInfo' => $userInfo,
183 'metadata' => [ 'Foo' ],
185 $this->assertSame( $id, $info->getId() );
186 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
187 $this->assertSame( $userInfo, $info->getUserInfo() );
188 $this->assertFalse( $info->isIdSafe() );
189 $this->assertFalse( $info->forceUse() );
190 $this->assertTrue( $info->wasPersisted() );
191 $this->assertFalse( $info->wasRemembered() );
192 $this->assertFalse( $info->forceHTTPS() );
193 $this->assertNull( $info->getProviderMetadata() );
195 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
197 'remembered' => true,
198 'userInfo' => $userInfo,
200 $this->assertFalse( $info->wasRemembered(), 'no provider' );
202 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
203 'provider' => $provider,
205 'remembered' => true,
207 $this->assertFalse( $info->wasRemembered(), 'no user' );
209 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
210 'provider' => $provider,
212 'remembered' => true,
213 'userInfo' => $anonInfo,
215 $this->assertFalse( $info->wasRemembered(), 'anonymous user' );
217 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
218 'provider' => $provider,
220 'remembered' => true,
221 'userInfo' => $unverifiedUserInfo,
223 $this->assertFalse( $info->wasRemembered(), 'unverified user' );
225 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
226 'provider' => $provider,
228 'remembered' => false,
229 'userInfo' => $userInfo,
231 $this->assertFalse( $info->wasRemembered(), 'specific override' );
233 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
237 $this->assertSame( $id, $info->getId() );
238 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
239 $this->assertTrue( $info->isIdSafe() );
241 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
245 $this->assertFalse( $info->forceUse(), 'no provider' );
247 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
248 'provider' => $provider,
251 $this->assertFalse( $info->forceUse(), 'no id' );
253 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
254 'provider' => $provider,
258 $this->assertTrue( $info->forceUse(), 'correct use' );
260 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
264 $this->assertTrue( $info->forceHTTPS() );
266 $fromInfo = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
268 'provider' => $provider,
269 'userInfo' => $userInfo,
273 'remembered' => true,
274 'forceHTTPS' => true,
275 'metadata' => [ 'foo!' ],
277 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
4, [
278 'copyFrom' => $fromInfo,
280 $this->assertSame( $id . 'A', $info->getId() );
281 $this->assertSame( SessionInfo
::MIN_PRIORITY +
4, $info->getPriority() );
282 $this->assertSame( $provider, $info->getProvider() );
283 $this->assertSame( $userInfo, $info->getUserInfo() );
284 $this->assertTrue( $info->isIdSafe() );
285 $this->assertTrue( $info->forceUse() );
286 $this->assertTrue( $info->wasPersisted() );
287 $this->assertTrue( $info->wasRemembered() );
288 $this->assertTrue( $info->forceHTTPS() );
289 $this->assertSame( [ 'foo!' ], $info->getProviderMetadata() );
291 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
4, [
293 'provider' => $provider2,
294 'userInfo' => $unverifiedUserInfo,
297 'persisted' => false,
298 'remembered' => false,
299 'forceHTTPS' => false,
301 'copyFrom' => $fromInfo,
303 $this->assertSame( $id . 'X', $info->getId() );
304 $this->assertSame( SessionInfo
::MIN_PRIORITY +
4, $info->getPriority() );
305 $this->assertSame( $provider2, $info->getProvider() );
306 $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
307 $this->assertFalse( $info->isIdSafe() );
308 $this->assertFalse( $info->forceUse() );
309 $this->assertFalse( $info->wasPersisted() );
310 $this->assertFalse( $info->wasRemembered() );
311 $this->assertFalse( $info->forceHTTPS() );
312 $this->assertNull( $info->getProviderMetadata() );
314 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
318 '[' . SessionInfo
::MIN_PRIORITY
. "]null<null>$id",
323 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
324 'provider' => $provider,
327 'userInfo' => $userInfo
330 '[' . SessionInfo
::MIN_PRIORITY
. "]Mock<+:{$userInfo->getId()}:UTSysop>$id",
335 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
336 'provider' => $provider,
339 'userInfo' => $unverifiedUserInfo
342 '[' . SessionInfo
::MIN_PRIORITY
. "]Mock<-:{$userInfo->getId()}:UTSysop>$id",
348 public function testCompare() {
349 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
350 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, [ 'id' => $id ] );
351 $info2 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
2, [ 'id' => $id ] );
353 $this->assertTrue( SessionInfo
::compare( $info1, $info2 ) < 0, '<' );
354 $this->assertTrue( SessionInfo
::compare( $info2, $info1 ) > 0, '>' );
355 $this->assertTrue( SessionInfo
::compare( $info1, $info1 ) === 0, '==' );