Merge "Add phpdoc for some ApiQueryInfo properties"
[lhc/web/wiklou.git] / tests / phpunit / includes / context / RequestContextTest.php
1 <?php
2
3 /**
4 * @group Database
5 * @group RequestContext
6 */
7 class RequestContextTest extends MediaWikiTestCase {
8
9 /**
10 * Test the relationship between title and wikipage in RequestContext
11 * @covers RequestContext::getWikiPage
12 * @covers RequestContext::getTitle
13 */
14 public function testWikiPageTitle() {
15 $context = new RequestContext();
16
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." );
21
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." );
26
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."
33 );
34 }
35
36 /**
37 * @covers RequestContext::importScopedSession
38 */
39 public function testImportScopedSession() {
40 $context = RequestContext::getMain();
41
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." );
45
46 $user = User::newFromName( 'UnitTestContextUser' );
47 $user->addToDatabase();
48
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'
55 )
56 );
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
63
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."
73 );
74 $this->assertEquals(
75 $sinfo['headers'],
76 $context->getRequest()->getAllHeaders(),
77 "Correct context headers."
78 );
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."
86 );
87
88 unset( $sc ); // restore previous context
89
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." );
95 }
96 }