| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 5 |
use GetConfiguration; |
| 6 |
use MediaWiki\Json\FormatJson; |
| 7 |
use MediaWiki\MainConfigNames; |
| 8 |
use NullJob; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class GetConfigurationTest extends MaintenanceBaseTestCase { |
| 15 |
|
| 16 |
protected function getMaintenanceClass() { |
| 17 |
return GetConfiguration::class; |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
public function testExecuteForFatalError( |
| 22 |
$options, $expectedOutputRegex, $configName = null, $configValue = null |
| 23 |
) { |
| 24 |
if ( $configName !== null ) { |
| 25 |
$this->overrideConfigValue( $configName, $configValue ); |
| 26 |
} |
| 27 |
foreach ( $options as $name => $value ) { |
| 28 |
$this->maintenance->setOption( $name, $value ); |
| 29 |
} |
| 30 |
$this->expectCallToFatalError(); |
| 31 |
|
| 32 |
$this->maintenance->setName( 'getConfiguration.php' ); |
| 33 |
$this->maintenance->validateParamsAndArgs(); |
| 34 |
$this->maintenance->execute(); |
| 35 |
$this->expectOutputRegex( $expectedOutputRegex ); |
| 36 |
} |
| 37 |
|
| 38 |
public static function provideExecuteForFatalError() { |
| 39 |
return [ |
| 40 |
'Config could not be encoded as JSON' => [ |
| 41 |
[ 'format' => 'json' ], '/Failed to serialize the requested settings/', |
| 42 |
MainConfigNames::AutoCreateTempUser, [ 'enabled' => true, 'expiryAfterDays' => INF ] |
| 43 |
], |
| 44 |
'Undefined format' => [ [ 'format' => 'invalid-format' ], '/--format set to an unrecognized format/' ], |
| 45 |
'Using both iregex and regex' => [ |
| 46 |
[ 'iregex' => 'wgAuto', 'regex' => 'wgAuto' ], |
| 47 |
'/Can only use either --regex or --iregex/', |
| 48 |
], |
| 49 |
'Setting does not begin with wg' => [ |
| 50 |
[ 'settings' => 'wgAutoCreateTempUser InvalidConfig' ], |
| 51 |
'/Variable \'InvalidConfig\' does start with \'wg\'/' |
| 52 |
], |
| 53 |
'Setting is undefined' => [ |
| 54 |
[ 'settings' => 'wgAutoCreateTempUser wgInvalidConfigForGetConfigurationTest' ], |
| 55 |
'/Variable \'wgInvalidConfigForGetConfigurationTest\' is not set/' |
| 56 |
], |
| 57 |
'Config that is referenced by --settings has non-array and non-scalar items' => [ |
| 58 |
[ 'settings' => 'wgAutoCreateTempUser' ], |
| 59 |
'/Variable wgAutoCreateTempUser includes non-array, non-scalar, items/', |
| 60 |
MainConfigNames::AutoCreateTempUser, [ 'enabled' => true, 'invalid' => new NullJob( [] ) ] |
| 61 |
], |
| 62 |
'Config referenced by --settings is an object' => [ |
| 63 |
[ 'settings' => 'wgTestConfig' ], |
| 64 |
'/Variable wgTestConfig includes non-array, non-scalar, items/', |
| 65 |
'TestConfig', new NullJob( [] ), |
| 66 |
], |
| 67 |
]; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
public function testExecuteForJSONFormat( $configName, $configValue ) { |
| 72 |
$this->overrideConfigValue( $configName, $configValue ); |
| 73 |
$this->maintenance->setOption( 'format', 'json' ); |
| 74 |
$this->maintenance->setOption( 'settings', 'wg' . $configName ); |
| 75 |
$this->maintenance->validateParamsAndArgs(); |
| 76 |
$this->maintenance->execute(); |
| 77 |
$this->expectOutputString( FormatJson::encode( [ 'wg' . $configName => $configValue ] ) . "\n" ); |
| 78 |
} |
| 79 |
|
| 80 |
public static function provideConfigVars() { |
| 81 |
return [ |
| 82 |
'Config var set to boolean value' => [ MainConfigNames::NewUserLog, true ], |
| 83 |
'Config var set to an integer' => [ MainConfigNames::RCMaxAge, 12345 ], |
| 84 |
'Config var set to a string' => [ MainConfigNames::ServerName, 'test' ], |
| 85 |
'Config var set to an array' => [ |
| 86 |
MainConfigNames::AutoCreateTempUser, |
| 87 |
[ 'enabled' => true, 'genPattern' => '~$1' ], |
| 88 |
], |
| 89 |
]; |
| 90 |
} |
| 91 |
|
| 92 |
public function testExecuteForJSONFormatWithJSONPartialOutputOnError() { |
| 93 |
$this->overrideConfigValue( |
| 94 |
MainConfigNames::AutoCreateTempUser, [ 'enabled' => true, 'expireAfterDays' => INF ] |
| 95 |
); |
| 96 |
$this->maintenance->setOption( 'format', 'json' ); |
| 97 |
$this->maintenance->setOption( 'settings', 'wgAutoCreateTempUser' ); |
| 98 |
$this->maintenance->setOption( 'json-partial-output-on-error', 1 ); |
| 99 |
$this->maintenance->validateParamsAndArgs(); |
| 100 |
$this->maintenance->execute(); |
| 101 |
$expectedJson = FormatJson::encode( [ |
| 102 |
'wgAutoCreateTempUser' => [ 'enabled' => true, 'expireAfterDays' => 0 ], |
| 103 |
'wgGetConfigurationJsonErrorOccurred' => true, |
| 104 |
] ); |
| 105 |
$this->expectOutputString( $expectedJson . "\n" ); |
| 106 |
} |
| 107 |
|
| 108 |
public function testExecuteForJSONFormatWithRegexOption() { |
| 109 |
|
| 110 |
$this->overrideConfigValue( 'GetConfigurationTestabc1', true ); |
| 111 |
$this->overrideConfigValue( 'GetconfigurationTestdef2', false ); |
| 112 |
$this->overrideConfigValue( 'GetConfigurationTestxzy3', "test" ); |
| 113 |
$this->maintenance->setOption( 'format', 'json' ); |
| 114 |
$this->maintenance->setOption( 'iregex', 'getconfigurationtest.*\d' ); |
| 115 |
$this->maintenance->validateParamsAndArgs(); |
| 116 |
$this->maintenance->execute(); |
| 117 |
|
| 118 |
$actualJson = $this->getActualOutputForAssertion(); |
| 119 |
$actualItems = FormatJson::decode( $actualJson, true ); |
| 120 |
$this->assertArrayEquals( |
| 121 |
[ |
| 122 |
'wgGetConfigurationTestabc1' => true, |
| 123 |
'wgGetConfigurationTestxzy3' => "test", |
| 124 |
'wgGetconfigurationTestdef2' => false, |
| 125 |
], |
| 126 |
$actualItems, |
| 127 |
false, |
| 128 |
true |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
public function testForPHPFormat( $configName, $configValue ) { |
| 134 |
$this->overrideConfigValue( $configName, $configValue ); |
| 135 |
$this->maintenance->setOption( 'format', 'php' ); |
| 136 |
$this->maintenance->setOption( 'settings', 'wg' . $configName ); |
| 137 |
$this->maintenance->validateParamsAndArgs(); |
| 138 |
$this->maintenance->execute(); |
| 139 |
$this->expectOutputString( serialize( [ 'wg' . $configName => $configValue ] ) . "\n" ); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
public function testForVarDumpFormat( $configName, $configValue ) { |
| 144 |
$this->overrideConfigValue( $configName, $configValue ); |
| 145 |
$this->maintenance->setOption( 'format', 'vardump' ); |
| 146 |
$this->maintenance->setOption( 'settings', 'wg' . $configName ); |
| 147 |
$this->maintenance->validateParamsAndArgs(); |
| 148 |
$this->maintenance->execute(); |
| 149 |
$expectedOutputString = '$wg' . $configName . ' = '; |
| 150 |
|
| 151 |
ob_start(); |
| 152 |
var_dump( $configValue ); |
| 153 |
$expectedOutputString .= trim( ob_get_clean() ); |
| 154 |
|
| 155 |
$this->expectOutputString( $expectedOutputString . ";\n" ); |
| 156 |
} |
| 157 |
} |