2012-10-24 14:32:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
2021-01-08 01:01:28 +00:00
|
|
|
use MediaWiki\Permissions\UltimateAuthority;
|
|
|
|
|
use MediaWiki\User\UserIdentityValue;
|
|
|
|
|
|
2013-03-14 22:43:42 +00:00
|
|
|
/**
|
|
|
|
|
* @group Database
|
2014-06-27 21:18:07 +00:00
|
|
|
* @group RequestContext
|
2013-03-14 22:43:42 +00:00
|
|
|
*/
|
2020-06-30 15:09:24 +00:00
|
|
|
class RequestContextTest extends MediaWikiIntegrationTestCase {
|
2012-10-24 14:32:46 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test the relationship between title and wikipage in RequestContext
|
2013-10-21 21:09:13 +00:00
|
|
|
* @covers RequestContext::getWikiPage
|
|
|
|
|
* @covers RequestContext::getTitle
|
2012-10-24 14:32:46 +00:00
|
|
|
*/
|
|
|
|
|
public function testWikiPageTitle() {
|
|
|
|
|
$context = new RequestContext();
|
|
|
|
|
|
|
|
|
|
$curTitle = Title::newFromText( "A" );
|
|
|
|
|
$context->setTitle( $curTitle );
|
|
|
|
|
$this->assertTrue( $curTitle->equals( $context->getWikiPage()->getTitle() ),
|
|
|
|
|
"When a title is first set WikiPage should be created on-demand for that title." );
|
|
|
|
|
|
|
|
|
|
$curTitle = Title::newFromText( "B" );
|
|
|
|
|
$context->setWikiPage( WikiPage::factory( $curTitle ) );
|
|
|
|
|
$this->assertTrue( $curTitle->equals( $context->getTitle() ),
|
|
|
|
|
"Title must be updated when a new WikiPage is provided." );
|
|
|
|
|
|
|
|
|
|
$curTitle = Title::newFromText( "C" );
|
|
|
|
|
$context->setTitle( $curTitle );
|
2014-04-24 12:35:05 +00:00
|
|
|
$this->assertTrue(
|
|
|
|
|
$curTitle->equals( $context->getWikiPage()->getTitle() ),
|
|
|
|
|
"When a title is updated the WikiPage should be purged "
|
|
|
|
|
. "and recreated on-demand with the new title."
|
|
|
|
|
);
|
2012-10-24 14:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
/**
|
|
|
|
|
* @covers RequestContext::importScopedSession
|
|
|
|
|
*/
|
2013-03-14 22:43:42 +00:00
|
|
|
public function testImportScopedSession() {
|
2016-02-01 20:44:03 +00:00
|
|
|
// Make sure session handling is started
|
|
|
|
|
if ( !MediaWiki\Session\PHPSessionHandler::isInstalled() ) {
|
|
|
|
|
MediaWiki\Session\PHPSessionHandler::install(
|
|
|
|
|
MediaWiki\Session\SessionManager::singleton()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$oldSessionId = session_id();
|
|
|
|
|
|
2013-03-14 22:43:42 +00:00
|
|
|
$context = RequestContext::getMain();
|
|
|
|
|
|
|
|
|
|
$oInfo = $context->exportSession();
|
|
|
|
|
$this->assertEquals( '127.0.0.1', $oInfo['ip'], "Correct initial IP address." );
|
2019-09-17 14:31:49 +00:00
|
|
|
$this->assertSame( 0, $oInfo['userId'], "Correct initial user ID." );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->assertFalse( MediaWiki\Session\SessionManager::getGlobalSession()->isPersistent(),
|
|
|
|
|
'Global session isn\'t persistent to start' );
|
2013-03-14 22:43:42 +00:00
|
|
|
|
|
|
|
|
$user = User::newFromName( 'UnitTestContextUser' );
|
|
|
|
|
$user->addToDatabase();
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$sinfo = [
|
2013-03-14 22:43:42 +00:00
|
|
|
'sessionId' => 'd612ee607c87e749ef14da4983a702cd',
|
|
|
|
|
'userId' => $user->getId(),
|
|
|
|
|
'ip' => '192.0.2.0',
|
2016-02-17 09:09:32 +00:00
|
|
|
'headers' => [
|
2014-04-24 12:35:05 +00:00
|
|
|
'USER-AGENT' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0'
|
2016-02-17 09:09:32 +00:00
|
|
|
]
|
|
|
|
|
];
|
2014-06-27 21:18:07 +00:00
|
|
|
// importScopedSession() sets these variables
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [
|
2014-06-27 21:18:07 +00:00
|
|
|
'wgRequest' => new FauxRequest,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2013-03-14 22:43:42 +00:00
|
|
|
$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." );
|
2014-04-24 12:35:05 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
$sinfo['ip'],
|
|
|
|
|
$context->getRequest()->getIP(),
|
|
|
|
|
"Correct context IP address."
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$sinfo['headers'],
|
|
|
|
|
$context->getRequest()->getAllHeaders(),
|
|
|
|
|
"Correct context headers."
|
|
|
|
|
);
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
$sinfo['sessionId'],
|
|
|
|
|
MediaWiki\Session\SessionManager::getGlobalSession()->getId(),
|
|
|
|
|
"Correct context session ID."
|
|
|
|
|
);
|
2016-03-18 13:55:54 +00:00
|
|
|
if ( \MediaWiki\Session\PHPSessionHandler::isEnabled() ) {
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->assertEquals( $sinfo['sessionId'], session_id(), "Correct context session ID." );
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertEquals( $oldSessionId, session_id(), "Unchanged PHP session ID." );
|
|
|
|
|
}
|
2020-12-17 23:10:11 +00:00
|
|
|
$this->assertTrue( $context->getUser()->isRegistered(), "Correct context user." );
|
2013-03-14 22:43:42 +00:00
|
|
|
$this->assertEquals( $sinfo['userId'], $context->getUser()->getId(), "Correct context user ID." );
|
2014-04-24 12:35:05 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
'UnitTestContextUser',
|
|
|
|
|
$context->getUser()->getName(),
|
|
|
|
|
"Correct context user name."
|
|
|
|
|
);
|
2013-03-14 22:43:42 +00:00
|
|
|
|
2013-04-26 14:42:31 +00:00
|
|
|
unset( $sc ); // restore previous context
|
2013-03-14 22:43:42 +00:00
|
|
|
|
|
|
|
|
$info = $context->exportSession();
|
2014-10-15 01:45:51 +00:00
|
|
|
$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." );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->assertFalse( MediaWiki\Session\SessionManager::getGlobalSession()->isPersistent(),
|
|
|
|
|
'Global session isn\'t persistent after restoring the context' );
|
2013-03-14 22:43:42 +00:00
|
|
|
}
|
2021-01-08 01:01:28 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers RequestContext::getUser
|
|
|
|
|
* @covers RequestContext::setUser
|
|
|
|
|
* @covers RequestContext::getAuthority
|
|
|
|
|
* @covers RequestContext::setAuthority
|
|
|
|
|
*/
|
|
|
|
|
public function testTestGetSetAuthority() {
|
|
|
|
|
$context = new RequestContext();
|
|
|
|
|
|
|
|
|
|
$user = $this->getTestUser()->getUser();
|
|
|
|
|
|
|
|
|
|
$context->setUser( $user );
|
2021-03-04 19:45:28 +00:00
|
|
|
$this->assertTrue( $user->equals( $context->getAuthority()->getUser() ) );
|
2021-01-08 01:01:28 +00:00
|
|
|
$this->assertTrue( $user->equals( $context->getUser() ) );
|
|
|
|
|
|
2021-02-15 18:58:09 +00:00
|
|
|
$authorityActor = new UserIdentityValue( 42, 'Test' );
|
2021-01-08 01:01:28 +00:00
|
|
|
$authority = new UltimateAuthority( $authorityActor );
|
|
|
|
|
|
|
|
|
|
$context->setAuthority( $authority );
|
|
|
|
|
$this->assertTrue( $context->getUser()->equals( $authorityActor ) );
|
2021-03-04 19:45:28 +00:00
|
|
|
$this->assertTrue( $context->getAuthority()->getUser()->equals( $authorityActor ) );
|
2021-01-08 01:01:28 +00:00
|
|
|
}
|
2012-10-24 14:32:46 +00:00
|
|
|
}
|