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