3 namespace MediaWiki\Session
;
12 * @covers MediaWiki\Session\CookieSessionProvider
14 class CookieSessionProviderTest
extends MediaWikiTestCase
{
16 private function getConfig() {
17 return new \
HashConfig( [
18 'CookiePrefix' => 'CookiePrefix',
19 'CookiePath' => 'CookiePath',
20 'CookieDomain' => 'CookieDomain',
21 'CookieSecure' => true,
22 'CookieHttpOnly' => true,
23 'SessionName' => false,
24 'CookieExpiration' => 100,
25 'ExtendedLoginCookieExpiration' => 200,
29 public function testConstructor() {
31 new CookieSessionProvider();
32 $this->fail( 'Expected exception not thrown' );
33 } catch ( \InvalidArgumentException
$ex ) {
35 'MediaWiki\\Session\\CookieSessionProvider::__construct: priority must be specified',
41 new CookieSessionProvider( [ 'priority' => 'foo' ] );
42 $this->fail( 'Expected exception not thrown' );
43 } catch ( \InvalidArgumentException
$ex ) {
45 'MediaWiki\\Session\\CookieSessionProvider::__construct: Invalid priority',
50 new CookieSessionProvider( [ 'priority' => SessionInfo
::MIN_PRIORITY
- 1 ] );
51 $this->fail( 'Expected exception not thrown' );
52 } catch ( \InvalidArgumentException
$ex ) {
54 'MediaWiki\\Session\\CookieSessionProvider::__construct: Invalid priority',
59 new CookieSessionProvider( [ 'priority' => SessionInfo
::MAX_PRIORITY +
1 ] );
60 $this->fail( 'Expected exception not thrown' );
61 } catch ( \InvalidArgumentException
$ex ) {
63 'MediaWiki\\Session\\CookieSessionProvider::__construct: Invalid priority',
69 new CookieSessionProvider( [ 'priority' => 1, 'cookieOptions' => null ] );
70 $this->fail( 'Expected exception not thrown' );
71 } catch ( \InvalidArgumentException
$ex ) {
73 'MediaWiki\\Session\\CookieSessionProvider::__construct: cookieOptions must be an array',
78 $config = $this->getConfig();
79 $p = \TestingAccessWrapper
::newFromObject(
80 new CookieSessionProvider( [ 'priority' => 1 ] )
82 $p->setLogger( new \
TestLogger() );
83 $p->setConfig( $config );
84 $this->assertEquals( 1, $p->priority
);
85 $this->assertEquals( [
86 'callUserSetCookiesHook' => false,
87 'sessionName' => 'CookiePrefix_session',
89 $this->assertEquals( [
90 'prefix' => 'CookiePrefix',
91 'path' => 'CookiePath',
92 'domain' => 'CookieDomain',
95 ], $p->cookieOptions
);
97 $config->set( 'SessionName', 'SessionName' );
98 $p = \TestingAccessWrapper
::newFromObject(
99 new CookieSessionProvider( [ 'priority' => 3 ] )
101 $p->setLogger( new \
TestLogger() );
102 $p->setConfig( $config );
103 $this->assertEquals( 3, $p->priority
);
104 $this->assertEquals( [
105 'callUserSetCookiesHook' => false,
106 'sessionName' => 'SessionName',
108 $this->assertEquals( [
109 'prefix' => 'CookiePrefix',
110 'path' => 'CookiePath',
111 'domain' => 'CookieDomain',
114 ], $p->cookieOptions
);
116 $p = \TestingAccessWrapper
::newFromObject( new CookieSessionProvider( [
118 'callUserSetCookiesHook' => true,
120 'prefix' => 'XPrefix',
122 'domain' => 'XDomain',
123 'secure' => 'XSecure',
124 'httpOnly' => 'XHttpOnly',
126 'sessionName' => 'XSession',
128 $p->setLogger( new \
TestLogger() );
129 $p->setConfig( $config );
130 $this->assertEquals( 10, $p->priority
);
131 $this->assertEquals( [
132 'callUserSetCookiesHook' => true,
133 'sessionName' => 'XSession',
135 $this->assertEquals( [
136 'prefix' => 'XPrefix',
138 'domain' => 'XDomain',
139 'secure' => 'XSecure',
140 'httpOnly' => 'XHttpOnly',
141 ], $p->cookieOptions
);
144 public function testBasics() {
145 $provider = new CookieSessionProvider( [ 'priority' => 10 ] );
147 $this->assertTrue( $provider->persistsSessionId() );
148 $this->assertTrue( $provider->canChangeUser() );
150 $extendedCookies = [ 'UserID', 'UserName', 'Token' ];
154 \TestingAccessWrapper
::newFromObject( $provider )->getExtendedLoginCookies(),
155 'List of extended cookies (subclasses can add values, but we\'re calling the core one here)'
158 $msg = $provider->whyNoSession();
159 $this->assertInstanceOf( 'Message', $msg );
160 $this->assertSame( 'sessionprovider-nocookies', $msg->getKey() );
163 public function testProvideSessionInfo() {
166 'sessionName' => 'session',
167 'cookieOptions' => [ 'prefix' => 'x' ],
169 $provider = new CookieSessionProvider( $params );
170 $logger = new \
TestLogger( true );
171 $provider->setLogger( $logger );
172 $provider->setConfig( $this->getConfig() );
173 $provider->setManager( new SessionManager() );
175 $user = static::getTestSysop()->getUser();
176 $id = $user->getId();
177 $name = $user->getName();
178 $token = $user->getToken( true );
180 $sessionId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
183 $request = new \
FauxRequest();
184 $info = $provider->provideSessionInfo( $request );
185 $this->assertNull( $info );
186 $this->assertSame( [], $logger->getBuffer() );
187 $logger->clearBuffer();
190 $request = new \
FauxRequest();
191 $request->setCookies( [
192 'session' => $sessionId,
194 $info = $provider->provideSessionInfo( $request );
195 $this->assertNotNull( $info );
196 $this->assertSame( $params['priority'], $info->getPriority() );
197 $this->assertSame( $sessionId, $info->getId() );
198 $this->assertNotNull( $info->getUserInfo() );
199 $this->assertSame( 0, $info->getUserInfo()->getId() );
200 $this->assertNull( $info->getUserInfo()->getName() );
201 $this->assertFalse( $info->forceHTTPS() );
205 'Session "{session}" requested without UserID cookie',
207 ], $logger->getBuffer() );
208 $logger->clearBuffer();
210 // User, no session key
211 $request = new \
FauxRequest();
212 $request->setCookies( [
216 $info = $provider->provideSessionInfo( $request );
217 $this->assertNotNull( $info );
218 $this->assertSame( $params['priority'], $info->getPriority() );
219 $this->assertNotSame( $sessionId, $info->getId() );
220 $this->assertNotNull( $info->getUserInfo() );
221 $this->assertSame( $id, $info->getUserInfo()->getId() );
222 $this->assertSame( $name, $info->getUserInfo()->getName() );
223 $this->assertFalse( $info->forceHTTPS() );
224 $this->assertSame( [], $logger->getBuffer() );
225 $logger->clearBuffer();
227 // User and session key
228 $request = new \
FauxRequest();
229 $request->setCookies( [
230 'session' => $sessionId,
234 $info = $provider->provideSessionInfo( $request );
235 $this->assertNotNull( $info );
236 $this->assertSame( $params['priority'], $info->getPriority() );
237 $this->assertSame( $sessionId, $info->getId() );
238 $this->assertNotNull( $info->getUserInfo() );
239 $this->assertSame( $id, $info->getUserInfo()->getId() );
240 $this->assertSame( $name, $info->getUserInfo()->getName() );
241 $this->assertFalse( $info->forceHTTPS() );
242 $this->assertSame( [], $logger->getBuffer() );
243 $logger->clearBuffer();
245 // User with bad token
246 $request = new \
FauxRequest();
247 $request->setCookies( [
248 'session' => $sessionId,
250 'xToken' => 'BADTOKEN',
252 $info = $provider->provideSessionInfo( $request );
253 $this->assertNull( $info );
257 'Session "{session}" requested with invalid Token cookie.'
259 ], $logger->getBuffer() );
260 $logger->clearBuffer();
262 // User id with no token
263 $request = new \
FauxRequest();
264 $request->setCookies( [
265 'session' => $sessionId,
268 $info = $provider->provideSessionInfo( $request );
269 $this->assertNotNull( $info );
270 $this->assertSame( $params['priority'], $info->getPriority() );
271 $this->assertSame( $sessionId, $info->getId() );
272 $this->assertNotNull( $info->getUserInfo() );
273 $this->assertFalse( $info->getUserInfo()->isVerified() );
274 $this->assertSame( $id, $info->getUserInfo()->getId() );
275 $this->assertSame( $name, $info->getUserInfo()->getName() );
276 $this->assertFalse( $info->forceHTTPS() );
277 $this->assertSame( [], $logger->getBuffer() );
278 $logger->clearBuffer();
280 $request = new \
FauxRequest();
281 $request->setCookies( [
284 $info = $provider->provideSessionInfo( $request );
285 $this->assertNull( $info );
286 $this->assertSame( [], $logger->getBuffer() );
287 $logger->clearBuffer();
289 // User and session key, with forceHTTPS flag
290 $request = new \
FauxRequest();
291 $request->setCookies( [
292 'session' => $sessionId,
295 'forceHTTPS' => true,
297 $info = $provider->provideSessionInfo( $request );
298 $this->assertNotNull( $info );
299 $this->assertSame( $params['priority'], $info->getPriority() );
300 $this->assertSame( $sessionId, $info->getId() );
301 $this->assertNotNull( $info->getUserInfo() );
302 $this->assertSame( $id, $info->getUserInfo()->getId() );
303 $this->assertSame( $name, $info->getUserInfo()->getName() );
304 $this->assertTrue( $info->forceHTTPS() );
305 $this->assertSame( [], $logger->getBuffer() );
306 $logger->clearBuffer();
309 $request = new \
FauxRequest();
310 $request->setCookies( [
311 'session' => $sessionId,
314 $info = $provider->provideSessionInfo( $request );
315 $this->assertNull( $info );
316 $this->assertSame( [], $logger->getBuffer() );
317 $logger->clearBuffer();
319 // User id with matching name
320 $request = new \
FauxRequest();
321 $request->setCookies( [
322 'session' => $sessionId,
324 'xUserName' => $name,
326 $info = $provider->provideSessionInfo( $request );
327 $this->assertNotNull( $info );
328 $this->assertSame( $params['priority'], $info->getPriority() );
329 $this->assertSame( $sessionId, $info->getId() );
330 $this->assertNotNull( $info->getUserInfo() );
331 $this->assertFalse( $info->getUserInfo()->isVerified() );
332 $this->assertSame( $id, $info->getUserInfo()->getId() );
333 $this->assertSame( $name, $info->getUserInfo()->getName() );
334 $this->assertFalse( $info->forceHTTPS() );
335 $this->assertSame( [], $logger->getBuffer() );
336 $logger->clearBuffer();
338 // User id with wrong name
339 $request = new \
FauxRequest();
340 $request->setCookies( [
341 'session' => $sessionId,
343 'xUserName' => 'Wrong',
345 $info = $provider->provideSessionInfo( $request );
346 $this->assertNull( $info );
350 'Session "{session}" requested with mismatched UserID and UserName cookies.',
352 ], $logger->getBuffer() );
353 $logger->clearBuffer();
356 public function testGetVaryCookies() {
357 $provider = new CookieSessionProvider( [
359 'sessionName' => 'MySessionName',
360 'cookieOptions' => [ 'prefix' => 'MyCookiePrefix' ],
362 $this->assertArrayEquals( [
363 'MyCookiePrefixToken',
364 'MyCookiePrefixLoggedOut',
367 ], $provider->getVaryCookies() );
370 public function testSuggestLoginUsername() {
371 $provider = new CookieSessionProvider( [
373 'sessionName' => 'MySessionName',
374 'cookieOptions' => [ 'prefix' => 'x' ],
377 $request = new \
FauxRequest();
378 $this->assertEquals( null, $provider->suggestLoginUsername( $request ) );
380 $request->setCookies( [
381 'xUserName' => 'Example',
383 $this->assertEquals( 'Example', $provider->suggestLoginUsername( $request ) );
386 public function testPersistSession() {
387 $provider = new CookieSessionProvider( [
389 'sessionName' => 'MySessionName',
390 'callUserSetCookiesHook' => false,
391 'cookieOptions' => [ 'prefix' => 'x' ],
393 $config = $this->getConfig();
394 $provider->setLogger( new \
TestLogger() );
395 $provider->setConfig( $config );
396 $provider->setManager( SessionManager
::singleton() );
398 $sessionId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
399 $store = new TestBagOStuff();
400 $user = static::getTestSysop()->getUser();
403 $backend = new SessionBackend(
404 new SessionId( $sessionId ),
405 new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
406 'provider' => $provider,
412 new \Psr\Log\
NullLogger(),
415 \TestingAccessWrapper
::newFromObject( $backend )->usePhpSessionHandling
= false;
417 $mock = $this->getMock( 'stdClass', [ 'onUserSetCookies' ] );
418 $mock->expects( $this->never() )->method( 'onUserSetCookies' );
419 $this->mergeMwGlobalArrayValue( 'wgHooks', [ 'UserSetCookies' => [ $mock ] ] );
422 $backend->setUser( $anon );
423 $backend->setRememberUser( true );
424 $backend->setForceHTTPS( false );
425 $request = new \
FauxRequest();
426 $provider->persistSession( $backend, $request );
427 $this->assertSame( $sessionId, $request->response()->getCookie( 'MySessionName' ) );
428 $this->assertSame( '', $request->response()->getCookie( 'xUserID' ) );
429 $this->assertSame( null, $request->response()->getCookie( 'xUserName' ) );
430 $this->assertSame( '', $request->response()->getCookie( 'xToken' ) );
431 $this->assertSame( '', $request->response()->getCookie( 'forceHTTPS' ) );
432 $this->assertSame( [], $backend->getData() );
434 // Logged-in user, no remember
435 $backend->setUser( $user );
436 $backend->setRememberUser( false );
437 $backend->setForceHTTPS( false );
438 $request = new \
FauxRequest();
439 $provider->persistSession( $backend, $request );
440 $this->assertSame( $sessionId, $request->response()->getCookie( 'MySessionName' ) );
441 $this->assertSame( (string)$user->getId(), $request->response()->getCookie( 'xUserID' ) );
442 $this->assertSame( $user->getName(), $request->response()->getCookie( 'xUserName' ) );
443 $this->assertSame( '', $request->response()->getCookie( 'xToken' ) );
444 $this->assertSame( '', $request->response()->getCookie( 'forceHTTPS' ) );
445 $this->assertSame( [], $backend->getData() );
447 // Logged-in user, remember
448 $backend->setUser( $user );
449 $backend->setRememberUser( true );
450 $backend->setForceHTTPS( true );
451 $request = new \
FauxRequest();
453 $provider->persistSession( $backend, $request );
454 $this->assertSame( $sessionId, $request->response()->getCookie( 'MySessionName' ) );
455 $this->assertSame( (string)$user->getId(), $request->response()->getCookie( 'xUserID' ) );
456 $this->assertSame( $user->getName(), $request->response()->getCookie( 'xUserName' ) );
457 $this->assertSame( $user->getToken(), $request->response()->getCookie( 'xToken' ) );
458 $this->assertSame( 'true', $request->response()->getCookie( 'forceHTTPS' ) );
459 $this->assertSame( [], $backend->getData() );
463 * @dataProvider provideCookieData
464 * @param bool $secure
465 * @param bool $remember
467 public function testCookieData( $secure, $remember ) {
468 $this->setMwGlobals( [
469 'wgSecureLogin' => false,
472 $provider = new CookieSessionProvider( [
474 'sessionName' => 'MySessionName',
475 'callUserSetCookiesHook' => false,
476 'cookieOptions' => [ 'prefix' => 'x' ],
478 $config = $this->getConfig();
479 $config->set( 'CookieSecure', $secure );
480 $provider->setLogger( new \
TestLogger() );
481 $provider->setConfig( $config );
482 $provider->setManager( SessionManager
::singleton() );
484 $sessionId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
485 $user = static::getTestSysop()->getUser();
486 $this->assertFalse( $user->requiresHTTPS(), 'sanity check' );
488 $backend = new SessionBackend(
489 new SessionId( $sessionId ),
490 new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
491 'provider' => $provider,
497 new \Psr\Log\
NullLogger(),
500 \TestingAccessWrapper
::newFromObject( $backend )->usePhpSessionHandling
= false;
501 $backend->setUser( $user );
502 $backend->setRememberUser( $remember );
503 $backend->setForceHTTPS( $secure );
504 $request = new \
FauxRequest();
506 $provider->persistSession( $backend, $request );
509 'expire' => (int)100,
510 'path' => $config->get( 'CookiePath' ),
511 'domain' => $config->get( 'CookieDomain' ),
513 'httpOnly' => $config->get( 'CookieHttpOnly' ),
517 $normalExpiry = $config->get( 'CookieExpiration' );
518 $extendedExpiry = $config->get( 'ExtendedLoginCookieExpiration' );
519 $extendedExpiry = (int)( $extendedExpiry === null ?
0 : $extendedExpiry );
522 'value' => (string)$sessionId,
526 'value' => (string)$user->getId(),
527 'expire' => $remember ?
$extendedExpiry : $normalExpiry,
530 'value' => $user->getName(),
531 'expire' => $remember ?
$extendedExpiry : $normalExpiry
534 'value' => $remember ?
$user->getToken() : '',
535 'expire' => $remember ?
$extendedExpiry : -31536000,
538 'value' => $secure ?
'true' : '',
540 'expire' => $secure ?
$remember ?
$defaults['expire'] : 0 : -31536000,
543 foreach ( $expect as $key => $value ) {
544 $actual = $request->response()->getCookieData( $key );
545 if ( $actual && $actual['expire'] > 0 ) {
546 // Round expiry so we don't randomly fail if the seconds ticked during the test.
547 $actual['expire'] = round( $actual['expire'] - $time, -2 );
549 $this->assertEquals( $value, $actual, "Cookie $key" );
553 public static function provideCookieData() {
562 protected function getSentRequest() {
563 $sentResponse = $this->getMock( 'FauxResponse', [ 'headersSent', 'setCookie', 'header' ] );
564 $sentResponse->expects( $this->any() )->method( 'headersSent' )
565 ->will( $this->returnValue( true ) );
566 $sentResponse->expects( $this->never() )->method( 'setCookie' );
567 $sentResponse->expects( $this->never() )->method( 'header' );
569 $sentRequest = $this->getMock( 'FauxRequest', [ 'response' ] );
570 $sentRequest->expects( $this->any() )->method( 'response' )
571 ->will( $this->returnValue( $sentResponse ) );
575 public function testPersistSessionWithHook() {
576 $provider = new CookieSessionProvider( [
578 'sessionName' => 'MySessionName',
579 'callUserSetCookiesHook' => true,
580 'cookieOptions' => [ 'prefix' => 'x' ],
582 $provider->setLogger( new \Psr\Log\
NullLogger() );
583 $provider->setConfig( $this->getConfig() );
584 $provider->setManager( SessionManager
::singleton() );
586 $sessionId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
587 $store = new TestBagOStuff();
588 $user = static::getTestSysop()->getUser();
591 $backend = new SessionBackend(
592 new SessionId( $sessionId ),
593 new SessionInfo( SessionInfo
::MIN_PRIORITY
, [
594 'provider' => $provider,
600 new \Psr\Log\
NullLogger(),
603 \TestingAccessWrapper
::newFromObject( $backend )->usePhpSessionHandling
= false;
606 $mock = $this->getMock( 'stdClass', [ 'onUserSetCookies' ] );
607 $mock->expects( $this->never() )->method( 'onUserSetCookies' );
608 $this->mergeMwGlobalArrayValue( 'wgHooks', [ 'UserSetCookies' => [ $mock ] ] );
609 $backend->setUser( $anon );
610 $backend->setRememberUser( true );
611 $backend->setForceHTTPS( false );
612 $request = new \
FauxRequest();
613 $provider->persistSession( $backend, $request );
614 $this->assertSame( $sessionId, $request->response()->getCookie( 'MySessionName' ) );
615 $this->assertSame( '', $request->response()->getCookie( 'xUserID' ) );
616 $this->assertSame( null, $request->response()->getCookie( 'xUserName' ) );
617 $this->assertSame( '', $request->response()->getCookie( 'xToken' ) );
618 $this->assertSame( '', $request->response()->getCookie( 'forceHTTPS' ) );
619 $this->assertSame( [], $backend->getData() );
621 $provider->persistSession( $backend, $this->getSentRequest() );
623 // Logged-in user, no remember
624 $mock = $this->getMock( __CLASS__
, [ 'onUserSetCookies' ] );
625 $mock->expects( $this->once() )->method( 'onUserSetCookies' )
626 ->will( $this->returnCallback( function ( $u, &$sessionData, &$cookies ) use ( $user ) {
627 $this->assertSame( $user, $u );
628 $this->assertEquals( [
629 'wsUserID' => $user->getId(),
630 'wsUserName' => $user->getName(),
631 'wsToken' => $user->getToken(),
633 $this->assertEquals( [
634 'UserID' => $user->getId(),
635 'UserName' => $user->getName(),
639 $sessionData['foo'] = 'foo!';
640 $cookies['bar'] = 'bar!';
643 $this->mergeMwGlobalArrayValue( 'wgHooks', [ 'UserSetCookies' => [ $mock ] ] );
644 $backend->setUser( $user );
645 $backend->setRememberUser( false );
646 $backend->setForceHTTPS( false );
647 $backend->setLoggedOutTimestamp( $loggedOut = time() );
648 $request = new \
FauxRequest();
649 $provider->persistSession( $backend, $request );
650 $this->assertSame( $sessionId, $request->response()->getCookie( 'MySessionName' ) );
651 $this->assertSame( (string)$user->getId(), $request->response()->getCookie( 'xUserID' ) );
652 $this->assertSame( $user->getName(), $request->response()->getCookie( 'xUserName' ) );
653 $this->assertSame( '', $request->response()->getCookie( 'xToken' ) );
654 $this->assertSame( '', $request->response()->getCookie( 'forceHTTPS' ) );
655 $this->assertSame( 'bar!', $request->response()->getCookie( 'xbar' ) );
656 $this->assertSame( (string)$loggedOut, $request->response()->getCookie( 'xLoggedOut' ) );
657 $this->assertEquals( [
658 'wsUserID' => $user->getId(),
659 'wsUserName' => $user->getName(),
660 'wsToken' => $user->getToken(),
662 ], $backend->getData() );
664 $provider->persistSession( $backend, $this->getSentRequest() );
666 // Logged-in user, remember
667 $mock = $this->getMock( __CLASS__
, [ 'onUserSetCookies' ] );
668 $mock->expects( $this->once() )->method( 'onUserSetCookies' )
669 ->will( $this->returnCallback( function ( $u, &$sessionData, &$cookies ) use ( $user ) {
670 $this->assertSame( $user, $u );
671 $this->assertEquals( [
672 'wsUserID' => $user->getId(),
673 'wsUserName' => $user->getName(),
674 'wsToken' => $user->getToken(),
676 $this->assertEquals( [
677 'UserID' => $user->getId(),
678 'UserName' => $user->getName(),
679 'Token' => $user->getToken(),
682 $sessionData['foo'] = 'foo 2!';
683 $cookies['bar'] = 'bar 2!';
686 $this->mergeMwGlobalArrayValue( 'wgHooks', [ 'UserSetCookies' => [ $mock ] ] );
687 $backend->setUser( $user );
688 $backend->setRememberUser( true );
689 $backend->setForceHTTPS( true );
690 $backend->setLoggedOutTimestamp( 0 );
691 $request = new \
FauxRequest();
692 $provider->persistSession( $backend, $request );
693 $this->assertSame( $sessionId, $request->response()->getCookie( 'MySessionName' ) );
694 $this->assertSame( (string)$user->getId(), $request->response()->getCookie( 'xUserID' ) );
695 $this->assertSame( $user->getName(), $request->response()->getCookie( 'xUserName' ) );
696 $this->assertSame( $user->getToken(), $request->response()->getCookie( 'xToken' ) );
697 $this->assertSame( 'true', $request->response()->getCookie( 'forceHTTPS' ) );
698 $this->assertSame( 'bar 2!', $request->response()->getCookie( 'xbar' ) );
699 $this->assertSame( null, $request->response()->getCookie( 'xLoggedOut' ) );
700 $this->assertEquals( [
701 'wsUserID' => $user->getId(),
702 'wsUserName' => $user->getName(),
703 'wsToken' => $user->getToken(),
705 ], $backend->getData() );
707 $provider->persistSession( $backend, $this->getSentRequest() );
710 public function testUnpersistSession() {
711 $provider = new CookieSessionProvider( [
713 'sessionName' => 'MySessionName',
714 'cookieOptions' => [ 'prefix' => 'x' ],
716 $provider->setLogger( new \Psr\Log\
NullLogger() );
717 $provider->setConfig( $this->getConfig() );
718 $provider->setManager( SessionManager
::singleton() );
720 $request = new \
FauxRequest();
721 $provider->unpersistSession( $request );
722 $this->assertSame( '', $request->response()->getCookie( 'MySessionName' ) );
723 $this->assertSame( '', $request->response()->getCookie( 'xUserID' ) );
724 $this->assertSame( null, $request->response()->getCookie( 'xUserName' ) );
725 $this->assertSame( '', $request->response()->getCookie( 'xToken' ) );
726 $this->assertSame( '', $request->response()->getCookie( 'forceHTTPS' ) );
728 $provider->unpersistSession( $this->getSentRequest() );
731 public function testSetLoggedOutCookie() {
732 $provider = \TestingAccessWrapper
::newFromObject( new CookieSessionProvider( [
734 'sessionName' => 'MySessionName',
735 'cookieOptions' => [ 'prefix' => 'x' ],
737 $provider->setLogger( new \Psr\Log\
NullLogger() );
738 $provider->setConfig( $this->getConfig() );
739 $provider->setManager( SessionManager
::singleton() );
742 $t2 = time() - 86400 * 2;
745 $request = new \
FauxRequest();
746 $provider->setLoggedOutCookie( $t1, $request );
747 $this->assertSame( (string)$t1, $request->response()->getCookie( 'xLoggedOut' ) );
750 $request = new \
FauxRequest();
751 $provider->setLoggedOutCookie( $t2, $request );
752 $this->assertSame( null, $request->response()->getCookie( 'xLoggedOut' ) );
754 // Don't reset if it's already set
755 $request = new \
FauxRequest();
756 $request->setCookies( [
759 $provider->setLoggedOutCookie( $t1, $request );
760 $this->assertSame( null, $request->response()->getCookie( 'xLoggedOut' ) );
764 * To be mocked for hooks, since PHPUnit can't otherwise mock methods that
767 public function onUserSetCookies( $user, &$sessionData, &$cookies ) {
770 public function testGetCookie() {
771 $provider = new CookieSessionProvider( [
773 'sessionName' => 'MySessionName',
774 'cookieOptions' => [ 'prefix' => 'x' ],
776 $provider->setLogger( new \Psr\Log\
NullLogger() );
777 $provider->setConfig( $this->getConfig() );
778 $provider->setManager( SessionManager
::singleton() );
779 $provider = \TestingAccessWrapper
::newFromObject( $provider );
781 $request = new \
FauxRequest();
782 $request->setCookies( [
786 $this->assertSame( 'foo!', $provider->getCookie( $request, 'Foo', 'x' ) );
787 $this->assertNull( $provider->getCookie( $request, 'Bar', 'x' ) );
788 $this->assertNull( $provider->getCookie( $request, 'Baz', 'x' ) );
791 public function testGetRememberUserDuration() {
792 $config = $this->getConfig();
793 $provider = new CookieSessionProvider( [ 'priority' => 10 ] );
794 $provider->setLogger( new \Psr\Log\
NullLogger() );
795 $provider->setConfig( $config );
796 $provider->setManager( SessionManager
::singleton() );
798 $this->assertSame( 200, $provider->getRememberUserDuration() );
800 $config->set( 'ExtendedLoginCookieExpiration', null );
802 $this->assertSame( 100, $provider->getRememberUserDuration() );
804 $config->set( 'ExtendedLoginCookieExpiration', 0 );
806 $this->assertSame( null, $provider->getRememberUserDuration() );
809 public function testGetLoginCookieExpiration() {
810 $config = $this->getConfig();
811 $provider = \TestingAccessWrapper
::newFromObject( new CookieSessionProvider( [
814 $provider->setLogger( new \Psr\Log\
NullLogger() );
815 $provider->setConfig( $config );
816 $provider->setManager( SessionManager
::singleton() );
818 // First cookie is an extended cookie, remember me true
819 $this->assertSame( 200, $provider->getLoginCookieExpiration( 'Token', true ) );
820 $this->assertSame( 100, $provider->getLoginCookieExpiration( 'User', true ) );
822 // First cookie is an extended cookie, remember me false
823 $this->assertSame( 100, $provider->getLoginCookieExpiration( 'UserID', false ) );
824 $this->assertSame( 100, $provider->getLoginCookieExpiration( 'User', false ) );
826 $config->set( 'ExtendedLoginCookieExpiration', null );
828 $this->assertSame( 100, $provider->getLoginCookieExpiration( 'Token', true ) );
829 $this->assertSame( 100, $provider->getLoginCookieExpiration( 'User', true ) );
831 $this->assertSame( 100, $provider->getLoginCookieExpiration( 'Token', false ) );
832 $this->assertSame( 100, $provider->getLoginCookieExpiration( 'User', false ) );