| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Integration\Context; |
| 4 |
|
| 5 |
use LogicException; |
| 6 |
use MediaWiki\Actions\ActionFactory; |
| 7 |
use MediaWiki\Context\RequestContext; |
| 8 |
use MediaWiki\MainConfigNames; |
| 9 |
use MediaWiki\Permissions\UltimateAuthority; |
| 10 |
use MediaWiki\Request\FauxRequest; |
| 11 |
use MediaWiki\Session\PHPSessionHandler; |
| 12 |
use MediaWiki\Session\SessionManager; |
| 13 |
use MediaWiki\Title\Title; |
| 14 |
use MediaWiki\User\Options\StaticUserOptionsLookup; |
| 15 |
use MediaWiki\User\User; |
| 16 |
use MediaWiki\User\UserIdentityValue; |
| 17 |
use MediaWikiIntegrationTestCase; |
| 18 |
use Skin; |
| 19 |
use SkinFallback; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class RequestContextTest extends MediaWikiIntegrationTestCase { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public function testSanitizeLangCode( |
| 34 |
?string $input, string $expected |
| 35 |
): void { |
| 36 |
$this->assertSame( |
| 37 |
$expected, |
| 38 |
RequestContext::sanitizeLangCode( $input ) |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
public static function provideSanitizeLangCode() { |
| 43 |
global $wgLanguageCode; |
| 44 |
|
| 45 |
yield 'Null' => [ null, $wgLanguageCode ]; |
| 46 |
yield 'Blank' => [ '', $wgLanguageCode ]; |
| 47 |
|
| 48 |
yield 'Current' => [ $wgLanguageCode, $wgLanguageCode ]; |
| 49 |
|
| 50 |
yield 'Non-English' => [ 'fr', 'fr' ]; |
| 51 |
yield 'Documentation falls back to default' => [ 'qqq', $wgLanguageCode ]; |
| 52 |
|
| 53 |
yield 'Sub-codes' => [ 'fr-fr', 'fr-fr' ]; |
| 54 |
|
| 55 |
yield 'Lower-casing' => [ 'en-GB', 'en-gb' ]; |
| 56 |
|
| 57 |
yield 'Valid codes unknown to MW' => [ 'zzz', 'zzz' ]; |
| 58 |
yield 'Valid sub-codes unknown to MW' => [ 'en-IN', 'en-in' ]; |
| 59 |
yield 'Extended codes' => [ 'en-US-aave', 'en-us-aave' ]; |
| 60 |
|
| 61 |
yield 'Invalid code' => [ 'z!!z', 'z!!z' ]; |
| 62 |
|
| 63 |
yield 'Attempted XSS code' => [ 'a�', $wgLanguageCode ]; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function testWikiPageTitle() { |
| 72 |
$context = new RequestContext(); |
| 73 |
|
| 74 |
$curTitle = Title::makeTitle( NS_MAIN, "A" ); |
| 75 |
$context->setTitle( $curTitle ); |
| 76 |
$this->assertTrue( $curTitle->equals( $context->getWikiPage()->getTitle() ), |
| 77 |
"When a title is first set WikiPage should be created on-demand for that title." ); |
| 78 |
|
| 79 |
$curTitle = Title::makeTitle( NS_MAIN, "B" ); |
| 80 |
$context->setWikiPage( $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $curTitle ) ); |
| 81 |
$this->assertTrue( $curTitle->equals( $context->getTitle() ), |
| 82 |
"Title must be updated when a new WikiPage is provided." ); |
| 83 |
|
| 84 |
$curTitle = Title::makeTitle( NS_MAIN, "C" ); |
| 85 |
$context->setTitle( $curTitle ); |
| 86 |
$this->assertTrue( |
| 87 |
$curTitle->equals( $context->getWikiPage()->getTitle() ), |
| 88 |
"When a title is updated the WikiPage should be purged " |
| 89 |
. "and recreated on-demand with the new title." |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public function testImportScopedSession() { |
| 97 |
|
| 98 |
if ( !PHPSessionHandler::isInstalled() ) { |
| 99 |
PHPSessionHandler::install( |
| 100 |
SessionManager::singleton() |
| 101 |
); |
| 102 |
} |
| 103 |
$oldSessionId = session_id(); |
| 104 |
|
| 105 |
$context = RequestContext::getMain(); |
| 106 |
|
| 107 |
$oInfo = $context->exportSession(); |
| 108 |
$this->assertEquals( '127.0.0.1', $oInfo['ip'], "Correct initial IP address." ); |
| 109 |
$this->assertSame( 0, $oInfo['userId'], "Correct initial user ID." ); |
| 110 |
$this->assertFalse( SessionManager::getGlobalSession()->isPersistent(), |
| 111 |
'Global session isn\'t persistent to start' ); |
| 112 |
|
| 113 |
$user = User::newFromName( 'UnitTestContextUser' ); |
| 114 |
$user->addToDatabase(); |
| 115 |
|
| 116 |
$sinfo = [ |
| 117 |
'sessionId' => 'd612ee607c87e749ef14da4983a702cd', |
| 118 |
'userId' => $user->getId(), |
| 119 |
'ip' => '192.0.2.0', |
| 120 |
'headers' => [ |
| 121 |
'USER-AGENT' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0' |
| 122 |
] |
| 123 |
]; |
| 124 |
|
| 125 |
$this->setMwGlobals( [ |
| 126 |
'wgRequest' => new FauxRequest, |
| 127 |
] ); |
| 128 |
$sc = RequestContext::importScopedSession( $sinfo ); |
| 129 |
|
| 130 |
$info = $context->exportSession(); |
| 131 |
$this->assertEquals( $sinfo['ip'], $info['ip'], "Correct IP address." ); |
| 132 |
$this->assertEquals( $sinfo['headers'], $info['headers'], "Correct headers." ); |
| 133 |
$this->assertEquals( $sinfo['sessionId'], $info['sessionId'], "Correct session ID." ); |
| 134 |
$this->assertEquals( $sinfo['userId'], $info['userId'], "Correct user ID." ); |
| 135 |
$this->assertEquals( |
| 136 |
$sinfo['ip'], |
| 137 |
$context->getRequest()->getIP(), |
| 138 |
"Correct context IP address." |
| 139 |
); |
| 140 |
$this->assertEquals( |
| 141 |
$sinfo['headers'], |
| 142 |
$context->getRequest()->getAllHeaders(), |
| 143 |
"Correct context headers." |
| 144 |
); |
| 145 |
$this->assertEquals( |
| 146 |
$sinfo['sessionId'], |
| 147 |
SessionManager::getGlobalSession()->getId(), |
| 148 |
"Correct context session ID." |
| 149 |
); |
| 150 |
if ( \MediaWiki\Session\PHPSessionHandler::isEnabled() ) { |
| 151 |
$this->assertEquals( $sinfo['sessionId'], session_id(), "Correct context session ID." ); |
| 152 |
} else { |
| 153 |
$this->assertEquals( $oldSessionId, session_id(), "Unchanged PHP session ID." ); |
| 154 |
} |
| 155 |
$this->assertTrue( $context->getUser()->isRegistered(), "Correct context user." ); |
| 156 |
$this->assertEquals( $sinfo['userId'], $context->getUser()->getId(), "Correct context user ID." ); |
| 157 |
$this->assertEquals( |
| 158 |
'UnitTestContextUser', |
| 159 |
$context->getUser()->getName(), |
| 160 |
"Correct context user name." |
| 161 |
); |
| 162 |
|
| 163 |
unset( $sc ); |
| 164 |
|
| 165 |
$info = $context->exportSession(); |
| 166 |
$this->assertEquals( $oInfo['ip'], $info['ip'], "Correct restored IP address." ); |
| 167 |
$this->assertEquals( $oInfo['headers'], $info['headers'], "Correct restored headers." ); |
| 168 |
$this->assertEquals( $oInfo['sessionId'], $info['sessionId'], "Correct restored session ID." ); |
| 169 |
$this->assertEquals( $oInfo['userId'], $info['userId'], "Correct restored user ID." ); |
| 170 |
$this->assertFalse( SessionManager::getGlobalSession()->isPersistent(), |
| 171 |
'Global session isn\'t persistent after restoring the context' ); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public function testTestGetSetAuthority() { |
| 181 |
$context = new RequestContext(); |
| 182 |
|
| 183 |
$user = $this->getTestUser()->getUser(); |
| 184 |
|
| 185 |
$context->setUser( $user ); |
| 186 |
$this->assertTrue( $user->equals( $context->getAuthority()->getUser() ) ); |
| 187 |
$this->assertTrue( $user->equals( $context->getUser() ) ); |
| 188 |
|
| 189 |
$authorityActor = new UserIdentityValue( 42, 'Test' ); |
| 190 |
$authority = new UltimateAuthority( $authorityActor ); |
| 191 |
|
| 192 |
$context->setAuthority( $authority ); |
| 193 |
$this->assertTrue( $context->getUser()->equals( $authorityActor ) ); |
| 194 |
$this->assertTrue( $context->getAuthority()->getUser()->equals( $authorityActor ) ); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public function testGetActionName() { |
| 201 |
$factory = $this->createMock( ActionFactory::class ); |
| 202 |
$factory |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
->expects( $this->once() ) |
| 207 |
->method( 'getActionName' ) |
| 208 |
->willReturn( 'foo' ); |
| 209 |
$this->setService( 'ActionFactory', $factory ); |
| 210 |
|
| 211 |
$context = new RequestContext(); |
| 212 |
$this->assertSame( 'foo', $context->getActionName(), 'value from factory' ); |
| 213 |
$this->assertSame( 'foo', $context->getActionName(), 'cached' ); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public function testSetActionName() { |
| 220 |
$factory = $this->createMock( ActionFactory::class ); |
| 221 |
$factory |
| 222 |
->expects( $this->never() ) |
| 223 |
->method( 'getActionName' ); |
| 224 |
$this->setService( 'ActionFactory', $factory ); |
| 225 |
|
| 226 |
$context = new RequestContext(); |
| 227 |
$context->setActionName( 'fixed' ); |
| 228 |
$this->assertSame( 'fixed', $context->getActionName() ); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
public function testOverideActionName() { |
| 235 |
$factory = $this->createMock( ActionFactory::class ); |
| 236 |
$factory |
| 237 |
->method( 'getActionName' ) |
| 238 |
->willReturnOnConsecutiveCalls( 'aaa', 'bbb' ); |
| 239 |
$this->setService( 'ActionFactory', $factory ); |
| 240 |
|
| 241 |
$context = new RequestContext(); |
| 242 |
$this->assertSame( 'aaa', $context->getActionName(), 'first from factory' ); |
| 243 |
$this->assertSame( 'aaa', $context->getActionName(), 'cached first' ); |
| 244 |
|
| 245 |
|
| 246 |
@$context->setTitle( $this->createMock( Title::class ) ); |
| 247 |
$this->assertSame( 'bbb', $context->getActionName(), 'second from factory' ); |
| 248 |
$this->assertSame( 'bbb', $context->getActionName(), 'cached second' ); |
| 249 |
} |
| 250 |
|
| 251 |
private function registerTestSkin() { |
| 252 |
$skin = $this->createMock( Skin::class ); |
| 253 |
|
| 254 |
$skinFactory = $this->getServiceContainer()->getSkinFactory(); |
| 255 |
$skinFactory->register( 'test', 'test', |
| 256 |
static function () use ( $skin ) { |
| 257 |
return $skin; |
| 258 |
} |
| 259 |
); |
| 260 |
return $skin; |
| 261 |
} |
| 262 |
|
| 263 |
public function testGetSkinFromDefault() { |
| 264 |
$this->overrideConfigValue( MainConfigNames::DefaultSkin, 'test' ); |
| 265 |
$skin = $this->registerTestSkin(); |
| 266 |
$context = new RequestContext(); |
| 267 |
$this->assertSame( $skin, $context->getSkin() ); |
| 268 |
} |
| 269 |
|
| 270 |
public function testGetSkinFromPref() { |
| 271 |
$optionsLookup = new StaticUserOptionsLookup( [], [ 'skin' => 'test' ] ); |
| 272 |
$this->setService( 'UserOptionsLookup', $optionsLookup ); |
| 273 |
|
| 274 |
$skin = $this->registerTestSkin(); |
| 275 |
|
| 276 |
$context = new RequestContext(); |
| 277 |
$this->assertSame( $skin, $context->getSkin() ); |
| 278 |
} |
| 279 |
|
| 280 |
public function testGetSkinFromStringHook() { |
| 281 |
$skin = $this->registerTestSkin(); |
| 282 |
$this->setTemporaryHook( |
| 283 |
'RequestContextCreateSkin', |
| 284 |
static function ( $context, &$skin ) { |
| 285 |
$skin = 'test'; |
| 286 |
} |
| 287 |
); |
| 288 |
$context = new RequestContext(); |
| 289 |
$this->assertSame( $skin, $context->getSkin() ); |
| 290 |
} |
| 291 |
|
| 292 |
public function testGetSkinFromObjectHook() { |
| 293 |
$skin = $this->createMock( Skin::class ); |
| 294 |
$this->setTemporaryHook( |
| 295 |
'RequestContextCreateSkin', |
| 296 |
static function ( $context, &$skinRes ) use ( $skin ) { |
| 297 |
$skinRes = $skin; |
| 298 |
} |
| 299 |
); |
| 300 |
$context = new RequestContext(); |
| 301 |
$this->assertSame( $skin, $context->getSkin() ); |
| 302 |
} |
| 303 |
|
| 304 |
public function testGetSkinFromBadPrefs() { |
| 305 |
|
| 306 |
$this->overrideConfigValue( MainConfigNames::DefaultSkin, 'test' ); |
| 307 |
$optionsLookup = new StaticUserOptionsLookup( [], [ 'skin' => '' ] ); |
| 308 |
$this->setService( 'UserOptionsLookup', $optionsLookup ); |
| 309 |
$skin = $this->registerTestSkin(); |
| 310 |
|
| 311 |
$context = new RequestContext(); |
| 312 |
$this->assertSame( $skin, $context->getSkin() ); |
| 313 |
} |
| 314 |
|
| 315 |
public function testGetSkinFromBadDefault() { |
| 316 |
$this->overrideConfigValues( [ |
| 317 |
MainConfigNames::DefaultSkin => 'nonexistent', |
| 318 |
MainConfigNames::HiddenPrefs => [ 'skin' ] |
| 319 |
] ); |
| 320 |
$context = new RequestContext(); |
| 321 |
$this->assertInstanceOf( SkinFallback::class, $context->getSkin() ); |
| 322 |
} |
| 323 |
|
| 324 |
public function testCloningNotAllowed() { |
| 325 |
$context = RequestContext::getMain(); |
| 326 |
$this->expectException( LogicException::class ); |
| 327 |
|
| 328 |
clone $context; |
| 329 |
} |
| 330 |
} |