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