From f19a5565fb917b4490d910ee96e5088fe1e83e5f Mon Sep 17 00:00:00 2001 From: Mattias Michaux Date: Thu, 12 May 2016 10:50:08 +0200 Subject: [PATCH] Fixed tests, checks and naming. --- CRM/Utils/Rule.php | 2 +- CRM/Utils/Type.php | 6 ++--- tests/phpunit/CRM/Utils/TypeTest.php | 39 ++++++++++++++-------------- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/CRM/Utils/Rule.php b/CRM/Utils/Rule.php index 3e6dbd0c4e..b6f4f7e6dc 100644 --- a/CRM/Utils/Rule.php +++ b/CRM/Utils/Rule.php @@ -103,7 +103,7 @@ class CRM_Utils_Rule { // Ensure the string contains only valid characters: // For column names: alphanumeric and underscores // For aliases: backticks, alphanumeric hyphens and underscores. - if (!preg_match('^((`[\w-]{1,64}`|\w{1,64})\.)?(`[\w-]{1,64}`|\w{1,64})$/i', $str)) { + if (!preg_match('/^((`[\w-]{1,64}`|\w{1,64})\.)?(`[\w-]{1,64}`|\w{1,64})$/i', $str)) { return FALSE; } diff --git a/CRM/Utils/Type.php b/CRM/Utils/Type.php index 43fa00a5b2..f49f212701 100644 --- a/CRM/Utils/Type.php +++ b/CRM/Utils/Type.php @@ -268,7 +268,7 @@ class CRM_Utils_Type { } break; - case 'mysqlColumnNameOrAlias': + case 'MysqlColumnNameOrAlias': if (CRM_Utils_Rule::mysqlColumnNameOrAlias($data)) { $data = str_replace('`', '', $data); $parts = explode('.', $data); @@ -397,7 +397,7 @@ class CRM_Utils_Type { } break; - case 'mysqlColumnNameOrAlias': + case 'MysqlColumnNameOrAlias': if (CRM_Utils_Rule::mysqlColumnNameOrAlias($data)) { return $data; } @@ -436,7 +436,7 @@ class CRM_Utils_Type { $matches = str_replace('`', '', $matches); // Table name. - if (isset($matches[1])) { + if (isset($matches[1]) && $matches[1]) { $output .= '`' . $matches[1] . '`.'; } diff --git a/tests/phpunit/CRM/Utils/TypeTest.php b/tests/phpunit/CRM/Utils/TypeTest.php index 781b2d16ab..0bd51b3ff6 100644 --- a/tests/phpunit/CRM/Utils/TypeTest.php +++ b/tests/phpunit/CRM/Utils/TypeTest.php @@ -37,20 +37,20 @@ class CRM_Utils_TypeTest extends CiviUnitTestCase { array(-10, 'Positive', NULL), array('-10', 'Positive', NULL), array('-10foo', 'Positive', NULL), - array('civicrm_column_name', 'mysqlColumnNameOrAlias', 'civicrm_column_name'), - array('table.civicrm_column_name', 'mysqlColumnNameOrAlias', 'table.civicrm_column_name'), - array('table.civicrm_column_name.toomanydots', 'mysqlColumnNameOrAlias', NULL), - array('Home-street_address', 'mysqlColumnNameOrAlias', NULL), - array('`Home-street_address`', 'mysqlColumnNameOrAlias', '`Home-street_address`'), - array('table.`Home-street_address`', 'mysqlColumnNameOrAlias', 'table.`Home-street_address`'), - array('`table-alias`.`Home-street_address`', 'mysqlColumnNameOrAlias', '`table-alias`.`Home-street_address`'), - array('`table-alias`.column', 'mysqlColumnNameOrAlias', '`table-alias`.column'), - array('column_name, sleep(5)', 'mysqlColumnNameOrAlias', NULL), - array(str_repeat('a', 64), 'mysqlColumnNameOrAlias', str_repeat('a', 64)), - array(str_repeat('a', 65), 'mysqlColumnNameOrAlias', NULL), - array(str_repeat('a', 64) . '.' . str_repeat('a', 64), 'mysqlColumnNameOrAlias', str_repeat('a', 64) . '.' . str_repeat('a', 64)), - array(str_repeat('a', 64) . '.' . str_repeat('a', 65), 'mysqlColumnNameOrAlias', NULL), - array(str_repeat('a', 65) . '.' . str_repeat('a', 64), 'mysqlColumnNameOrAlias', NULL), + array('civicrm_column_name', 'MysqlColumnNameOrAlias', 'civicrm_column_name'), + array('table.civicrm_column_name', 'MysqlColumnNameOrAlias', 'table.civicrm_column_name'), + array('table.civicrm_column_name.toomanydots', 'MysqlColumnNameOrAlias', NULL), + array('Home-street_address', 'MysqlColumnNameOrAlias', NULL), + array('`Home-street_address`', 'MysqlColumnNameOrAlias', '`Home-street_address`'), + array('table.`Home-street_address`', 'MysqlColumnNameOrAlias', 'table.`Home-street_address`'), + array('`table-alias`.`Home-street_address`', 'MysqlColumnNameOrAlias', '`table-alias`.`Home-street_address`'), + array('`table-alias`.column', 'MysqlColumnNameOrAlias', '`table-alias`.column'), + array('column_name, sleep(5)', 'MysqlColumnNameOrAlias', NULL), + array(str_repeat('a', 64), 'MysqlColumnNameOrAlias', str_repeat('a', 64)), + array(str_repeat('a', 65), 'MysqlColumnNameOrAlias', NULL), + array(str_repeat('a', 64) . '.' . str_repeat('a', 64), 'MysqlColumnNameOrAlias', str_repeat('a', 64) . '.' . str_repeat('a', 64)), + array(str_repeat('a', 64) . '.' . str_repeat('a', 65), 'MysqlColumnNameOrAlias', NULL), + array(str_repeat('a', 65) . '.' . str_repeat('a', 64), 'MysqlColumnNameOrAlias', NULL), array('asc', 'MysqlOrderByDirection', 'asc'), array('DESC', 'MysqlOrderByDirection', 'desc'), array('DESCc', 'MysqlOrderByDirection', NULL), @@ -95,11 +95,12 @@ class CRM_Utils_TypeTest extends CiviUnitTestCase { array('-3', 'ContactReference', NULL), // Escape function is meant for sql, not xss array('

Hello

', 'Memo', '

Hello

'), - array('civicrm_column_name', 'MysqlColumnName', '`civicrm_column_name`'), - array('table.civicrm_column_name', 'MysqlColumnName', '`table`.`civicrm_column_name`'), - array('table.civicrm_column_name.toomanydots', 'MysqlColumnName', NULL), - array('Home-street_address', 'MysqlColumnName', '`Home-street_address`'), - array('column_name, sleep(5)', 'MysqlColumnName', NULL), + array('civicrm_column_name', 'MysqlColumnNameOrAlias', '`civicrm_column_name`'), + array('table.civicrm_column_name', 'MysqlColumnNameOrAlias', '`table`.`civicrm_column_name`'), + array('table.civicrm_column_name.toomanydots', 'MysqlColumnNameOrAlias', NULL), + array('Home-street_address', 'MysqlColumnNameOrAlias', NULL), + array('`Home-street_address`', 'MysqlColumnNameOrAlias', '`Home-street_address`'), + array('column_name, sleep(5)', 'MysqlColumnNameOrAlias', NULL), array('asc', 'MysqlOrderByDirection', 'asc'), array('DESC', 'MysqlOrderByDirection', 'desc'), array('DESCc', 'MysqlOrderByDirection', NULL), -- 2.25.1