add missing comments - tests directory
[civicrm-core.git] / tests / phpunit / api / v3 / ProfileTest.php
index 145a00b48f603e27e1377f560c335377a9ae044e..1a1e6ecb1854ffcd8b980d76a3ecc56083e11cc2 100644 (file)
@@ -2,9 +2,9 @@
 
 /*
  +--------------------------------------------------------------------+
-| CiviCRM version 4.4                                                |
+| CiviCRM version 4.5                                                |
 +--------------------------------------------------------------------+
-| Copyright CiviCRM LLC (c) 2004-2013                                |
+| Copyright CiviCRM LLC (c) 2004-2014                                |
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
@@ -38,7 +38,13 @@ require_once 'tests/phpunit/CiviTest/CiviUnitTestCase.php';
  */
 class api_v3_ProfileTest extends CiviUnitTestCase {
   protected $_apiversion;
-  protected $_profileID;
+  protected $_profileID = 0;
+  protected $_membershipTypeID;
+  protected $_contactID;
+
+  /**
+   * @return array
+   */
   function get_info() {
     return array(
       'name' => 'Profile Test',
@@ -53,17 +59,22 @@ class api_v3_ProfileTest extends CiviUnitTestCase {
     $config = CRM_Core_Config::singleton();
     $config->countryLimit[1] = 1013;
     $config->stateLimit[1] = 1013;
+    $this->createLoggedInUser();
+    $this->_membershipTypeID = $this->membershipTypeCreate();
   }
 
   function tearDown() {
 
     $this->quickCleanup(array(
-        'civicrm_contact',
-        'civicrm_phone',
-        'civicrm_address',
-      ), TRUE);
+      '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 (25, 26)');
+    CRM_Core_DAO::executeQuery(" DELETE FROM civicrm_uf_group WHERE id IN ($this->_profileID, 26)");
   }
 
   ////////////// test $this->callAPISuccess3_profile_get //////////////////
@@ -99,13 +110,12 @@ class api_v3_ProfileTest extends CiviUnitTestCase {
     $expected          = current($pofileFieldValues);
     $contactId         = key($pofileFieldValues);
     $params            = array(
-      'profile_id' => 25,
+      'profile_id' => $this->_profileID,
       'contact_id' => $contactId,
     );
     $result = $this->callAPISuccess('profile', 'get', $params);
     foreach ($expected as $profileField => $value) {
-      $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$profileField}"
-      );
+      $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values']));
     }
   }
 
@@ -114,13 +124,13 @@ class api_v3_ProfileTest extends CiviUnitTestCase {
     $expected          = current($pofileFieldValues);
     $contactId         = key($pofileFieldValues);
     $params            = array(
-      'profile_id' => array(25, 1, 'Billing'),
+      'profile_id' => array($this->_profileID, 1, 'Billing'),
       'contact_id' => $contactId,
     );
 
     $result = $this->callAPIAndDocument('profile', 'get', $params, __FUNCTION__, __FILE__);
     foreach ($expected as $profileField => $value) {
-      $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values'][25]), " error message: " . "missing/mismatching value for {$profileField}");
+      $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values'][$this->_profileID]), " error message: " . "missing/mismatching value for {$profileField}");
     }
     $this->assertEquals('abc1', $result['values'][1]['first_name'], " error message: " . "missing/mismatching value for {$profileField}");
     $this->assertFalse(array_key_exists('email-Primary', $result['values'][1]), 'profile 1 doesn not include email');
@@ -132,7 +142,6 @@ class api_v3_ProfileTest extends CiviUnitTestCase {
       'billing_city-5' => 'Gotham City',
       'billing_state_province_id-5' => '1021',
       'billing_country_id-5' => '1228',
-      'billing-email-5' => 'abc1.xyz1@yahoo.com',
       'billing_postal_code-5' => '90210',
       'billing-email-5' => 'abc1.xyz1@yahoo.com',
       'email-5' => 'abc1.xyz1@yahoo.com',
@@ -147,12 +156,12 @@ class api_v3_ProfileTest extends CiviUnitTestCase {
       'street_address' => 'is billing st',
       'location_type_id' => 2,
       'contact_id' => $contactId,
-      ));
+    ));
 
     $expected = current($individual);
 
     $params = array(
-      'profile_id' => array(25, 1, 'Billing'),
+      'profile_id' => array($this->_profileID, 1, 'Billing'),
       'contact_id' => $contactId,
     );
 
