Skip to content

Commit

Permalink
Remove instances of LOWERE casing searches from sort_name, display_name.
Browse files Browse the repository at this point in the history
Per civicrm#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
  • Loading branch information
eileenmcnaughton committed Oct 23, 2018
1 parent efd977b commit 9436d5d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
13 changes: 4 additions & 9 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -3402,8 +3402,6 @@ public function sortName(&$values) {
//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("'", '"');
Expand All @@ -3416,22 +3414,19 @@ public function sortName(&$values) {
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) {
Expand Down
18 changes: 17 additions & 1 deletion tests/phpunit/api/v3/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,22 @@ public function testCreateDisplayNameIndividual() {
$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.
*
Expand Down Expand Up @@ -3183,7 +3199,7 @@ public function testReturnCityProfile() {
* 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));
Expand Down

0 comments on commit 9436d5d

Please sign in to comment.