File: ConfigSchemaAggregatorTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (311 lines) | stat: -rw-r--r-- 11,011 bytes parent folder | download
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
<?php

namespace MediaWiki\Tests\Unit\Settings\Config;

use InvalidArgumentException;
use MediaWiki\Config\HashConfig;
use MediaWiki\Settings\Config\ConfigSchemaAggregator;
use MediaWiki\Settings\Config\MergeStrategy;
use MediaWiki\Settings\SettingsBuilderException;
use PHPUnit\Framework\TestCase;

/**
 * @covers \MediaWiki\Settings\Config\ConfigSchemaAggregator
 */
class ConfigSchemaAggregatorTest extends TestCase {

	public function testAddSchema() {
		$aggregator = new ConfigSchemaAggregator();
		$aggregator->addSchema( 'foo', [ 'type' => 'string', ] );
		$aggregator->addSchema( 'bar', [ 'type' => 'number', ] );
		$this->assertTrue( $aggregator->hasSchemaFor( 'foo' ) );
		$this->assertFalse( $aggregator->hasSchemaFor( 'xyzzy' ) );
		$this->assertSame( [ 'type' => 'number', ], $aggregator->getSchemaFor( 'bar' ) );
	}

	public function testAddSchemaMulti() {
		$aggregator = new ConfigSchemaAggregator();
		$aggregator->addSchemaMulti( [
			'foo' => [ 'type' => 'string', ],
			'bar' => [ 'type' => 'number', ],
		] );
		$this->assertTrue( $aggregator->hasSchemaFor( 'foo' ) );
		$this->assertFalse( $aggregator->hasSchemaFor( 'xyzzy' ) );
		$this->assertSame( [ 'type' => 'number', ], $aggregator->getSchemaFor( 'bar' ) );
	}

	public function testCombineSchema() {
		$aggregator = new ConfigSchemaAggregator();
		$aggregator->addTypes( [ 'foo' => 'string', ] );
		$aggregator->addSchema( 'bar', [ 'type' => 'number', ] );
		$aggregator->addSchema( 'foo', [ 'default' => 'X', ] );
		$this->assertTrue( $aggregator->hasSchemaFor( 'foo' ) );
		$this->assertTrue( $aggregator->hasSchemaFor( 'bar' ) );
		$this->assertFalse( $aggregator->hasSchemaFor( 'xyzzy' ) );
		$this->assertSame( [ 'type' => 'number', ], $aggregator->getSchemaFor( 'bar' ) );
		$this->assertSame( 'X', $aggregator->getDefaultFor( 'foo' ) );
		$this->assertSame( 'string', $aggregator->getTypeFor( 'foo' ) );
	}

	public function testAddSchemaOverrideFails() {
		$aggregator = new ConfigSchemaAggregator();
		$aggregator->addTypes( [ 'foo' => 'int', ] );

		$this->expectException( SettingsBuilderException::class );
		$aggregator->addSchema( 'foo', [ 'type' => 'string', ] );
	}

	public function testSetAndGetDefaults() {
		$aggregator = new ConfigSchemaAggregator();
		$aggregator->addSchema( 'no_default', [ 'type' => 'string', ] );
		$aggregator->addSchema( 'with_default', [ 'type' => 'string', 'default' => 'bla', ] );
		$aggregator->addDefaults( [ 'another_with_default' => 'blabla' ] );
		$this->assertFalse( $aggregator->hasDefaultFor( 'no_default' ) );
		$this->assertTrue( $aggregator->hasDefaultFor( 'with_default' ) );
		$this->assertTrue( $aggregator->hasDefaultFor( 'another_with_default' ) );
		$this->assertSame( 'bla', $aggregator->getDefaultFor( 'with_default' ) );
		$this->assertSame( [ 'default' => 'blabla' ], $aggregator->getSchemaFor( 'another_with_default' ) );
		$this->assertEquals( [
			'with_default' => 'bla',
			'another_with_default' => 'blabla',
		], $aggregator->getDefaults() );
	}

