Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2014-10-02-07-53-58
[civicrm-core.git] / tests / phpunit / CiviTest / CiviUnitTestCase.php
index e6ff0d9d6bc091f76e224411bec057b8cd4b8675..cd87688d6a22afdd61eadb7ea6265eff50334890 100644 (file)
@@ -39,11 +39,6 @@ require_once CIVICRM_SETTINGS_PATH;
 /**
  *  Include class definitions
  */
-require_once 'PHPUnit/Extensions/Database/TestCase.php';
-require_once 'PHPUnit/Framework/TestResult.php';
-require_once 'PHPUnit/Extensions/Database/DataSet/FlatXmlDataSet.php';
-require_once 'PHPUnit/Extensions/Database/DataSet/XmlDataSet.php';
-require_once 'PHPUnit/Extensions/Database/DataSet/QueryDataSet.php';
 require_once 'tests/phpunit/Utils.php';
 require_once 'api/api.php';
 require_once 'CRM/Financial/BAO/FinancialType.php';
@@ -58,7 +53,7 @@ define('API_LATEST_VERSION', 3);
 class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
 
   /**
-   * api version - easier to override than just a defin
+   * api version - easier to override than just a define
    */
   protected $_apiversion = API_LATEST_VERSION;
   /**
@@ -309,6 +304,10 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
     // Rebuild triggers
     civicrm_api('system', 'flush', array('version' => 3, 'triggers' => 1));
 
+    CRM_Core_BAO_ConfigSetting::setEnabledComponents(array(
+      'CiviEvent', 'CiviContribute', 'CiviMember', 'CiviMail', 'CiviReport', 'CiviPledge'
+    ));
+
     return TRUE;
   }
 
@@ -391,7 +390,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
     $xmlFiles = glob($fixturesDir . '/*.xml');
     foreach ($xmlFiles as $xmlFixture) {
       $op = new PHPUnit_Extensions_Database_Operation_Insert();
-      $dataset = new PHPUnit_Extensions_Database_DataSet_XMLDataSet($xmlFixture);
+      $dataset = $this->createXMLDataSet($xmlFixture);
       $this->_tablesToTruncate = array_merge($this->_tablesToTruncate, $dataset->getTableNames());
       $op->execute($this->_dbconn, $dataset);
     }
@@ -438,6 +437,8 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
    */
   protected function tearDown() {
     error_reporting(E_ALL & ~E_NOTICE);
+    $session = CRM_Core_Session::singleton();
+    $session->set('userID', NULL);
     $tablesToTruncate = array('civicrm_contact');
     $this->quickCleanup($tablesToTruncate);
     $this->cleanTempDirs();
@@ -988,38 +989,28 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
    * Generic function to create Organisation, to be used in test cases
    *
    * @param array   parameters for civicrm_contact_add api function call
+   * @param int     sequence number if creating multiple organizations
    *
    * @return int    id of Organisation created
    */
-  function organizationCreate($params = array()) {
+  function organizationCreate($params = array(), $seq = 0) {
     if (!$params) {
       $params = array();
     }
-    $orgParams = array(
-      'organization_name' => 'Unit Test Organization',
-      'contact_type' => 'Organization',
-    );
-    return $this->_contactCreate(array_merge($orgParams, $params));
+    $params = array_merge($this->sampleContact('Organization', $seq), $params);
+    return $this->_contactCreate($params);
   }
 
   /**
    * Generic function to create Individual, to be used in test cases
    *
    * @param array   parameters for civicrm_contact_add api function call
+   * @param int     sequence number if creating multiple individuals
    *
    * @return int    id of Individual created
    */
-  function individualCreate($params = array()) {
-    $params = array_merge(array(
-        'first_name' => 'Anthony',
-        'middle_name' => 'J.',
-        'last_name' => 'Anderson',
-        'prefix_id' => 3,
-        'suffix_id' => 3,
-        'email' => 'anthony_anderson@civicrm.org',
-        'contact_type' => 'Individual',
-      ), $params);
-
+  function individualCreate($params = array(), $seq = 0) {
+    $params = array_merge($this->sampleContact('Individual', $seq), $params);
     return $this->_contactCreate($params);
   }
 
@@ -1027,17 +1018,52 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
    * Generic function to create Household, to be used in test cases
    *
    * @param array   parameters for civicrm_contact_add api function call
+   * @param int     sequence number if creating multiple households
    *
    * @return int    id of Household created
    */
-  function householdCreate($params =  array()) {
-    $params = array_merge(array(
-        'household_name' => 'Unit Test household',
-        'contact_type' => 'Household',
-      ), $params);
+  function householdCreate($params = array(), $seq = 0) {
+    $params = array_merge($this->sampleContact('Household', $seq), $params);
     return $this->_contactCreate($params);
   }
 
+  /**
+   * Helper function for getting sample contact properties
+   *
+   * @param enum     contact type: Individual, Organization
+   * @param int      sequence number for the values of this type
+   *
+   * @return array   properties of sample contact (ie. $params for API call)
+   */
+  function sampleContact($contact_type, $seq = 0) {
+    $samples = array(
+      'Individual' => array(
+        // The number of values in each list need to be coprime numbers to not have duplicates
+        'first_name' => array('Anthony', 'Joe', 'Terrence', 'Lucie', 'Albert', 'Bill', 'Kim'),
+        'middle_name' => array('J.', 'M.', 'P', 'L.', 'K.', 'A.', 'B.', 'C.', 'D', 'E.', 'Z.'),
+        'last_name' => array('Anderson', 'Miller', 'Smith', 'Collins', 'Peterson'),
+      ),
+      'Organization' => array(
+        'organization_name' => array('Unit Test Organization', 'Acme', 'Roberts and Sons', 'Cryo Space Labs', 'Sharper Pens'),
+      ),
+      'Household' => array(
+        'household_name' => array('Unit Test household'),
+      ),
+    );
+    $params = array('contact_type' => $contact_type);
+    foreach ($samples[$contact_type] as $key => $values) {
+      $params[$key] = $values[$seq % sizeof($values)];
+    }
+    if ($contact_type == 'Individual' ) {
+        $params['email'] = strtolower(
+          $params['first_name'] . '_' . $params['last_name'] . '@civicrm.org'
+        );
+        $params['prefix_id'] = 3;
+        $params['suffix_id'] = 3;
+    }
+    return $params;
+  }
+
   /**
    * Private helper function for calling civicrm_contact_add
    *
@@ -2050,7 +2076,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
    * @param string $action - optional action - otherwise taken from function name
    */
   function documentMe($params, $result, $function, $filename, $description = "", $subfile = NULL, $action = NULL) {
-    if (defined('DONT_DOCUMENT_TEST_CONFIG')) {
+    if (defined('DONT_DOCUMENT_TEST_CONFIG') && DONT_DOCUMENT_TEST_CONFIG) {
       return;
     }
     $entity = substr(basename($filename), 0, strlen(basename($filename)) - 8);
@@ -2351,8 +2377,18 @@ AND    ( TABLE_NAME LIKE 'civicrm_value_%' )
       'civicrm_participant',
       'civicrm_participant_payment',
       'civicrm_pledge',
+      'civicrm_price_set_entity',
+      'civicrm_price_field_value',
+      'civicrm_price_field',
     );
     $this->quickCleanup($tablesToTruncate);
+    CRM_Core_DAO::executeQuery("DELETE FROM civicrm_membership_status WHERE name NOT IN('New', 'Current', 'Grace', 'Expired', 'Pending', 'Cancelled', 'Deceased')");
+    $this->restoreDefaultPriceSetConfig();
+  }
+
+  function restoreDefaultPriceSetConfig() {
+    CRM_Core_DAO::executeQuery("INSERT INTO `civicrm_price_field` (`id`, `price_set_id`, `name`, `label`, `html_type`, `is_enter_qty`, `help_pre`, `help_post`, `weight`, `is_display_amounts`, `options_per_line`, `is_active`, `is_required`, `active_on`, `expire_on`, `javascript`, `visibility_id`) VALUES (1, 1, 'contribution_amount', 'Contribution Amount', 'Text', 0, NULL, NULL, 1, 1, 1, 1, 1, NULL, NULL, NULL, 1)");
+    CRM_Core_DAO::executeQuery("INSERT INTO `civicrm_price_field_value` (`id`, `price_field_id`, `name`, `label`, `description`, `amount`, `count`, `max_value`, `weight`, `membership_type_id`, `membership_num_terms`, `is_default`, `is_active`, `financial_type_id`, `deductible_amount`) VALUES (1, 1, 'contribution_amount', 'Contribution Amount', NULL, '1', NULL, NULL, 1, NULL, NULL, 0, 1, 1, 0.00)");
   }
   /*
    * Function does a 'Get' on the entity & compares the fields in the Params with those returned
@@ -2770,9 +2806,9 @@ AND    ( TABLE_NAME LIKE 'civicrm_value_%' )
 
   /**
    * Set up an acl allowing contact to see 2 specified groups
-   *  - $this->_permissionedGroup & $this->_permissionedDisbaledGroup
+   *  - $this->_permissionedGroup & $this->_permissionedDisabledGroup
    *
-   *  You need to have precreated these groups & created the user e.g
+   *  You need to have pre-created these groups & created the user e.g
    *  $this->createLoggedInUser();
    *   $this->_permissionedDisabledGroup = $this->groupCreate(array('title' => 'pick-me-disabled', 'is_active' => 0, 'name' => 'pick-me-disabled'));
    *   $this->_permissionedGroup = $this->groupCreate(array('title' => 'pick-me-active', 'is_active' => 1, 'name' => 'pick-me-active'));
@@ -2781,6 +2817,7 @@ AND    ( TABLE_NAME LIKE 'civicrm_value_%' )
   function setupACL() {
     global $_REQUEST;
     $_REQUEST = $this->_params;
+
     CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM');
     $optionGroupID = $this->callAPISuccessGetValue('option_group', array('return' => 'id', 'name' => 'acl_role'));
     $optionValue = $this->callAPISuccess('option_value', 'create', array('option_group_id' => $optionGroupID,
@@ -2852,7 +2889,6 @@ AND    ( TABLE_NAME LIKE 'civicrm_value_%' )
     $this->callAPISuccess('price_field_value', 'create', $priceFieldValue);
     $this->callAPISuccess('price_field_value', 'create', $priceFieldValue);
     $this->callAPISuccess('price_field_value', 'create', array_merge($priceFieldValue, array('price_field_id' => $newPriceField['id'])));
-
   }
 
 /**