Merge pull request #1014 from colemanw/importParser
[civicrm-core.git] / tests / phpunit / api / v3 / EmailTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 require_once 'CiviTest/CiviUnitTestCase.php';
30 class api_v3_EmailTest extends CiviUnitTestCase {
31 protected $_apiversion;
32 protected $_contactID;
33 protected $_locationType;
34 protected $_entity;
35 protected $_params;
36 public $_eNoticeCompliant = TRUE;
37 function setUp() {
38 $this->_apiversion = 3;
39 $this->_entity = 'Email';
40 parent::setUp();
41 $this->_contactID = $this->organizationCreate(NULL);
42 $this->_locationType = $this->locationTypeCreate(NULL);
43 $this->_locationType2 = $this->locationTypeCreate(array(
44 'name' => 'New Location Type 2',
45 'vcard_name' => 'New Location Type 2',
46 'description' => 'Another Location Type',
47 'is_active' => 1,
48 ));
49 $this->_params = array(
50 'contact_id' => $this->_contactID,
51 'location_type_id' => $this->_locationType->id,
52 'email' => 'api@a-team.com',
53 'is_primary' => 1,
54 'version' => $this->_apiversion,
55 //TODO email_type_id
56 );
57 }
58
59 function tearDown() {
60 $this->contactDelete($this->_contactID);
61 $this->locationTypeDelete($this->_locationType->id);
62 $this->locationTypeDelete($this->_locationType2->id);
63 }
64 public function testCreateEmail() {
65 $params = $this->_params;
66 //check there are no emails to start with
67 $get = civicrm_api('email', 'get', array(
68 'version' => 3,
69 'location_type_id' => $this->_locationType->id,
70 ));
71 $this->assertEquals(0, $get['is_error'], 'In line ' . __LINE__);
72 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__);
73 $result = civicrm_api('email', 'create', $params);
74
75 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
76 $this->assertApiSuccess($result, 'In line ' . __LINE__);
77 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
78 $this->assertNotNull($result['id'], 'In line ' . __LINE__);
79 $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__);
80 $delresult = civicrm_api('email', 'delete', array('id' => $result['id'], 'version' => 3));
81 $this->assertEquals(0, $delresult['is_error'], 'In line ' . __LINE__);
82 }
83 /*
84 * If a new email is set to is_primary the prev should no longer be
85 *
86 * If is_primary is not set then it should become is_primary is no others exist
87 */
88
89
90
91 public function testCreateEmailPrimaryHandlingChangeToPrimary() {
92 $params = $this->_params;
93 unset($params['is_primary']);
94 $email1 = civicrm_api('email', 'create', $params);
95 $this->assertApiSuccess($email1, 'In line ' . __LINE__);
96 //now we check & make sure it has been set to primary
97 $check = civicrm_api('email', 'getcount', array(
98 'version' => 3,
99 'is_primary' => 1,
100 'id' => $email1['id'],
101 ));
102 $this->assertEquals(1, $check);
103 }
104 public function testCreateEmailPrimaryHandlingChangeExisting() {
105 $email1 = civicrm_api('email', 'create', $this->_params);
106 $this->assertApiSuccess($email1, 'In line ' . __LINE__);
107 $email2 = civicrm_api('email', 'create', $this->_params);
108 $this->assertApiSuccess($email2, 'In line ' . __LINE__);
109 $check = civicrm_api('email', 'getcount', array(
110 'version' => 3,
111 'is_primary' => 1,
112 'contact_id' => $this->_contactID,
113 ));
114 $this->assertEquals(1, $check);
115 }
116
117 public function testCreateEmailWithoutEmail() {
118 $result = civicrm_api('Email', 'Create', array('contact_id' => 4, 'version' => 3));
119 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__);
120 $this->assertContains('missing', $result['error_message'], 'In line ' . __LINE__);
121 $this->assertContains('email', $result['error_message'], 'In line ' . __LINE__);
122 }
123
124 public function testGetEmail() {
125 $result = civicrm_api('email', 'create', $this->_params);
126 $this->assertAPISuccess($result, 'create email in line ' . __LINE__);
127 $get = civicrm_api('email', 'create', $this->_params);
128 $this->assertAPISuccess($get, 'In line ' . __LINE__);
129 $this->assertEquals($get['count'], 1);
130 $get = civicrm_api('email', 'create', $this->_params + array('debug' => 1));
131 $this->assertAPISuccess($get, 'In line ' . __LINE__);
132 $this->assertEquals($get['count'], 1);
133 $get = civicrm_api('email', 'create', $this->_params + array('debug' => 1, 'action' => 'get'));
134 $this->assertAPISuccess($get, 'In line ' . __LINE__);
135 $this->assertEquals($get['count'], 1);
136 $delresult = civicrm_api('email', 'delete', array('id' => $result['id'], 'version' => 3));
137 $this->assertAPISuccess($delresult, 'In line ' . __LINE__);
138 }
139 public function testDeleteEmail() {
140 $params = array(
141 'contact_id' => $this->_contactID,
142 'location_type_id' => $this->_locationType->id,
143 'email' => 'api@a-team.com',
144 'is_primary' => 1,
145 'version' => $this->_apiversion,
146 //TODO email_type_id
147 );
148 //check there are no emails to start with
149 $get = civicrm_api('email', 'get', array(
150 'version' => 3,
151 'location_type_id' => $this->_locationType->id,
152 ));
153 $this->assertEquals(0, $get['is_error'], 'In line ' . __LINE__);
154 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__);
155
156 //create one
157 $create = civicrm_api('email', 'create', $params);
158
159 $this->assertEquals(0, $create['is_error'], 'In line ' . __LINE__);
160
161 $result = civicrm_api('email', 'delete', array('id' => $create['id'], 'version' => 3));
162 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
163 $this->assertAPISuccess($result, 'In line ' . __LINE__);
164 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
165 $get = civicrm_api('email', 'get', array(
166 'version' => 3,
167 'location_type_id' => $this->_locationType->id,
168 ));
169 $this->assertEquals(0, $get['is_error'], 'In line ' . __LINE__);
170 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__);
171 }
172
173 public function testReplaceEmail() {
174 // check there are no emails to start with
175 $get = civicrm_api('email', 'get', array(
176 'version' => $this->_apiversion,
177 'contact_id' => $this->_contactID,
178 ));
179 $this->assertAPISuccess($get, 'In line ' . __LINE__);
180 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__);
181
182 // initialize email list with three emails at loc #1 and two emails at loc #2
183 $replace1Params = array(
184 'version' => $this->_apiversion,
185 'contact_id' => $this->_contactID,
186 'values' => array(
187 array(
188 'location_type_id' => $this->_locationType->id,
189 'email' => '1-1@example.com',
190 'is_primary' => 1,
191 ),
192 array(
193 'location_type_id' => $this->_locationType->id,
194 'email' => '1-2@example.com',
195 'is_primary' => 0,
196 ),
197 array(
198 'location_type_id' => $this->_locationType->id,
199 'email' => '1-3@example.com',
200 'is_primary' => 0,
201 ),
202 array(
203 'location_type_id' => $this->_locationType2->id,
204 'email' => '2-1@example.com',
205 'is_primary' => 0,
206 ),
207 array(
208 'location_type_id' => $this->_locationType2->id,
209 'email' => '2-2@example.com',
210 'is_primary' => 0,
211 ),
212 ),
213 );
214 $replace1 = civicrm_api('email', 'replace', $replace1Params);
215 $this->documentMe($replace1Params, $replace1, __FUNCTION__, __FILE__);
216 $this->assertAPISuccess($replace1, 'In line ' . __LINE__);
217 $this->assertEquals(5, $replace1['count'], 'In line ' . __LINE__);
218
219 // check emails at location #1 or #2
220 $get = civicrm_api('email', 'get', array(
221 'version' => $this->_apiversion,
222 'contact_id' => $this->_contactID,
223 ));
224 $this->assertAPISuccess($get, 'In line ' . __LINE__);
225 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__);
226
227 // replace the subset of emails in location #1, but preserve location #2
228 $replace2Params = array(
229 'version' => $this->_apiversion,
230 'contact_id' => $this->_contactID,
231 'location_type_id' => $this->_locationType->id,
232 'values' => array(
233 array(
234 'email' => '1-4@example.com',
235 'is_primary' => 1,
236 ),
237 ),
238 );
239 $replace2 = civicrm_api('email', 'replace', $replace2Params);
240 // $this->documentMe($replace2Params, $replace2, __FUNCTION__, __FILE__);
241 $this->assertAPISuccess($replace2, 'In line ' . __LINE__);
242 $this->assertEquals(1, $replace2['count'], 'In line ' . __LINE__);
243
244 // check emails at location #1 -- all three replaced by one
245 $get = civicrm_api('email', 'get', array(
246 'version' => $this->_apiversion,
247 'contact_id' => $this->_contactID,
248 'location_type_id' => $this->_locationType->id,
249 ));
250 $this->assertAPISuccess($get, 'In line ' . __LINE__);
251 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__);
252
253 // check emails at location #2 -- preserve the original two
254 $get = civicrm_api('email', 'get', array(
255 'version' => $this->_apiversion,
256 'contact_id' => $this->_contactID,
257 'location_type_id' => $this->_locationType2->id,
258 ));
259 $this->assertAPISuccess($get, 'In line ' . __LINE__);
260 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__);
261
262 // replace the set of emails with an empty set
263 $replace3Params = array(
264 'version' => $this->_apiversion,
265 'contact_id' => $this->_contactID,
266 'values' => array(),
267 );
268 $replace3 = civicrm_api('email', 'replace', $replace3Params);
269 // $this->documentMe($replace3Params, $replace3, __FUNCTION__, __FILE__);
270 $this->assertEquals(0, $replace3['is_error'], 'In line ' . __LINE__);
271 $this->assertEquals(0, $replace3['count'], 'In line ' . __LINE__);
272
273 // check emails
274 $get = civicrm_api('email', 'get', array(
275 'version' => $this->_apiversion,
276 'contact_id' => $this->_contactID,
277 ));
278 $this->assertAPISuccess($get, 'In line ' . __LINE__);
279 $this->assertEquals(0, $get['count'], 'Incorrect email count at ' . __LINE__);
280 }
281
282 public function testReplaceEmailsInChain() {
283 // check there are no emails to start with
284 $get = civicrm_api('email', 'get', array(
285 'version' => $this->_apiversion,
286 'contact_id' => $this->_contactID,
287 ));
288 $this->assertAPISuccess($get, 'In line ' . __LINE__);
289 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__);
290 $description = "example demonstrates use of Replace in a nested API call";
291 $subfile = "NestedReplaceEmail";
292 // initialize email list with three emails at loc #1 and two emails at loc #2
293 $getReplace1Params = array(
294 'version' => $this->_apiversion,
295 'id' => $this->_contactID,
296 'api.email.replace' => array(
297 'values' => array(
298 array(
299 'location_type_id' => $this->_locationType->id,
300 'email' => '1-1@example.com',
301 'is_primary' => 1,
302 ),
303 array(
304 'location_type_id' => $this->_locationType->id,
305 'email' => '1-2@example.com',
306 'is_primary' => 0,
307 ),
308 array(
309 'location_type_id' => $this->_locationType->id,
310 'email' => '1-3@example.com',
311 'is_primary' => 0,
312 ),
313 array(
314 'location_type_id' => $this->_locationType2->id,
315 'email' => '2-1@example.com',
316 'is_primary' => 0,
317 ),
318 array(
319 'location_type_id' => $this->_locationType2->id,
320 'email' => '2-2@example.com',
321 'is_primary' => 0,
322 ),
323 ),
324 ),
325 );
326 $getReplace1 = civicrm_api('contact', 'get', $getReplace1Params);
327
328 $this->documentMe($getReplace1Params, $getReplace1, __FUNCTION__, __FILE__, $description, $subfile);
329 $this->assertAPISuccess($getReplace1['values'][$this->_contactID]['api.email.replace'], 'In line ' . __LINE__);
330 $this->assertEquals(5, $getReplace1['values'][$this->_contactID]['api.email.replace']['count'], 'In line ' . __LINE__);
331
332 // check emails at location #1 or #2
333 $get = civicrm_api('email', 'get', array(
334 'version' => $this->_apiversion,
335 'contact_id' => $this->_contactID,
336 ));
337 $this->assertAPISuccess($get, 'In line ' . __LINE__);
338 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__);
339
340 // replace the subset of emails in location #1, but preserve location #2
341 $getReplace2Params = array(
342 'version' => $this->_apiversion,
343 'id' => $this->_contactID,
344 'api.email.replace' => array(
345 'location_type_id' => $this->_locationType->id,
346 'values' => array(
347 array(
348 'email' => '1-4@example.com',
349 'is_primary' => 1,
350 ),
351 ),
352 ),
353 );
354 $getReplace2 = civicrm_api('contact', 'get', $getReplace2Params);
355 // $this->documentMe($getReplace2Params, $getReplace2, __FUNCTION__, __FILE__);
356 $this->assertEquals(0, $getReplace2['values'][$this->_contactID]['api.email.replace']['is_error'], 'In line ' . __LINE__);
357 $this->assertEquals(1, $getReplace2['values'][$this->_contactID]['api.email.replace']['count'], 'In line ' . __LINE__);
358
359 // check emails at location #1 -- all three replaced by one
360 $get = civicrm_api('email', 'get', array(
361 'version' => $this->_apiversion,
362 'contact_id' => $this->_contactID,
363 'location_type_id' => $this->_locationType->id,
364 ));
365
366 $this->assertAPISuccess($get, 'In line ' . __LINE__);
367 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__);
368
369 // check emails at location #2 -- preserve the original two
370 $get = civicrm_api('email', 'get', array(
371 'version' => $this->_apiversion,
372 'contact_id' => $this->_contactID,
373 'location_type_id' => $this->_locationType2->id,
374 ));
375 $this->assertAPISuccess($get, 'In line ' . __LINE__);
376 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__);
377 }
378
379 public function testReplaceEmailWithId() {
380 // check there are no emails to start with
381 $get = civicrm_api('email', 'get', array(
382 'version' => $this->_apiversion,
383 'contact_id' => $this->_contactID,
384 ));
385 $this->assertAPISuccess($get, 'In line ' . __LINE__);
386 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__);
387
388 // initialize email address
389 $replace1Params = array(
390 'version' => $this->_apiversion,
391 'contact_id' => $this->_contactID,
392 'values' => array(
393 array(
394 'location_type_id' => $this->_locationType->id,
395 'email' => '1-1@example.com',
396 'is_primary' => 1,
397 'on_hold' => 1,
398 ),
399 ),
400 );
401 $replace1 = civicrm_api('email', 'replace', $replace1Params);
402 // $this->documentMe($replace1Params, $replace1, __FUNCTION__, __FILE__);
403 $this->assertAPISuccess($replace1, 'In line ' . __LINE__);
404 $this->assertEquals(1, $replace1['count'], 'In line ' . __LINE__);
405
406 $keys = array_keys($replace1['values']);
407 $emailID = array_shift($keys);
408
409 // update the email address, but preserve any other fields
410 $replace2Params = array(
411 'version' => $this->_apiversion,
412 'contact_id' => $this->_contactID,
413 'values' => array(
414 array(
415 'id' => $emailID,
416 'email' => '1-2@example.com',
417 ),
418 ),
419 );
420 $replace2 = civicrm_api('email', 'replace', $replace2Params);
421 // $this->documentMe($replace2Params, $replace2, __FUNCTION__, __FILE__);
422 $this->assertAPISuccess($replace2, 'In line ' . __LINE__);
423 $this->assertEquals(1, $replace2['count'], 'In line ' . __LINE__);
424
425 // ensure the 'email' was updated while other fields were preserved
426 $get = civicrm_api('email', 'get', array(
427 'version' => $this->_apiversion,
428 'contact_id' => $this->_contactID,
429 'location_type_id' => $this->_locationType->id,
430 ));
431 $this->assertAPISuccess($get, 'In line ' . __LINE__);
432 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__);
433 $this->assertEquals(1, $get['values'][$emailID]['is_primary'], 'In line ' . __LINE__);
434 $this->assertEquals(1, $get['values'][$emailID]['on_hold'], 'In line ' . __LINE__);
435 $this->assertEquals('1-2@example.com', $get['values'][$emailID]['email'], 'In line ' . __LINE__);
436 }
437 }
438