Merge pull request #17253 from mattwire/utf8convertblocksize
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / EmailTest.php
CommitLineData
6a488035 1<?php
0eea664b 2
aba1cd8b
EM
3/**
4 * Class CRM_Core_BAO_EmailTest
acb109b7 5 * @group headless
aba1cd8b 6 */
6a488035 7class CRM_Core_BAO_EmailTest extends CiviUnitTestCase {
39b959db 8
2ffdaed1 9 /**
10 * Set up for class.
11 *
12 * @throws \CRM_Core_Exception
13 */
00be9182 14 public function setUp() {
6a488035 15 parent::setUp();
9099cab3 16 $this->quickCleanup(['civicrm_contact', 'civicrm_email']);
6a488035
TO
17 }
18
19 /**
100fef9d 20 * Add() method (create and update modes)
2ffdaed1 21 *
22 * @throws \CRM_Core_Exception
6a488035 23 */
2ffdaed1 24 public function testCreate() {
f2040bc6 25 $contactId = $this->individualCreate();
9099cab3 26 $params = [
6a488035
TO
27 'email' => 'jane.doe@example.com',
28 'is_primary' => 1,
29 'location_type_id' => 1,
30 'contact_id' => $contactId,
9099cab3 31 ];
6a488035 32
2ffdaed1 33 CRM_Core_BAO_Email::create($params);
6a488035
TO
34
35 $emailId = $this->assertDBNotNull('CRM_Core_DAO_Email', 'jane.doe@example.com', 'id', 'email',
36 'Database check for created email address.'
37 );
38
2ffdaed1 39 // Now call create() to modify an existing email address
9099cab3 40 $params = [
6a488035
TO
41 'id' => $emailId,
42 'contact_id' => $contactId,
43 'is_bulkmail' => 1,
44 'on_hold' => 1,
9099cab3 45 ];
6a488035 46
2ffdaed1 47 CRM_Core_BAO_Email::create($params);
6a488035
TO
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
93ac19cd 54 $this->contactDelete($contactId);
6a488035
TO
55 }
56
57 /**
2ffdaed1 58 * HoldEmail() method (set and reset on_hold condition).
59 *
60 * @throws \CRM_Core_Exception
6a488035 61 */
00be9182 62 public function testHoldEmail() {
f2040bc6 63 $contactId = $this->individualCreate();
6a488035 64
9099cab3 65 $params = [
6a488035
TO
66 'email' => 'jane.doe@example.com',
67 'is_primary' => 1,
68 'location_type_id' => 1,
69 'contact_id' => $contactId,
9099cab3 70 ];
6a488035 71
2ffdaed1 72 CRM_Core_BAO_Email::create($params);
6a488035
TO
73
74 $emailId = $this->assertDBNotNull('CRM_Core_DAO_Email', 'jane.doe@example.com', 'id', 'email',
75 'Database check for created email address.'
76 );
77
2ffdaed1 78 // Now call create() to update on_hold=1 ("On Hold Bounce") and check record state
9099cab3 79 $params = [
6a488035
TO
80 'id' => $emailId,
81 'contact_id' => $contactId,
82 'on_hold' => 1,
9099cab3 83 ];
6a488035 84
2ffdaed1 85 CRM_Core_BAO_Email::create($params);
6a488035
TO
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
2ffdaed1 101 // Now call create() to update on_hold=2 ("On Hold Opt-out") and check record state
9099cab3 102 $params = [
f61d1b83
AS
103 'id' => $emailId,
104 'contact_id' => $contactId,
105 'on_hold' => 2,
9099cab3 106 ];
f61d1b83 107
2ffdaed1 108 CRM_Core_BAO_Email::create($params);
f61d1b83
AS
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
2ffdaed1 124 // Now call create() with on_hold=null (not on hold) and verify that reset_date is set.
9099cab3 125 $params = [
6a488035
TO
126 'id' => $emailId,
127 'contact_id' => $contactId,
128 'on_hold' => 'null',
9099cab3 129 ];
6a488035 130
2ffdaed1 131 CRM_Core_BAO_Email::create($params);
6a488035
TO
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
93ac19cd 149 $this->contactDelete($contactId);
6a488035
TO
150 }
151
152 /**
100fef9d 153 * AllEmails() method - get all emails for our contact, with primary email first
2ffdaed1 154 *
155 * @throws \CRM_Core_Exception
6a488035 156 */
00be9182 157 public function testAllEmails() {
9099cab3 158 $contactParams = [
6a488035
TO
159 'first_name' => 'Alan',
160 'last_name' => 'Smith',
f2040bc6 161 'email' => 'alan.smith1@example.com',
9099cab3
CW
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 ];
6a488035 165
f2040bc6 166 $contactId = $this->individualCreate($contactParams);
6a488035
TO
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
93ac19cd 177 $this->contactDelete($contactId);
6a488035 178 }
96025800 179
669f45ef
SL
180 /**
181 * Test getting list of Emails for use in Receipts and Single Email sends
2ffdaed1 182 *
183 * @throws \CRM_Core_Exception
669f45ef
SL
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
6a488035 200}