CRM-14272 - Fix inherited membership relationship date handling
authorColeman Watts <coleman@civicrm.org>
Sun, 29 Nov 2015 22:44:31 +0000 (17:44 -0500)
committerColeman Watts <coleman@civicrm.org>
Mon, 30 Nov 2015 03:18:57 +0000 (22:18 -0500)
CRM/Contact/BAO/Relationship.php
tests/phpunit/api/v3/RelationshipTest.php

index 3be2132fda5a836ac1eb0f234b5ec7d26efc3a9d..ba71b147e99f437703d7f7c0879949b9c654aa56 100644 (file)
@@ -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;
     }
 
index 763a884c40cbee762022ffd1a7b5f4445dc0b31f..1446a154304689d88589a5f7d2f15f3c478c5fd1 100644 (file)
@@ -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);