update completetransaction test to reflect the update to make it only complete incomp...
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionTest.php
index 5e194923a5b7b42ee8b7d0d47d797bcaa4205201..73fce6b58faef5fe59afc7fa27a2dbf7aa02041e 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | 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.                                    |
  |                                                                    |
@@ -396,6 +396,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
 
     $lineItems = $this->callAPISuccess('line_item','get',array(
       'entity_id' => $contribution['id'],
+      'contribution_id' => $contribution['id'],
       'entity_table' => 'civicrm_contribution',
       'sequential' => 1,
     ));
@@ -424,18 +425,13 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
     $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1, 'In line ' . __LINE__);
     $lineItems = $this->callAPISuccess('line_item','get',array(
       'entity_id' => $contribution['id'],
+      'contribution_id' => $contribution['id'],
       'entity_table' => 'civicrm_contribution',
       'sequential' => 1,
       ));
     $this->assertEquals(1, $lineItems['count']);
     $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
-    $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
-    $lineItems = $this->callAPISuccess('line_item','get',array(
-      'entity_id' => $contribution['id'],
-      'entity_table' => 'civicrm_contribution',
-      'sequential' => 1,
-    ));
-    $this->assertEquals(1, $lineItems['count']);
+    $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
     $this->_checkFinancialRecords($contribution, 'offline');
     $this->contributionGetnCheck($params, $contribution['id']);
   }
@@ -628,19 +624,19 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
     $description = "Demonstrates creating contribution with SoftCredit";
     $subfile     = "ContributionCreateWithSoftCredit";
     $contact2    = $this->callAPISuccess('Contact', 'create', array('display_name' => 'superman', 'contact_type' => 'Individual'));
