Merge "Bump wikimedia/parsoid to 0.21.0-a11"
[mediawiki.git] / tests / phpunit / maintenance / GetConfigurationTest.php
blobea617204bdeb622e78a8c5d3dd15e15c64cc6dac
1 <?php
3 namespace MediaWiki\Tests\Maintenance;
5 use GetConfiguration;
6 use MediaWiki\Json\FormatJson;
7 use MediaWiki\MainConfigNames;
8 use NullJob;
10 /**
11 * @covers \GetConfiguration
12 * @author Dreamy Jazz
14 class GetConfigurationTest extends MaintenanceBaseTestCase {
16 protected function getMaintenanceClass() {
17 return GetConfiguration::class;
20 /** @dataProvider provideExecuteForFatalError */
21 public function testExecuteForFatalError(
22 $options, $expectedOutputRegex, $configName = null, $configValue = null
23 ) {
24 if ( $configName !== null ) {
25 $this->overrideConfigValue( $configName, $configValue );
27 foreach ( $options as $name => $value ) {
28 $this->maintenance->setOption( $name, $value );
30 $this->expectCallToFatalError();
31 // ::maybeShowHelp uses ->mName which is null unless we call this.
32 $this->maintenance->setName( 'getConfiguration.php' );
33 $this->maintenance->validateParamsAndArgs();
34 $this->maintenance->execute();
35 $this->expectOutputRegex( $expectedOutputRegex );
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 ]
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/',
49 'Setting does not begin with wg' => [
50 [ 'settings' => 'wgAutoCreateTempUser InvalidConfig' ],
51 '/Variable \'InvalidConfig\' does start with \'wg\'/'
53 'Setting is undefined' => [
54 [ 'settings' => 'wgAutoCreateTempUser wgInvalidConfigForGetConfigurationTest' ],
55 '/Variable \'wgInvalidConfigForGetConfigurationTest\' is not set/'
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( [] ) ]
62 'Config referenced by --settings is an object' => [
63 [ 'settings' => 'wgTestConfig' ],
64 '/Variable wgTestConfig includes non-array, non-scalar, items/',
65 'TestConfig', new NullJob( [] ),
70 /** @dataProvider provideConfigVars */
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" );
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' ],
92 public function testExecuteForJSONFormatWithJSONPartialOutputOnError() {
93 $this->overrideConfigValue(
94 MainConfigNames::AutoCreateTempUser, [ 'enabled' => true, 'expireAfterDays' => INF ]
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" );
108 public function testExecuteForJSONFormatWithRegexOption() {
109 // Create three testing configuration values which will allow testing using regex.
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 // Validate that the JSON output is as expected.
118 $actualJson = $this->getActualOutputForAssertion();
119 $actualItems = FormatJson::decode( $actualJson, true );
120 $this->assertArrayEquals(
122 'wgGetConfigurationTestabc1' => true,
123 'wgGetConfigurationTestxzy3' => "test",
124 'wgGetconfigurationTestdef2' => false,
126 $actualItems,
127 false,
128 true
132 /** @dataProvider provideConfigVars */
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" );
142 /** @dataProvider provideConfigVars */
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 // Get the config value in var_dump format
151 ob_start();
152 var_dump( $configValue );
153 $expectedOutputString .= trim( ob_get_clean() );
155 $this->expectOutputString( $expectedOutputString . ";\n" );