From 53295c62f1fd21eb62e770b09d567d7bddcfdb5c Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Mon, 7 Jun 2021 14:04:10 +1200 Subject: [PATCH] Test fix up for AdditionalPaymentTest 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 --- .../Contribute/Form/AdditionalPaymentTest.php | 44 ++++++++++--------- .../CRM/Member/Form/MembershipTest.php | 2 +- .../CRMTraits/Financial/OrderTrait.php | 16 ++++--- tests/phpunit/api/v3/ContributionTest.php | 3 +- tests/phpunit/api/v3/MembershipTest.php | 2 +- 5 files changed, 36 insertions(+), 31 deletions(-) diff --git a/tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php b/tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php index 229317008a..72a8b8af45 100644 --- a/tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php +++ b/tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php @@ -18,6 +18,19 @@ */ 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, diff --git a/tests/phpunit/CRM/Member/Form/MembershipTest.php b/tests/phpunit/CRM/Member/Form/MembershipTest.php index c34942cf3b..4a16b367bc 100644 --- a/tests/phpunit/CRM/Member/Form/MembershipTest.php +++ b/tests/phpunit/CRM/Member/Form/MembershipTest.php @@ -1375,7 +1375,7 @@ Expires: ', */ public function testContributionFormStatusUpdate(): void { - $this->_contactID = $this->createLoggedInUser(); + $this->_contactID = $this->ids['Contact']['order'] = $this->createLoggedInUser(); $this->createContributionAndMembershipOrder(); $params = [ diff --git a/tests/phpunit/CRMTraits/Financial/OrderTrait.php b/tests/phpunit/CRMTraits/Financial/OrderTrait.php index cf5093a007..1ea8177966 100644 --- a/tests/phpunit/CRMTraits/Financial/OrderTrait.php +++ b/tests/phpunit/CRMTraits/Financial/OrderTrait.php @@ -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]; } /** diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index ef23c3c9ca..a4c1f7fdc6 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -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], diff --git a/tests/phpunit/api/v3/MembershipTest.php b/tests/phpunit/api/v3/MembershipTest.php index 24feffa2a1..6bb625f1e3 100644 --- a/tests/phpunit/api/v3/MembershipTest.php +++ b/tests/phpunit/api/v3/MembershipTest.php @@ -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']); -- 2.25.1