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 | |
85 | * @param $expectedResult | |
86 | */ | |
87 | public function testMoney($inputData, $expectedResult) { | |
88 | $this->assertEquals($expectedResult, CRM_Utils_Rule::money($inputData)); | |
89 | } | |
90 | ||
91 | /** | |
92 | * @return array | |
93 | */ | |
94 | public function moneyDataProvider() { | |
9099cab3 CW |
95 | return [ |
96 | [10, TRUE], | |
97 | ['145.0E+3', FALSE], | |
98 | ['10', TRUE], | |
99 | [-10, TRUE], | |
100 | ['-10', TRUE], | |
101 | ['-10foo', FALSE], | |
102 | ['-10.0345619', TRUE], | |
103 | ['-10.010,4345619', TRUE], | |
104 | ['10.0104345619', TRUE], | |
105 | ['-0', TRUE], | |
106 | ['-.1', TRUE], | |
107 | ['.1', TRUE], | |
970c1413 | 108 | // Test currency symbols too, default locale uses $, so if we wanted to test others we'd need to reconfigure locale |
9099cab3 CW |
109 | ['$500.3333', TRUE], |
110 | ['-$500.3333', TRUE], | |
111 | ['$-500.3333', TRUE], | |
112 | ]; | |
970c1413 | 113 | } |
17a8b8a4 | 114 | |
8a52ae34 CW |
115 | /** |
116 | * @dataProvider colorDataProvider | |
117 | * @param $inputData | |
118 | * @param $expectedResult | |
119 | */ | |
120 | public function testColor($inputData, $expectedResult) { | |
121 | $this->assertEquals($expectedResult, CRM_Utils_Rule::color($inputData)); | |
122 | } | |
123 | ||
124 | /** | |
125 | * @return array | |
126 | */ | |
127 | public function colorDataProvider() { | |
128 | return [ | |
129 | ['#000000', TRUE], | |
130 | ['#ffffff', TRUE], | |
131 | ['#123456', TRUE], | |
132 | ['#00aaff', TRUE], | |
133 | // Some of these are valid css colors but we reject anything that doesn't conform to the html5 spec for <input type="color"> | |
134 | ['#ffffff00', FALSE], | |
135 | ['#fff', FALSE], | |
136 | ['##000000', FALSE], | |
137 | ['ffffff', FALSE], | |
138 | ['red', FALSE], | |
139 | ['#orange', FALSE], | |
140 | ['', FALSE], | |
141 | ['rgb(255, 255, 255)', FALSE], | |
142 | ]; | |
143 | } | |
144 | ||
5df85a46 SL |
145 | /** |
146 | * @return array | |
147 | */ | |
148 | public function extenionKeyTests() { | |
9099cab3 CW |
149 | $keys = []; |
150 | $keys[] = ['org.civicrm.multisite', TRUE]; | |
151 | $keys[] = ['au.org.contribute2016', TRUE]; | |
152 | $keys[] = ['%3Csvg%20onload=alert(0)%3E', FALSE]; | |
5df85a46 SL |
153 | return $keys; |
154 | } | |
155 | ||
156 | /** | |
157 | * @param $key | |
158 | * @param $expectedResult | |
159 | * @dataProvider extenionKeyTests | |
160 | */ | |
161 | public function testExtenionKeyValid($key, $expectedResult) { | |
9e1d9d01 | 162 | $this->assertEquals($expectedResult, CRM_Utils_Rule::checkExtensionKeyIsValid($key)); |
5df85a46 SL |
163 | } |
164 | ||
d22982f3 SM |
165 | /** |
166 | * @return array | |
167 | */ | |
168 | public function alphanumericData() { | |
169 | $expectTrue = [ | |
170 | 0, | |
171 | 999, | |
172 | -5, | |
173 | '', | |
174 | 'foo', | |
175 | '0', | |
176 | '-', | |
177 | '_foo', | |
178 | 'one-two', | |
39b959db | 179 | 'f00', |
d22982f3 SM |
180 | ]; |
181 | $expectFalse = [ | |
182 | ' ', | |
183 | 5.7, | |
184 | 'one two', | |
185 | 'one.two', | |
186 | 'A<B', | |
187 | "<script>alert('XSS');</script>", | |
188 | '(foo)', | |
189 | 'foo;', | |
39b959db | 190 | '[foo]', |
d22982f3 SM |
191 | ]; |
192 | $data = []; | |
193 | foreach ($expectTrue as $value) { | |
194 | $data[] = [$value, TRUE]; | |
195 | } | |
196 | foreach ($expectFalse as $value) { | |
197 | $data[] = [$value, FALSE]; | |
198 | } | |
199 | return $data; | |
200 | } | |
201 | ||
202 | /** | |
203 | * @dataProvider alphanumericData | |
204 | * @param $value | |
205 | * @param $expected | |
206 | */ | |
207 | public function testAlphanumeric($value, $expected) { | |
208 | $this->assertEquals($expected, CRM_Utils_Rule::alphanumeric($value)); | |
209 | } | |
210 | ||
f942c321 | 211 | } |