Update 'Thank You' & other corrections in message templates
[civicrm-core.git] / tests / phpunit / api / v3 / PaymentTest.php
index 6395c462a984d92c77e7718a7e6a7bca954cb4aa..c5725dae9156cfc5b72a1771b600ba0911ba2f57 100644 (file)
@@ -107,6 +107,52 @@ class api_v3_PaymentTest extends CiviUnitTestCase {
     ]);
   }
 
+  /**
+   * Retrieve Payment using trxn_id.
+   */
+  public function testGetPaymentWithTrxnID() {
+    $this->_individualId2 = $this->individualCreate();
+    $params1 = [
+      'contact_id' => $this->_individualId,
+      'trxn_id' => 111111,
+      'total_amount' => 10,
+    ];
+    $contributionID1 = $this->contributionCreate($params1);
+
+    $params2 = [
+      'contact_id' => $this->_individualId2,
+      'trxn_id' => 222222,
+      'total_amount' => 20,
+    ];
+    $contributionID2 = $this->contributionCreate($params2);
+
+    $paymentParams = ['trxn_id' => 111111];
+    $payment = $this->callAPISuccess('payment', 'get', $paymentParams);
+    $expectedResult = [
+      $payment['id'] => [
+        'total_amount' => 10,
+        'trxn_id' => 111111,
+        'status_id' => 1,
+        'is_payment' => 1,
+        'contribution_id' => $contributionID1,
+      ],
+    ];
+    $this->checkPaymentResult($payment, $expectedResult);
+
+    $paymentParams = ['trxn_id' => 222222];
+    $payment = $this->callAPISuccess('payment', 'get', $paymentParams);
+    $expectedResult = [
+      $payment['id'] => [
+        'total_amount' => 20,
+        'trxn_id' => 222222,
+        'status_id' => 1,
+        'is_payment' => 1,
+        'contribution_id' => $contributionID2,
+      ],
+    ];
+    $this->checkPaymentResult($payment, $expectedResult);
+  }
+
   /**
    * Test email receipt for partial payment.
    */
