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