From e73cf7de6a64057c389d165f976658fbdd9fb114 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 24 Sep 2021 09:12:25 +1200 Subject: [PATCH] Add test to cancel subscription form --- CRM/Contribute/Form/CancelSubscription.php | 23 +----- .../Form/CancelSubscriptionTest.php | 56 +++++++++++++ .../CRM/Contribute/Form/RecurForms.php | 82 +++++++++++++++++++ .../Form/UpdateSubscriptionTest.php | 67 +-------------- 4 files changed, 141 insertions(+), 87 deletions(-) create mode 100644 tests/phpunit/CRM/Contribute/Form/CancelSubscriptionTest.php create mode 100644 tests/phpunit/CRM/Contribute/Form/RecurForms.php diff --git a/CRM/Contribute/Form/CancelSubscription.php b/CRM/Contribute/Form/CancelSubscription.php index a5d1a36852..1504cb2db3 100644 --- a/CRM/Contribute/Form/CancelSubscription.php +++ b/CRM/Contribute/Form/CancelSubscription.php @@ -222,7 +222,7 @@ class CRM_Contribute_Form_CancelSubscription extends CRM_Contribute_Form_Contrib 'id' => $this->getSubscriptionDetails()->recur_id, 'membership_id' => $this->_mid, 'processor_message' => $message, - 'cancel_reason' => $params['cancel_reason'], + 'cancel_reason' => $this->getSubmittedValue('cancel_reason'), ]); $tplParams = []; @@ -255,25 +255,6 @@ class CRM_Contribute_Form_CancelSubscription extends CRM_Contribute_Form_Contrib } if (CRM_Utils_Array::value('is_notify', $params) == 1) { - if ($this->getSubscriptionDetails()->contribution_page_id) { - CRM_Core_DAO::commonRetrieveAll( - 'CRM_Contribute_DAO_ContributionPage', - 'id', - $this->getSubscriptionDetails()->contribution_page_id, - $value, - ['title', 'receipt_from_name', 'receipt_from_email'] - ); - $receiptFrom - = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$this->getSubscriptionDetails()->contribution_page_id]) . - '" <' . - $value[$this->getSubscriptionDetails()->contribution_page_id]['receipt_from_email'] . - '>'; - } - else { - $domainValues = CRM_Core_BAO_Domain::getNameAndEmail(); - $receiptFrom = "$domainValues[0] <$domainValues[1]>"; - } - // send notification $sendTemplateParams = [ @@ -283,7 +264,7 @@ class CRM_Contribute_Form_CancelSubscription extends CRM_Contribute_Form_Contrib 'tplParams' => $tplParams, //'isTest' => $isTest, set this from _objects 'PDFFilename' => 'receipt.pdf', - 'from' => $receiptFrom, + 'from' => CRM_Contribute_BAO_ContributionRecur::getRecurFromAddress($this->getContributionRecurID()), 'toName' => $this->_donorDisplayName, 'toEmail' => $this->_donorEmail, ]; diff --git a/tests/phpunit/CRM/Contribute/Form/CancelSubscriptionTest.php b/tests/phpunit/CRM/Contribute/Form/CancelSubscriptionTest.php new file mode 100644 index 0000000000..acdb085eee --- /dev/null +++ b/tests/phpunit/CRM/Contribute/Form/CancelSubscriptionTest.php @@ -0,0 +1,56 @@ +addContribution(); + /* @var CRM_Contribute_Form_CancelSubscription $form */ + $form = $this->getFormObject('CRM_Contribute_Form_CancelSubscription', ['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'); + } + + /** + * Get the strings to check for. + * + * @return string[] + */ + public function getExpectedMailStrings(): array { + return [ + 'MIME-Version: 1.0', + 'From: "Bob" ', + 'To: Anthony Anderson ', + "Subject: Recurring Contribution Cancellation Notification - Mr. Anthony\n Anderson II", + 'Return-Path: bob@example.org', + 'Dear Anthony,', + 'Your recurring contribution of $ 10.00, every 1 month has been cancelled as requested', + ]; + } + +} diff --git a/tests/phpunit/CRM/Contribute/Form/RecurForms.php b/tests/phpunit/CRM/Contribute/Form/RecurForms.php new file mode 100644 index 0000000000..991212a888 --- /dev/null +++ b/tests/phpunit/CRM/Contribute/Form/RecurForms.php @@ -0,0 +1,82 @@ +ids['Contact'][0])) { + $this->ids['Contact'][0] = $this->individualCreate(); + } + return $this->ids['Contact'][0]; + } + + /** + * + */ + public function addContribution(): void { + $this->paymentProcessorId = $this->processorCreate(); + $this->callAPISuccess('Order', 'create', [ + 'contact_id' => $this->getContactID(), + 'contribution_recur_id' => $this->getContributionRecurID(), + 'financial_type_id' => 'Donation', + 'total_amount' => 10, + 'contribution_page_id' => $this->getContributionPageID(), + 'api.Payment.create' => [ + 'total_amount' => 10, + 'payment_processor_id' => $this->paymentProcessorId, + 'is_send_contribution_notification' => FALSE, + ], + ]); + } + + /** + * Get contribution recur ID. + * + * return int + */ + public function getContributionRecurID(): int { + if (!isset($this->ids['ContributionRecur'][0])) { + $this->ids['ContributionRecur'][0] = $this->callAPISuccess('ContributionRecur', 'create', [ + 'contact_id' => $this->getContactID(), + 'amount' => 10, + 'installments' => 12, + 'frequency_interval' => 1, + 'frequency_unit' => 'month', + ])['id']; + } + return $this->ids['ContributionRecur'][0]; + } + + /** + * Get a contribution page id. + * + * @return int + */ + public function getContributionPageID(): int { + if (!isset($this->ids['ContributionPage'][0])) { + $this->ids['ContributionPage'][0] = $this->callAPISuccess('ContributionPage', 'create', [ + 'receipt_from_name' => 'Bob', + 'receipt_from_email' => 'bob@example.org', + 'financial_type_id' => 'Donation', + ])['id']; + } + return $this->ids['ContributionPage'][0]; + } + +} diff --git a/tests/phpunit/CRM/Contribute/Form/UpdateSubscriptionTest.php b/tests/phpunit/CRM/Contribute/Form/UpdateSubscriptionTest.php index 9c97d09465..d758acedfe 100644 --- a/tests/phpunit/CRM/Contribute/Form/UpdateSubscriptionTest.php +++ b/tests/phpunit/CRM/Contribute/Form/UpdateSubscriptionTest.php @@ -12,7 +12,7 @@ /** * Class CRM_Contribute_Form_UpdateSubscriptionTest */ -class CRM_Contribute_Form_UpdateSubscriptionTest extends CiviUnitTestCase { +class CRM_Contribute_Form_UpdateSubscriptionTest extends CRM_Contribute_Form_RecurForms { /** * Test the mail sent on update. @@ -55,69 +55,4 @@ class CRM_Contribute_Form_UpdateSubscriptionTest extends CiviUnitTestCase { ]; } - /** - * Get contact id. - * - * return int - */ - public function getContactID(): int { - if (!isset($this->ids['Contact'][0])) { - $this->ids['Contact'][0] = $this->individualCreate(); - } - return $this->ids['Contact'][0]; - } - - /** - * - */ - public function addContribution(): void { - $this->paymentProcessorId = $this->processorCreate(); - $this->callAPISuccess('Order', 'create', [ - 'contact_id' => $this->getContactID(), - 'contribution_recur_id' => $this->getContributionRecurID(), - 'financial_type_id' => 'Donation', - 'total_amount' => 10, - 'contribution_page_id' => $this->getContributionPageID(), - 'api.Payment.create' => [ - 'total_amount' => 10, - 'payment_processor_id' => $this->paymentProcessorId, - 'is_send_contribution_notification' => FALSE, - ], - ]); - } - - /** - * Get contribution recur ID. - * - * return int - */ - public function getContributionRecurID(): int { - if (!isset($this->ids['ContributionRecur'][0])) { - $this->ids['ContributionRecur'][0] = $this->callAPISuccess('ContributionRecur', 'create', [ - 'contact_id' => $this->getContactID(), - 'amount' => 10, - 'installments' => 12, - 'frequency_interval' => 1, - 'frequency_unit' => 'month', - ])['id']; - } - return $this->ids['ContributionRecur'][0]; - } - - /** - * Get a contribution page id. - * - * @return int - */ - public function getContributionPageID(): int { - if (!isset($this->ids['ContributionPage'][0])) { - $this->ids['ContributionPage'][0] = $this->callAPISuccess('ContributionPage', 'create', [ - 'receipt_from_name' => 'Bob', - 'receipt_from_email' => 'bob@example.org', - 'financial_type_id' => 'Donation', - ])['id']; - } - return $this->ids['ContributionPage'][0]; - } - } -- 2.25.1