Skip to content

Commit

Permalink
Merge pull request #14955 from eileenmcnaughton/rel_alok_ref
Browse files Browse the repository at this point in the history
[REF] Extract chunk of code relating to whether to disabled an inherited relationship
  • Loading branch information
eileenmcnaughton authored Aug 9, 2019
2 parents 920cc06 + 98fd6fc commit 2fb8548
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
45 changes: 30 additions & 15 deletions CRM/Contact/BAO/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -1670,21 +1670,9 @@ public static function relatedMemberships($contactId, &$params, $ids, $action =
foreach ($details['memberships'] as $membershipId => $membershipValues) {
$relTypeIds = [];
if ($action & CRM_Core_Action::DELETE) {
// Delete memberships of the related contacts only if relationship type exists for membership type
$query = "
SELECT relationship_type_id, relationship_direction
FROM civicrm_membership_type
WHERE id = {$membershipValues['membership_type_id']}";
$dao = CRM_Core_DAO::executeQuery($query);
$relTypeDirs = [];
while ($dao->fetch()) {
$relTypeId = $dao->relationship_type_id;
$relDirection = $dao->relationship_direction;
}
$relTypeIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $relTypeId);
if (in_array($values[$cid]['relationshipTypeId'], $relTypeIds
//CRM-16300 check if owner membership exist for related membership
) && !empty($membershipValues['owner_membership_id']) && !empty($values[$mainRelatedContactId]['memberships'][$membershipValues['owner_membership_id']])) {
// @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);
if ($isDeletable) {
CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipValues['owner_membership_id'], $membershipValues['membership_contact_id']);
}
continue;
Expand Down Expand Up @@ -2324,4 +2312,31 @@ private static function isRelationshipTypeCurrentEmployer(int $existingTypeID):
return $isCurrentEmployerRelationshipType;
}

/**
* Is the inherited relationship invalidated by this relationship change.
*
* @param $membershipValues
* @param array $values
* @param int $cid
* @param int $mainRelatedContactId
*
* @return array
*/
private static function isInheritedMembershipInvalidated($membershipValues, array $values, $cid, $mainRelatedContactId): array {
// Delete memberships of the related contacts only if relationship type exists for membership type
$query = "
SELECT relationship_type_id, relationship_direction
FROM civicrm_membership_type
WHERE id = {$membershipValues['membership_type_id']}";
$dao = CRM_Core_DAO::executeQuery($query);
while ($dao->fetch()) {
$relTypeId = $dao->relationship_type_id;
}
$relTypeIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $relTypeId);
$isDeletable = in_array($values[$cid]['relationshipTypeId'], $relTypeIds
//CRM-16300 check if owner membership exist for related membership
) && !empty($membershipValues['owner_membership_id']) && !empty($values[$mainRelatedContactId]['memberships'][$membershipValues['owner_membership_id']]);
return [$relTypeId, $isDeletable];
}

}
11 changes: 7 additions & 4 deletions tests/phpunit/CRM/Contact/BAO/RelationshipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,18 @@ public function testSingleMembershipForTwoRelationships() {
'relationship_type_id' => $orgToPersonTypeId2,
]);

$this->callAPISuccessGetCount('Membership', ['contact_id' => $individualID], 1);

// Disable the relationship & check the membership is removed.
$relationshipOne['is_active'] = 0;
$this->callAPISuccess('Relationship', 'create', array_merge($relationshipOne, ['is_active' => 0]));
$this->callAPISuccessGetCount('Membership', ['contact_id' => $individualID], 0);
/*
* @todo this section not yet working due to bug in would-be-tested code.
$this->callAPISuccessGetCount('Membership', ['contact_id' => $individualID], 1);
$relationshipTwo['is_active'] = 0;
$this->callAPISuccess('Relationship', 'create', $relationshipTwo);
$this->callAPISuccessGetCount('Membership', ['contact_id' => $individualID], 1);
$relationshipOne['is_active'] = 0;
$this->callAPISuccess('Relationship', 'create', $relationshipOne);
$this->callAPISuccessGetCount('Membership', ['contact_id' => $individualID], 0);
$relationshipOne['is_active'] = 1;
$this->callAPISuccess('Relationship', 'create', $relationshipOne);
$this->callAPISuccessGetCount('Membership', ['contact_id' => $individualID], 1);
Expand Down

0 comments on commit 2fb8548

Please sign in to comment.