Merge pull request #10946 from mattwire/CRM-21037_activity_sendsms_unittests
[civicrm-core.git] / tests / phpunit / api / v3 / EmailTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Class api_v3_EmailTest
30 * @group headless
31 */
32 class api_v3_EmailTest extends CiviUnitTestCase {
33 protected $_apiversion;
34 protected $_contactID;
35 protected $_locationType;
36 protected $_entity;
37 protected $_params;
38
39 public function setUp() {
40 $this->_apiversion = 3;
41 $this->_entity = 'Email';
42 parent::setUp();
43 $this->useTransaction(TRUE);
44
45 $this->_contactID = $this->organizationCreate(NULL);
46 $this->_locationType = $this->locationTypeCreate(NULL);
47 $this->_locationType2 = $this->locationTypeCreate(array(
48 'name' => 'New Location Type 2',
49 'vcard_name' => 'New Location Type 2',
50 'description' => 'Another Location Type',
51 'is_active' => 1,
52 ));
53 $this->_params = array(
54 'contact_id' => $this->_contactID,
55 'location_type_id' => $this->_locationType->id,
56 'email' => 'api@a-team.com',
57 'is_primary' => 1,
58
59 //TODO email_type_id
60 );
61 }
62
63 public function testCreateEmail() {
64 $params = $this->_params;
65 //check there are no emails to start with
66 $get = $this->callAPISuccess('email', 'get', array(
67 'location_type_id' => $this->_locationType->id,
68 ));
69 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__);
70
71 $result = $this->callAPIAndDocument('email', 'create', $params, __FUNCTION__, __FILE__);
72 $this->assertEquals(1, $result['count']);
73 $this->assertNotNull($result['id']);
74 $this->assertNotNull($result['values'][$result['id']]['id']);
75 $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id']));
76 }
77
78 /**
79 * If a new email is set to is_primary the prev should no longer be.
80 *
81 * If is_primary is not set then it should become is_primary is no others exist
82 */
83 public function testCreateEmailPrimaryHandlingChangeToPrimary() {
84 $params = $this->_params;
85 unset($params['is_primary']);
86 $email1 = $this->callAPISuccess('email', 'create', $params);
87 //now we check & make sure it has been set to primary
88 $expected = 1;
89 $check = $this->callAPISuccess('email', 'getcount', array(
90 'is_primary' => 1,
91 'id' => $email1['id'],
92 ),
93 $expected
94 );
95 }
96
97 public function testCreateEmailPrimaryHandlingChangeExisting() {
98 $email1 = $this->callAPISuccess('email', 'create', $this->_params);
99 $email2 = $this->callAPISuccess('email', 'create', $this->_params);
100 $check = $this->callAPISuccess('email', 'getcount', array(
101 'is_primary' => 1,
102 'contact_id' => $this->_contactID,
103 ));
104 $this->assertEquals(1, $check);
105 }
106
107 public function testCreateEmailWithoutEmail() {
108 $result = $this->callAPIFailure('Email', 'Create', array('contact_id' => 4));
109 $this->assertContains('missing', $result['error_message']);
110 $this->assertContains('email', $result['error_message']);
111 }
112
113 public function testGetEmail() {
114 $result = $this->callAPISuccess('email', 'create', $this->_params);
115 $get = $this->callAPISuccess('email', 'create', $this->_params);
116 $this->assertEquals($get['count'], 1);
117 $get = $this->callAPISuccess('email', 'create', $this->_params + array('debug' => 1));
118 $this->assertEquals($get['count'], 1);
119 $get = $this->callAPISuccess('email', 'create', $this->_params + array('debug' => 1, 'action' => 'get'));
120 $this->assertEquals($get['count'], 1);
121 $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id']));
122 }
123
124 public function testDeleteEmail() {
125 $params = array(
126 'contact_id' => $this->_contactID,
127 'location_type_id' => $this->_locationType->id,
128 'email' => 'api@a-team.com',
129 'is_primary' => 1,
130
131 //TODO email_type_id
132 );
133 //check there are no emails to start with
134 $get = $this->callAPISuccess('email', 'get', array(
135 'location_type_id' => $this->_locationType->id,
136 ));
137 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__);
138
139 //create one
140 $create = $this->callAPISuccess('email', 'create', $params);
141
142 $result = $this->callAPIAndDocument('email', 'delete', array('id' => $create['id']), __FUNCTION__, __FILE__);
143 $this->assertEquals(1, $result['count']);
144 $get = $this->callAPISuccess('email', 'get', array(
145 'location_type_id' => $this->_locationType->id,
146 ));
147 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__);
148 }
149
150 public function testReplaceEmail() {
151 // check there are no emails to start with
152 $get = $this->callAPISuccess('email', 'get', array(
153 'contact_id' => $this->_contactID,
154 ));
155 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__);
156
157 // initialize email list with three emails at loc #1 and two emails at loc #2
158 $replace1Params = array(
159 'contact_id' => $this->_contactID,
160 'values' => array(
161 array(
162 'location_type_id' => $this->_locationType->id,
163 'email' => '1-1@example.com',
164 'is_primary' => 1,
165 ),
166 array(
167 'location_type_id' => $this->_locationType->id,
168 'email' => '1-2@example.com',
169 'is_primary' => 0,
170 ),
171 array(
172 'location_type_id' => $this->_locationType->id,
173 'email' => '1-3@example.com',
174 'is_primary' => 0,
175 ),
176 array(
177 'location_type_id' => $this->_locationType2->id,
178 'email' => '2-1@example.com',
179 'is_primary' => 0,
180 ),
181 array(
182 'location_type_id' => $this->_locationType2->id,
183 'email' => '2-2@example.com',
184 'is_primary' => 0,
185 ),
186 ),
187 );
188 $replace1 = $this->callAPIAndDocument('email', 'replace', $replace1Params, __FUNCTION__, __FILE__);
189 $this->assertEquals(5, $replace1['count']);
190
191 // check emails at location #1 or #2
192 $get = $this->callAPISuccess('email', 'get', array(
193 'contact_id' => $this->_contactID,
194 ));
195 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__);
196
197 // replace the subset of emails in location #1, but preserve location #2
198 $replace2Params = array(
199 'contact_id' => $this->_contactID,
200 'location_type_id' => $this->_locationType->id,
201 'values' => array(
202 array(
203 'email' => '1-4@example.com',
204 'is_primary' => 1,
205 ),
206 ),
207 );
208 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
209 $this->assertEquals(1, $replace2['count']);
210
211 // check emails at location #1 -- all three replaced by one
212 $get = $this->callAPISuccess('email', 'get', array(
213 'contact_id' => $this->_contactID,
214 'location_type_id' => $this->_locationType->id,
215 ));
216 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__);
217
218 // check emails at location #2 -- preserve the original two
219 $get = $this->callAPISuccess('email', 'get', array(
220 'contact_id' => $this->_contactID,
221 'location_type_id' => $this->_locationType2->id,
222 ));
223
224 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__);
225
226 // replace the set of emails with an empty set
227 $replace3Params = array(
228 'contact_id' => $this->_contactID,
229 'values' => array(),
230 );
231 $replace3 = $this->callAPISuccess('email', 'replace', $replace3Params);
232 $this->assertEquals(0, $replace3['count']);
233
234 // check emails
235 $get = $this->callAPISuccess('email', 'get', array(
236
237 'contact_id' => $this->_contactID,
238 ));
239 $this->assertAPISuccess($get);
240 $this->assertEquals(0, $get['count'], 'Incorrect email count at ' . __LINE__);
241 }
242
243 public function testReplaceEmailsInChain() {
244 // check there are no emails to start with
245 $get = $this->callAPISuccess('email', 'get', array(
246
247 'contact_id' => $this->_contactID,
248 ));
249 $this->assertAPISuccess($get);
250 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__);
251 $description = "Demonstrates use of Replace in a nested API call.";
252 $subfile = "NestedReplaceEmail";
253 // initialize email list with three emails at loc #1 and two emails at loc #2
254 $getReplace1Params = array(
255
256 'id' => $this->_contactID,
257 'api.email.replace' => array(
258 'values' => array(
259 array(
260 'location_type_id' => $this->_locationType->id,
261 'email' => '1-1@example.com',
262 'is_primary' => 1,
263 ),
264 array(
265 'location_type_id' => $this->_locationType->id,
266 'email' => '1-2@example.com',
267 'is_primary' => 0,
268 ),
269 array(
270 'location_type_id' => $this->_locationType->id,
271 'email' => '1-3@example.com',
272 'is_primary' => 0,
273 ),
274 array(
275 'location_type_id' => $this->_locationType2->id,
276 'email' => '2-1@example.com',
277 'is_primary' => 0,
278 ),
279 array(
280 'location_type_id' => $this->_locationType2->id,
281 'email' => '2-2@example.com',
282 'is_primary' => 0,
283 ),
284 ),
285 ),
286 );
287 $getReplace1 = $this->callAPIAndDocument('contact', 'get', $getReplace1Params, __FUNCTION__, __FILE__, $description, $subfile);
288 $this->assertEquals(5, $getReplace1['values'][$this->_contactID]['api.email.replace']['count']);
289
290 // check emails at location #1 or #2
291 $get = $this->callAPISuccess('email', 'get', array(
292 'contact_id' => $this->_contactID,
293 ));
294 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__);
295
296 // replace the subset of emails in location #1, but preserve location #2
297 $getReplace2Params = array(
298 'id' => $this->_contactID,
299 'api.email.replace' => array(
300 'location_type_id' => $this->_locationType->id,
301 'values' => array(
302 array(
303 'email' => '1-4@example.com',
304 'is_primary' => 1,
305 ),
306 ),
307 ),
308 );
309 $getReplace2 = $this->callAPISuccess('contact', 'get', $getReplace2Params);
310 $this->assertEquals(0, $getReplace2['values'][$this->_contactID]['api.email.replace']['is_error']);
311 $this->assertEquals(1, $getReplace2['values'][$this->_contactID]['api.email.replace']['count']);
312
313 // check emails at location #1 -- all three replaced by one
314 $get = $this->callAPISuccess('email', 'get', array(
315 'contact_id' => $this->_contactID,
316 'location_type_id' => $this->_locationType->id,
317 ));
318
319 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__);
320
321 // check emails at location #2 -- preserve the original two
322 $get = $this->callAPISuccess('email', 'get', array(
323 'contact_id' => $this->_contactID,
324 'location_type_id' => $this->_locationType2->id,
325 ));
326 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__);
327 }
328
329 public function testReplaceEmailWithId() {
330 // check there are no emails to start with
331 $get = $this->callAPISuccess('email', 'get', array(
332 'contact_id' => $this->_contactID,
333 ));
334 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__);
335
336 // initialize email address
337 $replace1Params = array(
338 'contact_id' => $this->_contactID,
339 'values' => array(
340 array(
341 'location_type_id' => $this->_locationType->id,
342 'email' => '1-1@example.com',
343 'is_primary' => 1,
344 'on_hold' => 1,
345 ),
346 ),
347 );
348 $replace1 = $this->callAPISuccess('email', 'replace', $replace1Params);
349 $this->assertEquals(1, $replace1['count']);
350
351 $keys = array_keys($replace1['values']);
352 $emailID = array_shift($keys);
353
354 // update the email address, but preserve any other fields
355 $replace2Params = array(
356 'contact_id' => $this->_contactID,
357 'values' => array(
358 array(
359 'id' => $emailID,
360 'email' => '1-2@example.com',
361 ),
362 ),
363 );
364 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
365 $this->assertEquals(1, $replace2['count']);
366
367 // ensure the 'email' was updated while other fields were preserved
368 $get = $this->callAPISuccess('email', 'get', array(
369 'contact_id' => $this->_contactID,
370 'location_type_id' => $this->_locationType->id,
371 ));
372
373 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__);
374 $this->assertEquals(1, $get['values'][$emailID]['is_primary']);
375 $this->assertEquals(1, $get['values'][$emailID]['on_hold']);
376 $this->assertEquals('1-2@example.com', $get['values'][$emailID]['email']);
377 }
378
379 public function testEmailOnHold() {
380 $params = array();
381 $params_change = array();
382 $params = array(
383 'contact_id' => $this->_contactID,
384 'email' => 'api@a-team.com',
385 'on_hold' => '2',
386 );
387 $result = $this->callAPIAndDocument('email', 'create', $params, __FUNCTION__, __FILE__);
388 $this->assertEquals(1, $result['count']);
389 $this->assertNotNull($result['id']);
390 $this->assertNotNull($result['values'][$result['id']]['id']);
391 $this->assertEquals(2, $result['values'][$result['id']]['on_hold']);
392 $this->assertEquals(date('Y-m-d H:i'), date('Y-m-d H:i', strtotime($result['values'][$result['id']]['hold_date'])));
393
394 // set on_hold is '0'
395 // if isMultipleBulkMail is active, the value in On-hold select is string
396 $params_change = array(
397 'id' => $result['id'],
398 'contact_id' => $this->_contactID,
399 'email' => 'api@a-team.com',
400 'is_primary' => 1,
401 'on_hold' => '0',
402 );
403 $result_change = $this->callAPISuccess('email', 'create', $params_change + array('action' => 'get'));
404 $this->assertEquals(1, $result_change['count']);
405 $this->assertEquals($result['id'], $result_change['id']);
406 $this->assertEmpty($result_change['values'][$result_change['id']]['on_hold']);
407 $this->assertEquals(date('Y-m-d H:i'), date('Y-m-d H:i', strtotime($result_change['values'][$result_change['id']]['reset_date'])));
408 $this->assertEmpty($result_change['values'][$result_change['id']]['hold_date']);
409
410 $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id']));
411 }
412
413 }