Test fix up for AdditionalPaymentTest
authorEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 7 Jun 2021 02:04:10 +0000 (14:04 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 7 Jun 2021 02:04:33 +0000 (14:04 +1200)
This addresses a poor set up issue where the membership + contribution was being
set up incorrectly &  hence the line items were wrong, along with the ability
to validate the financials. It was blocking https://github.com/civicrm/civicrm-core/pull/20495
along with the efforts to get financial validation on all tests

tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php
tests/phpunit/CRM/Member/Form/MembershipTest.php
tests/phpunit/CRMTraits/Financial/OrderTrait.php
tests/phpunit/api/v3/ContributionTest.php
tests/phpunit/api/v3/MembershipTest.php

index 229317008af25ef68616d713f4d82ff0e9f9b294..72a8b8af456f4c358f2ea1c57b60d54aa89a906f 100644 (file)
  */
 class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase {
 
+  use CRMTraits_Financial_OrderTrait;
+
+
+  /**
+   * Should financials be checked after the test but before tear down.
+   *
+   * Ideally all tests (or at least all that call any financial api calls ) should do this but there
+   * are some test data issues and some real bugs currently blocking.
+   *
+   * @var bool
+   */
+  protected $isValidateFinancialsOnPostAssert = TRUE;
+
   /**
    * Contact ID.
    *
@@ -145,7 +158,6 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase {
 
     $mut->stop();
     $mut->clearMessages();
-    $this->validateAllPayments();
   }
 
   /**
@@ -160,7 +172,6 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase {
     // pay additional amount
     $this->submitPayment(70);
     $this->checkResults([30, 70], 2);
-    $this->validateAllPayments();
   }
 
   /**
@@ -193,7 +204,6 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase {
     $this->assertEquals(CRM_Core_Session::singleton()->getLoggedInContactID(), $activities[0]['source_contact_id']);
     $this->assertEquals([$this->_individualId], $activities[0]['target_contact_id']);
     $this->assertEquals([], $activities[0]['assignee_contact_id']);
-    $this->validateAllPayments();
   }
 
   /**
@@ -238,7 +248,6 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase {
     ]);
     $mut->stop();
     $mut->clearMessages();
-    $this->validateAllPayments();
   }
 
   /**
@@ -269,7 +278,6 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase {
     ]);
     $mut->stop();
     $mut->clearMessages();
-    $this->validateAllPayments();
   }
 
   /**
@@ -278,7 +286,7 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase {
    * @throws \CRM_Core_Exception
    * @throws \CiviCRM_API3_Exception
    */
-  public function testAddPaymentForPendingPayLaterContribution() {
+  public function testAddPaymentForPendingPayLaterContribution(): void {
     $this->createPendingOrder();
 
     // pay additional amount
@@ -292,7 +300,6 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase {
     // pay additional amount
     $this->submitPayment(30);
     $this->checkResults([30, 70], 2);
-    $this->validateAllPayments();
   }
 
   /**
@@ -301,16 +308,11 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase {
    * @throws \CRM_Core_Exception
    * @throws \CiviCRM_API3_Exception
    */
-  public function testMembershipStatusAfterCompletingPayLaterContribution() {
-    $this->createPendingOrder();
-    $membership = $this->createPendingMembershipAndRecordContribution($this->_contributionId);
-    // pay additional amount
-    $this->submitPayment(100);
-    $this->callAPISuccessGetSingle('Contribution', ['id' => $this->_contributionId]);
-    $contributionMembership = $this->callAPISuccessGetSingle('Membership', ['id' => $membership['id']]);
-    $membershipStatus = $this->callAPISuccessGetSingle('MembershipStatus', ['id' => $contributionMembership['status_id']]);
-    $this->assertEquals('New', $membershipStatus['name']);
-    $this->validateAllPayments();
+  public function testMembershipStatusAfterCompletingPayLaterContribution(): void {
+    $this->createContributionAndMembershipOrder();
+    $this->submitPayment(300);
+    $this->callAPISuccessGetSingle('Contribution', ['id' => $this->ids['Contribution'][0]]);
+    $this->callAPISuccessGetSingle('Membership', ['id' => $this->ids['Membership']['order'], 'status_id' => 'New']);
   }
 
   /**
@@ -319,6 +321,7 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase {
    * @return array|int
    *
    * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
    */
   private function createPendingMembershipAndRecordContribution($contributionId) {
     $this->_individualId = $this->individualCreate();
@@ -376,7 +379,6 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase {
 
     $this->submitPayment(10);
     $this->checkResults([40, 20, 30, 10], 4);
-    $this->validateAllPayments();
   }
 
   /**
@@ -403,7 +405,6 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase {
 
     $this->submitPayment(10, 'live');
     $this->checkResults([50, 20, 20, 10], 4);
-    $this->validateAllPayments();
   }
 
   /**
@@ -416,13 +417,14 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase {
    * @param bool $isEmailReceipt
    *
    * @throws \CiviCRM_API3_Exception
+   * @throws \CRM_Core_Exception
    */
   public function submitPayment($amount, $mode = NULL, $isEmailReceipt = FALSE) {
     $form = new CRM_Contribute_Form_AdditionalPayment();
 
     $submitParams = [
-      'contribution_id' => $this->_contributionId,
-      'contact_id' => $this->_individualId,
+      'contribution_id' => $this->ids['Contribution'][0] ?? $this->_contributionId,
+      'contact_id' => $this->ids['Contact']['order'] ?? $this->_individualId,
       'total_amount' => $amount,
       'currency' => 'USD',
       'financial_type_id' => 1,
index c34942cf3ba3accecb9752e511b38dc631bb9c21..4a16b367bc858e5424a3eddc7daa110c3dabfa5c 100644 (file)
@@ -1375,7 +1375,7 @@ Expires: ',
    */
   public function testContributionFormStatusUpdate(): void {
 
-    $this->_contactID = $this->createLoggedInUser();
+    $this->_contactID = $this->ids['Contact']['order'] = $this->createLoggedInUser();
     $this->createContributionAndMembershipOrder();
 
     $params = [
index cf5093a00737ab18bc60fb436a73c55a4d5f861d..1ea8177966a7da5678b30dd57a10562268e10185 100644 (file)
@@ -79,16 +79,19 @@ trait CRMTraits_Financial_OrderTrait {
    */
   protected function createContributionAndMembershipOrder(): void {
     $this->ids['membership_type'][0] = $this->membershipTypeCreate();
-    $orderID = $this->callAPISuccess('Order', 'create', [
+    if (empty($this->ids['Contact']['order'])) {
+      $this->ids['Contact']['order'] = $this->individualCreate();
+    }
+    $order = $this->callAPISuccess('Order', 'create', [
       'financial_type_id' => 'Donation',
-      'contact_id' => $this->_contactID,
+      'contact_id' => $this->ids['Contact']['order'],
       'is_test' => 0,
       'payment_instrument_id' => 'Check',
       'receive_date' => date('Y-m-d'),
       'line_items' => [
         [
           'params' => [
-            'contact_id' => $this->_contactID,
+            'contact_id' => $this->ids['Contact']['order'],
             'source' => 'Payment',
           ],
           'line_item' => [
@@ -110,7 +113,7 @@ trait CRMTraits_Financial_OrderTrait {
         ],
         [
           'params' => [
-            'contact_id' => $this->_contactID,
+            'contact_id' => $this->ids['Contact']['order'],
             'membership_type_id' => 'General',
             'source' => 'Payment',
             // This is necessary because Membership_BAO otherwise ignores the
@@ -121,9 +124,10 @@ trait CRMTraits_Financial_OrderTrait {
           'line_item' => $this->getMembershipLineItem(),
         ],
       ],
-    ])['id'];
+    ]);
 
-    $this->ids['Contribution'][0] = $orderID;
+    $this->ids['Contribution'][0] = $order['id'];
+    $this->ids['Membership']['order'] = $order['values'][$order['id']]['membership_id'][0];
   }
 
   /**
index ef23c3c9cae53c295cdf8ed4c87b7025b487811e..a4c1f7fdc682f51206f89042cd04780df9565989 100644 (file)
@@ -2794,11 +2794,10 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
    * @throws \CRM_Core_Exception|\CiviCRM_API3_Exception
    */
   public function testContributionOrder() {
-    $this->_contactID = $this->individualCreate();
     $this->createContributionAndMembershipOrder();
     $contribution = $this->callAPISuccess('contribution', 'get')['values'][$this->ids['Contribution'][0]];
     $this->assertEquals('Pending Label**', $contribution['contribution_status']);
-    $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_contactID]);
+    $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->ids['Contact']['order']]);
 
     $this->callAPISuccess('Payment', 'create', [
       'contribution_id' => $this->ids['Contribution'][0],
index 24feffa2a1d764ff5cb85d4262d13af21f2b5d99..6bb625f1e319d9db85005b868fdeac7dfce4e3d5 100644 (file)
@@ -144,7 +144,7 @@ class api_v3_MembershipTest extends CiviUnitTestCase {
    * @throws \CiviCRM_API3_Exception
    */
   public function testActivityForCancelledContribution(): void {
-    $contactId = $this->createLoggedInUser();
+    $contactId = $this->ids['Contact']['order'] = $this->createLoggedInUser();
 
     $this->createContributionAndMembershipOrder();
     $membershipID = $this->callAPISuccessGetValue('MembershipPayment', ['return' => 'id']);