1<?php
2
3namespace Wikimedia\ParamValidator\TypeDef;
4
5use Wikimedia\Message\DataMessageValue;
6use Wikimedia\Message\MessageValue;
7use Wikimedia\ParamValidator\ParamValidator;
8use Wikimedia\ParamValidator\SimpleCallbacks;
9use Wikimedia\ParamValidator\ValidationException;
10
11/**
12 * @covers Wikimedia\ParamValidator\TypeDef\EnumDef
13 */
14class EnumDefTest extends TypeDefTestCase {
15
16	protected function getInstance( SimpleCallbacks $callbacks, array $options ) {
17		return new EnumDef( $callbacks, $options );
18	}
19
20	public function provideValidate() {
21		$settings = [
22			ParamValidator::PARAM_TYPE => [ 'a', 'b', 'c', 'd', 'e' ],
23			EnumDef::PARAM_DEPRECATED_VALUES => [
24				'b' => MessageValue::new( 'not-to-be', [ '??' ] ),
25				'c' => true,
26				'e' => DataMessageValue::new( 'xyz', [ '??' ], 'bogus', [ 'x' => 'y' ] ),
27			],
28		];
29
30		return [
31			'Basic' => [ 'a', 'a', $settings ],
32			'Deprecated' => [ 'c', 'c', $settings, [], [
33				[ 'code' => 'deprecated-value', 'data' => null ],
34			] ],
35			'Deprecated with message' => [
36				'b', 'b', $settings, [], [
37				[ 'code' => 'deprecated-value', 'data' => null ]
38			] ],
39			'Deprecated with data message' => [
40				'e', 'e', $settings, [], [
41				[ 'code' => 'deprecated-value', 'data' => [ 'x' => 'y' ] ]
42			] ],
43			'Deprecated, from default' => [
44				'c', 'c', $settings, [ 'is-default' => true ], []
45			],
46			'Bad value, non-multi' => [
47				'x',
48				new ValidationException(
49					DataMessageValue::new( 'paramvalidator-badvalue-enumnotmulti', [], 'badvalue', [] ),
50					'test', 'x', $settings
51				),
52				$settings,
53			],
54			'Bad value, non-multi but looks like it' => [
55				'x|y',
56				new ValidationException(
57					DataMessageValue::new( 'paramvalidator-badvalue-enumnotmulti', [], 'badvalue', [] ),
58					'test', 'x|y', $settings
59				),
60				$settings,
61			],
62			'Bad value, multi' => [
63				'x|y',
64				new ValidationException(
65					DataMessageValue::new( 'paramvalidator-badvalue-enummulti', [], 'badvalue', [] ),
66					'test', 'x|y', $settings + [ ParamValidator::PARAM_ISMULTI => true ]
67				),
68				$settings + [ ParamValidator::PARAM_ISMULTI => true ],
69				[ 'values-list' => [ 'x|y' ] ],
70			],
71		];
72	}
73
74	public function provideCheckSettings() {
75		return [
76			'Basic test' => [
77				[
78					ParamValidator::PARAM_TYPE => [ 'a', 'b', 'c', 'd', 'e' ],
79				],
80				self::STDRET,
81				[
82					'issues' => [ 'X' ],
83					'allowedKeys' => [ 'Y', EnumDef::PARAM_DEPRECATED_VALUES ],
84					'messages' => [],
85				],
86			],
87			'Bad type for PARAM_DEPRECATED_VALUES' => [
88				[
89					ParamValidator::PARAM_TYPE => [ 'a', 'b', 'c', 'd', 'e' ],
90					EnumDef::PARAM_DEPRECATED_VALUES => false,
91				],
92				self::STDRET,
93				[
94					'issues' => [
95						'X',
96						EnumDef::PARAM_DEPRECATED_VALUES => 'PARAM_DEPRECATED_VALUES must be an array, got boolean',
97					],
98					'allowedKeys' => [ 'Y', EnumDef::PARAM_DEPRECATED_VALUES ],
99					'messages' => [],
100				],
101			],
102			'PARAM_DEPRECATED_VALUES value errors' => [
103				[
104					ParamValidator::PARAM_TYPE => [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 0, '1' ],
105					EnumDef::PARAM_DEPRECATED_VALUES => [
106						'b' => null,
107						'c' => false,
108						'd' => true,
109						'e' => MessageValue::new( 'e' ),
110						'f' => 'f',
111						'g' => $this,
112						0 => true,
113						1 => true,
114						'x' => null,
115					],
116				],
117				self::STDRET,
118				[
119					'issues' => [
120						'X',
121						// phpcs:disable Generic.Files.LineLength
122						'Values in PARAM_DEPRECATED_VALUES must be null, true, or MessageValue, but value for "c" is false',
123						'Values in PARAM_DEPRECATED_VALUES must be null, true, or MessageValue, but value for "f" is string',
124						'Values in PARAM_DEPRECATED_VALUES must be null, true, or MessageValue, but value for "g" is ' . static::class,
125						// phpcs:enable
126						'PARAM_DEPRECATED_VALUES contains "x", which is not one of the enumerated values',
127					],
128					'allowedKeys' => [ 'Y', EnumDef::PARAM_DEPRECATED_VALUES ],
129					'messages' => [
130						MessageValue::new( 'e' ),
131					],
132				],
133			],
134		];
135	}
136
137	public function provideGetEnumValues() {
138		return [
139			'Basic test' => [
140				[ ParamValidator::PARAM_TYPE => [ 'a', 'b', 'c', 'd' ] ],
141				[ 'a', 'b', 'c', 'd' ],
142			],
143		];
144	}
145
146	public function provideStringifyValue() {
147		return [
148			'Basic test' => [ 123, '123' ],
149			'Array' => [ [ 1, 2, 3 ], '1|2|3' ],
150			'Array with pipes' => [ [ 1, 2, '3|4', 5 ], "\x1f1\x1f2\x1f3|4\x1f5" ],
151		];
152	}
153
154	public function provideGetInfo() {
155		return [
156			'Non-multi' => [
157				[
158					ParamValidator::PARAM_TYPE => [ 'a', 'b', 'c', 'd' ],
159				],
160				[
161					'type' => [ 'a', 'b', 'c', 'd' ],
162				],
163				[
164					// phpcs:ignore Generic.Files.LineLength.TooLong
165					ParamValidator::PARAM_TYPE => '<message key="paramvalidator-help-type-enum"><text>1</text><list listType="comma"><text>a</text><text>b</text><text>c</text><text>d</text></list><num>4</num></message>',
166					ParamValidator::PARAM_ISMULTI => null,
167				],
168			],
169			'Multi' => [
170				[
171					ParamValidator::PARAM_TYPE => [ 'a', 'b', 'c', 'd' ],
172					ParamValidator::PARAM_ISMULTI => true,
173				],
174				[
175					'type' => [ 'a', 'b', 'c', 'd' ],
176				],
177				[
178					// phpcs:ignore Generic.Files.LineLength.TooLong
179					ParamValidator::PARAM_TYPE => '<message key="paramvalidator-help-type-enum"><text>2</text><list listType="comma"><text>a</text><text>b</text><text>c</text><text>d</text></list><num>4</num></message>',
180					ParamValidator::PARAM_ISMULTI => null,
181				],
182			],
183			'Deprecated values' => [
184				[
185					ParamValidator::PARAM_TYPE => [ 'a', 'b', 'c', 'd' ],
186					EnumDef::PARAM_DEPRECATED_VALUES => [ 'b' => 'B', 'c' => false, 'x' => true ],
187				],
188				[
189					'type' => [ 'a', 'd', 'b', 'c' ],
190					'deprecatedvalues' => [ 'b', 'c' ],
191				],
192				[
193					// phpcs:ignore Generic.Files.LineLength.TooLong
194					ParamValidator::PARAM_TYPE => '<message key="paramvalidator-help-type-enum"><text>1</text><list listType="comma"><text>a</text><text>d</text><text>b</text><text>c</text></list><num>4</num></message>',
195					ParamValidator::PARAM_ISMULTI => null,
196				],
197			],
198			'Deprecated values are all not allowed values' => [
199				[
200					ParamValidator::PARAM_TYPE => [ 'a', 'b', 'c', 'd' ],
201					EnumDef::PARAM_DEPRECATED_VALUES => [ 'x' => true ],
202				],
203				[
204					'type' => [ 'a', 'b', 'c', 'd' ],
205				],
206				[
207					// phpcs:ignore Generic.Files.LineLength.TooLong
208					ParamValidator::PARAM_TYPE => '<message key="paramvalidator-help-type-enum"><text>1</text><list listType="comma"><text>a</text><text>b</text><text>c</text><text>d</text></list><num>4</num></message>',
209					ParamValidator::PARAM_ISMULTI => null,
210				],
211			],
212			'Empty-string is a value' => [
213				[
214					ParamValidator::PARAM_TYPE => [ '', 'a', 'b', 'c', 'd' ],
215				],
216				[
217					'type' => [ '', 'a', 'b', 'c', 'd' ],
218				],
219				[
220					// phpcs:ignore Generic.Files.LineLength.TooLong
221					ParamValidator::PARAM_TYPE => '<message key="paramvalidator-help-type-enum"><text>1</text><text><message key="paramvalidator-help-type-enum-can-be-empty"><list listType="comma"><text>a</text><text>b</text><text>c</text><text>d</text></list><num>4</num></message></text><num>5</num></message>',
222					ParamValidator::PARAM_ISMULTI => null,
223				],
224			],
225		];
226	}
227
228}
229