When syncing to UF record, use API instead of DAO to create email
[civicrm-core.git] / tests / phpunit / api / v3 / EmailTest.php
CommitLineData
6a488035 1<?php
b6708aeb 2/*
3 +--------------------------------------------------------------------+
2fe49090 4| CiviCRM version 5 |
b6708aeb 5+--------------------------------------------------------------------+
6b83d5bd 6| Copyright CiviCRM LLC (c) 2004-2019 |
b6708aeb 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+--------------------------------------------------------------------+
e70a7fc0 26 */
6a488035 27
e9479dcf
EM
28/**
29 * Class api_v3_EmailTest
acb109b7 30 * @group headless
e9479dcf 31 */
6a488035 32class api_v3_EmailTest extends CiviUnitTestCase {
6a488035
TO
33 protected $_contactID;
34 protected $_locationType;
35 protected $_entity;
36 protected $_params;
b7c9bc4c 37
00be9182 38 public function setUp() {
6a488035
TO
39 $this->_entity = 'Email';
40 parent::setUp();
881567d2
TO
41 $this->useTransaction(TRUE);
42
6a488035
TO
43 $this->_contactID = $this->organizationCreate(NULL);
44 $this->_locationType = $this->locationTypeCreate(NULL);
45 $this->_locationType2 = $this->locationTypeCreate(array(
5896d037
TO
46 'name' => 'New Location Type 2',
47 'vcard_name' => 'New Location Type 2',
48 'description' => 'Another Location Type',
49 'is_active' => 1,
50 ));
6a488035
TO
51 $this->_params = array(
52 'contact_id' => $this->_contactID,
53 'location_type_id' => $this->_locationType->id,
54 'email' => 'api@a-team.com',
55 'is_primary' => 1,
e5eb3437 56
6a488035
TO
57 //TODO email_type_id
58 );
59 }
60
2d932085
CW
61 /**
62 * @param int $version
63 * @dataProvider versionThreeAndFour
64 */
65 public function testCreateEmail($version) {
66 $this->_apiversion = $version;
6a488035
TO
67 $params = $this->_params;
68 //check there are no emails to start with
e5eb3437 69 $get = $this->callAPISuccess('email', 'get', array(
70 'location_type_id' => $this->_locationType->id,
71 ));
6a488035 72 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__);
6a488035 73
e5eb3437 74 $result = $this->callAPIAndDocument('email', 'create', $params, __FUNCTION__, __FILE__);
ba4a1892
TM
75 $this->assertEquals(1, $result['count']);
76 $this->assertNotNull($result['id']);
77 $this->assertNotNull($result['values'][$result['id']]['id']);
e5eb3437 78 $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id']));
6a488035 79 }
5896d037 80
2890c0f9
AS
81 /**
82 * If no location is specified when creating a new email, it should default to
83 * the LocationType default
84 *
85 * Only API v3
86 */
87 public function testCreateEmailDefaultLocation() {
88 $params = $this->_params;
89 unset($params['location_type_id']);
90 $result = $this->callAPIAndDocument('email', 'create', $params, __FUNCTION__, __FILE__);
91 $this->assertEquals(CRM_Core_BAO_LocationType::getDefault()->id, $result['values'][$result['id']]['location_type_id']);
92 $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id']));
93 }
94
8d7a9d07 95 /**
fe482240 96 * If a new email is set to is_primary the prev should no longer be.
6a488035
TO
97 *
98 * If is_primary is not set then it should become is_primary is no others exist
2d932085
CW
99 * @param int $version
100 * @dataProvider versionThreeAndFour
6a488035 101 */
2d932085
CW
102 public function testCreateEmailPrimaryHandlingChangeToPrimary($version) {
103 $this->_apiversion = $version;
6a488035
TO
104 $params = $this->_params;
105 unset($params['is_primary']);
e5eb3437 106 $email1 = $this->callAPISuccess('email', 'create', $params);
6a488035 107 //now we check & make sure it has been set to primary
e5eb3437 108 $expected = 1;
109 $check = $this->callAPISuccess('email', 'getcount', array(
39b959db
SL
110 'is_primary' => 1,
111 'id' => $email1['id'],
112 ),
e5eb3437 113 $expected
5896d037 114 );
6a488035 115 }
e5eb3437 116
2d932085
CW
117 /**
118 * @param int $version
119 * @dataProvider versionThreeAndFour
120 */
121 public function testCreateEmailPrimaryHandlingChangeExisting($version) {
122 $this->_apiversion = $version;
e5eb3437 123 $email1 = $this->callAPISuccess('email', 'create', $this->_params);
124 $email2 = $this->callAPISuccess('email', 'create', $this->_params);
125 $check = $this->callAPISuccess('email', 'getcount', array(
5896d037
TO
126 'is_primary' => 1,
127 'contact_id' => $this->_contactID,
128 ));
6a488035
TO
129 $this->assertEquals(1, $check);
130 }
131
2d932085
CW
132 /**
133 * @param int $version
134 * @dataProvider versionThreeAndFour
135 */
136 public function testCreateEmailWithoutEmail($version) {
137 $this->_apiversion = $version;
e5eb3437 138 $result = $this->callAPIFailure('Email', 'Create', array('contact_id' => 4));
ba4a1892
TM
139 $this->assertContains('missing', $result['error_message']);
140 $this->assertContains('email', $result['error_message']);
6a488035
TO
141 }
142
2d932085
CW
143 /**
144 * @param int $version
145 * @dataProvider versionThreeAndFour
146 */
147 public function testGetEmail($version) {
148 $this->_apiversion = $version;
e5eb3437 149 $result = $this->callAPISuccess('email', 'create', $this->_params);
150 $get = $this->callAPISuccess('email', 'create', $this->_params);
6a488035 151 $this->assertEquals($get['count'], 1);
e5eb3437 152 $get = $this->callAPISuccess('email', 'create', $this->_params + array('debug' => 1));
6a488035 153 $this->assertEquals($get['count'], 1);
e5eb3437 154 $get = $this->callAPISuccess('email', 'create', $this->_params + array('debug' => 1, 'action' => 'get'));
6a488035 155 $this->assertEquals($get['count'], 1);
e5eb3437 156 $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id']));
6a488035 157 }
5896d037 158
2d932085
CW
159 /**
160 * @param int $version
161 * @dataProvider versionThreeAndFour
162 */
163 public function testDeleteEmail($version) {
164 $this->_apiversion = $version;
6a488035
TO
165 $params = array(
166 'contact_id' => $this->_contactID,
167 'location_type_id' => $this->_locationType->id,
168 'email' => 'api@a-team.com',
169 'is_primary' => 1,
e5eb3437 170
6a488035
TO
171 //TODO email_type_id
172 );
173 //check there are no emails to start with
e5eb3437 174 $get = $this->callAPISuccess('email', 'get', array(
5896d037 175 'location_type_id' => $this->_locationType->id,
e5eb3437 176 ));
6a488035
TO
177 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__);
178
179 //create one
e5eb3437 180 $create = $this->callAPISuccess('email', 'create', $params);
6a488035 181
6c6e6187 182 $result = $this->callAPIAndDocument('email', 'delete', array('id' => $create['id']), __FUNCTION__, __FILE__);
ba4a1892 183 $this->assertEquals(1, $result['count']);
e5eb3437 184 $get = $this->callAPISuccess('email', 'get', array(
185 'location_type_id' => $this->_locationType->id,
186 ));
6a488035
TO
187 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__);
188 }
189
2d932085
CW
190 /**
191 * @param int $version
192 * @dataProvider versionThreeAndFour
193 */
194 public function testReplaceEmail($version) {
195 $this->_apiversion = $version;
6a488035 196 // check there are no emails to start with
e5eb3437 197 $get = $this->callAPISuccess('email', 'get', array(
5896d037
TO
198 'contact_id' => $this->_contactID,
199 ));
6a488035
TO
200 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__);
201
202 // initialize email list with three emails at loc #1 and two emails at loc #2
203 $replace1Params = array(
6a488035
TO
204 'contact_id' => $this->_contactID,
205 'values' => array(
206 array(
207 'location_type_id' => $this->_locationType->id,
208 'email' => '1-1@example.com',
209 'is_primary' => 1,
210 ),
211 array(
212 'location_type_id' => $this->_locationType->id,
213 'email' => '1-2@example.com',
214 'is_primary' => 0,
215 ),
216 array(
217 'location_type_id' => $this->_locationType->id,
218 'email' => '1-3@example.com',
219 'is_primary' => 0,
220 ),
221 array(
222 'location_type_id' => $this->_locationType2->id,
223 'email' => '2-1@example.com',
224 'is_primary' => 0,
225 ),
226 array(
227 'location_type_id' => $this->_locationType2->id,
228 'email' => '2-2@example.com',
229 'is_primary' => 0,
230 ),
231 ),
232 );
e5eb3437 233 $replace1 = $this->callAPIAndDocument('email', 'replace', $replace1Params, __FUNCTION__, __FILE__);
ba4a1892 234 $this->assertEquals(5, $replace1['count']);
6a488035
TO
235
236 // check emails at location #1 or #2
e5eb3437 237 $get = $this->callAPISuccess('email', 'get', array(
5896d037
TO
238 'contact_id' => $this->_contactID,
239 ));
6a488035
TO
240 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__);
241
242 // replace the subset of emails in location #1, but preserve location #2
243 $replace2Params = array(
6a488035
TO
244 'contact_id' => $this->_contactID,
245 'location_type_id' => $this->_locationType->id,
246 'values' => array(
247 array(
248 'email' => '1-4@example.com',
249 'is_primary' => 1,
250 ),
251 ),
252 );
e5eb3437 253 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
ba4a1892 254 $this->assertEquals(1, $replace2['count']);
6a488035
TO
255
256 // check emails at location #1 -- all three replaced by one
e5eb3437 257 $get = $this->callAPISuccess('email', 'get', array(
258 'contact_id' => $this->_contactID,
259 'location_type_id' => $this->_locationType->id,
260 ));
6a488035
TO
261 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__);
262
263 // check emails at location #2 -- preserve the original two
e5eb3437 264 $get = $this->callAPISuccess('email', 'get', array(
265 'contact_id' => $this->_contactID,
266 'location_type_id' => $this->_locationType2->id,
267 ));
268
6a488035
TO
269 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__);
270
271 // replace the set of emails with an empty set
272 $replace3Params = array(
6a488035
TO
273 'contact_id' => $this->_contactID,
274 'values' => array(),
275 );
e5eb3437 276 $replace3 = $this->callAPISuccess('email', 'replace', $replace3Params);
ba4a1892 277 $this->assertEquals(0, $replace3['count']);
6a488035
TO
278
279 // check emails
e5eb3437 280 $get = $this->callAPISuccess('email', 'get', array(
281
5896d037
TO
282 'contact_id' => $this->_contactID,
283 ));
a15773db 284 $this->assertAPISuccess($get);
6a488035
TO
285 $this->assertEquals(0, $get['count'], 'Incorrect email count at ' . __LINE__);
286 }
287
2d932085
CW
288 /**
289 * @param int $version
290 * @dataProvider versionThreeAndFour
291 */
292 public function testReplaceEmailsInChain($version) {
293 $this->_apiversion = $version;
6a488035 294 // check there are no emails to start with
e5eb3437 295 $get = $this->callAPISuccess('email', 'get', array(
296
5896d037
TO
297 'contact_id' => $this->_contactID,
298 ));
a15773db 299 $this->assertAPISuccess($get);
6a488035 300 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__);
5c49fee0 301 $description = "Demonstrates use of Replace in a nested API call.";
6a488035
TO
302 $subfile = "NestedReplaceEmail";
303 // initialize email list with three emails at loc #1 and two emails at loc #2
304 $getReplace1Params = array(
e5eb3437 305
6a488035
TO
306 'id' => $this->_contactID,
307 'api.email.replace' => array(
308 'values' => array(
309 array(
310 'location_type_id' => $this->_locationType->id,
311 'email' => '1-1@example.com',
312 'is_primary' => 1,
313 ),
314 array(
315 'location_type_id' => $this->_locationType->id,
316 'email' => '1-2@example.com',
317 'is_primary' => 0,
318 ),
319 array(
320 'location_type_id' => $this->_locationType->id,
321 'email' => '1-3@example.com',
322 'is_primary' => 0,
323 ),
324 array(
325 'location_type_id' => $this->_locationType2->id,
326 'email' => '2-1@example.com',
327 'is_primary' => 0,
328 ),
329 array(
330 'location_type_id' => $this->_locationType2->id,
331 'email' => '2-2@example.com',
332 'is_primary' => 0,
333 ),
334 ),
335 ),
336 );
e5eb3437 337 $getReplace1 = $this->callAPIAndDocument('contact', 'get', $getReplace1Params, __FUNCTION__, __FILE__, $description, $subfile);
ba4a1892 338 $this->assertEquals(5, $getReplace1['values'][$this->_contactID]['api.email.replace']['count']);
6a488035
TO
339
340 // check emails at location #1 or #2
e5eb3437 341 $get = $this->callAPISuccess('email', 'get', array(
5896d037
TO
342 'contact_id' => $this->_contactID,
343 ));
6a488035
TO
344 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__);
345
346 // replace the subset of emails in location #1, but preserve location #2
347 $getReplace2Params = array(
6a488035
TO
348 'id' => $this->_contactID,
349 'api.email.replace' => array(
350 'location_type_id' => $this->_locationType->id,
351 'values' => array(
352 array(
353 'email' => '1-4@example.com',
354 'is_primary' => 1,
355 ),
356 ),
357 ),
358 );
e5eb3437 359 $getReplace2 = $this->callAPISuccess('contact', 'get', $getReplace2Params);
ba4a1892
TM
360 $this->assertEquals(0, $getReplace2['values'][$this->_contactID]['api.email.replace']['is_error']);
361 $this->assertEquals(1, $getReplace2['values'][$this->_contactID]['api.email.replace']['count']);
6a488035
TO
362
363 // check emails at location #1 -- all three replaced by one
e5eb3437 364 $get = $this->callAPISuccess('email', 'get', array(
365 'contact_id' => $this->_contactID,
366 'location_type_id' => $this->_locationType->id,
367 ));
6a488035 368
6a488035
TO
369 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__);
370
371 // check emails at location #2 -- preserve the original two
e5eb3437 372 $get = $this->callAPISuccess('email', 'get', array(
373 'contact_id' => $this->_contactID,
374 'location_type_id' => $this->_locationType2->id,
375 ));
6a488035
TO
376 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__);
377 }
378
2d932085
CW
379 /**
380 * @param int $version
381 * @dataProvider versionThreeAndFour
382 */
383 public function testReplaceEmailWithId($version) {
384 $this->_apiversion = $version;
6a488035 385 // check there are no emails to start with
e5eb3437 386 $get = $this->callAPISuccess('email', 'get', array(
5896d037
TO
387 'contact_id' => $this->_contactID,
388 ));
6a488035
TO
389 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__);
390
391 // initialize email address
392 $replace1Params = array(
6a488035
TO
393 'contact_id' => $this->_contactID,
394 'values' => array(
395 array(
396 'location_type_id' => $this->_locationType->id,
397 'email' => '1-1@example.com',
398 'is_primary' => 1,
399 'on_hold' => 1,
400 ),
401 ),
402 );
e5eb3437 403 $replace1 = $this->callAPISuccess('email', 'replace', $replace1Params);
ba4a1892 404 $this->assertEquals(1, $replace1['count']);
6a488035
TO
405
406 $keys = array_keys($replace1['values']);
407 $emailID = array_shift($keys);
408
409 // update the email address, but preserve any other fields
410 $replace2Params = array(
6a488035
TO
411 'contact_id' => $this->_contactID,
412 'values' => array(
413 array(
414 'id' => $emailID,
415 'email' => '1-2@example.com',
416 ),
417 ),
418 );
e5eb3437 419 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
ba4a1892 420 $this->assertEquals(1, $replace2['count']);
6a488035
TO
421
422 // ensure the 'email' was updated while other fields were preserved
e5eb3437 423 $get = $this->callAPISuccess('email', 'get', array(
424 'contact_id' => $this->_contactID,
425 'location_type_id' => $this->_locationType->id,
426 ));
427
6a488035 428 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__);
ba4a1892
TM
429 $this->assertEquals(1, $get['values'][$emailID]['is_primary']);
430 $this->assertEquals(1, $get['values'][$emailID]['on_hold']);
431 $this->assertEquals('1-2@example.com', $get['values'][$emailID]['email']);
6a488035 432 }
96025800 433
5d32acf9 434 public function testEmailOnHold() {
435 $params = array();
436 $params_change = array();
437 $params = array(
438 'contact_id' => $this->_contactID,
439 'email' => 'api@a-team.com',
440 'on_hold' => '2',
441 );
442 $result = $this->callAPIAndDocument('email', 'create', $params, __FUNCTION__, __FILE__);
443 $this->assertEquals(1, $result['count']);
444 $this->assertNotNull($result['id']);
445 $this->assertNotNull($result['values'][$result['id']]['id']);
446 $this->assertEquals(2, $result['values'][$result['id']]['on_hold']);
447 $this->assertEquals(date('Y-m-d H:i'), date('Y-m-d H:i', strtotime($result['values'][$result['id']]['hold_date'])));
448
449 // set on_hold is '0'
450 // if isMultipleBulkMail is active, the value in On-hold select is string
451 $params_change = array(
452 'id' => $result['id'],
453 'contact_id' => $this->_contactID,
454 'email' => 'api@a-team.com',
455 'is_primary' => 1,
456 'on_hold' => '0',
457 );
458 $result_change = $this->callAPISuccess('email', 'create', $params_change + array('action' => 'get'));
459 $this->assertEquals(1, $result_change['count']);
460 $this->assertEquals($result['id'], $result_change['id']);
461 $this->assertEmpty($result_change['values'][$result_change['id']]['on_hold']);
462 $this->assertEquals(date('Y-m-d H:i'), date('Y-m-d H:i', strtotime($result_change['values'][$result_change['id']]['reset_date'])));
463 $this->assertEmpty($result_change['values'][$result_change['id']]['hold_date']);
464
465 $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id']));
466 }
467
6a488035 468}