18 set_error_handler(
function ( $errno, $errstr )
use ( &$warnings ) {
19 if ( preg_match(
'/headers already sent/', $errstr ) ) {
24 $reset[] = new \Wikimedia\ScopedCallback(
'restore_error_handler' );
26 $rProp = new \ReflectionProperty( PHPSessionHandler::class,
'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 \Wikimedia\ScopedCallback(
34 [ PHPSessionHandler::class,
'install' ],
35 [ $oldManager, $oldStore, $oldLogger ]
43 $handler = \TestingAccessWrapper::newFromObject(
44 $this->getMockBuilder( PHPSessionHandler::class )
46 ->disableOriginalConstructor()
50 $rProp = new \ReflectionProperty( PHPSessionHandler::class,
'instance' );
51 $rProp->setAccessible(
true );
52 $reset = new \Wikimedia\ScopedCallback( [ $rProp,
'setValue' ], [ $rProp->getValue() ] );
55 $handler->setEnableFlags(
'enable' );
56 $this->assertTrue(
$handler->enable );
57 $this->assertFalse(
$handler->warn );
61 $this->assertTrue(
$handler->enable );
65 $handler->setEnableFlags(
'disable' );
66 $this->assertFalse(
$handler->enable );
69 $rProp->setValue(
null );
75 $rProp->setValue(
null );
77 session_write_close();
78 ini_set(
'session.use_cookies', 1 );
79 ini_set(
'session.use_trans_sid', 1 );
82 $logger = new \TestLogger();
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 );
111 'wgSessionProviders' => [ [
'class' =>
'DummySessionProvider' ] ],
112 'wgObjectCacheSessionExpiry' => 2,
116 $logger = new \TestLogger(
true,
function ( $m ) {
118 return preg_match(
'/^SessionBackend "\{session\}" /', $m ) ? null : $m;
125 $wrap = \TestingAccessWrapper::newFromObject( $rProp->getValue() );
126 $reset[] = new \Wikimedia\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\"" );
140 $sessionA = str_repeat(
'a', 32 );
141 $sessionB = str_repeat(
'b', 32 );
142 $sessionC = str_repeat(
'c', 32 );
145 $_SESSION[
'AuthenticationSessionTest'] =
'bogus';
147 session_id( $sessionA );
149 $this->assertSame( [], $_SESSION );
150 $this->assertSame( $sessionA, session_id() );
154 $_SESSION[
'AuthenticationSessionTest'] = $rand;
155 $expect = [
'AuthenticationSessionTest' => $rand ];
156 session_write_close();
158 [ LogLevel::WARNING,
'Something wrote to $_SESSION!' ],
159 ], $logger->getBuffer() );
163 $_SESSION[
'AuthenticationSessionTest'] =
'bogus';
167 $this->assertSame( $expect, $_SESSION );
170 if ( function_exists(
'session_reset' ) ) {
171 $_SESSION[
'AuthenticationSessionTest'] =
'bogus';
173 $this->assertSame( $expect, $_SESSION );
177 $_SESSION[
'AuthenticationSessionTest'] = $rand;
178 session_write_close();
180 $this->assertSame( $expect, $_SESSION );
182 session_id( $sessionA );
184 $this->assertSame( [], $_SESSION );
185 session_write_close();
188 session_id( $sessionB );
190 $this->assertSame( $sessionB, session_id() );
191 $_SESSION[
'id'] =
'B';
192 session_write_close();
194 session_id( $sessionC );
196 $this->assertSame( [], $_SESSION );
197 $_SESSION[
'id'] =
'C';
198 session_write_close();
200 session_id( $sessionB );
202 $this->assertSame( [
'id' =>
'B' ], $_SESSION );
203 session_write_close();
205 session_id( $sessionC );
207 $this->assertSame( [
'id' =>
'C' ], $_SESSION );
210 session_id( $sessionB );
212 $this->assertSame( [
'id' =>
'B' ], $_SESSION );
215 session_write_close();
217 $session = $manager->getEmptySession();
218 $session->set(
'Unchanged',
'setup' );
219 $session->set(
'Unchanged, null',
null );
220 $session->set(
'Changed in $_SESSION',
'setup' );
221 $session->set(
'Changed in Session',
'setup' );
222 $session->set(
'Changed in both',
'setup' );
223 $session->set(
'Deleted in Session',
'setup' );
224 $session->set(
'Deleted in $_SESSION',
'setup' );
225 $session->set(
'Deleted in both',
'setup' );
226 $session->set(
'Deleted in Session, changed in $_SESSION',
'setup' );
227 $session->set(
'Deleted in $_SESSION, changed in Session',
'setup' );
231 session_id( $session->getId() );
233 $session->set(
'Added in Session',
'Session' );
234 $session->set(
'Added in both',
'Session' );
235 $session->set(
'Changed in Session',
'Session' );
236 $session->set(
'Changed in both',
'Session' );
237 $session->set(
'Deleted in $_SESSION, changed in Session',
'Session' );
238 $session->remove(
'Deleted in Session' );
239 $session->remove(
'Deleted in both' );
240 $session->remove(
'Deleted in Session, changed in $_SESSION' );
242 $_SESSION[
'Added in $_SESSION'] =
'$_SESSION';
243 $_SESSION[
'Added in both'] =
'$_SESSION';
244 $_SESSION[
'Changed in $_SESSION'] =
'$_SESSION';
245 $_SESSION[
'Changed in both'] =
'$_SESSION';
246 $_SESSION[
'Deleted in Session, changed in $_SESSION'] =
'$_SESSION';
247 unset( $_SESSION[
'Deleted in $_SESSION'] );
248 unset( $_SESSION[
'Deleted in both'] );
249 unset( $_SESSION[
'Deleted in $_SESSION, changed in Session'] );
250 session_write_close();
252 $this->assertEquals( [
253 'Added in Session' =>
'Session',
254 'Added in $_SESSION' =>
'$_SESSION',
255 'Added in both' =>
'Session',
256 'Unchanged' =>
'setup',
257 'Unchanged, null' =>
null,
258 'Changed in Session' =>
'Session',
259 'Changed in $_SESSION' =>
'$_SESSION',
260 'Changed in both' =>
'Session',
261 'Deleted in Session, changed in $_SESSION' =>
'$_SESSION',
262 'Deleted in $_SESSION, changed in Session' =>
'Session',
263 ], iterator_to_array( $session ) );
266 $session->set( 42,
'forty-two' );
267 $session->set(
'forty-two', 42 );
268 $session->set(
'wrong', 43 );
273 $this->assertArrayHasKey(
'forty-two', $_SESSION );
274 $this->assertSame( 42, $_SESSION[
'forty-two'] );
275 $this->assertArrayHasKey(
'wrong', $_SESSION );
276 unset( $_SESSION[
'wrong'] );
277 session_write_close();
279 $this->assertEquals( [
282 ], iterator_to_array( $session ) );
285 $session = $manager->getEmptySession();
287 $id = $session->getId();
292 'SessionCheckInfo' => [
function ( &$reason ) {
297 $this->assertNull( $manager->getSessionById( $id,
true ),
'sanity check' );
298 session_write_close();
301 'SessionCheckInfo' => [],
303 $this->assertNotNull( $manager->getSessionById( $id,
true ),
'sanity check' );
320 $rProp = new \ReflectionProperty( PHPSessionHandler::class,
'instance' );
321 $rProp->setAccessible(
true );
322 $handler = $this->getMockBuilder( PHPSessionHandler::class )
324 ->disableOriginalConstructor()
326 \TestingAccessWrapper::newFromObject(
$handler )->setEnableFlags(
'disable' );
327 $oldValue = $rProp->getValue();
329 $reset = new \Wikimedia\ScopedCallback( [ $rProp,
'setValue' ], [ $oldValue ] );