2016-02-01 20:44:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Session;
|
|
|
|
|
|
2016-02-10 16:43:23 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2017-04-19 19:37:35 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
2016-02-10 16:43:23 +00:00
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
/**
|
|
|
|
|
* Utility functions for Session unit tests
|
|
|
|
|
*/
|
|
|
|
|
class TestUtils {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Override the singleton for unit testing
|
|
|
|
|
* @param SessionManager|null $manager
|
2016-09-29 08:16:05 +00:00
|
|
|
* @return \\Wikimedia\ScopedCallback|null
|
2016-02-01 20:44:03 +00:00
|
|
|
*/
|
|
|
|
|
public static function setSessionManagerSingleton( SessionManager $manager = null ) {
|
|
|
|
|
session_write_close();
|
|
|
|
|
|
|
|
|
|
$rInstance = new \ReflectionProperty(
|
2016-03-28 18:53:04 +00:00
|
|
|
SessionManager::class, 'instance'
|
2016-02-01 20:44:03 +00:00
|
|
|
);
|
|
|
|
|
$rInstance->setAccessible( true );
|
|
|
|
|
$rGlobalSession = new \ReflectionProperty(
|
2016-03-28 18:53:04 +00:00
|
|
|
SessionManager::class, 'globalSession'
|
2016-02-01 20:44:03 +00:00
|
|
|
);
|
|
|
|
|
$rGlobalSession->setAccessible( true );
|
|
|
|
|
$rGlobalSessionRequest = new \ReflectionProperty(
|
2016-03-28 18:53:04 +00:00
|
|
|
SessionManager::class, 'globalSessionRequest'
|
2016-02-01 20:44:03 +00:00
|
|
|
);
|
|
|
|
|
$rGlobalSessionRequest->setAccessible( true );
|
|
|
|
|
|
|
|
|
|
$oldInstance = $rInstance->getValue();
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$reset = [
|
|
|
|
|
[ $rInstance, $oldInstance ],
|
|
|
|
|
[ $rGlobalSession, $rGlobalSession->getValue() ],
|
|
|
|
|
[ $rGlobalSessionRequest, $rGlobalSessionRequest->getValue() ],
|
|
|
|
|
];
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
$rInstance->setValue( $manager );
|
|
|
|
|
$rGlobalSession->setValue( null );
|
|
|
|
|
$rGlobalSessionRequest->setValue( null );
|
|
|
|
|
if ( $manager && PHPSessionHandler::isInstalled() ) {
|
|
|
|
|
PHPSessionHandler::install( $manager );
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-12 05:36:03 +00:00
|
|
|
return new \Wikimedia\ScopedCallback( function () use ( &$reset, $oldInstance ) {
|
2016-02-01 20:44:03 +00:00
|
|
|
foreach ( $reset as &$arr ) {
|
|
|
|
|
$arr[0]->setValue( $arr[1] );
|
|
|
|
|
}
|
|
|
|
|
if ( $oldInstance && PHPSessionHandler::isInstalled() ) {
|
|
|
|
|
PHPSessionHandler::install( $oldInstance );
|
|
|
|
|
}
|
|
|
|
|
} );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If you need a SessionBackend for testing but don't want to create a real
|
|
|
|
|
* one, use this.
|
|
|
|
|
* @return SessionBackend Unconfigured! Use reflection to set any private
|
|
|
|
|
* fields necessary.
|
|
|
|
|
*/
|
|
|
|
|
public static function getDummySessionBackend() {
|
2016-03-28 18:53:04 +00:00
|
|
|
$rc = new \ReflectionClass( SessionBackend::class );
|
2016-02-01 20:44:03 +00:00
|
|
|
if ( !method_exists( $rc, 'newInstanceWithoutConstructor' ) ) {
|
|
|
|
|
\PHPUnit_Framework_Assert::markTestSkipped(
|
|
|
|
|
'ReflectionClass::newInstanceWithoutConstructor isn\'t available'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-10 16:43:23 +00:00
|
|
|
$ret = $rc->newInstanceWithoutConstructor();
|
2017-04-19 19:37:35 +00:00
|
|
|
TestingAccessWrapper::newFromObject( $ret )->logger = new \TestLogger;
|
2016-02-10 16:43:23 +00:00
|
|
|
return $ret;
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If you need a Session for testing but don't want to create a backend to
|
|
|
|
|
* construct one, use this.
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param object|null $backend Object to serve as the SessionBackend
|
2017-12-28 15:06:10 +00:00
|
|
|
* @param int $index
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param LoggerInterface|null $logger
|
2016-02-01 20:44:03 +00:00
|
|
|
* @return Session
|
|
|
|
|
*/
|
2016-02-10 16:43:23 +00:00
|
|
|
public static function getDummySession( $backend = null, $index = -1, $logger = null ) {
|
2016-03-28 18:53:04 +00:00
|
|
|
$rc = new \ReflectionClass( Session::class );
|
2016-02-01 20:44:03 +00:00
|
|
|
if ( !method_exists( $rc, 'newInstanceWithoutConstructor' ) ) {
|
|
|
|
|
\PHPUnit_Framework_Assert::markTestSkipped(
|
|
|
|
|
'ReflectionClass::newInstanceWithoutConstructor isn\'t available'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $backend === null ) {
|
|
|
|
|
$backend = new DummySessionBackend;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$session = $rc->newInstanceWithoutConstructor();
|
2017-04-19 19:37:35 +00:00
|
|
|
$priv = TestingAccessWrapper::newFromObject( $session );
|
2016-02-01 20:44:03 +00:00
|
|
|
$priv->backend = $backend;
|
|
|
|
|
$priv->index = $index;
|
2016-02-10 16:43:23 +00:00
|
|
|
$priv->logger = $logger ?: new \TestLogger;
|
2016-02-01 20:44:03 +00:00
|
|
|
return $session;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|