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