Merge "Use IDatabase type hints in /maintenance"
[lhc/web/wiklou.git] / tests / phpunit / includes / session / SessionManagerTest.php
1 <?php
2
3 namespace MediaWiki\Session;
4
5 use MediaWikiTestCase;
6 use Psr\Log\LogLevel;
7 use User;
8
9 /**
10 * @group Session
11 * @group Database
12 * @covers MediaWiki\Session\SessionManager
13 */
14 class SessionManagerTest extends MediaWikiTestCase {
15
16 protected $config, $logger, $store;
17
18 protected function getManager() {
19 \ObjectCache::$instances['testSessionStore'] = new TestBagOStuff();
20 $this->config = new \HashConfig( [
21 'LanguageCode' => 'en',
22 'SessionCacheType' => 'testSessionStore',
23 'ObjectCacheSessionExpiry' => 100,
24 'SessionProviders' => [
25 [ 'class' => 'DummySessionProvider' ],
26 ]
27 ] );
28 $this->logger = new \TestLogger( false, function ( $m ) {
29 return substr( $m, 0, 15 ) === 'SessionBackend ' ? null : $m;
30 } );
31 $this->store = new TestBagOStuff();
32
33 return new SessionManager( [
34 'config' => $this->config,
35 'logger' => $this->logger,
36 'store' => $this->store,
37 ] );
38 }
39
40 protected function objectCacheDef( $object ) {
41 return [ 'factory' => function () use ( $object ) {
42 return $object;
43 } ];
44 }
45
46 public function testSingleton() {
47 $reset = TestUtils::setSessionManagerSingleton( null );
48
49 $singleton = SessionManager::singleton();
50 $this->assertInstanceOf( SessionManager::class, $singleton );
51 $this->assertSame( $singleton, SessionManager::singleton() );
52 }
53
54 public function testGetGlobalSession() {
55 $context = \RequestContext::getMain();
56
57 if ( !PHPSessionHandler::isInstalled() ) {
58 PHPSessionHandler::install( SessionManager::singleton() );
59 }
60 $rProp = new \ReflectionProperty( PHPSessionHandler::class, 'instance' );
61 $rProp->setAccessible( true );
62 $handler = \TestingAccessWrapper::newFromObject( $rProp->getValue() );
63 $oldEnable = $handler->enable;
64 $reset[] = new \Wikimedia\ScopedCallback( function () use ( $handler, $oldEnable ) {
65 if ( $handler->enable ) {
66 session_write_close();
67 }
68 $handler->enable = $oldEnable;
69 } );
70 $reset[] = TestUtils::setSessionManagerSingleton( $this->getManager() );
71
72 $handler->enable = true;
73 $request = new \FauxRequest();
74 $context->setRequest( $request );
75 $id = $request->getSession()->getId();
76
77 session_id( '' );
78 $session = SessionManager::getGlobalSession();
79 $this->assertSame( $id, $session->getId() );
80
81 session_id( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
82 $session = SessionManager::getGlobalSession();
83 $this->assertSame( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', $session->getId() );
84 $this->assertSame( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', $request->getSession()->getId() );
85
86 session_write_close();
87 $handler->enable = false;
88 $request = new \FauxRequest();
89 $context->setRequest( $request );
90 $id = $request->getSession()->getId();
91
92 session_id( '' );
93 $session = SessionManager::getGlobalSession();
94 $this->assertSame( $id, $session->getId() );
95
96 session_id( 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
97 $session = SessionManager::getGlobalSession();
98 $this->assertSame( $id, $session->getId() );
99 $this->assertSame( $id, $request->getSession()->getId() );
100 }
101
102 public function testConstructor() {
103 $manager = \TestingAccessWrapper::newFromObject( $this->getManager() );
104 $this->assertSame( $this->config, $manager->config );
105 $this->assertSame( $this->logger, $manager->logger );
106 $this->assertSame( $this->store, $manager->store );
107
108 $manager = \TestingAccessWrapper::newFromObject( new SessionManager() );
109 $this->assertSame( \RequestContext::getMain()->getConfig(), $manager->config );
110
111 $manager = \TestingAccessWrapper::newFromObject( new SessionManager( [
112 'config' => $this->config,
113 ] ) );
114 $this->assertSame( \ObjectCache::$instances['testSessionStore'], $manager->store );
115
116 foreach ( [
117 'config' => '$options[\'config\'] must be an instance of Config',
118 'logger' => '$options[\'logger\'] must be an instance of LoggerInterface',
119 'store' => '$options[\'store\'] must be an instance of BagOStuff',
120 ] as $key => $error ) {
121 try {
122 new SessionManager( [ $key => new \stdClass ] );
123 $this->fail( 'Expected exception not thrown' );
124 } catch ( \InvalidArgumentException $ex ) {
125 $this->assertSame( $error, $ex->getMessage() );
126 }
127 }
128 }
129
130 public function testGetSessionForRequest() {
131 $manager = $this->getManager();
132 $request = new \FauxRequest();
133 $request->unpersist1 = false;
134 $request->unpersist2 = false;
135
136 $id1 = '';
137 $id2 = '';
138 $idEmpty = 'empty-session-------------------';
139
140 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
141 ->setMethods(
142 [ 'provideSessionInfo', 'newSessionInfo', '__toString', 'describe', 'unpersistSession' ]
143 );
144
145 $provider1 = $providerBuilder->getMock();
146 $provider1->expects( $this->any() )->method( 'provideSessionInfo' )
147 ->with( $this->identicalTo( $request ) )
148 ->will( $this->returnCallback( function ( $request ) {
149 return $request->info1;
150 } ) );
151 $provider1->expects( $this->any() )->method( 'newSessionInfo' )
152 ->will( $this->returnCallback( function () use ( $idEmpty, $provider1 ) {
153 return new SessionInfo( SessionInfo::MIN_PRIORITY, [
154 'provider' => $provider1,
155 'id' => $idEmpty,
156 'persisted' => true,
157 'idIsSafe' => true,
158 ] );
159 } ) );
160 $provider1->expects( $this->any() )->method( '__toString' )
161 ->will( $this->returnValue( 'Provider1' ) );
162 $provider1->expects( $this->any() )->method( 'describe' )
163 ->will( $this->returnValue( '#1 sessions' ) );
164 $provider1->expects( $this->any() )->method( 'unpersistSession' )
165 ->will( $this->returnCallback( function ( $request ) {
166 $request->unpersist1 = true;
167 } ) );
168
169 $provider2 = $providerBuilder->getMock();
170 $provider2->expects( $this->any() )->method( 'provideSessionInfo' )
171 ->with( $this->identicalTo( $request ) )
172 ->will( $this->returnCallback( function ( $request ) {
173 return $request->info2;
174 } ) );
175 $provider2->expects( $this->any() )->method( '__toString' )
176 ->will( $this->returnValue( 'Provider2' ) );
177 $provider2->expects( $this->any() )->method( 'describe' )
178 ->will( $this->returnValue( '#2 sessions' ) );
179 $provider2->expects( $this->any() )->method( 'unpersistSession' )
180 ->will( $this->returnCallback( function ( $request ) {
181 $request->unpersist2 = true;
182 } ) );
183
184 $this->config->set( 'SessionProviders', [
185 $this->objectCacheDef( $provider1 ),
186 $this->objectCacheDef( $provider2 ),
187 ] );
188
189 // No provider returns info
190 $request->info1 = null;
191 $request->info2 = null;
192 $session = $manager->getSessionForRequest( $request );
193 $this->assertInstanceOf( Session::class, $session );
194 $this->assertSame( $idEmpty, $session->getId() );
195 $this->assertFalse( $request->unpersist1 );
196 $this->assertFalse( $request->unpersist2 );
197
198 // Both providers return info, picks best one
199 $request->info1 = new SessionInfo( SessionInfo::MIN_PRIORITY + 1, [
200 'provider' => $provider1,
201 'id' => ( $id1 = $manager->generateSessionId() ),
202 'persisted' => true,
203 'idIsSafe' => true,
204 ] );
205 $request->info2 = new SessionInfo( SessionInfo::MIN_PRIORITY + 2, [
206 'provider' => $provider2,
207 'id' => ( $id2 = $manager->generateSessionId() ),
208 'persisted' => true,
209 'idIsSafe' => true,
210 ] );
211 $session = $manager->getSessionForRequest( $request );
212 $this->assertInstanceOf( Session::class, $session );
213 $this->assertSame( $id2, $session->getId() );
214 $this->assertFalse( $request->unpersist1 );
215 $this->assertFalse( $request->unpersist2 );
216
217 $request->info1 = new SessionInfo( SessionInfo::MIN_PRIORITY + 2, [
218 'provider' => $provider1,
219 'id' => ( $id1 = $manager->generateSessionId() ),
220 'persisted' => true,
221 'idIsSafe' => true,
222 ] );
223 $request->info2 = new SessionInfo( SessionInfo::MIN_PRIORITY + 1, [
224 'provider' => $provider2,
225 'id' => ( $id2 = $manager->generateSessionId() ),
226 'persisted' => true,
227 'idIsSafe' => true,
228 ] );
229 $session = $manager->getSessionForRequest( $request );
230 $this->assertInstanceOf( Session::class, $session );
231 $this->assertSame( $id1, $session->getId() );
232 $this->assertFalse( $request->unpersist1 );
233 $this->assertFalse( $request->unpersist2 );
234
235 // Tied priorities
236 $request->info1 = new SessionInfo( SessionInfo::MAX_PRIORITY, [
237 'provider' => $provider1,
238 'id' => ( $id1 = $manager->generateSessionId() ),
239 'persisted' => true,
240 'userInfo' => UserInfo::newAnonymous(),
241 'idIsSafe' => true,
242 ] );
243 $request->info2 = new SessionInfo( SessionInfo::MAX_PRIORITY, [
244 'provider' => $provider2,
245 'id' => ( $id2 = $manager->generateSessionId() ),
246 'persisted' => true,
247 'userInfo' => UserInfo::newAnonymous(),
248 'idIsSafe' => true,
249 ] );
250 try {
251 $manager->getSessionForRequest( $request );
252 $this->fail( 'Expcected exception not thrown' );
253 } catch ( \OverflowException $ex ) {
254 $this->assertStringStartsWith(
255 'Multiple sessions for this request tied for top priority: ',
256 $ex->getMessage()
257 );
258 $this->assertCount( 2, $ex->sessionInfos );
259 $this->assertContains( $request->info1, $ex->sessionInfos );
260 $this->assertContains( $request->info2, $ex->sessionInfos );
261 }
262 $this->assertFalse( $request->unpersist1 );
263 $this->assertFalse( $request->unpersist2 );
264
265 // Bad provider
266 $request->info1 = new SessionInfo( SessionInfo::MAX_PRIORITY, [
267 'provider' => $provider2,
268 'id' => ( $id1 = $manager->generateSessionId() ),
269 'persisted' => true,
270 'idIsSafe' => true,
271 ] );
272 $request->info2 = null;
273 try {
274 $manager->getSessionForRequest( $request );
275 $this->fail( 'Expcected exception not thrown' );
276 } catch ( \UnexpectedValueException $ex ) {
277 $this->assertSame(
278 'Provider1 returned session info for a different provider: ' . $request->info1,
279 $ex->getMessage()
280 );
281 }
282 $this->assertFalse( $request->unpersist1 );
283 $this->assertFalse( $request->unpersist2 );
284
285 // Unusable session info
286 $this->logger->setCollect( true );
287 $request->info1 = new SessionInfo( SessionInfo::MAX_PRIORITY, [
288 'provider' => $provider1,
289 'id' => ( $id1 = $manager->generateSessionId() ),
290 'persisted' => true,
291 'userInfo' => UserInfo::newFromName( 'UTSysop', false ),
292 'idIsSafe' => true,
293 ] );
294 $request->info2 = new SessionInfo( SessionInfo::MIN_PRIORITY, [
295 'provider' => $provider2,
296 'id' => ( $id2 = $manager->generateSessionId() ),
297 'persisted' => true,
298 'idIsSafe' => true,
299 ] );
300 $session = $manager->getSessionForRequest( $request );
301 $this->assertInstanceOf( Session::class, $session );
302 $this->assertSame( $id2, $session->getId() );
303 $this->logger->setCollect( false );
304 $this->assertTrue( $request->unpersist1 );
305 $this->assertFalse( $request->unpersist2 );
306 $request->unpersist1 = false;
307
308 $this->logger->setCollect( true );
309 $request->info1 = new SessionInfo( SessionInfo::MAX_PRIORITY, [
310 'provider' => $provider1,
311 'id' => ( $id1 = $manager->generateSessionId() ),
312 'persisted' => true,
313 'idIsSafe' => true,
314 ] );
315 $request->info2 = new SessionInfo( SessionInfo::MAX_PRIORITY, [
316 'provider' => $provider2,
317 'id' => ( $id2 = $manager->generateSessionId() ),
318 'persisted' => true,
319 'userInfo' => UserInfo::newFromName( 'UTSysop', false ),
320 'idIsSafe' => true,
321 ] );
322 $session = $manager->getSessionForRequest( $request );
323 $this->assertInstanceOf( Session::class, $session );
324 $this->assertSame( $id1, $session->getId() );
325 $this->logger->setCollect( false );
326 $this->assertFalse( $request->unpersist1 );
327 $this->assertTrue( $request->unpersist2 );
328 $request->unpersist2 = false;
329
330 // Unpersisted session ID
331 $request->info1 = new SessionInfo( SessionInfo::MAX_PRIORITY, [
332 'provider' => $provider1,
333 'id' => ( $id1 = $manager->generateSessionId() ),
334 'persisted' => false,
335 'userInfo' => UserInfo::newFromName( 'UTSysop', true ),
336 'idIsSafe' => true,
337 ] );
338 $request->info2 = null;
339 $session = $manager->getSessionForRequest( $request );
340 $this->assertInstanceOf( Session::class, $session );
341 $this->assertSame( $id1, $session->getId() );
342 $this->assertTrue( $request->unpersist1 ); // The saving of the session does it
343 $this->assertFalse( $request->unpersist2 );
344 $session->persist();
345 $this->assertTrue( $session->isPersistent(), 'sanity check' );
346 }
347
348 public function testGetSessionById() {
349 $manager = $this->getManager();
350 try {
351 $manager->getSessionById( 'bad' );
352 $this->fail( 'Expected exception not thrown' );
353 } catch ( \InvalidArgumentException $ex ) {
354 $this->assertSame( 'Invalid session ID', $ex->getMessage() );
355 }
356
357 // Unknown session ID
358 $id = $manager->generateSessionId();
359 $session = $manager->getSessionById( $id, true );
360 $this->assertInstanceOf( Session::class, $session );
361 $this->assertSame( $id, $session->getId() );
362
363 $id = $manager->generateSessionId();
364 $this->assertNull( $manager->getSessionById( $id, false ) );
365
366 // Known but unloadable session ID
367 $this->logger->setCollect( true );
368 $id = $manager->generateSessionId();
369 $this->store->setSession( $id, [ 'metadata' => [
370 'userId' => User::idFromName( 'UTSysop' ),
371 'userToken' => 'bad',
372 ] ] );
373
374 $this->assertNull( $manager->getSessionById( $id, true ) );
375 $this->assertNull( $manager->getSessionById( $id, false ) );
376 $this->logger->setCollect( false );
377
378 // Known session ID
379 $this->store->setSession( $id, [] );
380 $session = $manager->getSessionById( $id, false );
381 $this->assertInstanceOf( Session::class, $session );
382 $this->assertSame( $id, $session->getId() );
383
384 // Store isn't checked if the session is already loaded
385 $this->store->setSession( $id, [ 'metadata' => [
386 'userId' => User::idFromName( 'UTSysop' ),
387 'userToken' => 'bad',
388 ] ] );
389 $session2 = $manager->getSessionById( $id, false );
390 $this->assertInstanceOf( Session::class, $session2 );
391 $this->assertSame( $id, $session2->getId() );
392 unset( $session, $session2 );
393 $this->logger->setCollect( true );
394 $this->assertNull( $manager->getSessionById( $id, true ) );
395 $this->logger->setCollect( false );
396
397 // Failure to create an empty session
398 $manager = $this->getManager();
399 $provider = $this->getMockBuilder( 'DummySessionProvider' )
400 ->setMethods( [ 'provideSessionInfo', 'newSessionInfo', '__toString' ] )
401 ->getMock();
402 $provider->expects( $this->any() )->method( 'provideSessionInfo' )
403 ->will( $this->returnValue( null ) );
404 $provider->expects( $this->any() )->method( 'newSessionInfo' )
405 ->will( $this->returnValue( null ) );
406 $provider->expects( $this->any() )->method( '__toString' )
407 ->will( $this->returnValue( 'MockProvider' ) );
408 $this->config->set( 'SessionProviders', [
409 $this->objectCacheDef( $provider ),
410 ] );
411 $this->logger->setCollect( true );
412 $this->assertNull( $manager->getSessionById( $id, true ) );
413 $this->logger->setCollect( false );
414 $this->assertSame( [
415 [ LogLevel::ERROR, 'Failed to create empty session: {exception}' ]
416 ], $this->logger->getBuffer() );
417 }
418
419 public function testGetEmptySession() {
420 $manager = $this->getManager();
421 $pmanager = \TestingAccessWrapper::newFromObject( $manager );
422 $request = new \FauxRequest();
423
424 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
425 ->setMethods( [ 'provideSessionInfo', 'newSessionInfo', '__toString' ] );
426
427 $expectId = null;
428 $info1 = null;
429 $info2 = null;
430
431 $provider1 = $providerBuilder->getMock();
432 $provider1->expects( $this->any() )->method( 'provideSessionInfo' )
433 ->will( $this->returnValue( null ) );
434 $provider1->expects( $this->any() )->method( 'newSessionInfo' )
435 ->with( $this->callback( function ( $id ) use ( &$expectId ) {
436 return $id === $expectId;
437 } ) )
438 ->will( $this->returnCallback( function () use ( &$info1 ) {
439 return $info1;
440 } ) );
441 $provider1->expects( $this->any() )->method( '__toString' )
442 ->will( $this->returnValue( 'MockProvider1' ) );
443
444 $provider2 = $providerBuilder->getMock();
445 $provider2->expects( $this->any() )->method( 'provideSessionInfo' )
446 ->will( $this->returnValue( null ) );
447 $provider2->expects( $this->any() )->method( 'newSessionInfo' )
448 ->with( $this->callback( function ( $id ) use ( &$expectId ) {
449 return $id === $expectId;
450 } ) )
451 ->will( $this->returnCallback( function () use ( &$info2 ) {
452 return $info2;
453 } ) );
454 $provider1->expects( $this->any() )->method( '__toString' )
455 ->will( $this->returnValue( 'MockProvider2' ) );
456
457 $this->config->set( 'SessionProviders', [
458 $this->objectCacheDef( $provider1 ),
459 $this->objectCacheDef( $provider2 ),
460 ] );
461
462 // No info
463 $expectId = null;
464 $info1 = null;
465 $info2 = null;
466 try {
467 $manager->getEmptySession();
468 $this->fail( 'Expected exception not thrown' );
469 } catch ( \UnexpectedValueException $ex ) {
470 $this->assertSame(
471 'No provider could provide an empty session!',
472 $ex->getMessage()
473 );
474 }
475
476 // Info
477 $expectId = null;
478 $info1 = new SessionInfo( SessionInfo::MIN_PRIORITY, [
479 'provider' => $provider1,
480 'id' => 'empty---------------------------',
481 'persisted' => true,
482 'idIsSafe' => true,
483 ] );
484 $info2 = null;
485 $session = $manager->getEmptySession();
486 $this->assertInstanceOf( Session::class, $session );
487 $this->assertSame( 'empty---------------------------', $session->getId() );
488
489 // Info, explicitly
490 $expectId = 'expected------------------------';
491 $info1 = new SessionInfo( SessionInfo::MIN_PRIORITY, [
492 'provider' => $provider1,
493 'id' => $expectId,
494 'persisted' => true,
495 'idIsSafe' => true,
496 ] );
497 $info2 = null;
498 $session = $pmanager->getEmptySessionInternal( null, $expectId );
499 $this->assertInstanceOf( Session::class, $session );
500 $this->assertSame( $expectId, $session->getId() );
501
502 // Wrong ID
503 $expectId = 'expected-----------------------2';
504 $info1 = new SessionInfo( SessionInfo::MIN_PRIORITY, [
505 'provider' => $provider1,
506 'id' => "un$expectId",
507 'persisted' => true,
508 'idIsSafe' => true,
509 ] );
510 $info2 = null;
511 try {
512 $pmanager->getEmptySessionInternal( null, $expectId );
513 $this->fail( 'Expected exception not thrown' );
514 } catch ( \UnexpectedValueException $ex ) {
515 $this->assertSame(
516 'MockProvider1 returned empty session info with a wrong id: ' .
517 "un$expectId != $expectId",
518 $ex->getMessage()
519 );
520 }
521
522 // Unsafe ID
523 $expectId = 'expected-----------------------2';
524 $info1 = new SessionInfo( SessionInfo::MIN_PRIORITY, [
525 'provider' => $provider1,
526 'id' => $expectId,
527 'persisted' => true,
528 ] );
529 $info2 = null;
530 try {
531 $pmanager->getEmptySessionInternal( null, $expectId );
532 $this->fail( 'Expected exception not thrown' );
533 } catch ( \UnexpectedValueException $ex ) {
534 $this->assertSame(
535 'MockProvider1 returned empty session info with id flagged unsafe',
536 $ex->getMessage()
537 );
538 }
539
540 // Wrong provider
541 $expectId = null;
542 $info1 = new SessionInfo( SessionInfo::MIN_PRIORITY, [
543 'provider' => $provider2,
544 'id' => 'empty---------------------------',
545 'persisted' => true,
546 'idIsSafe' => true,
547 ] );
548 $info2 = null;
549 try {
550 $manager->getEmptySession();
551 $this->fail( 'Expected exception not thrown' );
552 } catch ( \UnexpectedValueException $ex ) {
553 $this->assertSame(
554 'MockProvider1 returned an empty session info for a different provider: ' . $info1,
555 $ex->getMessage()
556 );
557 }
558
559 // Highest priority wins
560 $expectId = null;
561 $info1 = new SessionInfo( SessionInfo::MIN_PRIORITY + 1, [
562 'provider' => $provider1,
563 'id' => 'empty1--------------------------',
564 'persisted' => true,
565 'idIsSafe' => true,
566 ] );
567 $info2 = new SessionInfo( SessionInfo::MIN_PRIORITY, [
568 'provider' => $provider2,
569 'id' => 'empty2--------------------------',
570 'persisted' => true,
571 'idIsSafe' => true,
572 ] );
573 $session = $manager->getEmptySession();
574 $this->assertInstanceOf( Session::class, $session );
575 $this->assertSame( 'empty1--------------------------', $session->getId() );
576
577 $expectId = null;
578 $info1 = new SessionInfo( SessionInfo::MIN_PRIORITY + 1, [
579 'provider' => $provider1,
580 'id' => 'empty1--------------------------',
581 'persisted' => true,
582 'idIsSafe' => true,
583 ] );
584 $info2 = new SessionInfo( SessionInfo::MIN_PRIORITY + 2, [
585 'provider' => $provider2,
586 'id' => 'empty2--------------------------',
587 'persisted' => true,
588 'idIsSafe' => true,
589 ] );
590 $session = $manager->getEmptySession();
591 $this->assertInstanceOf( Session::class, $session );
592 $this->assertSame( 'empty2--------------------------', $session->getId() );
593
594 // Tied priorities throw an exception
595 $expectId = null;
596 $info1 = new SessionInfo( SessionInfo::MIN_PRIORITY, [
597 'provider' => $provider1,
598 'id' => 'empty1--------------------------',
599 'persisted' => true,
600 'userInfo' => UserInfo::newAnonymous(),
601 'idIsSafe' => true,
602 ] );
603 $info2 = new SessionInfo( SessionInfo::MIN_PRIORITY, [
604 'provider' => $provider2,
605 'id' => 'empty2--------------------------',
606 'persisted' => true,
607 'userInfo' => UserInfo::newAnonymous(),
608 'idIsSafe' => true,
609 ] );
610 try {
611 $manager->getEmptySession();
612 $this->fail( 'Expected exception not thrown' );
613 } catch ( \UnexpectedValueException $ex ) {
614 $this->assertStringStartsWith(
615 'Multiple empty sessions tied for top priority: ',
616 $ex->getMessage()
617 );
618 }
619
620 // Bad id
621 try {
622 $pmanager->getEmptySessionInternal( null, 'bad' );
623 $this->fail( 'Expected exception not thrown' );
624 } catch ( \InvalidArgumentException $ex ) {
625 $this->assertSame( 'Invalid session ID', $ex->getMessage() );
626 }
627
628 // Session already exists
629 $expectId = 'expected-----------------------3';
630 $this->store->setSessionMeta( $expectId, [
631 'provider' => 'MockProvider2',
632 'userId' => 0,
633 'userName' => null,
634 'userToken' => null,
635 ] );
636 try {
637 $pmanager->getEmptySessionInternal( null, $expectId );
638 $this->fail( 'Expected exception not thrown' );
639 } catch ( \InvalidArgumentException $ex ) {
640 $this->assertSame( 'Session ID already exists', $ex->getMessage() );
641 }
642 }
643
644 public function testInvalidateSessionsForUser() {
645 $user = User::newFromName( 'UTSysop' );
646 $manager = $this->getManager();
647
648 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
649 ->setMethods( [ 'invalidateSessionsForUser', '__toString' ] );
650
651 $provider1 = $providerBuilder->getMock();
652 $provider1->expects( $this->once() )->method( 'invalidateSessionsForUser' )
653 ->with( $this->identicalTo( $user ) );
654 $provider1->expects( $this->any() )->method( '__toString' )
655 ->will( $this->returnValue( 'MockProvider1' ) );
656
657 $provider2 = $providerBuilder->getMock();
658 $provider2->expects( $this->once() )->method( 'invalidateSessionsForUser' )
659 ->with( $this->identicalTo( $user ) );
660 $provider2->expects( $this->any() )->method( '__toString' )
661 ->will( $this->returnValue( 'MockProvider2' ) );
662
663 $this->config->set( 'SessionProviders', [
664 $this->objectCacheDef( $provider1 ),
665 $this->objectCacheDef( $provider2 ),
666 ] );
667
668 $oldToken = $user->getToken( true );
669 $manager->invalidateSessionsForUser( $user );
670 $this->assertNotEquals( $oldToken, $user->getToken() );
671 }
672
673 public function testGetVaryHeaders() {
674 $manager = $this->getManager();
675
676 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
677 ->setMethods( [ 'getVaryHeaders', '__toString' ] );
678
679 $provider1 = $providerBuilder->getMock();
680 $provider1->expects( $this->once() )->method( 'getVaryHeaders' )
681 ->will( $this->returnValue( [
682 'Foo' => null,
683 'Bar' => [ 'X', 'Bar1' ],
684 'Quux' => null,
685 ] ) );
686 $provider1->expects( $this->any() )->method( '__toString' )
687 ->will( $this->returnValue( 'MockProvider1' ) );
688
689 $provider2 = $providerBuilder->getMock();
690 $provider2->expects( $this->once() )->method( 'getVaryHeaders' )
691 ->will( $this->returnValue( [
692 'Baz' => null,
693 'Bar' => [ 'X', 'Bar2' ],
694 'Quux' => [ 'Quux' ],
695 ] ) );
696 $provider2->expects( $this->any() )->method( '__toString' )
697 ->will( $this->returnValue( 'MockProvider2' ) );
698
699 $this->config->set( 'SessionProviders', [
700 $this->objectCacheDef( $provider1 ),
701 $this->objectCacheDef( $provider2 ),
702 ] );
703
704 $expect = [
705 'Foo' => [],
706 'Bar' => [ 'X', 'Bar1', 3 => 'Bar2' ],
707 'Quux' => [ 'Quux' ],
708 'Baz' => [],
709 ];
710
711 $this->assertEquals( $expect, $manager->getVaryHeaders() );
712
713 // Again, to ensure it's cached
714 $this->assertEquals( $expect, $manager->getVaryHeaders() );
715 }
716
717 public function testGetVaryCookies() {
718 $manager = $this->getManager();
719
720 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
721 ->setMethods( [ 'getVaryCookies', '__toString' ] );
722
723 $provider1 = $providerBuilder->getMock();
724 $provider1->expects( $this->once() )->method( 'getVaryCookies' )
725 ->will( $this->returnValue( [ 'Foo', 'Bar' ] ) );
726 $provider1->expects( $this->any() )->method( '__toString' )
727 ->will( $this->returnValue( 'MockProvider1' ) );
728
729 $provider2 = $providerBuilder->getMock();
730 $provider2->expects( $this->once() )->method( 'getVaryCookies' )
731 ->will( $this->returnValue( [ 'Foo', 'Baz' ] ) );
732 $provider2->expects( $this->any() )->method( '__toString' )
733 ->will( $this->returnValue( 'MockProvider2' ) );
734
735 $this->config->set( 'SessionProviders', [
736 $this->objectCacheDef( $provider1 ),
737 $this->objectCacheDef( $provider2 ),
738 ] );
739
740 $expect = [ 'Foo', 'Bar', 'Baz' ];
741
742 $this->assertEquals( $expect, $manager->getVaryCookies() );
743
744 // Again, to ensure it's cached
745 $this->assertEquals( $expect, $manager->getVaryCookies() );
746 }
747
748 public function testGetProviders() {
749 $realManager = $this->getManager();
750 $manager = \TestingAccessWrapper::newFromObject( $realManager );
751
752 $this->config->set( 'SessionProviders', [
753 [ 'class' => 'DummySessionProvider' ],
754 ] );
755 $providers = $manager->getProviders();
756 $this->assertArrayHasKey( 'DummySessionProvider', $providers );
757 $provider = \TestingAccessWrapper::newFromObject( $providers['DummySessionProvider'] );
758 $this->assertSame( $manager->logger, $provider->logger );
759 $this->assertSame( $manager->config, $provider->config );
760 $this->assertSame( $realManager, $provider->getManager() );
761
762 $this->config->set( 'SessionProviders', [
763 [ 'class' => 'DummySessionProvider' ],
764 [ 'class' => 'DummySessionProvider' ],
765 ] );
766 $manager->sessionProviders = null;
767 try {
768 $manager->getProviders();
769 $this->fail( 'Expected exception not thrown' );
770 } catch ( \UnexpectedValueException $ex ) {
771 $this->assertSame(
772 'Duplicate provider name "DummySessionProvider"',
773 $ex->getMessage()
774 );
775 }
776 }
777
778 public function testShutdown() {
779 $manager = \TestingAccessWrapper::newFromObject( $this->getManager() );
780 $manager->setLogger( new \Psr\Log\NullLogger() );
781
782 $mock = $this->getMockBuilder( 'stdClass' )
783 ->setMethods( [ 'shutdown' ] )->getMock();
784 $mock->expects( $this->once() )->method( 'shutdown' );
785
786 $manager->allSessionBackends = [ $mock ];
787 $manager->shutdown();
788 }
789
790 public function testGetSessionFromInfo() {
791 $manager = \TestingAccessWrapper::newFromObject( $this->getManager() );
792 $request = new \FauxRequest();
793
794 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
795
796 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
797 'provider' => $manager->getProvider( 'DummySessionProvider' ),
798 'id' => $id,
799 'persisted' => true,
800 'userInfo' => UserInfo::newFromName( 'UTSysop', true ),
801 'idIsSafe' => true,
802 ] );
803 \TestingAccessWrapper::newFromObject( $info )->idIsSafe = true;
804 $session1 = \TestingAccessWrapper::newFromObject(
805 $manager->getSessionFromInfo( $info, $request )
806 );
807 $session2 = \TestingAccessWrapper::newFromObject(
808 $manager->getSessionFromInfo( $info, $request )
809 );
810
811 $this->assertSame( $session1->backend, $session2->backend );
812 $this->assertNotEquals( $session1->index, $session2->index );
813 $this->assertSame( $session1->getSessionId(), $session2->getSessionId() );
814 $this->assertSame( $id, $session1->getId() );
815
816 \TestingAccessWrapper::newFromObject( $info )->idIsSafe = false;
817 $session3 = $manager->getSessionFromInfo( $info, $request );
818 $this->assertNotSame( $id, $session3->getId() );
819 }
820
821 public function testBackendRegistration() {
822 $manager = $this->getManager();
823
824 $session = $manager->getSessionForRequest( new \FauxRequest );
825 $backend = \TestingAccessWrapper::newFromObject( $session )->backend;
826 $sessionId = $session->getSessionId();
827 $id = (string)$sessionId;
828
829 $this->assertSame( $sessionId, $manager->getSessionById( $id, true )->getSessionId() );
830
831 $manager->changeBackendId( $backend );
832 $this->assertSame( $sessionId, $session->getSessionId() );
833 $this->assertNotEquals( $id, (string)$sessionId );
834 $id = (string)$sessionId;
835
836 $this->assertSame( $sessionId, $manager->getSessionById( $id, true )->getSessionId() );
837
838 // Destruction of the session here causes the backend to be deregistered
839 $session = null;
840
841 try {
842 $manager->changeBackendId( $backend );
843 $this->fail( 'Expected exception not thrown' );
844 } catch ( \InvalidArgumentException $ex ) {
845 $this->assertSame(
846 'Backend was not registered with this SessionManager', $ex->getMessage()
847 );
848 }
849
850 try {
851 $manager->deregisterSessionBackend( $backend );
852 $this->fail( 'Expected exception not thrown' );
853 } catch ( \InvalidArgumentException $ex ) {
854 $this->assertSame(
855 'Backend was not registered with this SessionManager', $ex->getMessage()
856 );
857 }
858
859 $session = $manager->getSessionById( $id, true );
860 $this->assertSame( $sessionId, $session->getSessionId() );
861 }
862
863 public function testGenerateSessionId() {
864 $manager = $this->getManager();
865
866 $id = $manager->generateSessionId();
867 $this->assertTrue( SessionManager::validateSessionId( $id ), "Generated ID: $id" );
868 }
869
870 public function testPreventSessionsForUser() {
871 $manager = $this->getManager();
872
873 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
874 ->setMethods( [ 'preventSessionsForUser', '__toString' ] );
875
876 $provider1 = $providerBuilder->getMock();
877 $provider1->expects( $this->once() )->method( 'preventSessionsForUser' )
878 ->with( $this->equalTo( 'UTSysop' ) );
879 $provider1->expects( $this->any() )->method( '__toString' )
880 ->will( $this->returnValue( 'MockProvider1' ) );
881
882 $this->config->set( 'SessionProviders', [
883 $this->objectCacheDef( $provider1 ),
884 ] );
885
886 $this->assertFalse( $manager->isUserSessionPrevented( 'UTSysop' ) );
887 $manager->preventSessionsForUser( 'UTSysop' );
888 $this->assertTrue( $manager->isUserSessionPrevented( 'UTSysop' ) );
889 }
890
891 public function testLoadSessionInfoFromStore() {
892 $manager = $this->getManager();
893 $logger = new \TestLogger( true );
894 $manager->setLogger( $logger );
895 $request = new \FauxRequest();
896
897 // TestingAccessWrapper can't handle methods with reference arguments, sigh.
898 $rClass = new \ReflectionClass( $manager );
899 $rMethod = $rClass->getMethod( 'loadSessionInfoFromStore' );
900 $rMethod->setAccessible( true );
901 $loadSessionInfoFromStore = function ( &$info ) use ( $rMethod, $manager, $request ) {
902 return $rMethod->invokeArgs( $manager, [ &$info, $request ] );
903 };
904
905 $userInfo = UserInfo::newFromName( 'UTSysop', true );
906 $unverifiedUserInfo = UserInfo::newFromName( 'UTSysop', false );
907
908 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
909 $metadata = [
910 'userId' => $userInfo->getId(),
911 'userName' => $userInfo->getName(),
912 'userToken' => $userInfo->getToken( true ),
913 'provider' => 'Mock',
914 ];
915
916 $builder = $this->getMockBuilder( SessionProvider::class )
917 ->setMethods( [ '__toString', 'mergeMetadata', 'refreshSessionInfo' ] );
918
919 $provider = $builder->getMockForAbstractClass();
920 $provider->setManager( $manager );
921 $provider->expects( $this->any() )->method( 'persistsSessionId' )
922 ->will( $this->returnValue( true ) );
923 $provider->expects( $this->any() )->method( 'canChangeUser' )
924 ->will( $this->returnValue( true ) );
925 $provider->expects( $this->any() )->method( 'refreshSessionInfo' )
926 ->will( $this->returnValue( true ) );
927 $provider->expects( $this->any() )->method( '__toString' )
928 ->will( $this->returnValue( 'Mock' ) );
929 $provider->expects( $this->any() )->method( 'mergeMetadata' )
930 ->will( $this->returnCallback( function ( $a, $b ) {
931 if ( $b === [ 'Throw' ] ) {
932 throw new MetadataMergeException( 'no merge!' );
933 }
934 return [ 'Merged' ];
935 } ) );
936
937 $provider2 = $builder->getMockForAbstractClass();
938 $provider2->setManager( $manager );
939 $provider2->expects( $this->any() )->method( 'persistsSessionId' )
940 ->will( $this->returnValue( false ) );
941 $provider2->expects( $this->any() )->method( 'canChangeUser' )
942 ->will( $this->returnValue( false ) );
943 $provider2->expects( $this->any() )->method( '__toString' )
944 ->will( $this->returnValue( 'Mock2' ) );
945 $provider2->expects( $this->any() )->method( 'refreshSessionInfo' )
946 ->will( $this->returnCallback( function ( $info, $request, &$metadata ) {
947 $metadata['changed'] = true;
948 return true;
949 } ) );
950
951 $provider3 = $builder->getMockForAbstractClass();
952 $provider3->setManager( $manager );
953 $provider3->expects( $this->any() )->method( 'persistsSessionId' )
954 ->will( $this->returnValue( true ) );
955 $provider3->expects( $this->any() )->method( 'canChangeUser' )
956 ->will( $this->returnValue( true ) );
957 $provider3->expects( $this->once() )->method( 'refreshSessionInfo' )
958 ->will( $this->returnValue( false ) );
959 $provider3->expects( $this->any() )->method( '__toString' )
960 ->will( $this->returnValue( 'Mock3' ) );
961
962 \TestingAccessWrapper::newFromObject( $manager )->sessionProviders = [
963 (string)$provider => $provider,
964 (string)$provider2 => $provider2,
965 (string)$provider3 => $provider3,
966 ];
967
968 // No metadata, basic usage
969 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
970 'provider' => $provider,
971 'id' => $id,
972 'userInfo' => $userInfo
973 ] );
974 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
975 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
976 $this->assertFalse( $info->isIdSafe() );
977 $this->assertSame( [], $logger->getBuffer() );
978
979 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
980 'provider' => $provider,
981 'userInfo' => $userInfo
982 ] );
983 $this->assertTrue( $info->isIdSafe(), 'sanity check' );
984 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
985 $this->assertTrue( $info->isIdSafe() );
986 $this->assertSame( [], $logger->getBuffer() );
987
988 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
989 'provider' => $provider2,
990 'id' => $id,
991 'userInfo' => $userInfo
992 ] );
993 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
994 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
995 $this->assertTrue( $info->isIdSafe() );
996 $this->assertSame( [], $logger->getBuffer() );
997
998 // Unverified user, no metadata
999 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1000 'provider' => $provider,
1001 'id' => $id,
1002 'userInfo' => $unverifiedUserInfo
1003 ] );
1004 $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
1005 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1006 $this->assertSame( [
1007 [
1008 LogLevel::INFO,
1009 'Session "{session}": Unverified user provided and no metadata to auth it',
1010 ]
1011 ], $logger->getBuffer() );
1012 $logger->clearBuffer();
1013
1014 // No metadata, missing data
1015 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1016 'id' => $id,
1017 'userInfo' => $userInfo
1018 ] );
1019 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1020 $this->assertSame( [
1021 [ LogLevel::WARNING, 'Session "{session}": Null provider and no metadata' ],
1022 ], $logger->getBuffer() );
1023 $logger->clearBuffer();
1024
1025 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1026 'provider' => $provider,
1027 'id' => $id,
1028 ] );
1029 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1030 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1031 $this->assertInstanceOf( UserInfo::class, $info->getUserInfo() );
1032 $this->assertTrue( $info->getUserInfo()->isVerified() );
1033 $this->assertTrue( $info->getUserInfo()->isAnon() );
1034 $this->assertFalse( $info->isIdSafe() );
1035 $this->assertSame( [], $logger->getBuffer() );
1036
1037 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1038 'provider' => $provider2,
1039 'id' => $id,
1040 ] );
1041 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1042 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1043 $this->assertSame( [
1044 [ LogLevel::INFO, 'Session "{session}": No user provided and provider cannot set user' ]
1045 ], $logger->getBuffer() );
1046 $logger->clearBuffer();
1047
1048 // Incomplete/bad metadata
1049 $this->store->setRawSession( $id, true );
1050 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1051 $this->assertSame( [
1052 [ LogLevel::WARNING, 'Session "{session}": Bad data' ],
1053 ], $logger->getBuffer() );
1054 $logger->clearBuffer();
1055
1056 $this->store->setRawSession( $id, [ 'data' => [] ] );
1057 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1058 $this->assertSame( [
1059 [ LogLevel::WARNING, 'Session "{session}": Bad data structure' ],
1060 ], $logger->getBuffer() );
1061 $logger->clearBuffer();
1062
1063 $this->store->deleteSession( $id );
1064 $this->store->setRawSession( $id, [ 'metadata' => $metadata ] );
1065 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1066 $this->assertSame( [
1067 [ LogLevel::WARNING, 'Session "{session}": Bad data structure' ],
1068 ], $logger->getBuffer() );
1069 $logger->clearBuffer();
1070
1071 $this->store->setRawSession( $id, [ 'metadata' => $metadata, 'data' => true ] );
1072 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1073 $this->assertSame( [
1074 [ LogLevel::WARNING, 'Session "{session}": Bad data structure' ],
1075 ], $logger->getBuffer() );
1076 $logger->clearBuffer();
1077
1078 $this->store->setRawSession( $id, [ 'metadata' => true, 'data' => [] ] );
1079 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1080 $this->assertSame( [
1081 [ LogLevel::WARNING, 'Session "{session}": Bad data structure' ],
1082 ], $logger->getBuffer() );
1083 $logger->clearBuffer();
1084
1085 foreach ( $metadata as $key => $dummy ) {
1086 $tmp = $metadata;
1087 unset( $tmp[$key] );
1088 $this->store->setRawSession( $id, [ 'metadata' => $tmp, 'data' => [] ] );
1089 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1090 $this->assertSame( [
1091 [ LogLevel::WARNING, 'Session "{session}": Bad metadata' ],
1092 ], $logger->getBuffer() );
1093 $logger->clearBuffer();
1094 }
1095
1096 // Basic usage with metadata
1097 $this->store->setRawSession( $id, [ 'metadata' => $metadata, 'data' => [] ] );
1098 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1099 'provider' => $provider,
1100 'id' => $id,
1101 'userInfo' => $userInfo
1102 ] );
1103 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1104 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1105 $this->assertTrue( $info->isIdSafe() );
1106 $this->assertSame( [], $logger->getBuffer() );
1107
1108 // Mismatched provider
1109 $this->store->setSessionMeta( $id, [ 'provider' => 'Bad' ] + $metadata );
1110 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1111 'provider' => $provider,
1112 'id' => $id,
1113 'userInfo' => $userInfo
1114 ] );
1115 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1116 $this->assertSame( [
1117 [ LogLevel::WARNING, 'Session "{session}": Wrong provider Bad !== Mock' ],
1118 ], $logger->getBuffer() );
1119 $logger->clearBuffer();
1120
1121 // Unknown provider
1122 $this->store->setSessionMeta( $id, [ 'provider' => 'Bad' ] + $metadata );
1123 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1124 'id' => $id,
1125 'userInfo' => $userInfo
1126 ] );
1127 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1128 $this->assertSame( [
1129 [ LogLevel::WARNING, 'Session "{session}": Unknown provider Bad' ],
1130 ], $logger->getBuffer() );
1131 $logger->clearBuffer();
1132
1133 // Fill in provider
1134 $this->store->setSessionMeta( $id, $metadata );
1135 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1136 'id' => $id,
1137 'userInfo' => $userInfo
1138 ] );
1139 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1140 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1141 $this->assertTrue( $info->isIdSafe() );
1142 $this->assertSame( [], $logger->getBuffer() );
1143
1144 // Bad user metadata
1145 $this->store->setSessionMeta( $id, [ 'userId' => -1, 'userToken' => null ] + $metadata );
1146 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1147 'provider' => $provider,
1148 'id' => $id,
1149 ] );
1150 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1151 $this->assertSame( [
1152 [ LogLevel::ERROR, 'Session "{session}": {exception}' ],
1153 ], $logger->getBuffer() );
1154 $logger->clearBuffer();
1155
1156 $this->store->setSessionMeta(
1157 $id, [ 'userId' => 0, 'userName' => '<X>', 'userToken' => null ] + $metadata
1158 );
1159 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1160 'provider' => $provider,
1161 'id' => $id,
1162 ] );
1163 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1164 $this->assertSame( [
1165 [ LogLevel::ERROR, 'Session "{session}": {exception}', ],
1166 ], $logger->getBuffer() );
1167 $logger->clearBuffer();
1168
1169 // Mismatched user by ID
1170 $this->store->setSessionMeta(
1171 $id, [ 'userId' => $userInfo->getId() + 1, 'userToken' => null ] + $metadata
1172 );
1173 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1174 'provider' => $provider,
1175 'id' => $id,
1176 'userInfo' => $userInfo
1177 ] );
1178 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1179 $this->assertSame( [
1180 [ LogLevel::WARNING, 'Session "{session}": User ID mismatch, {uid_a} !== {uid_b}' ],
1181 ], $logger->getBuffer() );
1182 $logger->clearBuffer();
1183
1184 // Mismatched user by name
1185 $this->store->setSessionMeta(
1186 $id, [ 'userId' => 0, 'userName' => 'X', 'userToken' => null ] + $metadata
1187 );
1188 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1189 'provider' => $provider,
1190 'id' => $id,
1191 'userInfo' => $userInfo
1192 ] );
1193 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1194 $this->assertSame( [
1195 [ LogLevel::WARNING, 'Session "{session}": User name mismatch, {uname_a} !== {uname_b}' ],
1196 ], $logger->getBuffer() );
1197 $logger->clearBuffer();
1198
1199 // ID matches, name doesn't
1200 $this->store->setSessionMeta(
1201 $id, [ 'userId' => $userInfo->getId(), 'userName' => 'X', 'userToken' => null ] + $metadata
1202 );
1203 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1204 'provider' => $provider,
1205 'id' => $id,
1206 'userInfo' => $userInfo
1207 ] );
1208 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1209 $this->assertSame( [
1210 [
1211 LogLevel::WARNING,
1212 'Session "{session}": User ID matched but name didn\'t (rename?), {uname_a} !== {uname_b}'
1213 ],
1214 ], $logger->getBuffer() );
1215 $logger->clearBuffer();
1216
1217 // Mismatched anon user
1218 $this->store->setSessionMeta(
1219 $id, [ 'userId' => 0, 'userName' => null, 'userToken' => null ] + $metadata
1220 );
1221 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1222 'provider' => $provider,
1223 'id' => $id,
1224 'userInfo' => $userInfo
1225 ] );
1226 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1227 $this->assertSame( [
1228 [
1229 LogLevel::WARNING,
1230 'Session "{session}": Metadata has an anonymous user, ' .
1231 'but a non-anon user was provided',
1232 ],
1233 ], $logger->getBuffer() );
1234 $logger->clearBuffer();
1235
1236 // Lookup user by ID
1237 $this->store->setSessionMeta( $id, [ 'userToken' => null ] + $metadata );
1238 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1239 'provider' => $provider,
1240 'id' => $id,
1241 ] );
1242 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1243 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1244 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1245 $this->assertTrue( $info->isIdSafe() );
1246 $this->assertSame( [], $logger->getBuffer() );
1247
1248 // Lookup user by name
1249 $this->store->setSessionMeta(
1250 $id, [ 'userId' => 0, 'userName' => 'UTSysop', 'userToken' => null ] + $metadata
1251 );
1252 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1253 'provider' => $provider,
1254 'id' => $id,
1255 ] );
1256 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1257 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1258 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1259 $this->assertTrue( $info->isIdSafe() );
1260 $this->assertSame( [], $logger->getBuffer() );
1261
1262 // Lookup anonymous user
1263 $this->store->setSessionMeta(
1264 $id, [ 'userId' => 0, 'userName' => null, 'userToken' => null ] + $metadata
1265 );
1266 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1267 'provider' => $provider,
1268 'id' => $id,
1269 ] );
1270 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1271 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1272 $this->assertTrue( $info->getUserInfo()->isAnon() );
1273 $this->assertTrue( $info->isIdSafe() );
1274 $this->assertSame( [], $logger->getBuffer() );
1275
1276 // Unverified user with metadata
1277 $this->store->setSessionMeta( $id, $metadata );
1278 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1279 'provider' => $provider,
1280 'id' => $id,
1281 'userInfo' => $unverifiedUserInfo
1282 ] );
1283 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1284 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1285 $this->assertTrue( $info->getUserInfo()->isVerified() );
1286 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1287 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1288 $this->assertTrue( $info->isIdSafe() );
1289 $this->assertSame( [], $logger->getBuffer() );
1290
1291 // Unverified user with metadata
1292 $this->store->setSessionMeta( $id, $metadata );
1293 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1294 'provider' => $provider,
1295 'id' => $id,
1296 'userInfo' => $unverifiedUserInfo
1297 ] );
1298 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1299 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1300 $this->assertTrue( $info->getUserInfo()->isVerified() );
1301 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1302 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1303 $this->assertTrue( $info->isIdSafe() );
1304 $this->assertSame( [], $logger->getBuffer() );
1305
1306 // Wrong token
1307 $this->store->setSessionMeta( $id, [ 'userToken' => 'Bad' ] + $metadata );
1308 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1309 'provider' => $provider,
1310 'id' => $id,
1311 'userInfo' => $userInfo
1312 ] );
1313 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1314 $this->assertSame( [
1315 [ LogLevel::WARNING, 'Session "{session}": User token mismatch' ],
1316 ], $logger->getBuffer() );
1317 $logger->clearBuffer();
1318
1319 // Provider metadata
1320 $this->store->setSessionMeta( $id, [ 'provider' => 'Mock2' ] + $metadata );
1321 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1322 'provider' => $provider2,
1323 'id' => $id,
1324 'userInfo' => $userInfo,
1325 'metadata' => [ 'Info' ],
1326 ] );
1327 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1328 $this->assertSame( [ 'Info', 'changed' => true ], $info->getProviderMetadata() );
1329 $this->assertSame( [], $logger->getBuffer() );
1330
1331 $this->store->setSessionMeta( $id, [ 'providerMetadata' => [ 'Saved' ] ] + $metadata );
1332 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1333 'provider' => $provider,
1334 'id' => $id,
1335 'userInfo' => $userInfo,
1336 ] );
1337 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1338 $this->assertSame( [ 'Saved' ], $info->getProviderMetadata() );
1339 $this->assertSame( [], $logger->getBuffer() );
1340
1341 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1342 'provider' => $provider,
1343 'id' => $id,
1344 'userInfo' => $userInfo,
1345 'metadata' => [ 'Info' ],
1346 ] );
1347 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1348 $this->assertSame( [ 'Merged' ], $info->getProviderMetadata() );
1349 $this->assertSame( [], $logger->getBuffer() );
1350
1351 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1352 'provider' => $provider,
1353 'id' => $id,
1354 'userInfo' => $userInfo,
1355 'metadata' => [ 'Throw' ],
1356 ] );
1357 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1358 $this->assertSame( [
1359 [
1360 LogLevel::WARNING,
1361 'Session "{session}": Metadata merge failed: {exception}',
1362 ],
1363 ], $logger->getBuffer() );
1364 $logger->clearBuffer();
1365
1366 // Remember from session
1367 $this->store->setSessionMeta( $id, $metadata );
1368 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1369 'provider' => $provider,
1370 'id' => $id,
1371 ] );
1372 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1373 $this->assertFalse( $info->wasRemembered() );
1374 $this->assertSame( [], $logger->getBuffer() );
1375
1376 $this->store->setSessionMeta( $id, [ 'remember' => true ] + $metadata );
1377 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1378 'provider' => $provider,
1379 'id' => $id,
1380 ] );
1381 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1382 $this->assertTrue( $info->wasRemembered() );
1383 $this->assertSame( [], $logger->getBuffer() );
1384
1385 $this->store->setSessionMeta( $id, [ 'remember' => false ] + $metadata );
1386 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1387 'provider' => $provider,
1388 'id' => $id,
1389 'userInfo' => $userInfo
1390 ] );
1391 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1392 $this->assertTrue( $info->wasRemembered() );
1393 $this->assertSame( [], $logger->getBuffer() );
1394
1395 // forceHTTPS from session
1396 $this->store->setSessionMeta( $id, $metadata );
1397 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1398 'provider' => $provider,
1399 'id' => $id,
1400 'userInfo' => $userInfo
1401 ] );
1402 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1403 $this->assertFalse( $info->forceHTTPS() );
1404 $this->assertSame( [], $logger->getBuffer() );
1405
1406 $this->store->setSessionMeta( $id, [ 'forceHTTPS' => true ] + $metadata );
1407 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1408 'provider' => $provider,
1409 'id' => $id,
1410 'userInfo' => $userInfo
1411 ] );
1412 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1413 $this->assertTrue( $info->forceHTTPS() );
1414 $this->assertSame( [], $logger->getBuffer() );
1415
1416 $this->store->setSessionMeta( $id, [ 'forceHTTPS' => false ] + $metadata );
1417 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1418 'provider' => $provider,
1419 'id' => $id,
1420 'userInfo' => $userInfo,
1421 'forceHTTPS' => true
1422 ] );
1423 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1424 $this->assertTrue( $info->forceHTTPS() );
1425 $this->assertSame( [], $logger->getBuffer() );
1426
1427 // "Persist" flag from session
1428 $this->store->setSessionMeta( $id, $metadata );
1429 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1430 'provider' => $provider,
1431 'id' => $id,
1432 'userInfo' => $userInfo
1433 ] );
1434 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1435 $this->assertFalse( $info->wasPersisted() );
1436 $this->assertSame( [], $logger->getBuffer() );
1437
1438 $this->store->setSessionMeta( $id, [ 'persisted' => true ] + $metadata );
1439 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1440 'provider' => $provider,
1441 'id' => $id,
1442 'userInfo' => $userInfo
1443 ] );
1444 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1445 $this->assertTrue( $info->wasPersisted() );
1446 $this->assertSame( [], $logger->getBuffer() );
1447
1448 $this->store->setSessionMeta( $id, [ 'persisted' => false ] + $metadata );
1449 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1450 'provider' => $provider,
1451 'id' => $id,
1452 'userInfo' => $userInfo,
1453 'persisted' => true
1454 ] );
1455 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1456 $this->assertTrue( $info->wasPersisted() );
1457 $this->assertSame( [], $logger->getBuffer() );
1458
1459 // Provider refreshSessionInfo() returning false
1460 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1461 'provider' => $provider3,
1462 ] );
1463 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1464 $this->assertSame( [], $logger->getBuffer() );
1465
1466 // Hook
1467 $called = false;
1468 $data = [ 'foo' => 1 ];
1469 $this->store->setSession( $id, [ 'metadata' => $metadata, 'data' => $data ] );
1470 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1471 'provider' => $provider,
1472 'id' => $id,
1473 'userInfo' => $userInfo
1474 ] );
1475 $this->mergeMwGlobalArrayValue( 'wgHooks', [
1476 'SessionCheckInfo' => [ function ( &$reason, $i, $r, $m, $d ) use (
1477 $info, $metadata, $data, $request, &$called
1478 ) {
1479 $this->assertSame( $info->getId(), $i->getId() );
1480 $this->assertSame( $info->getProvider(), $i->getProvider() );
1481 $this->assertSame( $info->getUserInfo(), $i->getUserInfo() );
1482 $this->assertSame( $request, $r );
1483 $this->assertEquals( $metadata, $m );
1484 $this->assertEquals( $data, $d );
1485 $called = true;
1486 return false;
1487 } ]
1488 ] );
1489 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1490 $this->assertTrue( $called );
1491 $this->assertSame( [
1492 [ LogLevel::WARNING, 'Session "{session}": Hook aborted' ],
1493 ], $logger->getBuffer() );
1494 $logger->clearBuffer();
1495 $this->mergeMwGlobalArrayValue( 'wgHooks', [ 'SessionCheckInfo' => [] ] );
1496
1497 // forceUse deletes bad backend data
1498 $this->store->setSessionMeta( $id, [ 'userToken' => 'Bad' ] + $metadata );
1499 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
1500 'provider' => $provider,
1501 'id' => $id,
1502 'userInfo' => $userInfo,
1503 'forceUse' => true,
1504 ] );
1505 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1506 $this->assertFalse( $this->store->getSession( $id ) );
1507 $this->assertSame( [
1508 [ LogLevel::WARNING, 'Session "{session}": User token mismatch' ],
1509 ], $logger->getBuffer() );
1510 $logger->clearBuffer();
1511 }
1512 }