1<?php
2
3namespace Wikimedia\ParamValidator\TypeDef;
4
5use Wikimedia\ParamValidator\ParamValidator;
6use Wikimedia\ParamValidator\SimpleCallbacks;
7
8/**
9 * @covers Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef
10 */
11class PresenceBooleanDefTest extends TypeDefTestCase {
12
13	protected function getInstance( SimpleCallbacks $callbacks, array $options ) {
14		return new PresenceBooleanDef( $callbacks, $options );
15	}
16
17	public function provideValidate() {
18		return [
19			[ null, false ],
20			[ '', true ],
21			[ '0', true ],
22			[ '1', true ],
23			[ 'anything really', true ],
24		];
25	}
26
27	public function provideNormalizeSettings() {
28		return [
29			[
30				[],
31				[ ParamValidator::PARAM_ISMULTI => false, ParamValidator::PARAM_DEFAULT => false ],
32			],
33			[
34				[ ParamValidator::PARAM_ISMULTI => true ],
35				[ ParamValidator::PARAM_ISMULTI => false, ParamValidator::PARAM_DEFAULT => false ],
36			],
37			[
38				[ ParamValidator::PARAM_DEFAULT => null ],
39				[ ParamValidator::PARAM_DEFAULT => null, ParamValidator::PARAM_ISMULTI => false ],
40			],
41		];
42	}
43
44	public function provideCheckSettings() {
45		return [
46			'Basic test' => [
47				[],
48				self::STDRET,
49				self::STDRET,
50			],
51			'PARAM_ISMULTI not allowed' => [
52				[
53					ParamValidator::PARAM_ISMULTI => true,
54				],
55				self::STDRET,
56				[
57					'issues' => [
58						'X',
59						ParamValidator::PARAM_ISMULTI
60							=> 'PARAM_ISMULTI cannot be used for presence-boolean-type parameters',
61					],
62					'allowedKeys' => [ 'Y' ],
63					'messages' => [],
64				],
65			],
66			'PARAM_ISMULTI not allowed, but another ISMULTI issue was already logged' => [
67				[
68					ParamValidator::PARAM_ISMULTI => true,
69				],
70				[
71					'issues' => [
72						ParamValidator::PARAM_ISMULTI => 'XXX',
73					],
74					'allowedKeys' => [ 'Y' ],
75					'messages' => [],
76				],
77				[
78					'issues' => [
79						ParamValidator::PARAM_ISMULTI => 'XXX',
80					],
81					'allowedKeys' => [ 'Y' ],
82					'messages' => [],
83				],
84			],
85			'PARAM_DEFAULT can be false' => [
86				[ ParamValidator::PARAM_DEFAULT => false ],
87				self::STDRET,
88				self::STDRET,
89			],
90			'PARAM_DEFAULT can be null' => [
91				[ ParamValidator::PARAM_DEFAULT => null ],
92				self::STDRET,
93				self::STDRET,
94			],
95			'PARAM_DEFAULT cannot be true' => [
96				[
97					ParamValidator::PARAM_DEFAULT => true,
98				],
99				self::STDRET,
100				[
101					'issues' => [
102						'X',
103						ParamValidator::PARAM_DEFAULT
104							=> 'Default for presence-boolean-type parameters must be false or null',
105					],
106					'allowedKeys' => [ 'Y' ],
107					'messages' => [],
108				],
109			],
110			'PARAM_DEFAULT invalid, but another DEFAULT issue was already logged' => [
111				[
112					ParamValidator::PARAM_DEFAULT => true,
113				],
114				[
115					'issues' => [
116						ParamValidator::PARAM_DEFAULT => 'XXX',
117					],
118					'allowedKeys' => [ 'Y' ],
119					'messages' => [],
120				],
121				[
122					'issues' => [
123						ParamValidator::PARAM_DEFAULT => 'XXX',
124					],
125					'allowedKeys' => [ 'Y' ],
126					'messages' => [],
127				],
128			],
129		];
130	}
131
132	public function provideGetInfo() {
133		return [
134			'Basic test' => [
135				[],
136				[ 'default' => null ],
137				[
138					// phpcs:ignore Generic.Files.LineLength.TooLong
139					ParamValidator::PARAM_TYPE => '<message key="paramvalidator-help-type-presenceboolean"><text>1</text></message>',
140					ParamValidator::PARAM_DEFAULT => null,
141				],
142			],
143		];
144	}
145
146}
147