From 7c3f2c0323df4212392e6d6a5ac4a9a9fb617336 Mon Sep 17 00:00:00 2001 From: Eileen Date: Wed, 25 Sep 2013 10:50:03 +1200 Subject: [PATCH] CRM-13234 fix profile api treatment of fields where DB doesn't match api name ---------------------------------------- * CRM-13234: facilitate data-entry-entities via api http://issues.civicrm.org/jira/browse/CRM-13234 --- api/v3/Profile.php | 34 ++++++++++++++++++++-------- tests/phpunit/api/v3/ProfileTest.php | 34 ++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/api/v3/Profile.php b/api/v3/Profile.php index 55824100e5..e869d38985 100644 --- a/api/v3/Profile.php +++ b/api/v3/Profile.php @@ -454,7 +454,9 @@ 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 + * @param $is_flush + * + * @internal param $params * * @return */ @@ -516,20 +518,31 @@ function _civicrm_api3_buildprofile_submitfields($profileID, $optionsBehaviour = $result = civicrm_api3($entity, 'getfields', array('action' => 'create')); $entityGetFieldsResult = _civicrm_api3_profile_appendaliases($result['values'], $entity); foreach ($entityFields as $entityfield => $realName) { - $profileFields[$profileID][strtolower($entityfield)] = array_merge($profileFields[$profileID][$entityfield], $entityGetFieldsResult[$realName]); + $fieldName = strtolower($entityfield); + if(!stristr('-', $fieldName) && realName != $fieldName) { + // we want to keep the '-' pattern for locations but otherwise + // we are going to make the api-standard field the main / preferred name but support the db name + // in future naming the fields in the DB to reflect the way the rest of the api / BAO / metadata works would + // reduce code + $fieldName = $realName; + } + $profileFields[$profileID][$fieldName] = array_merge($profileFields[$profileID][$entityfield], $entityGetFieldsResult[$realName]); if($optionsBehaviour && !empty($entityGetFieldsResult[$realName]['pseudoconstant'])) { if($optionsBehaviour > 1 || !in_array($realName, array('state_province_id', 'county_id', 'country_id'))) { $options = civicrm_api3($entity, 'getoptions', array('field' => $realName)); - $profileFields[$profileID][$entityfield]['options'] = $options['values']; + $profileFields[$profileID][$fieldName]['options'] = $options['values']; } } - if($entityfield != strtolower($entityfield)) { - // we will make the mixed case version (e.g. of 'Primary') an aliase - if(!isset($profileFields[$profileID][strtolower($entityfield)])) { - $profileFields[$profileID][strtolower($entityfield)]['api.aliases'] = array(); + if($entityfield != $fieldName) { + if(isset($profileFields[$profileID][$entityfield])) { + unset($profileFields[$profileID][$entityfield]); + } + // we will make the mixed case version (e.g. of 'Primary') an alias + if(!isset($profileFields[$profileID][$fieldName]['api.aliases'])) { + $profileFields[$profileID][$fieldName]['api.aliases'] = array(); } - $profileFields[$profileID][strtolower($entityfield)]['api.aliases'][] = $entityfield; + $profileFields[$profileID][$fieldName]['api.aliases'][] = $entityfield; } /** * putting this on hold -this would cause the api to set the default - but could have unexpected behaviour @@ -549,12 +562,13 @@ function _civicrm_api3_buildprofile_submitfields($profileID, $optionsBehaviour = * */ function _civicrm_api3_map_profile_fields_to_entity(&$field) { - $entity = _civicrm_api_get_entity_name_from_camel($field['field_type']); + $entity = $field['field_type']; $contactTypes = civicrm_api3('contact', 'getoptions', array('field' => 'contact_type')); - $locationFields = array('email' => 'email'); if(in_array($entity, $contactTypes['values'])) { $entity = 'contact'; } + $entity = _civicrm_api_get_entity_name_from_camel($entity); + $locationFields = array('email' => 'email'); $fieldName = $field['field_name']; if(!empty($field['location_type_id'])) { if($fieldName == 'email') { diff --git a/tests/phpunit/api/v3/ProfileTest.php b/tests/phpunit/api/v3/ProfileTest.php index ee58756525..9ac44e7f17 100644 --- a/tests/phpunit/api/v3/ProfileTest.php +++ b/tests/phpunit/api/v3/ProfileTest.php @@ -339,6 +339,9 @@ class api_v3_ProfileTest extends CiviUnitTestCase { 'get_options' => 'all') ); $this->assertTrue(array_key_exists('total_amount', $result['values'])); + $this->assertTrue(array_key_exists('financial_type_id', $result['values'])); + $this->assertEquals(array('contribution_type_id', 'contribution_type', 'financial_type'), $result['values']['financial_type_id']['api.aliases']); + $this->assertTrue(!array_key_exists('financial_type', $result['values'])); $this->assertEquals(12, $result['values']['receive_date']['type']); } @@ -447,6 +450,37 @@ class api_v3_ProfileTest extends CiviUnitTestCase { $this->assertEquals('my@mail.com', $profileDetails['values']['email-Primary']); } + /** + * Ensure caches are being cleared so we don't get into a debugging trap because of cached metadata + * First we delete & create to increment the version & then check for caching probs + */ + function testProfileSubmitCheckCaching() { + $this->callAPISuccess('membership_type', 'delete', array('id' => $this->_membershipTypeID)); + $this->_membershipTypeID = $this->membershipTypeCreate(); + + $membershipTypes = $this->callAPISuccess('membership_type', 'get', array()); + $profileFields = $this->callAPISuccess('profile', 'getfields', array('get_options' => 'all', 'action' => 'submit', 'profile_id' => 'membership_batch_entry')); + $getoptions = $this->callAPISuccess('membership', 'getoptions', array('field' => 'membership_type', 'context' => 'validate')); + $this->assertEquals(array_keys($membershipTypes['values']), array_keys($getoptions['values'])); + $this->assertEquals(array_keys($membershipTypes['values']), array_keys($profileFields['values']['membership_type']['options'])); + +} + /** + * Check we can submit membership batch profiles (create mode) + */ + function testProfileSubmitMembershipBatch() { + $this->_contactID = $this->individualCreate(); + $this->callAPISuccess('profile', 'submit', array( + 'profile_id' => 'membership_batch_entry', + 'financial_type_id' => 1, + 'membership_type' => $this->_membershipTypeID, + 'join_date' => 'now', + 'total_amount' => 10, + 'contribution_status_id' => 1, + 'receive_date' => 'now', + 'contact_id' => $this->_contactID, + )); + } /** * set is deprecated but we need to ensure it still works */ -- 2.25.1