Skip to content

Commit

Permalink
[Ref] Unit test attempt to create reported bugs , minor refactor
Browse files Browse the repository at this point in the history
This lays the ground work to test a couple of reported bugs. Minor cleanup of code
  • Loading branch information
eileenmcnaughton committed Jun 9, 2020
1 parent c0bac35 commit 6f672ca
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 20 deletions.
26 changes: 26 additions & 0 deletions CRM/Import/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,32 @@ protected function parsePseudoConstantField($submittedValue, $fieldSpec) {
return '';
}

/**
* Format custom field input.
*
* @param string $customDataType
* @param array $params
*
* @return array
*/
protected function formatCustomFields(array $params, string $customDataType): array {
$dateType = CRM_Core_Session::singleton()->get('dateTypes');
$customFields = CRM_Core_BAO_CustomField::getFields($customDataType);
foreach ($params as $key => $val) {
$customFieldID = CRM_Core_BAO_CustomField::getKeyID($key);
if ($customFieldID) {
if ($customFields[$customFieldID]['data_type'] === 'Date') {
CRM_Contact_Import_Parser_Contact::formatCustomDate($params, $formatted, $dateType, $key);
unset($params[$key]);
}
elseif ($customFields[$customFieldID]['data_type'] === 'Boolean') {
$params[$key] = CRM_Utils_String::strtoboolstr($val);
}
}
}
return $params;
}

/**
* This is code extracted from 4 places where this exact snippet was being duplicated.
*
Expand Down
6 changes: 1 addition & 5 deletions CRM/Member/Import/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
* $Id$
*
* Class CRM_Member_Import_Parser
*/
abstract class CRM_Member_Import_Parser extends CRM_Import_Parser {

Expand Down
15 changes: 3 additions & 12 deletions CRM/Member/Import/Parser/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,10 @@ public function import($onDuplicate, &$values) {
}

$session = CRM_Core_Session::singleton();
$dateType = $session->get('dateTypes');
$formatted = [];
$dateType = CRM_Core_Session::singleton()->get('dateTypes');
$customDataType = !empty($params['contact_type']) ? $params['contact_type'] : 'Membership';
$customFields = CRM_Core_BAO_CustomField::getFields($customDataType);

$formatted = $this->formatCustomFields($params, $customDataType);

// don't add to recent items, CRM-4399
$formatted['skipRecentView'] = TRUE;
Expand Down Expand Up @@ -312,15 +312,6 @@ public function import($onDuplicate, &$values) {
$params[$key] = CRM_Utils_String::strtobool($val);
break;
}
if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
if ($customFields[$customFieldID]['data_type'] == 'Date') {
CRM_Contact_Import_Parser_Contact::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
Expand Down
39 changes: 36 additions & 3 deletions tests/phpunit/CRM/Member/Import/Parser/MembershipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
* @group headless
*/
class CRM_Member_Import_Parser_MembershipTest extends CiviUnitTestCase {
use CRMTraits_Custom_CustomDataTrait;

/**
* Membership type name used in test function.
*
Expand Down Expand Up @@ -79,8 +81,8 @@ public function setUp() {
'fixed_period_start_day' => 101,
'fixed_period_rollover_day' => 1231,
];
$ids = [];
$membershipType = CRM_Member_BAO_MembershipType::add($params, $ids);

$membershipType = CRM_Member_BAO_MembershipType::add($params);
$this->_membershipTypeID = $membershipType->id;

$this->_mebershipStatusID = $this->membershipStatusCreate('test status');
Expand All @@ -102,10 +104,10 @@ public function tearDown() {
'civicrm_membership_payment',
'civicrm_contact',
];
$this->quickCleanup($tablesToTruncate, TRUE);
$this->relationshipTypeDelete($this->_relationshipTypeId);
$this->membershipTypeDelete(['id' => $this->_membershipTypeID]);
$this->membershipStatusDelete($this->_mebershipStatusID);
$this->quickCleanup($tablesToTruncate, TRUE);
}

/**
Expand Down Expand Up @@ -328,4 +330,35 @@ protected function createImportObject(array $fields): \CRM_Member_Import_Parser_
return $membershipImporter;
}

/**
* Test importing to a custom field.
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
*/
public function testImportCustomData() {
$donaldDuckID = $this->individualCreate(['first_name' => 'Donald', 'last_name' => 'Duck']);
$this->createCustomGroupWithFieldsOfAllTypes(['extends' => 'Membership']);
$membershipImporter = $this->createImportObject([
'membership_contact_id',
'membership_type_id',
'membership_start_date',
$this->getCustomFieldName('text'),
$this->getCustomFieldName('select_string'),
]);
$importValues = [
$this->individualCreate(),
$this->_membershipTypeID,
date('Y-m-d'),
'blah',
'Red',
];

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

}

0 comments on commit 6f672ca

Please sign in to comment.