From d16e7909dd2fbbc2272076ce95f332e753e36747 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 19 May 2022 12:19:25 +1200 Subject: [PATCH] [Import] Fix & test check on contact subtype change --- CRM/Contact/Import/Parser/Contact.php | 39 +++++-------------- .../CRM/Contact/Import/Parser/ContactTest.php | 21 ++++++++++ 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index cae7b3a311..c8a42688cd 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -321,6 +321,16 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { throw new CRM_Core_Exception('Mismatched or Invalid Contact Subtype.', CRM_Import_Parser::NO_MATCH); } $params['id'] = $formatted['id'] = $this->lookupContactID($params, ($this->isSkipDuplicates() || $this->isIgnoreDuplicates())); + if ($params['id'] && $params['contact_sub_type']) { + $contactSubType = Contact::get(FALSE) + ->addWhere('id', '=', $params['id']) + ->addSelect('contact_sub_type') + ->execute() + ->first()['contact_sub_type']; + if (!empty($contactSubType) && $contactSubType[0] !== $params['contact_sub_type'] && !CRM_Contact_BAO_ContactType::isAllowEdit($params['id'], $contactSubType[0])) { + throw new CRM_Core_Exception('Mismatched contact SubTypes :', CRM_Import_Parser::NO_MATCH); + } + } } catch (CRM_Core_Exception $e) { $statuses = [CRM_Import_Parser::DUPLICATE => 'DUPLICATE', CRM_Import_Parser::ERROR => 'ERROR', CRM_Import_Parser::NO_MATCH => 'invalid_no_match']; @@ -342,36 +352,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { // could be removed in favour of a simple check for whether the contact_type & id match $matchedIDs = $this->getIdsOfMatchingContacts($formatted); if (!empty($matchedIDs)) { - if (count($matchedIDs) >= 1) { - $updateflag = TRUE; - foreach ($matchedIDs as $contactId) { - if ($params['id'] == $contactId) { - //validation of subtype for update mode - //CRM-5125 - $contactSubType = NULL; - if (!empty($params['contact_sub_type'])) { - $contactSubType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_sub_type'); - } - - if (!empty($contactSubType) && (!CRM_Contact_BAO_ContactType::isAllowEdit($params['id'], $contactSubType) && $contactSubType != CRM_Utils_Array::value('contact_sub_type', $formatted))) { - $message = "Mismatched contact SubTypes :"; - array_unshift($values, $message); - $updateflag = FALSE; - $this->_retCode = CRM_Import_Parser::NO_MATCH; - } - else { - $updateflag = FALSE; - $this->_retCode = CRM_Import_Parser::VALID; - } - } - } - if ($updateflag) { - $message = "Mismatched contact IDs OR Mismatched contact Types :"; - array_unshift($values, $message); - $this->_retCode = CRM_Import_Parser::NO_MATCH; - } - } } else { if (!empty($params['id'])) { diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php index b4d263bb6e..4c73c7e2ea 100644 --- a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -270,6 +270,27 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { $this->assertEquals(['Staff'], $contact['contact_sub_type']); } + /** + * Test updating an existing contact with external_identifier match but subtype mismatch. + * + * The subtype is not updated, as there is conflicting contact data. + * + * @throws \Exception + */ + public function testImportParserUpdateWithExternalIdentifierSubtypeChangeFail(): void { + $contactID = $this->individualCreate(['external_identifier' => 'billy', 'first_name' => 'William', 'contact_sub_type' => 'Parent']); + $this->addChild($contactID); + + $this->runImport([ + 'external_identifier' => 'billy', + 'nick_name' => 'Old Bill', + 'contact_sub_type' => 'Staff', + ], CRM_Import_Parser::DUPLICATE_UPDATE, NULL); + $contact = $this->callAPISuccessGetSingle('Contact', ['id' => $contactID]); + $this->assertEquals('', $contact['nick_name']); + $this->assertEquals(['Parent'], $contact['contact_sub_type']); + } + /** * Test updating an existing contact with external_identifier match but subtype mismatch. * -- 2.25.1