Skip to content

Commit

Permalink
Merge pull request #28557 from eileenmcnaughton/meta_more
Browse files Browse the repository at this point in the history
dev/core#4231 Fix failure to import non-default state
  • Loading branch information
colemanw authored Jun 20, 2024
2 parents ffc13f9 + ca69e98 commit d115a91
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
25 changes: 24 additions & 1 deletion CRM/Import/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1580,7 +1580,30 @@ protected function getTransformedFieldValue(string $fieldName, $importedValue) {
}

$comparisonValue = $this->getComparisonValue($importedValue);
return $options[$comparisonValue] ?? 'invalid_import_value';
$resolvedValue = $options[$comparisonValue] ?? 'invalid_import_value';
if (in_array($fieldName, ['state_province_id', 'county_id'], TRUE) && $resolvedValue === 'invalid_import_value') {
if ($fieldName === 'state_province_id') {
$stateID = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_state_province WHERE name = %1', [1 => [$comparisonValue, 'String']]);
if (!$stateID) {
$stateID = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_state_province WHERE abbreviation = %1', [1 => [$comparisonValue, 'String']]);
}
if ($stateID) {
$this->importableFieldsMetadata['state_province_id']['options'][$comparisonValue] = $stateID;
return $stateID;
}
}
if ($fieldName === 'county_id') {
$countyID = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_county WHERE name = %1', [1 => [$comparisonValue, 'String']]);
if (!$countyID) {
$countyID = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_county WHERE abbreviation = %1', [1 => [$comparisonValue, 'String']]);
}
if ($countyID) {
$this->importableFieldsMetadata['county_id']['options'][$comparisonValue] = $countyID;
return $countyID;
}
}
}
return $resolvedValue;
}
// @todo - make this generic - for fields where getOptions doesn't fetch
// getOptions does not retrieve these fields with high potential results
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
First Name,Last Name,State,Country
Bob,Smith,Alberta,Canada
22 changes: 22 additions & 0 deletions tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,28 @@ public function testImportMasterAddress(): void {
$this->assertEquals('out yonder', $newAddress['street_address']);
}

/**
* @throws \CRM_Core_Exception
*/
public function testImportNonDefaultCountryState(): void {
\Civi::settings()->set('defaultContactCountry', 1228);
$csv = 'individual_country_state.csv';
$mapper = [
['first_name'],
['last_name'],
['state_province', 'Primary'],
['country', 'Primary'],
];
$this->validateCSV($csv, $mapper);
$this->importCSV($csv, $mapper);
$address = Address::get(FALSE)
->addWhere('country_id.name', '=', 'Canada')
->addWhere('state_province_id.name', '=', 'Alberta')
->addSelect('contact_id.display_name')
->execute()->single();
$this->assertEquals('Bob Smith', $address['contact_id.display_name']);
}

/**
* Test updating an existing contact with external_identifier match but
* subtype mismatch.
Expand Down

0 comments on commit d115a91

Please sign in to comment.