/**
  * @covers RequestContext::importScopedSession
  */
 public function testImportScopedSession()
 {
     // Make sure session handling is started
     if (!MediaWiki\Session\PHPSessionHandler::isInstalled()) {
         MediaWiki\Session\PHPSessionHandler::install(MediaWiki\Session\SessionManager::singleton());
     }
     $oldSessionId = session_id();
     $context = RequestContext::getMain();
     $oInfo = $context->exportSession();
     $this->assertEquals('127.0.0.1', $oInfo['ip'], "Correct initial IP address.");
     $this->assertEquals(0, $oInfo['userId'], "Correct initial user ID.");
     $this->assertFalse(MediaWiki\Session\SessionManager::getGlobalSession()->isPersistent(), 'Global session isn\'t persistent to start');
     $user = User::newFromName('UnitTestContextUser');
     $user->addToDatabase();
     $sinfo = array('sessionId' => 'd612ee607c87e749ef14da4983a702cd', 'userId' => $user->getId(), 'ip' => '192.0.2.0', 'headers' => array('USER-AGENT' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0'));
     // importScopedSession() sets these variables
     $this->setMwGlobals(array('wgUser' => new User(), 'wgRequest' => new FauxRequest()));
     $sc = RequestContext::importScopedSession($sinfo);
     // load new context
     $info = $context->exportSession();
     $this->assertEquals($sinfo['ip'], $info['ip'], "Correct IP address.");
     $this->assertEquals($sinfo['headers'], $info['headers'], "Correct headers.");
     $this->assertEquals($sinfo['sessionId'], $info['sessionId'], "Correct session ID.");
     $this->assertEquals($sinfo['userId'], $info['userId'], "Correct user ID.");
     $this->assertEquals($sinfo['ip'], $context->getRequest()->getIP(), "Correct context IP address.");
     $this->assertEquals($sinfo['headers'], $context->getRequest()->getAllHeaders(), "Correct context headers.");
     $this->assertEquals($sinfo['sessionId'], MediaWiki\Session\SessionManager::getGlobalSession()->getId(), "Correct context session ID.");
     if (\MediaWiki\Session\PhpSessionHandler::isEnabled()) {
         $this->assertEquals($sinfo['sessionId'], session_id(), "Correct context session ID.");
     } else {
         $this->assertEquals($oldSessionId, session_id(), "Unchanged PHP session ID.");
     }
     $this->assertEquals(true, $context->getUser()->isLoggedIn(), "Correct context user.");
     $this->assertEquals($sinfo['userId'], $context->getUser()->getId(), "Correct context user ID.");
     $this->assertEquals('UnitTestContextUser', $context->getUser()->getName(), "Correct context user name.");
     unset($sc);
     // restore previous context
     $info = $context->exportSession();
     $this->assertEquals($oInfo['ip'], $info['ip'], "Correct restored IP address.");
     $this->assertEquals($oInfo['headers'], $info['headers'], "Correct restored headers.");
     $this->assertEquals($oInfo['sessionId'], $info['sessionId'], "Correct restored session ID.");
     $this->assertEquals($oInfo['userId'], $info['userId'], "Correct restored user ID.");
     $this->assertFalse(MediaWiki\Session\SessionManager::getGlobalSession()->isPersistent(), 'Global session isn\'t persistent after restoring the context');
 }
示例#2
0
/**
 * Initialise php session
 *
 * @deprecated since 1.27, use MediaWiki\\Session\\SessionManager instead.
 *  Generally, "using" SessionManager will be calling ->getSessionById() or
 *  ::getGlobalSession() (depending on whether you were passing $sessionId
 *  here), then calling $session->persist().
 * @param bool|string $sessionId
 */
function wfSetupSession($sessionId = false)
{
    wfDeprecated(__FUNCTION__, '1.27');
    // If they're calling this, they probably want our session management even
    // if NO_SESSION was set for Setup.php.
    if (!MediaWiki\Session\PHPSessionHandler::isInstalled()) {
        MediaWiki\Session\PHPSessionHandler::install(SessionManager::singleton());
    }
    if ($sessionId) {
        session_id($sessionId);
    }
    $session = SessionManager::getGlobalSession();
    $session->persist();
    if (session_id() !== $session->getId()) {
        session_id($session->getId());
    }
    MediaWiki\quietCall('session_cache_limiter', 'private, must-revalidate');
    MediaWiki\quietCall('session_start');
}