Skip to content

Commit

Permalink
Minor cleanup on import return codes
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed May 30, 2022
1 parent 779b3ef commit abbea1e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
41 changes: 14 additions & 27 deletions CRM/Contact/Import/Parser/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public function preview(&$values) {
* CRM_Import_Parser::ERROR or CRM_Import_Parser::VALID
*/
public function summary(&$values): int {
$rowNumber = (int) ($values[count($values) - 1]);
$rowNumber = (int) ($values[array_key_last($values)]);
try {
$this->validateValues($values);
}
Expand Down Expand Up @@ -270,6 +270,7 @@ public function getAllFields() {
* @throws \API_Exception
*/
public function import($onDuplicate, &$values) {
$rowNumber = (int) $values[array_key_last($values)];
$this->_unparsedStreetAddressContacts = [];
if (!$this->getSubmittedValue('doGeocodeAddress')) {
// CRM-5854, reset the geocode method to null to prevent geocoding
Expand All @@ -282,7 +283,7 @@ public function import($onDuplicate, &$values) {

if ($response != CRM_Import_Parser::VALID) {
$this->setImportStatus((int) $values[count($values) - 1], 'Invalid', "Invalid (Error Code: $response)");
return $response;
return FALSE;
}

$params = $this->getMappedRow($values);
Expand All @@ -302,7 +303,7 @@ public function import($onDuplicate, &$values) {
}
catch (CRM_Core_Exception $e) {
$statuses = [CRM_Import_Parser::DUPLICATE => 'DUPLICATE', CRM_Import_Parser::ERROR => 'ERROR', CRM_Import_Parser::NO_MATCH => 'invalid_no_match'];
$this->setImportStatus((int) $values[count($values) - 1], $statuses[$e->getErrorCode()], $e->getMessage());
$this->setImportStatus($rowNumber, $statuses[$e->getErrorCode()], $e->getMessage());
return FALSE;
}

Expand Down Expand Up @@ -334,10 +335,8 @@ public function import($onDuplicate, &$values) {
elseif (CRM_Core_Error::isAPIError($newContact, CRM_Core_Error::DUPLICATE_CONTACT)) {
// if duplicate, no need of further processing
if ($onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP) {
$errorMessage = "Skipping duplicate record";
array_unshift($values, $errorMessage);
$this->setImportStatus((int) $values[count($values) - 1], 'DUPLICATE', $errorMessage);
return CRM_Import_Parser::DUPLICATE;
$this->setImportStatus($rowNumber, 'DUPLICATE', 'Skipping duplicate record');
return FALSE;
}

// CRM-10433/CRM-20739 - IDs could be string or array; handle accordingly
Expand Down Expand Up @@ -427,10 +426,8 @@ public function import($onDuplicate, &$values) {
}
}
else {
$errorMessage = $relatedNewContact['error_message'];
array_unshift($values, $errorMessage);
$this->setImportStatus((int) $values[count($values) - 1], 'ERROR', $errorMessage);
return CRM_Import_Parser::ERROR;
$this->setImportStatus((int) $values[count($values) - 1], 'ERROR', $relatedNewContact['error_message']);
return FALSE;
}
}
else {
Expand All @@ -449,9 +446,8 @@ public function import($onDuplicate, &$values) {
}

if (!empty($relatedCsType) && (!CRM_Contact_BAO_ContactType::isAllowEdit($matchedIDs[0], $relatedCsType) && $relatedCsType != CRM_Utils_Array::value('contact_sub_type', $formatting))) {
$errorMessage = ts("Mismatched or Invalid contact subtype found for this related contact.");
array_unshift($values, $errorMessage);
return CRM_Import_Parser::NO_MATCH;
$this->setImportStatus((int) $values[count($values) - 1], 'invalid_no_match', 'Mismatched or Invalid contact subtype found for this related contact.');
return FALSE;
}
else {
$updatedContact = $this->createContact($formatting, $contactFields, $onDuplicate, $matchedIDs[0]);
Expand Down Expand Up @@ -550,10 +546,8 @@ public function import($onDuplicate, &$values) {
return $this->handleDuplicateError($newContact, $values, $onDuplicate, $formatted, $contactFields);
}
// Not a dupe, so we had an error
$errorMessage = $newContact['error_message'];
array_unshift($values, $errorMessage);
$this->setImportStatus((int) $values[count($values) - 1], 'ERROR', $errorMessage);
return CRM_Import_Parser::ERROR;
$this->setImportStatus((int) $values[count($values) - 1], 'ERROR', $newContact['error_message']);
return FALSE;

}

Expand Down Expand Up @@ -1910,15 +1904,8 @@ public function run(
$prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount);
}
}
else {
$returnCode = self::ERROR;
}

if ($returnCode & self::NO_MATCH) {
$this->setImportStatus((int) $values[count($values) - 1], 'invalid_no_match', array_shift($values));
}

if ($returnCode & self::UNPARSED_ADDRESS_WARNING) {
// @todo this should be done within import - it probably is!
if (isset($returnCode) && $returnCode === self::UNPARSED_ADDRESS_WARNING) {
$this->setImportStatus((int) $values[count($values) - 1], 'warning_unparsed_address', array_shift($values));
}
}
Expand Down
11 changes: 9 additions & 2 deletions tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,9 @@ public function testImportFieldsWithVariousOptions(): void {
*/
protected function runImport(array $originalValues, $onDuplicateAction, $expectedResult, $fieldMapping = [], $fields = NULL, int $ruleGroupId = NULL): void {
$values = array_values($originalValues);
// Stand in for row number.
$values[] = 1;

if ($fieldMapping) {
$fields = [];
foreach ($fieldMapping as $mappedField) {
Expand Down Expand Up @@ -1855,6 +1858,8 @@ public function testImportParserDoesNotMatchPrimaryToRelated(): void {
$fields = array_keys($contactImportValues);
$values = array_values($contactImportValues);
$values[] = 'tim.cook@apple.com';
// Stand in for row number.
$values[] = 1;

$userJobID = $this->getUserJobID([
'mapper' => $mapper,
Expand All @@ -1863,9 +1868,11 @@ public function testImportParserDoesNotMatchPrimaryToRelated(): void {

$parser = new CRM_Contact_Import_Parser_Contact($fields);
$parser->setUserJobID($userJobID);
$parser->init();
$dataSource = new CRM_Import_DataSource_CSV($userJobID);

$this->assertEquals(CRM_Import_Parser::ERROR, $parser->import(CRM_Import_Parser::DUPLICATE_UPDATE, $values), 'Return code from parser import was not as expected');
$parser->init();
$parser->import(CRM_Import_Parser::DUPLICATE_UPDATE, $values);
$this->assertEquals(1, $dataSource->getRowCount([CRM_Import_Parser::ERROR]));
$this->callAPISuccessGetSingle('Contact', [
'first_name' => 'Bob',
'last_name' => 'Dobbs',
Expand Down

0 comments on commit abbea1e

Please sign in to comment.