Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove instances of LOWER casing searches from sort_name, display_name #12987

Merged
merged 1 commit into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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