4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
29 require_once 'CiviTest/CiviUnitTestCase.php';
30 class api_v3_EmailTest
extends CiviUnitTestCase
{
31 protected $_apiversion;
32 protected $_contactID;
33 protected $_locationType;
36 public $_eNoticeCompliant = TRUE;
38 $this->_apiversion
= 3;
39 $this->_entity
= 'Email';
41 $this->_contactID
= $this->organizationCreate(NULL);
42 $this->_locationType
= $this->locationTypeCreate(NULL);
43 $this->_locationType2
= $this->locationTypeCreate(array(
44 'name' => 'New Location Type 2',
45 'vcard_name' => 'New Location Type 2',
46 'description' => 'Another Location Type',
49 $this->_params
= array(
50 'contact_id' => $this->_contactID
,
51 'location_type_id' => $this->_locationType
->id
,
52 'email' => 'api@a-team.com',
54 'version' => $this->_apiversion
,
60 $this->contactDelete($this->_contactID
);
61 $this->locationTypeDelete($this->_locationType
->id
);
62 $this->locationTypeDelete($this->_locationType2
->id
);
64 public function testCreateEmail() {
65 $params = $this->_params
;
66 //check there are no emails to start with
67 $get = civicrm_api('email', 'get', array(
69 'location_type_id' => $this->_locationType
->id
,
71 $this->assertEquals(0, $get['is_error'], 'In line ' . __LINE__
);
72 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
73 $result = civicrm_api('email', 'create', $params);
75 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
76 $this->assertApiSuccess($result, 'In line ' . __LINE__
);
77 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
78 $this->assertNotNull($result['id'], 'In line ' . __LINE__
);
79 $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__
);
80 $delresult = civicrm_api('email', 'delete', array('id' => $result['id'], 'version' => 3));
81 $this->assertEquals(0, $delresult['is_error'], 'In line ' . __LINE__
);
84 * If a new email is set to is_primary the prev should no longer be
86 * If is_primary is not set then it should become is_primary is no others exist
91 public function testCreateEmailPrimaryHandlingChangeToPrimary() {
92 $params = $this->_params
;
93 unset($params['is_primary']);
94 $email1 = civicrm_api('email', 'create', $params);
95 $this->assertApiSuccess($email1, 'In line ' . __LINE__
);
96 //now we check & make sure it has been set to primary
97 $check = civicrm_api('email', 'getcount', array(
100 'id' => $email1['id'],
102 $this->assertEquals(1, $check);
104 public function testCreateEmailPrimaryHandlingChangeExisting() {
105 $email1 = civicrm_api('email', 'create', $this->_params
);
106 $this->assertApiSuccess($email1, 'In line ' . __LINE__
);
107 $email2 = civicrm_api('email', 'create', $this->_params
);
108 $this->assertApiSuccess($email2, 'In line ' . __LINE__
);
109 $check = civicrm_api('email', 'getcount', array(
112 'contact_id' => $this->_contactID
,
114 $this->assertEquals(1, $check);
117 public function testCreateEmailWithoutEmail() {
118 $result = civicrm_api('Email', 'Create', array('contact_id' => 4, 'version' => 3));
119 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
120 $this->assertContains('missing', $result['error_message'], 'In line ' . __LINE__
);
121 $this->assertContains('email', $result['error_message'], 'In line ' . __LINE__
);
124 public function testGetEmail() {
125 $result = civicrm_api('email', 'create', $this->_params
);
126 $this->assertAPISuccess($result, 'create email in line ' . __LINE__
);
127 $get = civicrm_api('email', 'create', $this->_params
);
128 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
129 $this->assertEquals($get['count'], 1);
130 $get = civicrm_api('email', 'create', $this->_params +
array('debug' => 1));
131 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
132 $this->assertEquals($get['count'], 1);
133 $get = civicrm_api('email', 'create', $this->_params +
array('debug' => 1, 'action' => 'get'));
134 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
135 $this->assertEquals($get['count'], 1);
136 $delresult = civicrm_api('email', 'delete', array('id' => $result['id'], 'version' => 3));
137 $this->assertAPISuccess($delresult, 'In line ' . __LINE__
);
139 public function testDeleteEmail() {
141 'contact_id' => $this->_contactID
,
142 'location_type_id' => $this->_locationType
->id
,
143 'email' => 'api@a-team.com',
145 'version' => $this->_apiversion
,
148 //check there are no emails to start with
149 $get = civicrm_api('email', 'get', array(
151 'location_type_id' => $this->_locationType
->id
,
153 $this->assertEquals(0, $get['is_error'], 'In line ' . __LINE__
);
154 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
157 $create = civicrm_api('email', 'create', $params);
159 $this->assertEquals(0, $create['is_error'], 'In line ' . __LINE__
);
161 $result = civicrm_api('email', 'delete', array('id' => $create['id'], 'version' => 3));
162 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
163 $this->assertAPISuccess($result, 'In line ' . __LINE__
);
164 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
165 $get = civicrm_api('email', 'get', array(
167 'location_type_id' => $this->_locationType
->id
,
169 $this->assertEquals(0, $get['is_error'], 'In line ' . __LINE__
);
170 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
173 public function testReplaceEmail() {
174 // check there are no emails to start with
175 $get = civicrm_api('email', 'get', array(
176 'version' => $this->_apiversion
,
177 'contact_id' => $this->_contactID
,
179 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
180 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
182 // initialize email list with three emails at loc #1 and two emails at loc #2
183 $replace1Params = array(
184 'version' => $this->_apiversion
,
185 'contact_id' => $this->_contactID
,
188 'location_type_id' => $this->_locationType
->id
,
189 'email' => '1-1@example.com',
193 'location_type_id' => $this->_locationType
->id
,
194 'email' => '1-2@example.com',
198 'location_type_id' => $this->_locationType
->id
,
199 'email' => '1-3@example.com',
203 'location_type_id' => $this->_locationType2
->id
,
204 'email' => '2-1@example.com',
208 'location_type_id' => $this->_locationType2
->id
,
209 'email' => '2-2@example.com',
214 $replace1 = civicrm_api('email', 'replace', $replace1Params);
215 $this->documentMe($replace1Params, $replace1, __FUNCTION__
, __FILE__
);
216 $this->assertAPISuccess($replace1, 'In line ' . __LINE__
);
217 $this->assertEquals(5, $replace1['count'], 'In line ' . __LINE__
);
219 // check emails at location #1 or #2
220 $get = civicrm_api('email', 'get', array(
221 'version' => $this->_apiversion
,
222 'contact_id' => $this->_contactID
,
224 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
225 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__
);
227 // replace the subset of emails in location #1, but preserve location #2
228 $replace2Params = array(
229 'version' => $this->_apiversion
,
230 'contact_id' => $this->_contactID
,
231 'location_type_id' => $this->_locationType
->id
,
234 'email' => '1-4@example.com',
239 $replace2 = civicrm_api('email', 'replace', $replace2Params);
240 // $this->documentMe($replace2Params, $replace2, __FUNCTION__, __FILE__);
241 $this->assertAPISuccess($replace2, 'In line ' . __LINE__
);
242 $this->assertEquals(1, $replace2['count'], 'In line ' . __LINE__
);
244 // check emails at location #1 -- all three replaced by one
245 $get = civicrm_api('email', 'get', array(
246 'version' => $this->_apiversion
,
247 'contact_id' => $this->_contactID
,
248 'location_type_id' => $this->_locationType
->id
,
250 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
251 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
253 // check emails at location #2 -- preserve the original two
254 $get = civicrm_api('email', 'get', array(
255 'version' => $this->_apiversion
,
256 'contact_id' => $this->_contactID
,
257 'location_type_id' => $this->_locationType2
->id
,
259 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
260 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__
);
262 // replace the set of emails with an empty set
263 $replace3Params = array(
264 'version' => $this->_apiversion
,
265 'contact_id' => $this->_contactID
,
268 $replace3 = civicrm_api('email', 'replace', $replace3Params);
269 // $this->documentMe($replace3Params, $replace3, __FUNCTION__, __FILE__);
270 $this->assertEquals(0, $replace3['is_error'], 'In line ' . __LINE__
);
271 $this->assertEquals(0, $replace3['count'], 'In line ' . __LINE__
);
274 $get = civicrm_api('email', 'get', array(
275 'version' => $this->_apiversion
,
276 'contact_id' => $this->_contactID
,
278 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
279 $this->assertEquals(0, $get['count'], 'Incorrect email count at ' . __LINE__
);
282 public function testReplaceEmailsInChain() {
283 // check there are no emails to start with
284 $get = civicrm_api('email', 'get', array(
285 'version' => $this->_apiversion
,
286 'contact_id' => $this->_contactID
,
288 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
289 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
290 $description = "example demonstrates use of Replace in a nested API call";
291 $subfile = "NestedReplaceEmail";
292 // initialize email list with three emails at loc #1 and two emails at loc #2
293 $getReplace1Params = array(
294 'version' => $this->_apiversion
,
295 'id' => $this->_contactID
,
296 'api.email.replace' => array(
299 'location_type_id' => $this->_locationType
->id
,
300 'email' => '1-1@example.com',
304 'location_type_id' => $this->_locationType
->id
,
305 'email' => '1-2@example.com',
309 'location_type_id' => $this->_locationType
->id
,
310 'email' => '1-3@example.com',
314 'location_type_id' => $this->_locationType2
->id
,
315 'email' => '2-1@example.com',
319 'location_type_id' => $this->_locationType2
->id
,
320 'email' => '2-2@example.com',
326 $getReplace1 = civicrm_api('contact', 'get', $getReplace1Params);
328 $this->documentMe($getReplace1Params, $getReplace1, __FUNCTION__
, __FILE__
, $description, $subfile);
329 $this->assertAPISuccess($getReplace1['values'][$this->_contactID
]['api.email.replace'], 'In line ' . __LINE__
);
330 $this->assertEquals(5, $getReplace1['values'][$this->_contactID
]['api.email.replace']['count'], 'In line ' . __LINE__
);
332 // check emails at location #1 or #2
333 $get = civicrm_api('email', 'get', array(
334 'version' => $this->_apiversion
,
335 'contact_id' => $this->_contactID
,
337 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
338 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__
);
340 // replace the subset of emails in location #1, but preserve location #2
341 $getReplace2Params = array(
342 'version' => $this->_apiversion
,
343 'id' => $this->_contactID
,
344 'api.email.replace' => array(
345 'location_type_id' => $this->_locationType
->id
,
348 'email' => '1-4@example.com',
354 $getReplace2 = civicrm_api('contact', 'get', $getReplace2Params);
355 // $this->documentMe($getReplace2Params, $getReplace2, __FUNCTION__, __FILE__);
356 $this->assertEquals(0, $getReplace2['values'][$this->_contactID
]['api.email.replace']['is_error'], 'In line ' . __LINE__
);
357 $this->assertEquals(1, $getReplace2['values'][$this->_contactID
]['api.email.replace']['count'], 'In line ' . __LINE__
);
359 // check emails at location #1 -- all three replaced by one
360 $get = civicrm_api('email', 'get', array(
361 'version' => $this->_apiversion
,
362 'contact_id' => $this->_contactID
,
363 'location_type_id' => $this->_locationType
->id
,
366 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
367 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
369 // check emails at location #2 -- preserve the original two
370 $get = civicrm_api('email', 'get', array(
371 'version' => $this->_apiversion
,
372 'contact_id' => $this->_contactID
,
373 'location_type_id' => $this->_locationType2
->id
,
375 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
376 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__
);
379 public function testReplaceEmailWithId() {
380 // check there are no emails to start with
381 $get = civicrm_api('email', 'get', array(
382 'version' => $this->_apiversion
,
383 'contact_id' => $this->_contactID
,
385 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
386 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
388 // initialize email address
389 $replace1Params = array(
390 'version' => $this->_apiversion
,
391 'contact_id' => $this->_contactID
,
394 'location_type_id' => $this->_locationType
->id
,
395 'email' => '1-1@example.com',
401 $replace1 = civicrm_api('email', 'replace', $replace1Params);
402 // $this->documentMe($replace1Params, $replace1, __FUNCTION__, __FILE__);
403 $this->assertAPISuccess($replace1, 'In line ' . __LINE__
);
404 $this->assertEquals(1, $replace1['count'], 'In line ' . __LINE__
);
406 $keys = array_keys($replace1['values']);
407 $emailID = array_shift($keys);
409 // update the email address, but preserve any other fields
410 $replace2Params = array(
411 'version' => $this->_apiversion
,
412 'contact_id' => $this->_contactID
,
416 'email' => '1-2@example.com',
420 $replace2 = civicrm_api('email', 'replace', $replace2Params);
421 // $this->documentMe($replace2Params, $replace2, __FUNCTION__, __FILE__);
422 $this->assertAPISuccess($replace2, 'In line ' . __LINE__
);
423 $this->assertEquals(1, $replace2['count'], 'In line ' . __LINE__
);
425 // ensure the 'email' was updated while other fields were preserved
426 $get = civicrm_api('email', 'get', array(
427 'version' => $this->_apiversion
,
428 'contact_id' => $this->_contactID
,
429 'location_type_id' => $this->_locationType
->id
,
431 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
432 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
433 $this->assertEquals(1, $get['values'][$emailID]['is_primary'], 'In line ' . __LINE__
);
434 $this->assertEquals(1, $get['values'][$emailID]['on_hold'], 'In line ' . __LINE__
);
435 $this->assertEquals('1-2@example.com', $get['values'][$emailID]['email'], 'In line ' . __LINE__
);