MediaWiki  1.30.0
RequestContextTest.php
Go to the documentation of this file.
1 <?php
2 
8 
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 
39  public function testImportScopedSession() {
40  // Make sure session handling is started
41  if ( !MediaWiki\Session\PHPSessionHandler::isInstalled() ) {
43  MediaWiki\Session\SessionManager::singleton()
44  );
45  }
46  $oldSessionId = session_id();
47 
49 
50  $oInfo = $context->exportSession();
51  $this->assertEquals( '127.0.0.1', $oInfo['ip'], "Correct initial IP address." );
52  $this->assertEquals( 0, $oInfo['userId'], "Correct initial user ID." );
53  $this->assertFalse( MediaWiki\Session\SessionManager::getGlobalSession()->isPersistent(),
54  'Global session isn\'t persistent to start' );
55 
56  $user = User::newFromName( 'UnitTestContextUser' );
57  $user->addToDatabase();
58 
59  $sinfo = [
60  'sessionId' => 'd612ee607c87e749ef14da4983a702cd',
61  'userId' => $user->getId(),
62  'ip' => '192.0.2.0',
63  'headers' => [
64  'USER-AGENT' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0'
65  ]
66  ];
67  // importScopedSession() sets these variables
68  $this->setMwGlobals( [
69  'wgUser' => new User,
70  'wgRequest' => new FauxRequest,
71  ] );
72  $sc = RequestContext::importScopedSession( $sinfo ); // load new context
73 
74  $info = $context->exportSession();
75  $this->assertEquals( $sinfo['ip'], $info['ip'], "Correct IP address." );
76  $this->assertEquals( $sinfo['headers'], $info['headers'], "Correct headers." );
77  $this->assertEquals( $sinfo['sessionId'], $info['sessionId'], "Correct session ID." );
78  $this->assertEquals( $sinfo['userId'], $info['userId'], "Correct user ID." );
79  $this->assertEquals(
80  $sinfo['ip'],
81  $context->getRequest()->getIP(),
82  "Correct context IP address."
83  );
84  $this->assertEquals(
85  $sinfo['headers'],
86  $context->getRequest()->getAllHeaders(),
87  "Correct context headers."
88  );
89  $this->assertEquals(
90  $sinfo['sessionId'],
91  MediaWiki\Session\SessionManager::getGlobalSession()->getId(),
92  "Correct context session ID."
93  );
94  if ( \MediaWiki\Session\PHPSessionHandler::isEnabled() ) {
95  $this->assertEquals( $sinfo['sessionId'], session_id(), "Correct context session ID." );
96  } else {
97  $this->assertEquals( $oldSessionId, session_id(), "Unchanged PHP session ID." );
98  }
99  $this->assertEquals( true, $context->getUser()->isLoggedIn(), "Correct context user." );
100  $this->assertEquals( $sinfo['userId'], $context->getUser()->getId(), "Correct context user ID." );
101  $this->assertEquals(
102  'UnitTestContextUser',
103  $context->getUser()->getName(),
104  "Correct context user name."
105  );
106 
107  unset( $sc ); // restore previous context
108 
109  $info = $context->exportSession();
110  $this->assertEquals( $oInfo['ip'], $info['ip'], "Correct restored IP address." );
111  $this->assertEquals( $oInfo['headers'], $info['headers'], "Correct restored headers." );
112  $this->assertEquals( $oInfo['sessionId'], $info['sessionId'], "Correct restored session ID." );
113  $this->assertEquals( $oInfo['userId'], $info['userId'], "Correct restored user ID." );
114  $this->assertFalse( MediaWiki\Session\SessionManager::getGlobalSession()->isPersistent(),
115  'Global session isn\'t persistent after restoring the context' );
116  }
117 }
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:244
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
MediaWiki\Session\PHPSessionHandler\install
static install(SessionManager $manager)
Install a session handler for the current web request.
Definition: PHPSessionHandler.php:108
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:268
$context
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition: hooks.txt:2581
MediaWiki\getTitle
getTitle()
Get the Title object that we'll be acting on, as specified in the WebRequest.
Definition: MediaWiki.php:137
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:550
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:121
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:672
MediaWikiTestCase
Definition: MediaWikiTestCase.php:15
MediaWiki
A helper class for throttling authentication attempts.
RequestContext
Group all the pieces relevant to the context of a request into one instance.
Definition: RequestContext.php:33
RequestContextTest\testImportScopedSession
testImportScopedSession()
RequestContext::importScopedSession.
Definition: RequestContextTest.php:39
RequestContext\getMain
static getMain()
Static methods.
Definition: RequestContext.php:470
RequestContext\importScopedSession
static importScopedSession(array $params)
Import an client IP address, HTTP headers, user ID, and session ID.
Definition: RequestContext.php:542
RequestContextTest\testWikiPageTitle
testWikiPageTitle()
Test the relationship between title and wikipage in RequestContext RequestContext::getWikiPage Reques...
Definition: RequestContextTest.php:14
RequestContextTest
Database RequestContext.
Definition: RequestContextTest.php:7
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51