e3a9cfb1afe3e080ef8f9ad4fb6f69e8a3674c9f
[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 */
11 public function testWikiPageTitle() {
12 $context = new RequestContext();
13
14 $curTitle = Title::newFromText( "A" );
15 $context->setTitle( $curTitle );
16 $this->assertTrue( $curTitle->equals( $context->getWikiPage()->getTitle() ),
17 "When a title is first set WikiPage should be created on-demand for that title." );
18
19 $curTitle = Title::newFromText( "B" );
20 $context->setWikiPage( WikiPage::factory( $curTitle ) );
21 $this->assertTrue( $curTitle->equals( $context->getTitle() ),
22 "Title must be updated when a new WikiPage is provided." );
23
24 $curTitle = Title::newFromText( "C" );
25 $context->setTitle( $curTitle );
26 $this->assertTrue( $curTitle->equals( $context->getWikiPage()->getTitle() ),
27 "When a title is updated the WikiPage should be purged and recreated on-demand with the new title." );
28 }
29
30 public function testImportScopedSession() {
31 $context = RequestContext::getMain();
32
33 $oInfo = $context->exportSession();
34 $this->assertEquals( '127.0.0.1', $oInfo['ip'], "Correct initial IP address." );
35 $this->assertEquals( 0, $oInfo['userId'], "Correct initial user ID." );
36
37 $user = User::newFromName( 'UnitTestContextUser' );
38 $user->addToDatabase();
39
40 $sinfo = array(
41 'sessionId' => 'd612ee607c87e749ef14da4983a702cd',
42 'userId' => $user->getId(),
43 'ip' => '192.0.2.0',
44 'headers' => array( 'USER-AGENT' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0' )
45 );
46 $sc = RequestContext::importScopedSession( $sinfo ); // load new context
47
48 $info = $context->exportSession();
49 $this->assertEquals( $sinfo['ip'], $info['ip'], "Correct IP address." );
50 $this->assertEquals( $sinfo['headers'], $info['headers'], "Correct headers." );
51 $this->assertEquals( $sinfo['sessionId'], $info['sessionId'], "Correct session ID." );
52 $this->assertEquals( $sinfo['userId'], $info['userId'], "Correct user ID." );
53 $this->assertEquals( $sinfo['ip'], $context->getRequest()->getIP(), "Correct context IP address." );
54 $this->assertEquals( $sinfo['headers'], $context->getRequest()->getAllHeaders(), "Correct context headers." );
55 $this->assertEquals( $sinfo['sessionId'], session_id(), "Correct context session ID." );
56 $this->assertEquals( true, $context->getUser()->isLoggedIn(), "Correct context user." );
57 $this->assertEquals( $sinfo['userId'], $context->getUser()->getId(), "Correct context user ID." );
58 $this->assertEquals( 'UnitTestContextUser', $context->getUser()->getName(), "Correct context user name." );
59
60 unset( $sc ); // restore previous context
61
62 $info = $context->exportSession();
63 $this->assertEquals( $oInfo['ip'], $info['ip'], "Correct initial IP address." );
64 $this->assertEquals( $oInfo['headers'], $info['headers'], "Correct initial headers." );
65 $this->assertEquals( $oInfo['sessionId'], $info['sessionId'], "Correct initial session ID." );
66 $this->assertEquals( $oInfo['userId'], $info['userId'], "Correct initial user ID." );
67 }
68 }