Merge pull request #17253 from mattwire/utf8convertblocksize
[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 /**
10 * Set up for class.
11 *
12 * @throws \CRM_Core_Exception
13 */
14 public function setUp() {
15 parent::setUp();
16 $this->quickCleanup(['civicrm_contact', 'civicrm_email']);
17 }
18
19 /**
20 * Add() method (create and update modes)
21 *
22 * @throws \CRM_Core_Exception
23 */
24 public function testCreate() {
25 $contactId = $this->individualCreate();
26 $params = [
27 'email' => 'jane.doe@example.com',
28 'is_primary' => 1,
29 'location_type_id' => 1,
30 'contact_id' => $contactId,
31 ];
32
33 CRM_Core_BAO_Email::create($params);
34
35 $emailId = $this->assertDBNotNull('CRM_Core_DAO_Email', 'jane.doe@example.com', 'id', 'email',
36 'Database check for created email address.'
37 );
38
39 // Now call create() to modify an existing email address
40 $params = [
41 'id' => $emailId,
42 'contact_id' => $contactId,
43 'is_bulkmail' => 1,
44 'on_hold' => 1,
45 ];
46
47 CRM_Core_BAO_Email::create($params);
48
49 $isBulkMail = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'is_bulkmail', 'id',
50 'Database check on updated email record.'
51 );
52 $this->assertEquals($isBulkMail, 1, 'Verify bulkmail value is 1.');
53
54 $this->contactDelete($contactId);
55 }
56
57 /**
58 * HoldEmail() method (set and reset on_hold condition).
59 *
60 * @throws \CRM_Core_Exception
61 */
62 public function testHoldEmail() {
63 $contactId = $this->individualCreate();
64
65 $params = [
66 'email' => 'jane.doe@example.com',
67 'is_primary' => 1,
68 'location_type_id' => 1,
69 'contact_id' => $contactId,
70 ];
71
72 CRM_Core_BAO_Email::create($params);
73
74 $emailId = $this->assertDBNotNull('CRM_Core_DAO_Email', 'jane.doe@example.com', 'id', 'email',
75 'Database check for created email address.'
76 );
77
78 // Now call create() to update on_hold=1 ("On Hold Bounce") and check record state
79 $params = [
80 'id' => $emailId,
81 'contact_id' => $contactId,
82 'on_hold' => 1,
83 ];
84
85 CRM_Core_BAO_Email::create($params);
86
87 // Use assertDBNotNull to get back value of hold_date and check if it's in the current year.
88 // NOTE: The assertEquals will fail IF this test is run just as the year is changing (low likelihood).
89 $holdDate = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'hold_date', 'id',
90 'Retrieve hold_date from the updated email record.'
91 );
92
93 $this->assertEquals(substr($holdDate, 0, 4), substr(date('YmdHis'), 0, 4),
94 'Compare hold_date (' . $holdDate . ') in DB to current year.'
95 );
96
97 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'on_hold', 'id', 1,
98 'Check if on_hold=1 in updated email record.'
99 );
100
101 // Now call create() to update on_hold=2 ("On Hold Opt-out") and check record state
102 $params = [
103 'id' => $emailId,
104 'contact_id' => $contactId,
105 'on_hold' => 2,
106 ];
107
108 CRM_Core_BAO_Email::create($params);
109
110 // Use assertDBNotNull to get back value of hold_date and check that it's in the current year.
111 // NOTE: The assertEquals will fail IF this test is run just as the year is changing (low likelihood).
112 $holdDate = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'hold_date', 'id',
113 'Retrieve hold_date from the updated email record.'
114 );
115
116 $this->assertEquals(substr($holdDate, 0, 4), substr(date('YmdHis'), 0, 4),
117 'Compare hold_date (' . $holdDate . ') in DB to current year.'
118 );
119
120 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'on_hold', 'id', 2,
121 'Check if on_hold=2 in updated email record.'
122 );
123
124 // Now call create() with on_hold=null (not on hold) and verify that reset_date is set.
125 $params = [
126 'id' => $emailId,
127 'contact_id' => $contactId,
128 'on_hold' => 'null',
129 ];
130
131 CRM_Core_BAO_Email::create($params);
132 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'on_hold', 'id', 0,
133 'Check if on_hold=0 in updated email record.'
134 );
135 $this->assertDBCompareValue('CRM_Core_DAO_Email', $emailId, 'hold_date', 'id', '',
136 'Check if hold_date has been set to empty string.'
137 );
138
139 // Use assertDBNotNull to get back value of reset_date and check if it's in the current year.
140 // NOTE: The assertEquals will fail IF this test is run just as the year is changing (low likelihood).
141 $resetDate = $this->assertDBNotNull('CRM_Core_DAO_Email', $emailId, 'reset_date', 'id',
142 'Retrieve reset_date from the updated email record.'
143 );
144
145 $this->assertEquals(substr($resetDate, 0, 4), substr(date('YmdHis'), 0, 4),
146 'Compare reset_date (' . $resetDate . ') in DB to current year.'
147 );
148
149 $this->contactDelete($contactId);
150 }
151
152 /**
153 * AllEmails() method - get all emails for our contact, with primary email first
154 *
155 * @throws \CRM_Core_Exception
156 */
157 public function testAllEmails() {
158 $contactParams = [
159 'first_name' => 'Alan',
160 'last_name' => 'Smith',
161 'email' => 'alan.smith1@example.com',
162 'api.email.create.0' => ['email' => 'alan.smith2@example.com', 'location_type_id' => 'Home'],
163 'api.email.create.1' => ['email' => 'alan.smith3@example.com', 'location_type_id' => 'Main'],
164 ];
165
166 $contactId = $this->individualCreate($contactParams);
167
168 $emails = CRM_Core_BAO_Email::allEmails($contactId);
169
170 $this->assertEquals(count($emails), 3, 'Checking number of returned emails.');
171
172 $firstEmailValue = array_slice($emails, 0, 1);
173
174 $this->assertEquals('alan.smith1@example.com', $firstEmailValue[0]['email'], 'Confirm primary email address value.');
175 $this->assertEquals(1, $firstEmailValue[0]['is_primary'], 'Confirm first email address is primary.');
176
177 $this->contactDelete($contactId);
178 }
179
180 /**
181 * Test getting list of Emails for use in Receipts and Single Email sends
182 *
183 * @throws \CRM_Core_Exception
184 */
185 public function testGetFromEmail() {
186 $this->createLoggedInUser();
187 $fromEmails = CRM_Core_BAO_Email::getFromEmail();
188 $emails = array_values($fromEmails);
189 $this->assertContains("(preferred)", $emails[0]);
190 Civi::settings()->set("allow_mail_from_logged_in_contact", 0);
191 $this->callAPISuccess('system', 'flush', []);
192 $fromEmails = CRM_Core_BAO_Email::getFromEmail();
193 $emails = array_values($fromEmails);
194 $this->assertNotContains("(preferred)", $emails[0]);
195 $this->assertContains("info@EXAMPLE.ORG", $emails[0]);
196 Civi::settings()->set("allow_mail_from_logged_in_contact", 1);
197 $this->callAPISuccess('system', 'flush', []);
198 }
199
200 }