| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Auth\AuthManager; |
| 4 |
use MediaWiki\Config\Config; |
| 5 |
use MediaWiki\Config\ServiceOptions; |
| 6 |
use MediaWiki\Context\IContextSource; |
| 7 |
use MediaWiki\Context\RequestContext; |
| 8 |
use MediaWiki\HookContainer\HookContainer; |
| 9 |
use MediaWiki\HookContainer\HookRunner; |
| 10 |
use MediaWiki\Language\ILanguageConverter; |
| 11 |
use MediaWiki\Language\Language; |
| 12 |
use MediaWiki\Languages\LanguageConverterFactory; |
| 13 |
use MediaWiki\Languages\LanguageNameUtils; |
| 14 |
use MediaWiki\Linker\LinkRenderer; |
| 15 |
use MediaWiki\MainConfigNames; |
| 16 |
use MediaWiki\MediaWikiServices; |
| 17 |
use MediaWiki\Parser\ParserFactory; |
| 18 |
use MediaWiki\Permissions\PermissionManager; |
| 19 |
use MediaWiki\Preferences\DefaultPreferencesFactory; |
| 20 |
use MediaWiki\Preferences\SignatureValidatorFactory; |
| 21 |
use MediaWiki\Request\FauxRequest; |
| 22 |
use MediaWiki\Session\SessionId; |
| 23 |
use MediaWiki\Tests\Session\TestUtils; |
| 24 |
use MediaWiki\Tests\Unit\DummyServicesTrait; |
| 25 |
use MediaWiki\Title\NamespaceInfo; |
| 26 |
use MediaWiki\Title\Title; |
| 27 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 28 |
use MediaWiki\User\Options\UserOptionsManager; |
| 29 |
use MediaWiki\User\User; |
| 30 |
use MediaWiki\User\UserGroupManager; |
| 31 |
use MediaWiki\User\UserGroupMembership; |
| 32 |
use MediaWiki\User\UserIdentity; |
| 33 |
use PHPUnit\Framework\MockObject\MockObject; |
| 34 |
use Wikimedia\TestingAccessWrapper; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
class DefaultPreferencesFactoryTest extends \MediaWikiIntegrationTestCase { |
| 61 |
use DummyServicesTrait; |
| 62 |
use TestAllServiceOptionsUsed; |
| 63 |
|
| 64 |
|
| 65 |
protected $context; |
| 66 |
|
| 67 |
|
| 68 |
protected $config; |
| 69 |
|
| 70 |
protected function setUp(): void { |
| 71 |
parent::setUp(); |
| 72 |
$this->context = new RequestContext(); |
| 73 |
$this->context->setTitle( Title::makeTitle( NS_MAIN, self::class ) ); |
| 74 |
|
| 75 |
$this->overrideConfigValues( [ |
| 76 |
MainConfigNames::DisableLangConversion => false, |
| 77 |
MainConfigNames::UsePigLatinVariant => false, |
| 78 |
] ); |
| 79 |
$this->config = $this->getServiceContainer()->getMainConfig(); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public function testConstruct() { |
| 86 |
|
| 87 |
|
| 88 |
$params = [ |
| 89 |
$this->createMock( ServiceOptions::class ), |
| 90 |
$this->createMock( Language::class ), |
| 91 |
$this->createMock( AuthManager::class ), |
| 92 |
$this->createMock( LinkRenderer::class ), |
| 93 |
$this->createMock( NamespaceInfo::class ), |
| 94 |
$this->createMock( PermissionManager::class ), |
| 95 |
$this->createMock( ILanguageConverter::class ), |
| 96 |
$this->createMock( LanguageNameUtils::class ), |
| 97 |
$this->createMock( HookContainer::class ), |
| 98 |
$this->createMock( UserOptionsLookup::class ), |
| 99 |
]; |
| 100 |
$preferencesFactory = new DefaultPreferencesFactory( ...$params ); |
| 101 |
$this->assertInstanceOf( |
| 102 |
DefaultPreferencesFactory::class, |
| 103 |
$preferencesFactory, |
| 104 |
'Created with some services missing' |
| 105 |
); |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
$params[9] = $this->createMock( UserOptionsManager::class ); |
| 110 |
$params[] = $this->createMock( LanguageConverterFactory::class ); |
| 111 |
$params[] = $this->createMock( ParserFactory::class ); |
| 112 |
$params[] = $this->createMock( SkinFactory::class ); |
| 113 |
$params[] = $this->createMock( UserGroupManager::class ); |
| 114 |
$params[] = $this->createMock( SignatureValidatorFactory::class ); |
| 115 |
$oldMwServices = MediaWikiServices::forceGlobalInstance( |
| 116 |
$this->createNoOpMock( MediaWikiServices::class ) |
| 117 |
); |
| 118 |
|
| 119 |
|
| 120 |
try { |
| 121 |
$preferencesFactory = new DefaultPreferencesFactory( ...$params ); |
| 122 |
$this->assertInstanceOf( |
| 123 |
DefaultPreferencesFactory::class, |
| 124 |
$preferencesFactory, |
| 125 |
'Created with all services, MediaWikiServices not used' |
| 126 |
); |
| 127 |
} finally { |
| 128 |
|
| 129 |
MediaWikiServices::forceGlobalInstance( $oldMwServices ); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
protected function getPreferencesFactory( array $options = [] ) { |
| 143 |
$nsInfo = $this->getDummyNamespaceInfo(); |
| 144 |
|
| 145 |
$services = $this->getServiceContainer(); |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
$permissionManager = $this->createNoOpMock( PermissionManager::class ); |
| 151 |
|
| 152 |
$language = $options['language'] ?? $services->getContentLanguage(); |
| 153 |
$userOptionsManager = $options['userOptionsManager'] ?? $services->getUserOptionsManager(); |
| 154 |
|
| 155 |
$userGroupManager = $options['userGroupManager'] ?? false; |
| 156 |
if ( !$userGroupManager ) { |
| 157 |
$userGroupManager = $this->createMock( UserGroupManager::class ); |
| 158 |
$userGroupManager->method( 'getUserGroupMemberships' )->willReturn( [] ); |
| 159 |
$userGroupManager->method( 'getUserEffectiveGroups' )->willReturnCallback( |
| 160 |
static function ( UserIdentity $user ) { |
| 161 |
return $user->isRegistered() ? [ '*', 'user' ] : [ '*' ]; |
| 162 |
} |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
return new DefaultPreferencesFactory( |
| 167 |
new LoggedServiceOptions( self::$serviceOptionsAccessLog, |
| 168 |
DefaultPreferencesFactory::CONSTRUCTOR_OPTIONS, $this->config ), |
| 169 |
$language, |
| 170 |
$services->getAuthManager(), |
| 171 |
$services->getLinkRenderer(), |
| 172 |
$nsInfo, |
| 173 |
$permissionManager, |
| 174 |
$services->getLanguageConverterFactory()->getLanguageConverter( $language ), |
| 175 |
$services->getLanguageNameUtils(), |
| 176 |
$services->getHookContainer(), |
| 177 |
$userOptionsManager, |
| 178 |
$services->getLanguageConverterFactory(), |
| 179 |
$services->getParserFactory(), |
| 180 |
$services->getSkinFactory(), |
| 181 |
$userGroupManager, |
| 182 |
$services->getSignatureValidatorFactory() |
| 183 |
); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public function testGetForm() { |
| 191 |
$this->setTemporaryHook( 'GetPreferences', HookContainer::NOOP ); |
| 192 |
|
| 193 |
$testUser = $this->createMock( User::class ); |
| 194 |
$prefFactory = $this->getPreferencesFactory(); |
| 195 |
$form = $prefFactory->getForm( $testUser, $this->context ); |
| 196 |
$this->assertInstanceOf( PreferencesFormOOUI::class, $form ); |
| 197 |
$this->assertCount( 6, $form->getPreferenceSections() ); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public function testSortSkinNames() { |
| 204 |
|
| 205 |
$factory = TestingAccessWrapper::newFromObject( |
| 206 |
$this->getPreferencesFactory() |
| 207 |
); |
| 208 |
$validSkinNames = [ |
| 209 |
'minerva' => 'Minerva Neue', |
| 210 |
'monobook' => 'Monobook', |
| 211 |
'cologne-blue' => 'Cologne Blue', |
| 212 |
'vector' => 'Vector', |
| 213 |
'vector-2022' => 'Vector 2022', |
| 214 |
'timeless' => 'Timeless', |
| 215 |
]; |
| 216 |
$currentSkin = 'monobook'; |
| 217 |
$preferredSkins = [ 'vector-2022', 'invalid-skin', 'vector' ]; |
| 218 |
|
| 219 |
uksort( $validSkinNames, static function ( $a, $b ) use ( $factory, $currentSkin, $preferredSkins ) { |
| 220 |
return $factory->sortSkinNames( $a, $b, $currentSkin, $preferredSkins ); |
| 221 |
} ); |
| 222 |
|
| 223 |
$this->assertArrayEquals( [ |
| 224 |
'monobook' => 'Monobook', |
| 225 |
'vector-2022' => 'Vector 2022', |
| 226 |
'vector' => 'Vector', |
| 227 |
'cologne-blue' => 'Cologne Blue', |
| 228 |
'minerva' => 'Minerva Neue', |
| 229 |
'timeless' => 'Timeless', |
| 230 |
], $validSkinNames ); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
public function testEmailAuthentication( $user, $cssClass ) { |
| 241 |
$this->overrideConfigValue( MainConfigNames::EmailAuthentication, true ); |
| 242 |
|
| 243 |
$prefs = $this->getPreferencesFactory() |
| 244 |
->getFormDescriptor( $user, $this->context ); |
| 245 |
$this->assertArrayHasKey( 'cssclass', $prefs['emailauthentication'] ); |
| 246 |
$this->assertEquals( $cssClass, $prefs['emailauthentication']['cssclass'] ); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
public function testShowRollbackConfIsHiddenForUsersWithoutRollbackRights() { |
| 253 |
$userMock = $this->createMock( User::class ); |
| 254 |
$userMock->method( 'isAllowed' )->willReturnCallback( |
| 255 |
static function ( $permission ) { |
| 256 |
return $permission === 'editmyoptions'; |
| 257 |
} |
| 258 |
); |
| 259 |
|
| 260 |
$userOptionsManagerMock = $this->createUserOptionsManagerMock( [ 'test' => 'yes' ], true ); |
| 261 |
$userMock = $this->getUserMockWithSession( $userMock ); |
| 262 |
$prefs = $this->getPreferencesFactory( [ |
| 263 |
'userOptionsManager' => $userOptionsManagerMock, |
| 264 |
] )->getFormDescriptor( $userMock, $this->context ); |
| 265 |
$this->assertArrayNotHasKey( 'showrollbackconfirmation', $prefs ); |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public function testShowRollbackConfIsShownForUsersWithRollbackRights() { |
| 272 |
$userMock = $this->createMock( User::class ); |
| 273 |
$userMock->method( 'isAllowed' )->willReturnCallback( |
| 274 |
static function ( $permission ) { |
| 275 |
return $permission === 'editmyoptions' || $permission === 'rollback'; |
| 276 |
} |
| 277 |
); |
| 278 |
$userMock = $this->getUserMockWithSession( $userMock ); |
| 279 |
|
| 280 |
$userOptionsManagerMock = $this->createUserOptionsManagerMock( [ 'test' => 'yes' ], true ); |
| 281 |
$prefs = $this->getPreferencesFactory( [ |
| 282 |
'userOptionsManager' => $userOptionsManagerMock, |
| 283 |
] )->getFormDescriptor( $userMock, $this->context ); |
| 284 |
$this->assertArrayHasKey( 'showrollbackconfirmation', $prefs ); |
| 285 |
$this->assertEquals( |
| 286 |
'rendering/advancedrendering', |
| 287 |
$prefs['showrollbackconfirmation']['section'] |
| 288 |
); |
| 289 |
} |
| 290 |
|
| 291 |
public function emailAuthenticationProvider() { |
| 292 |
$userNoEmail = new User; |
| 293 |
$userEmailUnauthed = new User; |
| 294 |
$userEmailUnauthed->setEmail( '[email protected]' ); |
| 295 |
$userEmailAuthed = new User; |
| 296 |
$userEmailAuthed->setEmail( '[email protected]' ); |
| 297 |
$userEmailAuthed->setEmailAuthenticationTimestamp( wfTimestamp() ); |
| 298 |
return [ |
| 299 |
[ $userNoEmail, 'mw-email-none' ], |
| 300 |
[ $userEmailUnauthed, 'mw-email-not-authenticated' ], |
| 301 |
[ $userEmailAuthed, 'mw-email-authenticated' ], |
| 302 |
]; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
public function testPreferencesFormPreSaveHookHasCorrectData() { |
| 315 |
$oldOptions = [ |
| 316 |
'test' => 'abc', |
| 317 |
'option' => 'old' |
| 318 |
]; |
| 319 |
$newOptions = [ |
| 320 |
'test' => 'abc', |
| 321 |
'option' => 'new' |
| 322 |
]; |
| 323 |
|
| 324 |
$this->overrideConfigValue( MainConfigNames::HiddenPrefs, [] ); |
| 325 |
|
| 326 |
$form = $this->createMock( PreferencesFormOOUI::class ); |
| 327 |
|
| 328 |
$userMock = $this->createMock( User::class ); |
| 329 |
|
| 330 |
$userOptionsManagerMock = $this->createUserOptionsManagerMock( $oldOptions ); |
| 331 |
$expectedOptions = $newOptions; |
| 332 |
$userOptionsManagerMock->expects( $this->exactly( count( $newOptions ) ) ) |
| 333 |
->method( 'setOption' ) |
| 334 |
->willReturnCallback( function ( $user, $oname, $val ) use ( $userMock, &$expectedOptions ) { |
| 335 |
$this->assertSame( $userMock, $user ); |
| 336 |
$this->assertArrayHasKey( $oname, $expectedOptions ); |
| 337 |
$this->assertSame( $expectedOptions[$oname], $val ); |
| 338 |
unset( $expectedOptions[$oname] ); |
| 339 |
} ); |
| 340 |
$userMock->method( 'isAllowed' )->willReturnCallback( |
| 341 |
static function ( $permission ) { |
| 342 |
return $permission === 'editmyprivateinfo' || $permission === 'editmyoptions'; |
| 343 |
} |
| 344 |
); |
| 345 |
$userMock->method( 'isAllowedAny' )->willReturnCallback( |
| 346 |
static function ( ...$permissions ) { |
| 347 |
foreach ( $permissions as $perm ) { |
| 348 |
if ( $perm === 'editmyprivateinfo' || $perm === 'editmyoptions' ) { |
| 349 |
return true; |
| 350 |
} |
| 351 |
} |
| 352 |
return false; |
| 353 |
} |
| 354 |
); |
| 355 |
|
| 356 |
$form->method( 'getModifiedUser' ) |
| 357 |
->willReturn( $userMock ); |
| 358 |
|
| 359 |
$form->method( 'getContext' ) |
| 360 |
->willReturn( $this->context ); |
| 361 |
|
| 362 |
$this->setTemporaryHook( 'PreferencesFormPreSave', |
| 363 |
function ( |
| 364 |
$formData, $form, $user, &$result, $oldUserOptions |
| 365 |
) use ( |
| 366 |
$newOptions, $oldOptions, $userMock |
| 367 |
) { |
| 368 |
$this->assertSame( $userMock, $user ); |
| 369 |
foreach ( $newOptions as $option => $value ) { |
| 370 |
$this->assertSame( $value, $formData[ $option ] ); |
| 371 |
} |
| 372 |
foreach ( $oldOptions as $option => $value ) { |
| 373 |
$this->assertSame( $value, $oldUserOptions[ $option ] ); |
| 374 |
} |
| 375 |
$this->assertTrue( $result ); |
| 376 |
} |
| 377 |
); |
| 378 |
|
| 379 |
|
| 380 |
$factory = TestingAccessWrapper::newFromObject( |
| 381 |
$this->getPreferencesFactory( [ 'userOptionsManager' => $userOptionsManagerMock ] ) |
| 382 |
); |
| 383 |
$factory->saveFormData( $newOptions, $form, [] ); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
public function testIntvalFilter() { |
| 392 |
|
| 393 |
$this->context->getRequest()->setVal( 'wprclimit', ' 0012 ' ); |
| 394 |
$user = new User; |
| 395 |
$prefFactory = $this->getPreferencesFactory(); |
| 396 |
$form = $prefFactory->getForm( $user, $this->context ); |
| 397 |
$form->show(); |
| 398 |
$form->trySubmit(); |
| 399 |
$userOptionsLookup = $this->getServiceContainer()->getUserOptionsLookup(); |
| 400 |
$this->assertEquals( 12, $userOptionsLookup->getOption( $user, 'rclimit' ) ); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
public function testVariantsSupport() { |
| 407 |
$userMock = $this->createMock( User::class ); |
| 408 |
$userMock->method( 'isAllowed' )->willReturn( true ); |
| 409 |
$userMock = $this->getUserMockWithSession( $userMock ); |
| 410 |
|
| 411 |
$language = $this->createMock( Language::class ); |
| 412 |
$language->method( 'getCode' ) |
| 413 |
->willReturn( 'sr' ); |
| 414 |
|
| 415 |
$userOptionsManagerMock = $this->createUserOptionsManagerMock( |
| 416 |
[ 'LanguageCode' => 'sr', 'variant' => 'sr' ], true |
| 417 |
); |
| 418 |
|
| 419 |
$prefs = $this->getPreferencesFactory( [ |
| 420 |
'language' => $language, |
| 421 |
'userOptionsManager' => $userOptionsManagerMock, |
| 422 |
] )->getFormDescriptor( $userMock, $this->context ); |
| 423 |
$this->assertArrayHasKey( 'default', $prefs['variant'] ); |
| 424 |
$this->assertEquals( 'sr', $prefs['variant']['default'] ); |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
public function testUserGroupMemberships() { |
| 431 |
$userMock = $this->createMock( User::class ); |
| 432 |
$userMock->method( 'isAllowed' )->willReturn( true ); |
| 433 |
$userMock->method( 'isAllowedAny' )->willReturn( true ); |
| 434 |
$userMock->method( 'isRegistered' )->willReturn( true ); |
| 435 |
$userMock = $this->getUserMockWithSession( $userMock ); |
| 436 |
|
| 437 |
$language = $this->createMock( Language::class ); |
| 438 |
$language->method( 'getCode' ) |
| 439 |
->willReturn( 'en' ); |
| 440 |
|
| 441 |
$userOptionsManagerMock = $this->createUserOptionsManagerMock( [], true ); |
| 442 |
|
| 443 |
$prefs = $this->getPreferencesFactory( [ |
| 444 |
'language' => $language, |
| 445 |
'userOptionsManager' => $userOptionsManagerMock, |
| 446 |
] )->getFormDescriptor( $userMock, $this->context ); |
| 447 |
$this->assertArrayHasKey( 'default', $prefs['usergroups'] ); |
| 448 |
$this->assertEquals( |
| 449 |
UserGroupMembership::getLinkHTML( 'user', $this->context ), |
| 450 |
( $prefs['usergroups']['default'] )() |
| 451 |
); |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
public function testAllServiceOptionsUsed() { |
| 458 |
$this->assertAllServiceOptionsUsed( [ |
| 459 |
|
| 460 |
'EnotifMinorEdits', |
| 461 |
|
| 462 |
'EnotifRevealEditorAddress', |
| 463 |
|
| 464 |
'SignatureValidation', |
| 465 |
] ); |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
private function createUserOptionsManagerMock( array $userOptions, bool $defaultOptions = false ) { |
| 474 |
$services = $this->getServiceContainer(); |
| 475 |
$defaults = $services->getMainConfig()->get( MainConfigNames::DefaultUserOptions ); |
| 476 |
$defaults['language'] = $services->getContentLanguage()->getCode(); |
| 477 |
$defaults['skin'] = Skin::normalizeKey( $services->getMainConfig()->get( MainConfigNames::DefaultSkin ) ); |
| 478 |
( new HookRunner( $services->getHookContainer() ) )->onUserGetDefaultOptions( $defaults ); |
| 479 |
$userOptions += $defaults; |
| 480 |
|
| 481 |
$mock = $this->createMock( UserOptionsManager::class ); |
| 482 |
$mock->method( 'getOptions' )->willReturn( $userOptions ); |
| 483 |
$mock->method( 'getOption' )->willReturnCallback( |
| 484 |
static function ( $user, $option ) use ( $userOptions ) { |
| 485 |
return $userOptions[$option] ?? null; |
| 486 |
} |
| 487 |
); |
| 488 |
if ( $defaultOptions ) { |
| 489 |
$mock->method( 'getDefaultOptions' )->willReturn( $defaults ); |
| 490 |
} |
| 491 |
return $mock; |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
private function getUserMockWithSession( MockObject $userMock ): MockObject { |
| 499 |
|
| 500 |
$mock = $this->getMockBuilder( stdClass::class ) |
| 501 |
->addMethods( [ 'getAllowedUserRights', 'deregisterSession', 'getSessionId' ] ) |
| 502 |
->getMock(); |
| 503 |
$mock->method( 'getSessionId' )->willReturn( |
| 504 |
new SessionId( str_repeat( 'X', 32 ) ) |
| 505 |
); |
| 506 |
$session = TestUtils::getDummySession( $mock ); |
| 507 |
$mockRequest = $this->getMockBuilder( FauxRequest::class ) |
| 508 |
->onlyMethods( [ 'getSession' ] ) |
| 509 |
->getMock(); |
| 510 |
$mockRequest->method( 'getSession' )->willReturn( $session ); |
| 511 |
$userMock->method( 'getRequest' )->willReturn( $mockRequest ); |
| 512 |
$userMock->method( 'getTitleKey' )->willReturn( '' ); |
| 513 |
return $userMock; |
| 514 |
} |
| 515 |
} |