Merge branch '4.5' into master
[civicrm-core.git] / tests / phpunit / api / v3 / EmailTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 require_once 'CiviTest/CiviUnitTestCase.php';
29
30 /**
31 * Class api_v3_EmailTest
32 */
33 class api_v3_EmailTest extends CiviUnitTestCase {
34 protected $_apiversion;
35 protected $_contactID;
36 protected $_locationType;
37 protected $_entity;
38 protected $_params;
39
40 public function setUp() {
41 $this->_apiversion = 3;
42 $this->_entity = 'Email';
43 parent::setUp();
44 $this->useTransaction(TRUE);
45
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',
52 'is_active' => 1,
53 ));
54 $this->_params = array(
55 'contact_id' => $this->_contactID,
56 'location_type_id' => $this->_locationType->id,
57 'email' => 'api@a-team.com',
58 'is_primary' => 1,
59
60 //TODO email_type_id
61 );
62 }
63
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,
69 ));
70 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__);
71
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']));
77 }
78
79 /*
80 * If a new email is set to is_primary the prev should no longer be
81 *
82 * If is_primary is not set then it should become is_primary is no others exist
83 */
84
85
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
91 $expected = 1;
92 $check = $this->callAPISuccess('email', 'getcount', array(
93 'is_primary' => 1,
94 'id' => $email1['id'],
95 ),
96 $expected
97 );
98 }
99
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(
104 'is_primary' => 1,
105 'contact_id' => $this->_contactID,
106 ));
107 $this->assertEquals(1, $check);
108 }
109
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__);
114 }
115
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']));
125 }
126
127 public function testDeleteEmail() {
128 $params = array(
129 'contact_id' => $this->_contactID,
130 'location_type_id' => $this->_locationType->id,
131 'email' => 'api@a-team.com',
132 'is_primary' => 1,
133
134 //TODO email_type_id
135 );
136 //check there are no emails to start with
137 $get = $this->callAPISuccess('email', 'get', array(
138 'location_type_id' => $this->_locationType->id,
139 ));
140 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__);
141
142 //create one
143 $create = $this->callAPISuccess('email', 'create', $params);
144
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,
149 ));
150 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__);
151 }
152
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,
157 ));
158 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__);
159
160 // initialize email list with three emails at loc #1 and two emails at loc #2
161 $replace1Params = array(
162 'contact_id' => $this->_contactID,
163 'values' => array(
164 array(
165 'location_type_id' => $this->_locationType->id,
166 'email' => '1-1@example.com',
167 'is_primary' => 1,
168 ),
169 array(
170 'location_type_id' => $this->_locationType->id,
171 'email' => '1-2@example.com',
172 'is_primary' => 0,
173 ),
174 array(
175 'location_type_id' => $this->_locationType->id,
176 'email' => '1-3@example.com',
177 'is_primary' => 0,
178 ),
179 array(
180 'location_type_id' => $this->_locationType2->id,
181 'email' => '2-1@example.com',
182 'is_primary' => 0,
183 ),
184 array(
185 'location_type_id' => $this->_locationType2->id,
186 'email' => '2-2@example.com',
187 'is_primary' => 0,
188 ),
189 ),
190 );
191 $replace1 = $this->callAPIAndDocument('email', 'replace', $replace1Params, __FUNCTION__, __FILE__);
192 $this->assertEquals(5, $replace1['count'], 'In line ' . __LINE__);
193
194 // check emails at location #1 or #2
195 $get = $this->callAPISuccess('email', 'get', array(
196 'contact_id' => $this->_contactID,
197 ));
198 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__);
199
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,
204 'values' => array(
205 array(
206 'email' => '1-4@example.com',
207 'is_primary' => 1,
208 ),
209 ),
210 );
211 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
212 $this->assertEquals(1, $replace2['count'], 'In line ' . __LINE__);
213
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,
218 ));
219 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__);
220
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,
225 ));
226
227 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__);
228
229 // replace the set of emails with an empty set
230 $replace3Params = array(
231 'contact_id' => $this->_contactID,
232 'values' => array(),
233 );
234 $replace3 = $this->callAPISuccess('email', 'replace', $replace3Params);
235 $this->assertEquals(0, $replace3['count'], 'In line ' . __LINE__);
236
237 // check emails
238 $get = $this->callAPISuccess('email', 'get', array(
239
240 'contact_id' => $this->_contactID,
241 ));
242 $this->assertAPISuccess($get, 'In line ' . __LINE__);
243 $this->assertEquals(0, $get['count'], 'Incorrect email count at ' . __LINE__);
244 }
245
246 public function testReplaceEmailsInChain() {
247 // check there are no emails to start with
248 $get = $this->callAPISuccess('email', 'get', array(
249
250 'contact_id' => $this->_contactID,
251 ));
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(
258
259 'id' => $this->_contactID,
260 'api.email.replace' => array(
261 'values' => array(
262 array(
263 'location_type_id' => $this->_locationType->id,
264 'email' => '1-1@example.com',
265 'is_primary' => 1,
266 ),
267 array(
268 'location_type_id' => $this->_locationType->id,
269 'email' => '1-2@example.com',
270 'is_primary' => 0,
271 ),
272 array(
273 'location_type_id' => $this->_locationType->id,
274 'email' => '1-3@example.com',
275 'is_primary' => 0,
276 ),
277 array(
278 'location_type_id' => $this->_locationType2->id,
279 'email' => '2-1@example.com',
280 'is_primary' => 0,
281 ),
282 array(
283 'location_type_id' => $this->_locationType2->id,
284 'email' => '2-2@example.com',
285 'is_primary' => 0,
286 ),
287 ),
288 ),
289 );
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__);
292
293 // check emails at location #1 or #2
294 $get = $this->callAPISuccess('email', 'get', array(
295 'contact_id' => $this->_contactID,
296 ));
297 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__);
298
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,
304 'values' => array(
305 array(
306 'email' => '1-4@example.com',
307 'is_primary' => 1,
308 ),
309 ),
310 ),
311 );
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__);
315
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,
320 ));
321
322 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__);
323
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,
328 ));
329 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__);
330 }
331
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,
336 ));
337 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__);
338
339 // initialize email address
340 $replace1Params = array(
341 'contact_id' => $this->_contactID,
342 'values' => array(
343 array(
344 'location_type_id' => $this->_locationType->id,
345 'email' => '1-1@example.com',
346 'is_primary' => 1,
347 'on_hold' => 1,
348 ),
349 ),
350 );
351 $replace1 = $this->callAPISuccess('email', 'replace', $replace1Params);
352 $this->assertEquals(1, $replace1['count'], 'In line ' . __LINE__);
353
354 $keys = array_keys($replace1['values']);
355 $emailID = array_shift($keys);
356
357 // update the email address, but preserve any other fields
358 $replace2Params = array(
359 'contact_id' => $this->_contactID,
360 'values' => array(
361 array(
362 'id' => $emailID,
363 'email' => '1-2@example.com',
364 ),
365 ),
366 );
367 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
368 $this->assertEquals(1, $replace2['count'], 'In line ' . __LINE__);
369
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,
374 ));
375
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__);
380 }
381 }