From 3753db6e232dd925b22b78758c8000d4231b1052 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Mon, 18 Dec 2023 19:25:40 +1300 Subject: [PATCH] Move ContributionPage test to ConfirmTest class --- .../Form/Contribution/ConfirmTest.php | 132 +++++++++++++++++- tests/phpunit/CiviTest/CiviUnitTestCase.php | 4 +- tests/phpunit/api/v3/ContributionPageTest.php | 116 --------------- 3 files changed, 131 insertions(+), 121 deletions(-) diff --git a/tests/phpunit/CRM/Contribute/Form/Contribution/ConfirmTest.php b/tests/phpunit/CRM/Contribute/Form/Contribution/ConfirmTest.php index 685976417d..21ea558e29 100644 --- a/tests/phpunit/CRM/Contribute/Form/Contribution/ConfirmTest.php +++ b/tests/phpunit/CRM/Contribute/Form/Contribution/ConfirmTest.php @@ -484,6 +484,7 @@ class CRM_Contribute_Form_Contribution_ConfirmTest extends CiviUnitTestCase { 'contribution_id' => $contribution['id'], 'total_amount' => round($totalAmount + $taxAmount, 2), 'payment_instrument_id' => 'Check', + 'version' => 3, ]); $mailUtil->checkMailLog([\Civi::format()->money(337.55), 'Tax Rate', 'Subtotal']); } @@ -586,7 +587,7 @@ class CRM_Contribute_Form_Contribution_ConfirmTest extends CiviUnitTestCase { 'contribution_status_id' => 1, ])['values']; $this->assertCount(2, $contributions); - $membershipPayment = $this->callAPISuccess('MembershipPayment', 'getsingle', ['return' => ['contribution_id', 'membership_id']]); + $membershipPayment = $this->callAPISuccess('MembershipPayment', 'getsingle', ['return' => ['contribution_id', 'membership_id'], 'version' => 3]); $this->assertArrayHasKey($membershipPayment['contribution_id'], $contributions); $membership = $this->callAPISuccessGetSingle('Membership', ['id' => $membershipPayment['membership_id']]); $this->assertEquals($membership['contact_id'], $contributions[$membershipPayment['contribution_id']]['contact_id']); @@ -627,7 +628,7 @@ class CRM_Contribute_Form_Contribution_ConfirmTest extends CiviUnitTestCase { $this->getContributionPageID()); $contribution = $this->callAPISuccess('Contribution', 'getsingle', ['contribution_page_id' => $this->getContributionPageID()]); - $membershipPayment = $this->callAPISuccess('MembershipPayment', 'getsingle', ['contribution_id' => $contribution['id']]); + $membershipPayment = $this->callAPISuccess('MembershipPayment', 'getsingle', ['contribution_id' => $contribution['id'], 'version' => 3]); $this->callAPISuccessGetSingle('LineItem', ['contribution_id' => $contribution['id'], 'entity_id' => $membershipPayment['id']]); $this->assertMailSentContainingStrings([ 'Dear Dave,', @@ -669,7 +670,7 @@ class CRM_Contribute_Form_Contribution_ConfirmTest extends CiviUnitTestCase { $this->getContributionPageID()); $contribution = $this->callAPISuccessGetSingle('Contribution', ['contribution_page_id' => $this->getContributionPageID()]); - $this->callAPISuccessGetSingle('MembershipPayment', ['contribution_id' => $contribution['id']]); + $this->callAPISuccessGetSingle('MembershipPayment', ['contribution_id' => $contribution['id'], 'version' => 3]); //Assert only one mail is being sent. $this->assertMailSentCount(1); $this->assertMailSentContainingStrings([ @@ -690,4 +691,129 @@ class CRM_Contribute_Form_Contribution_ConfirmTest extends CiviUnitTestCase { ]); } + /** + * Test submit recurring (yearly) membership with immediate confirmation (IATS style). + * + * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate + * processor (IATS style - denoted by returning trxn_id) + * - the first creates a new membership, completed contribution, in progress recurring. Check these + * - create another - end date should be extended + * + * @throws \CRM_Core_Exception + * @throws \Civi\API\Exception\UnauthorizedException + */ + public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPaymentYear(): void { + $this->doSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment(['duration_unit' => 'year', 'recur_frequency_unit' => 'year']); + } + + /** + * Test submit recurring (monthly) membership with immediate confirmation (IATS style). + * + * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate + * processor (IATS style - denoted by returning trxn_id) + * - the first creates a new membership, completed contribution, in progress recurring. Check these + * - create another - end date should be extended + * + * @throws \CRM_Core_Exception + * @throws \Civi\API\Exception\UnauthorizedException + */ + public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPaymentMonth(): void { + $this->doSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment(['duration_unit' => 'month', 'recur_frequency_unit' => 'month']); + } + + /** + * Helper function for testSubmitMembershipPriceSetPaymentProcessorRecurInstantPayment* + * + * @param array $params + * + * @throws \CRM_Core_Exception + * @throws \Civi\API\Exception\UnauthorizedException + */ + public function doSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment(array $params = []): void { + $membershipTypeParams['duration_unit'] = $params['duration_unit']; + if ($params['recur_frequency_unit'] === $params['duration_unit']) { + $durationUnit = $params['duration_unit']; + } + else { + $durationUnit = NULL; + } + if (empty($this->ids['MembershipType'])) { + $membershipTypeParams = array_merge([ + 'minimum_fee' => 2, + ], $membershipTypeParams); + $this->ids['MembershipType'] = [$this->membershipTypeCreate($membershipTypeParams)]; + } + $contributionPageParameters = [ + 'is_recur' => TRUE, + 'recur_frequency_unit' => 'month', + ]; + $this->contributionPageQuickConfigCreate($contributionPageParameters, [], FALSE, TRUE, TRUE, TRUE); + $dummyPP = Civi\Payment\System::singleton()->getById($this->ids['PaymentProcessor']['dummy']); + $dummyPP->setDoDirectPaymentResult(['payment_status_id' => 1, 'trxn_id' => 'create_first_success']); + $processor = $dummyPP->getPaymentProcessor(); + + if ($params['recur_frequency_unit'] === $params['duration_unit']) { + // Membership will be in "New" state because it will get confirmed as payment matches + $expectedMembershipStatus = 1; + } + else { + // Membership will still be in "Pending" state as it won't get confirmed as payment doesn't match + $expectedMembershipStatus = 5; + } + + $submitParams = [ + 'price_' . $this->ids['PriceField']['membership_amount'] => $this->ids['PriceFieldValue']['membership_general'], + 'is_recur' => 1, + 'billing_first_name' => 'Billy', + 'billing_middle_name' => 'Goat', + 'billing_last_name' => 'Gruff', + 'email-Primary' => 'billy-goat@the-bridge.net', + 'payment_processor_id' => $this->ids['PaymentProcessor']['dummy'], + 'credit_card_number' => '4111111111111111', + 'credit_card_type' => 'Visa', + 'credit_card_exp_date' => ['M' => 9, 'Y' => 2040], + 'cvv2' => 123, + 'frequency_interval' => 1, + 'frequency_unit' => $params['recur_frequency_unit'], + 'version' => 3, + ]; + $this->submitOnlineContributionForm($submitParams, + $this->getContributionPageID()); + $contribution = $this->callAPISuccess('contribution', 'getsingle', [ + 'contribution_page_id' => $this->getContributionPageID(), + 'contribution_status_id' => 1, + ]); + $this->assertEquals($processor['payment_instrument_id'], $contribution['payment_instrument_id']); + + $this->assertEquals('create_first_success', $contribution['trxn_id']); + $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', ['version' => 3]); + $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']); + $membership = $this->callAPISuccessGetSingle('membership', ['id' => $membershipPayment['membership_id']]); + $this->assertEquals($membership['contact_id'], $contribution['contact_id']); + $this->assertEquals($expectedMembershipStatus, $membership['status_id']); + $this->callAPISuccess('ContributionRecur', 'getsingle', ['id' => $contribution['contribution_recur_id']]); + $this->assertEquals($contribution['contribution_recur_id'], $membership['contribution_recur_id']); + + $this->callAPISuccess('line_item', 'getsingle', ['contribution_id' => $contribution['id'], 'entity_id' => $membership['id']]); + //renew it with processor setting completed - should extend membership + $submitParams['contact_id'] = $contribution['contact_id']; + $dummyPP->setDoDirectPaymentResult(['payment_status_id' => 1, 'trxn_id' => 'create_second_success']); + $this->submitOnlineContributionForm($submitParams, + $this->getContributionPageID()); + $this->callAPISuccess('contribution', 'getsingle', [ + 'id' => ['NOT IN' => [$contribution['id']]], + 'contribution_page_id' => $this->getContributionPageID(), + 'contribution_status_id' => 1, + ]); + $renewedMembership = $this->callAPISuccessGetSingle('membership', ['id' => $membershipPayment['membership_id']]); + if ($durationUnit) { + // We only have an end_date if frequency units match, otherwise membership won't be auto-renewed and dates won't be calculated. + $renewedMembershipEndDate = $this->membershipRenewalDate($durationUnit, $membership['end_date']); + $this->assertEquals($renewedMembershipEndDate, $renewedMembership['end_date']); + } + $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', ['id' => $contribution['contribution_recur_id']]); + $this->assertEquals($processor['payment_instrument_id'], $recurringContribution['payment_instrument_id']); + $this->assertEquals(5, $recurringContribution['contribution_status_id']); + } + } diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index b643348dc9..2c61ee83ca 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -3454,8 +3454,8 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { $participants[$lineItem['entity_id']] = $lineItem['entity_id']; } } - $membershipPayments = $this->callAPISuccess('MembershipPayment', 'get', ['contribution_id' => $contribution['id'], 'return' => 'membership_id'])['values']; - $participantPayments = $this->callAPISuccess('ParticipantPayment', 'get', ['contribution_id' => $contribution['id'], 'return' => 'participant_id'])['values']; + $membershipPayments = $this->callAPISuccess('MembershipPayment', 'get', ['contribution_id' => $contribution['id'], 'return' => 'membership_id', 'version' => 3])['values']; + $participantPayments = $this->callAPISuccess('ParticipantPayment', 'get', ['contribution_id' => $contribution['id'], 'return' => 'participant_id', 'version' => 3])['values']; $this->assertCount(count($memberships), $membershipPayments); $this->assertCount(count($participants), $participantPayments); foreach ($membershipPayments as $payment) { diff --git a/tests/phpunit/api/v3/ContributionPageTest.php b/tests/phpunit/api/v3/ContributionPageTest.php index d8c1e2e95b..e9f86f2234 100644 --- a/tests/phpunit/api/v3/ContributionPageTest.php +++ b/tests/phpunit/api/v3/ContributionPageTest.php @@ -540,122 +540,6 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase { } - /** - * Test submit recurring (yearly) membership with immediate confirmation (IATS style). - * - * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate - * processor (IATS style - denoted by returning trxn_id) - * - the first creates a new membership, completed contribution, in progress recurring. Check these - * - create another - end date should be extended - * - * @throws \CRM_Core_Exception - * @throws \Civi\API\Exception\UnauthorizedException - */ - public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPaymentYear(): void { - $this->doSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment(['duration_unit' => 'year', 'recur_frequency_unit' => 'year']); - } - - /** - * Test submit recurring (monthly) membership with immediate confirmation (IATS style). - * - * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate - * processor (IATS style - denoted by returning trxn_id) - * - the first creates a new membership, completed contribution, in progress recurring. Check these - * - create another - end date should be extended - * - * @throws \CRM_Core_Exception - * @throws \Civi\API\Exception\UnauthorizedException - */ - public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPaymentMonth(): void { - $this->doSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment(['duration_unit' => 'month', 'recur_frequency_unit' => 'month']); - } - - /** - * Test submit recurring (mismatched frequency unit) membership with immediate confirmation (IATS style). - * - * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate - * processor (IATS style - denoted by returning trxn_id) - * - the first creates a new membership, completed contribution, in progress recurring. Check these - * - create another - end date should be extended - */ - //public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPaymentDifferentFrequency(): void { - // $this->doSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment(array('duration_unit' => 'year', 'recur_frequency_unit' => 'month')); - //} - - /** - * Helper function for testSubmitMembershipPriceSetPaymentProcessorRecurInstantPayment* - * - * @param array $params - * - * @throws \CRM_Core_Exception - * @throws \Civi\API\Exception\UnauthorizedException - */ - public function doSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment(array $params = []): void { - $params['is_recur'] = 1; - $membershipTypeParams['duration_unit'] = $params['duration_unit']; - if ($params['recur_frequency_unit'] === $params['duration_unit']) { - $durationUnit = $params['duration_unit']; - } - else { - $durationUnit = NULL; - } - $this->setUpMembershipContributionPage(FALSE, FALSE, $membershipTypeParams); - $dummyPP = Civi\Payment\System::singleton()->getById($this->ids['PaymentProcessor']['dummy']); - $dummyPP->setDoDirectPaymentResult(['payment_status_id' => 1, 'trxn_id' => 'create_first_success']); - $processor = $dummyPP->getPaymentProcessor(); - - if ($params['recur_frequency_unit'] === $params['duration_unit']) { - // Membership will be in "New" state because it will get confirmed as payment matches - $expectedMembershipStatus = 1; - } - else { - // Membership will still be in "Pending" state as it won't get confirmed as payment doesn't match - $expectedMembershipStatus = 5; - } - - $submitParams = array_merge($this->getSubmitParamsMembership(TRUE), [ - 'is_recur' => 1, - 'frequency_interval' => 1, - 'frequency_unit' => $params['recur_frequency_unit'], - ]); - - $this->callAPISuccess('ContributionPage', 'submit', $submitParams); - $contribution = $this->callAPISuccess('contribution', 'getsingle', [ - 'contribution_page_id' => $this->getContributionPageID(), - 'contribution_status_id' => 1, - ]); - $this->assertEquals($processor['payment_instrument_id'], $contribution['payment_instrument_id']); - - $this->assertEquals('create_first_success', $contribution['trxn_id']); - $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', []); - $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']); - $membership = $this->callAPISuccessGetSingle('membership', ['id' => $membershipPayment['membership_id']]); - $this->assertEquals($membership['contact_id'], $contribution['contact_id']); - $this->assertEquals($expectedMembershipStatus, $membership['status_id']); - $this->callAPISuccess('contribution_recur', 'getsingle', ['id' => $contribution['contribution_recur_id']]); - $this->assertEquals($contribution['contribution_recur_id'], $membership['contribution_recur_id']); - - $this->callAPISuccess('line_item', 'getsingle', ['contribution_id' => $contribution['id'], 'entity_id' => $membership['id']]); - //renew it with processor setting completed - should extend membership - $submitParams['contact_id'] = $contribution['contact_id']; - $dummyPP->setDoDirectPaymentResult(['payment_status_id' => 1, 'trxn_id' => 'create_second_success']); - $this->callAPISuccess('contribution_page', 'submit', $submitParams); - $this->callAPISuccess('contribution', 'getsingle', [ - 'id' => ['NOT IN' => [$contribution['id']]], - 'contribution_page_id' => $this->getContributionPageID(), - 'contribution_status_id' => 1, - ]); - $renewedMembership = $this->callAPISuccessGetSingle('membership', ['id' => $membershipPayment['membership_id']]); - if ($durationUnit) { - // We only have an end_date if frequency units match, otherwise membership won't be auto-renewed and dates won't be calculated. - $renewedMembershipEndDate = $this->membershipRenewalDate($durationUnit, $membership['end_date']); - $this->assertEquals($renewedMembershipEndDate, $renewedMembership['end_date']); - } - $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', ['id' => $contribution['contribution_recur_id']]); - $this->assertEquals($processor['payment_instrument_id'], $recurringContribution['payment_instrument_id']); - $this->assertEquals(5, $recurringContribution['contribution_status_id']); - } - /** * Test submit recurring membership with immediate confirmation (IATS style). * -- 2.25.1