Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / ParamValidator / TypeDef / IntegerDefTest.php
1 <?php
2
3 namespace Wikimedia\ParamValidator\TypeDef;
4
5 use Wikimedia\ParamValidator\ParamValidator;
6 use Wikimedia\ParamValidator\ValidationException;
7
8 /**
9 * @covers Wikimedia\ParamValidator\TypeDef\IntegerDef
10 */
11 class IntegerDefTest extends TypeDefTestCase {
12
13 protected static $testClass = IntegerDef::class;
14
15 /**
16 * @param string $v Representing a positive integer
17 * @return string Representing $v + 1
18 */
19 private static function plusOne( $v ) {
20 for ( $i = strlen( $v ) - 1; $i >= 0; $i-- ) {
21 if ( $v[$i] === '9' ) {
22 $v[$i] = '0';
23 } else {
24 $v[$i] = $v[$i] + 1;
25 return $v;
26 }
27 }
28 return '1' . $v;
29 }
30
31 public function provideValidate() {
32 $badinteger = new ValidationException( 'test', '...', [], 'badinteger', [] );
33 $belowminimum = new ValidationException(
34 'test', '...', [], 'belowminimum', [ 'min' => 0, 'max' => 2, 'max2' => '' ]
35 );
36 $abovemaximum = new ValidationException(
37 'test', '...', [], 'abovemaximum', [ 'min' => 0, 'max' => 2, 'max2' => '' ]
38 );
39 $abovemaximum2 = new ValidationException(
40 'test', '...', [], 'abovemaximum', [ 'min' => 0, 'max' => 2, 'max2' => 4 ]
41 );
42 $abovehighmaximum = new ValidationException(
43 'test', '...', [], 'abovehighmaximum', [ 'min' => 0, 'max' => 2, 'max2' => 4 ]
44 );
45 $asWarn = function ( ValidationException $ex ) {
46 return [ $ex->getFailureCode() ] + $ex->getFailureData();
47 };
48
49 $minmax = [
50 IntegerDef::PARAM_MIN => 0,
51 IntegerDef::PARAM_MAX => 2,
52 ];
53 $minmax2 = [
54 IntegerDef::PARAM_MIN => 0,
55 IntegerDef::PARAM_MAX => 2,
56 IntegerDef::PARAM_MAX2 => 4,
57 ];
58 $ignore = [
59 IntegerDef::PARAM_IGNORE_RANGE => true,
60 ];
61 $usehigh = [ 'useHighLimits' => true ];
62
63 return [
64 [ '123', 123 ],
65 [ '-123', -123 ],
66 [ '000123', 123 ],
67 [ '000', 0 ],
68 [ '-0', 0 ],
69 [ (string)PHP_INT_MAX, PHP_INT_MAX ],
70 [ '0000' . PHP_INT_MAX, PHP_INT_MAX ],
71 [ (string)PHP_INT_MIN, PHP_INT_MIN ],
72 [ '-0000' . substr( PHP_INT_MIN, 1 ), PHP_INT_MIN ],
73
74 'Overflow' => [ self::plusOne( (string)PHP_INT_MAX ), $badinteger ],
75 'Negative overflow' => [ '-' . self::plusOne( substr( PHP_INT_MIN, 1 ) ), $badinteger ],
76
77 'Float' => [ '1.5', $badinteger ],
78 'Float (e notation)' => [ '1e1', $badinteger ],
79 'Bad sign (space)' => [ ' 1', $badinteger ],
80 'Bad sign (newline)' => [ "\n1", $badinteger ],
81 'Bogus value' => [ 'foo', $badinteger ],
82 'Bogus value (2)' => [ '1foo', $badinteger ],
83 'Hex value' => [ '0x123', $badinteger ],
84 'Newline' => [ "1\n", $badinteger ],
85
86 'Ok with range' => [ '1', 1, $minmax ],
87 'Below minimum' => [ '-1', $belowminimum, $minmax ],
88 'Below minimum, ignored' => [ '-1', 0, $minmax + $ignore, [], [ $asWarn( $belowminimum ) ] ],
89 'Above maximum' => [ '3', $abovemaximum, $minmax ],
90 'Above maximum, ignored' => [ '3', 2, $minmax + $ignore, [], [ $asWarn( $abovemaximum ) ] ],
91 'Not above max2 but can\'t use it' => [ '3', $abovemaximum2, $minmax2, [] ],
92 'Not above max2 but can\'t use it, ignored'
93 => [ '3', 2, $minmax2 + $ignore, [], [ $asWarn( $abovemaximum2 ) ] ],
94 'Not above max2' => [ '3', 3, $minmax2, $usehigh ],
95 'Above max2' => [ '5', $abovehighmaximum, $minmax2, $usehigh ],
96 'Above max2, ignored'
97 => [ '5', 4, $minmax2 + $ignore, $usehigh, [ $asWarn( $abovehighmaximum ) ] ],
98 ];
99 }
100
101 public function provideNormalizeSettings() {
102 return [
103 [ [], [] ],
104 [
105 [ IntegerDef::PARAM_MAX => 2 ],
106 [ IntegerDef::PARAM_MAX => 2 ],
107 ],
108 [
109 [ IntegerDef::PARAM_MIN => 1, IntegerDef::PARAM_MAX => 2, IntegerDef::PARAM_MAX2 => 4 ],
110 [ IntegerDef::PARAM_MIN => 1, IntegerDef::PARAM_MAX => 2, IntegerDef::PARAM_MAX2 => 4 ],
111 ],
112 [
113 [ IntegerDef::PARAM_MIN => 1, IntegerDef::PARAM_MAX => 4, IntegerDef::PARAM_MAX2 => 2 ],
114 [ IntegerDef::PARAM_MIN => 1, IntegerDef::PARAM_MAX => 4, IntegerDef::PARAM_MAX2 => 4 ],
115 ],
116 [
117 [ IntegerDef::PARAM_MAX2 => 2 ],
118 [],
119 ],
120 ];
121 }
122
123 public function provideDescribeSettings() {
124 return [
125 'Basic' => [ [], [], [] ],
126 'Default' => [
127 [ ParamValidator::PARAM_DEFAULT => 123 ],
128 [ 'default' => '123' ],
129 [ 'default' => [ 'value' => '123' ] ],
130 ],
131 'Min' => [
132 [ ParamValidator::PARAM_DEFAULT => 123, IntegerDef::PARAM_MIN => 0 ],
133 [ 'default' => '123', 'min' => 0 ],
134 [ 'default' => [ 'value' => '123' ], 'min' => [ 'min' => 0, 'max' => '', 'max2' => '' ] ],
135 ],
136 'Max' => [
137 [ IntegerDef::PARAM_MAX => 2 ],
138 [ 'max' => 2 ],
139 [ 'max' => [ 'min' => '', 'max' => 2, 'max2' => '' ] ],
140 ],
141 'Max2' => [
142 [ IntegerDef::PARAM_MAX => 2, IntegerDef::PARAM_MAX2 => 4 ],
143 [ 'max' => 2, 'max2' => 4 ],
144 [ 'max2' => [ 'min' => '', 'max' => 2, 'max2' => 4 ] ],
145 ],
146 'Minmax' => [
147 [ IntegerDef::PARAM_MIN => 0, IntegerDef::PARAM_MAX => 2 ],
148 [ 'min' => 0, 'max' => 2 ],
149 [ 'minmax' => [ 'min' => 0, 'max' => 2, 'max2' => '' ] ],
150 ],
151 'Minmax2' => [
152 [ IntegerDef::PARAM_MIN => 0, IntegerDef::PARAM_MAX => 2, IntegerDef::PARAM_MAX2 => 4 ],
153 [ 'min' => 0, 'max' => 2, 'max2' => 4 ],
154 [ 'minmax2' => [ 'min' => 0, 'max' => 2, 'max2' => 4 ] ],
155 ],
156 ];
157 }
158
159 }