@@ -181,7 +190,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase {
     $expected = current($individual);
 
     $params = array(
-      'profile_id' => array(25, 1, 'Billing'),
+      'profile_id' => array($this->_profileID, 1, 'Billing'),
       'contact_id' => $contactId,
     );
 
@@ -244,7 +253,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase {
 
     $params['activity_id'] = 100001;
     $result = $this->callAPIFailure('profile', 'get', $params,
-       'Invalid Activity Id (aid).');
+      'Invalid Activity Id (aid).');
   }
 
   /*
@@ -300,11 +309,11 @@ class api_v3_ProfileTest extends CiviUnitTestCase {
   function testGetFields() {
     $this->_createIndividualProfile();
     $this->_addCustomFieldToProfile($this->_profileID);
-    $result = $this->callAPIAndDocument('profile', 'getfields', array('action' => 'submit', 'profile_id' => 25), __FUNCTION__, __FILE__,
+    $result = $this->callAPIAndDocument('profile', 'getfields', array('action' => 'submit', 'profile_id' => $this->_profileID), __FUNCTION__, __FILE__,
       'demonstrates retrieving profile fields passing in an id');
     $this->assertArrayKeyExists('first_name', $result['values']);
     $this->assertEquals('2', $result['values']['first_name']['type']);
-    $this->assertEquals('Email', $result['values']['email']['title']);
+    $this->assertEquals('Email', $result['values']['email-primary']['title']);
     $this->assertEquals('civicrm_state_province', $result['values']['state_province-1']['pseudoconstant']['table']);
     $this->assertEquals('defaultValue', $result['values']['custom_1']['default_value']);
     $this->assertFalse(array_key_exists('participant_status', $result['values']));
@@ -318,8 +327,9 @@ class api_v3_ProfileTest extends CiviUnitTestCase {
       'profile_id' => 'participant_status',
       'get_options' => 'all')
     );
-    $this->assertTrue(array_key_exists('participant_status', $result['values']));
-    $this->assertEquals('Attended', $result['values']['participant_status']['options'][2]);
+    $this->assertTrue(array_key_exists('participant_status_id', $result['values']));
+    $this->assertEquals('Attended', $result['values']['participant_status_id']['options'][2]);
+    $this->assertEquals(array('participant_status'), $result['values']['participant_status_id']['api.aliases']);
   }
 
   /**
@@ -333,6 +343,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']);
   }
 
@@ -344,11 +357,11 @@ class api_v3_ProfileTest extends CiviUnitTestCase {
     $result = $this->callAPISuccess('uf_group', 'get', array('return' => 'id'));
     $profileIDs = array_keys($result['values']);
     foreach ($profileIDs as $profileID) {
-    $result = $this->callAPISuccess('profile', 'getfields', array(
-      'action' => 'submit',
-      'profile_id' => $profileID,
-      'get_options' => 'all')
-    );
+      $result = $this->callAPISuccess('profile', 'getfields', array(
+        'action' => 'submit',
+        'profile_id' => $profileID,
+        'get_options' => 'all')
+      );
     }
   }
   /////////////// test $this->callAPISuccess3_profile_set //////////////////
@@ -391,12 +404,12 @@ class api_v3_ProfileTest extends CiviUnitTestCase {
       'state_province-1' => '1000',
     );
 
-    $params = array_merge(array('profile_id' => 25, 'contact_id' => $contactId),
+    $params = array_merge(array('profile_id' => $this->_profileID, 'contact_id' => $contactId),
       $updateParams
     );
 
     $result = $this->callAPIFailure('profile', 'submit', $params,
-      'Missing required parameters for profile id 25: email-Primary'
+      "Missing required parameters for profile id $this->_profileID: email-Primary"
     );
   }
 
@@ -411,31 +424,82 @@ class api_v3_ProfileTest extends CiviUnitTestCase {
     $updateParams = array(
       'first_name' => 'abc2',
       'last_name' => 'xyz2',
-      'email-Primary' => 'abc2.xyz2@gmail.com',
+      'email-primary' => 'abc2.xyz2@gmail.com',
       'phone-1-1' => '022 321 826',
       'country-1' => '1013',
       'state_province-1' => '1000',
     );
 
     $params = array_merge(array(
-        'profile_id' => 25,
-        'contact_id' => $contactId,
-      ), $updateParams);
+      'profile_id' => $this->_profileID,
+      'contact_id' => $contactId,
+    ), $updateParams);
 
     $result = $this->callAPIAndDocument('profile', 'submit', $params, __FUNCTION__, __FILE__);
 
     $getParams = array(
-      'profile_id' => 25,
+      'profile_id' => $this->_profileID,
       'contact_id' => $contactId,
     );
     $profileDetails = $this->callAPISuccess('profile', 'get', $getParams);
 
     foreach ($updateParams as $profileField => $value) {
-      $this->assertEquals($value, CRM_Utils_Array::value($profileField, $profileDetails['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$profileField}"
+      $this->assertEquals($value, CRM_Utils_Array::value($profileField, $profileDetails['values']), "missing/mismatching value for {$profileField}"
       );
     }
+    unset($params['email-primary']);
+    $params['email-Primary'] = 'my@mail.com';
+    $this->callAPISuccess('profile', 'submit',  $params);
+    $profileDetails = $this->callAPISuccess('profile', 'get', $getParams);
+    $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_id']['options']));
+
+}
+
+  /**
+   * Test that the fields are returned in the right order despite the faffing around that goes on
+   */
+  function testMembershipGetFieldsOrder() {
+    $result = $this->callAPISuccess('profile', 'getfields', array('action' => 'submit', 'profile_id' => 'membership_batch_entry'));
+    $weight = 1;
+    foreach($result['values'] as $fieldName => $field) {
+      if($fieldName == 'profile_id') {
+        continue;
+      }
+      $this->assertEquals($field['weight'], $weight);
+      $weight++;
+    }
+  }
+  /**
+   * 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
    */
