summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/RequestContextTest.php
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2016-05-01 15:17:42 -0400
committerLuke Shumaker <lukeshu@sbcglobal.net>2016-05-01 15:17:42 -0400
commitf7d4cf9ed0ae68fec630d14e8f6aade38e49f036 (patch)
treea730c57badbe0e2f0f064ca2006c82d4b6ed54ea /tests/phpunit/includes/RequestContextTest.php
parentaee35e4a93d105024bcae947cd8b16c962191f5c (diff)
parent5d1e7dd0ccda0984ccf3e8e3d0f88ac888b05819 (diff)
Merge commit '5d1e7'
Diffstat (limited to 'tests/phpunit/includes/RequestContextTest.php')
-rw-r--r--tests/phpunit/includes/RequestContextTest.php96
1 files changed, 96 insertions, 0 deletions
diff --git a/tests/phpunit/includes/RequestContextTest.php b/tests/phpunit/includes/RequestContextTest.php
new file mode 100644
index 00000000..cae0e52e
--- /dev/null
+++ b/tests/phpunit/includes/RequestContextTest.php
@@ -0,0 +1,96 @@
+<?php
+
+/**
+ * @group Database
+ * @group RequestContext
+ */
+class RequestContextTest extends MediaWikiTestCase {
+
+ /**
+ * Test the relationship between title and wikipage in RequestContext
+ * @covers RequestContext::getWikiPage
+ * @covers RequestContext::getTitle
+ */
+ public function testWikiPageTitle() {
+ $context = new RequestContext();
+
+ $curTitle = Title::newFromText( "A" );
+ $context->setTitle( $curTitle );
+ $this->assertTrue( $curTitle->equals( $context->getWikiPage()->getTitle() ),
+ "When a title is first set WikiPage should be created on-demand for that title." );
+
+ $curTitle = Title::newFromText( "B" );
+ $context->setWikiPage( WikiPage::factory( $curTitle ) );
+ $this->assertTrue( $curTitle->equals( $context->getTitle() ),
+ "Title must be updated when a new WikiPage is provided." );
+
+ $curTitle = Title::newFromText( "C" );
+ $context->setTitle( $curTitle );
+ $this->assertTrue(
+ $curTitle->equals( $context->getWikiPage()->getTitle() ),
+ "When a title is updated the WikiPage should be purged "
+ . "and recreated on-demand with the new title."
+ );
+ }
+
+ /**
+ * @covers RequestContext::importScopedSession
+ */
+ public function testImportScopedSession() {
+ $context = RequestContext::getMain();
+
+ $oInfo = $context->exportSession();
+ $this->assertEquals( '127.0.0.1', $oInfo['ip'], "Correct initial IP address." );
+ $this->assertEquals( 0, $oInfo['userId'], "Correct initial user ID." );
+
+ $user = User::newFromName( 'UnitTestContextUser' );
+ $user->addToDatabase();
+
+ $sinfo = array(
+ 'sessionId' => 'd612ee607c87e749ef14da4983a702cd',
+ 'userId' => $user->getId(),
+ 'ip' => '192.0.2.0',
+ 'headers' => array(
+ 'USER-AGENT' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0'
+ )
+ );
+ // importScopedSession() sets these variables
+ $this->setMwGlobals( array(
+ 'wgUser' => new User,
+ 'wgRequest' => new FauxRequest,
+ ) );
+ $sc = RequestContext::importScopedSession( $sinfo ); // load new context
+
+ $info = $context->exportSession();
+ $this->assertEquals( $sinfo['ip'], $info['ip'], "Correct IP address." );
+ $this->assertEquals( $sinfo['headers'], $info['headers'], "Correct headers." );
+ $this->assertEquals( $sinfo['sessionId'], $info['sessionId'], "Correct session ID." );
+ $this->assertEquals( $sinfo['userId'], $info['userId'], "Correct user ID." );
+ $this->assertEquals(
+ $sinfo['ip'],
+ $context->getRequest()->getIP(),
+ "Correct context IP address."
+ );
+ $this->assertEquals(
+ $sinfo['headers'],
+ $context->getRequest()->getAllHeaders(),
+ "Correct context headers."
+ );
+ $this->assertEquals( $sinfo['sessionId'], session_id(), "Correct context session ID." );
+ $this->assertEquals( true, $context->getUser()->isLoggedIn(), "Correct context user." );
+ $this->assertEquals( $sinfo['userId'], $context->getUser()->getId(), "Correct context user ID." );
+ $this->assertEquals(
+ 'UnitTestContextUser',
+ $context->getUser()->getName(),
+ "Correct context user name."
+ );
+
+ unset( $sc ); // restore previous context
+
+ $info = $context->exportSession();
+ $this->assertEquals( $oInfo['ip'], $info['ip'], "Correct initial IP address." );
+ $this->assertEquals( $oInfo['headers'], $info['headers'], "Correct initial headers." );
+ $this->assertEquals( $oInfo['sessionId'], $info['sessionId'], "Correct initial session ID." );
+ $this->assertEquals( $oInfo['userId'], $info['userId'], "Correct initial user ID." );
+ }
+}