Merge pull request #4794 from civicrm/CRM-15740
[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 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 * If a new email is set to is_primary the prev should no longer be
80 *
81 * If is_primary is not set then it should become is_primary is no others exist
82 */
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 public function testDeleteEmail() {
127 $params = array(
128 'contact_id' => $this->_contactID,
129 'location_type_id' => $this->_locationType->id,
130 'email' => 'api@a-team.com',
131 'is_primary' => 1,
132
133 //TODO email_type_id
134 );
135 //check there are no emails to start with
136 $get = $this->callAPISuccess('email', 'get', array(
137 'location_type_id' => $this->_locationType->id,
138 ));
139 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__);
140
141 //create one
142 $create = $this->callAPISuccess('email', 'create', $params);
143
144 $result = $this->callAPIAndDocument('email', 'delete', array('id' => $create['id'],), __FUNCTION__, __FILE__);
145 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
146 $get = $this->callAPISuccess('email', 'get', array(
147 'location_type_id' => $this->_locationType->id,
148 ));
149 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__);
150 }
151
152 public function testReplaceEmail() {
153 // check there are no emails to start with
154 $get = $this->callAPISuccess('email', 'get', array(
155 'contact_id' => $this->_contactID,
156 ));
157 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__);
158
159 // initialize email list with three emails at loc #1 and two emails at loc #2
160 $replace1Params = array(
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 = $this->callAPIAndDocument('email', 'replace', $replace1Params, __FUNCTION__, __FILE__);
191 $this->assertEquals(5, $replace1['count'], 'In line ' . __LINE__);
192
193 // check emails at location #1 or #2
194 $get = $this->callAPISuccess('email', 'get', array(
195 'contact_id' => $this->_contactID,
196 ));
197 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__);
198
199 // replace the subset of emails in location #1, but preserve location #2
200 $replace2Params = array(
201 'contact_id' => $this->_contactID,
202 'location_type_id' => $this->_locationType->id,
203 'values' => array(
204 array(
205 'email' => '1-4@example.com',
206 'is_primary' => 1,
207 ),
208 ),
209 );
210 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
211 $this->assertEquals(1, $replace2['count'], 'In line ' . __LINE__);
212
213 // check emails at location #1 -- all three replaced by one
214 $get = $this->callAPISuccess('email', 'get', array(
215 'contact_id' => $this->_contactID,
216 'location_type_id' => $this->_locationType->id,
217 ));
218 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__);
219
220 // check emails at location #2 -- preserve the original two
221 $get = $this->callAPISuccess('email', 'get', array(
222 'contact_id' => $this->_contactID,
223 'location_type_id' => $this->_locationType2->id,
224 ));
225
226 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__);
227
228 // replace the set of emails with an empty set
229 $replace3Params = array(
230 'contact_id' => $this->_contactID,
231 'values' => array(),
232 );
233 $replace3 = $this->callAPISuccess('email', 'replace', $replace3Params);
234 $this->assertEquals(0, $replace3['count'], 'In line ' . __LINE__);
235
236 // check emails
237 $get = $this->callAPISuccess('email', 'get', array(
238
239 'contact_id' => $this->_contactID,
240 ));
241 $this->assertAPISuccess($get, 'In line ' . __LINE__);
242 $this->assertEquals(0, $get['count'], 'Incorrect email count at ' . __LINE__);
243 }
244
245 public function testReplaceEmailsInChain() {
246 // check there are no emails to start with
247 $get = $this->callAPISuccess('email', 'get', array(
248
249 'contact_id' => $this->_contactID,
250 ));
251 $this->assertAPISuccess($get, 'In line ' . __LINE__);
252 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__);
253 $description = "example demonstrates use of Replace in a nested API call";
254 $subfile = "NestedReplaceEmail";
255 // initialize email list with three emails at loc #1 and two emails at loc #2
256 $getReplace1Params = array(
257
258 'id' => $this->_contactID,
259 'api.email.replace' => array(
260 'values' => array(
261 array(
262 'location_type_id' => $this->_locationType->id,
263 'email' => '1-1@example.com',
264 'is_primary' => 1,
265 ),
266 array(
267 'location_type_id' => $this->_locationType->id,
268 'email' => '1-2@example.com',
269 'is_primary' => 0,
270 ),
271 array(
272 'location_type_id' => $this->_locationType->id,
273 'email' => '1-3@example.com',
274 'is_primary' => 0,
275 ),
276 array(
277 'location_type_id' => $this->_locationType2->id,
278 'email' => '2-1@example.com',
279 'is_primary' => 0,
280 ),
281 array(
282 'location_type_id' => $this->_locationType2->id,
283 'email' => '2-2@example.com',
284 'is_primary' => 0,
285 ),
286 ),
287 ),
288 );
289 $getReplace1 = $this->callAPIAndDocument('contact', 'get', $getReplace1Params, __FUNCTION__, __FILE__, $description, $subfile);
290 $this->assertEquals(5, $getReplace1['values'][$this->_contactID]['api.email.replace']['count'], 'In line ' . __LINE__);
291
292 // check emails at location #1 or #2
293 $get = $this->callAPISuccess('email', 'get', array(
294 'contact_id' => $this->_contactID,
295 ));
296 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__);
297
298 // replace the subset of emails in location #1, but preserve location #2
299 $getReplace2Params = array(
300 'id' => $this->_contactID,
301 'api.email.replace' => array(
302 'location_type_id' => $this->_locationType->id,
303 'values' => array(
304 array(
305 'email' => '1-4@example.com',
306 'is_primary' => 1,
307 ),
308 ),
309 ),
310 );
311 $getReplace2 = $this->callAPISuccess('contact', 'get', $getReplace2Params);
312 $this->assertEquals(0, $getReplace2['values'][$this->_contactID]['api.email.replace']['is_error'], 'In line ' . __LINE__);
313 $this->assertEquals(1, $getReplace2['values'][$this->_contactID]['api.email.replace']['count'], 'In line ' . __LINE__);
314
315 // check emails at location #1 -- all three replaced by one
316 $get = $this->callAPISuccess('email', 'get', array(
317 'contact_id' => $this->_contactID,
318 'location_type_id' => $this->_locationType->id,
319 ));
320
321 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__);
322
323 // check emails at location #2 -- preserve the original two
324 $get = $this->callAPISuccess('email', 'get', array(
325 'contact_id' => $this->_contactID,
326 'location_type_id' => $this->_locationType2->id,
327 ));
328 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__);
329 }
330
331 public function testReplaceEmailWithId() {
332 // check there are no emails to start with
333 $get = $this->callAPISuccess('email', 'get', array(
334 'contact_id' => $this->_contactID,
335 ));
336 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__);
337
338 // initialize email address
339 $replace1Params = array(
340 'contact_id' => $this->_contactID,
341 'values' => array(
342 array(
343 'location_type_id' => $this->_locationType->id,
344 'email' => '1-1@example.com',
345 'is_primary' => 1,
346 'on_hold' => 1,
347 ),
348 ),
349 );
350 $replace1 = $this->callAPISuccess('email', 'replace', $replace1Params);
351 $this->assertEquals(1, $replace1['count'], 'In line ' . __LINE__);
352
353 $keys = array_keys($replace1['values']);
354 $emailID = array_shift($keys);
355
356 // update the email address, but preserve any other fields
357 $replace2Params = array(
358 'contact_id' => $this->_contactID,
359 'values' => array(
360 array(
361 'id' => $emailID,
362 'email' => '1-2@example.com',
363 ),
364 ),
365 );
366 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
367 $this->assertEquals(1, $replace2['count'], 'In line ' . __LINE__);
368
369 // ensure the 'email' was updated while other fields were preserved
370 $get = $this->callAPISuccess('email', 'get', array(
371 'contact_id' => $this->_contactID,
372 'location_type_id' => $this->_locationType->id,
373 ));
374
375 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__);
376 $this->assertEquals(1, $get['values'][$emailID]['is_primary'], 'In line ' . __LINE__);
377 $this->assertEquals(1, $get['values'][$emailID]['on_hold'], 'In line ' . __LINE__);
378 $this->assertEquals('1-2@example.com', $get['values'][$emailID]['email'], 'In line ' . __LINE__);
379 }
380 }
381