Merge pull request #11010 from seamuslee001/CRM-21206
[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 * @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) {
132 $this->assertEquals($expectedResult, CRM_Utils_Rule::checkExtesnionKeyIsValid($key));
133 }
134
135 }