From 9384c60a3a8e5f3f72af23b427067da9cf95d5e7 Mon Sep 17 00:00:00 2001 From: Monish Deb Date: Mon, 25 Nov 2019 14:31:23 +0530 Subject: [PATCH] core#1420 Quicksearch with phone filter doesn't work with non-numeric character --- api/v3/Contact.php | 4 ++ tests/phpunit/api/v3/ContactTest.php | 83 +++++++++++++++++++++++++++- 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/api/v3/Contact.php b/api/v3/Contact.php index cc98dcaf98..ef3cd25f6c 100644 --- a/api/v3/Contact.php +++ b/api/v3/Contact.php @@ -758,6 +758,10 @@ function civicrm_api3_contact_getquick($params) { if ($field_name == 'contact_id') { $field_name = 'id'; } + // core#1420 : trim non-numeric character from phone search string + elseif ($field_name == 'phone_numeric') { + $name = preg_replace('/[^\d]/', '', $name); + } if (isset($table_names[$field_name])) { $table_name = $table_names[$field_name]; } diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index b807fbc6df..19261b15ff 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -3237,6 +3237,65 @@ class api_v3_ContactTest extends CiviUnitTestCase { $this->assertEquals('E Bobby, Bobby', $result['values'][0]['sort_name']); } + /** + * Test that getquick returns contacts with different cases of phone substring. + */ + public function testGetQuickPhone() { + $this->getQuickSearchSampleData(); + $criterias = [ + [ + 'criteria' => [ + 'name' => '87-6', + 'field_name' => 'phone_numeric', + ], + 'count' => 2, + 'sort_names' => [ + 'I Bobby, Bobby', + 'J Bobby, Bobby', + ], + ], + [ + 'criteria' => [ + 'name' => '876-1', + 'field_name' => 'phone_numeric', + ], + 'count' => 1, + 'sort_names' => [ + 'I Bobby, Bobby', + ], + ], + [ + 'criteria' => [ + 'name' => '87623', + 'field_name' => 'phone_numeric', + ], + 'count' => 1, + 'sort_names' => [ + 'J Bobby, Bobby', + ], + ], + [ + 'criteria' => [ + 'name' => '8a7abc6', + 'field_name' => 'phone_numeric', + ], + 'count' => 2, + 'sort_names' => [ + 'I Bobby, Bobby', + 'J Bobby, Bobby', + ], + ], + ]; + + foreach ($criterias as $criteria) { + $result = $this->callAPISuccess('contact', 'getquick', $criteria['criteria']); + $this->assertEquals($result['count'], $criteria['count']); + foreach ($criteria['sort_names'] as $key => $sortName) { + $this->assertEquals($sortName, $result['values'][$key]['sort_name']); + } + } + } + /** * Test that getquick returns contacts with an exact first name match first. * @@ -3387,8 +3446,28 @@ class api_v3_ContactTest extends CiviUnitTestCase { ['first_name' => 'Bobby', 'last_name' => 'F Bobby', 'external_identifier' => 'klm'], ['first_name' => 'Bobby', 'last_name' => 'G Bobby', 'external_identifier' => 'nop'], ['first_name' => 'Bobby', 'last_name' => 'H Bobby', 'external_identifier' => 'qrs', 'email' => 'bob@h.com'], - ['first_name' => 'Bobby', 'last_name' => 'I Bobby'], - ['first_name' => 'Bobby', 'last_name' => 'J Bobby'], + [ + 'first_name' => 'Bobby', + 'last_name' => 'I Bobby', + 'api.phone.create' => [ + 'phone' => '876-123', + 'phone_ext' => '444', + "phone_type_id" => "Phone", + 'location_type_id' => 1, + 'is_primary' => 1, + ], + ], + [ + 'first_name' => 'Bobby', + 'last_name' => 'J Bobby', + 'api.phone.create' => [ + 'phone' => '87-6-234', + 'phone_ext' => '134', + "phone_type_id" => "Phone", + 'location_type_id' => 1, + 'is_primary' => 1, + ], + ], ['first_name' => 'Bob', 'last_name' => 'K Bobby', 'external_identifier' => 'bcdef'], ['first_name' => 'Bob', 'last_name' => 'Aadvark'], ]; -- 2.25.1