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