	public function testSetAndGetDynamicDefaults() {
		$dyn1 = [ 'callback' => 'function1' ];
		$dyn2 = [ 'callback' => 'function2', 'use' => [ 'Stuff' ] ];

		$aggregator = new ConfigSchemaAggregator();
		$aggregator->addSchema( 'no_dynamicDefault', [ 'default' => 'xyz', ] );
		$aggregator->addSchema( 'with_dynamicDefault', [ 'dynamicDefault' => $dyn1, 'default' => 'bla', ] );
		$aggregator->addDynamicDefaults( [ 'another_with_dynamicDefault' => $dyn2 ] );
		$this->assertNull( $aggregator->getDynamicDefaultDeclarationFor( 'no_dynamicDefault' ) );
		$this->assertSame( $dyn1, $aggregator->getDynamicDefaultDeclarationFor( 'with_dynamicDefault' ) );
		$this->assertSame( [ 'dynamicDefault' => $dyn2 ], $aggregator->getSchemaFor( 'another_with_dynamicDefault' ) );
		$this->assertEquals( [
			'with_dynamicDefault' => $dyn1,
			'another_with_dynamicDefault' => $dyn2,
		], $aggregator->getDynamicDefaults() );
	}

	public function testGetDefaults() {
		$aggregator = new ConfigSchemaAggregator();
		$aggregator->addSchema( 'no_default', [ 'type' => 'string', ] );
		$aggregator->addSchema( 'with_default', [ 'type' => 'string', 'default' => 'bla', ] );
		$aggregator->addSchema(
			'another_with_default',
			[ 'type' => 'string', 'default' => 'blabla', ]
		);

		$this->assertEquals( [
			'with_default' => 'bla',
			'another_with_default' => 'blabla',
		], $aggregator->getDefaults() );

		$this->assertFalse( $aggregator->hasDefaultFor( 'no_default' ) );
		$this->assertNull( $aggregator->getDefaultFor( 'no_default' ) );
		$this->assertTrue( $aggregator->hasDefaultFor( 'with_default' ) );
		$this->assertSame( 'bla', $aggregator->getDefaultFor( 'with_default' ) );
	}

	public function testGetDefaultsFromProperties() {
		$aggregator = new ConfigSchemaAggregator();
		$aggregator->addSchema( 'test', [
			'default' => [ 'a' => 111, 'c' => 333 ],
			'properties' => [
				'a' => [ 'default' => 222 ],
				'b' => [
					'properties' => [
						'b-1' => [ 'default' => 777 ]
					]
				]
			]
		] );

		$expected = [
			'a' => 222, // overwritten by property
			'c' => 333, // kept from top level
			'b' => [ // complex property
				'b-1' => 777 // nested default
			]
		];

		$this->assertSame( $expected, $aggregator->getDefaultFor( 'test' ) );
	}

	public function testDefaultOverrideFails() {
		$aggregator = new ConfigSchemaAggregator();
		$aggregator->addSchema( 'foo', [ 'default' => 'bla', ] );
		$this->expectException( SettingsBuilderException::class );
		$aggregator->addDefaults( [ 'foo' => 'xyz', ] );
	}

	public function testSetAndGetTypes() {
		$aggregator = new ConfigSchemaAggregator();
		$aggregator->addSchema( 'no_type', [ 'default' => 'xyz', ] );
		$aggregator->addSchema( 'with_type', [ 'type' => 'string', 'default' => 'bla', ] );
		$aggregator->addTypes( [ 'another_with_type' => 'number' ] );
		$this->assertSame( 'string', $aggregator->getTypeFor( 'with_type' ) );
		$this->assertSame( [ 'type' => 'number' ], $aggregator->getSchemaFor( 'another_with_type' ) );
		$this->assertEquals( [
			'with_type' => 'string',
			'another_with_type' => 'number',
		], $aggregator->getTypes() );
	}

	public function testTypeOverrideFails() {
		$aggregator = new ConfigSchemaAggregator();
		$aggregator->addSchema( 'foo', [ 'type' => 'string', ] );
		$this->expectException( SettingsBuilderException::class );
		$aggregator->addTypes( [ 'foo' => 'number', ] );
	}

	public function testSetAndGetMergeStrategies() {
		$aggregator = new ConfigSchemaAggregator();
		$aggregator->addSchema( 'no_mergeStrategy', [ 'default' => 'xyz', ] );
		$aggregator->addSchema( 'with_mergeStrategy',
			[ 'mergeStrategy' => 'array_plus', 'default' => 'bla', ] );
		$aggregator->addMergeStrategies( [ 'another_with_mergeStrategy' => 'array_merge' ] );
		$this->assertSame(
			[ 'mergeStrategy' => 'array_merge' ],
			$aggregator->getSchemaFor( 'another_with_mergeStrategy' )
		);
		$this->assertEquals( [
			'with_mergeStrategy' => 'array_plus',
			'another_with_mergeStrategy' => 'array_merge',
		], $aggregator->getMergeStrategyNames() );

		foreach ( $aggregator->getMergeStrategies() as $key => $strategy ) {
			$this->assertEquals( $aggregator->getMergeStrategyFor( $key ), $strategy );
		}
		$this->assertEquals(
			array_keys( $aggregator->getMergeStrategyNames() ),
			array_keys( $aggregator->getMergeStrategies() )
		);
	}

