MediaWiki REL1_31
CookieSessionProviderTest.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Session;
4
6use User;
7use Psr\Log\LogLevel;
8use Wikimedia\TestingAccessWrapper;
9
16
17 private function getConfig() {
18 return new \HashConfig( [
19 'CookiePrefix' => 'CookiePrefix',
20 'CookiePath' => 'CookiePath',
21 'CookieDomain' => 'CookieDomain',
22 'CookieSecure' => true,
23 'CookieHttpOnly' => true,
24 'CookieSameSite' => '',
25 'SessionName' => false,
26 'CookieExpiration' => 100,
27 'ExtendedLoginCookieExpiration' => 200,
28 'ForceHTTPS' => false,
29 ] );
30 }
31
35 public static function provideForceHTTPS() {
36 return [
37 [ false ],
38 [ true ]
39 ];
40 }
41
42 public function testConstructor() {
43 try {
45 $this->fail( 'Expected exception not thrown' );
46 } catch ( \InvalidArgumentException $ex ) {
47 $this->assertSame(
48 'MediaWiki\\Session\\CookieSessionProvider::__construct: priority must be specified',
49 $ex->getMessage()
50 );
51 }
52
53 try {
54 new CookieSessionProvider( [ 'priority' => 'foo' ] );
55 $this->fail( 'Expected exception not thrown' );
56 } catch ( \InvalidArgumentException $ex ) {
57 $this->assertSame(
58 'MediaWiki\\Session\\CookieSessionProvider::__construct: Invalid priority',
59 $ex->getMessage()
60 );
61 }
62 try {
63 new CookieSessionProvider( [ 'priority' => SessionInfo::MIN_PRIORITY - 1 ] );
64 $this->fail( 'Expected exception not thrown' );
65 } catch ( \InvalidArgumentException $ex ) {
66 $this->assertSame(
67 'MediaWiki\\Session\\CookieSessionProvider::__construct: Invalid priority',
68 $ex->getMessage()
69 );
70 }
71 try {
72 new CookieSessionProvider( [ 'priority' => SessionInfo::MAX_PRIORITY + 1 ] );
73 $this->fail( 'Expected exception not thrown' );
74 } catch ( \InvalidArgumentException $ex ) {
75 $this->assertSame(
76 'MediaWiki\\Session\\CookieSessionProvider::__construct: Invalid priority',
77 $ex->getMessage()
78 );
79 }
80
81 try {
82 new CookieSessionProvider( [ 'priority' => 1, 'cookieOptions' => null ] );
83 $this->fail( 'Expected exception not thrown' );
84 } catch ( \InvalidArgumentException $ex ) {
85 $this->assertSame(
86 'MediaWiki\\Session\\CookieSessionProvider::__construct: cookieOptions must be an array',
87 $ex->getMessage()
88 );
89 }
90
91 $config = $this->getConfig();
92 $p = TestingAccessWrapper::newFromObject(
93 new CookieSessionProvider( [ 'priority' => 1 ] )
94 );
95 $p->setLogger( new \TestLogger() );
96 $p->setConfig( $config );
97 $this->assertEquals( 1, $p->priority );
98 $this->assertEquals( [
99 'callUserSetCookiesHook' => false,
100 'sessionName' => 'CookiePrefix_session',
101 ], $p->params );
102 $this->assertEquals( [
103 'prefix' => 'CookiePrefix',
104 'path' => 'CookiePath',
105 'domain' => 'CookieDomain',
106 'secure' => true,
107 'httpOnly' => true,
108 'sameSite' => '',
109 ], $p->cookieOptions );
110
111 $config->set( 'SessionName', 'SessionName' );
112 $p = TestingAccessWrapper::newFromObject(
113 new CookieSessionProvider( [ 'priority' => 3 ] )
114 );
115 $p->setLogger( new \TestLogger() );
116 $p->setConfig( $config );
117 $this->assertEquals( 3, $p->priority );
118 $this->assertEquals( [
119 'callUserSetCookiesHook' => false,
120 'sessionName' => 'SessionName',
121 ], $p->params );
122 $this->assertEquals( [
123 'prefix' => 'CookiePrefix',
124 'path' => 'CookiePath',
125 'domain' => 'CookieDomain',
126 'secure' => true,
127 'httpOnly' => true,
128 'sameSite' => '',
129 ], $p->cookieOptions );
130
131 $p = TestingAccessWrapper::newFromObject( new CookieSessionProvider( [
132 'priority' => 10,
133 'callUserSetCookiesHook' => true,
134 'cookieOptions' => [
135 'prefix' => 'XPrefix',
136 'path' => 'XPath',
137 'domain' => 'XDomain',
138 'secure' => 'XSecure',
139 'httpOnly' => 'XHttpOnly',
140 'sameSite' => 'XSameSite',
141 ],
142 'sessionName' => 'XSession',
143 ] ) );
144 $p->setLogger( new \TestLogger() );
145 $p->setConfig( $config );
146 $this->assertEquals( 10, $p->priority );
147 $this->assertEquals( [
148 'callUserSetCookiesHook' => true,
149 'sessionName' => 'XSession',
150 ], $p->params );
151 $this->assertEquals( [
152 'prefix' => 'XPrefix',
153 'path' => 'XPath',
154 'domain' => 'XDomain',
155 'secure' => 'XSecure',
156 'httpOnly' => 'XHttpOnly',
157 'sameSite' => 'XSameSite',
158 ], $p->cookieOptions );
159 }
160
161 public function testBasics() {
162 $provider = new CookieSessionProvider( [ 'priority' => 10 ] );
163
164 $this->assertTrue( $provider->persistsSessionId() );
165 $this->assertTrue( $provider->canChangeUser() );
166
167 $extendedCookies = [ 'UserID', 'UserName', 'Token' ];
168
169 $this->assertEquals(
170 $extendedCookies,
171 TestingAccessWrapper::newFromObject( $provider )->getExtendedLoginCookies(),
172 'List of extended cookies (subclasses can add values, but we\'re calling the core one here)'
173 );
174
175 $msg = $provider->whyNoSession();
176 $this->assertInstanceOf( \Message::class, $msg );
177 $this->assertSame( 'sessionprovider-nocookies', $msg->getKey() );
178 }
179
180 public function testProvideSessionInfo() {
181 $params = [
182 'priority' => 20,
183 'sessionName' => 'session',
184 'cookieOptions' => [ 'prefix' => 'x' ],
185 ];
186 $provider = new CookieSessionProvider( $params );
187 $logger = new \TestLogger( true );
188 $provider->setLogger( $logger );
189 $provider->setConfig( $this->getConfig() );
190 $provider->setManager( new SessionManager() );
191
192 $user = static::getTestSysop()->getUser();
193 $id = $user->getId();
194 $name = $user->getName();
195 $token = $user->getToken( true );
196
197 $sessionId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
198
199 // No data
200 $request = new \FauxRequest();
201 $info = $provider->provideSessionInfo( $request );
202 $this->assertNull( $info );
203 $this->assertSame( [], $logger->getBuffer() );
204 $logger->clearBuffer();
205
206 // Session key only
207 $request = new \FauxRequest();
208 $request->setCookies( [
209 'session' => $sessionId,
210 ], '' );
211 $info = $provider->provideSessionInfo( $request );
212 $this->assertNotNull( $info );
213 $this->assertSame( $params['priority'], $info->getPriority() );
214 $this->assertSame( $sessionId, $info->getId() );
215 $this->assertNotNull( $info->getUserInfo() );
216 $this->assertSame( 0, $info->getUserInfo()->getId() );
217 $this->assertNull( $info->getUserInfo()->getName() );
218 $this->assertFalse( $info->forceHTTPS() );
219 $this->assertSame( [
220 [
221 LogLevel::DEBUG,
222 'Session "{session}" requested without UserID cookie',
223 ],
224 ], $logger->getBuffer() );
225 $logger->clearBuffer();
226
227 // User, no session key
228 $request = new \FauxRequest();
229 $request->setCookies( [
230 'xUserID' => $id,
231 'xToken' => $token,
232 ], '' );
233 $info = $provider->provideSessionInfo( $request );
234 $this->assertNotNull( $info );
235 $this->assertSame( $params['priority'], $info->getPriority() );
236 $this->assertNotSame( $sessionId, $info->getId() );
237 $this->assertNotNull( $info->getUserInfo() );
238 $this->assertSame( $id, $info->getUserInfo()->getId() );
239 $this->assertSame( $name, $info->getUserInfo()->getName() );
240 $this->assertFalse( $info->forceHTTPS() );
241 $this->assertSame( [], $logger->getBuffer() );
242 $logger->clearBuffer();
243
244 // User and session key
245 $request = new \FauxRequest();
246 $request->setCookies( [
247 'session' => $sessionId,
248 'xUserID' => $id,
249 'xToken' => $token,
250 ], '' );
251 $info = $provider->provideSessionInfo( $request );
252 $this->assertNotNull( $info );
253 $this->assertSame( $params['priority'], $info->getPriority() );
254 $this->assertSame( $sessionId, $info->getId() );
255 $this->assertNotNull( $info->getUserInfo() );
256 $this->assertSame( $id, $info->getUserInfo()->getId() );
257 $this->assertSame( $name, $info->getUserInfo()->getName() );
258 $this->assertFalse( $info->forceHTTPS() );
259 $this->assertSame( [], $logger->getBuffer() );
260 $logger->clearBuffer();
261
262 // User with bad token
263 $request = new \FauxRequest();
264 $request->setCookies( [
265 'session' => $sessionId,
266 'xUserID' => $id,
267 'xToken' => 'BADTOKEN',
268 ], '' );
269 $info = $provider->provideSessionInfo( $request );
270 $this->assertNull( $info );
271 $this->assertSame( [
272 [
273 LogLevel::WARNING,
274 'Session "{session}" requested with invalid Token cookie.'
275 ],
276 ], $logger->getBuffer() );
277 $logger->clearBuffer();
278
279 // User id with no token
280 $request = new \FauxRequest();
281 $request->setCookies( [
282 'session' => $sessionId,
283 'xUserID' => $id,
284 ], '' );
285 $info = $provider->provideSessionInfo( $request );
286 $this->assertNotNull( $info );
287 $this->assertSame( $params['priority'], $info->getPriority() );
288 $this->assertSame( $sessionId, $info->getId() );
289 $this->assertNotNull( $info->getUserInfo() );
290 $this->assertFalse( $info->getUserInfo()->isVerified() );
291 $this->assertSame( $id, $info->getUserInfo()->getId() );
292 $this->assertSame( $name, $info->getUserInfo()->getName() );
293 $this->assertFalse( $info->forceHTTPS() );
294 $this->assertSame( [], $logger->getBuffer() );
295 $logger->clearBuffer();
296
297 $request = new \FauxRequest();
298 $request->setCookies( [
299 'xUserID' => $id,
300 ], '' );
301 $info = $provider->provideSessionInfo( $request );
302 $this->assertNull( $info );
303 $this->assertSame( [], $logger->getBuffer() );
304 $logger->clearBuffer();
305
306 // User and session key, with forceHTTPS flag
307 $request = new \FauxRequest();
308 $request->setCookies( [
309 'session' => $sessionId,
310 'xUserID' => $id,
311 'xToken' => $token,
312 'forceHTTPS' => true,
313 ], '' );
314 $info = $provider->provideSessionInfo( $request );
315 $this->assertNotNull( $info );
316 $this->assertSame( $params['priority'], $info->getPriority() );
317 $this->assertSame( $sessionId, $info->getId() );
318 $this->assertNotNull( $info->getUserInfo() );
319 $this->assertSame( $id, $info->getUserInfo()->getId() );
320 $this->assertSame( $name, $info->getUserInfo()->getName() );
321 $this->assertTrue( $info->forceHTTPS() );
322 $this->assertSame( [], $logger->getBuffer() );
323 $logger->clearBuffer();
324
325 // Invalid user id
326 $request = new \FauxRequest();
327 $request->setCookies( [
328 'session' => $sessionId,
329 'xUserID' => '-1',
330 ], '' );
331 $info = $provider->provideSessionInfo( $request );
332 $this->assertNull( $info );
333 $this->assertSame( [], $logger->getBuffer() );
334 $logger->clearBuffer();
335
336 // User id with matching name
337 $request = new \FauxRequest();
338 $request->setCookies( [
339 'session' => $sessionId,
340 'xUserID' => $id,
341 'xUserName' => $name,
342 ], '' );
343 $info = $provider->provideSessionInfo( $request );
344 $this->assertNotNull( $info );
345 $this->assertSame( $params['priority'], $info->getPriority() );
346 $this->assertSame( $sessionId, $info->getId() );
347 $this->assertNotNull( $info->getUserInfo() );
348 $this->assertFalse( $info->getUserInfo()->isVerified() );
349 $this->assertSame( $id, $info->getUserInfo()->getId() );
350 $this->assertSame( $name, $info->getUserInfo()->getName() );
351 $this->assertFalse( $info->forceHTTPS() );
352 $this->assertSame( [], $logger->getBuffer() );
353 $logger->clearBuffer();
354
355 // User id with wrong name
356 $request = new \FauxRequest();
357 $request->setCookies( [
358 'session' => $sessionId,
359 'xUserID' => $id,
360 'xUserName' => 'Wrong',
361 ], '' );
362 $info = $provider->provideSessionInfo( $request );
363 $this->assertNull( $info );
364 $this->assertSame( [
365 [
366 LogLevel::WARNING,
367 'Session "{session}" requested with mismatched UserID and UserName cookies.',
368 ],
369 ], $logger->getBuffer() );
370 $logger->clearBuffer();
371 }
372
373 public function testGetVaryCookies() {
374 $provider = new CookieSessionProvider( [
375 'priority' => 1,
376 'sessionName' => 'MySessionName',
377 'cookieOptions' => [ 'prefix' => 'MyCookiePrefix' ],
378 ] );
379 $this->assertArrayEquals( [
380 'MyCookiePrefixToken',
381 'MyCookiePrefixLoggedOut',
382 'MySessionName',
383 'forceHTTPS',
384 ], $provider->getVaryCookies() );
385 }
386
387 public function testSuggestLoginUsername() {
388 $provider = new CookieSessionProvider( [
389 'priority' => 1,
390 'sessionName' => 'MySessionName',
391 'cookieOptions' => [ 'prefix' => 'x' ],
392 ] );
393
394 $request = new \FauxRequest();
395 $this->assertEquals( null, $provider->suggestLoginUsername( $request ) );
396
397 $request->setCookies( [
398 'xUserName' => 'Example',
399 ], '' );
400 $this->assertEquals( 'Example', $provider->suggestLoginUsername( $request ) );
401 }
402
404 public function testPersistSession( $forceHTTPS ) {
405 $provider = new CookieSessionProvider( [
406 'priority' => 1,
407 'sessionName' => 'MySessionName',
408 'callUserSetCookiesHook' => false,
409 'cookieOptions' => [ 'prefix' => 'x' ],
410 ] );
411 $config = $this->getConfig();
412 $config->set( 'ForceHTTPS', $forceHTTPS );
413 $provider->setLogger( new \TestLogger() );
414 $provider->setConfig( $config );
415 $provider->setManager( SessionManager::singleton() );
416
417 $sessionId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
418 $store = new TestBagOStuff();
419
420 // For User::requiresHTTPS
421 $this->setMwGlobals( [ 'wgForceHTTPS' => $forceHTTPS ] );
422
423 $user = static::getTestSysop()->getUser();
424 $anon = new User;
425
426 $backend = new SessionBackend(
427 new SessionId( $sessionId ),
429 'provider' => $provider,
430 'id' => $sessionId,
431 'persisted' => true,
432 'idIsSafe' => true,
433 ] ),
434 $store,
435 new \Psr\Log\NullLogger(),
436 10
437 );
438 TestingAccessWrapper::newFromObject( $backend )->usePhpSessionHandling = false;
439
440 $mock = $this->getMockBuilder( stdClass::class )
441 ->setMethods( [ 'onUserSetCookies' ] )
442 ->getMock();
443 $mock->expects( $this->never() )->method( 'onUserSetCookies' );
444 $this->mergeMwGlobalArrayValue( 'wgHooks', [ 'UserSetCookies' => [ $mock ] ] );
445
446 // Anonymous user
447 $backend->setUser( $anon );
448 $backend->setRememberUser( true );
449 $backend->setForceHTTPS( false );
450 $request = new \FauxRequest();
451 $provider->persistSession( $backend, $request );
452 $this->assertSame( $sessionId, $request->response()->getCookie( 'MySessionName' ) );
453 $this->assertSame( '', $request->response()->getCookie( 'xUserID' ) );
454 $this->assertSame( null, $request->response()->getCookie( 'xUserName' ) );
455 $this->assertSame( '', $request->response()->getCookie( 'xToken' ) );
456 if ( $forceHTTPS ) {
457 $this->assertSame( null, $request->response()->getCookie( 'forceHTTPS' ) );
458 } else {
459 $this->assertSame( '', $request->response()->getCookie( 'forceHTTPS' ) );
460 }
461 $this->assertSame( [], $backend->getData() );
462
463 // Logged-in user, no remember
464 $backend->setUser( $user );
465 $backend->setRememberUser( false );
466 $backend->setForceHTTPS( false );
467 $request = new \FauxRequest();
468 $provider->persistSession( $backend, $request );
469 $this->assertSame( $sessionId, $request->response()->getCookie( 'MySessionName' ) );
470 $this->assertSame( (string)$user->getId(), $request->response()->getCookie( 'xUserID' ) );
471 $this->assertSame( $user->getName(), $request->response()->getCookie( 'xUserName' ) );
472 $this->assertSame( '', $request->response()->getCookie( 'xToken' ) );
473 if ( $forceHTTPS ) {
474 $this->assertSame( null, $request->response()->getCookie( 'forceHTTPS' ) );
475 } else {
476 $this->assertSame( '', $request->response()->getCookie( 'forceHTTPS' ) );
477 }
478 $this->assertSame( [], $backend->getData() );
479
480 // Logged-in user, remember
481 $backend->setUser( $user );
482 $backend->setRememberUser( true );
483 $backend->setForceHTTPS( true );
484 $request = new \FauxRequest();
485 $time = time();
486 $provider->persistSession( $backend, $request );
487 $this->assertSame( $sessionId, $request->response()->getCookie( 'MySessionName' ) );
488 $this->assertSame( (string)$user->getId(), $request->response()->getCookie( 'xUserID' ) );
489 $this->assertSame( $user->getName(), $request->response()->getCookie( 'xUserName' ) );
490 $this->assertSame( $user->getToken(), $request->response()->getCookie( 'xToken' ) );
491 if ( $forceHTTPS ) {
492 $this->assertSame( null, $request->response()->getCookie( 'forceHTTPS' ) );
493 } else {
494 $this->assertSame( 'true', $request->response()->getCookie( 'forceHTTPS' ) );
495 }
496 $this->assertSame( [], $backend->getData() );
497 }
498
505 public function testCookieData( $secure, $remember, $forceHTTPS ) {
506 $this->setMwGlobals( [
507 'wgSecureLogin' => false,
508 'wgForceHTTPS' => $forceHTTPS,
509 ] );
510
511 $provider = new CookieSessionProvider( [
512 'priority' => 1,
513 'sessionName' => 'MySessionName',
514 'callUserSetCookiesHook' => false,
515 'cookieOptions' => [ 'prefix' => 'x' ],
516 ] );
517 $config = $this->getConfig();
518 $config->set( 'CookieSecure', $secure );
519 $config->set( 'ForceHTTPS', $forceHTTPS );
520 $provider->setLogger( new \TestLogger() );
521 $provider->setConfig( $config );
522 $provider->setManager( SessionManager::singleton() );
523
524 $sessionId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
525 $user = static::getTestSysop()->getUser();
526 $this->assertSame( $user->requiresHTTPS(), $forceHTTPS, 'sanity check' );
527
528 $backend = new SessionBackend(
529 new SessionId( $sessionId ),
531 'provider' => $provider,
532 'id' => $sessionId,
533 'persisted' => true,
534 'idIsSafe' => true,
535 ] ),
536 new TestBagOStuff(),
537 new \Psr\Log\NullLogger(),
538 10
539 );
540 TestingAccessWrapper::newFromObject( $backend )->usePhpSessionHandling = false;
541 $backend->setUser( $user );
542 $backend->setRememberUser( $remember );
543 $backend->setForceHTTPS( $secure );
544 $request = new \FauxRequest();
545 $time = time();
546 $provider->persistSession( $backend, $request );
547
548 $defaults = [
549 'expire' => (int)100,
550 'path' => $config->get( 'CookiePath' ),
551 'domain' => $config->get( 'CookieDomain' ),
552 'secure' => $secure || $forceHTTPS,
553 'httpOnly' => $config->get( 'CookieHttpOnly' ),
554 'raw' => false,
555 ];
556
557 $normalExpiry = $config->get( 'CookieExpiration' );
558 $extendedExpiry = $config->get( 'ExtendedLoginCookieExpiration' );
559 $extendedExpiry = (int)( $extendedExpiry === null ? 0 : $extendedExpiry );
560 $expect = [
561 'MySessionName' => [
562 'value' => (string)$sessionId,
563 'expire' => 0,
564 ] + $defaults,
565 'xUserID' => [
566 'value' => (string)$user->getId(),
567 'expire' => $remember ? $extendedExpiry : $normalExpiry,
568 ] + $defaults,
569 'xUserName' => [
570 'value' => $user->getName(),
571 'expire' => $remember ? $extendedExpiry : $normalExpiry
572 ] + $defaults,
573 'xToken' => [
574 'value' => $remember ? $user->getToken() : '',
575 'expire' => $remember ? $extendedExpiry : -31536000,
576 ] + $defaults
577 ];
578 if ( !$forceHTTPS ) {
579 $expect['forceHTTPS'] = [
580 'value' => $secure ? 'true' : '',
581 'secure' => false,
582 'expire' => $secure ? $remember ? $defaults['expire'] : 0 : -31536000,
583 ] + $defaults;
584 }
585 foreach ( $expect as $key => $value ) {
586 $actual = $request->response()->getCookieData( $key );
587 if ( $actual && $actual['expire'] > 0 ) {
588 // Round expiry so we don't randomly fail if the seconds ticked during the test.
589 $actual['expire'] = round( $actual['expire'] - $time, -2 );
590 }
591 $this->assertEquals( $value, $actual, "Cookie $key" );
592 }
593 }
594
595 public static function provideCookieData() {
596 return \ArrayUtils::cartesianProduct(
597 [ false, true ], // $secure
598 [ false, true ], // $remember
599 [ false, true ] // $forceHTTPS
600 );
601 }
602
603 protected function getSentRequest() {
604 $sentResponse = $this->getMockBuilder( \FauxResponse::class )
605 ->setMethods( [ 'headersSent', 'setCookie', 'header' ] )->getMock();
606 $sentResponse->expects( $this->any() )->method( 'headersSent' )
607 ->will( $this->returnValue( true ) );
608 $sentResponse->expects( $this->never() )->method( 'setCookie' );
609 $sentResponse->expects( $this->never() )->method( 'header' );
610
611 $sentRequest = $this->getMockBuilder( \FauxRequest::class )
612 ->setMethods( [ 'response' ] )->getMock();
613 $sentRequest->expects( $this->any() )->method( 'response' )
614 ->will( $this->returnValue( $sentResponse ) );
615 return $sentRequest;
616 }
617
618 public function testPersistSessionWithHook() {
619 $provider = new CookieSessionProvider( [
620 'priority' => 1,
621 'sessionName' => 'MySessionName',
622 'callUserSetCookiesHook' => true,
623 'cookieOptions' => [ 'prefix' => 'x' ],
624 ] );
625 $provider->setLogger( new \Psr\Log\NullLogger() );
626 $provider->setConfig( $this->getConfig() );
627 $provider->setManager( SessionManager::singleton() );
628
629 // For User::requiresHTTPS
630 $this->setMwGlobals( [ 'wgForceHTTPS' => false ] );
631
632 $sessionId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
633 $store = new TestBagOStuff();
634 $user = static::getTestSysop()->getUser();
635 $anon = new User;
636
637 $backend = new SessionBackend(
638 new SessionId( $sessionId ),
640 'provider' => $provider,
641 'id' => $sessionId,
642 'persisted' => true,
643 'idIsSafe' => true,
644 ] ),
645 $store,
646 new \Psr\Log\NullLogger(),
647 10
648 );
649 TestingAccessWrapper::newFromObject( $backend )->usePhpSessionHandling = false;
650
651 // Anonymous user
652 $mock = $this->getMockBuilder( stdClass::class )
653 ->setMethods( [ 'onUserSetCookies' ] )->getMock();
654 $mock->expects( $this->never() )->method( 'onUserSetCookies' );
655 $this->mergeMwGlobalArrayValue( 'wgHooks', [ 'UserSetCookies' => [ $mock ] ] );
656 $backend->setUser( $anon );
657 $backend->setRememberUser( true );
658 $backend->setForceHTTPS( false );
659 $request = new \FauxRequest();
660 $provider->persistSession( $backend, $request );
661 $this->assertSame( $sessionId, $request->response()->getCookie( 'MySessionName' ) );
662 $this->assertSame( '', $request->response()->getCookie( 'xUserID' ) );
663 $this->assertSame( null, $request->response()->getCookie( 'xUserName' ) );
664 $this->assertSame( '', $request->response()->getCookie( 'xToken' ) );
665 $this->assertSame( '', $request->response()->getCookie( 'forceHTTPS' ) );
666 $this->assertSame( [], $backend->getData() );
667
668 $provider->persistSession( $backend, $this->getSentRequest() );
669
670 // Logged-in user, no remember
671 $mock = $this->getMockBuilder( __CLASS__ )
672 ->setMethods( [ 'onUserSetCookies' ] )->getMock();
673 $mock->expects( $this->once() )->method( 'onUserSetCookies' )
674 ->will( $this->returnCallback( function ( $u, &$sessionData, &$cookies ) use ( $user ) {
675 $this->assertSame( $user, $u );
676 $this->assertEquals( [
677 'wsUserID' => $user->getId(),
678 'wsUserName' => $user->getName(),
679 'wsToken' => $user->getToken(),
680 ], $sessionData );
681 $this->assertEquals( [
682 'UserID' => $user->getId(),
683 'UserName' => $user->getName(),
684 'Token' => false,
685 ], $cookies );
686
687 $sessionData['foo'] = 'foo!';
688 $cookies['bar'] = 'bar!';
689 return true;
690 } ) );
691 $this->mergeMwGlobalArrayValue( 'wgHooks', [ 'UserSetCookies' => [ $mock ] ] );
692 $backend->setUser( $user );
693 $backend->setRememberUser( false );
694 $backend->setForceHTTPS( false );
695 $backend->setLoggedOutTimestamp( $loggedOut = time() );
696 $request = new \FauxRequest();
697 $provider->persistSession( $backend, $request );
698 $this->assertSame( $sessionId, $request->response()->getCookie( 'MySessionName' ) );
699 $this->assertSame( (string)$user->getId(), $request->response()->getCookie( 'xUserID' ) );
700 $this->assertSame( $user->getName(), $request->response()->getCookie( 'xUserName' ) );
701 $this->assertSame( '', $request->response()->getCookie( 'xToken' ) );
702 $this->assertSame( '', $request->response()->getCookie( 'forceHTTPS' ) );
703 $this->assertSame( 'bar!', $request->response()->getCookie( 'xbar' ) );
704 $this->assertSame( (string)$loggedOut, $request->response()->getCookie( 'xLoggedOut' ) );
705 $this->assertEquals( [
706 'wsUserID' => $user->getId(),
707 'wsUserName' => $user->getName(),
708 'wsToken' => $user->getToken(),
709 'foo' => 'foo!',
710 ], $backend->getData() );
711
712 $provider->persistSession( $backend, $this->getSentRequest() );
713
714 // Logged-in user, remember
715 $mock = $this->getMockBuilder( __CLASS__ )
716 ->setMethods( [ 'onUserSetCookies' ] )->getMock();
717 $mock->expects( $this->once() )->method( 'onUserSetCookies' )
718 ->will( $this->returnCallback( function ( $u, &$sessionData, &$cookies ) use ( $user ) {
719 $this->assertSame( $user, $u );
720 $this->assertEquals( [
721 'wsUserID' => $user->getId(),
722 'wsUserName' => $user->getName(),
723 'wsToken' => $user->getToken(),
724 ], $sessionData );
725 $this->assertEquals( [
726 'UserID' => $user->getId(),
727 'UserName' => $user->getName(),
728 'Token' => $user->getToken(),
729 ], $cookies );
730
731 $sessionData['foo'] = 'foo 2!';
732 $cookies['bar'] = 'bar 2!';
733 return true;
734 } ) );
735 $this->mergeMwGlobalArrayValue( 'wgHooks', [ 'UserSetCookies' => [ $mock ] ] );
736 $backend->setUser( $user );
737 $backend->setRememberUser( true );
738 $backend->setForceHTTPS( true );
739 $backend->setLoggedOutTimestamp( 0 );
740 $request = new \FauxRequest();
741 $provider->persistSession( $backend, $request );
742 $this->assertSame( $sessionId, $request->response()->getCookie( 'MySessionName' ) );
743 $this->assertSame( (string)$user->getId(), $request->response()->getCookie( 'xUserID' ) );
744 $this->assertSame( $user->getName(), $request->response()->getCookie( 'xUserName' ) );
745 $this->assertSame( $user->getToken(), $request->response()->getCookie( 'xToken' ) );
746 $this->assertSame( 'true', $request->response()->getCookie( 'forceHTTPS' ) );
747 $this->assertSame( 'bar 2!', $request->response()->getCookie( 'xbar' ) );
748 $this->assertSame( null, $request->response()->getCookie( 'xLoggedOut' ) );
749 $this->assertEquals( [
750 'wsUserID' => $user->getId(),
751 'wsUserName' => $user->getName(),
752 'wsToken' => $user->getToken(),
753 'foo' => 'foo 2!',
754 ], $backend->getData() );
755
756 $provider->persistSession( $backend, $this->getSentRequest() );
757 }
758
759 public function testUnpersistSession() {
760 $provider = new CookieSessionProvider( [
761 'priority' => 1,
762 'sessionName' => 'MySessionName',
763 'cookieOptions' => [ 'prefix' => 'x' ],
764 ] );
765 $provider->setLogger( new \Psr\Log\NullLogger() );
766 $provider->setConfig( $this->getConfig() );
767 $provider->setManager( SessionManager::singleton() );
768
769 $request = new \FauxRequest();
770 $provider->unpersistSession( $request );
771 $this->assertSame( '', $request->response()->getCookie( 'MySessionName' ) );
772 $this->assertSame( '', $request->response()->getCookie( 'xUserID' ) );
773 $this->assertSame( null, $request->response()->getCookie( 'xUserName' ) );
774 $this->assertSame( '', $request->response()->getCookie( 'xToken' ) );
775 $this->assertSame( '', $request->response()->getCookie( 'forceHTTPS' ) );
776
777 $provider->unpersistSession( $this->getSentRequest() );
778 }
779
780 public function testSetLoggedOutCookie() {
781 $provider = TestingAccessWrapper::newFromObject( new CookieSessionProvider( [
782 'priority' => 1,
783 'sessionName' => 'MySessionName',
784 'cookieOptions' => [ 'prefix' => 'x' ],
785 ] ) );
786 $provider->setLogger( new \Psr\Log\NullLogger() );
787 $provider->setConfig( $this->getConfig() );
788 $provider->setManager( SessionManager::singleton() );
789
790 $t1 = time();
791 $t2 = time() - 86400 * 2;
792
793 // Set it
794 $request = new \FauxRequest();
795 $provider->setLoggedOutCookie( $t1, $request );
796 $this->assertSame( (string)$t1, $request->response()->getCookie( 'xLoggedOut' ) );
797
798 // Too old
799 $request = new \FauxRequest();
800 $provider->setLoggedOutCookie( $t2, $request );
801 $this->assertSame( null, $request->response()->getCookie( 'xLoggedOut' ) );
802
803 // Don't reset if it's already set
804 $request = new \FauxRequest();
805 $request->setCookies( [
806 'xLoggedOut' => $t1,
807 ], '' );
808 $provider->setLoggedOutCookie( $t1, $request );
809 $this->assertSame( null, $request->response()->getCookie( 'xLoggedOut' ) );
810 }
811
816 public function onUserSetCookies( $user, &$sessionData, &$cookies ) {
817 }
818
819 public function testGetCookie() {
820 $provider = new CookieSessionProvider( [
821 'priority' => 1,
822 'sessionName' => 'MySessionName',
823 'cookieOptions' => [ 'prefix' => 'x' ],
824 ] );
825 $provider->setLogger( new \Psr\Log\NullLogger() );
826 $provider->setConfig( $this->getConfig() );
827 $provider->setManager( SessionManager::singleton() );
828 $provider = TestingAccessWrapper::newFromObject( $provider );
829
830 $request = new \FauxRequest();
831 $request->setCookies( [
832 'xFoo' => 'foo!',
833 'xBar' => 'deleted',
834 ], '' );
835 $this->assertSame( 'foo!', $provider->getCookie( $request, 'Foo', 'x' ) );
836 $this->assertNull( $provider->getCookie( $request, 'Bar', 'x' ) );
837 $this->assertNull( $provider->getCookie( $request, 'Baz', 'x' ) );
838 }
839
840 public function testGetRememberUserDuration() {
841 $config = $this->getConfig();
842 $provider = new CookieSessionProvider( [ 'priority' => 10 ] );
843 $provider->setLogger( new \Psr\Log\NullLogger() );
844 $provider->setConfig( $config );
845 $provider->setManager( SessionManager::singleton() );
846
847 $this->assertSame( 200, $provider->getRememberUserDuration() );
848
849 $config->set( 'ExtendedLoginCookieExpiration', null );
850
851 $this->assertSame( 100, $provider->getRememberUserDuration() );
852
853 $config->set( 'ExtendedLoginCookieExpiration', 0 );
854
855 $this->assertSame( null, $provider->getRememberUserDuration() );
856 }
857
859 $config = $this->getConfig();
860 $provider = TestingAccessWrapper::newFromObject( new CookieSessionProvider( [
861 'priority' => 10
862 ] ) );
863 $provider->setLogger( new \Psr\Log\NullLogger() );
864 $provider->setConfig( $config );
865 $provider->setManager( SessionManager::singleton() );
866
867 // First cookie is an extended cookie, remember me true
868 $this->assertSame( 200, $provider->getLoginCookieExpiration( 'Token', true ) );
869 $this->assertSame( 100, $provider->getLoginCookieExpiration( 'User', true ) );
870
871 // First cookie is an extended cookie, remember me false
872 $this->assertSame( 100, $provider->getLoginCookieExpiration( 'UserID', false ) );
873 $this->assertSame( 100, $provider->getLoginCookieExpiration( 'User', false ) );
874
875 $config->set( 'ExtendedLoginCookieExpiration', null );
876
877 $this->assertSame( 100, $provider->getLoginCookieExpiration( 'Token', true ) );
878 $this->assertSame( 100, $provider->getLoginCookieExpiration( 'User', true ) );
879
880 $this->assertSame( 100, $provider->getLoginCookieExpiration( 'Token', false ) );
881 $this->assertSame( 100, $provider->getLoginCookieExpiration( 'User', false ) );
882 }
883}
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition COPYING.txt:326
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
Session Database MediaWiki\Session\CookieSessionProvider.
onUserSetCookies( $user, &$sessionData, &$cookies)
To be mocked for hooks, since PHPUnit can't otherwise mock methods that take references.
static provideForceHTTPS()
Provider for testing both values of $wgForceHTTPS.
testCookieData( $secure, $remember, $forceHTTPS)
provideCookieData
A CookieSessionProvider persists sessions using cookies.
This is the actual workhorse for Session.
Value object holding the session ID in a manner that can be globally updated.
Definition SessionId.php:38
Value object returned by SessionProvider.
const MIN_PRIORITY
Minimum allowed priority.
const MAX_PRIORITY
Maximum allowed priority.
This serves as the entry point to the MediaWiki session handling system.
static singleton()
Get the global SessionManager.
BagOStuff with utility functions for MediaWiki\\Session\\* testing.
A logger that may be configured to either buffer logs or to print them to the output where PHPUnit wi...
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:53
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2806
see documentation in includes Linker php for Linker::makeImageLink & $time
Definition hooks.txt:1795
This code would result in ircNotify being run twice when an article is and once for brion Hooks can return three possible true was required This is the default since MediaWiki *some string
Definition hooks.txt:181
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:2006
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
$params