| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Unit\Settings\Config; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Config\HashConfig; |
| 7 |
use MediaWiki\Settings\Config\ConfigSchemaAggregator; |
| 8 |
use MediaWiki\Settings\Config\MergeStrategy; |
| 9 |
use MediaWiki\Settings\SettingsBuilderException; |
| 10 |
use PHPUnit\Framework\TestCase; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class ConfigSchemaAggregatorTest extends TestCase { |
| 16 |
|
| 17 |
public function testAddSchema() { |
| 18 |
$aggregator = new ConfigSchemaAggregator(); |
| 19 |
$aggregator->addSchema( 'foo', [ 'type' => 'string', ] ); |
| 20 |
$aggregator->addSchema( 'bar', [ 'type' => 'number', ] ); |
| 21 |
$this->assertTrue( $aggregator->hasSchemaFor( 'foo' ) ); |
| 22 |
$this->assertFalse( $aggregator->hasSchemaFor( 'xyzzy' ) ); |
| 23 |
$this->assertSame( [ 'type' => 'number', ], $aggregator->getSchemaFor( 'bar' ) ); |
| 24 |
} |
| 25 |
|
| 26 |
public function testAddSchemaMulti() { |
| 27 |
$aggregator = new ConfigSchemaAggregator(); |
| 28 |
$aggregator->addSchemaMulti( [ |
| 29 |
'foo' => [ 'type' => 'string', ], |
| 30 |
'bar' => [ 'type' => 'number', ], |
| 31 |
] ); |
| 32 |
$this->assertTrue( $aggregator->hasSchemaFor( 'foo' ) ); |
| 33 |
$this->assertFalse( $aggregator->hasSchemaFor( 'xyzzy' ) ); |
| 34 |
$this->assertSame( [ 'type' => 'number', ], $aggregator->getSchemaFor( 'bar' ) ); |
| 35 |
} |
| 36 |
|
| 37 |
public function testCombineSchema() { |
| 38 |
$aggregator = new ConfigSchemaAggregator(); |
| 39 |
$aggregator->addTypes( [ 'foo' => 'string', ] ); |
| 40 |
$aggregator->addSchema( 'bar', [ 'type' => 'number', ] ); |
| 41 |
$aggregator->addSchema( 'foo', [ 'default' => 'X', ] ); |
| 42 |
$this->assertTrue( $aggregator->hasSchemaFor( 'foo' ) ); |
| 43 |
$this->assertTrue( $aggregator->hasSchemaFor( 'bar' ) ); |
| 44 |
$this->assertFalse( $aggregator->hasSchemaFor( 'xyzzy' ) ); |
| 45 |
$this->assertSame( [ 'type' => 'number', ], $aggregator->getSchemaFor( 'bar' ) ); |
| 46 |
$this->assertSame( 'X', $aggregator->getDefaultFor( 'foo' ) ); |
| 47 |
$this->assertSame( 'string', $aggregator->getTypeFor( 'foo' ) ); |
| 48 |
} |
| 49 |
|
| 50 |
public function testAddSchemaOverrideFails() { |
| 51 |
$aggregator = new ConfigSchemaAggregator(); |
| 52 |
$aggregator->addTypes( [ 'foo' => 'int', ] ); |
| 53 |
|
| 54 |
$this->expectException( SettingsBuilderException::class ); |
| 55 |
$aggregator->addSchema( 'foo', [ 'type' => 'string', ] ); |
| 56 |
} |
| 57 |
|
| 58 |
public function testSetAndGetDefaults() { |
| 59 |
$aggregator = new ConfigSchemaAggregator(); |
| 60 |
$aggregator->addSchema( 'no_default', [ 'type' => 'string', ] ); |
| 61 |
$aggregator->addSchema( 'with_default', [ 'type' => 'string', 'default' => 'bla', ] ); |
| 62 |
$aggregator->addDefaults( [ 'another_with_default' => 'blabla' ] ); |
| 63 |
$this->assertFalse( $aggregator->hasDefaultFor( 'no_default' ) ); |
| 64 |
$this->assertTrue( $aggregator->hasDefaultFor( 'with_default' ) ); |
| 65 |
$this->assertTrue( $aggregator->hasDefaultFor( 'another_with_default' ) ); |
| 66 |
$this->assertSame( 'bla', $aggregator->getDefaultFor( 'with_default' ) ); |
| 67 |
$this->assertSame( [ 'default' => 'blabla' ], $aggregator->getSchemaFor( 'another_with_default' ) ); |
| 68 |
$this->assertEquals( [ |
| 69 |
'with_default' => 'bla', |
| 70 |
'another_with_default' => 'blabla', |
| 71 |
], $aggregator->getDefaults() ); |
| 72 |
} |
| 73 |
|
| 74 |
public function testSetAndGetDynamicDefaults() { |
| 75 |
$dyn1 = [ 'callback' => 'function1' ]; |
| 76 |
$dyn2 = [ 'callback' => 'function2', 'use' => [ 'Stuff' ] ]; |
| 77 |
|
| 78 |
$aggregator = new ConfigSchemaAggregator(); |
| 79 |
$aggregator->addSchema( 'no_dynamicDefault', [ 'default' => 'xyz', ] ); |
| 80 |
$aggregator->addSchema( 'with_dynamicDefault', [ 'dynamicDefault' => $dyn1, 'default' => 'bla', ] ); |
| 81 |
$aggregator->addDynamicDefaults( [ 'another_with_dynamicDefault' => $dyn2 ] ); |
| 82 |
$this->assertNull( $aggregator->getDynamicDefaultDeclarationFor( 'no_dynamicDefault' ) ); |
| 83 |
$this->assertSame( $dyn1, $aggregator->getDynamicDefaultDeclarationFor( 'with_dynamicDefault' ) ); |
| 84 |
$this->assertSame( [ 'dynamicDefault' => $dyn2 ], $aggregator->getSchemaFor( 'another_with_dynamicDefault' ) ); |
| 85 |
$this->assertEquals( [ |
| 86 |
'with_dynamicDefault' => $dyn1, |
| 87 |
'another_with_dynamicDefault' => $dyn2, |
| 88 |
], $aggregator->getDynamicDefaults() ); |
| 89 |
} |
| 90 |
|
| 91 |
public function testGetDefaults() { |
| 92 |
$aggregator = new ConfigSchemaAggregator(); |
| 93 |
$aggregator->addSchema( 'no_default', [ 'type' => 'string', ] ); |
| 94 |
$aggregator->addSchema( 'with_default', [ 'type' => 'string', 'default' => 'bla', ] ); |
| 95 |
$aggregator->addSchema( |
| 96 |
'another_with_default', |
| 97 |
[ 'type' => 'string', 'default' => 'blabla', ] |
| 98 |
); |
| 99 |
|
| 100 |
$this->assertEquals( [ |
| 101 |
'with_default' => 'bla', |
| 102 |
'another_with_default' => 'blabla', |
| 103 |
], $aggregator->getDefaults() ); |
| 104 |
|
| 105 |
$this->assertFalse( $aggregator->hasDefaultFor( 'no_default' ) ); |
| 106 |
$this->assertNull( $aggregator->getDefaultFor( 'no_default' ) ); |
| 107 |
$this->assertTrue( $aggregator->hasDefaultFor( 'with_default' ) ); |
| 108 |
$this->assertSame( 'bla', $aggregator->getDefaultFor( 'with_default' ) ); |
| 109 |
} |
| 110 |
|
| 111 |
public function testGetDefaultsFromProperties() { |
| 112 |
$aggregator = new ConfigSchemaAggregator(); |
| 113 |
$aggregator->addSchema( 'test', [ |
| 114 |
'default' => [ 'a' => 111, 'c' => 333 ], |
| 115 |
'properties' => [ |
| 116 |
'a' => [ 'default' => 222 ], |
| 117 |
'b' => [ |
| 118 |
'properties' => [ |
| 119 |
'b-1' => [ 'default' => 777 ] |
| 120 |
] |
| 121 |
] |
| 122 |
] |
| 123 |
] ); |
| 124 |
|
| 125 |
$expected = [ |
| 126 |
'a' => 222, |
| 127 |
'c' => 333, |
| 128 |
'b' => [ |
| 129 |
'b-1' => 777 |
| 130 |
] |
| 131 |
]; |
| 132 |
|
| 133 |
$this->assertSame( $expected, $aggregator->getDefaultFor( 'test' ) ); |
| 134 |
} |
| 135 |
|
| 136 |
public function testDefaultOverrideFails() { |
| 137 |
$aggregator = new ConfigSchemaAggregator(); |
| 138 |
$aggregator->addSchema( 'foo', [ 'default' => 'bla', ] ); |
| 139 |
$this->expectException( SettingsBuilderException::class ); |
| 140 |
$aggregator->addDefaults( [ 'foo' => 'xyz', ] ); |
| 141 |
} |
| 142 |
|
| 143 |
public function testSetAndGetTypes() { |
| 144 |
$aggregator = new ConfigSchemaAggregator(); |
| 145 |
$aggregator->addSchema( 'no_type', [ 'default' => 'xyz', ] ); |
| 146 |
$aggregator->addSchema( 'with_type', [ 'type' => 'string', 'default' => 'bla', ] ); |
| 147 |
$aggregator->addTypes( [ 'another_with_type' => 'number' ] ); |
| 148 |
$this->assertSame( 'string', $aggregator->getTypeFor( 'with_type' ) ); |
| 149 |
$this->assertSame( [ 'type' => 'number' ], $aggregator->getSchemaFor( 'another_with_type' ) ); |
| 150 |
$this->assertEquals( [ |
| 151 |
'with_type' => 'string', |
| 152 |
'another_with_type' => 'number', |
| 153 |
], $aggregator->getTypes() ); |
| 154 |
} |
| 155 |
|
| 156 |
public function testTypeOverrideFails() { |
| 157 |
$aggregator = new ConfigSchemaAggregator(); |
| 158 |
$aggregator->addSchema( 'foo', [ 'type' => 'string', ] ); |
| 159 |
$this->expectException( SettingsBuilderException::class ); |
| 160 |
$aggregator->addTypes( [ 'foo' => 'number', ] ); |
| 161 |
} |
| 162 |
|
| 163 |
public function testSetAndGetMergeStrategies() { |
| 164 |
$aggregator = new ConfigSchemaAggregator(); |
| 165 |
$aggregator->addSchema( 'no_mergeStrategy', [ 'default' => 'xyz', ] ); |
| 166 |
$aggregator->addSchema( 'with_mergeStrategy', |
| 167 |
[ 'mergeStrategy' => 'array_plus', 'default' => 'bla', ] ); |
| 168 |
$aggregator->addMergeStrategies( [ 'another_with_mergeStrategy' => 'array_merge' ] ); |
| 169 |
$this->assertSame( |
| 170 |
[ 'mergeStrategy' => 'array_merge' ], |
| 171 |
$aggregator->getSchemaFor( 'another_with_mergeStrategy' ) |
| 172 |
); |
| 173 |
$this->assertEquals( [ |
| 174 |
'with_mergeStrategy' => 'array_plus', |
| 175 |
'another_with_mergeStrategy' => 'array_merge', |
| 176 |
], $aggregator->getMergeStrategyNames() ); |
| 177 |
|
| 178 |
foreach ( $aggregator->getMergeStrategies() as $key => $strategy ) { |
| 179 |
$this->assertEquals( $aggregator->getMergeStrategyFor( $key ), $strategy ); |
| 180 |
} |
| 181 |
$this->assertEquals( |
| 182 |
array_keys( $aggregator->getMergeStrategyNames() ), |
| 183 |
array_keys( $aggregator->getMergeStrategies() ) |
| 184 |
); |
| 185 |
} |
| 186 |
|
| 187 |
public function testMergeStrategieOverrideFails() { |
| 188 |
$aggregator = new ConfigSchemaAggregator(); |
| 189 |
$aggregator->addSchema( 'foo', [ 'mergeStrategy' => 'array_merge', ] ); |
| 190 |
$this->expectException( SettingsBuilderException::class ); |
| 191 |
$aggregator->addMergeStrategies( [ 'foo' => 'array_plus', ] ); |
| 192 |
} |
| 193 |
|
| 194 |
public static function provideGetMergeStrategiesFor() { |
| 195 |
yield 'no schema' => [ null, null ]; |
| 196 |
yield 'no strategy' => [ [ 'default' => '' ], null ]; |
| 197 |
yield 'with strategy' => [ [ 'mergeStrategy' => 'array_merge' ], 'array_merge' ]; |
| 198 |
|
| 199 |
yield 'with strategy and type=array' => [ |
| 200 |
[ |
| 201 |
'type' => 'array', |
| 202 |
'mergeStrategy' => 'replace' |
| 203 |
], |
| 204 |
'replace' |
| 205 |
]; |
| 206 |
|
| 207 |
yield 'without strategy and type=array' => [ |
| 208 |
[ 'type' => 'array' ], |
| 209 |
'array_merge' |
| 210 |
]; |
| 211 |
|
| 212 |
yield 'with strategy and type=object' => [ |
| 213 |
[ |
| 214 |
'type' => 'object', |
| 215 |
'mergeStrategy' => 'array_plus_2d' |
| 216 |
], |
| 217 |
'array_plus_2d' |
| 218 |
]; |
| 219 |
|
| 220 |
yield 'without strategy and type=object' => [ |
| 221 |
[ 'type' => 'object' ], |
| 222 |
'array_plus' |
| 223 |
]; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public function testGetMergeStrategyFor( $schema, $expected ) { |
| 230 |
$aggregator = new ConfigSchemaAggregator(); |
| 231 |
|
| 232 |
if ( $schema ) { |
| 233 |
$aggregator->addSchema( 'test', $schema ); |
| 234 |
} |
| 235 |
|
| 236 |
$strategy = $aggregator->getMergeStrategyFor( 'test' ); |
| 237 |
$this->assertSame( |
| 238 |
$expected, |
| 239 |
$strategy ? $strategy->getName() : null |
| 240 |
); |
| 241 |
} |
| 242 |
|
| 243 |
public static function provideValidate() { |
| 244 |
yield 'invalid config' => [ |
| 245 |
'config-schema' => [ 'foo' => [ 'type' => 'string', ], ], |
| 246 |
'config' => [ 'foo' => 1 ], |
| 247 |
'valid' => false, |
| 248 |
]; |
| 249 |
yield 'all good' => [ |
| 250 |
'config-schema' => [ 'foo' => [ 'type' => 'string', ], ], |
| 251 |
'config' => [ 'foo' => 'bar', ], |
| 252 |
'valid' => true, |
| 253 |
]; |
| 254 |
yield 'missing key' => [ |
| 255 |
'config-schema' => [ 'foo' => [ 'type' => 'string', ], ], |
| 256 |
'config' => [ 'bar' => 'bar' ], |
| 257 |
'valid' => false, |
| 258 |
]; |
| 259 |
yield 'no schema was added' => [ |
| 260 |
'config-schema' => [], |
| 261 |
'config' => [ 'foo' => 'bar', ], |
| 262 |
'valid' => true, |
| 263 |
]; |
| 264 |
yield 'key is in config but has no schema' => [ |
| 265 |
'config-schema' => [ 'foo' => [ 'type' => 'array', 'mergeStrategy' => MergeStrategy::ARRAY_MERGE ], ], |
| 266 |
'config' => [ 'foo' => [], 'baz' => false, ], |
| 267 |
'valid' => true, |
| 268 |
]; |
| 269 |
yield 'assoc array where list is expected' => [ |
| 270 |
'config-schema' => [ 'foo' => [ 'type' => 'array', ], ], |
| 271 |
'config' => [ 'foo' => [ 'x' => 1 ] ], |
| 272 |
'valid' => false, |
| 273 |
]; |
| 274 |
yield 'map with numeric keys' => [ |
| 275 |
'config-schema' => [ 'foo' => [ 'type' => 'object', ], ], |
| 276 |
'config' => [ 'foo' => [ 0 => 'x', 1 => 'y' ] ], |
| 277 |
'valid' => true, |
| 278 |
]; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
public function testValidateConfig( array $schemas, array $config, bool $valid ) { |
| 285 |
$aggregator = $this->newConfigSchemaAggregator( $schemas ); |
| 286 |
$status = $aggregator->validateConfig( new HashConfig( $config ) ); |
| 287 |
$this->assertSame( $valid, $status->isOK() ); |
| 288 |
} |
| 289 |
|
| 290 |
public function testValidateValue() { |
| 291 |
$aggregator = $this->newConfigSchemaAggregator( [ 'foo' => [ 'type' => 'integer' ] ] ); |
| 292 |
$this->assertTrue( $aggregator->validateValue( 'foo', 1 )->isOK() ); |
| 293 |
$this->assertFalse( $aggregator->validateValue( 'foo', 'bar' )->isOK() ); |
| 294 |
} |
| 295 |
|
| 296 |
public function testValidateInvalidSchema() { |
| 297 |
$this->expectException( InvalidArgumentException::class ); |
| 298 |
$aggregator = $this->newConfigSchemaAggregator( [ 'foo' => [ 'type' => 1 ] ] ); |
| 299 |
$aggregator->validateConfig( new HashConfig( [ 'foo' => 'bar' ] ) ); |
| 300 |
} |
| 301 |
|
| 302 |
private function newConfigSchemaAggregator( array $schemas = [] ) { |
| 303 |
$aggregator = new ConfigSchemaAggregator(); |
| 304 |
|
| 305 |
foreach ( $schemas as $key => $sch ) { |
| 306 |
$aggregator->addSchema( $key, $sch ); |
| 307 |
} |
| 308 |
|
| 309 |
return $aggregator; |
| 310 |
} |
| 311 |
} |