2012-10-24 14:32:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
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
|
|
|
*/
|
2012-10-24 14:32:46 +00:00
|
|
|
class RequestContextTest extends MediaWikiTestCase {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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() {
|
|
|
|
|
$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." );
|
|
|
|
|
|
|
|
|
|
$user = User::newFromName( 'UnitTestContextUser' );
|
|
|
|
|
$user->addToDatabase();
|
|
|
|
|
|
|
|
|
|
$sinfo = array(
|
|
|
|
|
'sessionId' => 'd612ee607c87e749ef14da4983a702cd',
|
|
|
|
|
'userId' => $user->getId(),
|
|
|
|
|
'ip' => '192.0.2.0',
|
2014-04-24 12:35:05 +00:00
|
|
|
'headers' => array(
|
|
|
|
|
'USER-AGENT' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0'
|
|
|
|
|
)
|
2013-03-14 22:43:42 +00:00
|
|
|
);
|
2014-06-27 21:18:07 +00:00
|
|
|
// importScopedSession() sets these variables
|
|
|
|
|
$this->setMwGlobals( array(
|
|
|
|
|
'wgUser' => new User,
|
|
|
|
|
'wgRequest' => new FauxRequest,
|
|
|
|
|
) );
|
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 17:28:29 +00:00
|
|
|
$this->assertEquals( $sinfo['sessionId'], session_id(), "Correct context session ID." );
|
2013-03-14 22:43:42 +00:00
|
|
|
$this->assertEquals( true, $context->getUser()->isLoggedIn(), "Correct context user." );
|
|
|
|
|
$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." );
|
2013-03-14 22:43:42 +00:00
|
|
|
}
|
2012-10-24 14:32:46 +00:00
|
|
|
}
|