1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
|
<?php
namespace MediaWiki\Tests\Unit\Settings;
use InvalidArgumentException;
use MediaWiki\MainConfigNames;
use MediaWiki\MainConfigSchema;
use MediaWiki\Registration\ExtensionRegistry;
use MediaWiki\Settings\Cache\CacheableSource;
use MediaWiki\Settings\Cache\CachedSource;
use MediaWiki\Settings\Config\ArrayConfigBuilder;
use MediaWiki\Settings\Config\MergeStrategy;
use MediaWiki\Settings\Config\PhpIniSink;
use MediaWiki\Settings\SettingsBuilder;
use MediaWiki\Settings\SettingsBuilderException;
use PHPUnit\Framework\TestCase;
use Wikimedia\ObjectCache\BagOStuff;
/**
* @covers \MediaWiki\Settings\SettingsBuilder
*/
class SettingsBuilderTest extends TestCase {
/**
* @param array $params
* @return SettingsBuilder
*/
private function newSettingsBuilder( $params = [] ): SettingsBuilder {
return new SettingsBuilder(
__DIR__,
$params['extensionRegistry'] ?? $this->createMock( ExtensionRegistry::class ),
$params['configBuilder'] ?? new ArrayConfigBuilder(),
$params['phpIniSink'] ?? $this->createMock( PhpIniSink::class ),
$params['cache'] ?? null
);
}
public function testLoadingFromFile() {
$phpIniSinkMock = $this->createMock( PhpIniSink::class );
$phpIniSinkMock->expects( $this->once() )->method( 'set' )->with( 'foo', 'bar' );
$setting = $this->newSettingsBuilder( [
'phpIniSink' => $phpIniSinkMock
] );
$setting->loadFile( 'fixtures/settings.json' )->apply();
$config = $setting->getConfig();
$this->assertSame( 'TEST', $config->get( 'Something' ) );
}
public function testLoadingIncludesRecursiveFromFile() {
$configBuilder = new ArrayConfigBuilder();
$phpIniSinkMock = $this->createMock( PhpIniSink::class );
$phpIniSinkMock->expects( $this->once() )->method( 'set' )->with( 'foo', 'bar' );
// Make sure the cache is hit twice, once for each file!
$mockCache = $this->createMock( BagOStuff::class );
$key = 'foo';
$mockCache
->expects( $this->atLeastOnce() )
->method( 'makeGlobalKey' )
->willReturn( $key );
$mockCache
->expects( $this->exactly( 2 ) )
->method( 'get' )
->willReturn( false );
$mockCache
->expects( $this->exactly( 2 ) )
->method( 'lock' )
->willReturn( true );
$mockCache
->expects( $this->exactly( 2 ) )
->method( 'unlock' )
->willReturn( true );
$setting = $this->newSettingsBuilder( [
'configBuilder' => $configBuilder,
'phpIniSink' => $phpIniSinkMock,
'cache' => $mockCache,
] );
$setting->loadFile( 'fixtures/settings-with-includes.json' )->apply();
$config = $configBuilder->build();
$this->assertSame( 'TEST', $config->get( 'Something' ) );
}
/**
* Ensure that includes work in a source that is not a SettingsIncludeLocator
*/
public function testLoadingIncludesRecursiveFromArray() {
$configBuilder = new ArrayConfigBuilder();
$phpIniSinkMock = $this->createMock( PhpIniSink::class );
$phpIniSinkMock->expects( $this->once() )->method( 'set' )->with( 'foo', 'bar' );
$setting = $this->newSettingsBuilder( [
'configBuilder' => $configBuilder,
'phpIniSink' => $phpIniSinkMock,
] );
$setting->loadArray( [ 'includes' => [ 'fixtures/settings.json' ] ] )->apply();
$config = $configBuilder->build();
$this->assertSame( 'TEST', $config->get( 'Something' ) );
}
public function testLoadingIncludesRecursiveLoop() {
$setting = $this->newSettingsBuilder();
$this->expectException( SettingsBuilderException::class );
$setting->loadFile( 'fixtures/settings-with-self-includes.json' )->apply();
}
public function testLoadingAndApplyingFromFileAfterFinalize() {
$configBuilder = new ArrayConfigBuilder();
$phpIniSinkMock = $this->createMock( PhpIniSink::class );
$phpIniSinkMock->expects( $this->once() )->method( 'set' )->with( 'foo', 'bar' );
$setting = $this->newSettingsBuilder( [
'configBuilder' => $configBuilder,
'phpIniSink' => $phpIniSinkMock
] );
$setting->loadFile( 'fixtures/settings.json' )->apply();
$config = $configBuilder->build();
$this->assertSame( 'TEST', $config->get( 'Something' ) );
// Check that after enterRegistrationStage(), we can still override config,
// but we can't load settings sources.
$setting->enterRegistrationStage();
$setting->overrideConfigValue( 'Foo', 'bar' );
$this->expectException( SettingsBuilderException::class );
$setting->loadFile( 'fixtures/settings.json' )->apply();
}
public function testSettingConfigAfterLock() {
$setting = $this->newSettingsBuilder();
// Check that after enterOperationStage(), we can't override config.
$setting->enterReadOnlyStage();
$this->expectException( SettingsBuilderException::class );
$setting->overrideConfigValue( 'Foo', 'bar' );
}
public function testLoadingExtensions() {
$extensionRegistryMock = $this->createMock( ExtensionRegistry::class );
$expectedQueuePaths = [
'/test/extensions/Foo/extension.json',
'/test/extensions/Bar/extension.json',
'/test/skins/Quux/skin.json',
];
$extensionRegistryMock
->expects( $this->exactly( 3 ) )
->method( 'queue' )
->willReturnCallback( function ( $path ) use ( &$expectedQueuePaths ) {
$this->assertContains( $path, $expectedQueuePaths );
$pathIdx = array_search( $path, $expectedQueuePaths, true );
unset( $expectedQueuePaths[$pathIdx] );
} );
$setting = $this->newSettingsBuilder( [
'extensionRegistry' => $extensionRegistryMock,
] );
$setting->loadFile( 'fixtures/default-schema.json' );
$setting->loadFile( 'fixtures/settings.json' );
$setting->apply();
}
public static function provideConfigDefaults() {
yield 'sets a value from a single settings file' => [
'settingsBatches' => [
[ 'config' => [ 'MySetting' => 'MyValue', ], ],
],
'expected' => [
'MySetting' => 'MyValue',
],
];
yield 'merges different values from multiple settings files' => [
'settingsBatches' => [
[ 'config' => [ 'MySetting' => 'MyValue', ], ],
[ 'config' => [ 'MyOtherSetting' => 'MyOtherValue', ], ],
],
'expected' => [
'MySetting' => 'MyValue',
'MyOtherSetting' => 'MyOtherValue',
],
];
yield 'overrides value in config' => [
'settingsBatches' => [
[ 'config' => [ 'MySetting' => 'MyValue', ], ],
[ 'config' => [ 'MySetting' => 'MyOtherValue', ], ],
],
'expected' => [
'MySetting' => 'MyOtherValue',
],
];
yield 'sets a default from schema' => [
'settingsBatches' => [
[ 'config-schema' => [ 'MySetting' => [ 'default' => 'MyDefault', ], ], ],
],
'expected' => [
'MySetting' => 'MyDefault',
],
];
yield 'value in config overrides default from schema' => [
'settingsBatches' => [
[
'config-schema' => [ 'MySetting' => [ 'default' => 'MyDefault', ], ],
'config' => [ 'MySetting' => 'MyValue', ],
],
],
'expected' => [
'MySetting' => 'MyValue',
],
];
yield 'default null is applied' => [
'settingsBatches' => [
[ 'config-schema' => [ 'MySetting' => [ 'default' => null, ], ], ],
],
'expected' => [
'MySetting' => null,
],
];
yield 'null value can override default' => [
'settingsBatches' => [
[
'config-schema' => [ 'MySetting' => [ 'default' => 'default', ], ],
'config' => [ 'MySetting' => null, ],
],
],
'expected' => [
'MySetting' => null,
],
];
yield 'merge strategy is applied when setting config' => [
'settingsBatches' => [
[
'config-schema' => [ 'MySetting' => [
'mergeStrategy' => MergeStrategy::ARRAY_MERGE_RECURSIVE
], ],
'config' => [ 'MySetting' => [ 'a' => [ 'b' => 'c' ], ], ],
],
[
'config' => [ 'MySetting' => [ 'a' => [ 'b' => 'd' ], ], ],
]
],
'expected' => [
'MySetting' => [ 'a' => [ 'b' => [ 'c', 'd' ], ], ],
],
];
yield 'merge strategy is ignored when using config-overrides' => [
'settingsBatches' => [
[
'config-schema' => [ 'MySetting' => [
'mergeStrategy' => MergeStrategy::ARRAY_MERGE_RECURSIVE
], ],
'config-overrides' => [ 'MySetting' => [ 'a' => [ 'b' => 'c' ], ], ],
],
[
'config-overrides' => [ 'MySetting' => [ 'a' => [ 'b' => 'd' ], ], ],
]
],
'expected' => [
'MySetting' => [ 'a' => [ 'b' => 'd' ], ],
],
];
yield 'config value is merged into default when setting both in the same settings file' => [
'settingsBatches' => [
[
'config' => [ 'MySetting' => [ 'a' => [ 'b' => 'd' ], ], ],
'config-schema' => [ 'MySetting' => [
'mergeStrategy' => MergeStrategy::ARRAY_MERGE_RECURSIVE,
'default' => [ 'a' => [ 'b' => 'c' ], ],
], ],
]
],
'expected' => [
'MySetting' => [ 'a' => [ 'b' => [ 'c', 'd' ], ], ],
],
];
yield 'default is merged backwards into config value when set later' => [
'settingsBatches' => [
[
'config' => [ 'MySetting' => [ 'a' => [ 'b' => 'd' ], ], ],
],
[
'config-schema' => [ 'MySetting' => [
'mergeStrategy' => MergeStrategy::ARRAY_MERGE_RECURSIVE,
'default' => [ 'a' => [ 'b' => 'c' ], ],
], ],
]
],
'expected' => [
'MySetting' => [ 'a' => [ 'b' => [ 'c', 'd' ], ], ],
],
];
}
/**
* @dataProvider provideConfigDefaults
*/
public function testConfigDefaults( array $settingsBatches, array $expected ) {
$configBuilder = new ArrayConfigBuilder();
$setting = $this->newSettingsBuilder( [ 'configBuilder' => $configBuilder ] );
foreach ( $settingsBatches as $batch ) {
$setting->loadArray( $batch );
}
$setting->apply();
foreach ( $expected as $key => $value ) {
$this->assertSame( $value, $configBuilder->build()->get( $key ) );
}
}
public function testSetConfig() {
$setting = $this->newSettingsBuilder();
$setting->loadArray( [
'config-schema' => [
'a' => [ 'mergeStrategy' => MergeStrategy::ARRAY_MERGE ]
]
] );
$setting->putConfigValues( [ 'a' => [ 1 ], 'b' => 2 ] );
$config = $setting->getConfig();
$this->assertSame( [ 1 ], $config->get( 'a' ) );
$this->assertSame( 2, $config->get( 'b' ) );
$setting->putConfigValues( [ 'a' => [ 11 ], 'b' => 22 ] );
$config = $setting->getConfig();
$this->assertSame( [ 1, 11 ], $config->get( 'a' ) );
$this->assertSame( 22, $config->get( 'b' ) );
$setting->putConfigValue( 'a', [ 111 ] );
$config = $setting->getConfig();
$this->assertSame( [ 1, 11, 111 ], $config->get( 'a' ) );
$this->assertSame( 22, $config->get( 'b' ) );
}
public function testOverrideConfig() {
$setting = $this->newSettingsBuilder();
$setting->loadArray( [
'config-schema' => [
'a' => [ 'mergeStrategy' => MergeStrategy::ARRAY_MERGE ]
]
] );
$setting->overrideConfigValues( [ 'a' => [ 1 ], 'b' => 2 ] );
$config = $setting->getConfig();
$this->assertSame( [ 1 ], $config->get( 'a' ) );
$this->assertSame( 2, $config->get( 'b' ) );
$setting->overrideConfigValues( [ 'a' => [ 11 ], 'b' => 22 ] );
$config = $setting->getConfig();
$this->assertSame( [ 11 ], $config->get( 'a' ) );
$this->assertSame( 22, $config->get( 'b' ) );
$setting->overrideConfigValue( 'a', [ 111 ] );
$config = $setting->getConfig();
$this->assertSame( [ 111 ], $config->get( 'a' ) );
$this->assertSame( 22, $config->get( 'b' ) );
}
public function testRegisterHookHandler() {
$setting = $this->newSettingsBuilder();
$hookName = 'TestHookForSettingsBuilderTest';
$setting->registerHookHandlers( [ $hookName => [ 'strtolower' ] ] );
$config = $setting->getConfig();
$this->assertArrayHasKey( $hookName, $config->get( MainConfigNames::Hooks ) );
}
public function testApplyPurgesState() {
$configBuilder = new ArrayConfigBuilder();
$setting = $this->newSettingsBuilder( [ 'configBuilder' => $configBuilder ] );
$setting->putConfigValue( 'MySetting', 'MyValue' )
->apply();
$this->assertSame( 'MyValue', $configBuilder->build()->get( 'MySetting' ) );
$configBuilder->set( 'MySetting', 'MyOtherValue' );
// Calling apply a second time should not redefine the global
// since the state should be cleared
$setting->apply();
$this->assertSame( 'MyOtherValue', $configBuilder->build()->get( 'MySetting' ) );
}
public function testApplyDefaultDoesNotOverwriteExisting() {
$configBuilder = new ArrayConfigBuilder();
// If we set a value via SettingsBuilder, defining a default later
// should not override it.
$this->newSettingsBuilder( [ 'configBuilder' => $configBuilder ] )
->putConfigValue( 'MySetting', 'existing' )
->loadArray( [ 'config-schema' => [ 'MySetting' => [ 'default' => 'default' ], ], ] )
->apply();
$this->assertSame( 'existing', $configBuilder->build()->get( 'MySetting' ) );
}
public function testApplyDefaultDoesNotOverwritePreexisting() {
$configBuilder = new ArrayConfigBuilder();
// If we set a value in the config directly, defining a default later
// would override it, unless we call assumeDirtyConfig().
$configBuilder->set( 'MySetting', 'existing' );
$this->newSettingsBuilder( [ 'configBuilder' => $configBuilder ] )
->assumeDirtyConfig()
->loadArray( [ 'config-schema' => [ 'MySetting' => [ 'default' => 'default' ], ], ] )
->apply();
$this->assertSame( 'existing', $configBuilder->build()->get( 'MySetting' ) );
}
public function testConfigSchemaOverrideNotAllowed() {
$this->expectException( SettingsBuilderException::class );
$this->newSettingsBuilder()
->loadArray( [ 'config-schema' => [ 'MySetting' => [ 'default' => 'default' ], ], ] )
->loadArray( [ 'config-schema' => [ 'MySetting' => [ 'default' => 'override' ], ], ] )
->apply();
}
public function testConfigSchemaDefaultsEvaluation() {
$settingsBuilder = $this->newSettingsBuilder();
$settingsBuilder->loadArray( [
'config-schema' => [
'A' => [ 'default' => [ 'a' ], 'type' => 'array' ],
'B' => [ 'default' => [ 'b' ], 'type' => 'array' ],
],
'config' => [
'B' => [ 'b2' ],
'C' => [ 'c2' ],
]
] );
$settingsBuilder->loadArray( [
'config-schema' => [
'X' => [ 'default' => [ 'x' ], 'type' => 'array' ],
],
'config' => [
'A' => [ 'a3' ],
'B' => [ 'b3' ],
],
'config-overrides' => [
'C' => [ 'c4' ],
]
] );
$config = $settingsBuilder->getConfig();
$this->assertSame( [ 'a', 'a3' ], $config->get( 'A' ) );
$this->assertSame( [ 'b', 'b2', 'b3' ], $config->get( 'B' ) );
$this->assertSame( [ 'c4' ], $config->get( 'C' ) );
$this->assertSame( [ 'x' ], $config->get( 'X' ) );
}
public static function provideValidate() {
yield 'all good' => [
'settings' => [
'config-schema' => [ 'foo' => [ 'type' => 'string', ], ],
'config' => [ 'foo' => 'bar', ],
],
'valid' => true,
];
yield 'missing key' => [
'settings' => [
'config-schema' => [ 'foo' => [ 'type' => 'string', ], ],
'config' => [ 'bar' => 'bar' ],
],
'valid' => false,
];
yield 'invalid config' => [
'settings' => [
'config-schema' => [ 'foo' => [ 'type' => 'string', ], ],
'config' => [ 'foo' => 1 ],
],
'valid' => false,
];
yield 'no schema was added' => [
'settings' => [
'config-schema' => [],
'config' => [ 'foo' => 'bar', ],
],
'valid' => true,
];
yield 'key is in config but has no schema' => [
'settings' => [
'config-schema' => [ 'foo' => [ 'type' => 'array', 'mergeStrategy' => MergeStrategy::ARRAY_MERGE ], ],
'config' => [ 'foo' => [], 'baz' => false, ],
],
'valid' => true,
];
}
/**
* @dataProvider provideValidate
*/
public function testValidate( array $settings, bool $valid ) {
$status = $this->newSettingsBuilder()
->loadArray( $settings )
->apply()
->validate();
$this->assertSame( $valid, $status->isOK() );
}
public function testValidateInvalidSchema() {
$this->expectException( InvalidArgumentException::class );
$this->newSettingsBuilder()
->loadArray( [
'config-schema' => [ 'foo' => [ 'type' => 1 ] ],
'config' => [ 'foo' => 'bar' ],
] )
->apply()
->validate();
}
public function testLoadsCacheableSource() {
$mockSource = $this->createMock( CacheableSource::class );
$mockCache = $this->createMock( BagOStuff::class );
$configBuilder = new ArrayConfigBuilder();
$builder = $this
->newSettingsBuilder( [
'configBuilder' => $configBuilder,
'cache' => $mockCache
] )
->load( $mockSource );
$hashKey = 'abc123';
$key = 'global:' . self::class . ':' . $hashKey;
// Mock a cache miss
$mockSource
->expects( $this->once() )
->method( 'getHashKey' )
->willReturn( $hashKey );
$mockCache
->expects( $this->once() )
->method( 'makeGlobalKey' )
->with( CachedSource::class, $hashKey )
->willReturn( $key );
$mockCache
->expects( $this->once() )
->method( 'get' )
->with( $key )
->willReturn( false );
$mockCache
->expects( $this->once() )
->method( 'lock' )
->willReturn( true );
$mockCache
->expects( $this->once() )
->method( 'unlock' )
->willReturn( true );
$mockSource
->expects( $this->once() )
->method( 'load' )
->willReturn( [ 'config' => [ 'MySetting' => 'BlaBla' ] ] );
$builder->apply();
$this->assertSame( 'BlaBla', $configBuilder->build()->get( 'MySetting' ) );
}
public function testGetDefaultConfig() {
$defaultConfig = $this->newSettingsBuilder()
->loadArray( [ 'config-schema' => [
'MySetting' => [ 'default' => 'bla' ],
'OtherSetting' => [ 'type' => 'number' ], // no default
] ] )
->apply()
->getDefaultConfig();
$this->assertTrue( $defaultConfig->has( 'MySetting' ) );
$this->assertSame( 'bla', $defaultConfig->get( 'MySetting' ) );
$this->assertTrue( $defaultConfig->has( 'OtherSetting' ) );
$this->assertNull( $defaultConfig->get( 'OtherSetting' ) );
}
public function testGetConfigSchema() {
$configSchema = $this->newSettingsBuilder()
->loadArray( [ 'config-schema' => [ 'MySetting' => [ 'default' => 'bla' ], ], ] )
->apply()
->getConfigSchema();
$this->assertTrue( $configSchema->hasSchemaFor( 'MySetting' ) );
$this->assertSame( 'bla', $configSchema->getDefaultFor( 'MySetting' ) );
}
/**
* Make sure that the 'config-schema' and 'config-schema-inverse' keys
* are fully processed and combined appropriately.
*/
public function testSchemaLoading() {
$settings = $this->newSettingsBuilder();
$settings->loadFile( 'fixtures/default-schema.json' );
$schema = $settings->getConfigSchema();
$this->assertSame(
'/DEFAULT/',
$schema->getDefaultFor( 'StyleDirectory' )
);
$this->assertSame(
[ 'callback' => [ MainConfigSchema::class, 'getDefaultUsePathInfo' ] ],
$schema->getDynamicDefaultDeclarationFor( 'UsePathInfo' )
);
$this->assertSame(
'replace',
$schema->getMergeStrategyFor( 'LBFactoryConf' )->getName()
);
$this->assertSame(
'/DEFAULT/',
$schema->getDefaultFor( 'ExtensionDirectory' )
);
$this->assertSame(
[ 'use' => [ 'ScriptPath' ], 'callback' => [ MainConfigSchema::class, 'getDefaultRestPath' ] ],
$schema->getDynamicDefaultDeclarationFor( 'RestPath' )
);
$this->assertSame(
[ 'use' => [ 'ScriptPath' ], 'callback' => [ MainConfigSchema::class, 'getDefaultRestPath' ] ],
$schema->getDynamicDefaultDeclarationFor( 'RestPath' )
);
$this->assertSame(
'replace',
$schema->getMergeStrategyFor( 'TiffThumbnailType' )->getName()
);
}
}
|