Commit | Line | Data |
---|---|---|
f942c321 DL |
1 | <?php |
2 | ||
aba1cd8b EM |
3 | /** |
4 | * Class CRM_Utils_TypeTest | |
7cec4a9a CB |
5 | * @package CiviCRM |
6 | * @subpackage CRM_Utils_Type | |
acb109b7 | 7 | * @group headless |
aba1cd8b | 8 | */ |
f942c321 DL |
9 | class CRM_Utils_TypeTest extends CiviUnitTestCase { |
10 | ||
00be9182 | 11 | public function setUp() { |
f942c321 DL |
12 | parent::setUp(); |
13 | } | |
14 | ||
15 | /** | |
16 | * @dataProvider validateDataProvider | |
1e1fdcf6 EM |
17 | * @param $inputData |
18 | * @param $inputType | |
19 | * @param $expectedResult | |
f942c321 | 20 | */ |
00be9182 | 21 | public function testValidate($inputData, $inputType, $expectedResult) { |
258570f7 | 22 | $this->assertTrue($expectedResult === CRM_Utils_Type::validate($inputData, $inputType, FALSE)); |
f942c321 DL |
23 | } |
24 | ||
e9479dcf EM |
25 | /** |
26 | * @return array | |
27 | */ | |
00be9182 | 28 | public function validateDataProvider() { |
f942c321 DL |
29 | return array( |
30 | array(10, 'Int', 10), | |
31 | array('145E+3', 'Int', NULL), | |
32 | array('10', 'Integer', 10), | |
33 | array(-10, 'Int', -10), | |
34 | array('-10', 'Integer', -10), | |
35 | array('-10foo', 'Int', NULL), | |
36 | array(10, 'Positive', 10), | |
37 | array('145.0E+3', 'Positive', NULL), | |
38 | array('10', 'Positive', 10), | |
39 | array(-10, 'Positive', NULL), | |
40 | array('-10', 'Positive', NULL), | |
41 | array('-10foo', 'Positive', NULL), | |
f19a5565 MM |
42 | array('civicrm_column_name', 'MysqlColumnNameOrAlias', 'civicrm_column_name'), |
43 | array('table.civicrm_column_name', 'MysqlColumnNameOrAlias', 'table.civicrm_column_name'), | |
44 | array('table.civicrm_column_name.toomanydots', 'MysqlColumnNameOrAlias', NULL), | |
62d6d1c0 | 45 | array('Home-street_address', 'MysqlColumnNameOrAlias', 'Home-street_address'), |
f19a5565 | 46 | array('`Home-street_address`', 'MysqlColumnNameOrAlias', '`Home-street_address`'), |
62d6d1c0 | 47 | array('`Home-street_address', 'MysqlColumnNameOrAlias', NULL), |
f19a5565 MM |
48 | array('table.`Home-street_address`', 'MysqlColumnNameOrAlias', 'table.`Home-street_address`'), |
49 | array('`table-alias`.`Home-street_address`', 'MysqlColumnNameOrAlias', '`table-alias`.`Home-street_address`'), | |
50 | array('`table-alias`.column', 'MysqlColumnNameOrAlias', '`table-alias`.column'), | |
7cec4a9a CB |
51 | // Spaces also permitted, only when enclosed in backticks. |
52 | array('`column alias`', 'MysqlColumnNameOrAlias', '`column alias`'), | |
53 | array('`table alias`.column', 'MysqlColumnNameOrAlias', '`table alias`.column'), | |
54 | array('`table alias`.`column alias`', 'MysqlColumnNameOrAlias', '`table alias`.`column alias`'), | |
55 | array('table alias.column alias', 'MysqlColumnNameOrAlias', NULL), | |
56 | array('table alias.column_alias', 'MysqlColumnNameOrAlias', NULL), | |
57 | array('table_alias.column alias', 'MysqlColumnNameOrAlias', NULL), | |
58 | // Functions are not permitted. | |
f19a5565 | 59 | array('column_name, sleep(5)', 'MysqlColumnNameOrAlias', NULL), |
7cec4a9a | 60 | // Length checking permits only 64 chars. |
f19a5565 MM |
61 | array(str_repeat('a', 64), 'MysqlColumnNameOrAlias', str_repeat('a', 64)), |
62 | array(str_repeat('a', 65), 'MysqlColumnNameOrAlias', NULL), | |
63 | array(str_repeat('a', 64) . '.' . str_repeat('a', 64), 'MysqlColumnNameOrAlias', str_repeat('a', 64) . '.' . str_repeat('a', 64)), | |
7cec4a9a | 64 | array('`' . str_repeat('a', 64) . '`.`' . str_repeat('b', 64) . '`', 'MysqlColumnNameOrAlias', '`' . str_repeat('a', 64) . '`.`' . str_repeat('b', 64) . '`'), |
f19a5565 MM |
65 | array(str_repeat('a', 64) . '.' . str_repeat('a', 65), 'MysqlColumnNameOrAlias', NULL), |
66 | array(str_repeat('a', 65) . '.' . str_repeat('a', 64), 'MysqlColumnNameOrAlias', NULL), | |
7cec4a9a | 67 | // ORDER BY can be ASC or DESC, case not significant. |
0fa4baf0 MM |
68 | array('asc', 'MysqlOrderByDirection', 'asc'), |
69 | array('DESC', 'MysqlOrderByDirection', 'desc'), | |
70 | array('DESCc', 'MysqlOrderByDirection', NULL), | |
71 | array('table.civicrm_column_name desc', 'MysqlOrderBy', 'table.civicrm_column_name desc'), | |
9d5c7f14 | 72 | array('field(civicrm_column_name,4,5,6)', 'MysqlOrderBy', 'field(civicrm_column_name,4,5,6)'), |
73 | array('field(table.civicrm_column_name,4,5,6)', 'MysqlOrderBy', 'field(table.civicrm_column_name,4,5,6)'), | |
96cb6be9 | 74 | array('table.civicrm_column_name desc,other_column, another_column desc', 'MysqlOrderBy', 'table.civicrm_column_name desc,other_column, another_column desc'), |
a33b83c5 | 75 | array('table.`Home-street_address` asc, `table-alias`.`Home-street_address` desc,`table-alias`.column', 'MysqlOrderBy', 'table.`Home-street_address` asc, `table-alias`.`Home-street_address` desc,`table-alias`.column'), |
828a2e9f | 76 | array('a string', 'String', 'a string'), |
f942c321 DL |
77 | ); |
78 | } | |
96025800 | 79 | |
258570f7 CW |
80 | /** |
81 | * @dataProvider escapeDataProvider | |
82 | * @param $inputData | |
83 | * @param $inputType | |
84 | * @param $expectedResult | |
85 | */ | |
86 | public function testEscape($inputData, $inputType, $expectedResult) { | |
87 | $this->assertTrue($expectedResult === CRM_Utils_Type::escape($inputData, $inputType, FALSE)); | |
88 | } | |
89 | ||
90 | /** | |
91 | * @return array | |
92 | */ | |
93 | public function escapeDataProvider() { | |
94 | return array( | |
95 | array(10, 'Int', 10), | |
96 | array('145E+3', 'Int', NULL), | |
97 | array('10', 'Integer', 10), | |
98 | array(-10, 'Int', -10), | |
99 | array(array(), 'Integer', NULL), | |
100 | array('-10foo', 'Int', NULL), | |
101 | array(10, 'Positive', 10), | |
102 | array('145.0E+3', 'Positive', NULL), | |
103 | array('10', 'Positive', 10), | |
104 | array(-10, 'Positive', NULL), | |
105 | array('-10', 'Positive', NULL), | |
106 | array('-10foo', 'Positive', NULL), | |
88ccd161 | 107 | array(array('10', 20), 'Country', array('10', 20)), |
258570f7 CW |
108 | array(array('10', '-10foo'), 'Country', NULL), |
109 | array('', 'Timestamp', ''), | |
110 | array('', 'ContactReference', ''), | |
111 | array('3', 'ContactReference', 3), | |
112 | array('-3', 'ContactReference', NULL), | |
113 | // Escape function is meant for sql, not xss | |
114 | array('<p onclick="alert(\'xss\');">Hello</p>', 'Memo', '<p onclick=\\"alert(\\\'xss\\\');\\">Hello</p>'), | |
f19a5565 MM |
115 | array('civicrm_column_name', 'MysqlColumnNameOrAlias', '`civicrm_column_name`'), |
116 | array('table.civicrm_column_name', 'MysqlColumnNameOrAlias', '`table`.`civicrm_column_name`'), | |
117 | array('table.civicrm_column_name.toomanydots', 'MysqlColumnNameOrAlias', NULL), | |
62d6d1c0 | 118 | array('Home-street_address', 'MysqlColumnNameOrAlias', '`Home-street_address`'), |
f19a5565 | 119 | array('`Home-street_address`', 'MysqlColumnNameOrAlias', '`Home-street_address`'), |
62d6d1c0 | 120 | array('`Home-street_address', 'MysqlColumnNameOrAlias', NULL), |
f19a5565 | 121 | array('column_name, sleep(5)', 'MysqlColumnNameOrAlias', NULL), |
0fa4baf0 MM |
122 | array('asc', 'MysqlOrderByDirection', 'asc'), |
123 | array('DESC', 'MysqlOrderByDirection', 'desc'), | |
124 | array('DESCc', 'MysqlOrderByDirection', NULL), | |
125 | array('table.civicrm_column_name desc', 'MysqlOrderBy', '`table`.`civicrm_column_name` desc'), | |
9d5c7f14 | 126 | array('field(contribution_status_id,4,5,6) asc', 'MysqlOrderBy', 'field(`contribution_status_id`,4,5,6) asc'), |
127 | array('field(contribution_status_id,4,5,6) asc, contact_id asc', 'MysqlOrderBy', 'field(`contribution_status_id`,4,5,6) asc, `contact_id` asc'), | |
0fa4baf0 | 128 | array('table.civicrm_column_name desc,other_column,another_column desc', 'MysqlOrderBy', '`table`.`civicrm_column_name` desc, `other_column`, `another_column` desc'), |
a33b83c5 | 129 | array('table.`Home-street_address` asc, `table-alias`.`Home-street_address` desc,`table-alias`.column', 'MysqlOrderBy', '`table`.`Home-street_address` asc, `table-alias`.`Home-street_address` desc, `table-alias`.`column`'), |
258570f7 CW |
130 | ); |
131 | } | |
132 | ||
f942c321 | 133 | } |