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, $expectedResult) {
88 $this->assertEquals($expectedResult, CRM_Utils_Rule
::money($inputData));
94 public function moneyDataProvider() {
102 ['-10.0345619', TRUE],
103 ['-10.010,4345619', TRUE],
104 ['10.0104345619', TRUE],
108 // Test currency symbols too, default locale uses $, so if we wanted to test others we'd need to reconfigure locale
110 ['-$500.3333', TRUE],
111 ['$-500.3333', TRUE],
116 * @dataProvider colorDataProvider
118 * @param $expectedResult
120 public function testColor($inputData, $expectedResult) {
121 $this->assertEquals($expectedResult, CRM_Utils_Rule
::color($inputData));
127 public function colorDataProvider() {
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],
141 ['rgb(255, 255, 255)', FALSE],
148 public function extenionKeyTests() {
150 $keys[] = ['org.civicrm.multisite', TRUE];
151 $keys[] = ['au.org.contribute2016', TRUE];
152 $keys[] = ['%3Csvg%20onload=alert(0)%3E', FALSE];
158 * @param $expectedResult
159 * @dataProvider extenionKeyTests
161 public function testExtenionKeyValid($key, $expectedResult) {
162 $this->assertEquals($expectedResult, CRM_Utils_Rule
::checkExtensionKeyIsValid($key));
168 public function alphanumericData() {
187 "<script>alert('XSS');</script>",
193 foreach ($expectTrue as $value) {
194 $data[] = [$value, TRUE];
196 foreach ($expectFalse as $value) {
197 $data[] = [$value, FALSE];
203 * @dataProvider alphanumericData
207 public function testAlphanumeric($value, $expected) {
208 $this->assertEquals($expected, CRM_Utils_Rule
::alphanumeric($value));