3 namespace MediaWiki\Session
;
10 * @covers MediaWiki\Session\PHPSessionHandler
12 class PHPSessionHandlerTest
extends MediaWikiTestCase
{
14 private function getResetter( &$rProp = null ) {
17 // Ignore "headers already sent" warnings during this test
18 set_error_handler( function ( $errno, $errstr ) use ( &$warnings ) {
19 if ( preg_match( '/headers already sent/', $errstr ) ) {
24 $reset[] = new \
ScopedCallback( 'restore_error_handler' );
26 $rProp = new \
ReflectionProperty( 'MediaWiki\\Session\\PHPSessionHandler', 'instance' );
27 $rProp->setAccessible( true );
28 if ( $rProp->getValue() ) {
29 $old = \TestingAccessWrapper
::newFromObject( $rProp->getValue() );
30 $oldManager = $old->manager
;
31 $oldStore = $old->store
;
32 $oldLogger = $old->logger
;
33 $reset[] = new \
ScopedCallback(
34 [ 'MediaWiki\\Session\\PHPSessionHandler', 'install' ],
35 [ $oldManager, $oldStore, $oldLogger ]
42 public function testEnableFlags() {
43 $handler = \TestingAccessWrapper
::newFromObject(
44 $this->getMockBuilder( 'MediaWiki\\Session\\PHPSessionHandler' )
46 ->disableOriginalConstructor()
50 $rProp = new \
ReflectionProperty( 'MediaWiki\\Session\\PHPSessionHandler', 'instance' );
51 $rProp->setAccessible( true );
52 $reset = new \
ScopedCallback( [ $rProp, 'setValue' ], [ $rProp->getValue() ] );
53 $rProp->setValue( $handler );
55 $handler->setEnableFlags( 'enable' );
56 $this->assertTrue( $handler->enable
);
57 $this->assertFalse( $handler->warn
);
58 $this->assertTrue( PHPSessionHandler
::isEnabled() );
60 $handler->setEnableFlags( 'warn' );
61 $this->assertTrue( $handler->enable
);
62 $this->assertTrue( $handler->warn
);
63 $this->assertTrue( PHPSessionHandler
::isEnabled() );
65 $handler->setEnableFlags( 'disable' );
66 $this->assertFalse( $handler->enable
);
67 $this->assertFalse( PHPSessionHandler
::isEnabled() );
69 $rProp->setValue( null );
70 $this->assertFalse( PHPSessionHandler
::isEnabled() );
73 public function testInstall() {
74 $reset = $this->getResetter( $rProp );
75 $rProp->setValue( null );
77 session_write_close();
78 ini_set( 'session.use_cookies', 1 );
79 ini_set( 'session.use_trans_sid', 1 );
81 $store = new TestBagOStuff();
82 $logger = new \
TestLogger();
83 $manager = new SessionManager( [
88 $this->assertFalse( PHPSessionHandler
::isInstalled() );
89 PHPSessionHandler
::install( $manager );
90 $this->assertTrue( PHPSessionHandler
::isInstalled() );
92 $this->assertFalse( wfIniGetBool( 'session.use_cookies' ) );
93 $this->assertFalse( wfIniGetBool( 'session.use_trans_sid' ) );
95 $this->assertNotNull( $rProp->getValue() );
96 $priv = \TestingAccessWrapper
::newFromObject( $rProp->getValue() );
97 $this->assertSame( $manager, $priv->manager
);
98 $this->assertSame( $store, $priv->store
);
99 $this->assertSame( $logger, $priv->logger
);
103 * @dataProvider provideHandlers
104 * @param string $handler php serialize_handler to use
106 public function testSessionHandling( $handler ) {
107 $this->hideDeprecated( '$_SESSION' );
108 $reset[] = $this->getResetter( $rProp );
110 $this->setMwGlobals( [
111 'wgSessionProviders' => [ [ 'class' => 'DummySessionProvider' ] ],
112 'wgObjectCacheSessionExpiry' => 2,
115 $store = new TestBagOStuff();
116 $logger = new \
TestLogger( true, function ( $m ) {
117 // Discard all log events starting with expected prefix
118 return preg_match( '/^SessionBackend "\{session\}" /', $m ) ?
null : $m;
120 $manager = new SessionManager( [
124 PHPSessionHandler
::install( $manager );
125 $wrap = \TestingAccessWrapper
::newFromObject( $rProp->getValue() );
126 $reset[] = new \
ScopedCallback(
127 [ $wrap, 'setEnableFlags' ],
128 [ $wrap->enable ?
$wrap->warn ?
'warn' : 'enable' : 'disable' ]
130 $wrap->setEnableFlags( 'warn' );
132 \MediaWiki\
suppressWarnings();
133 ini_set( 'session.serialize_handler', $handler );
134 \MediaWiki\restoreWarnings
();
135 if ( ini_get( 'session.serialize_handler' ) !== $handler ) {
136 $this->markTestSkipped( "Cannot set session.serialize_handler to \"$handler\"" );
139 // Session IDs for testing
140 $sessionA = str_repeat( 'a', 32 );
141 $sessionB = str_repeat( 'b', 32 );
142 $sessionC = str_repeat( 'c', 32 );
144 // Set up garbage data in the session
145 $_SESSION['AuthenticationSessionTest'] = 'bogus';
147 session_id( $sessionA );
149 $this->assertSame( [], $_SESSION );
150 $this->assertSame( $sessionA, session_id() );
152 // Set some data in the session so we can see if it works.
154 $_SESSION['AuthenticationSessionTest'] = $rand;
155 $expect = [ 'AuthenticationSessionTest' => $rand ];
156 session_write_close();
158 [ LogLevel
::WARNING
, 'Something wrote to $_SESSION!' ],
159 ], $logger->getBuffer() );
161 // Screw up $_SESSION so we can tell the difference between "this
162 // worked" and "this did nothing"
163 $_SESSION['AuthenticationSessionTest'] = 'bogus';
165 // Re-open the session and see that data was actually reloaded
167 $this->assertSame( $expect, $_SESSION );
169 // Make sure session_reset() works too.
170 if ( function_exists( 'session_reset' ) ) {
171 $_SESSION['AuthenticationSessionTest'] = 'bogus';
173 $this->assertSame( $expect, $_SESSION );
177 session_write_close();
178 ini_set( 'session.gc_divisor', 1 );
179 ini_set( 'session.gc_probability', 1 );
182 $this->assertSame( [], $_SESSION );
184 // Re-fill the session, then test that session_destroy() works.
185 $_SESSION['AuthenticationSessionTest'] = $rand;
186 session_write_close();
188 $this->assertSame( $expect, $_SESSION );
190 session_id( $sessionA );
192 $this->assertSame( [], $_SESSION );
193 session_write_close();
195 // Test that our session handler won't clone someone else's session
196 session_id( $sessionB );
198 $this->assertSame( $sessionB, session_id() );
199 $_SESSION['id'] = 'B';
200 session_write_close();
202 session_id( $sessionC );
204 $this->assertSame( [], $_SESSION );
205 $_SESSION['id'] = 'C';
206 session_write_close();
208 session_id( $sessionB );
210 $this->assertSame( [ 'id' => 'B' ], $_SESSION );
211 session_write_close();
213 session_id( $sessionC );
215 $this->assertSame( [ 'id' => 'C' ], $_SESSION );
218 session_id( $sessionB );
220 $this->assertSame( [ 'id' => 'B' ], $_SESSION );
222 // Test merging between Session and $_SESSION
223 session_write_close();
225 $session = $manager->getEmptySession();
226 $session->set( 'Unchanged', 'setup' );
227 $session->set( 'Unchanged, null', null );
228 $session->set( 'Changed in $_SESSION', 'setup' );
229 $session->set( 'Changed in Session', 'setup' );
230 $session->set( 'Changed in both', 'setup' );
231 $session->set( 'Deleted in Session', 'setup' );
232 $session->set( 'Deleted in $_SESSION', 'setup' );
233 $session->set( 'Deleted in both', 'setup' );
234 $session->set( 'Deleted in Session, changed in $_SESSION', 'setup' );
235 $session->set( 'Deleted in $_SESSION, changed in Session', 'setup' );
239 session_id( $session->getId() );
241 $session->set( 'Added in Session', 'Session' );
242 $session->set( 'Added in both', 'Session' );
243 $session->set( 'Changed in Session', 'Session' );
244 $session->set( 'Changed in both', 'Session' );
245 $session->set( 'Deleted in $_SESSION, changed in Session', 'Session' );
246 $session->remove( 'Deleted in Session' );
247 $session->remove( 'Deleted in both' );
248 $session->remove( 'Deleted in Session, changed in $_SESSION' );
250 $_SESSION['Added in $_SESSION'] = '$_SESSION';
251 $_SESSION['Added in both'] = '$_SESSION';
252 $_SESSION['Changed in $_SESSION'] = '$_SESSION';
253 $_SESSION['Changed in both'] = '$_SESSION';
254 $_SESSION['Deleted in Session, changed in $_SESSION'] = '$_SESSION';
255 unset( $_SESSION['Deleted in $_SESSION'] );
256 unset( $_SESSION['Deleted in both'] );
257 unset( $_SESSION['Deleted in $_SESSION, changed in Session'] );
258 session_write_close();
260 $this->assertEquals( [
261 'Added in Session' => 'Session',
262 'Added in $_SESSION' => '$_SESSION',
263 'Added in both' => 'Session',
264 'Unchanged' => 'setup',
265 'Unchanged, null' => null,
266 'Changed in Session' => 'Session',
267 'Changed in $_SESSION' => '$_SESSION',
268 'Changed in both' => 'Session',
269 'Deleted in Session, changed in $_SESSION' => '$_SESSION',
270 'Deleted in $_SESSION, changed in Session' => 'Session',
271 ], iterator_to_array( $session ) );
274 $session->set( 42, 'forty-two' );
275 $session->set( 'forty-two', 42 );
276 $session->set( 'wrong', 43 );
281 $this->assertArrayHasKey( 'forty-two', $_SESSION );
282 $this->assertSame( 42, $_SESSION['forty-two'] );
283 $this->assertArrayHasKey( 'wrong', $_SESSION );
284 unset( $_SESSION['wrong'] );
285 session_write_close();
287 $this->assertEquals( [
290 ], iterator_to_array( $session ) );
292 // Test that write doesn't break if the session is invalid
293 $session = $manager->getEmptySession();
295 session_id( $session->getId() );
297 $this->mergeMwGlobalArrayValue( 'wgHooks', [
298 'SessionCheckInfo' => [ function ( &$reason ) {
303 $this->assertNull( $manager->getSessionById( $session->getId(), true ), 'sanity check' );
304 session_write_close();
305 $this->mergeMwGlobalArrayValue( 'wgHooks', [
306 'SessionCheckInfo' => [],
308 $this->assertNotNull( $manager->getSessionById( $session->getId(), true ), 'sanity check' );
311 public static function provideHandlers() {
320 * @dataProvider provideDisabled
321 * @expectedException BadMethodCallException
322 * @expectedExceptionMessage Attempt to use PHP session management
324 public function testDisabled( $method, $args ) {
325 $rProp = new \
ReflectionProperty( 'MediaWiki\\Session\\PHPSessionHandler', 'instance' );
326 $rProp->setAccessible( true );
327 $handler = $this->getMockBuilder( 'MediaWiki\\Session\\PHPSessionHandler' )
329 ->disableOriginalConstructor()
331 \TestingAccessWrapper
::newFromObject( $handler )->setEnableFlags( 'disable' );
332 $oldValue = $rProp->getValue();
333 $rProp->setValue( $handler );
334 $reset = new \
ScopedCallback( [ $rProp, 'setValue' ], [ $oldValue ] );
336 call_user_func_array( [ $handler, $method ], $args );
339 public static function provideDisabled() {
341 [ 'open', [ '', '' ] ],
343 [ 'write', [ '', '' ] ],
344 [ 'destroy', [ '' ] ],
349 * @dataProvider provideWrongInstance
350 * @expectedException UnexpectedValueException
351 * @expectedExceptionMessageRegExp /: Wrong instance called!$/
353 public function testWrongInstance( $method, $args ) {
354 $handler = $this->getMockBuilder( 'MediaWiki\\Session\\PHPSessionHandler' )
356 ->disableOriginalConstructor()
358 \TestingAccessWrapper
::newFromObject( $handler )->setEnableFlags( 'enable' );
360 call_user_func_array( [ $handler, $method ], $args );
363 public static function provideWrongInstance() {
365 [ 'open', [ '', '' ] ],
368 [ 'write', [ '', '' ] ],
369 [ 'destroy', [ '' ] ],