Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for dev/membership#37 #20493

Merged
merged 1 commit into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions CRM/Contact/BAO/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -2380,6 +2380,25 @@ protected static function addInheritedMembership($membershipValues) {
AND is_current_member = 1";
$result = CRM_Core_DAO::singleValueQuery($query);
if ($result < CRM_Utils_Array::value('max_related', $membershipValues, PHP_INT_MAX)) {
// Convert custom_xx_id fields to custom_xx
// See https://lab.civicrm.org/dev/membership/-/issues/37
// This makes sure the value is copied and not the looked up value.
// Which is the case when the custom field is a contact reference field.
// The custom_xx contains the display name of the contact, instead of the contact id.
// The contact id is then available in custom_xx_id.
foreach ($membershipValues as $field => $value) {
if (stripos($field, 'custom_') !== 0) {
// No a custom field
continue;
}
$custom_id = substr($field, 7);
if (substr($custom_id, -3) === '_id') {
$custom_id = substr($custom_id, 0, -3);
unset($membershipValues[$field]);
$membershipValues['custom_' . $custom_id] = $value;
}
}

civicrm_api3('Membership', 'create', $membershipValues);
}
return $membershipValues;
Expand Down
49 changes: 49 additions & 0 deletions tests/phpunit/CRM/Member/BAO/MembershipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,55 @@ public function testMembershipUpdateDoesNotDeleteRelatedMembershipsByMistake():
$this->membershipStatusDelete($otherStatusID);
}

public function testRelatedMembershipWithContactReferenceCustomField() {
$relatedContactId = $this->individualCreate();
$customContactId = $this->individualCreate();
$membershipOrganizationId = $this->organizationCreate();
$organizationId = $this->organizationCreate();

$membershipTypeWithRelationship = $this->createMembershipType($membershipOrganizationId, TRUE);
$customGroup = $this->customGroupCreate(['extends' => 'Membership']);
$customField = $this->customFieldCreate([
'custom_group_id' => $customGroup['id'],
'data_type' => 'ContactReference',
'html_type' => 'Autocomplete-Select',
'default_value' => '',
]);

// Creating membership of organisation
$membership = $this->callAPISuccess("Membership", "create", [
'membership_type_id' => $membershipTypeWithRelationship["id"],
'contact_id' => $organizationId,
'status_id' => $this->_membershipStatusID,
'custom_' . $customField['id'] => $customContactId,
]);

$membership = $membership['values'][$membership["id"]];

// Create relationship between organization and individual contact
$relationship = $this->callAPISuccess('Relationship', 'create', [
// Employer of relationship
'relationship_type_id' => 5,
'contact_id_a' => $relatedContactId,
'contact_id_b' => $organizationId,
'is_active' => 1,
]);

// Check count of related memberships. It should be one for individual contact.
$relatedMembershipsCount = $this->getRelatedMembershipsCount($membership["id"]);
$this->assertEquals(1, $relatedMembershipsCount, 'Related membership count should be 1.');

$relatedMembership = $this->callAPISuccess("Membership", "getsingle", [
'owner_membership_id' => $membership["id"],
]);

$this->assertEquals($customContactId, $relatedMembership['custom_' . $customField['id'] . '_id']);

$this->callAPISuccess('Relationship', 'delete', ['id' => $relationship['id']]);
$relatedMembershipsCount = $this->getRelatedMembershipsCount($membership["id"]);
$this->assertEquals(0, $relatedMembershipsCount, 'Related membership count should be 0.');
}

/**
* Creates the given amount of contacts.
*
Expand Down