From ecb321077959992506bf95dc4e225fcb1eff6267 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 21 Sep 2021 09:00:29 +1200 Subject: [PATCH] [Ref] Extend email trait test, process more sanely --- CRM/Contact/Form/Task/EmailTrait.php | 31 +++++++++++++--- .../CRM/Contact/Form/Task/EmailCommonTest.php | 35 +++++++++++-------- .../CRM/Contribute/Form/Task/EmailTest.php | 15 ++++++-- 3 files changed, 60 insertions(+), 21 deletions(-) diff --git a/CRM/Contact/Form/Task/EmailTrait.php b/CRM/Contact/Form/Task/EmailTrait.php index 0655e3cbdc..db6af3198c 100644 --- a/CRM/Contact/Form/Task/EmailTrait.php +++ b/CRM/Contact/Form/Task/EmailTrait.php @@ -37,6 +37,13 @@ trait CRM_Contact_Form_Task_EmailTrait { */ public $_templates; + /** + * Email addresses to send to. + * + * @var array + */ + protected $emails = []; + /** * Store "to" contact details. * @var array @@ -190,10 +197,8 @@ trait CRM_Contact_Form_Task_EmailTrait { if ($to->getValue()) { foreach ($this->getEmails($to) as $value) { $contactId = $value['contact_id']; - $email = $value['email']; if ($contactId) { $this->_contactIds[] = $this->_toContactIds[] = $contactId; - $this->_toContactEmails[] = $email; $this->_allContactIds[] = $contactId; } } @@ -366,8 +371,6 @@ trait CRM_Contact_Form_Task_EmailTrait { */ public function postProcess() { $this->bounceIfSimpleMailLimitExceeded(count($this->_contactIds)); - - // check and ensure that $formValues = $this->controller->exportValues($this->getName()); $this->submit($formValues); } @@ -424,7 +427,7 @@ trait CRM_Contact_Form_Task_EmailTrait { if (!isset($this->_contactDetails[$contactId])) { continue; } - $email = $this->_toContactEmails[$key]; + $email = $this->getEmail($key); // prevent duplicate emails if same email address is selected CRM-4067 // we should allow same emails for different contacts $details = $this->_contactDetails[$contactId]; @@ -741,4 +744,22 @@ trait CRM_Contact_Form_Task_EmailTrait { return $fromEmailValues; } + /** + * Get the relevant emails. + * + * @param int $index + * + * @return string + */ + protected function getEmail(int $index): string { + if (empty($this->emails)) { + $toEmails = explode(',', $this->getSubmittedValue('to')); + foreach ($toEmails as $value) { + $parts = explode('::', $value); + $this->emails[] = $parts[1]; + } + } + return $this->emails[$index]; + } + } diff --git a/tests/phpunit/CRM/Contact/Form/Task/EmailCommonTest.php b/tests/phpunit/CRM/Contact/Form/Task/EmailCommonTest.php index 589bcdf2e7..930bfbebc2 100644 --- a/tests/phpunit/CRM/Contact/Form/Task/EmailCommonTest.php +++ b/tests/phpunit/CRM/Contact/Form/Task/EmailCommonTest.php @@ -80,19 +80,6 @@ class CRM_Contact_Form_Task_EmailCommonTest extends CiviUnitTestCase { Civi::settings()->set('allow_mail_from_logged_in_contact', 1); $loggedInContactID = $this->createLoggedInUser(); - /* @var CRM_Contact_Form_Task_Email $form*/ - $form = $this->getFormObject('CRM_Contact_Form_Task_Email'); - - for ($i = 0; $i < 27; $i++) { - $email = 'spy' . $i . '@secretsquirrels.com'; - $contactID = $this->individualCreate(['email' => $email]); - $form->_contactIds[$contactID] = $contactID; - $form->_toContactEmails[$this->callAPISuccessGetValue('Email', ['return' => 'id', 'email' => $email])] = $email; - } - $deceasedContactID = $this->individualCreate(['is_deceased' => 1, 'email' => 'dead@example.com']); - $form->_contactIds[$deceasedContactID] = $deceasedContactID; - $form->_toContactEmails[$this->callAPISuccessGetValue('Email', ['return' => 'id', 'email' => 'dead@example.com'])] = 'dead@example.com'; - $loggedInEmail = $this->callAPISuccess('Email', 'create', [ 'email' => 'mickey@mouse.com', 'location_type_id' => 1, @@ -101,15 +88,35 @@ class CRM_Contact_Form_Task_EmailCommonTest extends CiviUnitTestCase { 'signature_text' => 'This is a test Signature', 'signature_html' => '

This is a test Signature

', ]); + + $to = $form_contactIds = $form_toContactEmails = []; + for ($i = 0; $i < 27; $i++) { + $email = 'spy' . $i . '@secretsquirrels.com'; + $contactID = $this->individualCreate(['email' => $email]); + $form_contactIds[$contactID] = $contactID; + $to[] = $contactID . '::' . $email; + } + $deceasedContactID = $this->individualCreate(['is_deceased' => 1, 'email' => 'dead@example.com']); + $to[] = $deceasedContactID . '::' . 'email@example.com'; + /* @var CRM_Contact_Form_Task_Email $form*/ + $form = $this->getFormObject('CRM_Contact_Form_Task_Email', [ + 'to' => implode(',', $to), + ]); + $form->_contactIds = $form_contactIds; + $form->_contactIds[$deceasedContactID] = $deceasedContactID; + $form->_allContactIds = $form->_toContactIds = $form->_contactIds; - $form->_emails = [$loggedInEmail['id'] => 'mickey@mouse.com']; $form->_fromEmails = [$loggedInEmail['id'] => 'mickey@mouse.com']; // This rule somehow disappears if there's a form-related test before us, // so register it again. See packages/HTML/QuickForm/file.php. + // update - actually - it's never registered. Even in form made + // I can see it missing - It's really weird. $form->registerRule('maxfilesize', 'callback', '_ruleCheckMaxFileSize', 'HTML_QuickForm_file'); $form->isSearchContext = FALSE; $form->buildForm(); $form->submit(array_merge($form->_defaultValues, [ + // @todo - it's better to pass these into getForm + // and access them on the form using $this->getSubmittedValue(). 'from_email_address' => $loggedInEmail['id'], 'subject' => 'Really interesting stuff', 'bcc_id' => $bcc, diff --git a/tests/phpunit/CRM/Contribute/Form/Task/EmailTest.php b/tests/phpunit/CRM/Contribute/Form/Task/EmailTest.php index 56b13ca7a3..f2061638c1 100644 --- a/tests/phpunit/CRM/Contribute/Form/Task/EmailTest.php +++ b/tests/phpunit/CRM/Contribute/Form/Task/EmailTest.php @@ -46,15 +46,26 @@ class CRM_Contribute_Form_Task_EmailTest extends CiviUnitTestCase { ]); $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' => ''], [], [ + $form = $this->getFormObject('CRM_Contribute_Form_Task_Email', [ + 'cc_id' => '', + 'bcc_id' => '', + 'to' => implode(',', [ + $contact1 . '::teresajensen-nielsen65@spamalot.co.in', + $contact2 . '::bob@example.com', + ]), + 'subject' => '{contact.display_name}', + 'text_message' => '{contribution.total_amount}', + 'html_message' => '{domain.name}', + ], [], [ 'radio_ts' => 'ts_sel', - 'task' => CRM_Contribute_Task::TASK_EMAIL, + 'task' => CRM_Core_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']); + $form->postProcess(); } } -- 2.25.1