-    $params      = $this->_params + array(
-      'soft_credit_to' => $contact2['id'],
-
+    $softparams  =  array(
+      'contact_id' => $contact2['id'],
+      'amount' => 50,
+      'soft_credit_type_id' => 3
     );
 
+    $params      = $this->_params + array('soft_credit' => array(1 => $softparams));
     $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
-    //     $result = $this->callAPISuccess('contribution','get', array('return'=> 'soft_credit_to', 'sequential' => 1));
-    //     $this->assertEquals($contact2['id'], $result['values'][$result['id']]['soft_credit_to']) ;
-    //    well - the above doesn't work yet so lets do SQL
-    $query = "SELECT count(*) FROM civicrm_contribution_soft WHERE contact_id = " . $contact2['id'];
+    $result = $this->callAPISuccess('contribution','get', array('return'=> 'soft_credit', 'sequential' => 1));
 
-    $count = CRM_Core_DAO::singleValueQuery($query);
-    $this->assertEquals(1, $count);
+    $this->assertEquals($softparams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
+    $this->assertEquals($softparams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
+    $this->assertEquals($softparams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
 
     $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
     $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
@@ -694,10 +690,11 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
       ));
     $this->assertEquals(1, $lineItems['count']);
     $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
-    $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
+    $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
     $lineItems = $this->callAPISuccess('line_item','get',array(
 
         'entity_id' => $contribution['id'],
+        'contribution_id' => $contribution['id'],
         'entity_table' => 'civicrm_contribution',
         'sequential' => 1,
     ));
@@ -1215,12 +1212,11 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
   function testCompleteTransaction() {
     $mut = new CiviMailUtils( $this, true );
     $this->createLoggedInUser();
-    $params = array_merge($this->_params, array('contribution_status_id' => 1,));
+    $params = array_merge($this->_params, array('contribution_status_id' => 2,));
     $contribution = $this->callAPISuccess('contribution','create', $params);
-    $apiResult = $this->callAPISuccess('contribution', 'completetransaction', array(
+    $this->callAPISuccess('contribution', 'completetransaction', array(
       'id' => $contribution['id'],
-    )
-    );
+    ));
     $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1,));
     $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
     $mut->checkMailLog(array(
@@ -1234,13 +1230,12 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
    * CRM-14151
    * Test completing a transaction via the API
    *
-   * For wierd caching-y reasons this test performs differently in isolation than with other
    * tests.
    */
   function testCompleteTransactionWithReceiptDateSet() {
     $mut = new CiviMailUtils( $this, true );
     $this->createLoggedInUser();
-    $params = array_merge($this->_params, array('contribution_status_id' => 1,'receipt_date' => 'now'));
+    $params = array_merge($this->_params, array('contribution_status_id' => 2,'receipt_date' => 'now'));
     $contribution = $this->callAPISuccess('contribution','create', $params);
     $apiResult = $this->callAPISuccess('contribution', 'completetransaction', array(
       'id' => $contribution['id'],
@@ -1266,9 +1261,8 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
     $mut->clearMessages();
     $this->createLoggedInUser();
     $contributionID = $this->createPendingParticipantContribution();
-    $apiResult = $this->callAPISuccess('contribution', 'completetransaction', array(
-      'id' => $contributionID,
-    )
+    $this->callAPISuccess('contribution', 'completetransaction', array(
+      'id' => $contributionID,)
     );
     $participantStatus = $this->callAPISuccessGetValue('participant', array('id' => $this->ids['participant'], 'return' => 'participant_status_id'));
     $this->assertEquals(1, $participantStatus);
@@ -1277,8 +1271,143 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
       'Event',
       'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
     ));
-  $mut->stop();
+    $mut->stop();
+  }
 
+  /**
+   * test membership is renewed when transaction completed
+   */
+  function testCompleteTransactionMembershipPriceSet() {
+    $this->createPriceSetWithPage('membership');
+    $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
+    $this->callAPISuccess('contribution', 'completetransaction', array('id' =>  $this->_ids['contribution']));
+    $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
+    $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
+    $this->cleanUpAfterPriceSets();
+  }
+
+  /**
+   * test membership is renewed when transaction completed
+   */
+  function testCompleteTransactionMembershipPriceSetTwoTerms() {
+    $this->createPriceSetWithPage('membership');
+    $this->setUpPendingContribution($this->_ids['price_field_value'][1]);
+    $this->callAPISuccess('contribution', 'completetransaction', array('id' =>  $this->_ids['contribution']));
+    $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
+    $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
+    $this->cleanUpAfterPriceSets();
+  }
+
+  function cleanUpAfterPriceSets() {
+    $this->contactDelete($this->_ids['contact']);
+    $this->quickCleanup(array('civicrm_price_set_entity', 'civicrm_line_item', 'civicrm_contribution', 'civicrm_membership', 'civicrm_membership_payment'));
+    $this->callAPISuccess('price_field_value', 'delete', array('id' => $this->_ids['price_field_value'][0]));
+    $this->callAPISuccess('price_field_value', 'delete', array('id' => $this->_ids['price_field_value'][1]));
+    $this->callAPISuccess('price_field', 'delete', array('id' => $this->_ids['price_field'][0]));
+    $this->callAPISuccess('price_set', 'delete', array('id' => $this->_ids['price_set']));
+    $this->membershipTypeDelete(array('id' => $this->_ids['membership_type']));
+  }
+
+
+  /**
+   * 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
+   */
+  function createPriceSetWithPage($entity, $params = array()) {
+    $membershipTypeID = $this->membershipTypeCreate();
+    $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
+      'title' => "Test Contribution Page",
+      'financial_type_id' => 1,
+      'currency' => 'NZD',
+      'goal_amount' => 50,
+      'is_pay_later' => 1,
+      'is_monetary' => TRUE,
+      'is_email_receipt' => FALSE,
+    ));
+    $priceSet = $this->callAPISuccess('price_set', 'create', array(
+      'is_quick_config' => 0,
+      'extends' => 'CiviMember',
+      'financial_type_id' => 1,
+      'title' => 'my Page'
+    ));
+    $priceSetID = $priceSet['id'];
+
+    CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID );
+    $priceField = $this->callAPISuccess('price_field', 'create', array(
+      'price_set_id' => $priceSetID ,
+      'label' => 'Goat Breed',
+      'html_type' => 'Radio',
+    ));
+    $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
+        'price_set_id' => $priceSetID ,
+        'price_field_id' => $priceField['id'],
+        'label' => 'Long Haired Goat',
+        'amount' => 20,
+        'membership_type_id' => $membershipTypeID,
+        'membership_num_terms' => 1,
+      )
+    );
+    $this->_ids['price_field_value'] = array($priceFieldValue['id']);
+    $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
+        'price_set_id' => $priceSetID ,
+        'price_field_id' => $priceField['id'],
+        'label' => 'Shoe-eating Goat',
+        'amount' => 10,
+        'membership_type_id' => $membershipTypeID,
+        'membership_num_terms' => 2,
+      )
+    );
+    $this->_ids['price_field_value'][] = $priceFieldValue['id'];
+    $this->_ids['price_set'] = $priceSetID;
+    $this->_ids['contribution_page'] = $contributionPageResult['id'];
+    $this->_ids['price_field'] = array($priceField['id']);
+
+    $this->_ids['membership_type'] = $membershipTypeID;
+  }
+
+  /**
+   * Set up a pending transaction with a specific price field id
+   * @param $priceFieldValueID
+   */
+  function setUpPendingContribution($priceFieldValueID){
+    $contactID = $this->individualCreate();
+    $membership = $this->callAPISuccess('membership', 'create', array(
+      'contact_id' => $contactID,
+      'membership_type_id' => $this->_ids['membership_type'],
+      'start_date' => 'yesterday - 1 year',
+      'end_date' => 'yesterday',
+    ));
+    $contribution = $this->callAPISuccess('contribution', 'create', array(
+      'domain_id' => 1,
+      'contact_id' => $contactID,
+      'receive_date' => date('Ymd'),
+      'total_amount' => 100.00,
+      'financial_type_id' => 1,
+      'payment_instrument_id' => 'Credit Card',
+      'non_deductible_amount' => 10.00,
+      'trxn_id' => 'jdhfi88',
+      'invoice_id' => 'djfhiewuyr',
+      'source' => 'SSF',
+      'contribution_status_id' => 2,
+      'contribution_page_id' => $this->_ids['contribution_page'],
+      'api.membership_payment.create' => array('membership_id' => $membership['id']),
+    ));
+
+    $this->callAPISuccess('line_item', 'create', array(
+      'entity_id' => $contribution['id'],
+      'entity_table' => 'civicrm_contribution',
+      'price_field_id' => $this->_ids['price_field'][0],
+      'qty' => 1,
+      'unit_price' => 20,
+      'line_total' => 20,
+      'financial_type_id' => 1,
+      'price_field_value_id' => $priceFieldValueID,
+    ));
+    $this->_ids['contact'] = $contactID;
+    $this->_ids['contribution'] = $contribution['id'];
+    $this->_ids['membership'] = $membership['id'];
   }
 
   /**
@@ -1330,8 +1459,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
       'participant_id' => $participant['id'],
       'contribution_id' => $contribution['id'],
     ));
-    $apiResult = $this->callAPISuccess('contribution', 'sendconfirmation', array(
-
+    $this->callAPISuccess('contribution', 'sendconfirmation', array(
       'id' => $contribution['id'],
       'receipt_from_email' => 'api@civicrm.org',
       )
@@ -1396,7 +1524,11 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
     return $contribution['id'];
   }
 
- function _getFinancialTrxnAmount($contId) {
+  /**
+   * @param $contId
+   *
+   * @return null|string
+   */function _getFinancialTrxnAmount($contId) {
    $query = "SELECT
      SUM( ft.total_amount ) AS total
      FROM civicrm_financial_trxn AS ft
@@ -1408,7 +1540,11 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
    return $result;
  }
 
