Skip to content

Commit

Permalink
CIVICRM-1063 CIVICRM-1273 fix for not removing the right membership
Browse files Browse the repository at this point in the history
  • Loading branch information
agileware-pengyi committed Oct 9, 2019
1 parent 89f6769 commit c798d73
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 87 deletions.
40 changes: 37 additions & 3 deletions CRM/Contact/BAO/Relationship.php
Expand Up @@ -1701,7 +1701,7 @@ public static function relatedMemberships($contactId, &$params, $ids, $action =
$relTypeIds = [];
if ($action & CRM_Core_Action::DELETE) {
// @todo don't return relTypeId here - but it seems to be used later in a cryptic way (hint cryptic is not a complement).
list($relTypeId, $isDeletable) = self::isInheritedMembershipInvalidated($membershipValues, $values, $cid, $mainRelatedContactId);
list($relTypeId, $isDeletable) = self::isInheritedMembershipInvalidated($membershipValues, $values, $cid, $mainRelatedContactId, $relTypeId);
if ($isDeletable) {
CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipValues['owner_membership_id'], $membershipValues['membership_contact_id']);
}
Expand Down Expand Up @@ -2343,14 +2343,48 @@ private static function isRelationshipTypeCurrentEmployer(int $existingTypeID):
* @param int $cid
* @param int $mainRelatedContactId
*
* @param int $relTypeId
*
* @return array
* @throws \CiviCRM_API3_Exception
*/
private static function isInheritedMembershipInvalidated($membershipValues, array $values, $cid, $mainRelatedContactId): array {
private static function isInheritedMembershipInvalidated($membershipValues, array $values, $cid, $mainRelatedContactId, $relTypeId = null): array {
$membershipType = CRM_Member_BAO_MembershipType::getMembershipType($membershipValues['membership_type_id']);
$relTypeIds = $membershipType['relationship_type_id'];
$relDirections = $membershipType['relationship_direction'];

$similarMemberships = 0;
if (!empty($relTypeIds)) {
$relquery = [];
foreach ($relTypeIds as $key => $reltype_id) {
if ($reltype_id == $relTypeId) {
continue;
}
// b to a relationship
$relquery[] = "(relationship_type_id = $reltype_id AND contact_id_a = $cid AND contact_id_b = $mainRelatedContactId)";
if ($relDirections[$key] == 'a_b') {
// a_b may be a_b or any direction
$relquery[] = "(relationship_type_id = $reltype_id AND contact_id_a = $mainRelatedContactId AND contact_id_b = $cid)";
}
}

if (!empty($relquery)) {
$relquery = '(' . implode(' OR ', $relquery) . ') AND';
}
else {
$relquery = '';
}

$similarMemberships = "SELECT COUNT(id)
FROM civicrm_relationship
WHERE $relquery
is_active = 1 AND
(end_date IS NULL OR end_date > CURRENT_DATE())";
$similarMemberships = CRM_Core_DAO::singleValueQuery($similarMemberships);
}

$membshipInheritedFrom = $membershipValues['owner_membership_id'] ?? NULL;
if (!$membshipInheritedFrom || !in_array($values[$cid]['relationshipTypeId'], $relTypeIds)) {
if (!$membshipInheritedFrom || !in_array($values[$cid]['relationshipTypeId'], $relTypeIds) || $similarMemberships != 0) {
return [implode(',', $relTypeIds), FALSE];
}
//CRM-16300 check if owner membership exist for related membership
Expand Down
84 changes: 0 additions & 84 deletions tests/phpunit/CRM/Contact/BAO/RelationshipTest.php
Expand Up @@ -194,90 +194,6 @@ private function createRelationshipType($count) {
return $orgToPersonTypeId;
}

/**
* Test that two similars membership are not created for two relationships
*/
public function testSingleMembershipForTwoRelationships() {
$individual = $this->callAPISuccess('Contact', 'create', [
'display_name' => 'Individual A',
'contact_type' => 'Individual',
]);
$individual = $individual['values'][$individual['id']];

$organisation = $this->callAPISuccess('Contact', 'create', [
'organization_name' => 'Organization B',
'contact_type' => 'Organization',
]);
$organisation = $organisation['values'][$organisation['id']];

$membershipOrganisation = $this->callAPISuccess('Contact', 'create', [
'organization_name' => 'Membership Organization',
'contact_type' => 'Organization',
]);
$membershipOrganisation = $membershipOrganisation['values'][$membershipOrganisation['id']];

$orgToPersonTypeId1 = $this->createRelationshipType(1);
$orgToPersonTypeId2 = $this->createRelationshipType(2);

$relationShips = implode(CRM_Core_DAO::VALUE_SEPARATOR, array(
$orgToPersonTypeId1['id'],
$orgToPersonTypeId2['id'],
));

$relationShipDirections = implode(CRM_Core_DAO::VALUE_SEPARATOR, array(
'b_a',
'b_a',
));

$membershipType = $this->callAPISuccess('MembershipType', 'create', array(
'member_of_contact_id' => $membershipOrganisation['id'],
'financial_type_id' => 'Member Dues',
'duration_unit' => 'year',
'duration_interval' => 1,
'period_type' => 'rolling',
'name' => 'Inherited Membership',
'relationship_type_id' => $relationShips,
'relationship_direction' => $relationShipDirections,
));
$membershipType = $membershipType['values'][$membershipType['id']];

$this->callAPISuccess('Membership', 'create', array(
'membership_type_id' => $membershipType['id'],
'contact_id' => $organisation['id'],
));

$relationshipOne = $this->createRelationship($individual['id'], $organisation['id'], $orgToPersonTypeId1['id']);
$relationshipTwo = $this->createRelationship($individual['id'], $organisation['id'], $orgToPersonTypeId2['id']);

$this->assertMembershipsCountOfContact($individual['id'], 1);

$relationshipTwo['is_active'] = 0;
$this->callAPISuccess('Relationship', 'create', $relationshipTwo);
$this->assertMembershipsCountOfContact($individual['id'], 1);

$relationshipOne['is_active'] = 0;
$this->callAPISuccess('Relationship', 'create', $relationshipOne);
$this->assertMembershipsCountOfContact($individual['id'], 0);

$relationshipOne['is_active'] = 1;
$this->callAPISuccess('Relationship', 'create', $relationshipOne);
$this->assertMembershipsCountOfContact($individual['id'], 1);

$relationshipTwo['is_active'] = 1;
$this->callAPISuccess('Relationship', 'create', $relationshipTwo);
$this->assertMembershipsCountOfContact($individual['id'], 1);

$this->callAPISuccess('Relationship', 'delete', array(
'id' => $relationshipTwo['id'],
));
$this->assertMembershipsCountOfContact($individual['id'], 1);

$this->callAPISuccess('Relationship', 'delete', array(
'id' => $relationshipOne['id'],
));
$this->assertMembershipsCountOfContact($individual['id'], 0);
}

/**
* Unset non required Relationship data from array
* @param $relationship
Expand Down

0 comments on commit c798d73

Please sign in to comment.