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->_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',
63 $this->contactDelete($this->_contactID
);
64 $this->locationTypeDelete($this->_locationType
->id
);
65 $this->locationTypeDelete($this->_locationType2
->id
);
67 public function testCreateEmail() {
68 $params = $this->_params
;
69 //check there are no emails to start with
70 $get = $this->callAPISuccess('email', 'get', array(
71 'location_type_id' => $this->_locationType
->id
,
73 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
75 $result = $this->callAPIAndDocument('email', 'create', $params, __FUNCTION__
, __FILE__
);
76 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
77 $this->assertNotNull($result['id'], 'In line ' . __LINE__
);
78 $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__
);
79 $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id']));
82 * If a new email is set to is_primary the prev should no longer be
84 * If is_primary is not set then it should become is_primary is no others exist
89 public function testCreateEmailPrimaryHandlingChangeToPrimary() {
90 $params = $this->_params
;
91 unset($params['is_primary']);
92 $email1 = $this->callAPISuccess('email', 'create', $params);
93 //now we check & make sure it has been set to primary
95 $check = $this->callAPISuccess('email', 'getcount', array(
97 'id' => $email1['id'],
103 public function testCreateEmailPrimaryHandlingChangeExisting() {
104 $email1 = $this->callAPISuccess('email', 'create', $this->_params
);
105 $email2 = $this->callAPISuccess('email', 'create', $this->_params
);
106 $check = $this->callAPISuccess('email', 'getcount', array(
108 'contact_id' => $this->_contactID
,
110 $this->assertEquals(1, $check);
113 public function testCreateEmailWithoutEmail() {
114 $result = $this->callAPIFailure('Email', 'Create', array('contact_id' => 4));
115 $this->assertContains('missing', $result['error_message'], 'In line ' . __LINE__
);
116 $this->assertContains('email', $result['error_message'], 'In line ' . __LINE__
);
119 public function testGetEmail() {
120 $result = $this->callAPISuccess('email', 'create', $this->_params
);
121 $get = $this->callAPISuccess('email', 'create', $this->_params
);
122 $this->assertEquals($get['count'], 1);
123 $get = $this->callAPISuccess('email', 'create', $this->_params +
array('debug' => 1));
124 $this->assertEquals($get['count'], 1);
125 $get = $this->callAPISuccess('email', 'create', $this->_params +
array('debug' => 1, 'action' => 'get'));
126 $this->assertEquals($get['count'], 1);
127 $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id']));
129 public function testDeleteEmail() {
131 'contact_id' => $this->_contactID
,
132 'location_type_id' => $this->_locationType
->id
,
133 'email' => 'api@a-team.com',
138 //check there are no emails to start with
139 $get = $this->callAPISuccess('email', 'get', array(
140 'location_type_id' => $this->_locationType
->id
,
142 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
145 $create = $this->callAPISuccess('email', 'create', $params);
147 $result = $this->callAPIAndDocument('email', 'delete', array('id' => $create['id'],), __FUNCTION__
, __FILE__
);
148 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
149 $get = $this->callAPISuccess('email', 'get', array(
150 'location_type_id' => $this->_locationType
->id
,
152 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
155 public function testReplaceEmail() {
156 // check there are no emails to start with
157 $get = $this->callAPISuccess('email', 'get', array(
158 'contact_id' => $this->_contactID
,
160 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
162 // initialize email list with three emails at loc #1 and two emails at loc #2
163 $replace1Params = array(
164 'contact_id' => $this->_contactID
,
167 'location_type_id' => $this->_locationType
->id
,
168 'email' => '1-1@example.com',
172 'location_type_id' => $this->_locationType
->id
,
173 'email' => '1-2@example.com',
177 'location_type_id' => $this->_locationType
->id
,
178 'email' => '1-3@example.com',
182 'location_type_id' => $this->_locationType2
->id
,
183 'email' => '2-1@example.com',
187 'location_type_id' => $this->_locationType2
->id
,
188 'email' => '2-2@example.com',
193 $replace1 = $this->callAPIAndDocument('email', 'replace', $replace1Params, __FUNCTION__
, __FILE__
);
194 $this->assertEquals(5, $replace1['count'], 'In line ' . __LINE__
);
196 // check emails at location #1 or #2
197 $get = $this->callAPISuccess('email', 'get', array(
198 'contact_id' => $this->_contactID
,
200 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__
);
202 // replace the subset of emails in location #1, but preserve location #2
203 $replace2Params = array(
204 'contact_id' => $this->_contactID
,
205 'location_type_id' => $this->_locationType
->id
,
208 'email' => '1-4@example.com',
213 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
214 $this->assertEquals(1, $replace2['count'], 'In line ' . __LINE__
);
216 // check emails at location #1 -- all three replaced by one
217 $get = $this->callAPISuccess('email', 'get', array(
218 'contact_id' => $this->_contactID
,
219 'location_type_id' => $this->_locationType
->id
,
221 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
223 // check emails at location #2 -- preserve the original two
224 $get = $this->callAPISuccess('email', 'get', array(
225 'contact_id' => $this->_contactID
,
226 'location_type_id' => $this->_locationType2
->id
,
229 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__
);
231 // replace the set of emails with an empty set
232 $replace3Params = array(
233 'contact_id' => $this->_contactID
,
236 $replace3 = $this->callAPISuccess('email', 'replace', $replace3Params);
237 $this->assertEquals(0, $replace3['count'], 'In line ' . __LINE__
);
240 $get = $this->callAPISuccess('email', 'get', array(
242 'contact_id' => $this->_contactID
,
244 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
245 $this->assertEquals(0, $get['count'], 'Incorrect email count at ' . __LINE__
);
248 public function testReplaceEmailsInChain() {
249 // check there are no emails to start with
250 $get = $this->callAPISuccess('email', 'get', array(
252 'contact_id' => $this->_contactID
,
254 $this->assertAPISuccess($get, 'In line ' . __LINE__
);
255 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
256 $description = "example demonstrates use of Replace in a nested API call";
257 $subfile = "NestedReplaceEmail";
258 // initialize email list with three emails at loc #1 and two emails at loc #2
259 $getReplace1Params = array(
261 'id' => $this->_contactID
,
262 'api.email.replace' => array(
265 'location_type_id' => $this->_locationType
->id
,
266 'email' => '1-1@example.com',
270 'location_type_id' => $this->_locationType
->id
,
271 'email' => '1-2@example.com',
275 'location_type_id' => $this->_locationType
->id
,
276 'email' => '1-3@example.com',
280 'location_type_id' => $this->_locationType2
->id
,
281 'email' => '2-1@example.com',
285 'location_type_id' => $this->_locationType2
->id
,
286 'email' => '2-2@example.com',
292 $getReplace1 = $this->callAPIAndDocument('contact', 'get', $getReplace1Params, __FUNCTION__
, __FILE__
, $description, $subfile);
293 $this->assertEquals(5, $getReplace1['values'][$this->_contactID
]['api.email.replace']['count'], 'In line ' . __LINE__
);
295 // check emails at location #1 or #2
296 $get = $this->callAPISuccess('email', 'get', array(
297 'contact_id' => $this->_contactID
,
299 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__
);
301 // replace the subset of emails in location #1, but preserve location #2
302 $getReplace2Params = array(
303 'id' => $this->_contactID
,
304 'api.email.replace' => array(
305 'location_type_id' => $this->_locationType
->id
,
308 'email' => '1-4@example.com',
314 $getReplace2 = $this->callAPISuccess('contact', 'get', $getReplace2Params);
315 $this->assertEquals(0, $getReplace2['values'][$this->_contactID
]['api.email.replace']['is_error'], 'In line ' . __LINE__
);
316 $this->assertEquals(1, $getReplace2['values'][$this->_contactID
]['api.email.replace']['count'], 'In line ' . __LINE__
);
318 // check emails at location #1 -- all three replaced by one
319 $get = $this->callAPISuccess('email', 'get', array(
320 'contact_id' => $this->_contactID
,
321 'location_type_id' => $this->_locationType
->id
,
324 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
326 // check emails at location #2 -- preserve the original two
327 $get = $this->callAPISuccess('email', 'get', array(
328 'contact_id' => $this->_contactID
,
329 'location_type_id' => $this->_locationType2
->id
,
331 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__
);
334 public function testReplaceEmailWithId() {
335 // check there are no emails to start with
336 $get = $this->callAPISuccess('email', 'get', array(
337 'contact_id' => $this->_contactID
,
339 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
341 // initialize email address
342 $replace1Params = array(
343 'contact_id' => $this->_contactID
,
346 'location_type_id' => $this->_locationType
->id
,
347 'email' => '1-1@example.com',
353 $replace1 = $this->callAPISuccess('email', 'replace', $replace1Params);
354 $this->assertEquals(1, $replace1['count'], 'In line ' . __LINE__
);
356 $keys = array_keys($replace1['values']);
357 $emailID = array_shift($keys);
359 // update the email address, but preserve any other fields
360 $replace2Params = array(
361 'contact_id' => $this->_contactID
,
365 'email' => '1-2@example.com',
369 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
370 $this->assertEquals(1, $replace2['count'], 'In line ' . __LINE__
);
372 // ensure the 'email' was updated while other fields were preserved
373 $get = $this->callAPISuccess('email', 'get', array(
374 'contact_id' => $this->_contactID
,
375 'location_type_id' => $this->_locationType
->id
,
378 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
379 $this->assertEquals(1, $get['values'][$emailID]['is_primary'], 'In line ' . __LINE__
);
380 $this->assertEquals(1, $get['values'][$emailID]['on_hold'], 'In line ' . __LINE__
);
381 $this->assertEquals('1-2@example.com', $get['values'][$emailID]['email'], 'In line ' . __LINE__
);