Merge pull request #5536 from totten/4.5-httpclient
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionTest.php
index dd15e6ce85e19bfdf266fb1dc3a9b98381fec8ec..0da81ae91646afed1d8c587bdfb4a734e451e73e 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -38,7 +38,7 @@ require_once 'CiviTest/CiviMailUtils.php';
 class api_v3_ContributionTest extends CiviUnitTestCase {
 
   /**
-   * Assume empty database with just civicrm_data
+   * Assume empty database with just civicrm_data.
    */
   protected $_individualId;
   protected $_contribution;
@@ -50,6 +50,20 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   protected $_ids = array();
   protected $_pageParams = array();
 
+  /**
+   * Parameters to create payment processor.
+   *
+   * @var array
+   */
+  protected $_processorParams = array();
+
+  /**
+   * ID of created event.
+   *
+   * @var int
+   */
+  protected $_eventID;
+
   /**
    * Setup function.
    */
@@ -261,8 +275,6 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
     ));
   }
 
-  ///////////////// civicrm_contribution_
-
   /**
    * Create an contribution_id=FALSE and financial_type_id=Donation.
    */
@@ -278,7 +290,8 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * Check with complete array + custom field
+   * Check with complete array + custom field.
+   *
    * Note that the test is written on purpose without any
    * variables specific to participant so it can be replicated into other entities
    * and / or moved to the automated test suite
@@ -347,7 +360,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * Test checks that passing in line items suppresses the create mechanism
+   * Test checks that passing in line items suppresses the create mechanism.
    */
   public function testCreateContributionChainedLineItems() {
     $params = array(
@@ -380,7 +393,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
       ),
     );
 
-    $description = "Create Contribution with Nested Line Items";
+    $description = "Create Contribution with Nested Line Items.";
     $subfile = "CreateWithNestedLineItems";
     $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
 
