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