// 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.
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];
}
}
}
///////////////// 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']);
}
/**