CRM-14793 add test for incorrect behaviour
authorEileen McNaughton <eileen@fuzion.co.nz>
Wed, 4 Jun 2014 09:19:32 +0000 (21:19 +1200)
committerEileen McNaughton <eileen@fuzion.co.nz>
Wed, 4 Jun 2014 09:19:32 +0000 (21:19 +1200)
CRM/Batch/Form/Entry.php
tests/phpunit/CRM/Batch/Form/EntryTest.php
tests/phpunit/CiviTest/CiviUnitTestCase.php

index b7ba4558003421e20572f324e6597d0f51015958..0f2ba25ef7d04c341fd46b2dc1fe507f1b5055cd 100644 (file)
@@ -759,14 +759,31 @@ class CRM_Batch_Form_Entry extends CRM_Core_Form {
       $value['contact_id']
     );
   }
+
   /**
    * Function exists purely for unit testing purposes. If you feel tempted to use this in live code
    * then it probably means there is some functionality that needs to be moved
    * out of the form layer
+   *
    * @param unknown_type $params
+   *
+   * @return bool
    */
   function testProcessMembership($params) {
     return $this->processMembership($params);
   }
+
+  /**
+   * Function exists purely for unit testing purposes. If you feel tempted to use this in live code
+   * then it probably means there is some functionality that needs to be moved
+   * out of the form layer
+   *
+   * @param array $params
+   *
+   * @return bool
+   */
+  function testProcessContribution($params) {
+    return $this->processContribution($params);
+  }
 }
 
index c15f19d6cd3064ebd914fa01ed89fa816191a739..dbe88accb2243130df1701ef6636889fbb24b067 100644 (file)
@@ -107,12 +107,13 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase {
       'visibility' => 'Public',
       'is_active' => 1,
       'fixed_period_start_day' => 101,
-      'fixed_period_rollover_day' => 1231
+      'fixed_period_rollover_day' => 1231,
+      'domain_id' => CRM_Core_Config::domainID(),
     );
-    $membershipType = CRM_Member_BAO_MembershipType::add($params, $ids);
-    $this->_membershipTypeID = $membershipType->id;
+    $membershipType = $this->callAPISuccess('membership_type', 'create', $params);
+    $this->_membershipTypeID = $membershipType['id'];
 
