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