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