diff --git a/CRM/Member/Import/Parser/Membership.php b/CRM/Member/Import/Parser/Membership.php index 67fa939784cd..56d97dba3a94 100644 --- a/CRM/Member/Import/Parser/Membership.php +++ b/CRM/Member/Import/Parser/Membership.php @@ -316,36 +316,9 @@ public function import($onDuplicate, &$values) { $params['join_date'] = $params['start_date']; } - $session = CRM_Core_Session::singleton(); - $dateType = CRM_Core_Session::singleton()->get('dateTypes'); $formatted = $params; - - $customDataType = !empty($params['contact_type']) ? $params['contact_type'] : 'Membership'; - $customFields = CRM_Core_BAO_CustomField::getFields($customDataType); - // don't add to recent items, CRM-4399 $formatted['skipRecentView'] = TRUE; - foreach ($params as $key => $val) { - if ($val) { - switch ($key) { - case 'status_id': - // @todo - we can do this based on the presence of 'pseudoconstant' in the metadata rather than field specific. - $params[$key] = $this->parsePseudoConstantField($val, $this->fieldMetadata[$key]); - break; - - } - if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { - if ($customFields[$customFieldID]['data_type'] == 'Date') { - $this->formatCustomDate($params, $formatted, $dateType, $key); - unset($params[$key]); - } - elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') { - $params[$key] = CRM_Utils_String::strtoboolstr($val); - } - } - } - } - //date-Format part ends $formatValues = []; foreach ($params as $key => $field) { @@ -371,7 +344,6 @@ 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'])) { - array_unshift($values, 'Required parameter missing: Status'); $this->setImportStatus($rowNumber, 'ERROR', 'Required parameter missing: Status'); return CRM_Import_Parser::ERROR; } @@ -409,17 +381,16 @@ public function import($onDuplicate, &$values) { } //Format dates - $startDate = CRM_Utils_Date::customFormat(CRM_Utils_Array::value('start_date', $formatted), '%Y-%m-%d'); - $endDate = CRM_Utils_Date::customFormat(CRM_Utils_Array::value('end_date', $formatted), '%Y-%m-%d'); - $joinDate = CRM_Utils_Date::customFormat(CRM_Utils_Array::value('join_date', $formatted), '%Y-%m-%d'); + $startDate = $formatted['start_date']; + $endDate = $formatted['end_date'] ?? NULL; + $joinDate = $formatted['join_date']; - if (empty($formatValues['contact_id'])) { + if (empty($formatValues['id']) && empty($formatValues['contact_id'])) { $error = $this->checkContactDuplicate($formatValues); if (CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) { $matchedIDs = explode(',', $error['error_message']['params'][0]); if (count($matchedIDs) > 1) { - array_unshift($values, 'Multiple matching contact records detected for this row. The membership was not imported'); $this->setImportStatus($rowNumber, 'ERROR', 'Multiple matching contact records detected for this row. The membership was not imported'); return CRM_Import_Parser::ERROR; } @@ -511,7 +482,6 @@ public function import($onDuplicate, &$values) { $checkCid->external_identifier = $formatValues['external_identifier']; $checkCid->find(TRUE); if ($checkCid->id != $formatted['contact_id']) { - array_unshift($values, 'Mismatch of External ID:' . $formatValues['external_identifier'] . ' and Contact 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; } @@ -545,7 +515,6 @@ public function import($onDuplicate, &$values) { } elseif (empty($formatted['is_override'])) { if (empty($calcStatus)) { - 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. 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. Record was not imported.'); return CRM_Import_Parser::ERROR; } @@ -635,15 +604,6 @@ public function membership_format_params($params, &$values, $create = FALSE) { foreach ($params as $key => $value) { - //Handling Custom Data - if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { - $values[$key] = $value; - $type = $customFields[$customFieldID]['html_type']; - if (CRM_Core_BAO_CustomField::isSerialized($customFields[$customFieldID])) { - $values[$key] = self::unserializeCustomValue($customFieldID, $value, $type); - } - } - switch ($key) { case 'contact_id': if (!CRM_Utils_Rule::integer($value)) { @@ -660,13 +620,6 @@ public function membership_format_params($params, &$values, $create = FALSE) { $values[$key] = $value; break; - case 'membership_type_id': - if (!array_key_exists($value, CRM_Member_PseudoConstant::membershipType())) { - throw new Exception('Invalid Membership Type Id'); - } - $values[$key] = $value; - break; - default: break; } diff --git a/tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php b/tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php index 7e0779960965..4561ef893aa6 100644 --- a/tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php +++ b/tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php @@ -50,7 +50,9 @@ class CRM_Member_Import_Parser_MembershipTest extends CiviUnitTestCase { * * @var string */ - protected $_membershipTypeID = NULL; + protected $_membershipTypeID; + + protected $entity = 'Membership'; /** * Set up for test. @@ -393,7 +395,7 @@ public function testImportCustomData(): void { * Test the full form-flow import. */ public function testImportCSV() :void { - $this->importCSV('memberships.csv', [ + $this->importCSV('memberships_invalid.csv', [ ['name' => 'membership_contact_id'], ['name' => 'membership_source'], ['name' => 'membership_type_id'], @@ -424,6 +426,30 @@ public function testImportTSV() :void { $this->callAPISuccessGetSingle('Membership', []); } + /** + * Test dates are parsed. + */ + public function testUpdateWithCustomDates(): void { + $this->createCustomGroupWithFieldOfType([], 'date'); + $contactID = $this->individualCreate(['external_identifier' => 'ext-1']); + $this->callAPISuccess('Membership', 'create', [ + 'contact_id' => $contactID, + 'membership_type_id' => 'General', + 'start_date' => '2020-10-01', + ]); + $mapping = [ + ['name' => 'membership_id'], + ['name' => 'membership_source'], + ['name' => 'membership_type_id'], + ['name' => 'membership_start_date'], + ['name' => $this->getCustomFieldName('date')], + ]; + $this->importCSV('memberships_update_custom_date.csv', $mapping, ['dateFormats' => 32]); + $membership = $this->callAPISuccessGetSingle('Membership', []); + $this->assertEquals('2021-03-23', $membership['start_date']); + $this->assertEquals('2019-03-23 00:00:00', $membership[$this->getCustomFieldName('date')]); + } + /** * Import the csv file values. * diff --git a/tests/phpunit/CRM/Member/Import/Parser/data/memberships.csv b/tests/phpunit/CRM/Member/Import/Parser/data/memberships.csv index dfdf1524fbcd..56acafa69375 100644 --- a/tests/phpunit/CRM/Member/Import/Parser/data/memberships.csv +++ b/tests/phpunit/CRM/Member/Import/Parser/data/memberships.csv @@ -1,2 +1,2 @@ Contact ID,Source,Membership Type,Start Date,Ignore me -1,Import,General,2019-03-23,Just some cruft +1,Import,Made Up,2019-03-23,Just some cruft diff --git a/tests/phpunit/CRM/Member/Import/Parser/data/memberships_invalid.csv b/tests/phpunit/CRM/Member/Import/Parser/data/memberships_invalid.csv new file mode 100644 index 000000000000..56acafa69375 --- /dev/null +++ b/tests/phpunit/CRM/Member/Import/Parser/data/memberships_invalid.csv @@ -0,0 +1,2 @@ +Contact ID,Source,Membership Type,Start Date,Ignore me +1,Import,Made Up,2019-03-23,Just some cruft diff --git a/tests/phpunit/CRM/Member/Import/Parser/data/memberships_update_custom_date.csv b/tests/phpunit/CRM/Member/Import/Parser/data/memberships_update_custom_date.csv new file mode 100644 index 000000000000..f49273cef358 --- /dev/null +++ b/tests/phpunit/CRM/Member/Import/Parser/data/memberships_update_custom_date.csv @@ -0,0 +1,2 @@ +Membership ID,Source,Membership Type,Start Date,Custom Date +1,Import,General,23/03/2021,23/03/2019