@@ -135,6 +181,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase {
     $this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id']]);
     $mut->assertSubjects(['Payment Receipt - Annual CiviCRM meet']);
     $mut->checkMailLog([
+      'From: "FIXME" <info@EXAMPLE.ORG>',
       'Dear Anthony,',
       'Total Fees: $ 300.00',
       'This Payment Amount: $ 50.00',
@@ -158,6 +205,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase {
    */
   public function testPaymentEmailReceiptFullyPaid() {
     $mut = new CiviMailUtils($this);
+    CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviContribute', 'edit contributions', 'access CiviCRM'];
     list($lineItems, $contribution) = $this->createParticipantWithContribution();
 
     $params = [
@@ -166,15 +214,18 @@ class api_v3_PaymentTest extends CiviUnitTestCase {
     ];
     $payment = $this->callAPISuccess('payment', 'create', $params);
 
-    $this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id']]);
-    $mut->assertSubjects(['Payment Receipt - Annual CiviCRM meet']);
+    // Here we set the email to an  invalid email & use check_permissions, domain email should be used.
+    $email = $this->callAPISuccess('Email', 'create', ['contact_id' => 1, 'email' => 'bob@example.com']);
+    $this->callAPISuccess('Payment', 'sendconfirmation', ['id' => $payment['id'], 'from' => $email['id'], 'check_permissions' => 1]);
+    $mut->assertSubjects(['Payment Receipt - Annual CiviCRM meet', 'Registration Confirmation - Annual CiviCRM meet']);
     $mut->checkMailLog([
+      'From: "FIXME" <info@EXAMPLE.ORG>',
       'Dear Anthony,',
       'A payment has been received.',
       'Total Fees: $ 300.00',
       'This Payment Amount: $ 150.00',
       'Balance Owed: $ 0.00',
-      'Thank you for completing payment.',
+      'Thank you for completing this payment.',
     ]);
     $mut->stop();
     $mut->clearMessages();
@@ -489,7 +540,28 @@ class api_v3_PaymentTest extends CiviUnitTestCase {
   }
 
   /**
-   * Test update payment api
+   * Test update payment api.
+   *
+   * 1) create a contribution for $300 with a partial payment of $150
+   * - this results in 2 financial transactions. The accounts receivable transaction is linked
+   *   via entity_financial_trxns to the 2 line items. The $150 payment is not linked to the line items
+   *   so the line items are fully allocated even though they are only half paid.
+   *
+   * 2) add a payment of $50 -
+   *  This payment transaction IS linked to the line items so $350 of the $300 in line items is allocated
+   *  but $200 is paid
+   *
+   * 3) update that payment to be $100
+   *  This results in a negative and a positive payment ($50 & $100) - the negative payment results in
+   *  financial_items but the positive payment does not.
+   *
+   * The final result is we have
+   * - 1 partly paid contribution of $300
+   * -  payment financial_trxns totalling $250
+   * - 1 Accounts receivable financial_trxn totalling $300
+   * - 2 financial items totalling $300 linked to the Accounts receivable financial_trxn
+   * - 6 entries in the civicrm_entity_financial_trxn linked to line items - totalling $450.
+   * - 5 entries in the civicrm_entity_financial_trxn linked to contributions - totalling $550.
    */
   public function testUpdatePayment() {
     CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM', 'access CiviContribute', 'edit contributions'];
@@ -522,7 +594,6 @@ class api_v3_PaymentTest extends CiviUnitTestCase {
     foreach ($eft['values'] as $value) {
       $this->assertEquals($value['amount'], array_pop($amounts));
     }
-    CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM', 'access CiviContribute'];
 
     // update the amount for payment
     $params = [
@@ -531,9 +602,11 @@ class api_v3_PaymentTest extends CiviUnitTestCase {
       'id' => $payment['id'],
       'check_permissions' => TRUE,
     ];
-    $payment = $this->callAPIFailure('payment', 'create', $params, 'API permission check failed for Payment/create call; insufficient permission: require access CiviCRM and access CiviContribute and edit contributions');
+    // @todo - move this permissions test to it's own test - it just confuses here.
+    CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM', 'access CiviContribute'];
+    $this->callAPIFailure('payment', 'create', $params, 'API permission check failed for Payment/create call; insufficient permission: require access CiviCRM and access CiviContribute and edit contributions');
 
-    array_push(CRM_Core_Config::singleton()->userPermissionClass->permissions, 'access CiviCRM', 'edit contributions');
+    CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM', 'access CiviContribute', 'access CiviCRM', 'edit contributions'];
     $payment = $this->callAPIAndDocument('payment', 'create', $params, __FUNCTION__, __FILE__, 'Update Payment', 'UpdatePayment');
 
     // Check for proportional cancelled payment against lineitems.
@@ -559,6 +632,14 @@ class api_v3_PaymentTest extends CiviUnitTestCase {
     foreach ($eft['values'] as $value) {
       $this->assertEquals($value['amount'], array_pop($amounts));
     }
+    $items = $this->callAPISuccess('FinancialItem', 'get', [])['values'];
+    $this->assertCount(2, $items);
+    $itemSum = 0;
+    foreach ($items as $item) {
+      $this->assertEquals('civicrm_line_item', $item['entity_table']);
+      $itemSum += $item['amount'];
+    }
+    $this->assertEquals(300, $itemSum);
 
     $params = [
       'contribution_id' => $contribution['id'],
@@ -640,7 +721,28 @@ class api_v3_PaymentTest extends CiviUnitTestCase {
   /**
    * Test create payment api for pay later contribution with partial payment.
    *
-   * @throws \Exception
+   * https://lab.civicrm.org/dev/financial/issues/69
+   */
+  public function testCreatePaymentIncompletePaymentPartialPayment() {
+    $contributionParams = [
+      'total_amount' => 100,
+      'currency' => 'USD',
+      'contact_id' => $this->_individualId,
+      'financial_type_id' => 1,
+      'contribution_status_id' => 2,
+    ];
+    $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
+    $this->callAPISuccess('Payment', 'create', [
+      'contribution_id' => $contribution['id'],
+      'total_amount' => 50,
+      'payment_instrument_id' => 'Cash',
+    ]);
+    $payments = $this->callAPISuccess('Payment', 'get', ['contribution_id' => $contribution['id']])['values'];
+    $this->assertCount(1, $payments);
+  }
+
+  /**
+   * Test create payment api for pay later contribution with partial payment.
    */
   public function testCreatePaymentPayLaterPartialPayment() {
     $this->createLoggedInUser();