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 // Make sure session handling is started
41 if ( !MediaWiki\Session\PHPSessionHandler
::isInstalled() ) {
42 MediaWiki\Session\PHPSessionHandler
::install(
43 MediaWiki\Session\SessionManager
::singleton()
46 $oldSessionId = session_id();
48 $context = RequestContext
::getMain();
50 $oInfo = $context->exportSession();
51 $this->assertEquals( '127.0.0.1', $oInfo['ip'], "Correct initial IP address." );
52 $this->assertEquals( 0, $oInfo['userId'], "Correct initial user ID." );
53 $this->assertFalse( MediaWiki\Session\SessionManager
::getGlobalSession()->isPersistent(),
54 'Global session isn\'t persistent to start' );
56 $user = User
::newFromName( 'UnitTestContextUser' );
57 $user->addToDatabase();
60 'sessionId' => 'd612ee607c87e749ef14da4983a702cd',
61 'userId' => $user->getId(),
64 'USER-AGENT' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0'
67 // importScopedSession() sets these variables
68 $this->setMwGlobals( [
70 'wgRequest' => new FauxRequest
,
72 $sc = RequestContext
::importScopedSession( $sinfo ); // load new context
74 $info = $context->exportSession();
75 $this->assertEquals( $sinfo['ip'], $info['ip'], "Correct IP address." );
76 $this->assertEquals( $sinfo['headers'], $info['headers'], "Correct headers." );
77 $this->assertEquals( $sinfo['sessionId'], $info['sessionId'], "Correct session ID." );
78 $this->assertEquals( $sinfo['userId'], $info['userId'], "Correct user ID." );
81 $context->getRequest()->getIP(),
82 "Correct context IP address."
86 $context->getRequest()->getAllHeaders(),
87 "Correct context headers."
91 MediaWiki\Session\SessionManager
::getGlobalSession()->getId(),
92 "Correct context session ID."
94 if ( \MediaWiki\Session\PHPSessionHandler
::isEnabled() ) {
95 $this->assertEquals( $sinfo['sessionId'], session_id(), "Correct context session ID." );
97 $this->assertEquals( $oldSessionId, session_id(), "Unchanged PHP session ID." );
99 $this->assertEquals( true, $context->getUser()->isLoggedIn(), "Correct context user." );
100 $this->assertEquals( $sinfo['userId'], $context->getUser()->getId(), "Correct context user ID." );
102 'UnitTestContextUser',
103 $context->getUser()->getName(),
104 "Correct context user name."
107 unset( $sc ); // restore previous context
109 $info = $context->exportSession();
110 $this->assertEquals( $oInfo['ip'], $info['ip'], "Correct restored IP address." );
111 $this->assertEquals( $oInfo['headers'], $info['headers'], "Correct restored headers." );
112 $this->assertEquals( $oInfo['sessionId'], $info['sessionId'], "Correct restored session ID." );
113 $this->assertEquals( $oInfo['userId'], $info['userId'], "Correct restored user ID." );
114 $this->assertFalse( MediaWiki\Session\SessionManager
::getGlobalSession()->isPersistent(),
115 'Global session isn\'t persistent after restoring the context' );