7 use Wikimedia\TestingAccessWrapper;
18 $config->set(
'CookiePrefix',
'wgCookiePrefix' );
21 'sessionCookieName' =>
$name,
22 'sessionCookieOptions' => [],
24 if ( $prefix !==
null ) {
25 $params[
'sessionCookieOptions'][
'prefix'] = $prefix;
29 ->setConstructorArgs( [
$params ] )
30 ->getMockForAbstractClass();
32 $provider->setConfig(
$config );
40 ->getMockForAbstractClass();
41 $priv = TestingAccessWrapper::newFromObject( $provider );
42 $this->assertNull( $priv->sessionCookieName );
43 $this->assertSame( [], $priv->sessionCookieOptions );
46 ->setConstructorArgs( [ [
47 'sessionCookieName' =>
'Foo',
48 'sessionCookieOptions' => [
'Bar' ],
50 ->getMockForAbstractClass();
51 $priv = TestingAccessWrapper::newFromObject( $provider );
52 $this->assertSame(
'Foo', $priv->sessionCookieName );
53 $this->assertSame( [
'Bar' ], $priv->sessionCookieOptions );
57 ->setConstructorArgs( [ [
58 'sessionCookieName' =>
false,
60 ->getMockForAbstractClass();
61 $this->fail(
'Expected exception not thrown' );
62 }
catch ( \InvalidArgumentException $ex ) {
64 'sessionCookieName must be a string',
71 ->setConstructorArgs( [ [
72 'sessionCookieOptions' =>
'x',
74 ->getMockForAbstractClass();
75 $this->fail(
'Expected exception not thrown' );
76 }
catch ( \InvalidArgumentException $ex ) {
78 'sessionCookieOptions must be an array',
86 $this->assertFalse( $provider->persistsSessionID() );
87 $this->assertFalse( $provider->canChangeUser() );
90 $this->assertTrue( $provider->persistsSessionID() );
91 $this->assertFalse( $provider->canChangeUser() );
93 $msg = $provider->whyNoSession();
94 $this->assertInstanceOf(
'Message', $msg );
95 $this->assertSame(
'sessionprovider-nocookies', $msg->getKey() );
100 $this->assertSame( [], $provider->getVaryCookies() );
103 $this->assertSame( [
'wgCookiePrefixFoo' ], $provider->getVaryCookies() );
106 $this->assertSame( [
'BarFoo' ], $provider->getVaryCookies() );
109 $this->assertSame( [
'Foo' ], $provider->getVaryCookies() );
113 $this->
setMwGlobals(
'wgCookiePrefix',
'wgCookiePrefix' );
116 '' =>
'empty---------------------------',
117 'Foo' =>
'foo-----------------------------',
118 'wgCookiePrefixFoo' =>
'wgfoo---------------------------',
119 'BarFoo' =>
'foobar--------------------------',
123 $provider = TestingAccessWrapper::newFromObject( $this->
getProvider(
null ) );
125 $provider->getSessionIdFromCookie(
$request );
126 $this->fail(
'Expected exception not thrown' );
127 }
catch ( \BadMethodCallException $ex ) {
129 'MediaWiki\\Session\\ImmutableSessionProviderWithCookie::getSessionIdFromCookie ' .
130 'may not be called when $this->sessionCookieName === null',
135 $provider = TestingAccessWrapper::newFromObject( $this->
getProvider(
'Foo' ) );
137 'wgfoo---------------------------',
138 $provider->getSessionIdFromCookie(
$request )
141 $provider = TestingAccessWrapper::newFromObject( $this->
getProvider(
'Foo',
'Bar' ) );
143 'foobar--------------------------',
144 $provider->getSessionIdFromCookie(
$request )
147 $provider = TestingAccessWrapper::newFromObject( $this->
getProvider(
'Foo',
'' ) );
149 'foo-----------------------------',
150 $provider->getSessionIdFromCookie(
$request )
153 $provider = TestingAccessWrapper::newFromObject( $this->
getProvider(
'bad',
'' ) );
154 $this->assertSame(
null, $provider->getSessionIdFromCookie(
$request ) );
156 $provider = TestingAccessWrapper::newFromObject( $this->
getProvider(
'none',
'' ) );
157 $this->assertSame(
null, $provider->getSessionIdFromCookie(
$request ) );
161 $sentResponse = $this->getMockBuilder(
'FauxResponse' )
162 ->setMethods( [
'headersSent',
'setCookie',
'header' ] )
164 $sentResponse->expects( $this->
any() )->method(
'headersSent' )
165 ->will( $this->returnValue(
true ) );
166 $sentResponse->expects( $this->never() )->method(
'setCookie' );
167 $sentResponse->expects( $this->never() )->method(
'header' );
169 $sentRequest = $this->getMockBuilder(
'FauxRequest' )
170 ->setMethods( [
'response' ] )->getMock();
171 $sentRequest->expects( $this->
any() )->method(
'response' )
172 ->will( $this->returnValue( $sentResponse ) );
183 'wgCookieExpiration' => 100,
184 'wgSecureLogin' =>
false,
188 $provider->setLogger(
new \Psr\Log\NullLogger() );
189 $priv = TestingAccessWrapper::newFromObject( $provider );
190 $priv->sessionCookieOptions = [
192 'path' =>
'CookiePath',
193 'domain' =>
'CookieDomain',
198 $sessionId =
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
200 $this->assertFalse(
$user->requiresHTTPS(),
'sanity check' );
205 'provider' => $provider,
212 new \Psr\Log\NullLogger(),
215 TestingAccessWrapper::newFromObject( $backend )->usePhpSessionHandling =
false;
216 $backend->setRememberUser( $remember );
217 $backend->setForceHTTPS( $secure );
220 $priv->sessionCookieName =
null;
222 $provider->persistSession( $backend,
$request );
223 $this->assertSame( [],
$request->response()->getCookies() );
226 $priv->sessionCookieName =
'session';
229 $provider->persistSession( $backend,
$request );
231 $cookie =
$request->response()->getCookieData(
'xsession' );
232 $this->assertInternalType(
'array', $cookie );
233 if ( isset( $cookie[
'expire'] ) && $cookie[
'expire'] > 0 ) {
235 $cookie[
'expire'] = round( $cookie[
'expire'] -
$time, -2 );
237 $this->assertEquals( [
238 'value' => $sessionId,
240 'path' =>
'CookiePath',
241 'domain' =>
'CookieDomain',
247 $cookie =
$request->response()->getCookieData(
'forceHTTPS' );
249 $this->assertInternalType(
'array', $cookie );
250 if ( isset( $cookie[
'expire'] ) && $cookie[
'expire'] > 0 ) {
252 $cookie[
'expire'] = round( $cookie[
'expire'] -
$time, -2 );
254 $this->assertEquals( [
257 'path' =>
'CookiePath',
258 'domain' =>
'CookieDomain',
264 $this->assertNull( $cookie );
269 $provider->persistSession( $backend,
$request );
270 $this->assertSame( [],
$request->response()->getCookies() );
284 $provider->setLogger(
new \Psr\Log\NullLogger() );
285 $priv = TestingAccessWrapper::newFromObject( $provider );
288 $priv->sessionCookieName =
null;
290 $provider->unpersistSession(
$request );
291 $this->assertSame(
null,
$request->response()->getCookie(
'session',
'' ) );
294 $priv->sessionCookieName =
'session';
296 $provider->unpersistSession(
$request );
297 $this->assertSame(
'',
$request->response()->getCookie(
'session',
'' ) );
301 $provider->unpersistSession(
$request );
302 $this->assertSame(
null,
$request->response()->getCookie(
'session',
'' ) );