From 37eda84b1d0cbbd463b5e07f96f1c33dea8a0e6c Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 30 Aug 2013 18:20:07 +1200 Subject: [PATCH] CRM-13296 membership api enotice fix & tests --- api/v3/Membership.php | 68 ++++++++++++------- .../examples/Membership/filterIsCurrent.php | 10 +-- api/v3/examples/MembershipCreate.php | 14 ++-- api/v3/examples/MembershipGet.php | 8 +-- api/v3/examples/MembershipUpdate.php | 14 ++-- tests/phpunit/api/v3/MembershipTest.php | 10 +++ 6 files changed, 75 insertions(+), 49 deletions(-) diff --git a/api/v3/Membership.php b/api/v3/Membership.php index 3d4d54140b..d00ca9fb3d 100644 --- a/api/v3/Membership.php +++ b/api/v3/Membership.php @@ -199,11 +199,49 @@ function civicrm_api3_membership_get($params) { $membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE); } - if (empty($membershipValues)) { - // No results is NOT an error! - return civicrm_api3_create_success($membershipValues, $params); + $options = _civicrm_api3_get_options_from_params($params, TRUE,'membership','get'); + $return = $options['return']; + if(empty($membershipValues) || + (!empty($return) + && !array_key_exists('related_contact_id', $return) + && !array_key_exists('relationship_name', $return) + ) + ) { + return civicrm_api3_create_success($membershipValues, $params, 'membership', 'get'); } + $members = _civicrm_api3_membership_relationsship_get_customv2behaviour($params, $membershipValues, $contactID ); + return civicrm_api3_create_success($members, $params, 'membership', 'get'); +} + +/** + * When we copied apiv3 from api v2 we brought across some custom behaviours - in the case of + * membership a complicated return array is constructed. The original + * behaviour made contact_id a required field. We still need to keep this for v3 when contact_id + * is passed in as part of the reasonable expectation developers have that we will keep the api + * as stable as possible + * + * @param array $params parameters passed into get function + * @return array result for calling function + */ +function _civicrm_api3_membership_get_customv2behaviour(&$params, $membershipTypeId, $activeOnly) { + // get the membership for the given contact ID + $membershipParams = array('contact_id' => $params['contact_id']); + if ($membershipTypeId) { + $membershipParams['membership_type_id'] = $membershipTypeId; + } + $membershipValues = array(); + CRM_Member_BAO_Membership::getValues($membershipParams, $membershipValues, $activeOnly); + return $membershipValues; +} + + +/** + * non-standard behaviour inherited from v2 +* @param array $params parameters passed into get function +* @return array result for calling function +*/ +function _civicrm_api3_membership_relationsship_get_customv2behaviour(&$params, $membershipValues, $contactID) { $relationships = array(); foreach ($membershipValues as $membershipId => $values) { // populate the membership type name for the membership type id @@ -246,27 +284,5 @@ function civicrm_api3_membership_get($params) { } } - - return civicrm_api3_create_success($members, $params, 'membership', 'get'); -} - -/** - * When we copied apiv3 from api v2 we brought across some custom behaviours - in the case of - * membership a complicated return array is constructed. The original - * behaviour made contact_id a required field. We still need to keep this for v3 when contact_id - * is passed in as part of the reasonable expectation developers have that we will keep the api - * as stable as possible - * - * @param array $params parameters passed into get function - * @return array result for calling function - */ -function _civicrm_api3_membership_get_customv2behaviour(&$params, $membershipTypeId, $activeOnly) { - // get the membership for the given contact ID - $membershipParams = array('contact_id' => $params['contact_id']); - if ($membershipTypeId) { - $membershipParams['membership_type_id'] = $membershipTypeId; - } - $membershipValues = array(); - CRM_Member_BAO_Membership::getValues($membershipParams, $membershipValues, $activeOnly); - return $membershipValues; + return $members; } diff --git a/api/v3/examples/Membership/filterIsCurrent.php b/api/v3/examples/Membership/filterIsCurrent.php index b5fab187f9..e014948de6 100644 --- a/api/v3/examples/Membership/filterIsCurrent.php +++ b/api/v3/examples/Membership/filterIsCurrent.php @@ -5,7 +5,7 @@ */ function membership_get_example(){ $params = array( - 'contact_id' => 13, + 'contact_id' => 17, 'filters' => array( 'is_current' => 1, ), @@ -39,9 +39,9 @@ function membership_get_expectedresult(){ '1' => array( 'id' => '1', 'membership_id' => '1', - 'contact_id' => '13', - 'membership_contact_id' => '13', - 'membership_type_id' => '21', + 'contact_id' => '17', + 'membership_contact_id' => '17', + 'membership_type_id' => '25', 'join_date' => '2009-01-21', 'start_date' => '2013-07-29 00:00:00', 'membership_start_date' => '2009-01-21', @@ -49,7 +49,7 @@ function membership_get_expectedresult(){ 'membership_end_date' => '2009-12-21', 'source' => 'Payment', 'membership_source' => 'Payment', - 'status_id' => '18', + 'status_id' => '20', 'is_override' => '1', 'is_test' => 0, 'member_is_test' => 0, diff --git a/api/v3/examples/MembershipCreate.php b/api/v3/examples/MembershipCreate.php index fe7eb3a610..6ab9557be8 100644 --- a/api/v3/examples/MembershipCreate.php +++ b/api/v3/examples/MembershipCreate.php @@ -5,14 +5,14 @@ */ function membership_create_example(){ $params = array( - 'contact_id' => 25, - 'membership_type_id' => 42, + 'contact_id' => 30, + 'membership_type_id' => 48, 'join_date' => '2009-01-21', 'start_date' => '2009-01-21', 'end_date' => '2009-12-21', 'source' => 'Payment', 'is_override' => 1, - 'status_id' => 28, + 'status_id' => 31, 'custom_1' => 'custom string', ); @@ -43,17 +43,17 @@ function membership_create_expectedresult(){ 'values' => array( '1' => array( 'id' => '1', - 'contact_id' => '25', - 'membership_type_id' => '42', + 'contact_id' => '30', + 'membership_type_id' => '48', 'join_date' => '20090121000000', 'start_date' => '2013-07-29 00:00:00', 'end_date' => '2013-08-04 00:00:00', 'source' => 'Payment', - 'status_id' => '28', + 'status_id' => '31', 'is_override' => '1', 'owner_membership_id' => '', 'max_related' => '', - 'is_test' => '', + 'is_test' => 0, 'is_pay_later' => '', 'contribution_recur_id' => '', 'campaign_id' => '', diff --git a/api/v3/examples/MembershipGet.php b/api/v3/examples/MembershipGet.php index 3094dfb50d..315ff70e93 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' => 15, + 'membership_type_id' => 19, ); try{ @@ -35,13 +35,13 @@ function membership_get_expectedresult(){ 'values' => array( '1' => array( 'id' => '1', - 'contact_id' => '10', - 'membership_type_id' => '15', + 'contact_id' => '14', + 'membership_type_id' => '19', 'join_date' => '2009-01-21', 'start_date' => '2013-07-29 00:00:00', 'end_date' => '2013-08-04 00:00:00', 'source' => 'Payment', - 'status_id' => '15', + 'status_id' => '17', 'is_override' => '1', 'is_test' => 0, 'is_pay_later' => 0, diff --git a/api/v3/examples/MembershipUpdate.php b/api/v3/examples/MembershipUpdate.php index ea36ab409f..dbe01b116f 100644 --- a/api/v3/examples/MembershipUpdate.php +++ b/api/v3/examples/MembershipUpdate.php @@ -5,14 +5,14 @@ */ function membership_update_example(){ $params = array( - 'contact_id' => 31, - 'membership_type_id' => 54, + 'contact_id' => 36, + 'membership_type_id' => 60, 'join_date' => '2009-01-21', 'start_date' => '2009-01-21', 'end_date' => '2009-12-21', 'source' => 'Payment', 'is_override' => 1, - 'status_id' => 34, + 'status_id' => 37, 'custom_1' => 'custom string', ); @@ -43,17 +43,17 @@ function membership_update_expectedresult(){ 'values' => array( '1' => array( 'id' => '1', - 'contact_id' => '31', - 'membership_type_id' => '54', + 'contact_id' => '36', + 'membership_type_id' => '60', 'join_date' => '20090121000000', 'start_date' => '2013-07-29 00:00:00', 'end_date' => '2013-08-04 00:00:00', 'source' => 'Payment', - 'status_id' => '34', + 'status_id' => '37', 'is_override' => '1', 'owner_membership_id' => '', 'max_related' => '', - 'is_test' => '', + 'is_test' => 0, 'is_pay_later' => '', 'contribution_recur_id' => '', 'campaign_id' => '', diff --git a/tests/phpunit/api/v3/MembershipTest.php b/tests/phpunit/api/v3/MembershipTest.php index 0137833ba7..0e00afbd87 100644 --- a/tests/phpunit/api/v3/MembershipTest.php +++ b/tests/phpunit/api/v3/MembershipTest.php @@ -413,6 +413,16 @@ class api_v3_MembershipTest extends CiviUnitTestCase { $this->contactDelete($memberContactId); } + /** + * We are checking for no enotices + only id & end_date returned + */ + function testMembershipGetWithReturn() { + $membershipID = $this->contactMembershipCreate($this->_params); + $result = $this->callAPISuccess('membership', 'get', array('return' => 'end_date')); + foreach ($result['values'] as $membership) { + $this->assertEquals(array('id', 'end_date'), array_keys($membership)); + } + } ///////////////// civicrm_membership_create methods /** -- 2.25.1