Skip to content

Commit

Permalink
Updates to reflect changes made for QA for import_queue
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Jun 6, 2022
1 parent 397ed96 commit 6f97937
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 49 deletions.
4 changes: 2 additions & 2 deletions CRM/Import/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1843,8 +1843,8 @@ protected function getSubtypes($contactType) {
* @param int|null $entityID
* Optional created entity ID
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
* @noinspection PhpDocMissingThrowsInspection
* @noinspection PhpUnhandledExceptionInspection
*/
protected function setImportStatus(int $id, string $status, string $message, ?int $entityID = NULL): void {
$this->getDataSourceObject()->updateStatus($id, $status, $message, $entityID);
Expand Down
51 changes: 20 additions & 31 deletions CRM/Member/Import/Parser/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function run(
while ($row = $dataSource->getRow()) {
$values = array_values($row);
if ($mode == self::MODE_IMPORT) {
$this->import($this->getSubmittedValue('onDuplicate'), $values);
$this->import($values);
if ($statusID && (($this->_lineCount % 50) == 0)) {
$prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount);
}
Expand Down Expand Up @@ -298,15 +298,14 @@ public function validateValues($values) {
/**
* Handle the values in import mode.
*
* @param int $onDuplicate
* The code for what action to take on duplicates.
* @param array $values
* The array of values belonging to this line.
*
* @return bool
* the result of this processing
* @return int|void|null
* the result of this processing - which is ignored
*/
public function import($onDuplicate, &$values) {
public function import($values) {
$onDuplicate = $this->getSubmittedValue('onDuplicate');
$rowNumber = (int) ($values[array_key_last($values)]);
try {
$params = $this->getMappedRow($values);
Expand Down Expand Up @@ -344,8 +343,7 @@ public function import($onDuplicate, &$values) {
//fix for CRM-2219 Update Membership
// onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE
if (!empty($formatted['is_override']) && empty($formatted['status_id'])) {
$this->setImportStatus($rowNumber, 'ERROR', 'Required parameter missing: Status');
return CRM_Import_Parser::ERROR;
throw new CRM_Core_Exception('Required parameter missing: Status', CRM_Import_Parser::ERROR);
}

if (!empty($formatValues['membership_id'])) {
Expand All @@ -372,11 +370,7 @@ public function import($onDuplicate, &$values) {
$this->setImportStatus($rowNumber, 'IMPORTED', 'Required parameter missing: Status');
return CRM_Import_Parser::VALID;
}
else {
array_unshift($values, 'Matching Membership record not found for Membership ID ' . $formatValues['membership_id'] . '. Row was skipped.');
$this->setImportStatus($rowNumber, 'ERROR', 'Matching Membership record not found for Membership ID ' . $formatValues['membership_id'] . '. Row was skipped.');
return CRM_Import_Parser::ERROR;
}
throw new CRM_Core_Exception('Matching Membership record not found for Membership ID ' . $formatValues['membership_id'] . '. Row was skipped.', CRM_Import_Parser::ERROR);
}
}

Expand All @@ -391,8 +385,7 @@ public function import($onDuplicate, &$values) {
if (CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) {
$matchedIDs = explode(',', $error['error_message']['params'][0]);
if (count($matchedIDs) > 1) {
$this->setImportStatus($rowNumber, 'ERROR', 'Multiple matching contact records detected for this row. The membership was not imported');
return CRM_Import_Parser::ERROR;
throw new CRM_Core_Exception('Multiple matching contact records detected for this row. The membership was not imported', CRM_Import_Parser::ERROR);
}
else {
$cid = $matchedIDs[0];
Expand Down Expand Up @@ -426,13 +419,11 @@ public function import($onDuplicate, &$values) {
}
elseif (empty($formatted['is_override'])) {
if (empty($calcStatus)) {
$this->setImportStatus($rowNumber, 'ERROR', 'Status in import row (' . $formatValues['status_id'] . ') does not match calculated status based on your configured Membership Status Rules. Record was not imported.');
return CRM_Import_Parser::ERROR;
throw new CRM_Core_Exception('Status in import row (' . $formatValues['status_id'] . ') does not match calculated status based on your configured Membership Status Rules. Record was not imported.', CRM_Import_Parser::ERROR);
}
if ($formatted['status_id'] != $calcStatus['id']) {
//Status Hold" is either NOT mapped or is FALSE
$this->setImportStatus($rowNumber, 'ERROR', 'Status in import row (' . $formatValues['status_id'] . ') does not match calculated status based on your configured Membership Status Rules (' . $calcStatus['name'] . '). Record was not imported.');
return CRM_Import_Parser::ERROR;
throw new CRM_Core_Exception('Status in import row (' . $formatValues['status_id'] . ') does not match calculated status based on your configured Membership Status Rules (' . $calcStatus['name'] . '). Record was not imported.', CRM_Import_Parser::ERROR);
}
}

Expand Down Expand Up @@ -472,8 +463,7 @@ public function import($onDuplicate, &$values) {
$disp = $params['external_identifier'];
}
}
$this->setImportStatus($rowNumber, 'ERROR', 'No matching Contact found for (' . $disp . ')');
return CRM_Import_Parser::ERROR;
throw new CRM_Core_Exception('No matching Contact found for (' . $disp . ')', CRM_Import_Parser::ERROR);
}
}
else {
Expand All @@ -482,8 +472,7 @@ public function import($onDuplicate, &$values) {
$checkCid->external_identifier = $formatValues['external_identifier'];
$checkCid->find(TRUE);
if ($checkCid->id != $formatted['contact_id']) {
$this->setImportStatus($rowNumber, 'ERROR', 'Mismatch of External ID:' . $formatValues['external_identifier'] . ' and Contact Id:' . $formatted['contact_id']);
return CRM_Import_Parser::ERROR;
throw new CRM_Core_Exception('Mismatch of External ID:' . $formatValues['external_identifier'] . ' and Contact Id:' . $formatted['contact_id'], CRM_Import_Parser::ERROR);
}
}

Expand Down Expand Up @@ -515,14 +504,11 @@ public function import($onDuplicate, &$values) {
}
elseif (empty($formatted['is_override'])) {
if (empty($calcStatus)) {
$this->setImportStatus($rowNumber, 'ERROR', 'Status in import row (' . CRM_Utils_Array::value('status_id', $formatValues) . ') does not match calculated status based on your configured Membership Status Rules. Record was not imported.');
return CRM_Import_Parser::ERROR;
throw new CRM_Core_Exception('Status in import row (' . CRM_Utils_Array::value('status_id', $formatValues) . ') does not match calculated status based on your configured Membership Status Rules. Record was not imported.', CRM_Import_Parser::ERROR);
}
elseif ($formatted['status_id'] != $calcStatus['id']) {
if ($formatted['status_id'] != $calcStatus['id']) {
//Status Hold" is either NOT mapped or is FALSE
array_unshift($values, 'Status in import row (' . CRM_Utils_Array::value('status_id', $formatValues) . ') does not match calculated status based on your configured Membership Status Rules (' . $calcStatus['name'] . '). Record was not imported.');
$this->setImportStatus($rowNumber, 'ERROR', 'Status in import row (' . CRM_Utils_Array::value('status_id', $formatValues) . ') does not match calculated status based on your configured Membership Status Rules (' . $calcStatus['name'] . '). Record was not imported.');
return CRM_Import_Parser::ERROR;
throw new CRM_Core_Exception($rowNumber, 'ERROR', 'Status in import row (' . CRM_Utils_Array::value('status_id', $formatValues) . ') does not match calculated status based on your configured Membership Status Rules (' . $calcStatus['name'] . '). Record was not imported.', CRM_Import_Parser::ERROR);
}
}

Expand All @@ -533,8 +519,11 @@ public function import($onDuplicate, &$values) {
return CRM_Import_Parser::VALID;
}
}
catch (Exception $e) {
array_unshift($values, $e->getMessage());
catch (CRM_Core_Exception $e) {
$this->setImportStatus($rowNumber, 'ERROR', $e->getMessage());
return CRM_Import_Parser::ERROR;
}
catch (CiviCRM_API3_Exception $e) {
$this->setImportStatus($rowNumber, 'ERROR', $e->getMessage());
return CRM_Import_Parser::ERROR;
}
Expand Down
31 changes: 15 additions & 16 deletions tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function testImport(): void {

$importObject = $this->createImportObject(['email', 'membership_type_id', 'membership_start_date', 'membership_join_date']);
foreach ($params as $values) {
$this->assertEquals(CRM_Import_Parser::VALID, $importObject->import(CRM_Import_Parser::DUPLICATE_UPDATE, $values), $values[0]);
$this->assertEquals(CRM_Import_Parser::VALID, $importObject->import($values), $values[0]);
}
$result = $this->callAPISuccess('membership', 'get', ['sequential' => 1])['values'];
$this->assertCount(2, $result);
Expand All @@ -170,11 +170,12 @@ public function testImport(): void {
*
* @throws \CRM_Core_Exception
*/
public function testImportOverriddenMembershipButWithoutStatus() {
public function testImportOverriddenMembershipButWithoutStatus(): void {
$this->individualCreate(['email' => 'anthony_anderson2@civicrm.org']);
$membershipImporter = new CRM_Member_Import_Parser_Membership();
$membershipImporter->setUserJobID($this->getUserJobID([
['email'], ['membership_type_id'], ['membership_start_date'], ['member_is_override'],
'mapper' => [['email'], ['membership_type_id'], ['membership_start_date'], ['member_is_override']],
'onDuplicate' => CRM_Import_Parser::DUPLICATE_UPDATE,
]));
$membershipImporter->init();
$membershipImporter->_contactType = 'Individual';
Expand All @@ -186,7 +187,7 @@ public function testImportOverriddenMembershipButWithoutStatus() {
TRUE,
];

$importResponse = $membershipImporter->import(CRM_Import_Parser::DUPLICATE_UPDATE, $importValues);
$importResponse = $membershipImporter->import($importValues);
$this->assertEquals(CRM_Import_Parser::ERROR, $importResponse);
$this->assertContains('Required parameter missing: Status', $importValues);
}
Expand Down Expand Up @@ -214,7 +215,7 @@ public function testImportOverriddenMembershipWithStatus() {
'New',
];

$importResponse = $membershipImporter->import(CRM_Import_Parser::DUPLICATE_UPDATE, $importValues);
$importResponse = $membershipImporter->import($importValues);
$this->assertEquals(CRM_Import_Parser::VALID, $importResponse);
}

Expand All @@ -235,17 +236,18 @@ public function testImportOverriddenMembershipWithValidOverrideEndDate(): void {
date('Y-m-d'),
];

$importResponse = $membershipImporter->import(CRM_Import_Parser::DUPLICATE_UPDATE, $importValues);
$importResponse = $membershipImporter->import($importValues);
$this->assertEquals(CRM_Import_Parser::VALID, $importResponse);
}

public function testImportOverriddenMembershipWithInvalidOverrideEndDate(): void {
$this->individualCreate(['email' => 'anthony_anderson5@civicrm.org']);

$this->userJobID = $this->getUserJobID([
'mapper' => [['email'], ['membership_type_id'], ['membership_start_date'], ['member_is_override'], ['status_id'], ['status_override_end_date']],
'onDuplicate' => CRM_Import_Parser::DUPLICATE_UPDATE,
]);
$membershipImporter = new CRM_Member_Import_Parser_Membership();
$membershipImporter->setUserJobID($this->getUserJobID([
['email'], ['membership_type_id'], ['membership_start_date'], ['member_is_override'], ['status_id'], ['status_override_end_date'],
]));
$membershipImporter->setUserJobID($this->userJobID);
$membershipImporter->init();

$importValues = [
Expand All @@ -257,7 +259,7 @@ public function testImportOverriddenMembershipWithInvalidOverrideEndDate(): void
'abc',
];

$importResponse = $membershipImporter->import(CRM_Import_Parser::DUPLICATE_UPDATE, $importValues);
$importResponse = $membershipImporter->import($importValues);
$this->assertEquals(CRM_Import_Parser::ERROR, $importResponse);
$this->assertContains('Required parameter missing: Status', $importValues);
}
Expand Down Expand Up @@ -292,7 +294,7 @@ public function testImportMembershipWithRenamedStatus() {
'New-renamed',
];

$importResponse = $membershipImporter->import(CRM_Import_Parser::DUPLICATE_UPDATE, $importValues);
$importResponse = $membershipImporter->import($importValues);
$this->assertEquals(CRM_Import_Parser::VALID, $importResponse);
$createdStatusID = $this->callAPISuccessGetValue('Membership', ['return' => 'status_id']);
$this->assertEquals(CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'New'), $createdStatusID);
Expand Down Expand Up @@ -330,9 +332,6 @@ protected function createImportObject(array $fields): \CRM_Member_Import_Parser_
* @param array $submittedValues
*
* @return int
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
*/
protected function getUserJobID(array $submittedValues = []): int {
$userJobID = UserJob::create()->setValues([
Expand Down Expand Up @@ -384,7 +383,7 @@ public function testImportCustomData(): void {
'Red',
];

$importResponse = $membershipImporter->import(CRM_Import_Parser::DUPLICATE_UPDATE, $importValues);
$importResponse = $membershipImporter->import($importValues);
$this->assertEquals(CRM_Import_Parser::VALID, $importResponse);
$membership = $this->callAPISuccessGetSingle('Membership', []);
$this->assertEquals('blah', $membership[$this->getCustomFieldName('text')]);
Expand Down

0 comments on commit 6f97937

Please sign in to comment.