Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / tests / phpunit / includes / context / RequestContextTest.php
bloba9e5be24932001bc8837c26784a81460f9a59eb7
1 <?php
3 /**
4 * @group Database
5 * @group RequestContext
6 */
7 class RequestContextTest extends MediaWikiTestCase {
9 /**
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 );
29 $this->assertTrue(
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."
36 /**
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();
49 $sinfo = array(
50 'sessionId' => 'd612ee607c87e749ef14da4983a702cd',
51 'userId' => $user->getId(),
52 'ip' => '192.0.2.0',
53 'headers' => array(
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(
59 'wgUser' => new User,
60 'wgRequest' => new FauxRequest,
61 ) );
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." );
69 $this->assertEquals(
70 $sinfo['ip'],
71 $context->getRequest()->getIP(),
72 "Correct context IP address."
74 $this->assertEquals(
75 $sinfo['headers'],
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." );
82 $this->assertEquals(
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." );