4 * Class CRM_Utils_TypeTest
6 * @subpackage CRM_Utils_Type
9 class CRM_Utils_TypeTest
extends CiviUnitTestCase
{
12 * @dataProvider validateDataProvider
15 * @param $expectedResult
17 public function testValidate($inputData, $inputType, $expectedResult) {
18 $this->assertTrue($expectedResult === CRM_Utils_Type
::validate($inputData, $inputType, FALSE));
24 public function validateDataProvider() {
27 ['145E+3', 'Int', NULL],
28 ['10', 'Integer', 10],
30 ['-10', 'Integer', -10],
31 ['-10foo', 'Int', NULL],
33 ['145.0E+3', 'Positive', NULL],
34 ['10', 'Positive', 10],
35 [-10, 'Positive', NULL],
36 ['-10', 'Positive', NULL],
37 ['-10foo', 'Positive', NULL],
38 ['civicrm_column_name', 'MysqlColumnNameOrAlias', 'civicrm_column_name'],
39 ['table.civicrm_column_name', 'MysqlColumnNameOrAlias', 'table.civicrm_column_name'],
40 ['table.civicrm_column_name.toomanydots', 'MysqlColumnNameOrAlias', NULL],
41 ['Home-street_address', 'MysqlColumnNameOrAlias', 'Home-street_address'],
42 ['`Home-street_address`', 'MysqlColumnNameOrAlias', '`Home-street_address`'],
43 ['`Home-street_address', 'MysqlColumnNameOrAlias', NULL],
44 ['table.`Home-street_address`', 'MysqlColumnNameOrAlias', 'table.`Home-street_address`'],
45 ['`table-alias`.`Home-street_address`', 'MysqlColumnNameOrAlias', '`table-alias`.`Home-street_address`'],
46 ['`table-alias`.column', 'MysqlColumnNameOrAlias', '`table-alias`.column'],
47 // Spaces also permitted, only when enclosed in backticks.
48 ['`column alias`', 'MysqlColumnNameOrAlias', '`column alias`'],
49 ['`table alias`.column', 'MysqlColumnNameOrAlias', '`table alias`.column'],
50 ['`table alias`.`column alias`', 'MysqlColumnNameOrAlias', '`table alias`.`column alias`'],
51 ['table alias.column alias', 'MysqlColumnNameOrAlias', NULL],
52 ['table alias.column_alias', 'MysqlColumnNameOrAlias', NULL],
53 ['table_alias.column alias', 'MysqlColumnNameOrAlias', NULL],
54 // Functions are not permitted.
55 ['column_name, sleep(5)', 'MysqlColumnNameOrAlias', NULL],
56 // Length checking permits only 64 chars.
57 [str_repeat('a', 64), 'MysqlColumnNameOrAlias', str_repeat('a', 64)],
58 [str_repeat('a', 65), 'MysqlColumnNameOrAlias', NULL],
59 [str_repeat('a', 64) . '.' . str_repeat('a', 64), 'MysqlColumnNameOrAlias', str_repeat('a', 64) . '.' . str_repeat('a', 64)],
60 ['`' . str_repeat('a', 64) . '`.`' . str_repeat('b', 64) . '`', 'MysqlColumnNameOrAlias', '`' . str_repeat('a', 64) . '`.`' . str_repeat('b', 64) . '`'],
61 [str_repeat('a', 64) . '.' . str_repeat('a', 65), 'MysqlColumnNameOrAlias', NULL],
62 [str_repeat('a', 65) . '.' . str_repeat('a', 64), 'MysqlColumnNameOrAlias', NULL],
63 // ORDER BY can be ASC or DESC, case not significant.
64 ['asc', 'MysqlOrderByDirection', 'asc'],
65 ['DESC', 'MysqlOrderByDirection', 'desc'],
66 ['DESCc', 'MysqlOrderByDirection', NULL],
67 ['table.civicrm_column_name desc', 'MysqlOrderBy', 'table.civicrm_column_name desc'],
68 ['field(civicrm_column_name,4,5,6)', 'MysqlOrderBy', 'field(civicrm_column_name,4,5,6)'],
69 ['field(table.civicrm_column_name,4,5,6)', 'MysqlOrderBy', 'field(table.civicrm_column_name,4,5,6)'],
70 ['table.civicrm_column_name desc,other_column, another_column desc', 'MysqlOrderBy', 'table.civicrm_column_name desc,other_column, another_column desc'],
71 ['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'],
72 // Lab issue dev/core#93 allow for 3 column orderby
73 ['contact_id.gender_id.label', 'MysqlOrderBy', 'contact_id.gender_id.label'],
74 ['a string', 'String', 'a string'],
75 ['{"contact":{"contact_id":205}}', 'Json', '{"contact":{"contact_id":205}}'],
76 ['{"contact":{"contact_id":!n†rude®}}', 'Json', NULL],
81 * @dataProvider escapeDataProvider
84 * @param $expectedResult
86 public function testEscape($inputData, $inputType, $expectedResult) {
87 $this->assertTrue($expectedResult === CRM_Utils_Type
::escape($inputData, $inputType, FALSE));
93 public function escapeDataProvider() {
96 ['145E+3', 'Int', NULL],
97 ['10', 'Integer', 10],
99 [[], 'Integer', NULL],
100 ['-10foo', 'Int', NULL],
101 [10, 'Positive', 10],
102 ['145.0E+3', 'Positive', NULL],
103 ['10', 'Positive', 10],
104 [-10, 'Positive', NULL],
105 ['-10', 'Positive', NULL],
106 ['-10foo', 'Positive', NULL],
107 [['10', 20], 'Country', ['10', 20]],
108 [['10', '-10foo'], 'Country', NULL],
109 ['', 'Timestamp', ''],
110 ['', 'ContactReference', ''],
111 ['3', 'ContactReference', 3],
112 ['-3', 'ContactReference', NULL],
113 // Escape function is meant for sql, not xss
114 ['<p onclick="alert(\'xss\');">Hello</p>', 'Memo', '<p onclick=\\"alert(\\\'xss\\\');\\">Hello</p>'],
115 ['civicrm_column_name', 'MysqlColumnNameOrAlias', '`civicrm_column_name`'],
116 ['table.civicrm_column_name', 'MysqlColumnNameOrAlias', '`table`.`civicrm_column_name`'],
117 ['table.civicrm_column_name.toomanydots', 'MysqlColumnNameOrAlias', NULL],
118 ['Home-street_address', 'MysqlColumnNameOrAlias', '`Home-street_address`'],
119 ['`Home-street_address`', 'MysqlColumnNameOrAlias', '`Home-street_address`'],
120 ['`Home-street_address', 'MysqlColumnNameOrAlias', NULL],
121 ['column_name, sleep(5)', 'MysqlColumnNameOrAlias', NULL],
122 ['asc', 'MysqlOrderByDirection', 'asc'],
123 ['DESC', 'MysqlOrderByDirection', 'desc'],
124 ['DESCc', 'MysqlOrderByDirection', NULL],
125 ['table.civicrm_column_name desc', 'MysqlOrderBy', '`table`.`civicrm_column_name` desc'],
126 ['field(contribution_status_id,4,5,6) asc', 'MysqlOrderBy', 'field(`contribution_status_id`,4,5,6) asc'],
127 ['field(contribution_status_id,4,5,6) asc, contact_id asc', 'MysqlOrderBy', 'field(`contribution_status_id`,4,5,6) asc, `contact_id` asc'],
128 ['table.civicrm_column_name desc,other_column,another_column desc', 'MysqlOrderBy', '`table`.`civicrm_column_name` desc, `other_column`, `another_column` desc'],
129 ['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`'],
130 [TRUE, 'Boolean', TRUE],
131 [FALSE, 'Boolean', FALSE],
132 ['TRUE', 'Boolean', 'TRUE'],
133 ['false', 'Boolean', 'false'],
134 ['banana', 'Boolean', NULL],