@@ -427,7 +440,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * Test create with valid payment instument
+   * Test create with valid payment instrument.
    */
   public function testCreateContributionWithPaymentInstrument() {
     $params = $this->_params + array('payment_instrument' => 'EFT');
@@ -492,7 +505,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * Create test with unique field name on source
+   * Create test with unique field name on source.
    */
   public function testCreateContributionSource() {
 
@@ -517,7 +530,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * Create test with unique field name on source
+   * Create test with unique field name on source.
    */
   public function testCreateDefaultNow() {
 
@@ -576,7 +589,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
    * Test note created correctly.
    */
   public function testCreateContributionWithNote() {
-    $description = "Demonstrates creating contribution with Note Entity";
+    $description = "Demonstrates creating contribution with Note Entity.";
     $subfile = "ContributionCreateWithNote";
     $params = array(
       'contact_id' => $this->_individualId,
@@ -635,7 +648,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
    * This is the test for creating soft credits.
    */
   public function testCreateContributionWithSoftCredit() {
-    $description = "Demonstrates creating contribution with SoftCredit";
+    $description = "Demonstrates creating contribution with SoftCredit.";
     $subfile = "ContributionCreateWithSoftCredit";
     $contact2 = $this->callAPISuccess('Contact', 'create', array(
       'display_name' => 'superman',
@@ -660,7 +673,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   public function testCreateContributionWithSoftCreditDefaults() {
-    $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type";
+    $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type.";
     $subfile = "ContributionCreateWithSoftCreditDefaults";
     $contact2 = $this->callAPISuccess('Contact', 'create', array(
       'display_name' => 'superman',
@@ -682,7 +695,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   public function testCreateContributionWithHonoreeContact() {
-    $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id";
+    $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
     $subfile = "ContributionCreateWithHonoreeContact";
     $contact2 = $this->callAPISuccess('Contact', 'create', array(
       'display_name' => 'superman',
@@ -858,8 +871,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * Function tests that additional financial records are created when online contribution with pending option
-   * is created
+   * Function tests that additional financial records are created for online contribution with pending option.
    */
   public function testCreateContributionPendingOnline() {
     $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
@@ -903,7 +915,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * Function tests that line items, financial records are updated when contribution amount is changed
+   * Function tests that line items, financial records are updated when contribution amount is changed.
    */
   public function testCreateUpdateContributionChangeTotal() {
     $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
@@ -935,14 +947,14 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
 
     $this->assertEquals('125.00', $lineItems);
     $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
-    $fitemAmount = $this->_getFinancialItemAmount($contribution['id']);
-    // Financial trxn SUM = 125 + 5 (fee)
+
+    // Financial trxn SUM = 125 + 5 (fee).
     $this->assertEquals('130.00', $trxnAmount);
-    $this->assertEquals('125.00', $fitemAmount);
+    $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
   }
 
   /**
-   * Function tests that line items, financial records are updated when pay later contribution is received
+   * Function tests that line items, financial records are updated when pay later contribution is received.
    */
   public function testCreateUpdateContributionPayLater() {
     $contribParams = array(
@@ -970,7 +982,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * Function tests that financial records are updated when Payment Instrument is changed
+   * Function tests that financial records are updated when Payment Instrument is changed.
    */
   public function testCreateUpdateContributionPaymentInstrument() {
     $instrumentId = $this->_addPaymentInstrument();
@@ -995,7 +1007,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * Function tests that financial records are added when Contribution is Refunded
+   * Function tests that financial records are added when Contribution is Refunded.
    */
   public function testCreateUpdateContributionRefund() {
     $contribParams = array(
@@ -1020,7 +1032,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * Function tests invalid contribution status change
+   * Function tests invalid contribution status change.
    */
   public function testCreateUpdateContributionInValidStatusChange() {
     $contribParams = array(
@@ -1042,7 +1054,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * Function tests that financial records are added when Pending Contribution is Canceled
+   * Function tests that financial records are added when Pending Contribution is Canceled.
    */
   public function testCreateUpdateContributionCancelPending() {
     $contribParams = array(
@@ -1067,7 +1079,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * Function tests that financial records are added when Financial Type is Changed
+   * Function tests that financial records are added when Financial Type is Changed.
    */
   public function testCreateUpdateContributionChangeFinancialType() {
     $contribParams = array(
@@ -1091,7 +1103,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * Test that update does not change status id CRM-15105
+   * Test that update does not change status id CRM-15105.
    */
   public function testCreateUpdateWithoutChangingPendingStatus() {
     $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array('contribution_status_id' => 2)));
@@ -1104,7 +1116,8 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * To Update Contribution
+   * Test Updating a Contribution.
+   *
    * CHANGE: we require the API to do an incremental update
    */
   public function testCreateUpdateContribution() {
@@ -1115,7 +1128,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
 
     );
     $original = $this->callAPISuccess('contribution', 'get', $old_params);
-    //Make sure it came back
+    // Make sure it came back.
     $this->assertAPISuccess($original);
     $this->assertEquals($original['id'], $contributionID);
     //set up list of old params, verify
@@ -1209,6 +1222,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
 
   /**
    * Test civicrm_contribution_search with empty params.
+   *
    * All available contributions expected.
    */
   public function testSearchEmptyParams() {
@@ -1302,7 +1316,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * Test completing a transaction via the API
+   * Test completing a transaction via the API.
    *
    * Note that we are creating a logged in user because email goes out from
    * that person
@@ -1325,10 +1339,77 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * CRM-14151
-   * Test completing a transaction via the API
-   *
-   * tests.
+   * Test repeat contribution successfully creates line items.
+   */
+  public function testRepeatTransaction() {
+    $paymentProcessorID = $this->paymentProcessorCreate();
+    $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
+      'contact_id' => $this->_individualId,
+      'installments' => '12',
+      'frequency_interval' => '1',
+      'amount' => '500',
+      'contribution_status_id' => 1,
+      'start_date' => '2012-01-01 00:00:00',
+      'currency' => 'USD',
+      'frequency_unit' => 'month',
+      'payment_processor_id' => $paymentProcessorID,
+    ));
+    $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
+      $this->_params,
+      array('contribution_recur_id' => $contributionRecur['id']))
+    );
+
+    $this->callAPISuccess('contribution', 'repeattransaction', array(
+      'original_contribution_id' => $originalContribution['id'],
+      'contribution_status_id' => 'Completed',
+      'trxn_id' => uniqid(),
+    ));
+    $lineItemParams = array(
+      'entity_id' => $originalContribution['id'],
+      'sequential' => 1,
+      'return' => array(
+        'entity_table',
+        'qty',
+        'unit_price',
+        'line_total',
+        'label',
+        'financial_type_id',
+        'deductible_amount',
+        'price_field_value_id',
+        'price_field_id',
+      ),
+    );
+    $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
+      'entity_id' => $originalContribution['id'],
+    )));
+    $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
+      'entity_id' => $originalContribution['id'] + 1,
+    )));
+    unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
+    unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
+    $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
+
+    $this->quickCleanUpFinancialEntities();
+  }
+
+  /**
+   * Test completing a transaction does not 'mess' with net amount (CRM-15960).
+   */
+  public function testCompleteTransactionNetAmountOK() {
+    $this->createLoggedInUser();
+    $params = array_merge($this->_params, array('contribution_status_id' => 2));
+    unset($params['net_amount']);
+    $contribution = $this->callAPISuccess('contribution', 'create', $params);
+    $this->callAPISuccess('contribution', 'completetransaction', array(
+      'id' => $contribution['id'],
+    ));
+    $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
+    $this->assertEquals('Completed', $contribution['contribution_status']);
+    $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
+  }
+
+  /**
+   * CRM-14151 - Test completing a transaction via the API.
    */
   public function testCompleteTransactionWithReceiptDateSet() {
     $mut = new CiviMailUtils($this, TRUE);
@@ -1346,7 +1427,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * Test completing a transaction with an event via the API
+   * Test completing a transaction with an event via the API.
    *
    * Note that we are creating a logged in user because email goes out from
    * that person
@@ -1374,7 +1455,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * Test membership is renewed when transaction completed
+   * Test membership is renewed when transaction completed.
    */
   public function testCompleteTransactionMembershipPriceSet() {
     $this->createPriceSetWithPage('membership');
@@ -1386,7 +1467,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * Test membership is renewed when transaction completed
+   * Test membership is renewed when transaction completed.
    */
   public function testCompleteTransactionMembershipPriceSetTwoTerms() {
     $this->createPriceSetWithPage('membership');
@@ -1405,8 +1486,11 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
 
 
   /**
+   * Create price set with contribution test for test setup.
+   *
    * This could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
-   * on parent class at some point (fn is not in 4.4)
+   * on parent class at some point (fn is not in 4.4).
+   *
    * @param $entity
    * @param array $params
    */
@@ -1463,7 +1547,8 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * Set up a pending transaction with a specific price field id
+   * Set up a pending transaction with a specific price field id.
+   *
    * @param int $priceFieldValueID
    */
   public function setUpPendingContribution($priceFieldValueID) {
@@ -1507,7 +1592,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * Test sending a mail via the API
+   * Test sending a mail via the API.
    */
   public function testSendMail() {
     $mut = new CiviMailUtils($this, TRUE);
@@ -1529,7 +1614,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * Test sending a mail via the API
+   * Test sending a mail via the API.
    */
   public function testSendMailEvent() {
     $mut = new CiviMailUtils($this, TRUE);
@@ -1570,13 +1655,15 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * This function does a GET & compares the result against the $params
-   * Use as a double check on Creates
-   * @param $params
-   * @param $id
-   * @param int $delete
+   * This function does a GET & compares the result against the $params.
+   *
+   * Use as a double check on Creates.
+   *
+   * @param array $params
+   * @param int $id
+   * @param bool $delete
    */
-  public function contributionGetnCheck($params, $id, $delete = 1) {
+  public function contributionGetnCheck($params, $id, $delete = TRUE) {
 
     $contribution = $this->callAPISuccess('Contribution', 'Get', array(
       'id' => $id,
@@ -1599,8 +1686,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * Create a pending contribution & linked pending participant record
-   * (along with an event)
+   * Create a pending contribution & linked pending participant record (along with an event).
    */
   public function createPendingParticipantContribution() {
     $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
@@ -1624,6 +1710,8 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
+   * Get financial transaction amount.
+   *
    * @param int $contId
    *
    * @return null|string