-    $this->_mebershipStatusID = $this->membershipStatusCreate('test status');
+    $this->_membershipStatusID = $this->membershipStatusCreate('test status');
     $this->_contactID = $this->individualCreate();
     $contact2Params = array(
       'first_name' => 'Anthonita',
@@ -129,6 +130,7 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase {
 
     $session = CRM_Core_Session::singleton();
     $session->set('dateTypes', 1);
+    $this->_sethtmlGlobals();
 
   }
 
@@ -137,12 +139,14 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase {
    * This method is called after a test is executed.
    *
    */
-  function tearDown() {
-    $tablesToTruncate = array('civicrm_membership', 'civicrm_membership_log', 'civicrm_contribution', 'civicrm_membership_payment');
-    $this->quickCleanup($tablesToTruncate);
+  function tearDown()
+  {
+    $this->quickCleanUpFinancialEntities();
     $this->relationshipTypeDelete($this->_relationshipTypeId);
-    $this->membershipTypeDelete(array('id' => $this->_membershipTypeID));
-    $this->membershipStatusDelete($this->_mebershipStatusID);
+    if ($this->callAPISuccessGetCount('membership', array('id' => $this->_membershipTypeID))) {
+      $this->membershipTypeDelete(array('id' => $this->_membershipTypeID));
+    }
+    $this->membershipStatusDelete($this->_membershipStatusID);
     $this->contactDelete($this->_contactID);
     $this->contactDelete($this->_contactID2);
     $this->contactDelete($this->_orgContactID);
@@ -157,7 +161,6 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase {
     $this->assertTrue($form->testProcessMembership($params));
     $result = $this->callAPISuccess('membership', 'get', array());
     $this->assertEquals(3, $result['count']);
-
     //check start dates #1 should default to 1 Jan this year, #2 should be as entered
     $this->assertEquals(date('Y-m-d', strtotime('first day of January 2013')), $result['values'][1]['start_date']);
     $this->assertEquals('2013-02-03', $result['values'][2]['start_date']);
@@ -171,22 +174,41 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase {
     $this->assertEquals(date('Y-m-d', strtotime('07/22/2013')), $result['values'][1]['join_date']);
     $this->assertEquals(date('Y-m-d', strtotime('07/03/2013')), $result['values'][2]['join_date']);
     $this->assertEquals(date('Y-m-d', strtotime('now')), $result['values'][3]['join_date']);
+    $result = $this->callAPISuccess('contribution', 'get', array('return' => 'total_amount'));
+    $this->assertEquals(3, $result['count']);
+    foreach($result['values'] as $contribution) {
+      $this-> assertEquals($this->callAPISuccess('line_item', 'getvalue', array(
+        'contribution_id' => $contribution['id'],
+        'return' => 'line_total',
 
+      )), $contribution['total_amount']);
+    }
   }
 
-  /*
-   * data provider for test process membership
+  /**
+   *  Test Contribution Import
    */
+  function testProcessContribution() {
+    $this->offsetDefaultPriceSet();
+    $form = new CRM_Batch_Form_Entry();
+    $params = $this->getContributionData();
+    $this->assertTrue($form->testProcessContribution($params));
+    $result = $this->callAPISuccess('contribution', 'get', array('return' => 'total_amount'));
+    $this->assertEquals(2, $result['count']);
+    foreach($result['values'] as $contribution) {
+     $this-> assertEquals($this->callAPISuccess('line_item', 'getvalue', array(
+       'contribution_id' => $contribution['id'],
+       'return' => 'line_total',
+
+     )), $contribution['total_amount']);
+    }
+  }
   /**
+   * data provider for test process membership
    * @return array
    */
   function getMembershipData() {
 
-    /*
-     Array (
-
-    );
-    */
     return array(
       'batch_id' => 4,
       'primary_profiles' => array(1 => NULL, 2 => NULL, 3 => NULL),
@@ -245,5 +267,42 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase {
 
     );
   }
+
+  /**
+   * @return array
+   */
+  function getContributionData() {
+    return array(
+      //'batch_id' => 4,
+      'primary_profiles' => array(1 => NULL, 2 => NULL, 3 => NULL),
+      'primary_contact_id' => Array (
+        1 => $this->_contactID,
+        2 => $this->_contactID2,
+        3 => $this->_contactID3,
+      ),
+      'field' => array(
+        1 => array(
+          'financial_type' => 1,
+          'total_amount' => 15,
+          'receive_date' => '07/24/2013',
+          'receive_date_time' => NULL,
+          'payment_instrument' => 1,
+          'check_number' => NULL,
+          'contribution_status_id' => 1,
+        ),
+        2 => array(
+          'financial_type' => 1,
+          'total_amount' => 15,
+          'receive_date' => '07/24/2013',
+          'receive_date_time' => NULL,
+          'payment_instrument' => 1,
+          'check_number' => NULL,
+          'contribution_status_id' => 1,
+        ),
+      ),
+      'actualBatchTotal' => 30,
+
+    );
+  }
 }
 
index 912908c33dd6b01cffb91c6a4023b0db77938b69..3fc0dcc5531cb0c33e7a3f3d7e2be35cedac8353 100644 (file)
@@ -1150,8 +1150,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
    * @internal param int $membershipTypeID
    */
   function membershipTypeDelete($params) {
-    $result = $this->callAPISuccess('MembershipType', 'Delete', $params);
-    return;
+    $this->callAPISuccess('MembershipType', 'Delete', $params);
   }
 
   /**
@@ -2345,6 +2344,7 @@ AND    ( TABLE_NAME LIKE 'civicrm_value_%' )
       'civicrm_membership',
       'civicrm_membership_type',
       'civicrm_membership_payment',
+      'civicrm_membership_log',
       'civicrm_membership_status',
       'civicrm_event',
       'civicrm_participant',
@@ -2831,6 +2831,29 @@ AND    ( TABLE_NAME LIKE 'civicrm_value_%' )
     CRM_ACL_API::groupPermission('whatever', 9999, NULL, 'civicrm_saved_search', NULL, NULL, TRUE);
   }
 
+  /**
+   * alter default price set so that the field numbers are not all 1 (hiding errors)
+   */
+  function offsetDefaultPriceSet() {
+    $contributionPriceSet = $this->callAPISuccess('price_set', 'getsingle', array('name' => 'default_contribution_amount'));
+    $firstID = $contributionPriceSet['id'];
+    $this->callAPISuccess('price_set', 'create', array('id' => $contributionPriceSet['id'], 'is_active' => 0, 'name' => 'old'));
+    unset($contributionPriceSet['id']);
+    $newPriceSet = $this->callAPISuccess('price_set', 'create', $contributionPriceSet);
+    $priceField = $this->callAPISuccess('price_field', 'getsingle', array('price_set_id' => $firstID, 'options' => array('limit' => 1)));
+    unset($priceField['id']);
+    $priceField['price_set_id'] = $newPriceSet['id'];
+    $newPriceField = $this->callAPISuccess('price_field', 'create', $priceField);
+    $priceFieldValue = $this->callAPISuccess('price_field_value', 'getsingle', array('price_set_id' => $firstID, 'sequential' => 1, 'options' => array('limit' => 1)));
+
+    unset($priceFieldValue['id']);
+    //create some padding to use up ids
+    $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'])));
+
+  }
+
 /**
  * Create an instance of the paypal processor
  * @todo this isn't a great place to put it - but really it belongs on a class that extends