Merge in 5.26
[civicrm-core.git] / tests / phpunit / CRM / Utils / RuleTest.php
1 <?php
2
3 /**
4 * Class CRM_Utils_RuleTest
5 * @group headless
6 */
7 class CRM_Utils_RuleTest extends CiviUnitTestCase {
8
9 public function setUp() {
10 parent::setUp();
11 }
12
13 /**
14 * @dataProvider integerDataProvider
15 * @param $inputData
16 * @param $expectedResult
17 */
18 public function testInteger($inputData, $expectedResult) {
19 $this->assertEquals($expectedResult, CRM_Utils_Rule::integer($inputData));
20 }
21
22 /**
23 * @return array
24 */
25 public function integerDataProvider() {
26 return [
27 [10, TRUE],
28 ['145E+3', FALSE],
29 ['10', TRUE],
30 [-10, TRUE],
31 ['-10', TRUE],
32 ['-10foo', FALSE],
33 ];
34 }
35
36 /**
37 * @dataProvider positiveDataProvider
38 * @param $inputData
39 * @param $expectedResult
40 */
41 public function testPositive($inputData, $expectedResult) {
42 $this->assertEquals($expectedResult, CRM_Utils_Rule::positiveInteger($inputData));
43 }
44
45 /**
46 * @return array
47 */
48 public function positiveDataProvider() {
49 return [
50 [10, TRUE],
51 ['145.0E+3', FALSE],
52 ['10', TRUE],
53 [-10, FALSE],
54 ['-10', FALSE],
55 ['-10foo', FALSE],
56 ];
57 }
58
59 /**
60 * @dataProvider numericDataProvider
61 * @param $inputData
62 * @param $expectedResult
63 */
64 public function testNumeric($inputData, $expectedResult) {
65 $this->assertEquals($expectedResult, CRM_Utils_Rule::numeric($inputData));
66 }
67
68 /**
69 * @return array
70 */
71 public function numericDataProvider() {
72 return [
73 [10, TRUE],
74 ['145.0E+3', FALSE],
75 ['10', TRUE],
76 [-10, TRUE],
77 ['-10', TRUE],
78 ['-10foo', FALSE],
79 ];
80 }
81
82 /**
83 * @dataProvider moneyDataProvider
84 * @param $inputData
85 * @param $decimalPoint
86 * @param $thousandSeparator
87 * @param $currency
88 * @param $expectedResult
89 */
90 public function testMoney($inputData, $decimalPoint, $thousandSeparator, $currency, $expectedResult) {
91 $this->setDefaultCurrency($currency);
92 $this->setMonetaryDecimalPoint($decimalPoint);
93 $this->setMonetaryThousandSeparator($thousandSeparator);
94 $this->assertEquals($expectedResult, CRM_Utils_Rule::money($inputData));
95 }
96
97 /**
98 * @return array
99 */
100 public function moneyDataProvider() {
101 return [
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],
114 // Test currency symbols too, default locale uses $, so if we wanted to test others we'd need to reconfigure locale
115 ['$1,234,567.89', '.', ',', 'USD', TRUE],
116 ['-$1,234,567.89', '.', ',', 'USD', TRUE],
117 ['$-1,234,567.89', '.', ',', 'USD', TRUE],
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],
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],
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],
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],
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],
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],
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],
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],
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],
162 ];
163 }
164
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
195 /**
196 * @return array
197 */
198 public function extenionKeyTests() {
199 $keys = [];
200 $keys[] = ['org.civicrm.multisite', TRUE];
201 $keys[] = ['au.org.contribute2016', TRUE];
202 $keys[] = ['%3Csvg%20onload=alert(0)%3E', FALSE];
203 return $keys;
204 }
205
206 /**
207 * @param $key
208 * @param $expectedResult
209 * @dataProvider extenionKeyTests
210 */
211 public function testExtenionKeyValid($key, $expectedResult) {
212 $this->assertEquals($expectedResult, CRM_Utils_Rule::checkExtensionKeyIsValid($key));
213 }
214
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',
229 'f00',
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;',
240 '[foo]',
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
261 }