Merge "Provide direction hinting in the personal toolbar"
[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( $curTitle->equals( $context->getWikiPage()->getTitle() ),
29 "When a title is updated the WikiPage should be purged and recreated on-demand with the new title." );
30 }
31
32 /**
33 * @covers RequestContext::importScopedSession
34 */
35 public function testImportScopedSession() {
36 $context = RequestContext::getMain();
37
38 $oInfo = $context->exportSession();
39 $this->assertEquals( '127.0.0.1', $oInfo['ip'], "Correct initial IP address." );
40 $this->assertEquals( 0, $oInfo['userId'], "Correct initial user ID." );
41
42 $user = User::newFromName( 'UnitTestContextUser' );
43 $user->addToDatabase();
44
45 $sinfo = array(
46 'sessionId' => 'd612ee607c87e749ef14da4983a702cd',
47 'userId' => $user->getId(),
48 'ip' => '192.0.2.0',
49 'headers' => array( 'USER-AGENT' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0' )
50 );
51 $sc = RequestContext::importScopedSession( $sinfo ); // load new context
52
53 $info = $context->exportSession();
54 $this->assertEquals( $sinfo['ip'], $info['ip'], "Correct IP address." );
55 $this->assertEquals( $sinfo['headers'], $info['headers'], "Correct headers." );
56 $this->assertEquals( $sinfo['sessionId'], $info['sessionId'], "Correct session ID." );
57 $this->assertEquals( $sinfo['userId'], $info['userId'], "Correct user ID." );
58 $this->assertEquals( $sinfo['ip'], $context->getRequest()->getIP(), "Correct context IP address." );
59 $this->assertEquals( $sinfo['headers'], $context->getRequest()->getAllHeaders(), "Correct context headers." );
60 $this->assertEquals( $sinfo['sessionId'], session_id(), "Correct context session ID." );
61 $this->assertEquals( true, $context->getUser()->isLoggedIn(), "Correct context user." );
62 $this->assertEquals( $sinfo['userId'], $context->getUser()->getId(), "Correct context user ID." );
63 $this->assertEquals( 'UnitTestContextUser', $context->getUser()->getName(), "Correct context user name." );
64
65 unset( $sc ); // restore previous context
66
67 $info = $context->exportSession();
68 $this->assertEquals( $oInfo['ip'], $info['ip'], "Correct initial IP address." );
69 $this->assertEquals( $oInfo['headers'], $info['headers'], "Correct initial headers." );
70 $this->assertEquals( $oInfo['sessionId'], $info['sessionId'], "Correct initial session ID." );
71 $this->assertEquals( $oInfo['userId'], $info['userId'], "Correct initial user ID." );
72 }
73 }