From 79e1afb8069f622e4842aff945468986ba8849f1 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 7 Jun 2022 13:11:32 +1200 Subject: [PATCH] Fix merge resolution issue --- CRM/Activity/Import/Parser/Activity.php | 8 +-- CRM/Contact/Import/Parser/Contact.php | 36 +----------- CRM/Import/Parser.php | 58 ++++++++++++++++--- CRM/Member/Import/Parser/Membership.php | 9 +-- .../Activity/Import/Parser/ActivityTest.php | 6 +- 5 files changed, 57 insertions(+), 60 deletions(-) diff --git a/CRM/Activity/Import/Parser/Activity.php b/CRM/Activity/Import/Parser/Activity.php index a643be5c49..17318f99a4 100644 --- a/CRM/Activity/Import/Parser/Activity.php +++ b/CRM/Activity/Import/Parser/Activity.php @@ -127,8 +127,6 @@ class CRM_Activity_Import_Parser_Activity extends CRM_Import_Parser { /** * 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. * @@ -138,7 +136,7 @@ class CRM_Activity_Import_Parser_Activity extends CRM_Import_Parser { * * @throws \CRM_Core_Exception */ - public function import($onDuplicate, &$values) { + public function import(&$values) { // First make sure this is a valid line try { $this->validateValues($values); @@ -493,8 +491,6 @@ class CRM_Activity_Import_Parser_Activity extends CRM_Import_Parser { } // Trim whitespace around the values. - - $empty = TRUE; foreach ($values as $k => $v) { $values[$k] = trim($v, " \t\r\n"); } @@ -513,7 +509,7 @@ class CRM_Activity_Import_Parser_Activity extends CRM_Import_Parser { $returnCode = $this->summary($values); } elseif ($mode == self::MODE_IMPORT) { - $returnCode = $this->import($onDuplicate, $values); + $returnCode = $this->import($values); if ($statusID && (($this->_lineCount % 50) == 0)) { $prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount); } diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index d284039b30..4e3a454069 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -104,34 +104,6 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { return (bool) (CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'address_options')['street_address_parsing'] ?? FALSE); } - /** - * Is this a case where the user has opted to update existing contacts. - * - * @return bool - * - * @throws \API_Exception - */ - private function isUpdateExistingContacts(): bool { - return in_array((int) $this->getSubmittedValue('onDuplicate'), [ - CRM_Import_Parser::DUPLICATE_UPDATE, - CRM_Import_Parser::DUPLICATE_FILL, - ], TRUE); - } - - /** - * Did the user specify duplicates checking should be skipped, resulting in possible duplicate contacts. - * - * Note we still need to check for external_identifier as it will hard-fail - * if we duplicate. - * - * @return bool - * - * @throws \API_Exception - */ - private function isIgnoreDuplicates(): bool { - return ((int) $this->getSubmittedValue('onDuplicate')) === CRM_Import_Parser::DUPLICATE_NOCHECK; - } - /** * Handle the values in preview mode. * @@ -235,7 +207,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { //format common data, CRM-4062 $this->formatCommonData($field, $formatting); $isUpdate = empty($formatting['id']) ? 'new' : 'updated'; - if (empty($formatting['id']) || $this->isUpdateExistingContacts()) { + if (empty($formatting['id']) || $this->isUpdateExisting()) { $relatedNewContact = $this->createContact($formatting, $formatting['id']); $formatting['id'] = $relatedNewContact->id; } @@ -545,18 +517,17 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { /** * @param array $params * - * @return string|null * @throws \API_Exception * @throws \CRM_Core_Exception * @throws \Civi\API\Exception\NotImplementedException */ - protected function validateParams(array $params): ?string { + protected function validateParams(array $params): void { $contacts = array_merge(['0' => $params], $this->getRelatedContactsParams($params)); $errors = []; foreach ($contacts as $value) { // If we are referencing a related contact, or are in update mode then we // don't need all the required fields if we have enough to find an existing contact. - $useExistingMatchFields = !empty($value['relationship_type_id']) || $this->isUpdateExistingContacts(); + $useExistingMatchFields = !empty($value['relationship_type_id']) || $this->isUpdateExisting(); $prefixString = !empty($value['relationship_label']) ? '(' . $value['relationship_label'] . ') ' : ''; $this->validateRequiredContactFields($value['contact_type'], $value, $useExistingMatchFields, $prefixString); @@ -595,7 +566,6 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { $tempMsg = "Invalid value for field(s) : $errorMessage"; throw new CRM_Core_Exception($tempMsg); } - return $errorMessage; } /** diff --git a/CRM/Import/Parser.php b/CRM/Import/Parser.php index e74fefd412..b92831e750 100644 --- a/CRM/Import/Parser.php +++ b/CRM/Import/Parser.php @@ -181,9 +181,9 @@ abstract class CRM_Import_Parser { /** * Get configured contact type. * - * @throws \API_Exception + * @return string */ - protected function getContactType() { + protected function getContactType(): string { if (!$this->_contactType) { $contactTypeMapping = [ CRM_Import_Parser::CONTACT_INDIVIDUAL => 'Individual', @@ -199,10 +199,8 @@ abstract class CRM_Import_Parser { * Get configured contact type. * * @return string|null - * - * @throws \API_Exception */ - public function getContactSubType() { + public function getContactSubType(): ?string { if (!$this->_contactSubType) { $this->_contactSubType = $this->getSubmittedValue('contactSubType'); } @@ -338,6 +336,30 @@ abstract class CRM_Import_Parser { return ((int) $this->getSubmittedValue('onDuplicate')) === CRM_Import_Parser::DUPLICATE_SKIP; } + /** + * Is this a case where the user has opted to update existing contacts. + * + * @return bool + */ + protected function isUpdateExisting(): bool { + return in_array((int) $this->getSubmittedValue('onDuplicate'), [ + CRM_Import_Parser::DUPLICATE_UPDATE, + CRM_Import_Parser::DUPLICATE_FILL, + ], TRUE); + } + + /** + * Did the user specify duplicates checking should be skipped, resulting in possible duplicate contacts. + * + * Note we still need to check for external_identifier as it will hard-fail + * if we duplicate. + * + * @return bool + */ + protected function isIgnoreDuplicates(): bool { + return ((int) $this->getSubmittedValue('onDuplicate')) === CRM_Import_Parser::DUPLICATE_NOCHECK; + } + /** * Did the user specify duplicates should be filled with missing data. * @@ -1250,7 +1272,7 @@ abstract class CRM_Import_Parser { * * @throws \CRM_Core_Exception Exception thrown if field requirements are not met. */ - protected function validateRequiredFields(array $requiredFields, array $params, $prefixString): void { + protected function validateRequiredFields(array $requiredFields, array $params, $prefixString = ''): void { $missingFields = []; foreach ($requiredFields as $key => $required) { if (!is_array($required)) { @@ -1366,10 +1388,10 @@ abstract class CRM_Import_Parser { * Only show fields for the type to import (not appropriate when looking up * related contact fields). * - * * @return array - * @throws \API_Exception - * @throws \Civi\API\Exception\NotImplementedException + * + * @noinspection PhpDocMissingThrowsInspection + * @noinspection PhpUnhandledExceptionInspection */ protected function getFieldMetadata(string $fieldName, bool $loadOptions = FALSE, $limitToContactType = FALSE): array { @@ -1582,6 +1604,20 @@ abstract class CRM_Import_Parser { $this->validateParams($params); } + /** + * @param array $params + */ + protected function validateParams(array $params): void { + $this->validateRequiredFields($this->getRequiredFields(), $params); + $errors = []; + foreach ($params as $key => $value) { + $errors = array_merge($this->getInvalidValues($value, $key), $errors); + } + if ($errors) { + throw new CRM_Core_Exception('Invalid value for field(s) : ' . implode(',', $errors)); + } + } + /** * Search the value for the string 'invalid_import_value'. * @@ -1699,6 +1735,10 @@ abstract class CRM_Import_Parser { * @throws \API_Exception */ public function getMappedFieldLabel(array $mappedField): string { + // doNotImport is on it's way out - skip fields will be '' once all is done. + if ($mappedField['name'] === 'doNotImport') { + return ''; + } $this->setFieldMetadata(); return $this->getFieldMetadata($mappedField['name'])['title']; } diff --git a/CRM/Member/Import/Parser/Membership.php b/CRM/Member/Import/Parser/Membership.php index f3622e4cde..02e47c6a77 100644 --- a/CRM/Member/Import/Parser/Membership.php +++ b/CRM/Member/Import/Parser/Membership.php @@ -268,11 +268,8 @@ class CRM_Member_Import_Parser_Membership extends CRM_Import_Parser { * * @param array $values * The array of values belonging to this line. - * - * @return bool - * the result of this processing */ - public function validateValues($values) { + public function validateValues($values): void { $params = $this->getMappedRow($values); $errors = []; foreach ($params as $key => $value) { @@ -281,7 +278,7 @@ class CRM_Member_Import_Parser_Membership extends CRM_Import_Parser { if (empty($params['membership_type_id'])) { $errors[] = ts('Missing required fields'); - return NULL; + return; } //To check whether start date or join date is provided @@ -291,8 +288,6 @@ class CRM_Member_Import_Parser_Membership extends CRM_Import_Parser { if ($errors) { throw new CRM_Core_Exception('Invalid value for field(s) : ' . implode(',', $errors)); } - - return CRM_Import_Parser::VALID; } /** diff --git a/tests/phpunit/CRM/Activity/Import/Parser/ActivityTest.php b/tests/phpunit/CRM/Activity/Import/Parser/ActivityTest.php index 39515dfd59..7205b16dc3 100644 --- a/tests/phpunit/CRM/Activity/Import/Parser/ActivityTest.php +++ b/tests/phpunit/CRM/Activity/Import/Parser/ActivityTest.php @@ -50,10 +50,6 @@ class CRM_Activity_Import_Parser_ActivityTest extends CiviUnitTestCase { * * So far this is just testing the class constructor & preparing for more * tests. - * - * @throws \API_Exception - * @throws \CRM_Core_Exception - * @throws \CiviCRM_API3_Exception */ public function testImport(): void { $this->createCustomGroupWithFieldOfType(['extends' => 'Activity'], 'checkbox'); @@ -101,7 +97,7 @@ class CRM_Activity_Import_Parser_ActivityTest extends CiviUnitTestCase { $importer = $this->createImportObject(array_keys($values)); $params = array_values($values); CRM_Core_Session::singleton()->set('dateTypes', 1); - $outcome = $importer->import(NULL, $params); + $outcome = $importer->import($params); $this->assertEquals($expectedOutcome, $outcome); // If there was an error it's in element 0 return $outcome === CRM_Import_Parser::VALID ? '' : $params[0]; -- 2.25.1