Skip to content

Commit

Permalink
Add test to check update-with-id works and date handling works
Browse files Browse the repository at this point in the history
Fix test to test the matching csv (changed by mistake)
  • Loading branch information
eileenmcnaughton committed Jun 6, 2022
1 parent 2238da0 commit 397ed96
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 54 deletions.
55 changes: 4 additions & 51 deletions CRM/Member/Import/Parser/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)) {
Expand All @@ -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;
}
Expand Down
30 changes: 28 additions & 2 deletions tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Contact ID,Source,Membership Type,Start Date,Ignore me
1,Import,Made Up,2019-03-23,Just some cruft
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Membership ID,Source,Membership Type,Start Date,Custom Date
1,Import,General,23/03/2021,23/03/2019

0 comments on commit 397ed96

Please sign in to comment.