CRM-19129 Fix issue where no contact emails are found and split out generating of...
authorSeamus Lee <seamuslee001@gmail.com>
Sat, 30 Jul 2016 00:13:52 +0000 (10:13 +1000)
committerSeamus Lee <seamuslee001@gmail.com>
Mon, 1 Aug 2016 11:40:15 +0000 (21:40 +1000)
CRM/Contact/Form/Task/EmailCommon.php
CRM/Contribute/Form/Task/PDF.php
tests/phpunit/CRM/Contact/Form/Task/EmailCommonTest.php [new file with mode: 0644]

index 296e7dd2b5bb400b6805fba6c2ecd413c64e749e..e52e3bfc9b412d47be78b2d5f4b9e2edb11a6430 100644 (file)
@@ -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'] = '<br/><br/>--' . $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'] = '<br/><br/>--' . $defaultEmail['signature_html'];
+      }
+      if (!empty($defaultEmail['signature_text'])) {
+        $defaults['text_message'] = "\n\n--\n" . $defaultEmail['signature_text'];
+      }
+      $form->setDefaults($defaults);
     }
-    $form->setDefaults($defaults);
   }
 
   /**
index d4c111cdb764532cf8623297672b343f23f576d6..9e4d2a2ec5697b18cc9e63344eb16299aa365a2e 100644 (file)
@@ -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 (file)
index 0000000..6fe310d
--- /dev/null
@@ -0,0 +1,58 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.7                                                |                                    |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2016                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+ */
+ /**
+  * Test class for CRM_Contact_Form_Task_EmailCommon.
+  * @group headless
+  */
+class CRM_Contact_Form_Task_EmailCommonTest extends CiviUnitTestCase {
+
+  protected function setUp() {
+    parent::setUp();
+    $this->_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" <seamus@example.com>',
+      '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" <seamus@example.com>', $emails));
+    $this->assertEquals('"Seamus Lee" <seamus@example.com>', $optionValue['values'][$this->_optionValue['id']]['label']);
+  }
+
+}