From 9436d5d588236e6c7785d217a24200d26e97df4b Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 23 Oct 2018 18:54:45 +1300 Subject: [PATCH] Remove instances of LOWERE casing searches from sort_name, display_name. Per https://github.com/civicrm/civicrm-core/pull/12494 mysql handles lcase. Adding LOWER to mysql queries makes them slower. lowercasing php strings breaks under some character sets with some server configs. Less is more --- CRM/Contact/BAO/Query.php | 13 ++++--------- tests/phpunit/api/v3/ContactTest.php | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 027d8165df..6e321282e3 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -3402,8 +3402,6 @@ WHERE $smartGroupClause //By default, $sub elements should be joined together with OR statements (don't change this variable). $subGlue = ' OR '; - $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; - $firstChar = substr($value, 0, 1); $lastChar = substr($value, -1, 1); $quotes = array("'", '"'); @@ -3416,22 +3414,19 @@ WHERE $smartGroupClause elseif ($op == 'LIKE' && strpos($value, ',') === FALSE) { $value = str_replace(' ', '%', $value); } - $value = $strtolower(CRM_Core_DAO::escapeString(trim($value))); + $value = CRM_Core_DAO::escapeString(trim($value)); if (strlen($value)) { $fieldsub = array(); $value = "'" . self::getWildCardedValue($wildcard, $op, $value) . "'"; if ($fieldName == 'sort_name') { - // LOWER roughly translates to 'hurt my database without deriving any benefit' See CRM-19811. - $wc = self::caseImportant($op) ? "LOWER(contact_a.sort_name)" : "contact_a.sort_name"; + $wc = "contact_a.sort_name"; } else { - // LOWER roughly translates to 'hurt my database without deriving any benefit' See CRM-19811. - $wc = self::caseImportant($op) ? "LOWER(contact_a.display_name)" : "contact_a.display_name"; + $wc = "contact_a.display_name"; } $fieldsub[] = " ( $wc $op $value )"; if ($config->includeNickNameInName) { - // LOWER roughly translates to 'hurt my database without deriving any benefit' See CRM-19811. - $wc = self::caseImportant($op) ? "LOWER(contact_a.nick_name)" : "contact_a.nick_name"; + $wc = "contact_a.nick_name"; $fieldsub[] = " ( $wc $op $value )"; } if ($config->includeEmailInName) { diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index 3e2e9b0c26..ebef36284e 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -396,6 +396,22 @@ class api_v3_ContactTest extends CiviUnitTestCase { $this->getAndCheck($params, $contact['id'], 'contact'); } + /** + * Test that name searches are case insensitive. + */ + public function testGetNameVariantsCaseInsensitive() { + $this->callAPISuccess('contact', 'create', [ + 'display_name' => 'Abc1', + 'contact_type' => 'Individual', + ]); + $this->callAPISuccessGetSingle('Contact', ['display_name' => 'aBc1']); + $this->callAPISuccessGetSingle('Contact', ['sort_name' => 'aBc1']); + Civi::settings()->set('includeNickNameInName', TRUE); + $this->callAPISuccessGetSingle('Contact', ['display_name' => 'aBc1']); + $this->callAPISuccessGetSingle('Contact', ['sort_name' => 'aBc1']); + Civi::settings()->set('includeNickNameInName', FALSE); + } + /** * Test old keys still work. * @@ -3183,7 +3199,7 @@ class api_v3_ContactTest extends CiviUnitTestCase { * CRM-15443 - ensure getlist api does not return deleted contacts. */ public function testGetlistExcludeConditions() { - $name = md5(time()); + $name = 'Scarabée'; $contact = $this->individualCreate(array('last_name' => $name)); $this->individualCreate(array('last_name' => $name, 'is_deceased' => 1)); $this->individualCreate(array('last_name' => $name, 'is_deleted' => 1)); -- 2.25.1