Merge pull request #13374 from cividesk/dev-627
[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 array(
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),
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 array(
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),
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 array(
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),
79 );
80 }
81
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 }
114
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
145 /**
146 * @return array
147 */
148 public function extenionKeyTests() {
149 $keys = array();
150 $keys[] = array('org.civicrm.multisite', TRUE);
151 $keys[] = array('au.org.contribute2016', TRUE);
152 $keys[] = array('%3Csvg%20onload=alert(0)%3E', FALSE);
153 return $keys;
154 }
155
156 /**
157 * @param $key
158 * @param $expectedResult
159 * @dataProvider extenionKeyTests
160 */
161 public function testExtenionKeyValid($key, $expectedResult) {
162 $this->assertEquals($expectedResult, CRM_Utils_Rule::checkExtensionKeyIsValid($key));
163 }
164
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',
179 'f00',
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;',
190 '[foo]',
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
211 }