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->wasPersisted() );
107 $this->assertFalse( $info->wasRemembered() );
108 $this->assertFalse( $info->forceHTTPS() );
109 $this->assertNull( $info->getProviderMetadata() );
111 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
112 'provider' => $provider,
113 'userInfo' => $unverifiedUserInfo,
114 'metadata' => [ 'Foo' ],
116 $this->assertSame( $provider, $info->getProvider() );
117 $this->assertNotNull( $info->getId() );
118 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
119 $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
120 $this->assertTrue( $info->isIdSafe() );
121 $this->assertFalse( $info->wasPersisted() );
122 $this->assertFalse( $info->wasRemembered() );
123 $this->assertFalse( $info->forceHTTPS() );
124 $this->assertSame( [ 'Foo' ], $info->getProviderMetadata() );
126 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
127 'provider' => $provider,
128 'userInfo' => $userInfo
130 $this->assertSame( $provider, $info->getProvider() );
131 $this->assertNotNull( $info->getId() );
132 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
133 $this->assertSame( $userInfo, $info->getUserInfo() );
134 $this->assertTrue( $info->isIdSafe() );
135 $this->assertFalse( $info->wasPersisted() );
136 $this->assertTrue( $info->wasRemembered() );
137 $this->assertFalse( $info->forceHTTPS() );
138 $this->assertNull( $info->getProviderMetadata() );
140 $id = $manager->generateSessionId();
142 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
143 'provider' => $provider,
146 'userInfo' => $anonInfo
148 $this->assertSame( $provider, $info->getProvider() );
149 $this->assertSame( $id, $info->getId() );
150 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
151 $this->assertSame( $anonInfo, $info->getUserInfo() );
152 $this->assertFalse( $info->isIdSafe() );
153 $this->assertTrue( $info->wasPersisted() );
154 $this->assertFalse( $info->wasRemembered() );
155 $this->assertFalse( $info->forceHTTPS() );
156 $this->assertNull( $info->getProviderMetadata() );
158 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
159 'provider' => $provider,
161 'userInfo' => $userInfo
163 $this->assertSame( $provider, $info->getProvider() );
164 $this->assertSame( $id, $info->getId() );
165 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
166 $this->assertSame( $userInfo, $info->getUserInfo() );
167 $this->assertFalse( $info->isIdSafe() );
168 $this->assertFalse( $info->wasPersisted() );
169 $this->assertTrue( $info->wasRemembered() );
170 $this->assertFalse( $info->forceHTTPS() );
171 $this->assertNull( $info->getProviderMetadata() );
173 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
176 'userInfo' => $userInfo,
177 'metadata' => [ 'Foo' ],
179 $this->assertSame( $id, $info->getId() );
180 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
181 $this->assertSame( $userInfo, $info->getUserInfo() );
182 $this->assertFalse( $info->isIdSafe() );
183 $this->assertTrue( $info->wasPersisted() );
184 $this->assertFalse( $info->wasRemembered() );
185 $this->assertFalse( $info->forceHTTPS() );
186 $this->assertNull( $info->getProviderMetadata() );
188 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
190 'remembered' => true,
191 'userInfo' => $userInfo,
193 $this->assertFalse( $info->wasRemembered(), 'no provider' );
195 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
196 'provider' => $provider,
198 'remembered' => true,
200 $this->assertFalse( $info->wasRemembered(), 'no user' );
202 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
203 'provider' => $provider,
205 'remembered' => true,
206 'userInfo' => $anonInfo,
208 $this->assertFalse( $info->wasRemembered(), 'anonymous user' );
210 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
211 'provider' => $provider,
213 'remembered' => true,
214 'userInfo' => $unverifiedUserInfo,
216 $this->assertFalse( $info->wasRemembered(), 'unverified user' );
218 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
219 'provider' => $provider,
221 'remembered' => false,
222 'userInfo' => $userInfo,
224 $this->assertFalse( $info->wasRemembered(), 'specific override' );
226 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
5, [
230 $this->assertSame( $id, $info->getId() );
231 $this->assertSame( SessionInfo
::MIN_PRIORITY +
5, $info->getPriority() );
232 $this->assertTrue( $info->isIdSafe() );
234 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
238 $this->assertTrue( $info->forceHTTPS() );
240 $fromInfo = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
242 'provider' => $provider,
243 'userInfo' => $userInfo,
246 'remembered' => true,
247 'forceHTTPS' => true,
248 'metadata' => [ 'foo!' ],
250 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
4, [
251 'copyFrom' => $fromInfo,
253 $this->assertSame( $id . 'A', $info->getId() );
254 $this->assertSame( SessionInfo
::MIN_PRIORITY +
4, $info->getPriority() );
255 $this->assertSame( $provider, $info->getProvider() );
256 $this->assertSame( $userInfo, $info->getUserInfo() );
257 $this->assertTrue( $info->isIdSafe() );
258 $this->assertTrue( $info->wasPersisted() );
259 $this->assertTrue( $info->wasRemembered() );
260 $this->assertTrue( $info->forceHTTPS() );
261 $this->assertSame( [ 'foo!' ], $info->getProviderMetadata() );
263 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY +
4, [
265 'provider' => $provider2,
266 'userInfo' => $unverifiedUserInfo,
268 'persisted' => false,
269 'remembered' => false,
270 'forceHTTPS' => false,
272 'copyFrom' => $fromInfo,
274 $this->assertSame( $id . 'X', $info->getId() );
275 $this->assertSame( SessionInfo
::MIN_PRIORITY +
4, $info->getPriority() );
276 $this->assertSame( $provider2, $info->getProvider() );
277 $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
278 $this->assertFalse( $info->isIdSafe() );
279 $this->assertFalse( $info->wasPersisted() );
280 $this->assertFalse( $info->wasRemembered() );
281 $this->assertFalse( $info->forceHTTPS() );
282 $this->assertNull( $info->getProviderMetadata() );
284 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
288 '[' . SessionInfo
::MIN_PRIORITY
. "]null<null>$id",
293 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
294 'provider' => $provider,
297 'userInfo' => $userInfo
300 '[' . SessionInfo
::MIN_PRIORITY
. "]Mock<+:{$userInfo->getId()}:UTSysop>$id",
305 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
306 'provider' => $provider,
309 'userInfo' => $unverifiedUserInfo
312 '[' . SessionInfo
::MIN_PRIORITY
. "]Mock<-:{$userInfo->getId()}:UTSysop>$id",
318 public function testCompare() {
319 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
320 $info1 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
1, [ 'id' => $id ] );
321 $info2 = new SessionInfo( SessionInfo
::MIN_PRIORITY +
2, [ 'id' => $id ] );
323 $this->assertTrue( SessionInfo
::compare( $info1, $info2 ) < 0, '<' );
324 $this->assertTrue( SessionInfo
::compare( $info2, $info1 ) > 0, '>' );
325 $this->assertTrue( SessionInfo
::compare( $info1, $info1 ) === 0, '==' );