5 require_once 'CiviTest/CiviUnitTestCase.php';
6 class api_v3_EmailTest
extends CiviUnitTestCase
{
7 protected $_apiversion;
9 protected $_locationType;
12 public $_eNoticeCompliant = TRUE;
14 $this->_apiversion
= 3;
15 $this->_entity
= 'Email';
17 $this->_contactID
= $this->organizationCreate(NULL);
18 $this->_locationType
= $this->locationTypeCreate(NULL);
19 $this->_locationType2
= $this->locationTypeCreate(array(
20 'name' => 'New Location Type 2',
21 'vcard_name' => 'New Location Type 2',
22 'description' => 'Another Location Type',
25 $this->_params
= array(
26 'contact_id' => $this->_contactID
,
27 'location_type_id' => $this->_locationType
->id
,
28 'email' => 'api@a-team.com',
30 'version' => $this->_apiversion
,
36 $this->contactDelete($this->_contactID
);
37 $this->locationTypeDelete($this->_locationType
->id
);
38 $this->locationTypeDelete($this->_locationType2
->id
);
40 public function testCreateEmail() {
41 $params = $this->_params
;
42 //check there are no emails to start with
43 $get = civicrm_api('email', 'get', array(
45 'location_type_id' => $this->_locationType
->id
,
47 $this->assertEquals(0, $get['is_error'], 'In line ' . __LINE__
);
48 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
49 $result = civicrm_api('email', 'create', $params);
51 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
52 $this->assertApiSuccess($result, 'In line ' . __LINE__
);
53 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
54 $this->assertNotNull($result['id'], 'In line ' . __LINE__
);
55 $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__
);
56 $delresult = civicrm_api('email', 'delete', array('id' => $result['id'], 'version' => 3));
57 $this->assertEquals(0, $delresult['is_error'], 'In line ' . __LINE__
);
60 * If a new email is set to is_primary the prev should no longer be
62 * If is_primary is not set then it should become is_primary is no others exist
67 public function testCreateEmailPrimaryHandlingChangeToPrimary() {
68 $params = $this->_params
;
69 unset($params['is_primary']);
70 $email1 = civicrm_api('email', 'create', $params);
71 $this->assertApiSuccess($email1, 'In line ' . __LINE__
);
72 //now we check & make sure it has been set to primary
73 $check = civicrm_api('email', 'getcount', array(
76 'id' => $email1['id'],
78 $this->assertEquals(1, $check);
80 public function testCreateEmailPrimaryHandlingChangeExisting() {
81 $email1 = civicrm_api('email', 'create', $this->_params
);
82 $this->assertApiSuccess($email1, 'In line ' . __LINE__
);
83 $email2 = civicrm_api('email', 'create', $this->_params
);
84 $this->assertApiSuccess($email2, 'In line ' . __LINE__
);
85 $check = civicrm_api('email', 'getcount', array(
88 'contact_id' => $this->_contactID
,
90 $this->assertEquals(1, $check);
93 public function testCreateEmailWithoutEmail() {
94 $result = civicrm_api('Email', 'Create', array('contact_id' => 4, 'version' => 3));
95 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
96 $this->assertContains('missing', $result['error_message'], 'In line ' . __LINE__
);
97 $this->assertContains('email', $result['error_message'], 'In line ' . __LINE__
);
100 public function testGetEmail() {
101 $result = civicrm_api('email', 'create', $this->_params
);
102 $this->assertAPISuccess($result, 'create email in line ' . __LINE__
);
103 $get = civicrm_api('email', 'create', $this->_params
);
104 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
105 $this->assertEquals($get['count'], 1);
106 $get = civicrm_api('email', 'create', $this->_params +
array('debug' => 1));
107 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
108 $this->assertEquals($get['count'], 1);
109 $get = civicrm_api('email', 'create', $this->_params +
array('debug' => 1, 'action' => 'get'));
110 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
111 $this->assertEquals($get['count'], 1);
112 $delresult = civicrm_api('email', 'delete', array('id' => $result['id'], 'version' => 3));
113 $this->assertAPISuccess($delresult, 'In line ' . __LINE__
);
115 public function testDeleteEmail() {
117 'contact_id' => $this->_contactID
,
118 'location_type_id' => $this->_locationType
->id
,
119 'email' => 'api@a-team.com',
121 'version' => $this->_apiversion
,
124 //check there are no emails to start with
125 $get = civicrm_api('email', 'get', array(
127 'location_type_id' => $this->_locationType
->id
,
129 $this->assertEquals(0, $get['is_error'], 'In line ' . __LINE__
);
130 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
133 $create = civicrm_api('email', 'create', $params);
135 $this->assertEquals(0, $create['is_error'], 'In line ' . __LINE__
);
137 $result = civicrm_api('email', 'delete', array('id' => $create['id'], 'version' => 3));
138 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
139 $this->assertAPISuccess($result, 'In line ' . __LINE__
);
140 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
141 $get = civicrm_api('email', 'get', array(
143 'location_type_id' => $this->_locationType
->id
,
145 $this->assertEquals(0, $get['is_error'], 'In line ' . __LINE__
);
146 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
149 public function testReplaceEmail() {
150 // check there are no emails to start with
151 $get = civicrm_api('email', 'get', array(
152 'version' => $this->_apiversion
,
153 'contact_id' => $this->_contactID
,
155 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
156 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
158 // initialize email list with three emails at loc #1 and two emails at loc #2
159 $replace1Params = array(
160 'version' => $this->_apiversion
,
161 'contact_id' => $this->_contactID
,
164 'location_type_id' => $this->_locationType
->id
,
165 'email' => '1-1@example.com',
169 'location_type_id' => $this->_locationType
->id
,
170 'email' => '1-2@example.com',
174 'location_type_id' => $this->_locationType
->id
,
175 'email' => '1-3@example.com',
179 'location_type_id' => $this->_locationType2
->id
,
180 'email' => '2-1@example.com',
184 'location_type_id' => $this->_locationType2
->id
,
185 'email' => '2-2@example.com',
190 $replace1 = civicrm_api('email', 'replace', $replace1Params);
191 $this->documentMe($replace1Params, $replace1, __FUNCTION__
, __FILE__
);
192 $this->assertAPISuccess($replace1, 'In line ' . __LINE__
);
193 $this->assertEquals(5, $replace1['count'], 'In line ' . __LINE__
);
195 // check emails at location #1 or #2
196 $get = civicrm_api('email', 'get', array(
197 'version' => $this->_apiversion
,
198 'contact_id' => $this->_contactID
,
200 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
201 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__
);
203 // replace the subset of emails in location #1, but preserve location #2
204 $replace2Params = array(
205 'version' => $this->_apiversion
,
206 'contact_id' => $this->_contactID
,
207 'location_type_id' => $this->_locationType
->id
,
210 'email' => '1-4@example.com',
215 $replace2 = civicrm_api('email', 'replace', $replace2Params);
216 // $this->documentMe($replace2Params, $replace2, __FUNCTION__, __FILE__);
217 $this->assertAPISuccess($replace2, 'In line ' . __LINE__
);
218 $this->assertEquals(1, $replace2['count'], 'In line ' . __LINE__
);
220 // check emails at location #1 -- all three replaced by one
221 $get = civicrm_api('email', 'get', array(
222 'version' => $this->_apiversion
,
223 'contact_id' => $this->_contactID
,
224 'location_type_id' => $this->_locationType
->id
,
226 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
227 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
229 // check emails at location #2 -- preserve the original two
230 $get = civicrm_api('email', 'get', array(
231 'version' => $this->_apiversion
,
232 'contact_id' => $this->_contactID
,
233 'location_type_id' => $this->_locationType2
->id
,
235 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
236 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__
);
238 // replace the set of emails with an empty set
239 $replace3Params = array(
240 'version' => $this->_apiversion
,
241 'contact_id' => $this->_contactID
,
244 $replace3 = civicrm_api('email', 'replace', $replace3Params);
245 // $this->documentMe($replace3Params, $replace3, __FUNCTION__, __FILE__);
246 $this->assertEquals(0, $replace3['is_error'], 'In line ' . __LINE__
);
247 $this->assertEquals(0, $replace3['count'], 'In line ' . __LINE__
);
250 $get = civicrm_api('email', 'get', array(
251 'version' => $this->_apiversion
,
252 'contact_id' => $this->_contactID
,
254 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
255 $this->assertEquals(0, $get['count'], 'Incorrect email count at ' . __LINE__
);
258 public function testReplaceEmailsInChain() {
259 // check there are no emails to start with
260 $get = civicrm_api('email', 'get', array(
261 'version' => $this->_apiversion
,
262 'contact_id' => $this->_contactID
,
264 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
265 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
266 $description = "example demonstrates use of Replace in a nested API call";
267 $subfile = "NestedReplaceEmail";
268 // initialize email list with three emails at loc #1 and two emails at loc #2
269 $getReplace1Params = array(
270 'version' => $this->_apiversion
,
271 'id' => $this->_contactID
,
272 'api.email.replace' => array(
275 'location_type_id' => $this->_locationType
->id
,
276 'email' => '1-1@example.com',
280 'location_type_id' => $this->_locationType
->id
,
281 'email' => '1-2@example.com',
285 'location_type_id' => $this->_locationType
->id
,
286 'email' => '1-3@example.com',
290 'location_type_id' => $this->_locationType2
->id
,
291 'email' => '2-1@example.com',
295 'location_type_id' => $this->_locationType2
->id
,
296 'email' => '2-2@example.com',
302 $getReplace1 = civicrm_api('contact', 'get', $getReplace1Params);
304 $this->documentMe($getReplace1Params, $getReplace1, __FUNCTION__
, __FILE__
, $description, $subfile);
305 $this->assertAPISuccess($getReplace1['values'][$this->_contactID
]['api.email.replace'], 'In line ' . __LINE__
);
306 $this->assertEquals(5, $getReplace1['values'][$this->_contactID
]['api.email.replace']['count'], 'In line ' . __LINE__
);
308 // check emails at location #1 or #2
309 $get = civicrm_api('email', 'get', array(
310 'version' => $this->_apiversion
,
311 'contact_id' => $this->_contactID
,
313 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
314 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__
);
316 // replace the subset of emails in location #1, but preserve location #2
317 $getReplace2Params = array(
318 'version' => $this->_apiversion
,
319 'id' => $this->_contactID
,
320 'api.email.replace' => array(
321 'location_type_id' => $this->_locationType
->id
,
324 'email' => '1-4@example.com',
330 $getReplace2 = civicrm_api('contact', 'get', $getReplace2Params);
331 // $this->documentMe($getReplace2Params, $getReplace2, __FUNCTION__, __FILE__);
332 $this->assertEquals(0, $getReplace2['values'][$this->_contactID
]['api.email.replace']['is_error'], 'In line ' . __LINE__
);
333 $this->assertEquals(1, $getReplace2['values'][$this->_contactID
]['api.email.replace']['count'], 'In line ' . __LINE__
);
335 // check emails at location #1 -- all three replaced by one
336 $get = civicrm_api('email', 'get', array(
337 'version' => $this->_apiversion
,
338 'contact_id' => $this->_contactID
,
339 'location_type_id' => $this->_locationType
->id
,
342 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
343 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
345 // check emails at location #2 -- preserve the original two
346 $get = civicrm_api('email', 'get', array(
347 'version' => $this->_apiversion
,
348 'contact_id' => $this->_contactID
,
349 'location_type_id' => $this->_locationType2
->id
,
351 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
352 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__
);
355 public function testReplaceEmailWithId() {
356 // check there are no emails to start with
357 $get = civicrm_api('email', 'get', array(
358 'version' => $this->_apiversion
,
359 'contact_id' => $this->_contactID
,
361 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
362 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
364 // initialize email address
365 $replace1Params = array(
366 'version' => $this->_apiversion
,
367 'contact_id' => $this->_contactID
,
370 'location_type_id' => $this->_locationType
->id
,
371 'email' => '1-1@example.com',
377 $replace1 = civicrm_api('email', 'replace', $replace1Params);
378 // $this->documentMe($replace1Params, $replace1, __FUNCTION__, __FILE__);
379 $this->assertAPISuccess($replace1, 'In line ' . __LINE__
);
380 $this->assertEquals(1, $replace1['count'], 'In line ' . __LINE__
);
382 $keys = array_keys($replace1['values']);
383 $emailID = array_shift($keys);
385 // update the email address, but preserve any other fields
386 $replace2Params = array(
387 'version' => $this->_apiversion
,
388 'contact_id' => $this->_contactID
,
392 'email' => '1-2@example.com',
396 $replace2 = civicrm_api('email', 'replace', $replace2Params);
397 // $this->documentMe($replace2Params, $replace2, __FUNCTION__, __FILE__);
398 $this->assertAPISuccess($replace2, 'In line ' . __LINE__
);
399 $this->assertEquals(1, $replace2['count'], 'In line ' . __LINE__
);
401 // ensure the 'email' was updated while other fields were preserved
402 $get = civicrm_api('email', 'get', array(
403 'version' => $this->_apiversion
,
404 'contact_id' => $this->_contactID
,
405 'location_type_id' => $this->_locationType
->id
,
407 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
408 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
409 $this->assertEquals(1, $get['values'][$emailID]['is_primary'], 'In line ' . __LINE__
);
410 $this->assertEquals(1, $get['values'][$emailID]['on_hold'], 'In line ' . __LINE__
);
411 $this->assertEquals('1-2@example.com', $get['values'][$emailID]['email'], 'In line ' . __LINE__
);