Commit | Line | Data |
---|---|---|
f942c321 DL |
1 | <?php |
2 | ||
aba1cd8b EM |
3 | /** |
4 | * Class CRM_Utils_RuleTest | |
acb109b7 | 5 | * @group headless |
aba1cd8b | 6 | */ |
f942c321 DL |
7 | class CRM_Utils_RuleTest extends CiviUnitTestCase { |
8 | ||
00be9182 | 9 | public function setUp() { |
f942c321 DL |
10 | parent::setUp(); |
11 | } | |
12 | ||
13 | /** | |
14 | * @dataProvider integerDataProvider | |
1e1fdcf6 EM |
15 | * @param $inputData |
16 | * @param $expectedResult | |
f942c321 | 17 | */ |
00be9182 | 18 | public function testInteger($inputData, $expectedResult) { |
f942c321 DL |
19 | $this->assertEquals($expectedResult, CRM_Utils_Rule::integer($inputData)); |
20 | } | |
21 | ||
e9479dcf EM |
22 | /** |
23 | * @return array | |
24 | */ | |
00be9182 | 25 | public function integerDataProvider() { |
9099cab3 CW |
26 | return [ |
27 | [10, TRUE], | |
28 | ['145E+3', FALSE], | |
29 | ['10', TRUE], | |
30 | [-10, TRUE], | |
31 | ['-10', TRUE], | |
32 | ['-10foo', FALSE], | |
33 | ]; | |
f942c321 DL |
34 | } |
35 | ||
36 | /** | |
37 | * @dataProvider positiveDataProvider | |
1e1fdcf6 EM |
38 | * @param $inputData |
39 | * @param $expectedResult | |
f942c321 | 40 | */ |
00be9182 | 41 | public function testPositive($inputData, $expectedResult) { |
69244ea1 | 42 | $this->assertEquals($expectedResult, CRM_Utils_Rule::positiveInteger($inputData)); |
f942c321 DL |
43 | } |
44 | ||
4cbe18b8 EM |
45 | /** |
46 | * @return array | |
47 | */ | |
00be9182 | 48 | public function positiveDataProvider() { |
9099cab3 CW |
49 | return [ |
50 | [10, TRUE], | |
51 | ['145.0E+3', FALSE], | |
52 | ['10', TRUE], | |
53 | [-10, FALSE], | |
54 | ['-10', FALSE], | |
55 | ['-10foo', FALSE], | |
56 | ]; | |
f942c321 DL |
57 | } |
58 | ||
59 | /** | |
60 | * @dataProvider numericDataProvider | |
1e1fdcf6 EM |
61 | * @param $inputData |
62 | * @param $expectedResult | |
f942c321 | 63 | */ |
00be9182 | 64 | public function testNumeric($inputData, $expectedResult) { |
69244ea1 | 65 | $this->assertEquals($expectedResult, CRM_Utils_Rule::numeric($inputData)); |
f942c321 DL |
66 | } |
67 | ||
4cbe18b8 EM |
68 | /** |
69 | * @return array | |
70 | */ | |
00be9182 | 71 | public function numericDataProvider() { |
9099cab3 CW |
72 | return [ |
73 | [10, TRUE], | |
74 | ['145.0E+3', FALSE], | |
75 | ['10', TRUE], | |
76 | [-10, TRUE], | |
77 | ['-10', TRUE], | |
78 | ['-10foo', FALSE], | |
79 | ]; | |
f942c321 DL |
80 | } |
81 | ||
970c1413 MW |
82 | /** |
83 | * @dataProvider moneyDataProvider | |
84 | * @param $inputData | |
a8978922 JJ |
85 | * @param $decimalPoint |
86 | * @param $thousandSeparator | |
87 | * @param $currency | |
970c1413 MW |
88 | * @param $expectedResult |
89 | */ | |
dff93dda JJ |
90 | public function testMoney($inputData, $decimalPoint, $thousandSeparator, $currency, $expectedResult) { |
91 | $this->setDefaultCurrency($currency); | |
92 | $this->setMonetaryDecimalPoint($decimalPoint); | |
93 | $this->setMonetaryThousandSeparator($thousandSeparator); | |
970c1413 MW |
94 | $this->assertEquals($expectedResult, CRM_Utils_Rule::money($inputData)); |
95 | } | |
96 | ||
97 | /** | |
98 | * @return array | |
99 | */ | |
100 | public function moneyDataProvider() { | |
9099cab3 | 101 | return [ |
dff93dda JJ |
102 | [10, '.', ',', 'USD', TRUE], |
103 | ['145.0E+3', '.', ',', 'USD', FALSE], | |
104 | ['10', '.', ',', 'USD', TRUE], | |
105 | [-10, '.', ',', 'USD', TRUE], | |
106 | ['-10', '.', ',', 'USD', TRUE], | |
107 | ['-10foo', '.', ',', 'USD', FALSE], | |
108 | ['-10.0345619', '.', ',', 'USD', TRUE], | |
109 | ['-10.010,4345619', '.', ',', 'USD', TRUE], | |
110 | ['10.0104345619', '.', ',', 'USD', TRUE], | |
111 | ['-0', '.', ',', 'USD', TRUE], | |
112 | ['-.1', '.', ',', 'USD', TRUE], | |
113 | ['.1', '.', ',', 'USD', TRUE], | |
970c1413 | 114 | // Test currency symbols too, default locale uses $, so if we wanted to test others we'd need to reconfigure locale |
dff93dda JJ |
115 | ['$1,234,567.89', '.', ',', 'USD', TRUE], |
116 | ['-$1,234,567.89', '.', ',', 'USD', TRUE], | |
117 | ['$-1,234,567.89', '.', ',', 'USD', TRUE], | |
80da79bb JJ |
118 | // This is the float format. Encapsulated in strings |
119 | ['1234567.89', '.', ',', 'USD', TRUE], | |
120 | // This is the float format. | |
121 | [1234567.89, '.', ',', 'USD', TRUE], | |
dff93dda JJ |
122 | // Test EURO currency |
123 | ['€1,234,567.89', '.', ',', 'EUR', TRUE], | |
124 | ['-€1,234,567.89', '.', ',', 'EUR', TRUE], | |
125 | ['€-1,234,567.89', '.', ',', 'EUR', TRUE], | |
80da79bb JJ |
126 | // This is the float format. Encapsulated in strings |
127 | ['1234567.89', '.', ',', 'EUR', TRUE], | |
128 | // This is the float format. | |
129 | [1234567.89, '.', ',', 'EUR', TRUE], | |
dff93dda JJ |
130 | // Test Norwegian KR currency |
131 | ['kr1,234,567.89', '.', ',', 'NOK', TRUE], | |
132 | ['kr 1,234,567.89', '.', ',', 'NOK', TRUE], | |
133 | ['-kr1,234,567.89', '.', ',', 'NOK', TRUE], | |
134 | ['-kr 1,234,567.89', '.', ',', 'NOK', TRUE], | |
135 | ['kr-1,234,567.89', '.', ',', 'NOK', TRUE], | |
136 | ['kr -1,234,567.89', '.', ',', 'NOK', TRUE], | |
80da79bb JJ |
137 | // This is the float format. Encapsulated in strings |
138 | ['1234567.89', '.', ',', 'NOK', TRUE], | |
139 | // This is the float format. | |
140 | [1234567.89, '.', ',', 'NOK', TRUE], | |
dff93dda JJ |
141 | // Test different localization options: , as decimal separator and dot as thousand separator |
142 | ['$1.234.567,89', ',', '.', 'USD', TRUE], | |
143 | ['-$1.234.567,89', ',', '.', 'USD', TRUE], | |
144 | ['$-1.234.567,89', ',', '.', 'USD', TRUE], | |
145 | ['1.234.567,89', ',', '.', 'USD', TRUE], | |
80da79bb JJ |
146 | // This is the float format. Encapsulated in strings |
147 | ['1234567.89', ',', '.', 'USD', TRUE], | |
148 | // This is the float format. | |
149 | [1234567.89, ',', '.', 'USD', TRUE], | |
dff93dda JJ |
150 | ['$1,234,567.89', ',', '.', 'USD', FALSE], |
151 | ['-$1,234,567.89', ',', '.', 'USD', FALSE], | |
152 | ['$-1,234,567.89', ',', '.', 'USD', FALSE], | |
153 | // Now with a space as thousand separator | |
154 | ['$1 234 567,89', ',', ' ', 'USD', TRUE], | |
155 | ['-$1 234 567,89', ',', ' ', 'USD', TRUE], | |
156 | ['$-1 234 567,89', ',', ' ', 'USD', TRUE], | |
157 | ['1 234 567,89', ',', ' ', 'USD', TRUE], | |
80da79bb JJ |
158 | // This is the float format. Encapsulated in strings |
159 | ['1234567.89', ',', ' ', 'USD', TRUE], | |
160 | // This is the float format. | |
161 | [1234567.89, ',', ' ', 'USD', TRUE], | |
9099cab3 | 162 | ]; |
970c1413 | 163 | } |
17a8b8a4 | 164 | |
8a52ae34 CW |
165 | /** |
166 | * @dataProvider colorDataProvider | |
167 | * @param $inputData | |
168 | * @param $expectedResult | |
169 | */ | |
170 | public function testColor($inputData, $expectedResult) { | |
171 | $this->assertEquals($expectedResult, CRM_Utils_Rule::color($inputData)); | |
172 | } | |
173 | ||
174 | /** | |
175 | * @return array | |
176 | */ | |
177 | public function colorDataProvider() { | |
178 | return [ | |
179 | ['#000000', TRUE], | |
180 | ['#ffffff', TRUE], | |
181 | ['#123456', TRUE], | |
182 | ['#00aaff', TRUE], | |
183 | // Some of these are valid css colors but we reject anything that doesn't conform to the html5 spec for <input type="color"> | |
184 | ['#ffffff00', FALSE], | |
185 | ['#fff', FALSE], | |
186 | ['##000000', FALSE], | |
187 | ['ffffff', FALSE], | |
188 | ['red', FALSE], | |
189 | ['#orange', FALSE], | |
190 | ['', FALSE], | |
191 | ['rgb(255, 255, 255)', FALSE], | |
192 | ]; | |
193 | } | |
194 | ||
5df85a46 SL |
195 | /** |
196 | * @return array | |
197 | */ | |
198 | public function extenionKeyTests() { | |
9099cab3 CW |
199 | $keys = []; |
200 | $keys[] = ['org.civicrm.multisite', TRUE]; | |
201 | $keys[] = ['au.org.contribute2016', TRUE]; | |
202 | $keys[] = ['%3Csvg%20onload=alert(0)%3E', FALSE]; | |
5df85a46 SL |
203 | return $keys; |
204 | } | |
205 | ||
206 | /** | |
207 | * @param $key | |
208 | * @param $expectedResult | |
209 | * @dataProvider extenionKeyTests | |
210 | */ | |
211 | public function testExtenionKeyValid($key, $expectedResult) { | |
9e1d9d01 | 212 | $this->assertEquals($expectedResult, CRM_Utils_Rule::checkExtensionKeyIsValid($key)); |
5df85a46 SL |
213 | } |
214 | ||
d22982f3 SM |
215 | /** |
216 | * @return array | |
217 | */ | |
218 | public function alphanumericData() { | |
219 | $expectTrue = [ | |
220 | 0, | |
221 | 999, | |
222 | -5, | |
223 | '', | |
224 | 'foo', | |
225 | '0', | |
226 | '-', | |
227 | '_foo', | |
228 | 'one-two', | |
39b959db | 229 | 'f00', |
d22982f3 SM |
230 | ]; |
231 | $expectFalse = [ | |
232 | ' ', | |
233 | 5.7, | |
234 | 'one two', | |
235 | 'one.two', | |
236 | 'A<B', | |
237 | "<script>alert('XSS');</script>", | |
238 | '(foo)', | |
239 | 'foo;', | |
39b959db | 240 | '[foo]', |
d22982f3 SM |
241 | ]; |
242 | $data = []; | |
243 | foreach ($expectTrue as $value) { | |
244 | $data[] = [$value, TRUE]; | |
245 | } | |
246 | foreach ($expectFalse as $value) { | |
247 | $data[] = [$value, FALSE]; | |
248 | } | |
249 | return $data; | |
250 | } | |
251 | ||
252 | /** | |
253 | * @dataProvider alphanumericData | |
254 | * @param $value | |
255 | * @param $expected | |
256 | */ | |
257 | public function testAlphanumeric($value, $expected) { | |
258 | $this->assertEquals($expected, CRM_Utils_Rule::alphanumeric($value)); | |
259 | } | |
260 | ||
f942c321 | 261 | } |