From 7a5f4d9955b70ae24f38d4e63fc6d10a98ac7724 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 14 Dec 2023 23:42:08 +1300 Subject: [PATCH] Update updateSubscriptionTest to use FormTestTrait --- Civi/Test/FormTrait.php | 16 ++++++ Civi/Test/FormWrapper.php | 24 ++++++-- .../CRM/Contribute/Form/ContributionTest.php | 57 +++++++++---------- .../Form/UpdateSubscriptionTest.php | 34 +++++------ .../CRM/Event/Form/ParticipantTest.php | 11 +--- 5 files changed, 78 insertions(+), 64 deletions(-) diff --git a/Civi/Test/FormTrait.php b/Civi/Test/FormTrait.php index 757085ae1b..afd69ba6c0 100644 --- a/Civi/Test/FormTrait.php +++ b/Civi/Test/FormTrait.php @@ -102,6 +102,18 @@ trait FormTrait { $this->assertStringContainsString($string, $mail['headers']); } + /** + * Assert that the sent mail included the supplied strings. + * + * @param array $strings + * @param int $mailIndex + */ + protected function assertMailSentContainingHeaderStrings(array $strings, int $mailIndex = 0): void { + foreach ($strings as $string) { + $this->assertMailSentContainingHeaderString($string, $mailIndex); + } + } + /** * Assert the right number of mails were sent. * @@ -136,4 +148,8 @@ trait FormTrait { return $this->form->getDeprecatedProperty($property); } + protected function assertPrematureExit(): void { + $this->assertInstanceOf(\CRM_Core_Exception_PrematureExitException::class, $this->form->getException()); + } + } diff --git a/Civi/Test/FormWrapper.php b/Civi/Test/FormWrapper.php index ac13407d34..45adb71b9d 100644 --- a/Civi/Test/FormWrapper.php +++ b/Civi/Test/FormWrapper.php @@ -36,6 +36,15 @@ class FormWrapper { private $mail; + private $exception; + + /** + * @return \Exception + */ + public function getException() { + return $this->exception; + } + /** * @return null|array */ @@ -175,11 +184,16 @@ class FormWrapper { */ public function postProcess(): self { $this->startTrackingMail(); - $this->form->postProcess(); - foreach ($this->subsequentForms as $form) { - $form->preProcess(); - $form->buildForm(); - $form->postProcess(); + try { + $this->form->postProcess(); + foreach ($this->subsequentForms as $form) { + $form->preProcess(); + $form->buildForm(); + $form->postProcess(); + } + } + catch (\CRM_Core_Exception_PrematureExitException $e) { + $this->exception = $e; } $this->stopTrackingMail(); return $this; diff --git a/tests/phpunit/CRM/Contribute/Form/ContributionTest.php b/tests/phpunit/CRM/Contribute/Form/ContributionTest.php index b6912091f6..e4fe56cf08 100644 --- a/tests/phpunit/CRM/Contribute/Form/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/Form/ContributionTest.php @@ -422,37 +422,32 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase { */ public function testSubmitCreditCardInvalid(): void { $this->paymentProcessor->setDoDirectPaymentResult(['is_error' => 1]); - try { - $this->submitContributionForm([ - 'total_amount' => 50, - 'financial_type_id' => 1, - 'contact_id' => $this->_individualId, - 'payment_instrument_id' => $this->getPaymentInstrumentID('Credit Card'), - 'payment_processor_id' => $this->paymentProcessorID, - 'credit_card_exp_date' => ['M' => 5, 'Y' => 2012], - 'credit_card_number' => '411111111111111', - 'credit_card_type' => 'Visa', - ], NULL, 'live'); - } - catch (CRM_Core_Exception_PrematureExitException $e) { - $this->callAPISuccessGetCount('Contribution', [ - 'contact_id' => $this->_individualId, - 'contribution_status_id' => 'Failed', - ], 1); - $lineItem = $this->callAPISuccessGetSingle('line_item', []); - $this->assertEquals('50.00', $lineItem['unit_price']); - $this->assertEquals('50.00', $lineItem['line_total']); - $this->assertEquals(1, $lineItem['qty']); - $this->assertEquals(1, $lineItem['financial_type_id']); - $financialItem = $this->callAPISuccessGetSingle('financial_item', [ - 'civicrm_line_item' => $lineItem['id'], - 'entity_id' => $lineItem['id'], - ]); - $this->assertEquals('50.00', $financialItem['amount']); - $this->assertEquals(3, $financialItem['status_id']); - return; - } - $this->fail('An expected exception has not been raised.'); + $this->submitContributionForm([ + 'total_amount' => 50, + 'financial_type_id' => 1, + 'contact_id' => $this->_individualId, + 'payment_instrument_id' => $this->getPaymentInstrumentID('Credit Card'), + 'payment_processor_id' => $this->paymentProcessorID, + 'credit_card_exp_date' => ['M' => 5, 'Y' => 2012], + 'credit_card_number' => '411111111111111', + 'credit_card_type' => 'Visa', + ], NULL, 'live'); + $this->callAPISuccessGetCount('Contribution', [ + 'contact_id' => $this->_individualId, + 'contribution_status_id' => 'Failed', + ], 1); + $lineItem = $this->callAPISuccessGetSingle('line_item', []); + $this->assertEquals('50.00', $lineItem['unit_price']); + $this->assertEquals('50.00', $lineItem['line_total']); + $this->assertEquals(1, $lineItem['qty']); + $this->assertEquals(1, $lineItem['financial_type_id']); + $financialItem = $this->callAPISuccessGetSingle('financial_item', [ + 'civicrm_line_item' => $lineItem['id'], + 'entity_id' => $lineItem['id'], + ]); + $this->assertEquals('50.00', $financialItem['amount']); + $this->assertEquals(3, $financialItem['status_id']); + $this->assertPrematureExit(); } /** diff --git a/tests/phpunit/CRM/Contribute/Form/UpdateSubscriptionTest.php b/tests/phpunit/CRM/Contribute/Form/UpdateSubscriptionTest.php index d0d4e7ef95..0708b2bb5f 100644 --- a/tests/phpunit/CRM/Contribute/Form/UpdateSubscriptionTest.php +++ b/tests/phpunit/CRM/Contribute/Form/UpdateSubscriptionTest.php @@ -9,33 +9,31 @@ +--------------------------------------------------------------------+ */ +use Civi\Test\FormTrait; + /** * Class CRM_Contribute_Form_UpdateSubscriptionTest */ class CRM_Contribute_Form_UpdateSubscriptionTest extends CiviUnitTestCase { use CRMTraits_Contribute_RecurFormsTrait; + use FormTrait; /** * Test the mail sent on update. - * - * @throws \CRM_Core_Exception */ public function testMail(): void { - $mut = new CiviMailUtils($this, TRUE); $this->addContribution(); - /** @var CRM_Contribute_Form_UpdateSubscription $form */ - $form = $this->getFormObject('CRM_Contribute_Form_UpdateSubscription', ['is_notify' => TRUE]); - $form->set('crid', $this->getContributionRecurID()); - $form->buildForm(); - try { - $form->postProcess(); - } - catch (CRM_Core_Exception_PrematureExitException $e) { - $mut->checkMailLog($this->getExpectedMailStrings()); - return; - } - $this->fail('should not be reachable'); + $form = $this->getTestForm('CRM_Contribute_Form_UpdateSubscription', + ['is_notify' => TRUE], + ['crid' => $this->getContributionRecurID()]); + $form->processForm(); + $this->assertMailSentContainingHeaderStrings([ + 'Return-Path: bob@example.org', + 'Anthony Anderson ', + 'Subject: Recurring Contribution Update Notification - Mr. Anthony Anderson II', + ]); + $this->assertMailSentContainingStrings($this->getExpectedMailStrings()); } /** @@ -45,11 +43,7 @@ class CRM_Contribute_Form_UpdateSubscriptionTest extends CiviUnitTestCase { */ public function getExpectedMailStrings(): array { return [ - 'MIME-Version: 1.0', - 'From: "Bob" ', - 'To: Anthony Anderson ', - 'Subject: Recurring Contribution Update Notification - Mr. Anthony Anderson II', - 'Return-Path: bob@example.org', + '"Bob" ', 'Dear Anthony,', 'Your recurring contribution has been updated as requested:', 'Recurring contribution is for $10.00, every 1 month(s) for 12 installments.', diff --git a/tests/phpunit/CRM/Event/Form/ParticipantTest.php b/tests/phpunit/CRM/Event/Form/ParticipantTest.php index cf585c1b5e..af18d227f3 100644 --- a/tests/phpunit/CRM/Event/Form/ParticipantTest.php +++ b/tests/phpunit/CRM/Event/Form/ParticipantTest.php @@ -268,14 +268,9 @@ United States
', $this->setCurrencySeparators($thousandSeparator); $paymentProcessorID = $this->processorCreate(['is_test' => 0]); $_REQUEST['mode'] = 'live'; - Civi\Payment\System::singleton()->getById($paymentProcessorID)->setDoDirectPaymentResult(['payment_status_id' => 'failed']); - try { - $this->submitForm(['is_monetary' => 1, 'financial_type_id' => 1], $this->getSubmitParamsForCreditCardPayment($paymentProcessorID), TRUE); - } - catch (CRM_Core_Exception_PrematureExitException $e) { - return; - } - $this->fail('should have hit premature exit'); + \Civi\Payment\System::singleton()->getById($paymentProcessorID)->setDoDirectPaymentResult(['payment_status_id' => 'failed']); + $this->submitForm(['is_monetary' => 1, 'financial_type_id' => 1], $this->getSubmitParamsForCreditCardPayment($paymentProcessorID), TRUE); + $this->assertPrematureExit(); } /** -- 2.25.1