4 * Class CRM_Utils_RuleTest
7 class CRM_Utils_RuleTest
extends CiviUnitTestCase
{
9 public function setUp() {
14 * @dataProvider integerDataProvider
16 * @param $expectedResult
18 public function testInteger($inputData, $expectedResult) {
19 $this->assertEquals($expectedResult, CRM_Utils_Rule
::integer($inputData));
25 public function integerDataProvider() {
37 * @dataProvider positiveDataProvider
39 * @param $expectedResult
41 public function testPositive($inputData, $expectedResult) {
42 $this->assertEquals($expectedResult, CRM_Utils_Rule
::positiveInteger($inputData));
48 public function positiveDataProvider() {
60 * @dataProvider numericDataProvider
62 * @param $expectedResult
64 public function testNumeric($inputData, $expectedResult) {
65 $this->assertEquals($expectedResult, CRM_Utils_Rule
::numeric($inputData));
71 public function numericDataProvider() {
83 * @dataProvider moneyDataProvider
85 * @param $decimalPoint
86 * @param $thousandSeparator
88 * @param $expectedResult
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));
100 public function moneyDataProvider() {
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],
166 * @dataProvider colorDataProvider
168 * @param $expectedResult
170 public function testColor($inputData, $expectedResult) {
171 $this->assertEquals($expectedResult, CRM_Utils_Rule
::color($inputData));
177 public function colorDataProvider() {
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],
191 ['rgb(255, 255, 255)', FALSE],
198 public function extenionKeyTests() {
200 $keys[] = ['org.civicrm.multisite', TRUE];
201 $keys[] = ['au.org.contribute2016', TRUE];
202 $keys[] = ['%3Csvg%20onload=alert(0)%3E', FALSE];
208 * @param $expectedResult
209 * @dataProvider extenionKeyTests
211 public function testExtenionKeyValid($key, $expectedResult) {
212 $this->assertEquals($expectedResult, CRM_Utils_Rule
::checkExtensionKeyIsValid($key));
218 public function alphanumericData() {
237 "<script>alert('XSS');</script>",
243 foreach ($expectTrue as $value) {
244 $data[] = [$value, TRUE];
246 foreach ($expectFalse as $value) {
247 $data[] = [$value, FALSE];
253 * @dataProvider alphanumericData
257 public function testAlphanumeric($value, $expected) {
258 $this->assertEquals($expected, CRM_Utils_Rule
::alphanumeric($value));