From 8c33a68ca6818cdf9ac95d84d0ebc1ee24f95af7 Mon Sep 17 00:00:00 2001 From: eileen Date: Sun, 4 Aug 2013 19:15:34 +1200 Subject: [PATCH] CRM-13067 fix up membership date setting to be done in BAO & work for API --- CRM/Member/BAO/Membership.php | 30 ++++++- CRM/Member/BAO/MembershipType.php | 7 +- api/v3/Membership.php | 1 + api/v3/MembershipType.php | 3 +- .../examples/Membership/filterIsCurrent.php | 2 +- api/v3/examples/MembershipCreate.php | 4 +- api/v3/examples/MembershipGet.php | 4 +- api/v3/examples/MembershipUpdate.php | 4 +- tests/phpunit/api/v3/MembershipTest.php | 86 ++++++++++++++++++- 9 files changed, 121 insertions(+), 20 deletions(-) diff --git a/CRM/Member/BAO/Membership.php b/CRM/Member/BAO/Membership.php index 77d5c02af2..3559827b8b 100644 --- a/CRM/Member/BAO/Membership.php +++ b/CRM/Member/BAO/Membership.php @@ -237,6 +237,7 @@ class CRM_Member_BAO_Membership extends CRM_Member_DAO_Membership { * @static */ static function &create(&$params, &$ids, $skipRedirect = FALSE, $activityType = 'Membership Signup') { + $membershipID = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('membership', $ids)); // always calculate status if is_override/skipStatusCal is not true. // giving respect to is_override during import. CRM-4012 @@ -248,7 +249,30 @@ class CRM_Member_BAO_Membership extends CRM_Member_DAO_Membership { ) { $dates = array('start_date', 'end_date', 'join_date'); foreach ($dates as $date) { - $$date = CRM_Utils_Date::processDate(CRM_Utils_Array::value($date, $params), NULL, TRUE, 'Ymd'); + // see notes below if id is not passed in we will calc the dates, not sure impact of not + // changing the return null if doing an update so being cautious + // and leaving behaviour unchanged if id exists (null doesn't seem valid...) + $returnNull = TRUE; + if(!$membershipID) { + $returnNull = FALSE; + } + $$date = CRM_Utils_Date::processDate(CRM_Utils_Array::value($date, $params), NULL, $returnNull, 'Ymd'); + if(!empty($$date)) { + $params[$date] = $$date; + } + } + /* + * if we have not been given the start date or the end date we will calculate them. + * In the interests of being cautious we won't do this if 'id' is set + * even though skipStatusCal is the default from the api when id is set + * AND the forms currently take care of the dates + * this could possibly be revised to a less cautious approach + */ + if(!$membershipID && (empty($start_date) || empty($end_date))) { + $defaults = CRM_Member_BAO_MembershipType::getDatesForMembershipType($params['membership_type_id'], + $join_date, $start_date, $end_date + ); + $params = array_merge($defaults, $params); } //fix for CRM-3570, during import exclude the statuses those having is_admin = 1 @@ -261,7 +285,7 @@ class CRM_Member_BAO_Membership extends CRM_Member_DAO_Membership { $excludeIsAdmin = TRUE; } - $calcStatus = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($start_date, $end_date, $join_date, + $calcStatus = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($params['start_date'], $params['end_date'], $params['join_date'], 'today', $excludeIsAdmin ); if (empty($calcStatus)) { @@ -275,7 +299,7 @@ class CRM_Member_BAO_Membership extends CRM_Member_DAO_Membership { 'legacy_redirect_path' => 'civicrm/contact/view', 'legacy_redirect_query' => "reset=1&force=1&cid={$params['contact_id']}&selectedChild=member", ); - throw new CRM_Core_Exception(ts('The membership cannot be saved.'), 0, $errorParams); + throw new CRM_Core_Exception(ts('The membership cannot be saved because the status cannot be calculated.'), 0, $errorParams); } $params['status_id'] = $calcStatus['id']; } diff --git a/CRM/Member/BAO/MembershipType.php b/CRM/Member/BAO/MembershipType.php index 8b710a02b2..c7813c5bc3 100644 --- a/CRM/Member/BAO/MembershipType.php +++ b/CRM/Member/BAO/MembershipType.php @@ -131,7 +131,7 @@ class CRM_Member_BAO_MembershipType extends CRM_Member_DAO_MembershipType { * @param int $membershipTypeId * @static */ - static function del($membershipTypeId, $skipRedirect = FALSE) { + static function del($membershipTypeId) { //check dependencies $check = FALSE; $status = array(); @@ -150,8 +150,6 @@ class CRM_Member_BAO_MembershipType extends CRM_Member_DAO_MembershipType { } } if ($check) { - - $cnt = 1; $message = ts('This membership type cannot be deleted due to following reason(s):'); if (in_array('Membership', $status)) { @@ -175,8 +173,7 @@ class CRM_Member_BAO_MembershipType extends CRM_Member_DAO_MembershipType { //fix for membership type delete api $result = FALSE; if ($membershipType->find(TRUE)) { - $membershipType->delete(); - $result = TRUE; + return $membershipType->delete(); } return $result; diff --git a/api/v3/Membership.php b/api/v3/Membership.php index 1f7f5f5460..dc7d60f6cf 100644 --- a/api/v3/Membership.php +++ b/api/v3/Membership.php @@ -141,6 +141,7 @@ function civicrm_api3_membership_create($params) { */ function _civicrm_api3_membership_create_spec(&$params) { $params['contact_id']['api.required'] = 1; + $params['join_date']['api.default'] = 'now'; $params['skipStatusCal'] = array('title' => 'skip status calculation. By default this is 0 if id is not set and 1 if it is set'); } /** diff --git a/api/v3/MembershipType.php b/api/v3/MembershipType.php index ba39f35a92..7e07cfb70d 100644 --- a/api/v3/MembershipType.php +++ b/api/v3/MembershipType.php @@ -105,7 +105,6 @@ function civicrm_api3_membership_type_get($params) { * {getfields MembershipType_delete} */ function civicrm_api3_membership_type_delete($params) { - $memberDelete = CRM_Member_BAO_MembershipType::del($params['id'], 1); - return $memberDelete ? civicrm_api3_create_success($memberDelete) : civicrm_api3_create_error('Error while deleting membership type. id : ' . $params['id']); + return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params); } diff --git a/api/v3/examples/Membership/filterIsCurrent.php b/api/v3/examples/Membership/filterIsCurrent.php index 2bf364e3ab..5b87f5e235 100644 --- a/api/v3/examples/Membership/filterIsCurrent.php +++ b/api/v3/examples/Membership/filterIsCurrent.php @@ -41,7 +41,7 @@ function membership_get_expectedresult(){ 'membership_id' => '1', 'contact_id' => '13', 'membership_contact_id' => '13', - 'membership_type_id' => '11', + 'membership_type_id' => '21', 'join_date' => '2009-01-21', 'start_date' => '2009-01-21', 'membership_start_date' => '2009-01-21', diff --git a/api/v3/examples/MembershipCreate.php b/api/v3/examples/MembershipCreate.php index 47ec1be3ba..ca8404a1b7 100644 --- a/api/v3/examples/MembershipCreate.php +++ b/api/v3/examples/MembershipCreate.php @@ -6,7 +6,7 @@ function membership_create_example(){ $params = array( 'contact_id' => 25, - 'membership_type_id' => 22, + 'membership_type_id' => 42, 'join_date' => '2009-01-21', 'start_date' => '2009-01-21', 'end_date' => '2009-12-21', @@ -44,7 +44,7 @@ function membership_create_expectedresult(){ '1' => array( 'id' => '1', 'contact_id' => '25', - 'membership_type_id' => '22', + 'membership_type_id' => '42', 'join_date' => '20090121000000', 'start_date' => '20090121000000', 'end_date' => '20091221000000', diff --git a/api/v3/examples/MembershipGet.php b/api/v3/examples/MembershipGet.php index fe5bcf0904..1e7c01af64 100644 --- a/api/v3/examples/MembershipGet.php +++ b/api/v3/examples/MembershipGet.php @@ -5,7 +5,7 @@ */ function membership_get_example(){ $params = array( - 'membership_type_id' => 8, + 'membership_type_id' => 15, ); try{ @@ -36,7 +36,7 @@ function membership_get_expectedresult(){ '1' => array( 'id' => '1', 'contact_id' => '10', - 'membership_type_id' => '8', + 'membership_type_id' => '15', 'join_date' => '2009-01-21', 'start_date' => '2009-01-21', 'end_date' => '2009-12-21', diff --git a/api/v3/examples/MembershipUpdate.php b/api/v3/examples/MembershipUpdate.php index 53ecc5e652..e56f7a90f0 100644 --- a/api/v3/examples/MembershipUpdate.php +++ b/api/v3/examples/MembershipUpdate.php @@ -6,7 +6,7 @@ function membership_update_example(){ $params = array( 'contact_id' => 31, - 'membership_type_id' => 28, + 'membership_type_id' => 54, 'join_date' => '2009-01-21', 'start_date' => '2009-01-21', 'end_date' => '2009-12-21', @@ -44,7 +44,7 @@ function membership_update_expectedresult(){ '1' => array( 'id' => '1', 'contact_id' => '31', - 'membership_type_id' => '28', + 'membership_type_id' => '54', 'join_date' => '20090121000000', 'start_date' => '20090121000000', 'end_date' => '20091221000000', diff --git a/tests/phpunit/api/v3/MembershipTest.php b/tests/phpunit/api/v3/MembershipTest.php index b0053094ae..981f5490d8 100644 --- a/tests/phpunit/api/v3/MembershipTest.php +++ b/tests/phpunit/api/v3/MembershipTest.php @@ -39,6 +39,7 @@ class api_v3_MembershipTest extends CiviUnitTestCase { protected $_apiversion; protected $_contactID; protected $_membershipTypeID; + protected $_membershipTypeID2; protected $_membershipStatusID; protected $__membershipID; protected $_entity; @@ -51,9 +52,9 @@ class api_v3_MembershipTest extends CiviUnitTestCase { $this->_apiversion = 3; $this->_contactID = $this->individualCreate(); $this->_membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID)); + $this->_membershipTypeID2 = $this->membershipTypeCreate(array('period_type' => 'fixed','fixed_period_start_day' => '301', 'fixed_period_rollover_day' => '1111')); $this->_membershipStatusID = $this->membershipStatusCreate('test status'); - require_once 'CRM/Member/PseudoConstant.php'; CRM_Member_PseudoConstant::membershipType(NULL, TRUE); CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'name', TRUE); CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name'); @@ -80,6 +81,7 @@ class api_v3_MembershipTest extends CiviUnitTestCase { TRUE ); $this->membershipStatusDelete($this->_membershipStatusID); + $this->membershipTypeDelete(array('id' => $this->_membershipTypeID2)); $this->membershipTypeDelete(array('id' => $this->_membershipTypeID)); $this->contactDelete($this->_contactID); @@ -307,7 +309,7 @@ class api_v3_MembershipTest extends CiviUnitTestCase { */ function testGetWithRelationship() { $membershipOrgId = $this->organizationCreate(NULL); - $memberContactId = $this->individualCreate(NULL); + $memberContactId = $this->individualCreate(); $relTypeParams = array( 'name_a_b' => 'Relation 1', @@ -660,6 +662,84 @@ class api_v3_MembershipTest extends CiviUnitTestCase { )); } - ///////////////// civicrm_membership_delete methods + /** + * Test that if membership join date is not set it defaults to today + */ + function testEmptyJoinDate() { + unset($this->_params['join_date'], $this->_params['is_override']); + $result = $this->callAPISuccess($this->_entity, 'create', $this->_params); + $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); + $this->assertEquals(date('Y-m-d', strtotime('now')), $result['join_date']); + $this->assertEquals('2009-01-21', $result['start_date']); + $this->assertEquals('2009-12-21', $result['end_date']); + } + /** + * Test that if membership start date is not set it defaults to correct end date + * - fixed + */ + function testEmptyStartDateFixed() { + unset($this->_params['start_date'], $this->_params['is_override']); + $this->_params['membership_type_id'] = $this->_membershipTypeID2; + $result = $this->callAPISuccess($this->_entity, 'create', $this->_params); + $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); + $this->assertEquals('2009-01-21', $result['join_date']); + $this->assertEquals('2008-03-01', $result['start_date']); + $this->assertEquals('2009-12-21', $result['end_date']); + } + + /** + * Test that if membership start date is not set it defaults to correct end date + * - fixed + */ + function testEmptyStartDateRolling() { + unset($this->_params['start_date'], $this->_params['is_override']); + $result = $this->callAPISuccess($this->_entity, 'create', $this->_params); + $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); + $this->assertEquals('2009-01-21', $result['join_date']); + $this->assertEquals('2009-01-21', $result['start_date']); + $this->assertEquals('2009-12-21', $result['end_date']); + } + /** + * Test that if membership end date is not set it defaults to correct end date + * - rolling + */ + function testEmptyEndDateFixed() { + unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']); + $this->_params['membership_type_id'] = $this->_membershipTypeID2; + $result = $this->callAPISuccess($this->_entity, 'create', $this->_params); + $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); + $this->assertEquals('2009-01-21', $result['join_date']); + $this->assertEquals('2008-03-01', $result['start_date']); + $this->assertEquals('2010-02-28', $result['end_date']); + } + /** + * Test that if membership end date is not set it defaults to correct end date + * - rolling + */ + function testEmptyEndDateRolling() { + unset($this->_params['is_override'], $this->_params['end_date']); + $this->_params['membership_type_id'] = $this->_membershipTypeID; + $result = $this->callAPISuccess($this->_entity, 'create', $this->_params); + $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); + $this->assertEquals('2009-01-21', $result['join_date']); + $this->assertEquals('2009-01-21', $result['start_date']); + $this->assertEquals('2010-01-20', $result['end_date']); + } + + + /** + * Test that if datesdate are not set they not over-ridden if id is passed in + */ + function testMembershipDatesNotOverridden() { + $result = $this->callAPISuccess($this->_entity, 'create', $this->_params); + unset($this->_params['end_date'], $this->_params['start_date']); + $this->_params['id'] = $result['id']; + $this->callAPISuccess($this->_entity, 'create', $this->_params); + $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id'])); + $this->assertEquals('2009-01-21', $result['join_date']); + $this->assertEquals('2009-01-21', $result['start_date']); + $this->assertEquals('2009-12-21', $result['end_date']); + + } } -- 2.25.1