Merge pull request #13653 from MegaphoneJon/reporting-8
[civicrm-core.git] / tests / phpunit / CRM / Utils / RuleTest.php
CommitLineData
f942c321
DL
1<?php
2
aba1cd8b
EM
3/**
4 * Class CRM_Utils_RuleTest
acb109b7 5 * @group headless
aba1cd8b 6 */
f942c321
DL
7class 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() {
f942c321 26 return array(
a0631214
TO
27 array(10, TRUE),
28 array('145E+3', FALSE),
29 array('10', TRUE),
30 array(-10, TRUE),
31 array('-10', TRUE),
32 array('-10foo', FALSE),
f942c321
DL
33 );
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() {
f942c321 49 return array(
a0631214
TO
50 array(10, TRUE),
51 array('145.0E+3', FALSE),
52 array('10', TRUE),
53 array(-10, FALSE),
54 array('-10', FALSE),
55 array('-10foo', FALSE),
f942c321
DL
56 );
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() {
f942c321 72 return array(
a0631214
TO
73 array(10, TRUE),
74 array('145.0E+3', FALSE),
75 array('10', TRUE),
76 array(-10, TRUE),
77 array('-10', TRUE),
78 array('-10foo', FALSE),
f942c321
DL
79 );
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() {
95 return array(
96 array(10, TRUE),
97 array('145.0E+3', FALSE),
98 array('10', TRUE),
99 array(-10, TRUE),
100 array('-10', TRUE),
101 array('-10foo', FALSE),
102 array('-10.0345619', TRUE),
103 array('-10.010,4345619', TRUE),
104 array('10.0104345619', TRUE),
105 array('-0', TRUE),
106 array('-.1', TRUE),
107 array('.1', TRUE),
108 // Test currency symbols too, default locale uses $, so if we wanted to test others we'd need to reconfigure locale
109 array('$500.3333', TRUE),
110 array('-$500.3333', TRUE),
111 array('$-500.3333', TRUE),
112 );
113 }
17a8b8a4 114
5df85a46
SL
115 /**
116 * @return array
117 */
118 public function extenionKeyTests() {
119 $keys = array();
120 $keys[] = array('org.civicrm.multisite', TRUE);
121 $keys[] = array('au.org.contribute2016', TRUE);
122 $keys[] = array('%3Csvg%20onload=alert(0)%3E', FALSE);
123 return $keys;
124 }
125
126 /**
127 * @param $key
128 * @param $expectedResult
129 * @dataProvider extenionKeyTests
130 */
131 public function testExtenionKeyValid($key, $expectedResult) {
9e1d9d01 132 $this->assertEquals($expectedResult, CRM_Utils_Rule::checkExtensionKeyIsValid($key));
5df85a46
SL
133 }
134
d22982f3
SM
135 /**
136 * @return array
137 */
138 public function alphanumericData() {
139 $expectTrue = [
140 0,
141 999,
142 -5,
143 '',
144 'foo',
145 '0',
146 '-',
147 '_foo',
148 'one-two',
149 'f00'
150 ];
151 $expectFalse = [
152 ' ',
153 5.7,
154 'one two',
155 'one.two',
156 'A<B',
157 "<script>alert('XSS');</script>",
158 '(foo)',
159 'foo;',
160 '[foo]'
161 ];
162 $data = [];
163 foreach ($expectTrue as $value) {
164 $data[] = [$value, TRUE];
165 }
166 foreach ($expectFalse as $value) {
167 $data[] = [$value, FALSE];
168 }
169 return $data;
170 }
171
172 /**
173 * @dataProvider alphanumericData
174 * @param $value
175 * @param $expected
176 */
177 public function testAlphanumeric($value, $expected) {
178 $this->assertEquals($expected, CRM_Utils_Rule::alphanumeric($value));
179 }
180
f942c321 181}