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