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 $expectedResult
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));
97 public function moneyDataProvider() {
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.
153 * @dataProvider colorDataProvider
155 * @param $expectedResult
157 public function testColor($inputData, $expectedResult) {
158 $this->assertEquals($expectedResult, CRM_Utils_Rule
::color($inputData));
164 public function colorDataProvider() {
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],
178 ['rgb(255, 255, 255)', FALSE],
185 public function extenionKeyTests() {
187 $keys[] = ['org.civicrm.multisite', TRUE];
188 $keys[] = ['au.org.contribute2016', TRUE];
189 $keys[] = ['%3Csvg%20onload=alert(0)%3E', FALSE];
195 * @param $expectedResult
196 * @dataProvider extenionKeyTests
198 public function testExtenionKeyValid($key, $expectedResult) {
199 $this->assertEquals($expectedResult, CRM_Utils_Rule
::checkExtensionKeyIsValid($key));
205 public function alphanumericData() {
224 "<script>alert('XSS');</script>",
230 foreach ($expectTrue as $value) {
231 $data[] = [$value, TRUE];
233 foreach ($expectFalse as $value) {
234 $data[] = [$value, FALSE];
240 * @dataProvider alphanumericData
244 public function testAlphanumeric($value, $expected) {
245 $this->assertEquals($expected, CRM_Utils_Rule
::alphanumeric($value));