Merge pull request #16074 from civicrm/5.21
[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
00be9182 9 public function setUp() {
6a488035
TO
10 parent::setUp();
11
9099cab3 12 $this->quickCleanup(['civicrm_contact', 'civicrm_email']);
6a488035
TO
13 }
14
15 /**
100fef9d 16 * Add() method (create and update modes)
6a488035 17 */
00be9182 18 public function testAdd() {
f2040bc6 19 $contactId = $this->individualCreate();
6a488035 20
9099cab3
CW
21 $params = [];
22 $params = [
6a488035
TO
23 'email' => 'jane.doe@example.com',
24 'is_primary' => 1,
25 'location_type_id' => 1,
26 'contact_id' => $contactId,
9099cab3 27 ];
6a488035
TO
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
9099cab3
CW
37 $params = [];
38 $params = [
6a488035
TO
39 'id' => $emailId,
40 'contact_id' => $contactId,
41 'is_bulkmail' => 1,
42 'on_hold' => 1,
9099cab3 43 ];
6a488035
TO
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
93ac19cd 52 $this->contactDelete($contactId);
6a488035
TO
53 }
54
55 /**
100fef9d 56 * HoldEmail() method (set and reset on_hold condition)
6a488035 57 */
00be9182 58 public function testHoldEmail() {
f2040bc6 59 $contactId = $this->individualCreate();
6a488035 60
9099cab3 61 $params = [
6a488035
TO
62 'email' => 'jane.doe@example.com',
63 'is_primary' => 1,
64 'location_type_id' => 1,
65 'contact_id' => $contactId,
9099cab3 66 ];
6a488035
TO
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
f61d1b83 74 // Now call add() to update on_hold=1 ("On Hold Bounce") and check record state
9099cab3
CW
75 $params = [];
76 $params = [
6a488035
TO
77 'id' => $emailId,
78 'contact_id' => $contactId,
79 'on_hold' => 1,
9099cab3 80 ];
6a488035
TO
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
f61d1b83 98 // Now call add() to update on_hold=2 ("On Hold Opt-out") and check record state
9099cab3
CW
99 $params = [];
100 $params = [
f61d1b83
AS
101 'id' => $emailId,
102 'contact_id' => $contactId,
103 'on_hold' => 2,
9099cab3 104 ];
f61d1b83
AS
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.
9099cab3
CW
123 $params = [];
124 $params = [
6a488035
TO
125 'id' => $emailId,
126 'contact_id' => $contactId,
127 'on_hold' => 'null',
9099cab3 128 ];
6a488035
TO
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
93ac19cd 148 $this->contactDelete($contactId);
6a488035
TO
149 }
150
151 /**
100fef9d 152 * AllEmails() method - get all emails for our contact, with primary email first
6a488035 153 */
00be9182 154 public function testAllEmails() {
9099cab3 155 $contactParams = [
6a488035
TO
156 'first_name' => 'Alan',
157 'last_name' => 'Smith',
f2040bc6 158 'email' => 'alan.smith1@example.com',
9099cab3
CW
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 ];
6a488035 162
f2040bc6 163 $contactId = $this->individualCreate($contactParams);
6a488035
TO
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
93ac19cd 174 $this->contactDelete($contactId);
6a488035 175 }
96025800 176
669f45ef
SL
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
6a488035 195}