	public function testMergeStrategieOverrideFails() {
		$aggregator = new ConfigSchemaAggregator();
		$aggregator->addSchema( 'foo', [ 'mergeStrategy' => 'array_merge', ] );
		$this->expectException( SettingsBuilderException::class );
		$aggregator->addMergeStrategies( [ 'foo' => 'array_plus', ] );
	}

	public static function provideGetMergeStrategiesFor() {
		yield 'no schema' => [ null, null ];
		yield 'no strategy' => [ [ 'default' => '' ], null ];
		yield 'with strategy' => [ [ 'mergeStrategy' => 'array_merge' ], 'array_merge' ];

		yield 'with strategy and type=array' => [
			[
				'type' => 'array',
				'mergeStrategy' => 'replace'
			],
			'replace'
		];

		yield 'without strategy and type=array' => [
			[ 'type' => 'array' ],
			'array_merge'
		];

		yield 'with strategy and type=object' => [
			[
				'type' => 'object',
				'mergeStrategy' => 'array_plus_2d'
			],
			'array_plus_2d'
		];

		yield 'without strategy and type=object' => [
			[ 'type' => 'object' ],
			'array_plus'
		];
	}

	/**
	 * @dataProvider provideGetMergeStrategiesFor
	 */
	public function testGetMergeStrategyFor( $schema, $expected ) {
		$aggregator = new ConfigSchemaAggregator();

		if ( $schema ) {
			$aggregator->addSchema( 'test', $schema );
		}

		$strategy = $aggregator->getMergeStrategyFor( 'test' );
		$this->assertSame(
			$expected,
			$strategy ? $strategy->getName() : null
		);
	}

	public static function provideValidate() {
		yield 'invalid config' => [
			'config-schema' => [ 'foo' => [ 'type' => 'string', ], ],
			'config' => [ 'foo' => 1 ],
			'valid' => false,
		];
		yield 'all good' => [
			'config-schema' => [ 'foo' => [ 'type' => 'string', ], ],
			'config' => [ 'foo' => 'bar', ],
			'valid' => true,
		];
		yield 'missing key' => [
			'config-schema' => [ 'foo' => [ 'type' => 'string', ], ],
			'config' => [ 'bar' => 'bar' ],
			'valid' => false,
		];
		yield 'no schema was added' => [
			'config-schema' => [],
			'config' => [ 'foo' => 'bar', ],
			'valid' => true,
		];
		yield 'key is in config but has no schema' => [
			'config-schema' => [ 'foo' => [ 'type' => 'array', 'mergeStrategy' => MergeStrategy::ARRAY_MERGE ], ],
			'config' => [ 'foo' => [], 'baz' => false, ],
			'valid' => true,
		];
		yield 'assoc array where list is expected' => [
			'config-schema' => [ 'foo' => [ 'type' => 'array', ], ],
			'config' => [ 'foo' => [ 'x' => 1 ] ],
			'valid' => false,
		];
		yield 'map with numeric keys' => [
			'config-schema' => [ 'foo' => [ 'type' => 'object', ], ],
			'config' => [ 'foo' => [ 0 => 'x', 1 => 'y' ] ],
			'valid' => true,
		];
	}

	/**
	 * @dataProvider provideValidate
	 */
	public function testValidateConfig( array $schemas, array $config, bool $valid ) {
		$aggregator = $this->newConfigSchemaAggregator( $schemas );
		$status = $aggregator->validateConfig( new HashConfig( $config ) );
		$this->assertSame( $valid, $status->isOK() );
	}

	public function testValidateValue() {
		$aggregator = $this->newConfigSchemaAggregator( [ 'foo' => [ 'type' => 'integer' ] ] );
		$this->assertTrue( $aggregator->validateValue( 'foo', 1 )->isOK() );
		$this->assertFalse( $aggregator->validateValue( 'foo', 'bar' )->isOK() );
	}

	public function testValidateInvalidSchema() {
		$this->expectException( InvalidArgumentException::class );
		$aggregator = $this->newConfigSchemaAggregator( [ 'foo' => [ 'type' => 1 ] ] );
		$aggregator->validateConfig( new HashConfig( [ 'foo' => 'bar' ] ) );
	}

	private function newConfigSchemaAggregator( array $schemas = [] ) {
		$aggregator = new ConfigSchemaAggregator();

		foreach ( $schemas as $key => $sch ) {
			$aggregator->addSchema( $key, $sch );
		}

		return $aggregator;
	}
}