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
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']));
127 public function testDeleteEmail() {
129 'contact_id' => $this->_contactID
,
130 'location_type_id' => $this->_locationType
->id
,
131 'email' => 'api@a-team.com',
136 //check there are no emails to start with
137 $get = $this->callAPISuccess('email', 'get', array(
138 'location_type_id' => $this->_locationType
->id
,
140 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
143 $create = $this->callAPISuccess('email', 'create', $params);
145 $result = $this->callAPIAndDocument('email', 'delete', array('id' => $create['id']), __FUNCTION__
, __FILE__
);
146 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
147 $get = $this->callAPISuccess('email', 'get', array(
148 'location_type_id' => $this->_locationType
->id
,
150 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
153 public function testReplaceEmail() {
154 // check there are no emails to start with
155 $get = $this->callAPISuccess('email', 'get', array(
156 'contact_id' => $this->_contactID
,
158 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
160 // initialize email list with three emails at loc #1 and two emails at loc #2
161 $replace1Params = array(
162 'contact_id' => $this->_contactID
,
165 'location_type_id' => $this->_locationType
->id
,
166 'email' => '1-1@example.com',
170 'location_type_id' => $this->_locationType
->id
,
171 'email' => '1-2@example.com',
175 'location_type_id' => $this->_locationType
->id
,
176 'email' => '1-3@example.com',
180 'location_type_id' => $this->_locationType2
->id
,
181 'email' => '2-1@example.com',
185 'location_type_id' => $this->_locationType2
->id
,
186 'email' => '2-2@example.com',
191 $replace1 = $this->callAPIAndDocument('email', 'replace', $replace1Params, __FUNCTION__
, __FILE__
);
192 $this->assertEquals(5, $replace1['count'], 'In line ' . __LINE__
);
194 // check emails at location #1 or #2
195 $get = $this->callAPISuccess('email', 'get', array(
196 'contact_id' => $this->_contactID
,
198 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__
);
200 // replace the subset of emails in location #1, but preserve location #2
201 $replace2Params = array(
202 'contact_id' => $this->_contactID
,
203 'location_type_id' => $this->_locationType
->id
,
206 'email' => '1-4@example.com',
211 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
212 $this->assertEquals(1, $replace2['count'], 'In line ' . __LINE__
);
214 // check emails at location #1 -- all three replaced by one
215 $get = $this->callAPISuccess('email', 'get', array(
216 'contact_id' => $this->_contactID
,
217 'location_type_id' => $this->_locationType
->id
,
219 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
221 // check emails at location #2 -- preserve the original two
222 $get = $this->callAPISuccess('email', 'get', array(
223 'contact_id' => $this->_contactID
,
224 'location_type_id' => $this->_locationType2
->id
,
227 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__
);
229 // replace the set of emails with an empty set
230 $replace3Params = array(
231 'contact_id' => $this->_contactID
,
234 $replace3 = $this->callAPISuccess('email', 'replace', $replace3Params);
235 $this->assertEquals(0, $replace3['count'], 'In line ' . __LINE__
);
238 $get = $this->callAPISuccess('email', 'get', array(
240 'contact_id' => $this->_contactID
,
242 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
243 $this->assertEquals(0, $get['count'], 'Incorrect email count at ' . __LINE__
);
246 public function testReplaceEmailsInChain() {
247 // check there are no emails to start with
248 $get = $this->callAPISuccess('email', 'get', array(
250 'contact_id' => $this->_contactID
,
252 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
253 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
254 $description = "example demonstrates use of Replace in a nested API call";
255 $subfile = "NestedReplaceEmail";
256 // initialize email list with three emails at loc #1 and two emails at loc #2
257 $getReplace1Params = array(
259 'id' => $this->_contactID
,
260 'api.email.replace' => array(
263 'location_type_id' => $this->_locationType
->id
,
264 'email' => '1-1@example.com',
268 'location_type_id' => $this->_locationType
->id
,
269 'email' => '1-2@example.com',
273 'location_type_id' => $this->_locationType
->id
,
274 'email' => '1-3@example.com',
278 'location_type_id' => $this->_locationType2
->id
,
279 'email' => '2-1@example.com',
283 'location_type_id' => $this->_locationType2
->id
,
284 'email' => '2-2@example.com',
290 $getReplace1 = $this->callAPIAndDocument('contact', 'get', $getReplace1Params, __FUNCTION__
, __FILE__
, $description, $subfile);
291 $this->assertEquals(5, $getReplace1['values'][$this->_contactID
]['api.email.replace']['count'], 'In line ' . __LINE__
);
293 // check emails at location #1 or #2
294 $get = $this->callAPISuccess('email', 'get', array(
295 'contact_id' => $this->_contactID
,
297 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__
);
299 // replace the subset of emails in location #1, but preserve location #2
300 $getReplace2Params = array(
301 'id' => $this->_contactID
,
302 'api.email.replace' => array(
303 'location_type_id' => $this->_locationType
->id
,
306 'email' => '1-4@example.com',
312 $getReplace2 = $this->callAPISuccess('contact', 'get', $getReplace2Params);
313 $this->assertEquals(0, $getReplace2['values'][$this->_contactID
]['api.email.replace']['is_error'], 'In line ' . __LINE__
);
314 $this->assertEquals(1, $getReplace2['values'][$this->_contactID
]['api.email.replace']['count'], 'In line ' . __LINE__
);
316 // check emails at location #1 -- all three replaced by one
317 $get = $this->callAPISuccess('email', 'get', array(
318 'contact_id' => $this->_contactID
,
319 'location_type_id' => $this->_locationType
->id
,
322 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
324 // check emails at location #2 -- preserve the original two
325 $get = $this->callAPISuccess('email', 'get', array(
326 'contact_id' => $this->_contactID
,
327 'location_type_id' => $this->_locationType2
->id
,
329 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__
);
332 public function testReplaceEmailWithId() {
333 // check there are no emails to start with
334 $get = $this->callAPISuccess('email', 'get', array(
335 'contact_id' => $this->_contactID
,
337 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
339 // initialize email address
340 $replace1Params = array(
341 'contact_id' => $this->_contactID
,
344 'location_type_id' => $this->_locationType
->id
,
345 'email' => '1-1@example.com',
351 $replace1 = $this->callAPISuccess('email', 'replace', $replace1Params);
352 $this->assertEquals(1, $replace1['count'], 'In line ' . __LINE__
);
354 $keys = array_keys($replace1['values']);
355 $emailID = array_shift($keys);
357 // update the email address, but preserve any other fields
358 $replace2Params = array(
359 'contact_id' => $this->_contactID
,
363 'email' => '1-2@example.com',
367 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
368 $this->assertEquals(1, $replace2['count'], 'In line ' . __LINE__
);
370 // ensure the 'email' was updated while other fields were preserved
371 $get = $this->callAPISuccess('email', 'get', array(
372 'contact_id' => $this->_contactID
,
373 'location_type_id' => $this->_locationType
->id
,
376 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
377 $this->assertEquals(1, $get['values'][$emailID]['is_primary'], 'In line ' . __LINE__
);
378 $this->assertEquals(1, $get['values'][$emailID]['on_hold'], 'In line ' . __LINE__
);
379 $this->assertEquals('1-2@example.com', $get['values'][$emailID]['email'], 'In line ' . __LINE__
);