3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
29 class api_v3_EmailTest
extends CiviUnitTestCase
{
30 protected $_apiversion;
31 protected $_contactID;
32 protected $_locationType;
35 public $_eNoticeCompliant = TRUE;
37 $this->_apiversion
= 3;
38 $this->_entity
= 'Email';
40 $this->_contactID
= $this->organizationCreate(NULL);
41 $this->_locationType
= $this->locationTypeCreate(NULL);
42 $this->_locationType2
= $this->locationTypeCreate(array(
43 'name' => 'New Location Type 2',
44 'vcard_name' => 'New Location Type 2',
45 'description' => 'Another Location Type',
48 $this->_params
= array(
49 'contact_id' => $this->_contactID
,
50 'location_type_id' => $this->_locationType
->id
,
51 'email' => 'api@a-team.com',
59 $this->contactDelete($this->_contactID
);
60 $this->locationTypeDelete($this->_locationType
->id
);
61 $this->locationTypeDelete($this->_locationType2
->id
);
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'], 'In line ' . __LINE__
);
73 $this->assertNotNull($result['id'], 'In line ' . __LINE__
);
74 $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__
);
75 $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id']));
78 * If a new email is set to is_primary the prev should no longer be
80 * If is_primary is not set then it should become is_primary is no others exist
85 public function testCreateEmailPrimaryHandlingChangeToPrimary() {
86 $params = $this->_params
;
87 unset($params['is_primary']);
88 $email1 = $this->callAPISuccess('email', 'create', $params);
89 //now we check & make sure it has been set to primary
91 $check = $this->callAPISuccess('email', 'getcount', array(
93 'id' => $email1['id'],
99 public function testCreateEmailPrimaryHandlingChangeExisting() {
100 $email1 = $this->callAPISuccess('email', 'create', $this->_params
);
101 $email2 = $this->callAPISuccess('email', 'create', $this->_params
);
102 $check = $this->callAPISuccess('email', 'getcount', array(
104 'contact_id' => $this->_contactID
,
106 $this->assertEquals(1, $check);
109 public function testCreateEmailWithoutEmail() {
110 $result = $this->callAPIFailure('Email', 'Create', array('contact_id' => 4));
111 $this->assertContains('missing', $result['error_message'], 'In line ' . __LINE__
);
112 $this->assertContains('email', $result['error_message'], 'In line ' . __LINE__
);
115 public function testGetEmail() {
116 $result = $this->callAPISuccess('email', 'create', $this->_params
);
117 $get = $this->callAPISuccess('email', 'create', $this->_params
);
118 $this->assertEquals($get['count'], 1);
119 $get = $this->callAPISuccess('email', 'create', $this->_params +
array('debug' => 1));
120 $this->assertEquals($get['count'], 1);
121 $get = $this->callAPISuccess('email', 'create', $this->_params +
array('debug' => 1, 'action' => 'get'));
122 $this->assertEquals($get['count'], 1);
123 $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id']));
125 public function testDeleteEmail() {
127 'contact_id' => $this->_contactID
,
128 'location_type_id' => $this->_locationType
->id
,
129 'email' => 'api@a-team.com',
134 //check there are no emails to start with
135 $get = $this->callAPISuccess('email', 'get', array(
136 'location_type_id' => $this->_locationType
->id
,
138 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
141 $create = $this->callAPISuccess('email', 'create', $params);
143 $result = $this->callAPIAndDocument('email', 'delete', array('id' => $create['id'],), __FUNCTION__
, __FILE__
);
144 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
145 $get = $this->callAPISuccess('email', 'get', array(
146 'location_type_id' => $this->_locationType
->id
,
148 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
151 public function testReplaceEmail() {
152 // check there are no emails to start with
153 $get = $this->callAPISuccess('email', 'get', array(
154 'contact_id' => $this->_contactID
,
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 'contact_id' => $this->_contactID
,
163 'location_type_id' => $this->_locationType
->id
,
164 'email' => '1-1@example.com',
168 'location_type_id' => $this->_locationType
->id
,
169 'email' => '1-2@example.com',
173 'location_type_id' => $this->_locationType
->id
,
174 'email' => '1-3@example.com',
178 'location_type_id' => $this->_locationType2
->id
,
179 'email' => '2-1@example.com',
183 'location_type_id' => $this->_locationType2
->id
,
184 'email' => '2-2@example.com',
189 $replace1 = $this->callAPIAndDocument('email', 'replace', $replace1Params, __FUNCTION__
, __FILE__
);
190 $this->assertEquals(5, $replace1['count'], 'In line ' . __LINE__
);
192 // check emails at location #1 or #2
193 $get = $this->callAPISuccess('email', 'get', array(
194 'contact_id' => $this->_contactID
,
196 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__
);
198 // replace the subset of emails in location #1, but preserve location #2
199 $replace2Params = array(
200 'contact_id' => $this->_contactID
,
201 'location_type_id' => $this->_locationType
->id
,
204 'email' => '1-4@example.com',
209 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
210 $this->assertEquals(1, $replace2['count'], 'In line ' . __LINE__
);
212 // check emails at location #1 -- all three replaced by one
213 $get = $this->callAPISuccess('email', 'get', array(
214 'contact_id' => $this->_contactID
,
215 'location_type_id' => $this->_locationType
->id
,
217 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
219 // check emails at location #2 -- preserve the original two
220 $get = $this->callAPISuccess('email', 'get', array(
221 'contact_id' => $this->_contactID
,
222 'location_type_id' => $this->_locationType2
->id
,
225 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__
);
227 // replace the set of emails with an empty set
228 $replace3Params = array(
229 'contact_id' => $this->_contactID
,
232 $replace3 = $this->callAPISuccess('email', 'replace', $replace3Params);
233 $this->assertEquals(0, $replace3['count'], 'In line ' . __LINE__
);
236 $get = $this->callAPISuccess('email', 'get', array(
238 'contact_id' => $this->_contactID
,
240 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
241 $this->assertEquals(0, $get['count'], 'Incorrect email count at ' . __LINE__
);
244 public function testReplaceEmailsInChain() {
245 // check there are no emails to start with
246 $get = $this->callAPISuccess('email', 'get', array(
248 'contact_id' => $this->_contactID
,
250 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
251 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
252 $description = "example demonstrates use of Replace in a nested API call";
253 $subfile = "NestedReplaceEmail";
254 // initialize email list with three emails at loc #1 and two emails at loc #2
255 $getReplace1Params = array(
257 'id' => $this->_contactID
,
258 'api.email.replace' => array(
261 'location_type_id' => $this->_locationType
->id
,
262 'email' => '1-1@example.com',
266 'location_type_id' => $this->_locationType
->id
,
267 'email' => '1-2@example.com',
271 'location_type_id' => $this->_locationType
->id
,
272 'email' => '1-3@example.com',
276 'location_type_id' => $this->_locationType2
->id
,
277 'email' => '2-1@example.com',
281 'location_type_id' => $this->_locationType2
->id
,
282 'email' => '2-2@example.com',
288 $getReplace1 = $this->callAPIAndDocument('contact', 'get', $getReplace1Params, __FUNCTION__
, __FILE__
, $description, $subfile);
289 $this->assertEquals(5, $getReplace1['values'][$this->_contactID
]['api.email.replace']['count'], 'In line ' . __LINE__
);
291 // check emails at location #1 or #2
292 $get = $this->callAPISuccess('email', 'get', array(
293 'contact_id' => $this->_contactID
,
295 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__
);
297 // replace the subset of emails in location #1, but preserve location #2
298 $getReplace2Params = array(
299 'id' => $this->_contactID
,
300 'api.email.replace' => array(
301 'location_type_id' => $this->_locationType
->id
,
304 'email' => '1-4@example.com',
310 $getReplace2 = $this->callAPISuccess('contact', 'get', $getReplace2Params);
311 $this->assertEquals(0, $getReplace2['values'][$this->_contactID
]['api.email.replace']['is_error'], 'In line ' . __LINE__
);
312 $this->assertEquals(1, $getReplace2['values'][$this->_contactID
]['api.email.replace']['count'], 'In line ' . __LINE__
);
314 // check emails at location #1 -- all three replaced by one
315 $get = $this->callAPISuccess('email', 'get', array(
316 'contact_id' => $this->_contactID
,
317 'location_type_id' => $this->_locationType
->id
,
320 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
322 // check emails at location #2 -- preserve the original two
323 $get = $this->callAPISuccess('email', 'get', array(
324 'contact_id' => $this->_contactID
,
325 'location_type_id' => $this->_locationType2
->id
,
327 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__
);
330 public function testReplaceEmailWithId() {
331 // check there are no emails to start with
332 $get = $this->callAPISuccess('email', 'get', array(
333 'contact_id' => $this->_contactID
,
335 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
337 // initialize email address
338 $replace1Params = array(
339 'contact_id' => $this->_contactID
,
342 'location_type_id' => $this->_locationType
->id
,
343 'email' => '1-1@example.com',
349 $replace1 = $this->callAPISuccess('email', 'replace', $replace1Params);
350 $this->assertEquals(1, $replace1['count'], 'In line ' . __LINE__
);
352 $keys = array_keys($replace1['values']);
353 $emailID = array_shift($keys);
355 // update the email address, but preserve any other fields
356 $replace2Params = array(
357 'contact_id' => $this->_contactID
,
361 'email' => '1-2@example.com',
365 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
366 $this->assertEquals(1, $replace2['count'], 'In line ' . __LINE__
);
368 // ensure the 'email' was updated while other fields were preserved
369 $get = $this->callAPISuccess('email', 'get', array(
370 'contact_id' => $this->_contactID
,
371 'location_type_id' => $this->_locationType
->id
,
374 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
375 $this->assertEquals(1, $get['values'][$emailID]['is_primary'], 'In line ' . __LINE__
);
376 $this->assertEquals(1, $get['values'][$emailID]['on_hold'], 'In line ' . __LINE__
);
377 $this->assertEquals('1-2@example.com', $get['values'][$emailID]['email'], 'In line ' . __LINE__
);