Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 47 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
AutoModeratorConfigValidation | |
0.00% |
0 / 47 |
|
0.00% |
0 / 5 |
132 | |
0.00% |
0 / 1 |
getConfigDescriptors | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
2 | |||
validateField | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
20 | |||
validate | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
validateVariable | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getDefaultContent | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace AutoModerator\Config\Validation; |
4 | |
5 | use AutoModerator\Config\AutoModeratorWikiConfigLoader; |
6 | use InvalidArgumentException; |
7 | use MediaWiki\Message\Message; |
8 | use StatusValue; |
9 | |
10 | /** |
11 | * Validation class for MediaWiki:AutoModeratorConfig.json |
12 | */ |
13 | class AutoModeratorConfigValidation implements IConfigValidator { |
14 | use DatatypeValidationTrait; |
15 | |
16 | private function getConfigDescriptors(): array { |
17 | return [ |
18 | 'AutoModeratorEnableRevisionCheck' => [ |
19 | 'type' => 'bool', |
20 | ], |
21 | 'AutoModeratorFalsePositivePageTitle' => [ |
22 | 'type' => '?string', |
23 | ], |
24 | 'AutoModeratorSkipUserGroups' => [ |
25 | 'type' => 'array' |
26 | ], |
27 | 'AutoModeratorUseEditFlagMinor' => [ |
28 | 'type' => 'bool', |
29 | ], |
30 | 'AutoModeratorRevertTalkPageMessageEnabled' => [ |
31 | 'type' => 'bool', |
32 | ], |
33 | 'AutoModeratorEnableBotFlag' => [ |
34 | 'type' => 'bool', |
35 | ] |
36 | ]; |
37 | } |
38 | |
39 | /** |
40 | * Validate a given field |
41 | * |
42 | * @param string $fieldName Name of the field to be validated |
43 | * @param array $descriptor Descriptor of the field ( |
44 | * @param array $data |
45 | * @return StatusValue |
46 | */ |
47 | private function validateField( |
48 | string $fieldName, |
49 | array $descriptor, |
50 | array $data |
51 | ): StatusValue { |
52 | // validate is supposed to make sure $data has $field as a key, |
53 | // so this should not throw key errors. |
54 | $value = $data[$fieldName]; |
55 | |
56 | $expectedType = $descriptor['type']; |
57 | if ( !$this->validateFieldDatatype( $expectedType, $value ) ) { |
58 | return StatusValue::newFatal( |
59 | 'automoderator-config-validator-datatype-mismatch', |
60 | $fieldName, |
61 | $expectedType, |
62 | gettype( $value ) |
63 | ); |
64 | } |
65 | |
66 | if ( isset( $descriptor['maxSize'] ) && count( $value ) > $descriptor['maxSize'] ) { |
67 | return StatusValue::newFatal( |
68 | 'automoderator-config-validator-array-toobig', |
69 | $fieldName, |
70 | Message::numParam( $descriptor['maxSize'] ) |
71 | ); |
72 | } |
73 | |
74 | return StatusValue::newGood(); |
75 | } |
76 | |
77 | /** |
78 | * @inheritDoc |
79 | */ |
80 | public function validate( array $data ): StatusValue { |
81 | $status = StatusValue::newGood(); |
82 | foreach ( $this->getConfigDescriptors() as $field => $descriptor ) { |
83 | if ( !array_key_exists( $field, $data ) ) { |
84 | // No need to validate something we're not setting |
85 | continue; |
86 | } |
87 | |
88 | $status->merge( $this->validateField( $field, $descriptor, $data ) ); |
89 | } |
90 | |
91 | return $status; |
92 | } |
93 | |
94 | /** |
95 | * @inheritDoc |
96 | */ |
97 | public function validateVariable( string $variable, $value ): void { |
98 | if ( !in_array( $variable, AutoModeratorWikiConfigLoader::ALLOW_LIST ) ) { |
99 | throw new InvalidArgumentException( |
100 | 'Invalid attempt to set a variable via WikiPageConfigWriter' |
101 | ); |
102 | } |
103 | } |
104 | |
105 | /** |
106 | * @inheritDoc |
107 | */ |
108 | public function getDefaultContent(): array { |
109 | return []; |
110 | } |
111 | } |