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
|
<?php
namespace MediaWiki\Tests\Maintenance;
use GetConfiguration;
use MediaWiki\Json\FormatJson;
use MediaWiki\MainConfigNames;
use NullJob;
/**
* @covers \GetConfiguration
* @author Dreamy Jazz
*/
class GetConfigurationTest extends MaintenanceBaseTestCase {
protected function getMaintenanceClass() {
return GetConfiguration::class;
}
/** @dataProvider provideExecuteForFatalError */
public function testExecuteForFatalError(
$options, $expectedOutputRegex, $configName = null, $configValue = null
) {
if ( $configName !== null ) {
$this->overrideConfigValue( $configName, $configValue );
}
foreach ( $options as $name => $value ) {
$this->maintenance->setOption( $name, $value );
}
$this->expectCallToFatalError();
// ::maybeShowHelp uses ->mName which is null unless we call this.
$this->maintenance->setName( 'getConfiguration.php' );
$this->maintenance->validateParamsAndArgs();
$this->maintenance->execute();
$this->expectOutputRegex( $expectedOutputRegex );
}
public static function provideExecuteForFatalError() {
return [
'Config could not be encoded as JSON' => [
[ 'format' => 'json' ], '/Failed to serialize the requested settings/',
MainConfigNames::AutoCreateTempUser, [ 'enabled' => true, 'expiryAfterDays' => INF ]
],
'Undefined format' => [ [ 'format' => 'invalid-format' ], '/--format set to an unrecognized format/' ],
'Using both iregex and regex' => [
[ 'iregex' => 'wgAuto', 'regex' => 'wgAuto' ],
'/Can only use either --regex or --iregex/',
],
'Setting does not begin with wg' => [
[ 'settings' => 'wgAutoCreateTempUser InvalidConfig' ],
'/Variable \'InvalidConfig\' does start with \'wg\'/'
],
'Setting is undefined' => [
[ 'settings' => 'wgAutoCreateTempUser wgInvalidConfigForGetConfigurationTest' ],
'/Variable \'wgInvalidConfigForGetConfigurationTest\' is not set/'
],
'Config that is referenced by --settings has non-array and non-scalar items' => [
[ 'settings' => 'wgAutoCreateTempUser' ],
'/Variable wgAutoCreateTempUser includes non-array, non-scalar, items/',
MainConfigNames::AutoCreateTempUser, [ 'enabled' => true, 'invalid' => new NullJob( [] ) ]
],
'Config referenced by --settings is an object' => [
[ 'settings' => 'wgTestConfig' ],
'/Variable wgTestConfig includes non-array, non-scalar, items/',
'TestConfig', new NullJob( [] ),
],
];
}
/** @dataProvider provideConfigVars */
public function testExecuteForJSONFormat( $configName, $configValue ) {
$this->overrideConfigValue( $configName, $configValue );
$this->maintenance->setOption( 'format', 'json' );
$this->maintenance->setOption( 'settings', 'wg' . $configName );
$this->maintenance->validateParamsAndArgs();
$this->maintenance->execute();
$this->expectOutputString( FormatJson::encode( [ 'wg' . $configName => $configValue ] ) . "\n" );
}
public static function provideConfigVars() {
return [
'Config var set to boolean value' => [ MainConfigNames::NewUserLog, true ],
'Config var set to an integer' => [ MainConfigNames::RCMaxAge, 12345 ],
'Config var set to a string' => [ MainConfigNames::ServerName, 'test' ],
'Config var set to an array' => [
MainConfigNames::AutoCreateTempUser,
[ 'enabled' => true, 'genPattern' => '~$1' ],
],
];
}
public function testExecuteForJSONFormatWithJSONPartialOutputOnError() {
$this->overrideConfigValue(
MainConfigNames::AutoCreateTempUser, [ 'enabled' => true, 'expireAfterDays' => INF ]
);
$this->maintenance->setOption( 'format', 'json' );
$this->maintenance->setOption( 'settings', 'wgAutoCreateTempUser' );
$this->maintenance->setOption( 'json-partial-output-on-error', 1 );
$this->maintenance->validateParamsAndArgs();
$this->maintenance->execute();
$expectedJson = FormatJson::encode( [
'wgAutoCreateTempUser' => [ 'enabled' => true, 'expireAfterDays' => 0 ],
'wgGetConfigurationJsonErrorOccurred' => true,
] );
$this->expectOutputString( $expectedJson . "\n" );
}
public function testExecuteForJSONFormatWithRegexOption() {
// Create three testing configuration values which will allow testing using regex.
$this->overrideConfigValue( 'GetConfigurationTestabc1', true );
$this->overrideConfigValue( 'GetconfigurationTestdef2', false );
$this->overrideConfigValue( 'GetConfigurationTestxzy3', "test" );
$this->maintenance->setOption( 'format', 'json' );
$this->maintenance->setOption( 'iregex', 'getconfigurationtest.*\d' );
$this->maintenance->validateParamsAndArgs();
$this->maintenance->execute();
// Validate that the JSON output is as expected.
$actualJson = $this->getActualOutputForAssertion();
$actualItems = FormatJson::decode( $actualJson, true );
$this->assertArrayEquals(
[
'wgGetConfigurationTestabc1' => true,
'wgGetConfigurationTestxzy3' => "test",
'wgGetconfigurationTestdef2' => false,
],
$actualItems,
false,
true
);
}
/** @dataProvider provideConfigVars */
public function testForPHPFormat( $configName, $configValue ) {
$this->overrideConfigValue( $configName, $configValue );
$this->maintenance->setOption( 'format', 'php' );
$this->maintenance->setOption( 'settings', 'wg' . $configName );
$this->maintenance->validateParamsAndArgs();
$this->maintenance->execute();
$this->expectOutputString( serialize( [ 'wg' . $configName => $configValue ] ) . "\n" );
}
/** @dataProvider provideConfigVars */
public function testForVarDumpFormat( $configName, $configValue ) {
$this->overrideConfigValue( $configName, $configValue );
$this->maintenance->setOption( 'format', 'vardump' );
$this->maintenance->setOption( 'settings', 'wg' . $configName );
$this->maintenance->validateParamsAndArgs();
$this->maintenance->execute();
$expectedOutputString = '$wg' . $configName . ' = ';
// Get the config value in var_dump format
ob_start();
var_dump( $configValue );
$expectedOutputString .= trim( ob_get_clean() );
$this->expectOutputString( $expectedOutputString . ";\n" );
}
}
|