profile api test fixes (caching & case on entities)
authorEileen <eileen@fuzion.co.nz>
Thu, 19 Sep 2013 12:45:22 +0000 (00:45 +1200)
committerEileen <eileen@fuzion.co.nz>
Tue, 24 Sep 2013 22:41:34 +0000 (10:41 +1200)
api/v3/MembershipType.php
api/v3/Profile.php
tests/phpunit/api/v3/ProfileTest.php

index 276a7991c34c7889a564d6d25876e2f4f4534bcc..2bffc3fe5d2d38f72fd3aabdb76bddb443fd7a43 100644 (file)
  * {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);
 }
 
index d3c1d8faca2d1fe51f5a872b1f4f0bf9000c5ed5..55824100e5b560c52a198034acc31f5c727d5614 100644 (file)
@@ -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;
index 2acd16cfef8ef19825b57d7ea9778e3da5df57d1..ee587565257f9ef7d47567aac3042c8566a6ee19 100644 (file)
@@ -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',