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