3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
29 * Class api_v3_EmailTest
31 class api_v3_EmailTest
extends CiviUnitTestCase
{
32 protected $_apiversion;
33 protected $_contactID;
34 protected $_locationType;
38 public function setUp() {
39 $this->_apiversion
= 3;
40 $this->_entity
= 'Email';
42 $this->useTransaction(TRUE);
44 $this->_contactID
= $this->organizationCreate(NULL);
45 $this->_locationType
= $this->locationTypeCreate(NULL);
46 $this->_locationType2
= $this->locationTypeCreate(array(
47 'name' => 'New Location Type 2',
48 'vcard_name' => 'New Location Type 2',
49 'description' => 'Another Location Type',
52 $this->_params
= array(
53 'contact_id' => $this->_contactID
,
54 'location_type_id' => $this->_locationType
->id
,
55 'email' => 'api@a-team.com',
62 public function testCreateEmail() {
63 $params = $this->_params
;
64 //check there are no emails to start with
65 $get = $this->callAPISuccess('email', 'get', array(
66 'location_type_id' => $this->_locationType
->id
,
68 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
70 $result = $this->callAPIAndDocument('email', 'create', $params, __FUNCTION__
, __FILE__
);
71 $this->assertEquals(1, $result['count']);
72 $this->assertNotNull($result['id']);
73 $this->assertNotNull($result['values'][$result['id']]['id']);
74 $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
82 public function testCreateEmailPrimaryHandlingChangeToPrimary() {
83 $params = $this->_params
;
84 unset($params['is_primary']);
85 $email1 = $this->callAPISuccess('email', 'create', $params);
86 //now we check & make sure it has been set to primary
88 $check = $this->callAPISuccess('email', 'getcount', array(
90 'id' => $email1['id'],
96 public function testCreateEmailPrimaryHandlingChangeExisting() {
97 $email1 = $this->callAPISuccess('email', 'create', $this->_params
);
98 $email2 = $this->callAPISuccess('email', 'create', $this->_params
);
99 $check = $this->callAPISuccess('email', 'getcount', array(
101 'contact_id' => $this->_contactID
,
103 $this->assertEquals(1, $check);
106 public function testCreateEmailWithoutEmail() {
107 $result = $this->callAPIFailure('Email', 'Create', array('contact_id' => 4));
108 $this->assertContains('missing', $result['error_message']);
109 $this->assertContains('email', $result['error_message']);
112 public function testGetEmail() {
113 $result = $this->callAPISuccess('email', 'create', $this->_params
);
114 $get = $this->callAPISuccess('email', 'create', $this->_params
);
115 $this->assertEquals($get['count'], 1);
116 $get = $this->callAPISuccess('email', 'create', $this->_params +
array('debug' => 1));
117 $this->assertEquals($get['count'], 1);
118 $get = $this->callAPISuccess('email', 'create', $this->_params +
array('debug' => 1, 'action' => 'get'));
119 $this->assertEquals($get['count'], 1);
120 $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id']));
123 public function testDeleteEmail() {
125 'contact_id' => $this->_contactID
,
126 'location_type_id' => $this->_locationType
->id
,
127 'email' => 'api@a-team.com',
132 //check there are no emails to start with
133 $get = $this->callAPISuccess('email', 'get', array(
134 'location_type_id' => $this->_locationType
->id
,
136 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
139 $create = $this->callAPISuccess('email', 'create', $params);
141 $result = $this->callAPIAndDocument('email', 'delete', array('id' => $create['id']), __FUNCTION__
, __FILE__
);
142 $this->assertEquals(1, $result['count']);
143 $get = $this->callAPISuccess('email', 'get', array(
144 'location_type_id' => $this->_locationType
->id
,
146 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
149 public function testReplaceEmail() {
150 // check there are no emails to start with
151 $get = $this->callAPISuccess('email', 'get', array(
152 'contact_id' => $this->_contactID
,
154 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
156 // initialize email list with three emails at loc #1 and two emails at loc #2
157 $replace1Params = array(
158 'contact_id' => $this->_contactID
,
161 'location_type_id' => $this->_locationType
->id
,
162 'email' => '1-1@example.com',
166 'location_type_id' => $this->_locationType
->id
,
167 'email' => '1-2@example.com',
171 'location_type_id' => $this->_locationType
->id
,
172 'email' => '1-3@example.com',
176 'location_type_id' => $this->_locationType2
->id
,
177 'email' => '2-1@example.com',
181 'location_type_id' => $this->_locationType2
->id
,
182 'email' => '2-2@example.com',
187 $replace1 = $this->callAPIAndDocument('email', 'replace', $replace1Params, __FUNCTION__
, __FILE__
);
188 $this->assertEquals(5, $replace1['count']);
190 // check emails at location #1 or #2
191 $get = $this->callAPISuccess('email', 'get', array(
192 'contact_id' => $this->_contactID
,
194 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__
);
196 // replace the subset of emails in location #1, but preserve location #2
197 $replace2Params = array(
198 'contact_id' => $this->_contactID
,
199 'location_type_id' => $this->_locationType
->id
,
202 'email' => '1-4@example.com',
207 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
208 $this->assertEquals(1, $replace2['count']);
210 // check emails at location #1 -- all three replaced by one
211 $get = $this->callAPISuccess('email', 'get', array(
212 'contact_id' => $this->_contactID
,
213 'location_type_id' => $this->_locationType
->id
,
215 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
217 // check emails at location #2 -- preserve the original two
218 $get = $this->callAPISuccess('email', 'get', array(
219 'contact_id' => $this->_contactID
,
220 'location_type_id' => $this->_locationType2
->id
,
223 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__
);
225 // replace the set of emails with an empty set
226 $replace3Params = array(
227 'contact_id' => $this->_contactID
,
230 $replace3 = $this->callAPISuccess('email', 'replace', $replace3Params);
231 $this->assertEquals(0, $replace3['count']);
234 $get = $this->callAPISuccess('email', 'get', array(
236 'contact_id' => $this->_contactID
,
238 $this->assertAPISuccess($get);
239 $this->assertEquals(0, $get['count'], 'Incorrect email count at ' . __LINE__
);
242 public function testReplaceEmailsInChain() {
243 // check there are no emails to start with
244 $get = $this->callAPISuccess('email', 'get', array(
246 'contact_id' => $this->_contactID
,
248 $this->assertAPISuccess($get);
249 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
250 $description = "Demonstrates use of Replace in a nested API call.";
251 $subfile = "NestedReplaceEmail";
252 // initialize email list with three emails at loc #1 and two emails at loc #2
253 $getReplace1Params = array(
255 'id' => $this->_contactID
,
256 'api.email.replace' => array(
259 'location_type_id' => $this->_locationType
->id
,
260 'email' => '1-1@example.com',
264 'location_type_id' => $this->_locationType
->id
,
265 'email' => '1-2@example.com',
269 'location_type_id' => $this->_locationType
->id
,
270 'email' => '1-3@example.com',
274 'location_type_id' => $this->_locationType2
->id
,
275 'email' => '2-1@example.com',
279 'location_type_id' => $this->_locationType2
->id
,
280 'email' => '2-2@example.com',
286 $getReplace1 = $this->callAPIAndDocument('contact', 'get', $getReplace1Params, __FUNCTION__
, __FILE__
, $description, $subfile);
287 $this->assertEquals(5, $getReplace1['values'][$this->_contactID
]['api.email.replace']['count']);
289 // check emails at location #1 or #2
290 $get = $this->callAPISuccess('email', 'get', array(
291 'contact_id' => $this->_contactID
,
293 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__
);
295 // replace the subset of emails in location #1, but preserve location #2
296 $getReplace2Params = array(
297 'id' => $this->_contactID
,
298 'api.email.replace' => array(
299 'location_type_id' => $this->_locationType
->id
,
302 'email' => '1-4@example.com',
308 $getReplace2 = $this->callAPISuccess('contact', 'get', $getReplace2Params);
309 $this->assertEquals(0, $getReplace2['values'][$this->_contactID
]['api.email.replace']['is_error']);
310 $this->assertEquals(1, $getReplace2['values'][$this->_contactID
]['api.email.replace']['count']);
312 // check emails at location #1 -- all three replaced by one
313 $get = $this->callAPISuccess('email', 'get', array(
314 'contact_id' => $this->_contactID
,
315 'location_type_id' => $this->_locationType
->id
,
318 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
320 // check emails at location #2 -- preserve the original two
321 $get = $this->callAPISuccess('email', 'get', array(
322 'contact_id' => $this->_contactID
,
323 'location_type_id' => $this->_locationType2
->id
,
325 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__
);
328 public function testReplaceEmailWithId() {
329 // check there are no emails to start with
330 $get = $this->callAPISuccess('email', 'get', array(
331 'contact_id' => $this->_contactID
,
333 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
335 // initialize email address
336 $replace1Params = array(
337 'contact_id' => $this->_contactID
,
340 'location_type_id' => $this->_locationType
->id
,
341 'email' => '1-1@example.com',
347 $replace1 = $this->callAPISuccess('email', 'replace', $replace1Params);
348 $this->assertEquals(1, $replace1['count']);
350 $keys = array_keys($replace1['values']);
351 $emailID = array_shift($keys);
353 // update the email address, but preserve any other fields
354 $replace2Params = array(
355 'contact_id' => $this->_contactID
,
359 'email' => '1-2@example.com',
363 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
364 $this->assertEquals(1, $replace2['count']);
366 // ensure the 'email' was updated while other fields were preserved
367 $get = $this->callAPISuccess('email', 'get', array(
368 'contact_id' => $this->_contactID
,
369 'location_type_id' => $this->_locationType
->id
,
372 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
373 $this->assertEquals(1, $get['values'][$emailID]['is_primary']);
374 $this->assertEquals(1, $get['values'][$emailID]['on_hold']);
375 $this->assertEquals('1-2@example.com', $get['values'][$emailID]['email']);