From: Coleman Watts Date: Sun, 29 Nov 2015 22:44:31 +0000 (-0500) Subject: CRM-14272 - Fix inherited membership relationship date handling X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=45089d88b90e152d24d545e0d8f29a21e94896e9;p=civicrm-core.git CRM-14272 - Fix inherited membership relationship date handling --- diff --git a/CRM/Contact/BAO/Relationship.php b/CRM/Contact/BAO/Relationship.php index 3be2132fda..ba71b147e9 100644 --- a/CRM/Contact/BAO/Relationship.php +++ b/CRM/Contact/BAO/Relationship.php @@ -1439,22 +1439,26 @@ LEFT JOIN civicrm_country ON (civicrm_address.country_id = civicrm_country.id) // accordingly. $status = self::CURRENT; $targetContact = $targetContact = CRM_Utils_Array::value('contact_check', $params, array()); + $today = date('Ymd'); + + // If a relationship hasn't yet started, just return for now + // TODO: handle edge-case of updating start_date of an existing relationship + if (!empty($params['start_date'])) { + $startDate = substr(CRM_Utils_Date::format($params['start_date']), 0, 8); + if ($today < $startDate) { + return; + } + } if (!empty($params['end_date'])) { - $endDate = CRM_Utils_Date::setDateDefaults(CRM_Utils_Date::format($params['end_date']), NULL, 'Ymd'); - $today = date('Ymd'); - + $endDate = substr(CRM_Utils_Date::format($params['end_date']), 0, 8); if ($today > $endDate) { $status = self::PAST; } } - if (($action & CRM_Core_Action::ADD) && - ($status & self::PAST) - ) { - // if relationship is PAST and action is ADD, no qustion - // of creating RELATED membership and return back to - // calling method + if (($action & CRM_Core_Action::ADD) && ($status & self::PAST)) { + // If relationship is PAST and action is ADD, do nothing. return; } diff --git a/tests/phpunit/api/v3/RelationshipTest.php b/tests/phpunit/api/v3/RelationshipTest.php index 763a884c40..1446a15430 100644 --- a/tests/phpunit/api/v3/RelationshipTest.php +++ b/tests/phpunit/api/v3/RelationshipTest.php @@ -1265,6 +1265,16 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { )); $contactAMembership = $this->callAPISuccessGetSingle('membership', array('contact_id' => $this->_cId_a)); $this->assertEquals($originalMembership['id'], $contactAMembership['owner_membership_id']); + + // Adding a relationship with a future start date should NOT create a membership + $this->callAPISuccess('Relationship', 'create', array( + 'relationship_type_id' => $this->_relTypeID, + 'contact_id_a' => $this->_cId_a_2, + 'contact_id_b' => $this->_cId_b, + 'start_date' => 'now + 1 week', + )); + $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a_2), 0); + // Deleting the organization should cause the related membership to be deleted. $this->callAPISuccess('contact', 'delete', array('id' => $this->_cId_b)); $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a), 0);