From 6f9793775edee97e2e649ffec34879fb136e3efa Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Mon, 6 Jun 2022 16:41:42 +1200 Subject: [PATCH] Updates to reflect changes made for QA for import_queue --- CRM/Import/Parser.php | 4 +- CRM/Member/Import/Parser/Membership.php | 51 ++++++++----------- .../Member/Import/Parser/MembershipTest.php | 31 ++++++----- 3 files changed, 37 insertions(+), 49 deletions(-) diff --git a/CRM/Import/Parser.php b/CRM/Import/Parser.php index ef1e94583cda..d02d9c1f47f0 100644 --- a/CRM/Import/Parser.php +++ b/CRM/Import/Parser.php @@ -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); diff --git a/CRM/Member/Import/Parser/Membership.php b/CRM/Member/Import/Parser/Membership.php index 56d97dba3a94..f3622e4cde47 100644 --- a/CRM/Member/Import/Parser/Membership.php +++ b/CRM/Member/Import/Parser/Membership.php @@ -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); } @@ -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); @@ -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'])) { @@ -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); } } @@ -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]; @@ -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); } } @@ -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 { @@ -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); } } @@ -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); } } @@ -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; } diff --git a/tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php b/tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php index 4561ef893aa6..7a87f36c6fe1 100644 --- a/tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php +++ b/tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php @@ -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); @@ -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'; @@ -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); } @@ -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); } @@ -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 = [ @@ -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); } @@ -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); @@ -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([ @@ -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')]);