- function _getFinancialItemAmount($contId) {
+  /**
+   * @param $contId
+   *
+   * @return null|string
+   */function _getFinancialItemAmount($contId) {
    $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
    $query = "SELECT
      SUM(amount)
@@ -1419,7 +1555,11 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
    return $result;
  }
 
- function _checkFinancialItem($contId, $context) {
+  /**
+   * @param $contId
+   * @param $context
+   */
+  function _checkFinancialItem($contId, $context) {
    if ($context != 'paylater') {
      $params = array (
        'entity_id' =>   $contId,
@@ -1483,7 +1623,12 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
    }
  }
 
- function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL) {
+  /**
+   * @param $contribution
+   * @param $context
+   * @param null $instrumentId
+   */
+  function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL) {
    $trxnParams = array(
      'entity_id' =>   $contribution['id'],
      'entity_table' => 'civicrm_contribution',
@@ -1550,7 +1695,10 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
    $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, $compareParams);
  }
 
- function _addPaymentInstrument () {
+  /**
+   * @return mixed
+   */
+  function _addPaymentInstrument () {
    $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
    $optionParams = array(
      'option_group_id' => $gId,
@@ -1573,7 +1721,11 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
    return $optionValue['values'][$optionValue['id']]['value'];
  }
 
- function _checkFinancialRecords($params,$context) {
+  /**
+   * @param $params
+   * @param $context
+   */
+  function _checkFinancialRecords($params,$context) {
    $entityParams = array(
      'entity_id' => $params['id'],
      'entity_table' => 'civicrm_contribution',