dev/core#2902 revert apiv3 handling of membership
authorEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 11 Oct 2021 00:06:11 +0000 (13:06 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 11 Oct 2021 06:46:55 +0000 (19:46 +1300)
This reverts changes that were making api v3 date handling change if skipStatusCalc were set.

api/v3/Membership.php
tests/phpunit/api/v3/MembershipTest.php

index 52e64b8b741bf653cf755544d81991aa97c191c9..c7a3b1d5e478cba39c8a0fc6575f83f75a370d92 100644 (file)
@@ -90,9 +90,19 @@ function civicrm_api3_membership_create($params) {
 
   // Calculate membership dates
   // Fixme: This code belongs in the BAO
-  if (!empty($params['num_terms'])) {
+  if (empty($params['id']) || !empty($params['num_terms'])) {
     // If this is a new membership or we have a specified number of terms calculate membership dates.
-    if (!empty($params['id'])) {
+    if (empty($params['id'])) {
+      // This is a new membership, calculate the membership dates.
+      $calcDates = CRM_Member_BAO_MembershipType::getDatesForMembershipType(
+        $params['membership_type_id'],
+        CRM_Utils_Array::value('join_date', $params),
+        CRM_Utils_Array::value('start_date', $params),
+        CRM_Utils_Array::value('end_date', $params),
+        CRM_Utils_Array::value('num_terms', $params, 1)
+      );
+    }
+    else {
       // This is an existing membership, calculate the membership dates after renewal
       // num_terms is treated as a 'special sauce' for is_renewal but this
       // isn't really helpful for completing pendings.
@@ -102,10 +112,10 @@ function civicrm_api3_membership_create($params) {
         CRM_Utils_Array::value('membership_type_id', $params),
         $params['num_terms']
       );
-      foreach (['join_date', 'start_date', 'end_date'] as $date) {
-        if (empty($params[$date]) && isset($calcDates[$date])) {
-          $params[$date] = $calcDates[$date];
-        }
+    }
+    foreach (['join_date', 'start_date', 'end_date'] as $date) {
+      if (empty($params[$date]) && isset($calcDates[$date])) {
+        $params[$date] = $calcDates[$date];
       }
     }
   }
index e9c0cdb1582a7a2bd7dc8f47c7f9bb508fb8a349..31f06d7c901e10b18cb7564bca2597c3ddf5c548 100644 (file)
@@ -843,12 +843,33 @@ class api_v3_MembershipTest extends CiviUnitTestCase {
   ///////////////// civicrm_membership_create methods
 
   /**
-   * Test civicrm_contact_memberships_create with empty params.
-   * Error expected.
+   * Test dates are calculated for pending membership.
    */
-  public function testCreateWithEmptyParams() {
-    $params = [];
-    $this->callAPIFailure('membership', 'create', $params);
+  public function testCreatePending(): void {
+    $membership = $this->callAPISuccess('Membership', 'create', [
+      'contact_id' => $this->individualCreate(),
+      'membership_type_id' => 'General',
+      'status_id' => 'Pending',
+      'sequential' => 1,
+    ])['values'][0];
+    $this->assertEquals(date('Ymd'), $membership['start_date']);
+    $this->assertEquals(date('Ymd', strtotime('+1 year -1 day')), $membership['end_date']);
+  }
+
+  /**
+   * Test dates are calculated for pending membership.
+   */
+  public function testCreatePendingWithSkipStatusCalc(): void {
+    $membership = $this->callAPISuccess('Membership', 'create', [
+      'contact_id' => $this->individualCreate(),
+      'membership_type_id' => 'General',
+      'status_id' => 'Pending',
+      'sequential' => 1,
+      'skipStatusCal' => TRUE,
+    ])['values'][0];
+    $this->assertEquals(date('Ymd'), $membership['start_date']);
+    $this->assertEquals(date('Ymd'), $membership['join_date']);
+    $this->assertEquals(date('Ymd', strtotime('+1 year -1 day')), $membership['end_date']);
   }
 
   /**