Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev/core#2902 revert apiv3 handling of membership #21795

Merged
merged 1 commit into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions api/v3/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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];
}
}
}
Expand Down
31 changes: 26 additions & 5 deletions tests/phpunit/api/v3/MembershipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -846,12 +846,33 @@ public function testMembershipGetWithReturn() {
///////////////// 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']);
}

/**
Expand Down