Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / session / SessionInfoTest.php
blob1f98f1026296d4aee74f7b5c302884783cdba504
1 <?php
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;
11 use stdClass;
13 /**
14 * @group Session
15 * @group Database
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 );
27 try {
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' );
34 try {
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' );
41 try {
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' );
48 try {
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' );
55 try {
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' );
63 try {
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(),
68 'bad copyFrom' );
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' )
77 ->willReturn( true );
78 $provider->method( 'canChangeUser' )
79 ->willReturn( true );
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' )
88 ->willReturn( true );
89 $provider2->method( 'canChangeUser' )
90 ->willReturn( true );
91 $provider2->method( '__toString' )
92 ->willReturn( 'Mock2' );
94 try {
95 new SessionInfo( SessionInfo::MIN_PRIORITY, [
96 'provider' => $provider,
97 'userInfo' => $anonInfo,
98 'metadata' => 'foo',
99 ] );
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
108 ] );
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' ],
124 ] );
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
139 ] );
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,
155 'id' => $id,
156 'persisted' => true,
157 'userInfo' => $anonInfo
158 ] );
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,
172 'id' => $id,
173 'userInfo' => $userInfo
174 ] );
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, [
187 'id' => $id,
188 'persisted' => true,
189 'userInfo' => $userInfo,
190 'metadata' => [ 'Foo' ],
191 ] );
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, [
203 'id' => $id,
204 'remembered' => true,
205 'userInfo' => $userInfo,
206 ] );
207 $this->assertFalse( $info->wasRemembered(), 'no provider' );
209 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
210 'provider' => $provider,
211 'id' => $id,
212 'remembered' => true,
213 ] );
214 $this->assertFalse( $info->wasRemembered(), 'no user' );
216 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
217 'provider' => $provider,
218 'id' => $id,
219 'remembered' => true,
220 'userInfo' => $anonInfo,
221 ] );
222 $this->assertFalse( $info->wasRemembered(), 'anonymous user' );
224 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
225 'provider' => $provider,
226 'id' => $id,
227 'remembered' => true,
228 'userInfo' => $unverifiedUserInfo,
229 ] );
230 $this->assertFalse( $info->wasRemembered(), 'unverified user' );
232 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
233 'provider' => $provider,
234 'id' => $id,
235 'remembered' => false,
236 'userInfo' => $userInfo,
237 ] );
238 $this->assertFalse( $info->wasRemembered(), 'specific override' );
240 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
241 'id' => $id,
242 'idIsSafe' => true,
243 ] );
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, [
249 'id' => $id,
250 'forceUse' => true,
251 ] );
252 $this->assertFalse( $info->forceUse(), 'no provider' );
254 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
255 'provider' => $provider,
256 'forceUse' => true,
257 ] );
258 $this->assertFalse( $info->forceUse(), 'no id' );
260 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 5, [
261 'provider' => $provider,
262 'id' => $id,
263 'forceUse' => true,
264 ] );
265 $this->assertTrue( $info->forceUse(), 'correct use' );
267 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
268 'id' => $id,
269 'forceHTTPS' => 1,
270 ] );
271 $this->assertTrue( $info->forceHTTPS() );
273 $fromInfo = new SessionInfo( SessionInfo::MIN_PRIORITY, [
274 'id' => $id . 'A',
275 'provider' => $provider,
276 'userInfo' => $userInfo,
277 'idIsSafe' => true,
278 'forceUse' => true,
279 'persisted' => true,
280 'remembered' => true,
281 'forceHTTPS' => true,
282 'metadata' => [ 'foo!' ],
283 ] );
284 $info = new SessionInfo( SessionInfo::MIN_PRIORITY + 4, [
285 'copyFrom' => $fromInfo,
286 ] );
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, [
299 'id' => $id . 'X',
300 'provider' => $provider2,
301 'userInfo' => $unverifiedUserInfo,
302 'idIsSafe' => false,
303 'forceUse' => false,
304 'persisted' => false,
305 'remembered' => false,
306 'forceHTTPS' => false,
307 'metadata' => null,
308 'copyFrom' => $fromInfo,
309 ] );
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, [
322 'id' => $id,
323 ] );
324 $this->assertSame(
325 '[' . SessionInfo::MIN_PRIORITY . "]null<null>$id",
326 (string)$info,
327 'toString'
330 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
331 'provider' => $provider,
332 'id' => $id,
333 'persisted' => true,
334 'userInfo' => $userInfo
335 ] );
336 $this->assertSame(
337 '[' . SessionInfo::MIN_PRIORITY . "]Mock<+:{$userInfo->getId()}:$username>$id",
338 (string)$info,
339 'toString'
342 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
343 'provider' => $provider,
344 'id' => $id,
345 'persisted' => true,
346 'userInfo' => $unverifiedUserInfo
347 ] );
348 $this->assertSame(
349 '[' . SessionInfo::MIN_PRIORITY . "]Mock<-:{$userInfo->getId()}:$username>$id",
350 (string)$info,
351 'toString'
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 ), '==' );