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;
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']));
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
86 public function testCreateEmailPrimaryHandlingChangeToPrimary() {
87 $params = $this->_params
;
88 unset($params['is_primary']);
89 $email1 = $this->callAPISuccess('email', 'create', $params);
90 //now we check & make sure it has been set to primary
92 $check = $this->callAPISuccess('email', 'getcount', array(
94 'id' => $email1['id'],
100 public function testCreateEmailPrimaryHandlingChangeExisting() {
101 $email1 = $this->callAPISuccess('email', 'create', $this->_params
);
102 $email2 = $this->callAPISuccess('email', 'create', $this->_params
);
103 $check = $this->callAPISuccess('email', 'getcount', array(
105 'contact_id' => $this->_contactID
,
107 $this->assertEquals(1, $check);
110 public function testCreateEmailWithoutEmail() {
111 $result = $this->callAPIFailure('Email', 'Create', array('contact_id' => 4));
112 $this->assertContains('missing', $result['error_message'], 'In line ' . __LINE__
);
113 $this->assertContains('email', $result['error_message'], 'In line ' . __LINE__
);
116 public function testGetEmail() {
117 $result = $this->callAPISuccess('email', 'create', $this->_params
);
118 $get = $this->callAPISuccess('email', 'create', $this->_params
);
119 $this->assertEquals($get['count'], 1);
120 $get = $this->callAPISuccess('email', 'create', $this->_params +
array('debug' => 1));
121 $this->assertEquals($get['count'], 1);
122 $get = $this->callAPISuccess('email', 'create', $this->_params +
array('debug' => 1, 'action' => 'get'));
123 $this->assertEquals($get['count'], 1);
124 $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id']));
126 public function testDeleteEmail() {
128 'contact_id' => $this->_contactID
,
129 'location_type_id' => $this->_locationType
->id
,
130 'email' => 'api@a-team.com',
135 //check there are no emails to start with
136 $get = $this->callAPISuccess('email', 'get', array(
137 'location_type_id' => $this->_locationType
->id
,
139 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
142 $create = $this->callAPISuccess('email', 'create', $params);
144 $result = $this->callAPIAndDocument('email', 'delete', array('id' => $create['id'],), __FUNCTION__
, __FILE__
);
145 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
146 $get = $this->callAPISuccess('email', 'get', array(
147 'location_type_id' => $this->_locationType
->id
,
149 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
152 public function testReplaceEmail() {
153 // check there are no emails to start with
154 $get = $this->callAPISuccess('email', 'get', array(
155 'contact_id' => $this->_contactID
,
157 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
159 // initialize email list with three emails at loc #1 and two emails at loc #2
160 $replace1Params = array(
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 = $this->callAPIAndDocument('email', 'replace', $replace1Params, __FUNCTION__
, __FILE__
);
191 $this->assertEquals(5, $replace1['count'], 'In line ' . __LINE__
);
193 // check emails at location #1 or #2
194 $get = $this->callAPISuccess('email', 'get', array(
195 'contact_id' => $this->_contactID
,
197 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__
);
199 // replace the subset of emails in location #1, but preserve location #2
200 $replace2Params = array(
201 'contact_id' => $this->_contactID
,
202 'location_type_id' => $this->_locationType
->id
,
205 'email' => '1-4@example.com',
210 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
211 $this->assertEquals(1, $replace2['count'], 'In line ' . __LINE__
);
213 // check emails at location #1 -- all three replaced by one
214 $get = $this->callAPISuccess('email', 'get', array(
215 'contact_id' => $this->_contactID
,
216 'location_type_id' => $this->_locationType
->id
,
218 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
220 // check emails at location #2 -- preserve the original two
221 $get = $this->callAPISuccess('email', 'get', array(
222 'contact_id' => $this->_contactID
,
223 'location_type_id' => $this->_locationType2
->id
,
226 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__
);
228 // replace the set of emails with an empty set
229 $replace3Params = array(
230 'contact_id' => $this->_contactID
,
233 $replace3 = $this->callAPISuccess('email', 'replace', $replace3Params);
234 $this->assertEquals(0, $replace3['count'], 'In line ' . __LINE__
);
237 $get = $this->callAPISuccess('email', 'get', array(
239 'contact_id' => $this->_contactID
,
241 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
242 $this->assertEquals(0, $get['count'], 'Incorrect email count at ' . __LINE__
);
245 public function testReplaceEmailsInChain() {
246 // check there are no emails to start with
247 $get = $this->callAPISuccess('email', 'get', array(
249 'contact_id' => $this->_contactID
,
251 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
252 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
253 $description = "example demonstrates use of Replace in a nested API call";
254 $subfile = "NestedReplaceEmail";
255 // initialize email list with three emails at loc #1 and two emails at loc #2
256 $getReplace1Params = array(
258 'id' => $this->_contactID
,
259 'api.email.replace' => array(
262 'location_type_id' => $this->_locationType
->id
,
263 'email' => '1-1@example.com',
267 'location_type_id' => $this->_locationType
->id
,
268 'email' => '1-2@example.com',
272 'location_type_id' => $this->_locationType
->id
,
273 'email' => '1-3@example.com',
277 'location_type_id' => $this->_locationType2
->id
,
278 'email' => '2-1@example.com',
282 'location_type_id' => $this->_locationType2
->id
,
283 'email' => '2-2@example.com',
289 $getReplace1 = $this->callAPIAndDocument('contact', 'get', $getReplace1Params, __FUNCTION__
, __FILE__
, $description, $subfile);
290 $this->assertEquals(5, $getReplace1['values'][$this->_contactID
]['api.email.replace']['count'], 'In line ' . __LINE__
);
292 // check emails at location #1 or #2
293 $get = $this->callAPISuccess('email', 'get', array(
294 'contact_id' => $this->_contactID
,
296 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__
);
298 // replace the subset of emails in location #1, but preserve location #2
299 $getReplace2Params = array(
300 'id' => $this->_contactID
,
301 'api.email.replace' => array(
302 'location_type_id' => $this->_locationType
->id
,
305 'email' => '1-4@example.com',
311 $getReplace2 = $this->callAPISuccess('contact', 'get', $getReplace2Params);
312 $this->assertEquals(0, $getReplace2['values'][$this->_contactID
]['api.email.replace']['is_error'], 'In line ' . __LINE__
);
313 $this->assertEquals(1, $getReplace2['values'][$this->_contactID
]['api.email.replace']['count'], 'In line ' . __LINE__
);
315 // check emails at location #1 -- all three replaced by one
316 $get = $this->callAPISuccess('email', 'get', array(
317 'contact_id' => $this->_contactID
,
318 'location_type_id' => $this->_locationType
->id
,
321 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
323 // check emails at location #2 -- preserve the original two
324 $get = $this->callAPISuccess('email', 'get', array(
325 'contact_id' => $this->_contactID
,
326 'location_type_id' => $this->_locationType2
->id
,
328 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__
);
331 public function testReplaceEmailWithId() {
332 // check there are no emails to start with
333 $get = $this->callAPISuccess('email', 'get', array(
334 'contact_id' => $this->_contactID
,
336 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
338 // initialize email address
339 $replace1Params = array(
340 'contact_id' => $this->_contactID
,
343 'location_type_id' => $this->_locationType
->id
,
344 'email' => '1-1@example.com',
350 $replace1 = $this->callAPISuccess('email', 'replace', $replace1Params);
351 $this->assertEquals(1, $replace1['count'], 'In line ' . __LINE__
);
353 $keys = array_keys($replace1['values']);
354 $emailID = array_shift($keys);
356 // update the email address, but preserve any other fields
357 $replace2Params = array(
358 'contact_id' => $this->_contactID
,
362 'email' => '1-2@example.com',
366 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
367 $this->assertEquals(1, $replace2['count'], 'In line ' . __LINE__
);
369 // ensure the 'email' was updated while other fields were preserved
370 $get = $this->callAPISuccess('email', 'get', array(
371 'contact_id' => $this->_contactID
,
372 'location_type_id' => $this->_locationType
->id
,
375 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
376 $this->assertEquals(1, $get['values'][$emailID]['is_primary'], 'In line ' . __LINE__
);
377 $this->assertEquals(1, $get['values'][$emailID]['on_hold'], 'In line ' . __LINE__
);
378 $this->assertEquals('1-2@example.com', $get['values'][$emailID]['email'], 'In line ' . __LINE__
);