From 174dbdd5a5f09823e8cfd6383d3ff4e7ba742f76 Mon Sep 17 00:00:00 2001 From: Eileen Date: Fri, 20 Sep 2013 00:45:22 +1200 Subject: [PATCH] profile api test fixes (caching & case on entities) --- api/v3/MembershipType.php | 13 +++-- api/v3/Profile.php | 72 ++++++++++++++++------------ tests/phpunit/api/v3/ProfileTest.php | 10 +++- 3 files changed, 56 insertions(+), 39 deletions(-) diff --git a/api/v3/MembershipType.php b/api/v3/MembershipType.php index 276a7991c3..2bffc3fe5d 100644 --- a/api/v3/MembershipType.php +++ b/api/v3/MembershipType.php @@ -47,17 +47,16 @@ * {getfields MembershipType_get} */ function civicrm_api3_membership_type_create($params) { - $values = $params; - civicrm_api3_verify_mandatory($values, 'CRM_Member_DAO_MembershipType'); + $ids['membershipType'] = CRM_Utils_Array::value('id', $params); + $ids['memberOfContact'] = CRM_Utils_Array::value('member_of_contact_id', $params); + $ids['contributionType'] = CRM_Utils_Array::value('financial_type_id', $params); - $ids['membershipType'] = CRM_Utils_Array::value('id', $values); - $ids['memberOfContact'] = CRM_Utils_Array::value('member_of_contact_id', $values); - $ids['contributionType'] = CRM_Utils_Array::value('financial_type_id', $values); - - $membershipTypeBAO = CRM_Member_BAO_MembershipType::add($values, $ids); + $membershipTypeBAO = CRM_Member_BAO_MembershipType::add($params, $ids); $membershipType = array(); _civicrm_api3_object_to_array($membershipTypeBAO, $membershipType[$membershipTypeBAO->id]); CRM_Member_PseudoConstant::membershipType(NULL, TRUE); + civicrm_api3('membership', 'getfields', array('cache_clear' => 1, 'fieldname' => 'membership_type_id')); + civicrm_api3('profile', 'getfields', array('action' => 'submit', 'cache_clear' => 1)); return civicrm_api3_create_success($membershipType, $params, 'membership_type', 'create', $membershipTypeBAO); } diff --git a/api/v3/Profile.php b/api/v3/Profile.php index d3c1d8faca..55824100e5 100644 --- a/api/v3/Profile.php +++ b/api/v3/Profile.php @@ -295,7 +295,10 @@ function _civicrm_api3_profile_submit_spec(&$params, $apirequest) { // we don't resolve state, country & county for performance reasons $resolveOptions = CRM_Utils_Array::value('get_options',$apirequest['params']) == 'all' ? True : False; $profileID = _civicrm_api3_profile_getProfileID($apirequest['params']['profile_id']); - $params = _civicrm_api3_buildprofile_submitfields($profileID, $resolveOptions); + $params = _civicrm_api3_buildprofile_submitfields($profileID, $resolveOptions, CRM_Utils_Array::value('cache_clear', $params)); + } + elseif (isset($apirequest['params']['cache_clear'])) { + _civicrm_api3_buildprofile_submitfields(FALSE, FALSE, True); } $params['profile_id']['api.required'] = TRUE; } @@ -451,10 +454,19 @@ function _civicrm_api3_profile_getbillingpseudoprofile(&$params) { * * @param integer $profileID * @param integer $optionsBehaviour 0 = don't resolve, 1 = resolve non-aggressively, 2 = resolve aggressively - ie include country & state + * @param $params + * + * @return */ -function _civicrm_api3_buildprofile_submitfields($profileID, $optionsBehaviour = 1) { +function _civicrm_api3_buildprofile_submitfields($profileID, $optionsBehaviour = 1, $is_flush) { static $profileFields = array(); + if($is_flush) { + $profileFields = array(); + if(empty($profileID)) { + return; + } + } if(isset($profileFields[$profileID])) { return $profileFields[$profileID]; } @@ -537,29 +549,29 @@ function _civicrm_api3_buildprofile_submitfields($profileID, $optionsBehaviour = * */ function _civicrm_api3_map_profile_fields_to_entity(&$field) { - $entity = $field['field_type']; + $entity = _civicrm_api_get_entity_name_from_camel($field['field_type']); $contactTypes = civicrm_api3('contact', 'getoptions', array('field' => 'contact_type')); - $locationFields = array('email' => 'Email'); + $locationFields = array('email' => 'email'); if(in_array($entity, $contactTypes['values'])) { - $entity = 'Contact'; + $entity = 'contact'; } $fieldName = $field['field_name']; if(!empty($field['location_type_id'])) { if($fieldName == 'email') { - $entity = 'Email'; + $entity = 'email'; } else{ - $entity = 'Address'; + $entity = 'address'; } $fieldName .= '-' . $field['location_type_id']; } elseif(array_key_exists($fieldName, $locationFields)) { $fieldName .= '-Primary'; - $entity = 'Email'; + $entity = 'email'; } if(!empty($field['phone_type_id'])) { $fieldName .= '-' . $field['location_type_id']; - $entity = 'Phone'; + $entity = 'phone'; } // @todo - sort this out! @@ -568,29 +580,29 @@ function _civicrm_api3_map_profile_fields_to_entity(&$field) { // in a perfect world the uf_field table would hold the correct entity for each item // & only the relationships between entities would need to be coded $hardCodedEntityMappings = array( - 'street_address' => 'Address', - 'street_number' => 'Address', - 'supplemental_address_1' => 'Address', - 'supplemental_address_2' => 'Address', - 'supplemental_address_3' => 'Address', - 'postal_code' => 'Address', - 'city' => 'Address', - 'email' => 'Email', - 'state_province' => 'Address', - 'country' => 'Address', - 'county' => 'Address', + 'street_address' => 'address', + 'street_number' => 'address', + 'supplemental_address_1' => 'address', + 'supplemental_address_2' => 'address', + 'supplemental_address_3' => 'address', + 'postal_code' => 'address', + 'city' => 'address', + 'email' => 'email', + 'state_province' => 'address', + 'country' => 'address', + 'county' => 'address', //note that in discussions about how to restructure the api we discussed making these membership // fields into 'membership_payment' fields - which would entail declaring them in getfields // & renaming them in existing profiles - 'financial_type' => 'Contribution', - 'total_amount' => 'Contribution', - 'receive_date' => 'Contribution', - 'payment_instrument' => 'Contribution', - 'check_number' => 'Contribution', - 'contribution_status_id' => 'Contribution', - 'soft_credit' => 'Contribution', - 'group' => 'GroupContact', - 'tag' => 'EntityTag', + 'financial_type' => 'contribution', + 'total_amount' => 'contribution', + 'receive_date' => 'contribution', + 'payment_instrument' => 'contribution', + 'check_number' => 'contribution', + 'contribution_status_id' => 'contribution', + 'soft_credit' => 'contribution', + 'group' => 'group_contact', + 'tag' => 'entity_tag', ); if(array_key_exists($fieldName, $hardCodedEntityMappings)) { $entity = $hardCodedEntityMappings[$fieldName]; @@ -633,7 +645,7 @@ function _civicrm_api3_profile_appendaliases($values, $entity) { } } //special case on membership & contribution - can't see how to handle in a generic way - if(in_array($entity, array('Membership', 'Contribution'))) { + if(in_array($entity, array('membership', 'contribution'))) { $values['send_receipt'] = array('title' => 'Send Receipt', 'type' => (int) 16); } return $values; diff --git a/tests/phpunit/api/v3/ProfileTest.php b/tests/phpunit/api/v3/ProfileTest.php index 2acd16cfef..ee58756525 100644 --- a/tests/phpunit/api/v3/ProfileTest.php +++ b/tests/phpunit/api/v3/ProfileTest.php @@ -39,6 +39,8 @@ require_once 'tests/phpunit/CiviTest/CiviUnitTestCase.php'; class api_v3_ProfileTest extends CiviUnitTestCase { protected $_apiversion; protected $_profileID = 0; + protected $_membershipTypeID; + protected $_contactID; function get_info() { return array( 'name' => 'Profile Test', @@ -54,6 +56,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase { $config->countryLimit[1] = 1013; $config->stateLimit[1] = 1013; $this->createLoggedInUser(); + $this->_membershipTypeID = $this->membershipTypeCreate(); } function tearDown() { @@ -62,7 +65,10 @@ class api_v3_ProfileTest extends CiviUnitTestCase { 'civicrm_contact', 'civicrm_phone', 'civicrm_address', + 'civicrm_membership', + 'civicrm_contribution', ), TRUE); + $this->callAPISuccess('membership_type', 'delete', array('id' => $this->_membershipTypeID)); // ok can't be bothered wring an api to do this & truncating is crazy CRM_Core_DAO::executeQuery(" DELETE FROM civicrm_uf_group WHERE id IN ($this->_profileID, 26)"); } @@ -672,10 +678,10 @@ class api_v3_ProfileTest extends CiviUnitTestCase { ), $params ); - $contactID = $this->individualCreate($contactParams); + $this->_contactID = $this->individualCreate($contactParams); $this->_createIndividualProfile(); // expected result of above created profile with contact Id $contactId - $profileData[$contactID] = array( + $profileData[$this->_contactID] = array( 'first_name' => 'abc1', 'last_name' => 'xyz1', 'email-primary' => 'abc1.xyz1@yahoo.com', -- 2.25.1