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