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