Merge pull request #5536 from totten/4.5-httpclient
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionTest.php
index 9ee82f1aed81c485d1524a4c71819fe1673950aa..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.                                    |
  |                                                                    |
@@ -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.
    */
@@ -276,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
@@ -856,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);
@@ -901,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);
@@ -940,7 +954,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * 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(
@@ -1089,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)));
@@ -1208,6 +1222,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
 
   /**
    * Test civicrm_contribution_search with empty params.
+   *
    * All available contributions expected.
    */
   public function testSearchEmptyParams() {
@@ -1323,6 +1338,60 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
     $mut->stop();
   }
 
+  /**
+   * 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).
    */
@@ -1340,10 +1409,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
-   * CRM-14151
-   * Test completing a transaction via the API
-   *
-   * tests.
+   * CRM-14151 - Test completing a transaction via the API.
    */
   public function testCompleteTransactionWithReceiptDateSet() {
     $mut = new CiviMailUtils($this, TRUE);
@@ -1420,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).
+   *
    * @param $entity
    * @param array $params
    */
@@ -1479,6 +1548,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
 
   /**
    * Set up a pending transaction with a specific price field id.
+   *
    * @param int $priceFieldValueID
    */
   public function setUpPendingContribution($priceFieldValueID) {
@@ -1585,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,
@@ -1614,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'));
@@ -1639,6 +1710,8 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   }
 
   /**
+   * Get financial transaction amount.
+   *
    * @param int $contId
    *
    * @return null|string