From c3cea83f4e1f624d698d70a6ee943a655ee3be5f Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 21 Aug 2020 14:37:10 +1200 Subject: [PATCH] Fix regression whereby deleted contacts are in quicksearch results --- api/v3/Contact.php | 20 ++++++++++---------- tests/phpunit/api/v3/ContactTest.php | 24 +++++++++++++++++++++++- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/api/v3/Contact.php b/api/v3/Contact.php index d30d0d0b09..8217b646d1 100644 --- a/api/v3/Contact.php +++ b/api/v3/Contact.php @@ -818,7 +818,7 @@ function civicrm_api3_contact_getquick($params) { } $select = $actualSelectElements = ['sort_name']; - $where = ''; + foreach ($list as $value) { $suffix = substr($value, 0, 2) . substr($value, -1); switch ($value) { @@ -875,14 +875,14 @@ function civicrm_api3_contact_getquick($params) { // add acl clause here list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause('cc'); - + $whereClauses = ['cc.is_deleted = 0']; if ($aclWhere) { - $where .= " AND $aclWhere "; + $whereClauses[] = $aclWhere; } $isPrependWildcard = \Civi::settings()->get('includeWildCardInName'); if (!empty($params['org'])) { - $where .= " AND contact_type = \"Organization\""; + $whereClauses[] = 'contact_type = "Organization"'; // CRM-7157, hack: get current employer details when // employee_id is present. @@ -914,21 +914,21 @@ function civicrm_api3_contact_getquick($params) { if (!empty($params['contact_sub_type'])) { $contactSubType = CRM_Utils_Type::escape($params['contact_sub_type'], 'String'); - $where .= " AND cc.contact_sub_type = '{$contactSubType}'"; + $whereClauses[] = "cc.contact_sub_type = '{$contactSubType}'"; } if (!empty($params['contact_type'])) { $contactType = CRM_Utils_Type::escape($params['contact_type'], 'String'); - $where .= " AND cc.contact_type LIKE '{$contactType}'"; + $whereClauses[] = "cc.contact_type LIKE '{$contactType}'"; } // Set default for current_employer or return contact with particular id if (!empty($params['id'])) { - $where .= " AND cc.id = " . (int) $params['id']; + $whereClauses[] = 'cc.id = ' . (int) $params['id']; } if (!empty($params['cid'])) { - $where .= " AND cc.id <> " . (int) $params['cid']; + $whereClauses[] = 'cc.id <> ' . (int) $params['cid']; } // Contact's based of relationhip type @@ -949,10 +949,10 @@ function civicrm_api3_contact_getquick($params) { if ($config->includeNickNameInName) { $includeNickName = " OR nick_name LIKE '$strSearch'"; } - + $where = ' AND ' . implode(' AND ', $whereClauses); if (isset($customOptionsWhere)) { $customOptionsWhere = $customOptionsWhere ?: [0]; - $whereClause = " WHERE (" . implode(' OR ', $customOptionsWhere) . ") $where"; + $whereClause = ' WHERE (' . implode(' OR ', $customOptionsWhere) . ") $where"; } elseif (!empty($params['field_name']) && !empty($params['table_name']) && $params['field_name'] != 'sort_name') { $whereClause = " WHERE ( $table_name.$field_name LIKE '$strSearch') {$where}"; diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index 9dc8203194..62c1ff50e5 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -3306,7 +3306,7 @@ class api_v3_ContactTest extends CiviUnitTestCase { * Test that getquick returns contacts with an exact first name match first. */ public function testGetQuickID() { - $max = CRM_Core_DAO::singleValueQuery("SELECT max(id) FROM civicrm_contact"); + $max = CRM_Core_DAO::singleValueQuery('SELECT max(id) FROM civicrm_contact'); $this->getQuickSearchSampleData(); $result = $this->callAPISuccess('contact', 'getquick', [ 'name' => $max + 2, @@ -3481,6 +3481,28 @@ class api_v3_ContactTest extends CiviUnitTestCase { $this->assertEquals('Bob, Bob :: bob@bob.com', $result['values'][0]['data']); } + /** + * Test deleted contacts are excluded from getquick results. + * + * @throws \CiviCRM_API3_Exception + */ + public function testGetQuickNotDeleted() { + $this->getQuickSearchSampleData(); + $result = $this->callAPISuccess('contact', 'getquick', [ + 'name' => 'abc', + 'field_name' => 'external_identifier', + 'table_name' => 'cc', + ])['values']; + $this->assertCount(1, $result); + $this->callAPISuccess('Contact', 'delete', ['id' => $result['0']['id']]); + $result = $this->callAPISuccess('contact', 'getquick', [ + 'name' => 'abc', + 'field_name' => 'external_identifier', + 'table_name' => 'cc', + ])['values']; + $this->assertCount(0, $result); + } + /** * Test that getquick returns contacts by city. */ -- 2.25.1