From f68954cb960bc8c41fdbb5ffdafe3095023da502 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Sat, 30 Jul 2016 10:13:52 +1000 Subject: [PATCH] CRM-19129 Fix issue where no contact emails are found and split out generating of domain emails into own function so can be tested --- CRM/Contact/Form/Task/EmailCommon.php | 69 ++++++++++++------- CRM/Contribute/Form/Task/PDF.php | 2 +- .../CRM/Contact/Form/Task/EmailCommonTest.php | 58 ++++++++++++++++ 3 files changed, 104 insertions(+), 25 deletions(-) create mode 100644 tests/phpunit/CRM/Contact/Form/Task/EmailCommonTest.php diff --git a/CRM/Contact/Form/Task/EmailCommon.php b/CRM/Contact/Form/Task/EmailCommon.php index 296e7dd2b5..e52e3bfc9b 100644 --- a/CRM/Contact/Form/Task/EmailCommon.php +++ b/CRM/Contact/Form/Task/EmailCommon.php @@ -44,9 +44,25 @@ class CRM_Contact_Form_Task_EmailCommon { public $_toContactEmails = array(); /** + * Generate an array of Domain email addresses. + * @return array $domainEmails; + */ + public static function domainEmails() { + $domainEmails = array(); + $domainFrom = CRM_Core_OptionGroup::values('from_email_address'); + foreach (array_keys($domainFrom) as $k) { + $domainEmail = $domainFrom[$k]; + $domainEmails[$domainEmail] = htmlspecialchars($domainEmail); + } + return $domainEmails; + } + + /** + * Pre Process Form Addresses to be used in QUickfomr * @param CRM_Core_Form $form + * @param bool $bounce determine if we want to throw a status bounce. */ - public static function preProcessFromAddress(&$form) { + public static function preProcessFromAddress(&$form, $bounce = TRUE) { $form->_single = FALSE; $className = CRM_Utils_System::getClassName($form); if (property_exists($form, '_context') && @@ -88,43 +104,48 @@ class CRM_Contact_Form_Task_EmailCommon { $form->_noEmails = FALSE; } } + if (!empty($email)) { + $form->_emails[$emailId] = $emails[$emailId]; + $emails[$emailId] .= $item['locationType']; - $form->_emails[$emailId] = $emails[$emailId]; - $emails[$emailId] .= $item['locationType']; - - if ($item['is_primary']) { - $emails[$emailId] .= ' ' . ts('(preferred)'); + if ($item['is_primary']) { + $emails[$emailId] .= ' ' . ts('(preferred)'); + } + $emails[$emailId] = htmlspecialchars($emails[$emailId]); } - $emails[$emailId] = htmlspecialchars($emails[$emailId]); } $form->assign('noEmails', $form->_noEmails); - if ($form->_noEmails) { - CRM_Core_Error::statusBounce(ts('Your user record does not have a valid email address')); + if ($bounce) { + if ($form->_noEmails) { + CRM_Core_Error::statusBounce(ts('Your user record does not have a valid email address')); + } } // now add domain from addresses - $domainEmails = array(); - $domainFrom = CRM_Core_OptionGroup::values('from_email_address'); - foreach (array_keys($domainFrom) as $k) { - $domainEmail = $domainFrom[$k]; - $domainEmails[$domainEmail] = htmlspecialchars($domainEmail); + $domainEmails = self::domainEmails(); + foreach ($domainEmails as $domainEmail => $email) { $form->_emails[$domainEmail] = $domainEmail; } - $form->_fromEmails = CRM_Utils_Array::crmArrayMerge($emails, $domainEmails); - - // Add signature - $defaultEmail = civicrm_api3('email', 'getsingle', array('id' => key($form->_fromEmails))); - $defaults = array(); - if (!empty($defaultEmail['signature_html'])) { - $defaults['html_message'] = '

--' . $defaultEmail['signature_html']; + foreach ($form->_fromEmails as $key => $fromEmail) { + if (empty($fromEmail)) { + unset($form->_fromEmails[$key]); + } } - if (!empty($defaultEmail['signature_text'])) { - $defaults['text_message'] = "\n\n--\n" . $defaultEmail['signature_text']; + if (!empty($emails)) { + // Add signature + $defaultEmail = civicrm_api3('email', 'getsingle', array('id' => key($form->_fromEmails))); + $defaults = array(); + if (!empty($defaultEmail['signature_html'])) { + $defaults['html_message'] = '

--' . $defaultEmail['signature_html']; + } + if (!empty($defaultEmail['signature_text'])) { + $defaults['text_message'] = "\n\n--\n" . $defaultEmail['signature_text']; + } + $form->setDefaults($defaults); } - $form->setDefaults($defaults); } /** diff --git a/CRM/Contribute/Form/Task/PDF.php b/CRM/Contribute/Form/Task/PDF.php index d4c111cdb7..9e4d2a2ec5 100644 --- a/CRM/Contribute/Form/Task/PDF.php +++ b/CRM/Contribute/Form/Task/PDF.php @@ -93,7 +93,7 @@ AND {$this->_componentClause}"; 'title' => ts('Search Results'), ), ); - CRM_Contact_Form_Task_EmailCommon ::preProcessFromAddress($this); + CRM_Contact_Form_Task_EmailCommon ::preProcessFromAddress($this, FALSE); CRM_Utils_System::appendBreadCrumb($breadCrumb); CRM_Utils_System::setTitle(ts('Print Contribution Receipts')); } diff --git a/tests/phpunit/CRM/Contact/Form/Task/EmailCommonTest.php b/tests/phpunit/CRM/Contact/Form/Task/EmailCommonTest.php new file mode 100644 index 0000000000..6fe310df50 --- /dev/null +++ b/tests/phpunit/CRM/Contact/Form/Task/EmailCommonTest.php @@ -0,0 +1,58 @@ +_contactIds = array( + $this->individualCreate(array('first_name' => 'Antonia', 'last_name' => 'D`souza')), + $this->individualCreate(array('first_name' => 'Anthony', 'last_name' => 'Collins')), + ); + $this->_optionValue = $this->callApiSuccess('optionValue', 'create', array( + 'label' => '"Seamus Lee" ', + 'option_group_id' => 'from_email_address', + )); + } + + /** + * Test generating domain emails + */ + public function testDomainEmailGeneation() { + $emails = CRM_Contact_Form_Task_EmailCommon::domainEmails(); + $this->assertNotEmpty($emails); + $optionValue = $this->callAPISuccess('OptionValue', 'Get', array( + 'id' => $this->_optionValue['id'], + )); + $this->assertTrue(array_key_exists('"Seamus Lee" ', $emails)); + $this->assertEquals('"Seamus Lee" ', $optionValue['values'][$this->_optionValue['id']]['label']); + } + +} -- 2.25.1