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 $_apiversion;
34 protected $_contactID;
35 protected $_locationType;
39 public function setUp() {
40 $this->_apiversion
= 3;
41 $this->_entity
= 'Email';
43 $this->useTransaction(TRUE);
45 $this->_contactID
= $this->organizationCreate(NULL);
46 $this->_locationType
= $this->locationTypeCreate(NULL);
47 $this->_locationType2
= $this->locationTypeCreate(array(
48 'name' => 'New Location Type 2',
49 'vcard_name' => 'New Location Type 2',
50 'description' => 'Another Location Type',
53 $this->_params
= array(
54 'contact_id' => $this->_contactID
,
55 'location_type_id' => $this->_locationType
->id
,
56 'email' => 'api@a-team.com',
63 public function testCreateEmail() {
64 $params = $this->_params
;
65 //check there are no emails to start with
66 $get = $this->callAPISuccess('email', 'get', array(
67 'location_type_id' => $this->_locationType
->id
,
69 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
71 $result = $this->callAPIAndDocument('email', 'create', $params, __FUNCTION__
, __FILE__
);
72 $this->assertEquals(1, $result['count']);
73 $this->assertNotNull($result['id']);
74 $this->assertNotNull($result['values'][$result['id']]['id']);
75 $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id']));
79 * If a new email is set to is_primary the prev should no longer be.
81 * If is_primary is not set then it should become is_primary is no others exist
83 public function testCreateEmailPrimaryHandlingChangeToPrimary() {
84 $params = $this->_params
;
85 unset($params['is_primary']);
86 $email1 = $this->callAPISuccess('email', 'create', $params);
87 //now we check & make sure it has been set to primary
89 $check = $this->callAPISuccess('email', 'getcount', array(
91 'id' => $email1['id'],
97 public function testCreateEmailPrimaryHandlingChangeExisting() {
98 $email1 = $this->callAPISuccess('email', 'create', $this->_params
);
99 $email2 = $this->callAPISuccess('email', 'create', $this->_params
);
100 $check = $this->callAPISuccess('email', 'getcount', array(
102 'contact_id' => $this->_contactID
,
104 $this->assertEquals(1, $check);
107 public function testCreateEmailWithoutEmail() {
108 $result = $this->callAPIFailure('Email', 'Create', array('contact_id' => 4));
109 $this->assertContains('missing', $result['error_message']);
110 $this->assertContains('email', $result['error_message']);
113 public function testGetEmail() {
114 $result = $this->callAPISuccess('email', 'create', $this->_params
);
115 $get = $this->callAPISuccess('email', 'create', $this->_params
);
116 $this->assertEquals($get['count'], 1);
117 $get = $this->callAPISuccess('email', 'create', $this->_params +
array('debug' => 1));
118 $this->assertEquals($get['count'], 1);
119 $get = $this->callAPISuccess('email', 'create', $this->_params +
array('debug' => 1, 'action' => 'get'));
120 $this->assertEquals($get['count'], 1);
121 $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id']));
124 public function testDeleteEmail() {
126 'contact_id' => $this->_contactID
,
127 'location_type_id' => $this->_locationType
->id
,
128 'email' => 'api@a-team.com',
133 //check there are no emails to start with
134 $get = $this->callAPISuccess('email', 'get', array(
135 'location_type_id' => $this->_locationType
->id
,
137 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
140 $create = $this->callAPISuccess('email', 'create', $params);
142 $result = $this->callAPIAndDocument('email', 'delete', array('id' => $create['id']), __FUNCTION__
, __FILE__
);
143 $this->assertEquals(1, $result['count']);
144 $get = $this->callAPISuccess('email', 'get', array(
145 'location_type_id' => $this->_locationType
->id
,
147 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
150 public function testReplaceEmail() {
151 // check there are no emails to start with
152 $get = $this->callAPISuccess('email', 'get', array(
153 'contact_id' => $this->_contactID
,
155 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
157 // initialize email list with three emails at loc #1 and two emails at loc #2
158 $replace1Params = array(
159 'contact_id' => $this->_contactID
,
162 'location_type_id' => $this->_locationType
->id
,
163 'email' => '1-1@example.com',
167 'location_type_id' => $this->_locationType
->id
,
168 'email' => '1-2@example.com',
172 'location_type_id' => $this->_locationType
->id
,
173 'email' => '1-3@example.com',
177 'location_type_id' => $this->_locationType2
->id
,
178 'email' => '2-1@example.com',
182 'location_type_id' => $this->_locationType2
->id
,
183 'email' => '2-2@example.com',
188 $replace1 = $this->callAPIAndDocument('email', 'replace', $replace1Params, __FUNCTION__
, __FILE__
);
189 $this->assertEquals(5, $replace1['count']);
191 // check emails at location #1 or #2
192 $get = $this->callAPISuccess('email', 'get', array(
193 'contact_id' => $this->_contactID
,
195 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__
);
197 // replace the subset of emails in location #1, but preserve location #2
198 $replace2Params = array(
199 'contact_id' => $this->_contactID
,
200 'location_type_id' => $this->_locationType
->id
,
203 'email' => '1-4@example.com',
208 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
209 $this->assertEquals(1, $replace2['count']);
211 // check emails at location #1 -- all three replaced by one
212 $get = $this->callAPISuccess('email', 'get', array(
213 'contact_id' => $this->_contactID
,
214 'location_type_id' => $this->_locationType
->id
,
216 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
218 // check emails at location #2 -- preserve the original two
219 $get = $this->callAPISuccess('email', 'get', array(
220 'contact_id' => $this->_contactID
,
221 'location_type_id' => $this->_locationType2
->id
,
224 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__
);
226 // replace the set of emails with an empty set
227 $replace3Params = array(
228 'contact_id' => $this->_contactID
,
231 $replace3 = $this->callAPISuccess('email', 'replace', $replace3Params);
232 $this->assertEquals(0, $replace3['count']);
235 $get = $this->callAPISuccess('email', 'get', array(
237 'contact_id' => $this->_contactID
,
239 $this->assertAPISuccess($get);
240 $this->assertEquals(0, $get['count'], 'Incorrect email count at ' . __LINE__
);
243 public function testReplaceEmailsInChain() {
244 // check there are no emails to start with
245 $get = $this->callAPISuccess('email', 'get', array(
247 'contact_id' => $this->_contactID
,
249 $this->assertAPISuccess($get);
250 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
251 $description = "Demonstrates use of Replace in a nested API call.";
252 $subfile = "NestedReplaceEmail";
253 // initialize email list with three emails at loc #1 and two emails at loc #2
254 $getReplace1Params = array(
256 'id' => $this->_contactID
,
257 'api.email.replace' => array(
260 'location_type_id' => $this->_locationType
->id
,
261 'email' => '1-1@example.com',
265 'location_type_id' => $this->_locationType
->id
,
266 'email' => '1-2@example.com',
270 'location_type_id' => $this->_locationType
->id
,
271 'email' => '1-3@example.com',
275 'location_type_id' => $this->_locationType2
->id
,
276 'email' => '2-1@example.com',
280 'location_type_id' => $this->_locationType2
->id
,
281 'email' => '2-2@example.com',
287 $getReplace1 = $this->callAPIAndDocument('contact', 'get', $getReplace1Params, __FUNCTION__
, __FILE__
, $description, $subfile);
288 $this->assertEquals(5, $getReplace1['values'][$this->_contactID
]['api.email.replace']['count']);
290 // check emails at location #1 or #2
291 $get = $this->callAPISuccess('email', 'get', array(
292 'contact_id' => $this->_contactID
,
294 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__
);
296 // replace the subset of emails in location #1, but preserve location #2
297 $getReplace2Params = array(
298 'id' => $this->_contactID
,
299 'api.email.replace' => array(
300 'location_type_id' => $this->_locationType
->id
,
303 'email' => '1-4@example.com',
309 $getReplace2 = $this->callAPISuccess('contact', 'get', $getReplace2Params);
310 $this->assertEquals(0, $getReplace2['values'][$this->_contactID
]['api.email.replace']['is_error']);
311 $this->assertEquals(1, $getReplace2['values'][$this->_contactID
]['api.email.replace']['count']);
313 // check emails at location #1 -- all three replaced by one
314 $get = $this->callAPISuccess('email', 'get', array(
315 'contact_id' => $this->_contactID
,
316 'location_type_id' => $this->_locationType
->id
,
319 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
321 // check emails at location #2 -- preserve the original two
322 $get = $this->callAPISuccess('email', 'get', array(
323 'contact_id' => $this->_contactID
,
324 'location_type_id' => $this->_locationType2
->id
,
326 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__
);
329 public function testReplaceEmailWithId() {
330 // check there are no emails to start with
331 $get = $this->callAPISuccess('email', 'get', array(
332 'contact_id' => $this->_contactID
,
334 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
336 // initialize email address
337 $replace1Params = array(
338 'contact_id' => $this->_contactID
,
341 'location_type_id' => $this->_locationType
->id
,
342 'email' => '1-1@example.com',
348 $replace1 = $this->callAPISuccess('email', 'replace', $replace1Params);
349 $this->assertEquals(1, $replace1['count']);
351 $keys = array_keys($replace1['values']);
352 $emailID = array_shift($keys);
354 // update the email address, but preserve any other fields
355 $replace2Params = array(
356 'contact_id' => $this->_contactID
,
360 'email' => '1-2@example.com',
364 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
365 $this->assertEquals(1, $replace2['count']);
367 // ensure the 'email' was updated while other fields were preserved
368 $get = $this->callAPISuccess('email', 'get', array(
369 'contact_id' => $this->_contactID
,
370 'location_type_id' => $this->_locationType
->id
,
373 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
374 $this->assertEquals(1, $get['values'][$emailID]['is_primary']);
375 $this->assertEquals(1, $get['values'][$emailID]['on_hold']);
376 $this->assertEquals('1-2@example.com', $get['values'][$emailID]['email']);
379 public function testEmailOnHold() {
381 $params_change = array();
383 'contact_id' => $this->_contactID
,
384 'email' => 'api@a-team.com',
387 $result = $this->callAPIAndDocument('email', 'create', $params, __FUNCTION__
, __FILE__
);
388 $this->assertEquals(1, $result['count']);
389 $this->assertNotNull($result['id']);
390 $this->assertNotNull($result['values'][$result['id']]['id']);
391 $this->assertEquals(2, $result['values'][$result['id']]['on_hold']);
392 $this->assertEquals(date('Y-m-d H:i'), date('Y-m-d H:i', strtotime($result['values'][$result['id']]['hold_date'])));
394 // set on_hold is '0'
395 // if isMultipleBulkMail is active, the value in On-hold select is string
396 $params_change = array(
397 'id' => $result['id'],
398 'contact_id' => $this->_contactID
,
399 'email' => 'api@a-team.com',
403 $result_change = $this->callAPISuccess('email', 'create', $params_change +
array('action' => 'get'));
404 $this->assertEquals(1, $result_change['count']);
405 $this->assertEquals($result['id'], $result_change['id']);
406 $this->assertEmpty($result_change['values'][$result_change['id']]['on_hold']);
407 $this->assertEquals(date('Y-m-d H:i'), date('Y-m-d H:i', strtotime($result_change['values'][$result_change['id']]['reset_date'])));
408 $this->assertEmpty($result_change['values'][$result_change['id']]['hold_date']);
410 $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id']));