4 * Class CRM_Core_BAO_EmailTest
7 class CRM_Core_BAO_EmailTest
extends CiviUnitTestCase
{
9 public function setUp() {
12 $this->quickCleanup(array('civicrm_contact', 'civicrm_email'));
16 * Add() method (create and update modes)
18 public function testAdd() {
19 $contactId = $this->individualCreate();
23 'email' => 'jane.doe@example.com',
25 'location_type_id' => 1,
26 'contact_id' => $contactId,
29 CRM_Core_BAO_Email
::add($params);
31 $emailId = $this->assertDBNotNull('CRM_Core_DAO_Email', 'jane.doe@example.com', 'id', 'email',
32 'Database check for created email address.'
35 // Now call add() to modify an existing email address
40 'contact_id' => $contactId,
45 CRM_Core_BAO_Email
::add($params);
47 $isBulkMail = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'is_bulkmail', 'id',
48 'Database check on updated email record.'
50 $this->assertEquals($isBulkMail, 1, 'Verify bulkmail value is 1.');
52 $this->contactDelete($contactId);
56 * HoldEmail() method (set and reset on_hold condition)
58 public function testHoldEmail() {
59 $contactId = $this->individualCreate();
62 'email' => 'jane.doe@example.com',
64 'location_type_id' => 1,
65 'contact_id' => $contactId,
68 CRM_Core_BAO_Email
::add($params);
70 $emailId = $this->assertDBNotNull('CRM_Core_DAO_Email', 'jane.doe@example.com', 'id', 'email',
71 'Database check for created email address.'
74 // Now call add() to update on_hold=1 ("On Hold Bounce") and check record state
78 'contact_id' => $contactId,
82 CRM_Core_BAO_Email
::add($params);
84 // Use assertDBNotNull to get back value of hold_date and check if it's in the current year.
85 // NOTE: The assertEquals will fail IF this test is run just as the year is changing (low likelihood).
86 $holdDate = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'hold_date', 'id',
87 'Retrieve hold_date from the updated email record.'
90 $this->assertEquals(substr($holdDate, 0, 4), substr(date('YmdHis'), 0, 4),
91 'Compare hold_date (' . $holdDate . ') in DB to current year.'
94 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'on_hold', 'id', 1,
95 'Check if on_hold=1 in updated email record.'
98 // Now call add() to update on_hold=2 ("On Hold Opt-out") and check record state
102 'contact_id' => $contactId,
106 CRM_Core_BAO_Email
::add($params);
108 // Use assertDBNotNull to get back value of hold_date and check that it's in the current year.
109 // NOTE: The assertEquals will fail IF this test is run just as the year is changing (low likelihood).
110 $holdDate = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'hold_date', 'id',
111 'Retrieve hold_date from the updated email record.'
114 $this->assertEquals(substr($holdDate, 0, 4), substr(date('YmdHis'), 0, 4),
115 'Compare hold_date (' . $holdDate . ') in DB to current year.'
118 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'on_hold', 'id', 2,
119 'Check if on_hold=2 in updated email record.'
122 // Now call add() with on_hold=null (not on hold) and verify that reset_date is set.
126 'contact_id' => $contactId,
130 CRM_Core_BAO_Email
::add($params);
131 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'on_hold', 'id', 0,
132 'Check if on_hold=0 in updated email record.'
134 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'hold_date', 'id', '',
135 'Check if hold_date has been set to empty string.'
138 // Use assertDBNotNull to get back value of reset_date and check if it's in the current year.
139 // NOTE: The assertEquals will fail IF this test is run just as the year is changing (low likelihood).
140 $resetDate = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'reset_date', 'id',
141 'Retrieve reset_date from the updated email record.'
144 $this->assertEquals(substr($resetDate, 0, 4), substr(date('YmdHis'), 0, 4),
145 'Compare reset_date (' . $resetDate . ') in DB to current year.'
148 $this->contactDelete($contactId);
152 * AllEmails() method - get all emails for our contact, with primary email first
154 public function testAllEmails() {
155 $contactParams = array(
156 'first_name' => 'Alan',
157 'last_name' => 'Smith',
158 'email' => 'alan.smith1@example.com',
159 'api.email.create.0' => array('email' => 'alan.smith2@example.com', 'location_type_id' => 'Home'),
160 'api.email.create.1' => array('email' => 'alan.smith3@example.com', 'location_type_id' => 'Main'),
163 $contactId = $this->individualCreate($contactParams);
165 $emails = CRM_Core_BAO_Email
::allEmails($contactId);
167 $this->assertEquals(count($emails), 3, 'Checking number of returned emails.');
169 $firstEmailValue = array_slice($emails, 0, 1);
171 $this->assertEquals('alan.smith1@example.com', $firstEmailValue[0]['email'], 'Confirm primary email address value.');
172 $this->assertEquals(1, $firstEmailValue[0]['is_primary'], 'Confirm first email address is primary.');
174 $this->contactDelete($contactId);
178 * Test getting list of Emails for use in Receipts and Single Email sends
180 public function testGetFromEmail() {
181 $this->createLoggedInUser();
182 $fromEmails = CRM_Core_BAO_Email
::getFromEmail();
183 $emails = array_values($fromEmails);
184 $this->assertContains("(preferred)", $emails[0]);
185 Civi
::settings()->set("allow_mail_from_logged_in_contact", 0);
186 $this->callAPISuccess('system', 'flush', []);
187 $fromEmails = CRM_Core_BAO_Email
::getFromEmail();
188 $emails = array_values($fromEmails);
189 $this->assertNotContains("(preferred)", $emails[0]);
190 $this->assertContains("info@EXAMPLE.ORG", $emails[0]);
191 Civi
::settings()->set("allow_mail_from_logged_in_contact", 1);
192 $this->callAPISuccess('system', 'flush', []);