From 2d7aca209608a3de817d7afe9f5942ffde36d04e Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sun, 19 Sep 2021 22:44:20 +1200 Subject: [PATCH] [REF] Extract getFromValues, add test This extracts the code used to get the from values, moves the default setting to the default and adds a test. The goal is actually to add a test on the submit function but it's proving to be challenging so this at least gets the default setting tested. Note that this code used to be on a static class, not a trait, and some stuff is in weird places - in particular stuff that should be in setDefaults and postProcess is happening on preProcess. However, most of the classes that override this trait do not do much more than call the trait so that helps at least --- CRM/Contact/Form/Task/EmailTrait.php | 57 +++++++++++------- .../CRM/Contribute/Form/Task/EmailTest.php | 60 +++++++++++++++++++ 2 files changed, 96 insertions(+), 21 deletions(-) create mode 100644 tests/phpunit/CRM/Contribute/Form/Task/EmailTest.php diff --git a/CRM/Contact/Form/Task/EmailTrait.php b/CRM/Contact/Form/Task/EmailTrait.php index 4bfa01b407..0655e3cbdc 100644 --- a/CRM/Contact/Form/Task/EmailTrait.php +++ b/CRM/Contact/Form/Task/EmailTrait.php @@ -138,24 +138,6 @@ trait CRM_Contact_Form_Task_EmailTrait { // are having to re-write contactIds afterwards due to this inappropriate variable setting // If we don't have any contact IDs, use the logged in contact ID $form->_contactIds = $form->_contactIds ?: [CRM_Core_Session::getLoggedInContactID()]; - - $fromEmailValues = CRM_Core_BAO_Email::getFromEmail(); - - if (empty($fromEmailValues)) { - CRM_Core_Error::statusBounce(ts('Your user record does not have a valid email address and no from addresses have been configured.')); - } - - $form->_emails = $fromEmailValues; - $defaults = []; - $form->_fromEmails = $fromEmailValues; - if (is_numeric(key($form->_fromEmails))) { - $emailID = (int) key($form->_fromEmails); - $defaults = CRM_Core_BAO_Email::getEmailSignatureDefaults($emailID); - } - if (!Civi::settings()->get('allow_mail_from_logged_in_contact')) { - $defaults['from_email_address'] = current(CRM_Core_BAO_Domain::getNameAndEmail(FALSE, TRUE)); - } - $form->setDefaults($defaults); } /** @@ -269,7 +251,7 @@ trait CRM_Contact_Form_Task_EmailTrait { $this->add('text', 'subject', ts('Subject'), ['size' => 50, 'maxlength' => 254], TRUE); - $this->add('select', 'from_email_address', ts('From'), $this->_fromEmails, TRUE); + $this->add('select', 'from_email_address', ts('From'), $this->getFromEmails(), TRUE); CRM_Mailing_BAO_Mailing::commonCompose($this); @@ -353,6 +335,27 @@ trait CRM_Contact_Form_Task_EmailTrait { CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'templates/CRM/Contact/Form/Task/EmailCommon.js', 0, 'html-header'); } + /** + * Set relevant default values. + * + * @return array + * + * @throws \API_Exception + * @throws \CRM_Core_Exception + */ + public function setDefaultValues(): array { + $defaults = parent::setDefaultValues(); + $fromEmails = $this->getFromEmails(); + if (is_numeric(key($fromEmails))) { + $emailID = (int) key($fromEmails); + $defaults = CRM_Core_BAO_Email::getEmailSignatureDefaults($emailID); + } + if (!Civi::settings()->get('allow_mail_from_logged_in_contact')) { + $defaults['from_email_address'] = current(CRM_Core_BAO_Domain::getNameAndEmail(FALSE, TRUE)); + } + return $defaults; + } + /** * Process the form after the input has been submitted and validated. * @@ -375,7 +378,7 @@ trait CRM_Contact_Form_Task_EmailTrait { * @param int $count * The number of emails the user is attempting to send */ - protected function bounceIfSimpleMailLimitExceeded($count) { + protected function bounceIfSimpleMailLimitExceeded($count): void { $limit = Civi::settings()->get('simple_mail_limit'); if ($count > $limit) { CRM_Core_Error::statusBounce(ts('Please do not use this task to send a lot of emails (greater than %1). Many countries have legal requirements when sending bulk emails and the CiviMail framework has opt out functionality and domain tokens to help meet these.', @@ -396,7 +399,7 @@ trait CRM_Contact_Form_Task_EmailTrait { * @throws \Civi\API\Exception\UnauthorizedException * @throws \API_Exception */ - public function submit($formValues) { + public function submit($formValues): void { $this->saveMessageTemplate($formValues); $from = $formValues['from_email_address'] ?? NULL; @@ -726,4 +729,16 @@ trait CRM_Contact_Form_Task_EmailTrait { return NULL; } + /** + * @return array + */ + protected function getFromEmails(): array { + $fromEmailValues = CRM_Core_BAO_Email::getFromEmail(); + + if (empty($fromEmailValues)) { + CRM_Core_Error::statusBounce(ts('Your user record does not have a valid email address and no from addresses have been configured.')); + } + return $fromEmailValues; + } + } diff --git a/tests/phpunit/CRM/Contribute/Form/Task/EmailTest.php b/tests/phpunit/CRM/Contribute/Form/Task/EmailTest.php new file mode 100644 index 0000000000..56b13ca7a3 --- /dev/null +++ b/tests/phpunit/CRM/Contribute/Form/Task/EmailTest.php @@ -0,0 +1,60 @@ +quickCleanUpFinancialEntities(); + parent::tearDown(); + } + + /** + * Test that email tokens are rendered. + */ + public function testEmailTokens(): void { + Civi::settings()->set('max_attachments', 0); + $contact1 = $this->individualCreate(); + $contact2 = $this->individualCreate(); + $userID = $this->createLoggedInUser(); + Civi::settings()->set('allow_mail_from_logged_in_contact', TRUE); + $this->callAPISuccess('Email', 'create', [ + 'contact_id' => $userID, + 'email' => 'benny_jetts@example.com', + 'signature_html' => 'Benny, Benny', + 'is_primary' => 1, + ]); + $contribution1 = $this->contributionCreate(['contact_id' => $contact2]); + $contribution2 = $this->contributionCreate(['total_amount' => 999, 'contact_id' => $contact1]); + $form = $this->getFormObject('CRM_Contribute_Form_Task_Email', ['cc_id' => '', 'bcc_id' => ''], [], [ + 'radio_ts' => 'ts_sel', + 'task' => CRM_Contribute_Task::TASK_EMAIL, + 'mark_x_' . $contribution1 => 1, + 'mark_x_' . $contribution2 => 1, + ]); + $form->set('cid', $contact1 . ',' . $contact2); + $form->buildForm(); + $this->assertEquals('

--Benny, Benny', $form->_defaultValues['html_message']); + } + +} -- 2.25.1