issue 1522
[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 $expectedResult
86 */
87 public function testMoney($inputData, $decimalPoint, $thousandSeparator, $currency, $expectedResult) {
88 $this->setDefaultCurrency($currency);
89 $this->setMonetaryDecimalPoint($decimalPoint);
90 $this->setMonetaryThousandSeparator($thousandSeparator);
91 $this->assertEquals($expectedResult, CRM_Utils_Rule::money($inputData));
92 }
93
94 /**
95 * @return array
96 */
97 public function moneyDataProvider() {
98 return [
99 [10, '.', ',', 'USD', TRUE],
100 ['145.0E+3', '.', ',', 'USD', FALSE],
101 ['10', '.', ',', 'USD', TRUE],
102 [-10, '.', ',', 'USD', TRUE],
103 ['-10', '.', ',', 'USD', TRUE],
104 ['-10foo', '.', ',', 'USD', FALSE],
105 ['-10.0345619', '.', ',', 'USD', TRUE],
106 ['-10.010,4345619', '.', ',', 'USD', TRUE],
107 ['10.0104345619', '.', ',', 'USD', TRUE],
108 ['-0', '.', ',', 'USD', TRUE],
109 ['-.1', '.', ',', 'USD', TRUE],
110 ['.1', '.', ',', 'USD', TRUE],
111 // Test currency symbols too, default locale uses $, so if we wanted to test others we'd need to reconfigure locale
112 ['$1,234,567.89', '.', ',', 'USD', TRUE],
113 ['-$1,234,567.89', '.', ',', 'USD', TRUE],
114 ['$-1,234,567.89', '.', ',', 'USD', TRUE],
115 ['1234567.89', '.', ',', 'USD', TRUE], // This is the float format. Encapsulated in strings
116 [1234567.89, '.', ',', 'USD', TRUE], // This is the float format.
117 // Test EURO currency
118 ['€1,234,567.89', '.', ',', 'EUR', TRUE],
119 ['-€1,234,567.89', '.', ',', 'EUR', TRUE],
120 ['€-1,234,567.89', '.', ',', 'EUR', TRUE],
121 ['1234567.89', '.', ',', 'EUR', TRUE], // This is the float format. Encapsulated in strings
122 [1234567.89, '.', ',', 'EUR', TRUE], // This is the float format.
123 // Test Norwegian KR currency
124 ['kr1,234,567.89', '.', ',', 'NOK', TRUE],
125 ['kr 1,234,567.89', '.', ',', 'NOK', TRUE],
126 ['-kr1,234,567.89', '.', ',', 'NOK', TRUE],
127 ['-kr 1,234,567.89', '.', ',', 'NOK', TRUE],
128 ['kr-1,234,567.89', '.', ',', 'NOK', TRUE],
129 ['kr -1,234,567.89', '.', ',', 'NOK', TRUE],
130 ['1234567.89', '.', ',', 'NOK', TRUE], // This is the float format. Encapsulated in strings
131 [1234567.89, '.', ',', 'NOK', TRUE], // This is the float format.
132 // Test different localization options: , as decimal separator and dot as thousand separator
133 ['$1.234.567,89', ',', '.', 'USD', TRUE],
134 ['-$1.234.567,89', ',', '.', 'USD', TRUE],
135 ['$-1.234.567,89', ',', '.', 'USD', TRUE],
136 ['1.234.567,89', ',', '.', 'USD', TRUE],
137 ['1234567.89', ',', '.', 'USD', TRUE], // This is the float format. Encapsulated in strings
138 [1234567.89, ',', '.', 'USD', TRUE], // This is the float format.
139 ['$1,234,567.89', ',', '.', 'USD', FALSE],
140 ['-$1,234,567.89', ',', '.', 'USD', FALSE],
141 ['$-1,234,567.89', ',', '.', 'USD', FALSE],
142 // Now with a space as thousand separator
143 ['$1 234 567,89', ',', ' ', 'USD', TRUE],
144 ['-$1 234 567,89', ',', ' ', 'USD', TRUE],
145 ['$-1 234 567,89', ',', ' ', 'USD', TRUE],
146 ['1 234 567,89', ',', ' ', 'USD', TRUE],
147 ['1234567.89', ',', ' ', 'USD', TRUE], // This is the float format. Encapsulated in strings
148 [1234567.89, ',', ' ', 'USD', TRUE], // This is the float format.
149 ];
150 }
151
152 /**
153 * @dataProvider colorDataProvider
154 * @param $inputData
155 * @param $expectedResult
156 */
157 public function testColor($inputData, $expectedResult) {
158 $this->assertEquals($expectedResult, CRM_Utils_Rule::color($inputData));
159 }
160
161 /**
162 * @return array
163 */
164 public function colorDataProvider() {
165 return [
166 ['#000000', TRUE],
167 ['#ffffff', TRUE],
168 ['#123456', TRUE],
169 ['#00aaff', TRUE],
170 // Some of these are valid css colors but we reject anything that doesn't conform to the html5 spec for <input type="color">
171 ['#ffffff00', FALSE],
172 ['#fff', FALSE],
173 ['##000000', FALSE],
174 ['ffffff', FALSE],
175 ['red', FALSE],
176 ['#orange', FALSE],
177 ['', FALSE],
178 ['rgb(255, 255, 255)', FALSE],
179 ];
180 }
181
182 /**
183 * @return array
184 */
185 public function extenionKeyTests() {
186 $keys = [];
187 $keys[] = ['org.civicrm.multisite', TRUE];
188 $keys[] = ['au.org.contribute2016', TRUE];
189 $keys[] = ['%3Csvg%20onload=alert(0)%3E', FALSE];
190 return $keys;
191 }
192
193 /**
194 * @param $key
195 * @param $expectedResult
196 * @dataProvider extenionKeyTests
197 */
198 public function testExtenionKeyValid($key, $expectedResult) {
199 $this->assertEquals($expectedResult, CRM_Utils_Rule::checkExtensionKeyIsValid($key));
200 }
201
202 /**
203 * @return array
204 */
205 public function alphanumericData() {
206 $expectTrue = [
207 0,
208 999,
209 -5,
210 '',
211 'foo',
212 '0',
213 '-',
214 '_foo',
215 'one-two',
216 'f00',
217 ];
218 $expectFalse = [
219 ' ',
220 5.7,
221 'one two',
222 'one.two',
223 'A<B',
224 "<script>alert('XSS');</script>",
225 '(foo)',
226 'foo;',
227 '[foo]',
228 ];
229 $data = [];
230 foreach ($expectTrue as $value) {
231 $data[] = [$value, TRUE];
232 }
233 foreach ($expectFalse as $value) {
234 $data[] = [$value, FALSE];
235 }
236 return $data;
237 }
238
239 /**
240 * @dataProvider alphanumericData
241 * @param $value
242 * @param $expected
243 */
244 public function testAlphanumeric($value, $expected) {
245 $this->assertEquals($expected, CRM_Utils_Rule::alphanumeric($value));
246 }
247
248 }