Update session log messages
[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 'Quux' => array( 'Quux' ),
601 );
602
603 $this->assertEquals( $expect, $manager->getVaryHeaders() );
604
605 // Again, to ensure it's cached
606 $this->assertEquals( $expect, $manager->getVaryHeaders() );
607 }
608
609 public function testGetVaryCookies() {
610 $manager = $this->getManager();
611
612 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
613 ->setMethods( array( 'getVaryCookies', '__toString' ) );
614
615 $provider1 = $providerBuilder->getMock();
616 $provider1->expects( $this->once() )->method( 'getVaryCookies' )
617 ->will( $this->returnValue( array( 'Foo', 'Bar' ) ) );
618 $provider1->expects( $this->any() )->method( '__toString' )
619 ->will( $this->returnValue( 'MockProvider1' ) );
620
621 $provider2 = $providerBuilder->getMock();
622 $provider2->expects( $this->once() )->method( 'getVaryCookies' )
623 ->will( $this->returnValue( array( 'Foo', 'Baz' ) ) );
624 $provider2->expects( $this->any() )->method( '__toString' )
625 ->will( $this->returnValue( 'MockProvider2' ) );
626
627 $this->config->set( 'SessionProviders', array(
628 $this->objectCacheDef( $provider1 ),
629 $this->objectCacheDef( $provider2 ),
630 ) );
631
632 $expect = array( 'Foo', 'Bar', 'Baz' );
633
634 $this->assertEquals( $expect, $manager->getVaryCookies() );
635
636 // Again, to ensure it's cached
637 $this->assertEquals( $expect, $manager->getVaryCookies() );
638 }
639
640 public function testGetProviders() {
641 $realManager = $this->getManager();
642 $manager = \TestingAccessWrapper::newFromObject( $realManager );
643
644 $this->config->set( 'SessionProviders', array(
645 array( 'class' => 'DummySessionProvider' ),
646 ) );
647 $providers = $manager->getProviders();
648 $this->assertArrayHasKey( 'DummySessionProvider', $providers );
649 $provider = \TestingAccessWrapper::newFromObject( $providers['DummySessionProvider'] );
650 $this->assertSame( $manager->logger, $provider->logger );
651 $this->assertSame( $manager->config, $provider->config );
652 $this->assertSame( $realManager, $provider->getManager() );
653
654 $this->config->set( 'SessionProviders', array(
655 array( 'class' => 'DummySessionProvider' ),
656 array( 'class' => 'DummySessionProvider' ),
657 ) );
658 $manager->sessionProviders = null;
659 try {
660 $manager->getProviders();
661 $this->fail( 'Expected exception not thrown' );
662 } catch ( \UnexpectedValueException $ex ) {
663 $this->assertSame(
664 'Duplicate provider name "DummySessionProvider"',
665 $ex->getMessage()
666 );
667 }
668 }
669
670 public function testShutdown() {
671 $manager = \TestingAccessWrapper::newFromObject( $this->getManager() );
672 $manager->setLogger( new \Psr\Log\NullLogger() );
673
674 $mock = $this->getMock( 'stdClass', array( 'save' ) );
675 $mock->expects( $this->once() )->method( 'save' );
676
677 $manager->allSessionBackends = array( $mock );
678 $manager->shutdown();
679 }
680
681 public function testGetSessionFromInfo() {
682 $manager = \TestingAccessWrapper::newFromObject( $this->getManager() );
683 $request = new \FauxRequest();
684
685 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
686
687 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
688 'provider' => $manager->getProvider( 'DummySessionProvider' ),
689 'id' => $id,
690 'persisted' => true,
691 'userInfo' => UserInfo::newFromName( 'UTSysop', true ),
692 'idIsSafe' => true,
693 ) );
694 \TestingAccessWrapper::newFromObject( $info )->idIsSafe = true;
695 $session1 = \TestingAccessWrapper::newFromObject(
696 $manager->getSessionFromInfo( $info, $request )
697 );
698 $session2 = \TestingAccessWrapper::newFromObject(
699 $manager->getSessionFromInfo( $info, $request )
700 );
701
702 $this->assertSame( $session1->backend, $session2->backend );
703 $this->assertNotEquals( $session1->index, $session2->index );
704 $this->assertSame( $session1->getSessionId(), $session2->getSessionId() );
705 $this->assertSame( $id, $session1->getId() );
706
707 \TestingAccessWrapper::newFromObject( $info )->idIsSafe = false;
708 $session3 = $manager->getSessionFromInfo( $info, $request );
709 $this->assertNotSame( $id, $session3->getId() );
710 }
711
712 public function testBackendRegistration() {
713 $manager = $this->getManager();
714
715 $session = $manager->getSessionForRequest( new \FauxRequest );
716 $backend = \TestingAccessWrapper::newFromObject( $session )->backend;
717 $sessionId = $session->getSessionId();
718 $id = (string)$sessionId;
719
720 $this->assertSame( $sessionId, $manager->getSessionById( $id, true )->getSessionId() );
721
722 $manager->changeBackendId( $backend );
723 $this->assertSame( $sessionId, $session->getSessionId() );
724 $this->assertNotEquals( $id, (string)$sessionId );
725 $id = (string)$sessionId;
726
727 $this->assertSame( $sessionId, $manager->getSessionById( $id, true )->getSessionId() );
728
729 // Destruction of the session here causes the backend to be deregistered
730 $session = null;
731
732 try {
733 $manager->changeBackendId( $backend );
734 $this->fail( 'Expected exception not thrown' );
735 } catch ( \InvalidArgumentException $ex ) {
736 $this->assertSame(
737 'Backend was not registered with this SessionManager', $ex->getMessage()
738 );
739 }
740
741 try {
742 $manager->deregisterSessionBackend( $backend );
743 $this->fail( 'Expected exception not thrown' );
744 } catch ( \InvalidArgumentException $ex ) {
745 $this->assertSame(
746 'Backend was not registered with this SessionManager', $ex->getMessage()
747 );
748 }
749
750 $session = $manager->getSessionById( $id, true );
751 $this->assertSame( $sessionId, $session->getSessionId() );
752 }
753
754 public function testGenerateSessionId() {
755 $manager = $this->getManager();
756
757 $id = $manager->generateSessionId();
758 $this->assertTrue( SessionManager::validateSessionId( $id ), "Generated ID: $id" );
759 }
760
761 public function testAutoCreateUser() {
762 global $wgGroupPermissions;
763
764 $that = $this;
765
766 \ObjectCache::$instances[__METHOD__] = new TestBagOStuff();
767 $this->setMwGlobals( array( 'wgMainCacheType' => __METHOD__ ) );
768 $this->setMWGlobals( array(
769 'wgAuth' => new AuthPlugin,
770 ) );
771
772 $this->stashMwGlobals( array( 'wgGroupPermissions' ) );
773 $wgGroupPermissions['*']['createaccount'] = true;
774 $wgGroupPermissions['*']['autocreateaccount'] = false;
775
776 // Replace the global singleton with one configured for testing
777 $manager = $this->getManager();
778 $reset = TestUtils::setSessionManagerSingleton( $manager );
779
780 $logger = new \TestLogger( true, function ( $m ) {
781 if ( substr( $m, 0, 15 ) === 'SessionBackend ' ) {
782 // Don't care.
783 return null;
784 }
785 $m = str_replace( 'MediaWiki\Session\SessionManager::autoCreateUser: ', '', $m );
786 return $m;
787 } );
788 $manager->setLogger( $logger );
789
790 $session = SessionManager::getGlobalSession();
791
792 // Can't create an already-existing user
793 $user = User::newFromName( 'UTSysop' );
794 $id = $user->getId();
795 $this->assertFalse( $manager->autoCreateUser( $user ) );
796 $this->assertSame( $id, $user->getId() );
797 $this->assertSame( 'UTSysop', $user->getName() );
798 $this->assertSame( array(), $logger->getBuffer() );
799 $logger->clearBuffer();
800
801 // Sanity check that creation works at all
802 $user = User::newFromName( 'UTSessionAutoCreate1' );
803 $this->assertSame( 0, $user->getId(), 'sanity check' );
804 $this->assertTrue( $manager->autoCreateUser( $user ) );
805 $this->assertNotEquals( 0, $user->getId() );
806 $this->assertSame( 'UTSessionAutoCreate1', $user->getName() );
807 $this->assertEquals(
808 $user->getId(), User::idFromName( 'UTSessionAutoCreate1', User::READ_LATEST )
809 );
810 $this->assertSame( array(
811 array( LogLevel::INFO, 'creating new user ({username}) - from: {url}' ),
812 ), $logger->getBuffer() );
813 $logger->clearBuffer();
814
815 // Check lack of permissions
816 $wgGroupPermissions['*']['createaccount'] = false;
817 $wgGroupPermissions['*']['autocreateaccount'] = false;
818 $user = User::newFromName( 'UTDoesNotExist' );
819 $this->assertFalse( $manager->autoCreateUser( $user ) );
820 $this->assertSame( 0, $user->getId() );
821 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
822 $this->assertEquals( 0, User::idFromName( 'UTDoesNotExist', User::READ_LATEST ) );
823 $session->clear();
824 $this->assertSame( array(
825 array(
826 LogLevel::DEBUG,
827 'user is blocked from this wiki, blacklisting',
828 ),
829 ), $logger->getBuffer() );
830 $logger->clearBuffer();
831
832 // Check other permission
833 $wgGroupPermissions['*']['createaccount'] = false;
834 $wgGroupPermissions['*']['autocreateaccount'] = true;
835 $user = User::newFromName( 'UTSessionAutoCreate2' );
836 $this->assertSame( 0, $user->getId(), 'sanity check' );
837 $this->assertTrue( $manager->autoCreateUser( $user ) );
838 $this->assertNotEquals( 0, $user->getId() );
839 $this->assertSame( 'UTSessionAutoCreate2', $user->getName() );
840 $this->assertEquals(
841 $user->getId(), User::idFromName( 'UTSessionAutoCreate2', User::READ_LATEST )
842 );
843 $this->assertSame( array(
844 array( LogLevel::INFO, 'creating new user ({username}) - from: {url}' ),
845 ), $logger->getBuffer() );
846 $logger->clearBuffer();
847
848 // Test account-creation block
849 $anon = new User;
850 $block = new \Block( array(
851 'address' => $anon->getName(),
852 'user' => $id,
853 'reason' => __METHOD__,
854 'expiry' => time() + 100500,
855 'createAccount' => true,
856 ) );
857 $block->insert();
858 $this->assertInstanceOf( 'Block', $anon->isBlockedFromCreateAccount(), 'sanity check' );
859 $reset2 = new \ScopedCallback( array( $block, 'delete' ) );
860 $user = User::newFromName( 'UTDoesNotExist' );
861 $this->assertFalse( $manager->autoCreateUser( $user ) );
862 $this->assertSame( 0, $user->getId() );
863 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
864 $this->assertEquals( 0, User::idFromName( 'UTDoesNotExist', User::READ_LATEST ) );
865 \ScopedCallback::consume( $reset2 );
866 $session->clear();
867 $this->assertSame( array(
868 array( LogLevel::DEBUG, 'user is blocked from this wiki, blacklisting' ),
869 ), $logger->getBuffer() );
870 $logger->clearBuffer();
871
872 // Sanity check that creation still works
873 $user = User::newFromName( 'UTSessionAutoCreate3' );
874 $this->assertSame( 0, $user->getId(), 'sanity check' );
875 $this->assertTrue( $manager->autoCreateUser( $user ) );
876 $this->assertNotEquals( 0, $user->getId() );
877 $this->assertSame( 'UTSessionAutoCreate3', $user->getName() );
878 $this->assertEquals(
879 $user->getId(), User::idFromName( 'UTSessionAutoCreate3', User::READ_LATEST )
880 );
881 $this->assertSame( array(
882 array( LogLevel::INFO, 'creating new user ({username}) - from: {url}' ),
883 ), $logger->getBuffer() );
884 $logger->clearBuffer();
885
886 // Test prevention by AuthPlugin
887 global $wgAuth;
888 $oldWgAuth = $wgAuth;
889 $mockWgAuth = $this->getMock( 'AuthPlugin', array( 'autoCreate' ) );
890 $mockWgAuth->expects( $this->once() )->method( 'autoCreate' )
891 ->will( $this->returnValue( false ) );
892 $this->setMwGlobals( array(
893 'wgAuth' => $mockWgAuth,
894 ) );
895 $user = User::newFromName( 'UTDoesNotExist' );
896 $this->assertFalse( $manager->autoCreateUser( $user ) );
897 $this->assertSame( 0, $user->getId() );
898 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
899 $this->assertEquals( 0, User::idFromName( 'UTDoesNotExist', User::READ_LATEST ) );
900 $this->setMwGlobals( array(
901 'wgAuth' => $oldWgAuth,
902 ) );
903 $session->clear();
904 $this->assertSame( array(
905 array( LogLevel::DEBUG, 'denied by AuthPlugin' ),
906 ), $logger->getBuffer() );
907 $logger->clearBuffer();
908
909 // Test prevention by wfReadOnly()
910 $this->setMwGlobals( array(
911 'wgReadOnly' => 'Because',
912 ) );
913 $user = User::newFromName( 'UTDoesNotExist' );
914 $this->assertFalse( $manager->autoCreateUser( $user ) );
915 $this->assertSame( 0, $user->getId() );
916 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
917 $this->assertEquals( 0, User::idFromName( 'UTDoesNotExist', User::READ_LATEST ) );
918 $this->setMwGlobals( array(
919 'wgReadOnly' => false,
920 ) );
921 $session->clear();
922 $this->assertSame( array(
923 array( LogLevel::DEBUG, 'denied by wfReadOnly()' ),
924 ), $logger->getBuffer() );
925 $logger->clearBuffer();
926
927 // Test prevention by a previous session
928 $session->set( 'MWSession::AutoCreateBlacklist', 'test' );
929 $user = User::newFromName( 'UTDoesNotExist' );
930 $this->assertFalse( $manager->autoCreateUser( $user ) );
931 $this->assertSame( 0, $user->getId() );
932 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
933 $this->assertEquals( 0, User::idFromName( 'UTDoesNotExist', User::READ_LATEST ) );
934 $session->clear();
935 $this->assertSame( array(
936 array( LogLevel::DEBUG, 'blacklisted in session (test)' ),
937 ), $logger->getBuffer() );
938 $logger->clearBuffer();
939
940 // Test uncreatable name
941 $user = User::newFromName( 'UTDoesNotExist@' );
942 $this->assertFalse( $manager->autoCreateUser( $user ) );
943 $this->assertSame( 0, $user->getId() );
944 $this->assertNotSame( 'UTDoesNotExist@', $user->getName() );
945 $this->assertEquals( 0, User::idFromName( 'UTDoesNotExist', User::READ_LATEST ) );
946 $session->clear();
947 $this->assertSame( array(
948 array( LogLevel::DEBUG, 'Invalid username, blacklisting' ),
949 ), $logger->getBuffer() );
950 $logger->clearBuffer();
951
952 // Test AbortAutoAccount hook
953 $mock = $this->getMock( __CLASS__, array( 'onAbortAutoAccount' ) );
954 $mock->expects( $this->once() )->method( 'onAbortAutoAccount' )
955 ->will( $this->returnCallback( function ( User $user, &$msg ) {
956 $msg = 'No way!';
957 return false;
958 } ) );
959 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array( $mock ) ) );
960 $user = User::newFromName( 'UTDoesNotExist' );
961 $this->assertFalse( $manager->autoCreateUser( $user ) );
962 $this->assertSame( 0, $user->getId() );
963 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
964 $this->assertEquals( 0, User::idFromName( 'UTDoesNotExist', User::READ_LATEST ) );
965 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array() ) );
966 $session->clear();
967 $this->assertSame( array(
968 array( LogLevel::DEBUG, 'denied by hook: No way!' ),
969 ), $logger->getBuffer() );
970 $logger->clearBuffer();
971
972 // Test AbortAutoAccount hook screwing up the name
973 $mock = $this->getMock( 'stdClass', array( 'onAbortAutoAccount' ) );
974 $mock->expects( $this->once() )->method( 'onAbortAutoAccount' )
975 ->will( $this->returnCallback( function ( User $user ) {
976 $user->setName( 'UTDoesNotExistEither' );
977 } ) );
978 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array( $mock ) ) );
979 try {
980 $user = User::newFromName( 'UTDoesNotExist' );
981 $manager->autoCreateUser( $user );
982 $this->fail( 'Expected exception not thrown' );
983 } catch ( \UnexpectedValueException $ex ) {
984 $this->assertSame(
985 'AbortAutoAccount hook tried to change the user name',
986 $ex->getMessage()
987 );
988 }
989 $this->assertSame( 0, $user->getId() );
990 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
991 $this->assertNotSame( 'UTDoesNotExistEither', $user->getName() );
992 $this->assertEquals( 0, User::idFromName( 'UTDoesNotExist', User::READ_LATEST ) );
993 $this->assertEquals( 0, User::idFromName( 'UTDoesNotExistEither', User::READ_LATEST ) );
994 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 'AbortAutoAccount' => array() ) );
995 $session->clear();
996 $this->assertSame( array(), $logger->getBuffer() );
997 $logger->clearBuffer();
998
999 // Test for "exception backoff"
1000 $user = User::newFromName( 'UTDoesNotExist' );
1001 $cache = \ObjectCache::getLocalClusterInstance();
1002 $backoffKey = wfMemcKey( 'MWSession', 'autocreate-failed', md5( $user->getName() ) );
1003 $cache->set( $backoffKey, 1, 60 * 10 );
1004 $this->assertFalse( $manager->autoCreateUser( $user ) );
1005 $this->assertSame( 0, $user->getId() );
1006 $this->assertNotSame( 'UTDoesNotExist', $user->getName() );
1007 $this->assertEquals( 0, User::idFromName( 'UTDoesNotExist', User::READ_LATEST ) );
1008 $cache->delete( $backoffKey );
1009 $session->clear();
1010 $this->assertSame( array(
1011 array( LogLevel::DEBUG, 'denied by prior creation attempt failures' ),
1012 ), $logger->getBuffer() );
1013 $logger->clearBuffer();
1014
1015 // Sanity check that creation still works, and test completion hook
1016 $cb = $this->callback( function ( User $user ) use ( $that ) {
1017 $that->assertNotEquals( 0, $user->getId() );
1018 $that->assertSame( 'UTSessionAutoCreate4', $user->getName() );
1019 $that->assertEquals(
1020 $user->getId(), User::idFromName( 'UTSessionAutoCreate4', User::READ_LATEST )
1021 );
1022 return true;
1023 } );
1024 $mock = $this->getMock( 'stdClass',
1025 array( 'onAuthPluginAutoCreate', 'onLocalUserCreated' ) );
1026 $mock->expects( $this->once() )->method( 'onAuthPluginAutoCreate' )
1027 ->with( $cb );
1028 $mock->expects( $this->once() )->method( 'onLocalUserCreated' )
1029 ->with( $cb, $this->identicalTo( true ) );
1030 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1031 'AuthPluginAutoCreate' => array( $mock ),
1032 'LocalUserCreated' => array( $mock ),
1033 ) );
1034 $user = User::newFromName( 'UTSessionAutoCreate4' );
1035 $this->assertSame( 0, $user->getId(), 'sanity check' );
1036 $this->assertTrue( $manager->autoCreateUser( $user ) );
1037 $this->assertNotEquals( 0, $user->getId() );
1038 $this->assertSame( 'UTSessionAutoCreate4', $user->getName() );
1039 $this->assertEquals(
1040 $user->getId(),
1041 User::idFromName( 'UTSessionAutoCreate4', User::READ_LATEST )
1042 );
1043 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1044 'AuthPluginAutoCreate' => array(),
1045 'LocalUserCreated' => array(),
1046 ) );
1047 $this->assertSame( array(
1048 array( LogLevel::INFO, 'creating new user ({username}) - from: {url}' ),
1049 ), $logger->getBuffer() );
1050 $logger->clearBuffer();
1051 }
1052
1053 public function onAbortAutoAccount( User $user, &$msg ) {
1054 }
1055
1056 public function testPreventSessionsForUser() {
1057 $manager = $this->getManager();
1058
1059 $providerBuilder = $this->getMockBuilder( 'DummySessionProvider' )
1060 ->setMethods( array( 'preventSessionsForUser', '__toString' ) );
1061
1062 $provider1 = $providerBuilder->getMock();
1063 $provider1->expects( $this->once() )->method( 'preventSessionsForUser' )
1064 ->with( $this->equalTo( 'UTSysop' ) );
1065 $provider1->expects( $this->any() )->method( '__toString' )
1066 ->will( $this->returnValue( 'MockProvider1' ) );
1067
1068 $this->config->set( 'SessionProviders', array(
1069 $this->objectCacheDef( $provider1 ),
1070 ) );
1071
1072 $user = User::newFromName( 'UTSysop' );
1073 $token = $user->getToken( true );
1074
1075 $this->assertFalse( $manager->isUserSessionPrevented( 'UTSysop' ) );
1076 $manager->preventSessionsForUser( 'UTSysop' );
1077 $this->assertNotEquals( $token, User::newFromName( 'UTSysop' )->getToken() );
1078 $this->assertTrue( $manager->isUserSessionPrevented( 'UTSysop' ) );
1079 }
1080
1081 public function testLoadSessionInfoFromStore() {
1082 $manager = $this->getManager();
1083 $logger = new \TestLogger( true );
1084 $manager->setLogger( $logger );
1085 $request = new \FauxRequest();
1086
1087 // TestingAccessWrapper can't handle methods with reference arguments, sigh.
1088 $rClass = new \ReflectionClass( $manager );
1089 $rMethod = $rClass->getMethod( 'loadSessionInfoFromStore' );
1090 $rMethod->setAccessible( true );
1091 $loadSessionInfoFromStore = function ( &$info ) use ( $rMethod, $manager, $request ) {
1092 return $rMethod->invokeArgs( $manager, array( &$info, $request ) );
1093 };
1094
1095 $userInfo = UserInfo::newFromName( 'UTSysop', true );
1096 $unverifiedUserInfo = UserInfo::newFromName( 'UTSysop', false );
1097
1098 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
1099 $metadata = array(
1100 'userId' => $userInfo->getId(),
1101 'userName' => $userInfo->getName(),
1102 'userToken' => $userInfo->getToken( true ),
1103 'provider' => 'Mock',
1104 );
1105
1106 $builder = $this->getMockBuilder( 'MediaWiki\\Session\\SessionProvider' )
1107 ->setMethods( array( '__toString', 'mergeMetadata', 'refreshSessionInfo' ) );
1108
1109 $provider = $builder->getMockForAbstractClass();
1110 $provider->setManager( $manager );
1111 $provider->expects( $this->any() )->method( 'persistsSessionId' )
1112 ->will( $this->returnValue( true ) );
1113 $provider->expects( $this->any() )->method( 'canChangeUser' )
1114 ->will( $this->returnValue( true ) );
1115 $provider->expects( $this->any() )->method( 'refreshSessionInfo' )
1116 ->will( $this->returnValue( true ) );
1117 $provider->expects( $this->any() )->method( '__toString' )
1118 ->will( $this->returnValue( 'Mock' ) );
1119 $provider->expects( $this->any() )->method( 'mergeMetadata' )
1120 ->will( $this->returnCallback( function ( $a, $b ) {
1121 if ( $b === array( 'Throw' ) ) {
1122 throw new \UnexpectedValueException( 'no merge!' );
1123 }
1124 return array( 'Merged' );
1125 } ) );
1126
1127 $provider2 = $builder->getMockForAbstractClass();
1128 $provider2->setManager( $manager );
1129 $provider2->expects( $this->any() )->method( 'persistsSessionId' )
1130 ->will( $this->returnValue( false ) );
1131 $provider2->expects( $this->any() )->method( 'canChangeUser' )
1132 ->will( $this->returnValue( false ) );
1133 $provider2->expects( $this->any() )->method( '__toString' )
1134 ->will( $this->returnValue( 'Mock2' ) );
1135 $provider2->expects( $this->any() )->method( 'refreshSessionInfo' )
1136 ->will( $this->returnCallback( function ( $info, $request, &$metadata ) {
1137 $metadata['changed'] = true;
1138 return true;
1139 } ) );
1140
1141 $provider3 = $builder->getMockForAbstractClass();
1142 $provider3->setManager( $manager );
1143 $provider3->expects( $this->any() )->method( 'persistsSessionId' )
1144 ->will( $this->returnValue( true ) );
1145 $provider3->expects( $this->any() )->method( 'canChangeUser' )
1146 ->will( $this->returnValue( true ) );
1147 $provider3->expects( $this->once() )->method( 'refreshSessionInfo' )
1148 ->will( $this->returnValue( false ) );
1149 $provider3->expects( $this->any() )->method( '__toString' )
1150 ->will( $this->returnValue( 'Mock3' ) );
1151
1152 \TestingAccessWrapper::newFromObject( $manager )->sessionProviders = array(
1153 (string)$provider => $provider,
1154 (string)$provider2 => $provider2,
1155 (string)$provider3 => $provider3,
1156 );
1157
1158 // No metadata, basic usage
1159 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1160 'provider' => $provider,
1161 'id' => $id,
1162 'userInfo' => $userInfo
1163 ) );
1164 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1165 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1166 $this->assertFalse( $info->isIdSafe() );
1167 $this->assertSame( array(), $logger->getBuffer() );
1168
1169 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1170 'provider' => $provider,
1171 'userInfo' => $userInfo
1172 ) );
1173 $this->assertTrue( $info->isIdSafe(), 'sanity check' );
1174 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1175 $this->assertTrue( $info->isIdSafe() );
1176 $this->assertSame( array(), $logger->getBuffer() );
1177
1178 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1179 'provider' => $provider2,
1180 'id' => $id,
1181 'userInfo' => $userInfo
1182 ) );
1183 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1184 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1185 $this->assertTrue( $info->isIdSafe() );
1186 $this->assertSame( array(), $logger->getBuffer() );
1187
1188 // Unverified user, no metadata
1189 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1190 'provider' => $provider,
1191 'id' => $id,
1192 'userInfo' => $unverifiedUserInfo
1193 ) );
1194 $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
1195 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1196 $this->assertSame( array(
1197 array(
1198 LogLevel::WARNING,
1199 'Session "{session}": Unverified user provided and no metadata to auth it',
1200 )
1201 ), $logger->getBuffer() );
1202 $logger->clearBuffer();
1203
1204 // No metadata, missing data
1205 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1206 'id' => $id,
1207 'userInfo' => $userInfo
1208 ) );
1209 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1210 $this->assertSame( array(
1211 array( LogLevel::WARNING, 'Session "{session}": Null provider and no metadata' ),
1212 ), $logger->getBuffer() );
1213 $logger->clearBuffer();
1214
1215 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1216 'provider' => $provider,
1217 'id' => $id,
1218 ) );
1219 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1220 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1221 $this->assertInstanceOf( 'MediaWiki\\Session\\UserInfo', $info->getUserInfo() );
1222 $this->assertTrue( $info->getUserInfo()->isVerified() );
1223 $this->assertTrue( $info->getUserInfo()->isAnon() );
1224 $this->assertFalse( $info->isIdSafe() );
1225 $this->assertSame( array(), $logger->getBuffer() );
1226
1227 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1228 'provider' => $provider2,
1229 'id' => $id,
1230 ) );
1231 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1232 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1233 $this->assertSame( array(
1234 array( LogLevel::INFO, 'Session "{session}": No user provided and provider cannot set user' )
1235 ), $logger->getBuffer() );
1236 $logger->clearBuffer();
1237
1238 // Incomplete/bad metadata
1239 $this->store->setRawSession( $id, true );
1240 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1241 $this->assertSame( array(
1242 array( LogLevel::WARNING, 'Session "{session}": Bad data' ),
1243 ), $logger->getBuffer() );
1244 $logger->clearBuffer();
1245
1246 $this->store->setRawSession( $id, array( 'data' => array() ) );
1247 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1248 $this->assertSame( array(
1249 array( LogLevel::WARNING, 'Session "{session}": Bad data structure' ),
1250 ), $logger->getBuffer() );
1251 $logger->clearBuffer();
1252
1253 $this->store->deleteSession( $id );
1254 $this->store->setRawSession( $id, array( 'metadata' => $metadata ) );
1255 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1256 $this->assertSame( array(
1257 array( LogLevel::WARNING, 'Session "{session}": Bad data structure' ),
1258 ), $logger->getBuffer() );
1259 $logger->clearBuffer();
1260
1261 $this->store->setRawSession( $id, array( 'metadata' => $metadata, 'data' => true ) );
1262 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1263 $this->assertSame( array(
1264 array( LogLevel::WARNING, 'Session "{session}": Bad data structure' ),
1265 ), $logger->getBuffer() );
1266 $logger->clearBuffer();
1267
1268 $this->store->setRawSession( $id, array( 'metadata' => true, 'data' => array() ) );
1269 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1270 $this->assertSame( array(
1271 array( LogLevel::WARNING, 'Session "{session}": Bad data structure' ),
1272 ), $logger->getBuffer() );
1273 $logger->clearBuffer();
1274
1275 foreach ( $metadata as $key => $dummy ) {
1276 $tmp = $metadata;
1277 unset( $tmp[$key] );
1278 $this->store->setRawSession( $id, array( 'metadata' => $tmp, 'data' => array() ) );
1279 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1280 $this->assertSame( array(
1281 array( LogLevel::WARNING, 'Session "{session}": Bad metadata' ),
1282 ), $logger->getBuffer() );
1283 $logger->clearBuffer();
1284 }
1285
1286 // Basic usage with metadata
1287 $this->store->setRawSession( $id, array( 'metadata' => $metadata, 'data' => array() ) );
1288 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1289 'provider' => $provider,
1290 'id' => $id,
1291 'userInfo' => $userInfo
1292 ) );
1293 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1294 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1295 $this->assertTrue( $info->isIdSafe() );
1296 $this->assertSame( array(), $logger->getBuffer() );
1297
1298 // Mismatched provider
1299 $this->store->setSessionMeta( $id, array( 'provider' => 'Bad' ) + $metadata );
1300 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1301 'provider' => $provider,
1302 'id' => $id,
1303 'userInfo' => $userInfo
1304 ) );
1305 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1306 $this->assertSame( array(
1307 array( LogLevel::WARNING, 'Session "{session}": Wrong provider Bad !== Mock' ),
1308 ), $logger->getBuffer() );
1309 $logger->clearBuffer();
1310
1311 // Unknown provider
1312 $this->store->setSessionMeta( $id, array( 'provider' => 'Bad' ) + $metadata );
1313 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1314 'id' => $id,
1315 'userInfo' => $userInfo
1316 ) );
1317 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1318 $this->assertSame( array(
1319 array( LogLevel::WARNING, 'Session "{session}": Unknown provider Bad' ),
1320 ), $logger->getBuffer() );
1321 $logger->clearBuffer();
1322
1323 // Fill in provider
1324 $this->store->setSessionMeta( $id, $metadata );
1325 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1326 'id' => $id,
1327 'userInfo' => $userInfo
1328 ) );
1329 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1330 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1331 $this->assertTrue( $info->isIdSafe() );
1332 $this->assertSame( array(), $logger->getBuffer() );
1333
1334 // Bad user metadata
1335 $this->store->setSessionMeta( $id, array( 'userId' => -1, 'userToken' => null ) + $metadata );
1336 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1337 'provider' => $provider,
1338 'id' => $id,
1339 ) );
1340 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1341 $this->assertSame( array(
1342 array( LogLevel::ERROR, 'Session "{session}": {exception}' ),
1343 ), $logger->getBuffer() );
1344 $logger->clearBuffer();
1345
1346 $this->store->setSessionMeta(
1347 $id, array( 'userId' => 0, 'userName' => '<X>', 'userToken' => null ) + $metadata
1348 );
1349 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1350 'provider' => $provider,
1351 'id' => $id,
1352 ) );
1353 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1354 $this->assertSame( array(
1355 array( LogLevel::ERROR, 'Session "{session}": {exception}', ),
1356 ), $logger->getBuffer() );
1357 $logger->clearBuffer();
1358
1359 // Mismatched user by ID
1360 $this->store->setSessionMeta(
1361 $id, array( 'userId' => $userInfo->getId() + 1, 'userToken' => null ) + $metadata
1362 );
1363 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1364 'provider' => $provider,
1365 'id' => $id,
1366 'userInfo' => $userInfo
1367 ) );
1368 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1369 $this->assertSame( array(
1370 array( LogLevel::WARNING, 'Session "{session}": User ID mismatch, {uid_a} !== {uid_b}' ),
1371 ), $logger->getBuffer() );
1372 $logger->clearBuffer();
1373
1374 // Mismatched user by name
1375 $this->store->setSessionMeta(
1376 $id, array( 'userId' => 0, 'userName' => 'X', 'userToken' => null ) + $metadata
1377 );
1378 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1379 'provider' => $provider,
1380 'id' => $id,
1381 'userInfo' => $userInfo
1382 ) );
1383 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1384 $this->assertSame( array(
1385 array( LogLevel::WARNING, 'Session "{session}": User name mismatch, {uname_a} !== {uname_b}' ),
1386 ), $logger->getBuffer() );
1387 $logger->clearBuffer();
1388
1389 // ID matches, name doesn't
1390 $this->store->setSessionMeta(
1391 $id, array( 'userId' => $userInfo->getId(), 'userName' => 'X', 'userToken' => null ) + $metadata
1392 );
1393 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1394 'provider' => $provider,
1395 'id' => $id,
1396 'userInfo' => $userInfo
1397 ) );
1398 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1399 $this->assertSame( array(
1400 array(
1401 LogLevel::WARNING,
1402 'Session "{session}": User ID matched but name didn\'t (rename?), {uname_a} !== {uname_b}'
1403 ),
1404 ), $logger->getBuffer() );
1405 $logger->clearBuffer();
1406
1407 // Mismatched anon user
1408 $this->store->setSessionMeta(
1409 $id, array( 'userId' => 0, 'userName' => null, 'userToken' => null ) + $metadata
1410 );
1411 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1412 'provider' => $provider,
1413 'id' => $id,
1414 'userInfo' => $userInfo
1415 ) );
1416 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1417 $this->assertSame( array(
1418 array(
1419 LogLevel::WARNING,
1420 'Session "{session}": Metadata has an anonymous user, ' .
1421 'but a non-anon user was provided',
1422 ),
1423 ), $logger->getBuffer() );
1424 $logger->clearBuffer();
1425
1426 // Lookup user by ID
1427 $this->store->setSessionMeta( $id, array( 'userToken' => null ) + $metadata );
1428 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1429 'provider' => $provider,
1430 'id' => $id,
1431 ) );
1432 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1433 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1434 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1435 $this->assertTrue( $info->isIdSafe() );
1436 $this->assertSame( array(), $logger->getBuffer() );
1437
1438 // Lookup user by name
1439 $this->store->setSessionMeta(
1440 $id, array( 'userId' => 0, 'userName' => 'UTSysop', 'userToken' => null ) + $metadata
1441 );
1442 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1443 'provider' => $provider,
1444 'id' => $id,
1445 ) );
1446 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1447 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1448 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1449 $this->assertTrue( $info->isIdSafe() );
1450 $this->assertSame( array(), $logger->getBuffer() );
1451
1452 // Lookup anonymous user
1453 $this->store->setSessionMeta(
1454 $id, array( 'userId' => 0, 'userName' => null, 'userToken' => null ) + $metadata
1455 );
1456 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1457 'provider' => $provider,
1458 'id' => $id,
1459 ) );
1460 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1461 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1462 $this->assertTrue( $info->getUserInfo()->isAnon() );
1463 $this->assertTrue( $info->isIdSafe() );
1464 $this->assertSame( array(), $logger->getBuffer() );
1465
1466 // Unverified user with metadata
1467 $this->store->setSessionMeta( $id, $metadata );
1468 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1469 'provider' => $provider,
1470 'id' => $id,
1471 'userInfo' => $unverifiedUserInfo
1472 ) );
1473 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1474 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1475 $this->assertTrue( $info->getUserInfo()->isVerified() );
1476 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1477 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1478 $this->assertTrue( $info->isIdSafe() );
1479 $this->assertSame( array(), $logger->getBuffer() );
1480
1481 // Unverified user with metadata
1482 $this->store->setSessionMeta( $id, $metadata );
1483 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1484 'provider' => $provider,
1485 'id' => $id,
1486 'userInfo' => $unverifiedUserInfo
1487 ) );
1488 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1489 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1490 $this->assertTrue( $info->getUserInfo()->isVerified() );
1491 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1492 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1493 $this->assertTrue( $info->isIdSafe() );
1494 $this->assertSame( array(), $logger->getBuffer() );
1495
1496 // Wrong token
1497 $this->store->setSessionMeta( $id, array( 'userToken' => 'Bad' ) + $metadata );
1498 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1499 'provider' => $provider,
1500 'id' => $id,
1501 'userInfo' => $userInfo
1502 ) );
1503 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1504 $this->assertSame( array(
1505 array( LogLevel::WARNING, 'Session "{session}": User token mismatch' ),
1506 ), $logger->getBuffer() );
1507 $logger->clearBuffer();
1508
1509 // Provider metadata
1510 $this->store->setSessionMeta( $id, array( 'provider' => 'Mock2' ) + $metadata );
1511 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1512 'provider' => $provider2,
1513 'id' => $id,
1514 'userInfo' => $userInfo,
1515 'metadata' => array( 'Info' ),
1516 ) );
1517 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1518 $this->assertSame( array( 'Info', 'changed' => true ), $info->getProviderMetadata() );
1519 $this->assertSame( array(), $logger->getBuffer() );
1520
1521 $this->store->setSessionMeta( $id, array( 'providerMetadata' => array( 'Saved' ) ) + $metadata );
1522 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1523 'provider' => $provider,
1524 'id' => $id,
1525 'userInfo' => $userInfo,
1526 ) );
1527 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1528 $this->assertSame( array( 'Saved' ), $info->getProviderMetadata() );
1529 $this->assertSame( array(), $logger->getBuffer() );
1530
1531 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1532 'provider' => $provider,
1533 'id' => $id,
1534 'userInfo' => $userInfo,
1535 'metadata' => array( 'Info' ),
1536 ) );
1537 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1538 $this->assertSame( array( 'Merged' ), $info->getProviderMetadata() );
1539 $this->assertSame( array(), $logger->getBuffer() );
1540
1541 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1542 'provider' => $provider,
1543 'id' => $id,
1544 'userInfo' => $userInfo,
1545 'metadata' => array( 'Throw' ),
1546 ) );
1547 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1548 $this->assertSame( array(
1549 array(
1550 LogLevel::WARNING,
1551 'Session "{session}": Metadata merge failed: {exception}',
1552 ),
1553 ), $logger->getBuffer() );
1554 $logger->clearBuffer();
1555
1556 // Remember from session
1557 $this->store->setSessionMeta( $id, $metadata );
1558 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1559 'provider' => $provider,
1560 'id' => $id,
1561 ) );
1562 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1563 $this->assertFalse( $info->wasRemembered() );
1564 $this->assertSame( array(), $logger->getBuffer() );
1565
1566 $this->store->setSessionMeta( $id, array( 'remember' => true ) + $metadata );
1567 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1568 'provider' => $provider,
1569 'id' => $id,
1570 ) );
1571 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1572 $this->assertTrue( $info->wasRemembered() );
1573 $this->assertSame( array(), $logger->getBuffer() );
1574
1575 $this->store->setSessionMeta( $id, array( 'remember' => false ) + $metadata );
1576 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1577 'provider' => $provider,
1578 'id' => $id,
1579 'userInfo' => $userInfo
1580 ) );
1581 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1582 $this->assertTrue( $info->wasRemembered() );
1583 $this->assertSame( array(), $logger->getBuffer() );
1584
1585 // forceHTTPS from session
1586 $this->store->setSessionMeta( $id, $metadata );
1587 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1588 'provider' => $provider,
1589 'id' => $id,
1590 'userInfo' => $userInfo
1591 ) );
1592 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1593 $this->assertFalse( $info->forceHTTPS() );
1594 $this->assertSame( array(), $logger->getBuffer() );
1595
1596 $this->store->setSessionMeta( $id, array( 'forceHTTPS' => true ) + $metadata );
1597 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1598 'provider' => $provider,
1599 'id' => $id,
1600 'userInfo' => $userInfo
1601 ) );
1602 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1603 $this->assertTrue( $info->forceHTTPS() );
1604 $this->assertSame( array(), $logger->getBuffer() );
1605
1606 $this->store->setSessionMeta( $id, array( 'forceHTTPS' => false ) + $metadata );
1607 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1608 'provider' => $provider,
1609 'id' => $id,
1610 'userInfo' => $userInfo,
1611 'forceHTTPS' => true
1612 ) );
1613 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1614 $this->assertTrue( $info->forceHTTPS() );
1615 $this->assertSame( array(), $logger->getBuffer() );
1616
1617 // "Persist" flag from session
1618 $this->store->setSessionMeta( $id, $metadata );
1619 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1620 'provider' => $provider,
1621 'id' => $id,
1622 'userInfo' => $userInfo
1623 ) );
1624 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1625 $this->assertFalse( $info->wasPersisted() );
1626 $this->assertSame( array(), $logger->getBuffer() );
1627
1628 $this->store->setSessionMeta( $id, array( 'persisted' => true ) + $metadata );
1629 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1630 'provider' => $provider,
1631 'id' => $id,
1632 'userInfo' => $userInfo
1633 ) );
1634 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1635 $this->assertTrue( $info->wasPersisted() );
1636 $this->assertSame( array(), $logger->getBuffer() );
1637
1638 $this->store->setSessionMeta( $id, array( 'persisted' => false ) + $metadata );
1639 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1640 'provider' => $provider,
1641 'id' => $id,
1642 'userInfo' => $userInfo,
1643 'persisted' => true
1644 ) );
1645 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1646 $this->assertTrue( $info->wasPersisted() );
1647 $this->assertSame( array(), $logger->getBuffer() );
1648
1649 // Provider refreshSessionInfo() returning false
1650 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1651 'provider' => $provider3,
1652 ) );
1653 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1654 $this->assertSame( array(), $logger->getBuffer() );
1655
1656 // Hook
1657 $that = $this;
1658 $called = false;
1659 $data = array( 'foo' => 1 );
1660 $this->store->setSession( $id, array( 'metadata' => $metadata, 'data' => $data ) );
1661 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1662 'provider' => $provider,
1663 'id' => $id,
1664 'userInfo' => $userInfo
1665 ) );
1666 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1667 'SessionCheckInfo' => array( function ( &$reason, $i, $r, $m, $d ) use (
1668 $that, $info, $metadata, $data, $request, &$called
1669 ) {
1670 $that->assertSame( $info->getId(), $i->getId() );
1671 $that->assertSame( $info->getProvider(), $i->getProvider() );
1672 $that->assertSame( $info->getUserInfo(), $i->getUserInfo() );
1673 $that->assertSame( $request, $r );
1674 $that->assertEquals( $metadata, $m );
1675 $that->assertEquals( $data, $d );
1676 $called = true;
1677 return false;
1678 } )
1679 ) );
1680 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1681 $this->assertTrue( $called );
1682 $this->assertSame( array(
1683 array( LogLevel::WARNING, 'Session "{session}": Hook aborted' ),
1684 ), $logger->getBuffer() );
1685 $logger->clearBuffer();
1686 }
1687
1688 }