CRM-2144 Ensure consistancy with previous behavior where user emails are first then...
authorSeamus Lee <seamuslee001@gmail.com>
Sat, 31 Mar 2018 00:23:57 +0000 (11:23 +1100)
committerSeamus Lee <seamuslee001@gmail.com>
Sat, 31 Mar 2018 21:26:22 +0000 (07:26 +1000)
Add unit test to try to verify order of emails

Fix issue where email was not being used as array key causing wrong email to be used

CRM/Core/BAO/Email.php
tests/phpunit/CRM/Core/BAO/EmailTest.php

index e78dc24af773dff7e622f81822db2efe74708a35..3cb40c59167ec720c651f8c538767fa9a3ed3e1f 100644 (file)
@@ -304,6 +304,7 @@ AND    reset_date IS NULL
       return $fromEmailValues;
     }
 
+    $contactFromEmails = [];
     // add logged in user's active email ids
     $contactID = CRM_Core_Session::singleton()->getLoggedInContactID();
     if ($contactID) {
@@ -321,10 +322,10 @@ AND    reset_date IS NULL
         if (!empty($emailVal['is_primary'])) {
           $fromEmailHtml .= ' ' . ts('(preferred)');
         }
-        $fromEmailValues[$emailId] = $fromEmailHtml;
+        $contactFromEmails[$fromEmail] = $fromEmailHtml;
       }
     }
-    return $fromEmailValues;
+    return CRM_Utils_Array::crmArrayMerge($contactFromEmails, $fromEmailValues);
   }
 
   /**
index 39c4d879ebeba3de6383394d064be01113da6b7a..b2abc69c66a5275a3a3c7714762d6e09e9324a63 100644 (file)
@@ -149,4 +149,22 @@ class CRM_Core_BAO_EmailTest extends CiviUnitTestCase {
     $this->contactDelete($contactId);
   }
 
+  /**
+   * Test getting list of Emails for use in Receipts and Single Email sends
+   */
+  public function testGetFromEmail() {
+    $this->createLoggedInUser();
+    $fromEmails = CRM_Core_BAO_Email::getFromEmail();
+    $emails = array_values($fromEmails);
+    $this->assertContains("(preferred)", $emails[0]);
+    Civi::settings()->set("allow_mail_from_logged_in_contact", 0);
+    $this->callAPISuccess('system', 'flush', []);
+    $fromEmails = CRM_Core_BAO_Email::getFromEmail();
+    $emails = array_values($fromEmails);
+    $this->assertNotContains("(preferred)", $emails[0]);
+    $this->assertContains("info@EXAMPLE.ORG", $emails[0]);
+    Civi::settings()->set("allow_mail_from_logged_in_contact", 1);
+    $this->callAPISuccess('system', 'flush', []);
+  }
+
 }