3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Class api_v3_EmailTest
32 class api_v3_EmailTest
extends CiviUnitTestCase
{
33 protected $_contactID;
34 protected $_locationType;
38 public function setUp() {
39 $this->_entity
= 'Email';
41 $this->useTransaction(TRUE);
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',
52 'contact_id' => $this->_contactID
,
53 'location_type_id' => $this->_locationType
->id
,
54 'email' => 'api@a-team.com',
63 * @dataProvider versionThreeAndFour
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
,
72 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
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']]);
82 * If no location is specified when creating a new email, it should default to
83 * the LocationType default
86 * @dataProvider versionThreeAndFour
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']]);
98 * If a new email is set to is_primary the prev should no longer be.
100 * If is_primary is not set then it should become is_primary is no others exist
101 * @param int $version
102 * @dataProvider versionThreeAndFour
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
111 $check = $this->callAPISuccess('email', 'getcount', [
113 'id' => $email1['id'],
120 * @param int $version
121 * @dataProvider versionThreeAndFour
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', [
129 'contact_id' => $this->_contactID
,
131 $this->assertEquals(1, $check);
135 * @param int $version
136 * @dataProvider versionThreeAndFour
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']);
146 * @param int $version
147 * @dataProvider versionThreeAndFour
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']]);
162 * @param int $version
163 * @dataProvider versionThreeAndFour
165 public function testDeleteEmail($version) {
166 $this->_apiversion
= $version;
168 'contact_id' => $this->_contactID
,
169 'location_type_id' => $this->_locationType
->id
,
170 'email' => 'api@a-team.com',
175 //check there are no emails to start with
176 $get = $this->callAPISuccess('email', 'get', [
177 'location_type_id' => $this->_locationType
->id
,
179 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
182 $create = $this->callAPISuccess('email', 'create', $params);
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
,
189 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
193 * @param int $version
194 * @dataProvider versionThreeAndFour
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
,
202 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
204 // initialize email list with three emails at loc #1 and two emails at loc #2
206 'contact_id' => $this->_contactID
,
209 'location_type_id' => $this->_locationType
->id
,
210 'email' => '1-1@example.com',
214 'location_type_id' => $this->_locationType
->id
,
215 'email' => '1-2@example.com',
219 'location_type_id' => $this->_locationType
->id
,
220 'email' => '1-3@example.com',
224 'location_type_id' => $this->_locationType2
->id
,
225 'email' => '2-1@example.com',
229 'location_type_id' => $this->_locationType2
->id
,
230 'email' => '2-2@example.com',
235 $replace1 = $this->callAPIAndDocument('email', 'replace', $replace1Params, __FUNCTION__
, __FILE__
);
236 $this->assertEquals(5, $replace1['count']);
238 // check emails at location #1 or #2
239 $get = $this->callAPISuccess('email', 'get', [
240 'contact_id' => $this->_contactID
,
242 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__
);
244 // replace the subset of emails in location #1, but preserve location #2
246 'contact_id' => $this->_contactID
,
247 'location_type_id' => $this->_locationType
->id
,
250 'email' => '1-4@example.com',
255 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
256 $this->assertEquals(1, $replace2['count']);
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
,
263 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
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
,
271 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__
);
273 // replace the set of emails with an empty set
275 'contact_id' => $this->_contactID
,
278 $replace3 = $this->callAPISuccess('email', 'replace', $replace3Params);
279 $this->assertEquals(0, $replace3['count']);
282 $get = $this->callAPISuccess('email', 'get', [
284 'contact_id' => $this->_contactID
,
286 $this->assertAPISuccess($get);
287 $this->assertEquals(0, $get['count'], 'Incorrect email count at ' . __LINE__
);
291 * @param int $version
292 * @dataProvider versionThreeAndFour
294 public function testReplaceEmailsInChain($version) {
295 $this->_apiversion
= $version;
296 // check there are no emails to start with
297 $get = $this->callAPISuccess('email', 'get', [
299 'contact_id' => $this->_contactID
,
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 = [
308 'id' => $this->_contactID
,
309 'api.email.replace' => [
312 'location_type_id' => $this->_locationType
->id
,
313 'email' => '1-1@example.com',
317 'location_type_id' => $this->_locationType
->id
,
318 'email' => '1-2@example.com',
322 'location_type_id' => $this->_locationType
->id
,
323 'email' => '1-3@example.com',
327 'location_type_id' => $this->_locationType2
->id
,
328 'email' => '2-1@example.com',
332 'location_type_id' => $this->_locationType2
->id
,
333 'email' => '2-2@example.com',
339 $getReplace1 = $this->callAPIAndDocument('contact', 'get', $getReplace1Params, __FUNCTION__
, __FILE__
, $description, $subfile);
340 $this->assertEquals(5, $getReplace1['values'][$this->_contactID
]['api.email.replace']['count']);
342 // check emails at location #1 or #2
343 $get = $this->callAPISuccess('email', 'get', [
344 'contact_id' => $this->_contactID
,
346 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__
);
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
,
355 'email' => '1-4@example.com',
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']);
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
,
371 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
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
,
378 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__
);
382 * @param int $version
383 * @dataProvider versionThreeAndFour
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
,
391 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
393 // initialize email address
395 'contact_id' => $this->_contactID
,
398 'location_type_id' => $this->_locationType
->id
,
399 'email' => '1-1@example.com',
405 $replace1 = $this->callAPISuccess('email', 'replace', $replace1Params);
406 $this->assertEquals(1, $replace1['count']);
408 $keys = array_keys($replace1['values']);
409 $emailID = array_shift($keys);
411 // update the email address, but preserve any other fields
413 'contact_id' => $this->_contactID
,
417 'email' => '1-2@example.com',
421 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
422 $this->assertEquals(1, $replace2['count']);
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
,
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']);
436 public function testEmailOnHold() {
440 'contact_id' => $this->_contactID
,
441 'email' => 'api@a-team.com',
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'])));
451 // set on_hold is '0'
452 // if isMultipleBulkMail is active, the value in On-hold select is string
454 'id' => $result['id'],
455 'contact_id' => $this->_contactID
,
456 'email' => 'api@a-team.com',
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']);
467 $delresult = $this->callAPISuccess('email', 'delete', ['id' => $result['id']]);