From a8ea39225c9e00b857fff2d4239b21fdb64083cb Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 11 Dec 2020 14:58:41 +1300 Subject: [PATCH] [REF] extract getIdsOfMatchingContact It seems this really needs sorting to be shared between the parser classes to fix up the error handling --- CRM/Contact/Import/Parser/Contact.php | 22 +++++-------------- CRM/Import/Parser.php | 22 +++++++++++++++++++ .../CRM/Contact/Import/Parser/ContactTest.php | 8 ++++--- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 0f12925e1d..300ac47b16 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -542,16 +542,8 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser { $createNewContact = FALSE; // @todo - it feels like all the rows from here to the end of the IF // could be removed in favour of a simple check for whether the contact_type & id match - // the call to the deprecated function seems to add no value other that to do an additional - // check for the contact_id & type. - $error = _civicrm_api3_deprecated_duplicate_formatted_contact($formatted); - if (CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) { - if (is_array($error['error_message']['params'][0])) { - $matchedIDs = $error['error_message']['params'][0]; - } - else { - $matchedIDs = explode(',', $error['error_message']['params'][0]); - } + $matchedIDs = $this->getIdsOfMatchingContacts($formatted); + if (!empty($matchedIDs)) { if (count($matchedIDs) >= 1) { $updateflag = TRUE; foreach ($matchedIDs as $contactId) { @@ -642,10 +634,6 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser { if (isset($newContact) && is_a($newContact, 'CRM_Contact_BAO_Contact')) { $relationship = TRUE; } - elseif (is_a($error, 'CRM_Core_Error')) { - $newContact = $error; - $relationship = TRUE; - } } //fixed CRM-4148 @@ -925,7 +913,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser { 'contact' => $primaryContactId, ]; - list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::legacyCreateMultiple($relationParams, $relationIds); + [$valid, $invalid, $duplicate, $saved, $relationshipIds] = CRM_Contact_BAO_Relationship::legacyCreateMultiple($relationParams, $relationIds); if ($valid || $duplicate) { $relationIds['contactTarget'] = $relContactId; @@ -1195,7 +1183,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser { } } if (!empty($relation)) { - list($id, $first, $second) = CRM_Utils_System::explode('_', $relation, 3); + [$id, $first, $second] = CRM_Utils_System::explode('_', $relation, 3); $direction = "contact_sub_type_$second"; $relationshipType = new CRM_Contact_BAO_RelationshipType(); $relationshipType->id = $id; @@ -1609,7 +1597,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser { $formatted['updateBlankLocInfo'] = FALSE; } - list($data, $contactDetails) = CRM_Contact_BAO_Contact::formatProfileContactParams($formatted, $contactFields, $contactId, NULL, $formatted['contact_type']); + [$data, $contactDetails] = CRM_Contact_BAO_Contact::formatProfileContactParams($formatted, $contactFields, $contactId, NULL, $formatted['contact_type']); // manage is_opt_out if (array_key_exists('is_opt_out', $contactFields) && array_key_exists('is_opt_out', $formatted)) { diff --git a/CRM/Import/Parser.php b/CRM/Import/Parser.php index 957cb9b486..3aa731f55f 100644 --- a/CRM/Import/Parser.php +++ b/CRM/Import/Parser.php @@ -604,4 +604,26 @@ abstract class CRM_Import_Parser { return $values; } + /** + * Get the ids of any contacts that match according to the rule. + * + * @param array $formatted + * + * @return array + */ + protected function getIdsOfMatchingContacts(array $formatted):array { + // the call to the deprecated function seems to add no value other that to do an additional + // check for the contact_id & type. + $error = _civicrm_api3_deprecated_duplicate_formatted_contact($formatted); + if (!CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) { + return []; + } + if (is_array($error['error_message']['params'][0])) { + return $error['error_message']['params'][0]; + } + else { + return explode(',', $error['error_message']['params'][0]); + } + } + } diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php index 1a154a2671..b445733873 100644 --- a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -97,16 +97,18 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { } /** - * Test that import parser will not fail when same external_identifier found of deleted contact. + * Test that import parser will not fail when same external_identifier found + * of deleted contact. * * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception */ - public function testImportParserWtihDeletedContactExternalIdentifier() { + public function testImportParserWithDeletedContactExternalIdentifier(): void { $contactId = $this->individualCreate([ 'external_identifier' => 'ext-1', ]); $this->callAPISuccess('Contact', 'delete', ['id' => $contactId]); - list($originalValues, $result) = $this->setUpBaseContact([ + [$originalValues, $result] = $this->setUpBaseContact([ 'external_identifier' => 'ext-1', ]); $originalValues['nick_name'] = 'Old Bill'; -- 2.25.1