f595d2dc649340ba372c171b476f6d389a1fe9b8
[lhc/web/wiklou.git] / tests / phpunit / includes / RequestContextTest.php
1 <?php
2
3 /**
4 * @group Database
5 */
6 class RequestContextTest extends MediaWikiTestCase {
7
8 /**
9 * Test the relationship between title and wikipage in RequestContext
10 * @covers RequestContext::getWikiPage
11 * @covers RequestContext::getTitle
12 */
13 public function testWikiPageTitle() {
14 $context = new RequestContext();
15
16 $curTitle = Title::newFromText( "A" );
17 $context->setTitle( $curTitle );
18 $this->assertTrue( $curTitle->equals( $context->getWikiPage()->getTitle() ),
19 "When a title is first set WikiPage should be created on-demand for that title." );
20
21 $curTitle = Title::newFromText( "B" );
22 $context->setWikiPage( WikiPage::factory( $curTitle ) );
23 $this->assertTrue( $curTitle->equals( $context->getTitle() ),
24 "Title must be updated when a new WikiPage is provided." );
25
26 $curTitle = Title::newFromText( "C" );
27 $context->setTitle( $curTitle );
28 $this->assertTrue(
29 $curTitle->equals( $context->getWikiPage()->getTitle() ),
30 "When a title is updated the WikiPage should be purged "
31 . "and recreated on-demand with the new title."
32 );
33 }
34
35 /**
36 * @covers RequestContext::importScopedSession
37 */
38 public function testImportScopedSession() {
39 $context = RequestContext::getMain();
40
41 $oInfo = $context->exportSession();
42 $this->assertEquals( '127.0.0.1', $oInfo['ip'], "Correct initial IP address." );
43 $this->assertEquals( 0, $oInfo['userId'], "Correct initial user ID." );
44
45 $user = User::newFromName( 'UnitTestContextUser' );
46 $user->addToDatabase();
47
48 $sinfo = array(
49 'sessionId' => 'd612ee607c87e749ef14da4983a702cd',
50 'userId' => $user->getId(),
51 'ip' => '192.0.2.0',
52 'headers' => array(
53 'USER-AGENT' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0'
54 )
55 );
56 $sc = RequestContext::importScopedSession( $sinfo ); // load new context
57
58 $info = $context->exportSession();
59 $this->assertEquals( $sinfo['ip'], $info['ip'], "Correct IP address." );
60 $this->assertEquals( $sinfo['headers'], $info['headers'], "Correct headers." );
61 $this->assertEquals( $sinfo['sessionId'], $info['sessionId'], "Correct session ID." );
62 $this->assertEquals( $sinfo['userId'], $info['userId'], "Correct user ID." );
63 $this->assertEquals(
64 $sinfo['ip'],
65 $context->getRequest()->getIP(),
66 "Correct context IP address."
67 );
68 $this->assertEquals(
69 $sinfo['headers'],
70 $context->getRequest()->getAllHeaders(),
71 "Correct context headers."
72 );
73 $this->assertEquals( $sinfo['sessionId'], session_id(), "Correct context session ID." );
74 $this->assertEquals( true, $context->getUser()->isLoggedIn(), "Correct context user." );
75 $this->assertEquals( $sinfo['userId'], $context->getUser()->getId(), "Correct context user ID." );
76 $this->assertEquals(
77 'UnitTestContextUser',
78 $context->getUser()->getName(),
79 "Correct context user name."
80 );
81
82 unset( $sc ); // restore previous context
83
84 $info = $context->exportSession();
85 $this->assertEquals( $oInfo['ip'], $info['ip'], "Correct initial IP address." );
86 $this->assertEquals( $oInfo['headers'], $info['headers'], "Correct initial headers." );
87 $this->assertEquals( $oInfo['sessionId'], $info['sessionId'], "Correct initial session ID." );
88 $this->assertEquals( $oInfo['userId'], $info['userId'], "Correct initial user ID." );
89 }
90 }