@@ -454,14 +518,14 @@ class api_v3_ProfileTest extends CiviUnitTestCase {
     );
 
     $params = array_merge(array(
-      'profile_id' => 25,
+      'profile_id' => $this->_profileID,
       'contact_id' => $contactId,
     ), $updateParams);
 
     $result = $this->callAPISuccess('profile', 'set', $params);
     $this->assertArrayKeyExists('values', $result);
     $getParams = array(
-      'profile_id' => 25,
+      'profile_id' => $this->_profileID,
       'contact_id' => $contactId,
     );
     $profileDetails = $this->callAPISuccess('profile', 'get', $getParams);
@@ -544,8 +608,8 @@ class api_v3_ProfileTest extends CiviUnitTestCase {
       'activity_status_id' => '2',
     );
     $profileParams = array_merge($params, $updateParams);
-    $profile       = $this->callAPISuccess('profile', 'submit', $profileParams);
-    $result        = $this->callAPISuccess('profile', 'get', $params);
+    $this->callAPISuccess('profile', 'submit', $profileParams);
+    $result = $this->callAPISuccess('profile', 'get', $params);
 
     foreach ($updateParams as $profileField => $value) {
       $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$profileField}"
@@ -584,7 +648,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase {
     $contactId = key($pofileFieldValues);
 
     $params = array(
-      'profile_id' => 25,
+      'profile_id' => $this->_profileID,
       'contact_id' => $contactId,
       'first_name' => 'abc2',
       'last_name' => 'xyz2',
@@ -640,6 +704,11 @@ class api_v3_ProfileTest extends CiviUnitTestCase {
   /*
      * Helper function to create an Individual with address/email/phone info. Import UF Group and UF Fields
      */
+  /**
+   * @param array $params
+   *
+   * @return mixed
+   */
   function _createIndividualContact($params = array()) {
     $contactParams = array_merge(array(
       'first_name' => 'abc1',
@@ -667,13 +736,13 @@ 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',
+      'email-primary' => 'abc1.xyz1@yahoo.com',
       'phone-1-1' => '021 512 755',
       'country-1' => '1228',
       'state_province-1' => '1021',
@@ -682,6 +751,9 @@ class api_v3_ProfileTest extends CiviUnitTestCase {
     return $profileData;
   }
 
+  /**
+   * @return array
+   */
   function _createContactWithActivity() {
     // @TODO: Create profile with custom fields
     $op = new PHPUnit_Extensions_Database_Operation_Insert();
@@ -740,7 +812,7 @@ class api_v3_ProfileTest extends CiviUnitTestCase {
       'profile_id' => 26,
       'contact_id' => $contactId,
       'activity_id' => $activityValues['id'],
-         );
+    );
 
     // expected result of above created profile
     $expected = array(
@@ -761,23 +833,77 @@ class api_v3_ProfileTest extends CiviUnitTestCase {
    * Create a profile
    */
   function _createIndividualProfile() {
-    // Create new profile having group_type: Contact,Individual
-    $op = new PHPUnit_Extensions_Database_Operation_Insert();
-    $op->execute($this->_dbconn,
-      new PHPUnit_Extensions_Database_DataSet_XMLDataSet(
-        dirname(__FILE__) . "/dataset/uf_group_25.xml"
-      )
-    );
-    // Create Contact + Idividual fields for profile
-    $op = new PHPUnit_Extensions_Database_Operation_Insert();
-    $op->execute($this->_dbconn,
-      new PHPUnit_Extensions_Database_DataSet_XMLDataSet(
-        dirname(__FILE__) . "/dataset/uf_field_uf_group_25.xml"
-      )
+
+    // creating these via the api as we want to utilise & test the flushing of caches when fields created
+    // via the api
+
+    $ufGroupParams = array(
+      'group_type' => 'Individual,Contact',// really we should remove this & test the ufField create sets it
+      'name' => 'test_individual_contact_profile',
+      'title' => 'Flat Coffee',
+      'api.uf_field.create' => array(
+        array(
+          'field_name' => 'first_name',
+          'is_required' => 1,
+          'visibility' => 'Public Pages and Listings',
+          'field_type' => 'Individual',
+          'label' => 'First Name',
+        ),
+        array(
+          'field_name' => 'last_name',
+          'is_required' => 1,
+          'visibility' => 'Public Pages and Listings',
+          'field_type' => 'Individual',
+          'label' => 'Last Name',
+        ),
+        array(
+          'field_name' => 'email',
+          'is_required' => 1,
+          'visibility' => 'Public Pages and Listings',
+          'field_type' => 'Contact',
+          'label' => 'Email',
+        ),
+        array(
+          'field_name' => 'phone',
+          'is_required' => 1,
+          'visibility' => 'Public Pages and Listings',
+          'field_type' => 'Contact',
+          'location_type_id' => 1,
+          'phone_type_id' => 1,
+          'label' => 'Phone'
+        ),
+        array(
+          'field_name' => 'country',
+          'is_required' => 1,
+          'visibility' => 'Public Pages and Listings',
+          'field_type' => 'Contact',
+          'location_type_id' => 1,
+          'label' => 'Country'
+        ),
+        array(
+          'field_name' => 'state_province',
+          'is_required' => 1,
+          'visibility' => 'Public Pages and Listings',
+          'field_type' => 'Contact',
+          'location_type_id' => 1,
+          'label' => 'State Province'
+        ),
+        array(
+          'field_name' => 'postal_code',
+          'is_required' => 0,
+          'field_type' => 'Contact',
+          'location_type_id' => 1,
+          'label' => 'State Province'
+         ),
+      ),
     );
-    $this->_profileID = 25;
+    $profile = $this->callAPISuccess('uf_group', 'create', $ufGroupParams);
+    $this->_profileID = $profile['id'];
   }
 
+  /**
+   * @param $profileID
+   */
   function _addCustomFieldToProfile($profileID) {
     $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, '');
     $this->uFFieldCreate(array('uf_group_id' => $profileID, 'field_name' => 'custom_' . $ids['custom_field_id'], 'contact_type' => 'Contact'));