Merge "Revert "TableSorter: Avoid FOUC and preserve styling in VisualEditor""
[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 $this->assertFalse( $manager->isUserSessionPrevented( 'UTSysop' ) );
1066 $manager->preventSessionsForUser( 'UTSysop' );
1067 $this->assertTrue( $manager->isUserSessionPrevented( 'UTSysop' ) );
1068 }
1069
1070 public function testLoadSessionInfoFromStore() {
1071 $manager = $this->getManager();
1072 $logger = new \TestLogger( true, function ( $m ) {
1073 return preg_replace(
1074 '/^Session \[\d+\]\w+<(?:null|anon|[+-]:\d+:\w+)>\w+: /', 'Session X: ', $m
1075 );
1076 } );
1077 $manager->setLogger( $logger );
1078 $request = new \FauxRequest();
1079
1080 // TestingAccessWrapper can't handle methods with reference arguments, sigh.
1081 $rClass = new \ReflectionClass( $manager );
1082 $rMethod = $rClass->getMethod( 'loadSessionInfoFromStore' );
1083 $rMethod->setAccessible( true );
1084 $loadSessionInfoFromStore = function ( &$info ) use ( $rMethod, $manager, $request ) {
1085 return $rMethod->invokeArgs( $manager, array( &$info, $request ) );
1086 };
1087
1088 $userInfo = UserInfo::newFromName( 'UTSysop', true );
1089 $unverifiedUserInfo = UserInfo::newFromName( 'UTSysop', false );
1090
1091 $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
1092 $metadata = array(
1093 'userId' => $userInfo->getId(),
1094 'userName' => $userInfo->getName(),
1095 'userToken' => $userInfo->getToken( true ),
1096 'provider' => 'Mock',
1097 );
1098
1099 $builder = $this->getMockBuilder( 'MediaWiki\\Session\\SessionProvider' )
1100 ->setMethods( array( '__toString', 'mergeMetadata', 'refreshSessionInfo' ) );
1101
1102 $provider = $builder->getMockForAbstractClass();
1103 $provider->setManager( $manager );
1104 $provider->expects( $this->any() )->method( 'persistsSessionId' )
1105 ->will( $this->returnValue( true ) );
1106 $provider->expects( $this->any() )->method( 'canChangeUser' )
1107 ->will( $this->returnValue( true ) );
1108 $provider->expects( $this->any() )->method( 'refreshSessionInfo' )
1109 ->will( $this->returnValue( true ) );
1110 $provider->expects( $this->any() )->method( '__toString' )
1111 ->will( $this->returnValue( 'Mock' ) );
1112 $provider->expects( $this->any() )->method( 'mergeMetadata' )
1113 ->will( $this->returnCallback( function ( $a, $b ) {
1114 if ( $b === array( 'Throw' ) ) {
1115 throw new \UnexpectedValueException( 'no merge!' );
1116 }
1117 return array( 'Merged' );
1118 } ) );
1119
1120 $provider2 = $builder->getMockForAbstractClass();
1121 $provider2->setManager( $manager );
1122 $provider2->expects( $this->any() )->method( 'persistsSessionId' )
1123 ->will( $this->returnValue( false ) );
1124 $provider2->expects( $this->any() )->method( 'canChangeUser' )
1125 ->will( $this->returnValue( false ) );
1126 $provider2->expects( $this->any() )->method( '__toString' )
1127 ->will( $this->returnValue( 'Mock2' ) );
1128 $provider2->expects( $this->any() )->method( 'refreshSessionInfo' )
1129 ->will( $this->returnCallback( function ( $info, $request, &$metadata ) {
1130 $metadata['changed'] = true;
1131 return true;
1132 } ) );
1133
1134 $provider3 = $builder->getMockForAbstractClass();
1135 $provider3->setManager( $manager );
1136 $provider3->expects( $this->any() )->method( 'persistsSessionId' )
1137 ->will( $this->returnValue( true ) );
1138 $provider3->expects( $this->any() )->method( 'canChangeUser' )
1139 ->will( $this->returnValue( true ) );
1140 $provider3->expects( $this->once() )->method( 'refreshSessionInfo' )
1141 ->will( $this->returnValue( false ) );
1142 $provider3->expects( $this->any() )->method( '__toString' )
1143 ->will( $this->returnValue( 'Mock3' ) );
1144
1145 \TestingAccessWrapper::newFromObject( $manager )->sessionProviders = array(
1146 (string)$provider => $provider,
1147 (string)$provider2 => $provider2,
1148 (string)$provider3 => $provider3,
1149 );
1150
1151 // No metadata, basic usage
1152 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1153 'provider' => $provider,
1154 'id' => $id,
1155 'userInfo' => $userInfo
1156 ) );
1157 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1158 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1159 $this->assertFalse( $info->isIdSafe() );
1160 $this->assertSame( array(), $logger->getBuffer() );
1161
1162 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1163 'provider' => $provider,
1164 'userInfo' => $userInfo
1165 ) );
1166 $this->assertTrue( $info->isIdSafe(), 'sanity check' );
1167 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1168 $this->assertTrue( $info->isIdSafe() );
1169 $this->assertSame( array(), $logger->getBuffer() );
1170
1171 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1172 'provider' => $provider2,
1173 'id' => $id,
1174 'userInfo' => $userInfo
1175 ) );
1176 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1177 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1178 $this->assertTrue( $info->isIdSafe() );
1179 $this->assertSame( array(), $logger->getBuffer() );
1180
1181 // Unverified user, no metadata
1182 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1183 'provider' => $provider,
1184 'id' => $id,
1185 'userInfo' => $unverifiedUserInfo
1186 ) );
1187 $this->assertSame( $unverifiedUserInfo, $info->getUserInfo() );
1188 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1189 $this->assertSame( array(
1190 array( LogLevel::WARNING, 'Session X: Unverified user provided and no metadata to auth it' )
1191 ), $logger->getBuffer() );
1192 $logger->clearBuffer();
1193
1194 // No metadata, missing data
1195 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1196 'id' => $id,
1197 'userInfo' => $userInfo
1198 ) );
1199 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1200 $this->assertSame( array(
1201 array( LogLevel::WARNING, 'Session X: Null provider and no metadata' ),
1202 ), $logger->getBuffer() );
1203 $logger->clearBuffer();
1204
1205 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1206 'provider' => $provider,
1207 'id' => $id,
1208 ) );
1209 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1210 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1211 $this->assertInstanceOf( 'MediaWiki\\Session\\UserInfo', $info->getUserInfo() );
1212 $this->assertTrue( $info->getUserInfo()->isVerified() );
1213 $this->assertTrue( $info->getUserInfo()->isAnon() );
1214 $this->assertFalse( $info->isIdSafe() );
1215 $this->assertSame( array(), $logger->getBuffer() );
1216
1217 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1218 'provider' => $provider2,
1219 'id' => $id,
1220 ) );
1221 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1222 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1223 $this->assertSame( array(
1224 array( LogLevel::INFO, 'Session X: No user provided and provider cannot set user' )
1225 ), $logger->getBuffer() );
1226 $logger->clearBuffer();
1227
1228 // Incomplete/bad metadata
1229 $this->store->setRawSession( $id, true );
1230 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1231 $this->assertSame( array(
1232 array( LogLevel::WARNING, 'Session X: Bad data' ),
1233 ), $logger->getBuffer() );
1234 $logger->clearBuffer();
1235
1236 $this->store->setRawSession( $id, array( 'data' => array() ) );
1237 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1238 $this->assertSame( array(
1239 array( LogLevel::WARNING, 'Session X: Bad data structure' ),
1240 ), $logger->getBuffer() );
1241 $logger->clearBuffer();
1242
1243 $this->store->deleteSession( $id );
1244 $this->store->setRawSession( $id, array( 'metadata' => $metadata ) );
1245 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1246 $this->assertSame( array(
1247 array( LogLevel::WARNING, 'Session X: Bad data structure' ),
1248 ), $logger->getBuffer() );
1249 $logger->clearBuffer();
1250
1251 $this->store->setRawSession( $id, array( 'metadata' => $metadata, 'data' => true ) );
1252 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1253 $this->assertSame( array(
1254 array( LogLevel::WARNING, 'Session X: Bad data structure' ),
1255 ), $logger->getBuffer() );
1256 $logger->clearBuffer();
1257
1258 $this->store->setRawSession( $id, array( 'metadata' => true, 'data' => array() ) );
1259 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1260 $this->assertSame( array(
1261 array( LogLevel::WARNING, 'Session X: Bad data structure' ),
1262 ), $logger->getBuffer() );
1263 $logger->clearBuffer();
1264
1265 foreach ( $metadata as $key => $dummy ) {
1266 $tmp = $metadata;
1267 unset( $tmp[$key] );
1268 $this->store->setRawSession( $id, array( 'metadata' => $tmp, 'data' => array() ) );
1269 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1270 $this->assertSame( array(
1271 array( LogLevel::WARNING, 'Session X: Bad metadata' ),
1272 ), $logger->getBuffer() );
1273 $logger->clearBuffer();
1274 }
1275
1276 // Basic usage with metadata
1277 $this->store->setRawSession( $id, array( 'metadata' => $metadata, 'data' => array() ) );
1278 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1279 'provider' => $provider,
1280 'id' => $id,
1281 'userInfo' => $userInfo
1282 ) );
1283 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1284 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1285 $this->assertTrue( $info->isIdSafe() );
1286 $this->assertSame( array(), $logger->getBuffer() );
1287
1288 // Mismatched provider
1289 $this->store->setSessionMeta( $id, array( 'provider' => 'Bad' ) + $metadata );
1290 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1291 'provider' => $provider,
1292 'id' => $id,
1293 'userInfo' => $userInfo
1294 ) );
1295 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1296 $this->assertSame( array(
1297 array( LogLevel::WARNING, 'Session X: Wrong provider, Bad !== Mock' ),
1298 ), $logger->getBuffer() );
1299 $logger->clearBuffer();
1300
1301 // Unknown provider
1302 $this->store->setSessionMeta( $id, array( 'provider' => 'Bad' ) + $metadata );
1303 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1304 'id' => $id,
1305 'userInfo' => $userInfo
1306 ) );
1307 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1308 $this->assertSame( array(
1309 array( LogLevel::WARNING, 'Session X: Unknown provider, Bad' ),
1310 ), $logger->getBuffer() );
1311 $logger->clearBuffer();
1312
1313 // Fill in provider
1314 $this->store->setSessionMeta( $id, $metadata );
1315 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1316 'id' => $id,
1317 'userInfo' => $userInfo
1318 ) );
1319 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1320 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1321 $this->assertTrue( $info->isIdSafe() );
1322 $this->assertSame( array(), $logger->getBuffer() );
1323
1324 // Bad user metadata
1325 $this->store->setSessionMeta( $id, array( 'userId' => -1, 'userToken' => null ) + $metadata );
1326 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1327 'provider' => $provider,
1328 'id' => $id,
1329 ) );
1330 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1331 $this->assertSame( array(
1332 array( LogLevel::ERROR, 'Session X: Invalid ID' ),
1333 ), $logger->getBuffer() );
1334 $logger->clearBuffer();
1335
1336 $this->store->setSessionMeta(
1337 $id, array( 'userId' => 0, 'userName' => '<X>', 'userToken' => null ) + $metadata
1338 );
1339 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1340 'provider' => $provider,
1341 'id' => $id,
1342 ) );
1343 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1344 $this->assertSame( array(
1345 array( LogLevel::ERROR, 'Session X: Invalid user name' ),
1346 ), $logger->getBuffer() );
1347 $logger->clearBuffer();
1348
1349 // Mismatched user by ID
1350 $this->store->setSessionMeta(
1351 $id, array( 'userId' => $userInfo->getId() + 1, 'userToken' => null ) + $metadata
1352 );
1353 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1354 'provider' => $provider,
1355 'id' => $id,
1356 'userInfo' => $userInfo
1357 ) );
1358 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1359 $this->assertSame( array(
1360 array( LogLevel::WARNING, 'Session X: User ID mismatch, 2 !== 1' ),
1361 ), $logger->getBuffer() );
1362 $logger->clearBuffer();
1363
1364 // Mismatched user by name
1365 $this->store->setSessionMeta(
1366 $id, array( 'userId' => 0, 'userName' => 'X', 'userToken' => null ) + $metadata
1367 );
1368 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1369 'provider' => $provider,
1370 'id' => $id,
1371 'userInfo' => $userInfo
1372 ) );
1373 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1374 $this->assertSame( array(
1375 array( LogLevel::WARNING, 'Session X: User name mismatch, X !== UTSysop' ),
1376 ), $logger->getBuffer() );
1377 $logger->clearBuffer();
1378
1379 // ID matches, name doesn't
1380 $this->store->setSessionMeta(
1381 $id, array( 'userId' => $userInfo->getId(), 'userName' => 'X', 'userToken' => null ) + $metadata
1382 );
1383 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1384 'provider' => $provider,
1385 'id' => $id,
1386 'userInfo' => $userInfo
1387 ) );
1388 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1389 $this->assertSame( array(
1390 array(
1391 LogLevel::WARNING, 'Session X: User ID matched but name didn\'t (rename?), X !== UTSysop'
1392 ),
1393 ), $logger->getBuffer() );
1394 $logger->clearBuffer();
1395
1396 // Mismatched anon user
1397 $this->store->setSessionMeta(
1398 $id, array( 'userId' => 0, 'userName' => null, 'userToken' => null ) + $metadata
1399 );
1400 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1401 'provider' => $provider,
1402 'id' => $id,
1403 'userInfo' => $userInfo
1404 ) );
1405 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1406 $this->assertSame( array(
1407 array(
1408 LogLevel::WARNING, 'Session X: Metadata has an anonymous user, but a non-anon user was provided'
1409 ),
1410 ), $logger->getBuffer() );
1411 $logger->clearBuffer();
1412
1413 // Lookup user by ID
1414 $this->store->setSessionMeta( $id, array( 'userToken' => null ) + $metadata );
1415 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1416 'provider' => $provider,
1417 'id' => $id,
1418 ) );
1419 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1420 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1421 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1422 $this->assertTrue( $info->isIdSafe() );
1423 $this->assertSame( array(), $logger->getBuffer() );
1424
1425 // Lookup user by name
1426 $this->store->setSessionMeta(
1427 $id, array( 'userId' => 0, 'userName' => 'UTSysop', 'userToken' => null ) + $metadata
1428 );
1429 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1430 'provider' => $provider,
1431 'id' => $id,
1432 ) );
1433 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1434 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1435 $this->assertSame( $userInfo->getId(), $info->getUserInfo()->getId() );
1436 $this->assertTrue( $info->isIdSafe() );
1437 $this->assertSame( array(), $logger->getBuffer() );
1438
1439 // Lookup anonymous user
1440 $this->store->setSessionMeta(
1441 $id, array( 'userId' => 0, 'userName' => null, 'userToken' => null ) + $metadata
1442 );
1443 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1444 'provider' => $provider,
1445 'id' => $id,
1446 ) );
1447 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1448 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1449 $this->assertTrue( $info->getUserInfo()->isAnon() );
1450 $this->assertTrue( $info->isIdSafe() );
1451 $this->assertSame( array(), $logger->getBuffer() );
1452
1453 // Unverified user with metadata
1454 $this->store->setSessionMeta( $id, $metadata );
1455 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1456 'provider' => $provider,
1457 'id' => $id,
1458 'userInfo' => $unverifiedUserInfo
1459 ) );
1460 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1461 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1462 $this->assertTrue( $info->getUserInfo()->isVerified() );
1463 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1464 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1465 $this->assertTrue( $info->isIdSafe() );
1466 $this->assertSame( array(), $logger->getBuffer() );
1467
1468 // Unverified user with metadata
1469 $this->store->setSessionMeta( $id, $metadata );
1470 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1471 'provider' => $provider,
1472 'id' => $id,
1473 'userInfo' => $unverifiedUserInfo
1474 ) );
1475 $this->assertFalse( $info->isIdSafe(), 'sanity check' );
1476 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1477 $this->assertTrue( $info->getUserInfo()->isVerified() );
1478 $this->assertSame( $unverifiedUserInfo->getId(), $info->getUserInfo()->getId() );
1479 $this->assertSame( $unverifiedUserInfo->getName(), $info->getUserInfo()->getName() );
1480 $this->assertTrue( $info->isIdSafe() );
1481 $this->assertSame( array(), $logger->getBuffer() );
1482
1483 // Wrong token
1484 $this->store->setSessionMeta( $id, array( 'userToken' => 'Bad' ) + $metadata );
1485 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1486 'provider' => $provider,
1487 'id' => $id,
1488 'userInfo' => $userInfo
1489 ) );
1490 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1491 $this->assertSame( array(
1492 array( LogLevel::WARNING, 'Session X: User token mismatch' ),
1493 ), $logger->getBuffer() );
1494 $logger->clearBuffer();
1495
1496 // Provider metadata
1497 $this->store->setSessionMeta( $id, array( 'provider' => 'Mock2' ) + $metadata );
1498 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1499 'provider' => $provider2,
1500 'id' => $id,
1501 'userInfo' => $userInfo,
1502 'metadata' => array( 'Info' ),
1503 ) );
1504 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1505 $this->assertSame( array( 'Info', 'changed' => true ), $info->getProviderMetadata() );
1506 $this->assertSame( array(), $logger->getBuffer() );
1507
1508 $this->store->setSessionMeta( $id, array( 'providerMetadata' => array( 'Saved' ) ) + $metadata );
1509 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1510 'provider' => $provider,
1511 'id' => $id,
1512 'userInfo' => $userInfo,
1513 ) );
1514 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1515 $this->assertSame( array( 'Saved' ), $info->getProviderMetadata() );
1516 $this->assertSame( array(), $logger->getBuffer() );
1517
1518 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1519 'provider' => $provider,
1520 'id' => $id,
1521 'userInfo' => $userInfo,
1522 'metadata' => array( 'Info' ),
1523 ) );
1524 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1525 $this->assertSame( array( 'Merged' ), $info->getProviderMetadata() );
1526 $this->assertSame( array(), $logger->getBuffer() );
1527
1528 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1529 'provider' => $provider,
1530 'id' => $id,
1531 'userInfo' => $userInfo,
1532 'metadata' => array( 'Throw' ),
1533 ) );
1534 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1535 $this->assertSame( array(
1536 array( LogLevel::WARNING, 'Session X: Metadata merge failed: no merge!' ),
1537 ), $logger->getBuffer() );
1538 $logger->clearBuffer();
1539
1540 // Remember from session
1541 $this->store->setSessionMeta( $id, $metadata );
1542 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1543 'provider' => $provider,
1544 'id' => $id,
1545 ) );
1546 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1547 $this->assertFalse( $info->wasRemembered() );
1548 $this->assertSame( array(), $logger->getBuffer() );
1549
1550 $this->store->setSessionMeta( $id, array( 'remember' => true ) + $metadata );
1551 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1552 'provider' => $provider,
1553 'id' => $id,
1554 ) );
1555 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1556 $this->assertTrue( $info->wasRemembered() );
1557 $this->assertSame( array(), $logger->getBuffer() );
1558
1559 $this->store->setSessionMeta( $id, array( 'remember' => false ) + $metadata );
1560 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1561 'provider' => $provider,
1562 'id' => $id,
1563 'userInfo' => $userInfo
1564 ) );
1565 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1566 $this->assertTrue( $info->wasRemembered() );
1567 $this->assertSame( array(), $logger->getBuffer() );
1568
1569 // forceHTTPS from session
1570 $this->store->setSessionMeta( $id, $metadata );
1571 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1572 'provider' => $provider,
1573 'id' => $id,
1574 'userInfo' => $userInfo
1575 ) );
1576 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1577 $this->assertFalse( $info->forceHTTPS() );
1578 $this->assertSame( array(), $logger->getBuffer() );
1579
1580 $this->store->setSessionMeta( $id, array( 'forceHTTPS' => true ) + $metadata );
1581 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1582 'provider' => $provider,
1583 'id' => $id,
1584 'userInfo' => $userInfo
1585 ) );
1586 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1587 $this->assertTrue( $info->forceHTTPS() );
1588 $this->assertSame( array(), $logger->getBuffer() );
1589
1590 $this->store->setSessionMeta( $id, array( 'forceHTTPS' => false ) + $metadata );
1591 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1592 'provider' => $provider,
1593 'id' => $id,
1594 'userInfo' => $userInfo,
1595 'forceHTTPS' => true
1596 ) );
1597 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1598 $this->assertTrue( $info->forceHTTPS() );
1599 $this->assertSame( array(), $logger->getBuffer() );
1600
1601 // "Persist" flag from session
1602 $this->store->setSessionMeta( $id, $metadata );
1603 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1604 'provider' => $provider,
1605 'id' => $id,
1606 'userInfo' => $userInfo
1607 ) );
1608 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1609 $this->assertFalse( $info->wasPersisted() );
1610 $this->assertSame( array(), $logger->getBuffer() );
1611
1612 $this->store->setSessionMeta( $id, array( 'persisted' => true ) + $metadata );
1613 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1614 'provider' => $provider,
1615 'id' => $id,
1616 'userInfo' => $userInfo
1617 ) );
1618 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1619 $this->assertTrue( $info->wasPersisted() );
1620 $this->assertSame( array(), $logger->getBuffer() );
1621
1622 $this->store->setSessionMeta( $id, array( 'persisted' => false ) + $metadata );
1623 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1624 'provider' => $provider,
1625 'id' => $id,
1626 'userInfo' => $userInfo,
1627 'persisted' => true
1628 ) );
1629 $this->assertTrue( $loadSessionInfoFromStore( $info ) );
1630 $this->assertTrue( $info->wasPersisted() );
1631 $this->assertSame( array(), $logger->getBuffer() );
1632
1633 // Provider refreshSessionInfo() returning false
1634 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1635 'provider' => $provider3,
1636 ) );
1637 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1638 $this->assertSame( array(), $logger->getBuffer() );
1639
1640 // Hook
1641 $that = $this;
1642 $called = false;
1643 $data = array( 'foo' => 1 );
1644 $this->store->setSession( $id, array( 'metadata' => $metadata, 'data' => $data ) );
1645 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, array(
1646 'provider' => $provider,
1647 'id' => $id,
1648 'userInfo' => $userInfo
1649 ) );
1650 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
1651 'SessionCheckInfo' => array( function ( &$reason, $i, $r, $m, $d ) use (
1652 $that, $info, $metadata, $data, $request, &$called
1653 ) {
1654 $that->assertSame( $info->getId(), $i->getId() );
1655 $that->assertSame( $info->getProvider(), $i->getProvider() );
1656 $that->assertSame( $info->getUserInfo(), $i->getUserInfo() );
1657 $that->assertSame( $request, $r );
1658 $that->assertEquals( $metadata, $m );
1659 $that->assertEquals( $data, $d );
1660 $called = true;
1661 return false;
1662 } )
1663 ) );
1664 $this->assertFalse( $loadSessionInfoFromStore( $info ) );
1665 $this->assertTrue( $called );
1666 $this->assertSame( array(
1667 array( LogLevel::WARNING, 'Session X: Hook aborted' ),
1668 ), $logger->getBuffer() );
1669 $logger->clearBuffer();
1670 }
1671
1672 }