Merge pull request #16074 from civicrm/5.21
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / EmailTest.php
1 <?php
2
3 /**
4 * Class CRM_Core_BAO_EmailTest
5 * @group headless
6 */
7 class CRM_Core_BAO_EmailTest extends CiviUnitTestCase {
8
9 public function setUp() {
10 parent::setUp();
11
12 $this->quickCleanup(['civicrm_contact', 'civicrm_email']);
13 }
14
15 /**
16 * Add() method (create and update modes)
17 */
18 public function testAdd() {
19 $contactId = $this->individualCreate();
20
21 $params = [];
22 $params = [
23 'email' => 'jane.doe@example.com',
24 'is_primary' => 1,
25 'location_type_id' => 1,
26 'contact_id' => $contactId,
27 ];
28
29 CRM_Core_BAO_Email::add($params);
30
31 $emailId = $this->assertDBNotNull('CRM_Core_DAO_Email', 'jane.doe@example.com', 'id', 'email',
32 'Database check for created email address.'
33 );
34
35 // Now call add() to modify an existing email address
36
37 $params = [];
38 $params = [
39 'id' => $emailId,
40 'contact_id' => $contactId,
41 'is_bulkmail' => 1,
42 'on_hold' => 1,
43 ];
44
45 CRM_Core_BAO_Email::add($params);
46
47 $isBulkMail = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'is_bulkmail', 'id',
48 'Database check on updated email record.'
49 );
50 $this->assertEquals($isBulkMail, 1, 'Verify bulkmail value is 1.');
51
52 $this->contactDelete($contactId);
53 }
54
55 /**
56 * HoldEmail() method (set and reset on_hold condition)
57 */
58 public function testHoldEmail() {
59 $contactId = $this->individualCreate();
60
61 $params = [
62 'email' => 'jane.doe@example.com',
63 'is_primary' => 1,
64 'location_type_id' => 1,
65 'contact_id' => $contactId,
66 ];
67
68 CRM_Core_BAO_Email::add($params);
69
70 $emailId = $this->assertDBNotNull('CRM_Core_DAO_Email', 'jane.doe@example.com', 'id', 'email',
71 'Database check for created email address.'
72 );
73
74 // Now call add() to update on_hold=1 ("On Hold Bounce") and check record state
75 $params = [];
76 $params = [
77 'id' => $emailId,
78 'contact_id' => $contactId,
79 'on_hold' => 1,
80 ];
81
82 CRM_Core_BAO_Email::add($params);
83
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.'
88 );
89
90 $this->assertEquals(substr($holdDate, 0, 4), substr(date('YmdHis'), 0, 4),
91 'Compare hold_date (' . $holdDate . ') in DB to current year.'
92 );
93
94 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'on_hold', 'id', 1,
95 'Check if on_hold=1 in updated email record.'
96 );
97
98 // Now call add() to update on_hold=2 ("On Hold Opt-out") and check record state
99 $params = [];
100 $params = [
101 'id' => $emailId,
102 'contact_id' => $contactId,
103 'on_hold' => 2,
104 ];
105
106 CRM_Core_BAO_Email::add($params);
107
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.'
112 );
113
114 $this->assertEquals(substr($holdDate, 0, 4), substr(date('YmdHis'), 0, 4),
115 'Compare hold_date (' . $holdDate . ') in DB to current year.'
116 );
117
118 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'on_hold', 'id', 2,
119 'Check if on_hold=2 in updated email record.'
120 );
121
122 // Now call add() with on_hold=null (not on hold) and verify that reset_date is set.
123 $params = [];
124 $params = [
125 'id' => $emailId,
126 'contact_id' => $contactId,
127 'on_hold' => 'null',
128 ];
129
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.'
133 );
134 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'hold_date', 'id', '',
135 'Check if hold_date has been set to empty string.'
136 );
137
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.'
142 );
143
144 $this->assertEquals(substr($resetDate, 0, 4), substr(date('YmdHis'), 0, 4),
145 'Compare reset_date (' . $resetDate . ') in DB to current year.'
146 );
147
148 $this->contactDelete($contactId);
149 }
150
151 /**
152 * AllEmails() method - get all emails for our contact, with primary email first
153 */
154 public function testAllEmails() {
155 $contactParams = [
156 'first_name' => 'Alan',
157 'last_name' => 'Smith',
158 'email' => 'alan.smith1@example.com',
159 'api.email.create.0' => ['email' => 'alan.smith2@example.com', 'location_type_id' => 'Home'],
160 'api.email.create.1' => ['email' => 'alan.smith3@example.com', 'location_type_id' => 'Main'],
161 ];
162
163 $contactId = $this->individualCreate($contactParams);
164
165 $emails = CRM_Core_BAO_Email::allEmails($contactId);
166
167 $this->assertEquals(count($emails), 3, 'Checking number of returned emails.');
168
169 $firstEmailValue = array_slice($emails, 0, 1);
170
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.');
173
174 $this->contactDelete($contactId);
175 }
176
177 /**
178 * Test getting list of Emails for use in Receipts and Single Email sends
179 */
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', []);
193 }
194
195 }