Skip to content
Snippets Groups Projects
Unverified Commit 2bb4289c authored by colemanw's avatar colemanw Committed by GitHub
Browse files

Merge pull request #23508 from eileenmcnaughton/import_subkid

[Import] Fix & test check on contact subtype change
parents 69d6d956 d16e7909
Branches
Tags
No related merge requests found
......@@ -444,6 +444,16 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser {
try {
$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'];
......@@ -466,36 +476,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;
}
}
}
}
......
......@@ -273,6 +273,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.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment