5 * @group RequestContext
7 class RequestContextTest
extends MediaWikiTestCase
{
10 * Test the relationship between title and wikipage in RequestContext
11 * @covers RequestContext::getWikiPage
12 * @covers RequestContext::getTitle
14 public function testWikiPageTitle() {
15 $context = new RequestContext();
17 $curTitle = Title
::newFromText( "A" );
18 $context->setTitle( $curTitle );
19 $this->assertTrue( $curTitle->equals( $context->getWikiPage()->getTitle() ),
20 "When a title is first set WikiPage should be created on-demand for that title." );
22 $curTitle = Title
::newFromText( "B" );
23 $context->setWikiPage( WikiPage
::factory( $curTitle ) );
24 $this->assertTrue( $curTitle->equals( $context->getTitle() ),
25 "Title must be updated when a new WikiPage is provided." );
27 $curTitle = Title
::newFromText( "C" );
28 $context->setTitle( $curTitle );
30 $curTitle->equals( $context->getWikiPage()->getTitle() ),
31 "When a title is updated the WikiPage should be purged "
32 . "and recreated on-demand with the new title."
37 * @covers RequestContext::importScopedSession
39 public function testImportScopedSession() {
40 $context = RequestContext
::getMain();
42 $oInfo = $context->exportSession();
43 $this->assertEquals( '127.0.0.1', $oInfo['ip'], "Correct initial IP address." );
44 $this->assertEquals( 0, $oInfo['userId'], "Correct initial user ID." );
46 $user = User
::newFromName( 'UnitTestContextUser' );
47 $user->addToDatabase();
50 'sessionId' => 'd612ee607c87e749ef14da4983a702cd',
51 'userId' => $user->getId(),
54 'USER-AGENT' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0'
57 // importScopedSession() sets these variables
58 $this->setMwGlobals( array(
60 'wgRequest' => new FauxRequest
,
62 $sc = RequestContext
::importScopedSession( $sinfo ); // load new context
64 $info = $context->exportSession();
65 $this->assertEquals( $sinfo['ip'], $info['ip'], "Correct IP address." );
66 $this->assertEquals( $sinfo['headers'], $info['headers'], "Correct headers." );
67 $this->assertEquals( $sinfo['sessionId'], $info['sessionId'], "Correct session ID." );
68 $this->assertEquals( $sinfo['userId'], $info['userId'], "Correct user ID." );
71 $context->getRequest()->getIP(),
72 "Correct context IP address."
76 $context->getRequest()->getAllHeaders(),
77 "Correct context headers."
79 $this->assertEquals( $sinfo['sessionId'], session_id(), "Correct context session ID." );
80 $this->assertEquals( true, $context->getUser()->isLoggedIn(), "Correct context user." );
81 $this->assertEquals( $sinfo['userId'], $context->getUser()->getId(), "Correct context user ID." );
83 'UnitTestContextUser',
84 $context->getUser()->getName(),
85 "Correct context user name."
88 unset( $sc ); // restore previous context
90 $info = $context->exportSession();
91 $this->assertEquals( $oInfo['ip'], $info['ip'], "Correct restored IP address." );
92 $this->assertEquals( $oInfo['headers'], $info['headers'], "Correct restored headers." );
93 $this->assertEquals( $oInfo['sessionId'], $info['sessionId'], "Correct restored session ID." );
94 $this->assertEquals( $oInfo['userId'], $info['userId'], "Correct restored user ID." );