4 * Class CRM_Core_BAO_EmailTest
7 class CRM_Core_BAO_EmailTest
extends CiviUnitTestCase
{
8 public function setUp() {
11 $this->quickCleanup(array('civicrm_contact', 'civicrm_email'));
15 * Add() method (create and update modes)
17 public function testAdd() {
18 $contactId = $this->individualCreate();
22 'email' => 'jane.doe@example.com',
24 'location_type_id' => 1,
25 'contact_id' => $contactId,
28 CRM_Core_BAO_Email
::add($params);
30 $emailId = $this->assertDBNotNull('CRM_Core_DAO_Email', 'jane.doe@example.com', 'id', 'email',
31 'Database check for created email address.'
34 // Now call add() to modify an existing email address
39 'contact_id' => $contactId,
44 CRM_Core_BAO_Email
::add($params);
46 $isBulkMail = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'is_bulkmail', 'id',
47 'Database check on updated email record.'
49 $this->assertEquals($isBulkMail, 1, 'Verify bulkmail value is 1.');
51 $this->contactDelete($contactId);
55 * HoldEmail() method (set and reset on_hold condition)
57 public function testHoldEmail() {
58 $contactId = $this->individualCreate();
61 'email' => 'jane.doe@example.com',
63 'location_type_id' => 1,
64 'contact_id' => $contactId,
67 CRM_Core_BAO_Email
::add($params);
69 $emailId = $this->assertDBNotNull('CRM_Core_DAO_Email', 'jane.doe@example.com', 'id', 'email',
70 'Database check for created email address.'
73 // Now call add() to update on_hold=true and check record state
77 'contact_id' => $contactId,
81 CRM_Core_BAO_Email
::add($params);
83 // Use assertDBNotNull to get back value of hold_date and check if it's in the current year.
84 // NOTE: The assertEquals will fail IF this test is run just as the year is changing (low likelihood).
85 $holdDate = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'hold_date', 'id',
86 'Retrieve hold_date from the updated email record.'
89 $this->assertEquals(substr($holdDate, 0, 4), substr(date('YmdHis'), 0, 4),
90 'Compare hold_date (' . $holdDate . ') in DB to current year.'
93 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'on_hold', 'id', 1,
94 'Check if on_hold=1 in updated email record.'
97 // Now call add() with on_hold=false and verify that reset_date is set.
101 'contact_id' => $contactId,
105 CRM_Core_BAO_Email
::add($params);
106 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'on_hold', 'id', 0,
107 'Check if on_hold=0 in updated email record.'
109 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'hold_date', 'id', '',
110 'Check if hold_date has been set to empty string.'
113 // Use assertDBNotNull to get back value of reset_date and check if it's in the current year.
114 // NOTE: The assertEquals will fail IF this test is run just as the year is changing (low likelihood).
115 $resetDate = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'reset_date', 'id',
116 'Retrieve reset_date from the updated email record.'
119 $this->assertEquals(substr($resetDate, 0, 4), substr(date('YmdHis'), 0, 4),
120 'Compare reset_date (' . $resetDate . ') in DB to current year.'
123 $this->contactDelete($contactId);
127 * AllEmails() method - get all emails for our contact, with primary email first
129 public function testAllEmails() {
130 $contactParams = array(
131 'first_name' => 'Alan',
132 'last_name' => 'Smith',
133 'email' => 'alan.smith1@example.com',
134 'api.email.create.0' => array('email' => 'alan.smith2@example.com', 'location_type_id' => 'Home'),
135 'api.email.create.1' => array('email' => 'alan.smith3@example.com', 'location_type_id' => 'Main'),
138 $contactId = $this->individualCreate($contactParams);
140 $emails = CRM_Core_BAO_Email
::allEmails($contactId);
142 $this->assertEquals(count($emails), 3, 'Checking number of returned emails.');
144 $firstEmailValue = array_slice($emails, 0, 1);
146 $this->assertEquals('alan.smith1@example.com', $firstEmailValue[0]['email'], 'Confirm primary email address value.');
147 $this->assertEquals(1, $firstEmailValue[0]['is_primary'], 'Confirm first email address is primary.');
149 $this->contactDelete($contactId);
153 * Test getting list of Emails for use in Receipts and Single Email sends
155 public function testGetFromEmail() {
156 $this->createLoggedInUser();
157 $fromEmails = CRM_Core_BAO_Email
::getFromEmail();
158 $emails = array_values($fromEmails);
159 $this->assertContains("(preferred)", $emails[0]);
160 Civi
::settings()->set("allow_mail_from_logged_in_contact", 0);
161 $this->callAPISuccess('system', 'flush', []);
162 $fromEmails = CRM_Core_BAO_Email
::getFromEmail();
163 $emails = array_values($fromEmails);
164 $this->assertNotContains("(preferred)", $emails[0]);
165 $this->assertContains("info@EXAMPLE.ORG", $emails[0]);
166 Civi
::settings()->set("allow_mail_from_logged_in_contact", 1);
167 $this->callAPISuccess('system', 'flush', []);