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