/** * Override the singleton for unit testing * @param SessionManager|null $manager * @return \\ScopedCallback|null */ public static function setSessionManagerSingleton(SessionManager $manager = null) { session_write_close(); $rInstance = new \ReflectionProperty(SessionManager::class, 'instance'); $rInstance->setAccessible(true); $rGlobalSession = new \ReflectionProperty(SessionManager::class, 'globalSession'); $rGlobalSession->setAccessible(true); $rGlobalSessionRequest = new \ReflectionProperty(SessionManager::class, 'globalSessionRequest'); $rGlobalSessionRequest->setAccessible(true); $oldInstance = $rInstance->getValue(); $reset = [[$rInstance, $oldInstance], [$rGlobalSession, $rGlobalSession->getValue()], [$rGlobalSessionRequest, $rGlobalSessionRequest->getValue()]]; $rInstance->setValue($manager); $rGlobalSession->setValue(null); $rGlobalSessionRequest->setValue(null); if ($manager && PHPSessionHandler::isInstalled()) { PHPSessionHandler::install($manager); } return new \ScopedCallback(function () use(&$reset, $oldInstance) { foreach ($reset as &$arr) { $arr[0]->setValue($arr[1]); } if ($oldInstance && PHPSessionHandler::isInstalled()) { PHPSessionHandler::install($oldInstance); } }); }
public function testGetGlobalSession() { $context = \RequestContext::getMain(); if (!PHPSessionHandler::isInstalled()) { PHPSessionHandler::install(SessionManager::singleton()); } $rProp = new \ReflectionProperty('MediaWiki\\Session\\PHPSessionHandler', 'instance'); $rProp->setAccessible(true); $handler = \TestingAccessWrapper::newFromObject($rProp->getValue()); $oldEnable = $handler->enable; $reset[] = new \ScopedCallback(function () use($handler, $oldEnable) { if ($handler->enable) { session_write_close(); } $handler->enable = $oldEnable; }); $reset[] = TestUtils::setSessionManagerSingleton($this->getManager()); $handler->enable = true; $request = new \FauxRequest(); $context->setRequest($request); $id = $request->getSession()->getId(); session_id(''); $session = SessionManager::getGlobalSession(); $this->assertSame($id, $session->getId()); session_id('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); $session = SessionManager::getGlobalSession(); $this->assertSame('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', $session->getId()); $this->assertSame('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', $request->getSession()->getId()); session_write_close(); $handler->enable = false; $request = new \FauxRequest(); $context->setRequest($request); $id = $request->getSession()->getId(); session_id(''); $session = SessionManager::getGlobalSession(); $this->assertSame($id, $session->getId()); session_id('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); $session = SessionManager::getGlobalSession(); $this->assertSame($id, $session->getId()); $this->assertSame($id, $request->getSession()->getId()); }
public function testInstall() { $reset = $this->getResetter($rProp); $rProp->setValue(null); session_write_close(); ini_set('session.use_cookies', 1); ini_set('session.use_trans_sid', 1); $store = new \HashBagOStuff(); $logger = new \TestLogger(); $manager = new SessionManager(array('store' => $store, 'logger' => $logger)); $this->assertFalse(PHPSessionHandler::isInstalled()); PHPSessionHandler::install($manager); $this->assertTrue(PHPSessionHandler::isInstalled()); $this->assertFalse(wfIniGetBool('session.use_cookies')); $this->assertFalse(wfIniGetBool('session.use_trans_sid')); $this->assertNotNull($rProp->getValue()); $priv = \TestingAccessWrapper::newFromObject($rProp->getValue()); $this->assertSame($manager, $priv->manager); $this->assertSame($store, $priv->store); $this->assertSame($logger, $priv->logger); }
public function testResetIdOfGlobalSession() { if (!PHPSessionHandler::isInstalled()) { PHPSessionHandler::install(SessionManager::singleton()); } if (!PHPSessionHandler::isEnabled()) { $rProp = new \ReflectionProperty('MediaWiki\\Session\\PHPSessionHandler', 'instance'); $rProp->setAccessible(true); $handler = \TestingAccessWrapper::newFromObject($rProp->getValue()); $resetHandler = new \ScopedCallback(function () use($handler) { session_write_close(); $handler->enable = false; }); $handler->enable = true; } $backend = $this->getBackend(User::newFromName('UTSysop')); \TestingAccessWrapper::newFromObject($backend)->usePhpSessionHandling = true; TestUtils::setSessionManagerSingleton($this->manager); $manager = \TestingAccessWrapper::newFromObject($this->manager); $request = \RequestContext::getMain()->getRequest(); $manager->globalSession = $backend->getSession($request); $manager->globalSessionRequest = $request; session_id(self::SESSIONID); \MediaWiki\quietCall('session_start'); $backend->resetId(); $this->assertNotEquals(self::SESSIONID, $backend->getId()); $this->assertSame($backend->getId(), session_id()); session_write_close(); session_id(''); $this->assertNotSame($backend->getId(), session_id(), 'sanity check'); $backend->persist(); $this->assertSame($backend->getId(), session_id()); session_write_close(); }