18 $rProp = new \ReflectionProperty( PHPSessionHandler::class,
'instance' );
19 $rProp->setAccessible(
true );
20 if ( $rProp->getValue() ) {
21 $old = TestingAccessWrapper::newFromObject( $rProp->getValue() );
22 $oldManager = $old->manager;
23 $oldStore = $old->store;
24 $oldLogger = $old->logger;
25 $reset[] = new \Wikimedia\ScopedCallback(
26 [ PHPSessionHandler::class,
'install' ],
27 [ $oldManager, $oldStore, $oldLogger ]
35 $handler = TestingAccessWrapper::newFromObject(
36 $this->getMockBuilder( PHPSessionHandler::class )
38 ->disableOriginalConstructor()
42 $rProp = new \ReflectionProperty( PHPSessionHandler::class,
'instance' );
43 $rProp->setAccessible(
true );
44 $reset = new \Wikimedia\ScopedCallback( [ $rProp,
'setValue' ], [ $rProp->getValue() ] );
47 $handler->setEnableFlags(
'enable' );
48 $this->assertTrue(
$handler->enable );
49 $this->assertFalse(
$handler->warn );
53 $this->assertTrue(
$handler->enable );
57 $handler->setEnableFlags(
'disable' );
58 $this->assertFalse(
$handler->enable );
61 $rProp->setValue(
null );
67 $rProp->setValue(
null );
69 session_write_close();
70 ini_set(
'session.use_cookies', 1 );
71 ini_set(
'session.use_trans_sid', 1 );
74 $logger = new \TestLogger();
84 $this->assertFalse(
wfIniGetBool(
'session.use_cookies' ) );
85 $this->assertFalse(
wfIniGetBool(
'session.use_trans_sid' ) );
87 $this->assertNotNull( $rProp->getValue() );
88 $priv = TestingAccessWrapper::newFromObject( $rProp->getValue() );
89 $this->assertSame( $manager, $priv->manager );
90 $this->assertSame( $store, $priv->store );
91 $this->assertSame( $logger, $priv->logger );
103 'wgSessionProviders' => [ [
'class' => \DummySessionProvider::class ] ],
104 'wgObjectCacheSessionExpiry' => 2,
108 $logger = new \TestLogger(
true,
function ( $m ) {
110 return preg_match(
'/^SessionBackend "\{session\}" /', $m ) ? null : $m;
117 $wrap = TestingAccessWrapper::newFromObject( $rProp->getValue() );
118 $reset[] = new \Wikimedia\ScopedCallback(
119 [ $wrap,
'setEnableFlags' ],
120 [ $wrap->enable ? $wrap->warn ?
'warn' :
'enable' :
'disable' ]
122 $wrap->setEnableFlags(
'warn' );
124 \Wikimedia\suppressWarnings();
125 ini_set(
'session.serialize_handler',
$handler );
126 \Wikimedia\restoreWarnings();
127 if ( ini_get(
'session.serialize_handler' ) !==
$handler ) {
128 $this->markTestSkipped(
"Cannot set session.serialize_handler to \"$handler\"" );
132 $sessionA = str_repeat(
'a', 32 );
133 $sessionB = str_repeat(
'b', 32 );
134 $sessionC = str_repeat(
'c', 32 );
137 $_SESSION[
'AuthenticationSessionTest'] =
'bogus';
139 session_id( $sessionA );
141 $this->assertSame( [], $_SESSION );
142 $this->assertSame( $sessionA, session_id() );
146 $_SESSION[
'AuthenticationSessionTest'] = $rand;
147 $expect = [
'AuthenticationSessionTest' => $rand ];
148 session_write_close();
150 [ LogLevel::WARNING,
'Something wrote to $_SESSION!' ],
151 ], $logger->getBuffer() );
155 $_SESSION[
'AuthenticationSessionTest'] =
'bogus';
159 $this->assertSame( $expect, $_SESSION );
162 if ( function_exists(
'session_reset' ) ) {
163 $_SESSION[
'AuthenticationSessionTest'] =
'bogus';
165 $this->assertSame( $expect, $_SESSION );
169 $_SESSION[
'AuthenticationSessionTest'] = $rand;
170 session_write_close();
172 $this->assertSame( $expect, $_SESSION );
174 session_id( $sessionA );
176 $this->assertSame( [], $_SESSION );
177 session_write_close();
180 session_id( $sessionB );
182 $this->assertSame( $sessionB, session_id() );
183 $_SESSION[
'id'] =
'B';
184 session_write_close();
186 session_id( $sessionC );
188 $this->assertSame( [], $_SESSION );
189 $_SESSION[
'id'] =
'C';
190 session_write_close();
192 session_id( $sessionB );
194 $this->assertSame( [
'id' =>
'B' ], $_SESSION );
195 session_write_close();
197 session_id( $sessionC );
199 $this->assertSame( [
'id' =>
'C' ], $_SESSION );
202 session_id( $sessionB );
204 $this->assertSame( [
'id' =>
'B' ], $_SESSION );
207 session_write_close();
209 $session = $manager->getEmptySession();
210 $session->set(
'Unchanged',
'setup' );
211 $session->set(
'Unchanged, null',
null );
212 $session->set(
'Changed in $_SESSION',
'setup' );
213 $session->set(
'Changed in Session',
'setup' );
214 $session->set(
'Changed in both',
'setup' );
215 $session->set(
'Deleted in Session',
'setup' );
216 $session->set(
'Deleted in $_SESSION',
'setup' );
217 $session->set(
'Deleted in both',
'setup' );
218 $session->set(
'Deleted in Session, changed in $_SESSION',
'setup' );
219 $session->set(
'Deleted in $_SESSION, changed in Session',
'setup' );
223 session_id( $session->getId() );
225 $session->set(
'Added in Session',
'Session' );
226 $session->set(
'Added in both',
'Session' );
227 $session->set(
'Changed in Session',
'Session' );
228 $session->set(
'Changed in both',
'Session' );
229 $session->set(
'Deleted in $_SESSION, changed in Session',
'Session' );
230 $session->remove(
'Deleted in Session' );
231 $session->remove(
'Deleted in both' );
232 $session->remove(
'Deleted in Session, changed in $_SESSION' );
234 $_SESSION[
'Added in $_SESSION'] =
'$_SESSION';
235 $_SESSION[
'Added in both'] =
'$_SESSION';
236 $_SESSION[
'Changed in $_SESSION'] =
'$_SESSION';
237 $_SESSION[
'Changed in both'] =
'$_SESSION';
238 $_SESSION[
'Deleted in Session, changed in $_SESSION'] =
'$_SESSION';
239 unset( $_SESSION[
'Deleted in $_SESSION'] );
240 unset( $_SESSION[
'Deleted in both'] );
241 unset( $_SESSION[
'Deleted in $_SESSION, changed in Session'] );
242 session_write_close();
244 $this->assertEquals( [
245 'Added in Session' =>
'Session',
246 'Added in $_SESSION' =>
'$_SESSION',
247 'Added in both' =>
'Session',
248 'Unchanged' =>
'setup',
249 'Unchanged, null' =>
null,
250 'Changed in Session' =>
'Session',
251 'Changed in $_SESSION' =>
'$_SESSION',
252 'Changed in both' =>
'Session',
253 'Deleted in Session, changed in $_SESSION' =>
'$_SESSION',
254 'Deleted in $_SESSION, changed in Session' =>
'Session',
255 ], iterator_to_array( $session ) );
258 $session->set( 42,
'forty-two' );
259 $session->set(
'forty-two', 42 );
260 $session->set(
'wrong', 43 );
265 $this->assertArrayHasKey(
'forty-two', $_SESSION );
266 $this->assertSame( 42, $_SESSION[
'forty-two'] );
267 $this->assertArrayHasKey(
'wrong', $_SESSION );
268 unset( $_SESSION[
'wrong'] );
269 session_write_close();
271 $this->assertEquals( [
274 ], iterator_to_array( $session ) );
277 $session = $manager->getEmptySession();
279 $id = $session->getId();
284 'SessionCheckInfo' => [
function ( &$reason ) {
289 $this->assertNull( $manager->getSessionById( $id,
true ),
'sanity check' );
290 session_write_close();
293 'SessionCheckInfo' => [],
295 $this->assertNotNull( $manager->getSessionById( $id,
true ),
'sanity check' );
312 $rProp = new \ReflectionProperty( PHPSessionHandler::class,
'instance' );
313 $rProp->setAccessible(
true );
314 $handler = $this->getMockBuilder( PHPSessionHandler::class )
316 ->disableOriginalConstructor()
318 TestingAccessWrapper::newFromObject(
$handler )->setEnableFlags(
'disable' );
319 $oldValue = $rProp->getValue();
321 $reset = new \Wikimedia\ScopedCallback( [ $rProp,
'setValue' ], [ $oldValue ] );