3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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';
31 * Class api_v3_EmailTest
33 class api_v3_EmailTest
extends CiviUnitTestCase
{
34 protected $_apiversion;
35 protected $_contactID;
36 protected $_locationType;
40 public function setUp() {
41 $this->_apiversion
= 3;
42 $this->_entity
= 'Email';
44 $this->useTransaction(TRUE);
46 $this->_contactID
= $this->organizationCreate(NULL);
47 $this->_locationType
= $this->locationTypeCreate(NULL);
48 $this->_locationType2
= $this->locationTypeCreate(array(
49 'name' => 'New Location Type 2',
50 'vcard_name' => 'New Location Type 2',
51 'description' => 'Another Location Type',
54 $this->_params
= array(
55 'contact_id' => $this->_contactID
,
56 'location_type_id' => $this->_locationType
->id
,
57 'email' => 'api@a-team.com',
64 public function testCreateEmail() {
65 $params = $this->_params
;
66 //check there are no emails to start with
67 $get = $this->callAPISuccess('email', 'get', array(
68 'location_type_id' => $this->_locationType
->id
,
70 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
72 $result = $this->callAPIAndDocument('email', 'create', $params, __FUNCTION__
, __FILE__
);
73 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
74 $this->assertNotNull($result['id'], 'In line ' . __LINE__
);
75 $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__
);
76 $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id']));
80 * If a new email is set to is_primary the prev should no longer be
82 * If is_primary is not set then it should become is_primary is no others exist
84 public function testCreateEmailPrimaryHandlingChangeToPrimary() {
85 $params = $this->_params
;
86 unset($params['is_primary']);
87 $email1 = $this->callAPISuccess('email', 'create', $params);
88 //now we check & make sure it has been set to primary
90 $check = $this->callAPISuccess('email', 'getcount', array(
92 'id' => $email1['id'],
98 public function testCreateEmailPrimaryHandlingChangeExisting() {
99 $email1 = $this->callAPISuccess('email', 'create', $this->_params
);
100 $email2 = $this->callAPISuccess('email', 'create', $this->_params
);
101 $check = $this->callAPISuccess('email', 'getcount', array(
103 'contact_id' => $this->_contactID
,
105 $this->assertEquals(1, $check);
108 public function testCreateEmailWithoutEmail() {
109 $result = $this->callAPIFailure('Email', 'Create', array('contact_id' => 4));
110 $this->assertContains('missing', $result['error_message'], 'In line ' . __LINE__
);
111 $this->assertContains('email', $result['error_message'], 'In line ' . __LINE__
);
114 public function testGetEmail() {
115 $result = $this->callAPISuccess('email', 'create', $this->_params
);
116 $get = $this->callAPISuccess('email', 'create', $this->_params
);
117 $this->assertEquals($get['count'], 1);
118 $get = $this->callAPISuccess('email', 'create', $this->_params +
array('debug' => 1));
119 $this->assertEquals($get['count'], 1);
120 $get = $this->callAPISuccess('email', 'create', $this->_params +
array('debug' => 1, 'action' => 'get'));
121 $this->assertEquals($get['count'], 1);
122 $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__
);