From f3e6da5ee547c6a3cf4e96a972d72f1f1b7130a2 Mon Sep 17 00:00:00 2001
From: eileen <emcnaughton@wikimedia.org>
Date: Sun, 27 Oct 2019 12:35:28 +1300
Subject: [PATCH] Fix test setup Function to use order api

After ongoing issues resolving https://github.com/civicrm/civicrm-core/pull/14763 I have concluded the underlying
issue on the failing tests is the test setup. Specifically the use of 'partial_amount_to_pay' does not
create the correct underlying entities - it 'sort of' creates a payment without linking it to
the financial items.

These parameters are part of our first attempt at partial payments. I am removing them from the test here but
will deprecate later from other places in the code
---
 CRM/Financial/BAO/Payment.php               |  2 +
 tests/phpunit/CiviTest/CiviUnitTestCase.php | 58 ++++++++++-----------
 tests/phpunit/api/v3/OrderTest.php          |  6 ++-
 tests/phpunit/api/v3/PaymentTest.php        | 18 ++++---
 4 files changed, 45 insertions(+), 39 deletions(-)

diff --git a/CRM/Financial/BAO/Payment.php b/CRM/Financial/BAO/Payment.php
index db3c92a60d..1a96763798 100644
--- a/CRM/Financial/BAO/Payment.php
+++ b/CRM/Financial/BAO/Payment.php
@@ -196,6 +196,8 @@ class CRM_Financial_BAO_Payment {
    * @param array $params
    *
    * @return array
+   *
+   * @throws \CiviCRM_API3_Exception
    */
   public static function sendConfirmation($params) {
 
diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php
index 95a616edd8..e9404e0688 100644
--- a/tests/phpunit/CiviTest/CiviUnitTestCase.php
+++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php
@@ -2626,59 +2626,59 @@ AND    ( TABLE_NAME LIKE 'civicrm_value_%' )
    * Add participant with contribution
    *
    * @return array
+   *
+   * @throws \CRM_Core_Exception
    */
   protected function createParticipantWithContribution() {
     // creating price set, price field
     $this->_contactId = $this->individualCreate();
     $event = $this->eventCreate();
     $this->_eventId = $event['id'];
-    $eventParams = array(
+    $eventParams = [
       'id' => $this->_eventId,
       'financial_type_id' => 4,
       'is_monetary' => 1,
-    );
+    ];
     $this->callAPISuccess('event', 'create', $eventParams);
     $priceFields = $this->createPriceSet('event', $this->_eventId);
-    $participantParams = array(
+    $participantParams = [
       'financial_type_id' => 4,
       'event_id' => $this->_eventId,
       'role_id' => 1,
       'status_id' => 14,
       'fee_currency' => 'USD',
       'contact_id' => $this->_contactId,
-    );
+    ];
     $participant = $this->callAPISuccess('Participant', 'create', $participantParams);
-    $contributionParams = array(
-      'total_amount' => 150,
+    $orderParams = [
+      'total_amount' => 300,
       'currency' => 'USD',
       'contact_id' => $this->_contactId,
       'financial_type_id' => 4,
-      'contribution_status_id' => 1,
-      'partial_payment_total' => 300.00,
-      'partial_amount_to_pay' => 150,
+      'contribution_status_id' => 'Pending',
       'contribution_mode' => 'participant',
       'participant_id' => $participant['id'],
-    );
+      'api.Payment.create' => ['total_amount' => 150],
+    ];
     foreach ($priceFields['values'] as $key => $priceField) {
-      $lineItems[1][$key] = array(
-        'price_field_id' => $priceField['price_field_id'],
-        'price_field_value_id' => $priceField['id'],
-        'label' => $priceField['label'],
-        'field_title' => $priceField['label'],
-        'qty' => 1,
-        'unit_price' => $priceField['amount'],
-        'line_total' => $priceField['amount'],
-        'financial_type_id' => $priceField['financial_type_id'],
-      );
-    }
-    $contributionParams['line_item'] = $lineItems;
-    $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
-    $paymentParticipant = array(
-      'participant_id' => $participant['id'],
-      'contribution_id' => $contribution['id'],
-    );
-    $this->callAPISuccess('ParticipantPayment', 'create', $paymentParticipant);
-    return array($lineItems, $contribution);
+      $orderParams['line_items'][] = [
+        'line_item' => [
+          [
+            'price_field_id' => $priceField['price_field_id'],
+            'price_field_value_id' => $priceField['id'],
+            'label' => $priceField['label'],
+            'field_title' => $priceField['label'],
+            'qty' => 1,
+            'unit_price' => $priceField['amount'],
+            'line_total' => $priceField['amount'],
+            'financial_type_id' => $priceField['financial_type_id'],
+            'entity_table' => 'civicrm_participant',
+          ],
+        ],
+        'params' => $participant,
+      ];
+    }
+    return $this->callAPISuccess('Order', 'create', $orderParams);
   }
 
   /**
diff --git a/tests/phpunit/api/v3/OrderTest.php b/tests/phpunit/api/v3/OrderTest.php
index 3633f70d8b..1abf849ecd 100644
--- a/tests/phpunit/api/v3/OrderTest.php
+++ b/tests/phpunit/api/v3/OrderTest.php
@@ -97,10 +97,12 @@ class api_v3_OrderTest extends CiviUnitTestCase {
 
   /**
    * Test Get Order api for participant contribution.
+   *
+   * @throws \CRM_Core_Exception
    */
   public function testGetOrderParticipant() {
     $this->addOrder(FALSE, 100);
-    list($items, $contribution) = $this->createParticipantWithContribution();
+    $contribution = $this->createParticipantWithContribution();
 
     $params = [
       'contribution_id' => $contribution['id'],
@@ -108,7 +110,7 @@ class api_v3_OrderTest extends CiviUnitTestCase {
 
     $order = $this->callAPISuccess('Order', 'get', $params);
 
-    $this->assertEquals(2, count($order['values'][$contribution['id']]['line_items']));
+    $this->assertCount(2, $order['values'][$contribution['id']]['line_items']);
     $this->callAPISuccess('Contribution', 'Delete', [
       'id' => $contribution['id'],
     ]);
diff --git a/tests/phpunit/api/v3/PaymentTest.php b/tests/phpunit/api/v3/PaymentTest.php
index 4faa39bc40..0b85d83aa8 100644
--- a/tests/phpunit/api/v3/PaymentTest.php
+++ b/tests/phpunit/api/v3/PaymentTest.php
@@ -158,7 +158,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase {
    */
   public function testPaymentEmailReceipt() {
     $mut = new CiviMailUtils($this);
-    list($lineItems, $contribution) = $this->createParticipantWithContribution();
+    $contribution = $this->createParticipantWithContribution();
     $event = $this->callAPISuccess('Event', 'get', []);
     $this->addLocationToEvent($event['id']);
     $params = [
@@ -206,7 +206,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();
+    $contribution = $this->createParticipantWithContribution();
 
     $params = [
       'contribution_id' => $contribution['id'],
@@ -242,7 +242,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase {
     $this->setCurrencySeparators($thousandSeparator);
     $decimalSeparator = ($thousandSeparator === ',' ? '.' : ',');
     $mut = new CiviMailUtils($this);
-    list($lineItems, $contribution) = $this->createParticipantWithContribution();
+    $contribution = $this->createParticipantWithContribution();
     $this->callAPISuccess('payment', 'create', [
       'contribution_id' => $contribution['id'],
       'total_amount' => 50,
@@ -286,9 +286,11 @@ class api_v3_PaymentTest extends CiviUnitTestCase {
 
   /**
    * Test create payment api with no line item in params
+   *
+   * @throws \CRM_Core_Exception
    */
   public function testCreatePaymentNoLineItems() {
-    list($lineItems, $contribution) = $this->createParticipantWithContribution();
+    $contribution = $this->createParticipantWithContribution();
 
     //Create partial payment
     $params = [
@@ -385,7 +387,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase {
    * Test create payment api with line item in params
    */
   public function testCreatePaymentLineItems() {
-    list($lineItems, $contribution) = $this->createParticipantWithContribution();
+    $contribution = $this->createParticipantWithContribution();
     $lineItems = $this->callAPISuccess('LineItem', 'get', ['contribution_id' => $contribution['id']]);
 
     //Create partial payment by passing line item array is params
@@ -480,7 +482,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase {
    */
   public function testCancelPayment() {
     CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM', 'access CiviContribute'];
-    list($lineItems, $contribution) = $this->createParticipantWithContribution();
+    $contribution = $this->createParticipantWithContribution();
 
     $params = [
       'contribution_id' => $contribution['id'],
@@ -516,7 +518,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase {
    */
   public function testDeletePayment() {
     CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM', 'access CiviContribute'];
-    list($lineItems, $contribution) = $this->createParticipantWithContribution();
+    $contribution = $this->createParticipantWithContribution();
 
     $params = [
       'contribution_id' => $contribution['id'],
@@ -568,7 +570,7 @@ class api_v3_PaymentTest extends CiviUnitTestCase {
    */
   public function testUpdatePayment() {
     CRM_Core_Config::singleton()->userPermissionClass->permissions = ['administer CiviCRM', 'access CiviContribute', 'edit contributions'];
-    list($lineItems, $contribution) = $this->createParticipantWithContribution();
+    $contribution = $this->createParticipantWithContribution();
 
     //Create partial payment by passing line item array is params
     $params = [
-- 
2.25.1