add autogenerated comment blocks to tests dir
[civicrm-core.git] / tests / phpunit / api / v3 / EmailTest.php
CommitLineData
6a488035 1<?php
b6708aeb 2/*
3 +--------------------------------------------------------------------+
06a1bc01 4| CiviCRM version 4.5 |
b6708aeb 5+--------------------------------------------------------------------+
06a1bc01 6| Copyright CiviCRM LLC (c) 2004-2014 |
b6708aeb 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*/
6a488035
TO
27
28require_once 'CiviTest/CiviUnitTestCase.php';
e9479dcf
EM
29
30/**
31 * Class api_v3_EmailTest
32 */
6a488035
TO
33class api_v3_EmailTest extends CiviUnitTestCase {
34 protected $_apiversion;
35 protected $_contactID;
36 protected $_locationType;
37 protected $_entity;
38 protected $_params;
b7c9bc4c 39
6a488035
TO
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,
e5eb3437 57
6a488035
TO
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
e5eb3437 70 $get = $this->callAPISuccess('email', 'get', array(
71 'location_type_id' => $this->_locationType->id,
72 ));
6a488035 73 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__);
6a488035 74
e5eb3437 75 $result = $this->callAPIAndDocument('email', 'create', $params, __FUNCTION__, __FILE__);
6a488035
TO
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__);
e5eb3437 79 $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id']));
6a488035
TO
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']);
e5eb3437 92 $email1 = $this->callAPISuccess('email', 'create', $params);
6a488035 93 //now we check & make sure it has been set to primary
e5eb3437 94 $expected = 1;
95 $check = $this->callAPISuccess('email', 'getcount', array(
96 'is_primary' => 1,
97 'id' => $email1['id'],
98 ),
99 $expected
100 );
6a488035 101 }
e5eb3437 102
6a488035 103 public function testCreateEmailPrimaryHandlingChangeExisting() {
e5eb3437 104 $email1 = $this->callAPISuccess('email', 'create', $this->_params);
105 $email2 = $this->callAPISuccess('email', 'create', $this->_params);
106 $check = $this->callAPISuccess('email', 'getcount', array(
6a488035
TO
107 'is_primary' => 1,
108 'contact_id' => $this->_contactID,
109 ));
110 $this->assertEquals(1, $check);
111 }
112
113 public function testCreateEmailWithoutEmail() {
e5eb3437 114 $result = $this->callAPIFailure('Email', 'Create', array('contact_id' => 4));
6a488035
TO
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() {
e5eb3437 120 $result = $this->callAPISuccess('email', 'create', $this->_params);
121 $get = $this->callAPISuccess('email', 'create', $this->_params);
6a488035 122 $this->assertEquals($get['count'], 1);
e5eb3437 123 $get = $this->callAPISuccess('email', 'create', $this->_params + array('debug' => 1));
6a488035 124 $this->assertEquals($get['count'], 1);
e5eb3437 125 $get = $this->callAPISuccess('email', 'create', $this->_params + array('debug' => 1, 'action' => 'get'));
6a488035 126 $this->assertEquals($get['count'], 1);
e5eb3437 127 $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id']));
6a488035
TO
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,
e5eb3437 135
6a488035
TO
136 //TODO email_type_id
137 );
138 //check there are no emails to start with
e5eb3437 139 $get = $this->callAPISuccess('email', 'get', array(
140 'location_type_id' => $this->_locationType->id,
141 ));
6a488035
TO
142 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__);
143
144 //create one
e5eb3437 145 $create = $this->callAPISuccess('email', 'create', $params);
6a488035 146
e5eb3437 147 $result = $this->callAPIAndDocument('email', 'delete', array('id' => $create['id'],), __FUNCTION__, __FILE__);
6a488035 148 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
e5eb3437 149 $get = $this->callAPISuccess('email', 'get', array(
150 'location_type_id' => $this->_locationType->id,
151 ));
6a488035
TO
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
e5eb3437 157 $get = $this->callAPISuccess('email', 'get', array(
6a488035
TO
158 'contact_id' => $this->_contactID,
159 ));
6a488035
TO
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(
6a488035
TO
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 );
e5eb3437 193 $replace1 = $this->callAPIAndDocument('email', 'replace', $replace1Params, __FUNCTION__, __FILE__);
6a488035
TO
194 $this->assertEquals(5, $replace1['count'], 'In line ' . __LINE__);
195
196 // check emails at location #1 or #2
e5eb3437 197 $get = $this->callAPISuccess('email', 'get', array(
6a488035
TO
198 'contact_id' => $this->_contactID,
199 ));
6a488035
TO
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(
6a488035
TO
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 );
e5eb3437 213 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
6a488035
TO
214 $this->assertEquals(1, $replace2['count'], 'In line ' . __LINE__);
215
216 // check emails at location #1 -- all three replaced by one
e5eb3437 217 $get = $this->callAPISuccess('email', 'get', array(
218 'contact_id' => $this->_contactID,
219 'location_type_id' => $this->_locationType->id,
220 ));
6a488035
TO
221 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__);
222
223 // check emails at location #2 -- preserve the original two
e5eb3437 224 $get = $this->callAPISuccess('email', 'get', array(
225 'contact_id' => $this->_contactID,
226 'location_type_id' => $this->_locationType2->id,
227 ));
228
6a488035
TO
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(
6a488035
TO
233 'contact_id' => $this->_contactID,
234 'values' => array(),
235 );
e5eb3437 236 $replace3 = $this->callAPISuccess('email', 'replace', $replace3Params);
6a488035
TO
237 $this->assertEquals(0, $replace3['count'], 'In line ' . __LINE__);
238
239 // check emails
e5eb3437 240 $get = $this->callAPISuccess('email', 'get', array(
241
6a488035
TO
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
e5eb3437 250 $get = $this->callAPISuccess('email', 'get', array(
251
6a488035
TO
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(
e5eb3437 260
6a488035
TO
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 );
e5eb3437 292 $getReplace1 = $this->callAPIAndDocument('contact', 'get', $getReplace1Params, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
293 $this->assertEquals(5, $getReplace1['values'][$this->_contactID]['api.email.replace']['count'], 'In line ' . __LINE__);
294
295 // check emails at location #1 or #2
e5eb3437 296 $get = $this->callAPISuccess('email', 'get', array(
6a488035
TO
297 'contact_id' => $this->_contactID,
298 ));
6a488035
TO
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(
6a488035
TO
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 );
e5eb3437 314 $getReplace2 = $this->callAPISuccess('contact', 'get', $getReplace2Params);
6a488035
TO
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
e5eb3437 319 $get = $this->callAPISuccess('email', 'get', array(
320 'contact_id' => $this->_contactID,
321 'location_type_id' => $this->_locationType->id,
322 ));
6a488035 323
6a488035
TO
324 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__);
325
326 // check emails at location #2 -- preserve the original two
e5eb3437 327 $get = $this->callAPISuccess('email', 'get', array(
328 'contact_id' => $this->_contactID,
329 'location_type_id' => $this->_locationType2->id,
330 ));
6a488035
TO
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
e5eb3437 336 $get = $this->callAPISuccess('email', 'get', array(
6a488035
TO
337 'contact_id' => $this->_contactID,
338 ));
6a488035
TO
339 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__);
340
341 // initialize email address
342 $replace1Params = array(
6a488035
TO
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 );
e5eb3437 353 $replace1 = $this->callAPISuccess('email', 'replace', $replace1Params);
6a488035
TO
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(
6a488035
TO
361 'contact_id' => $this->_contactID,
362 'values' => array(
363 array(
364 'id' => $emailID,
365 'email' => '1-2@example.com',
366 ),
367 ),
368 );
e5eb3437 369 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
6a488035
TO
370 $this->assertEquals(1, $replace2['count'], 'In line ' . __LINE__);
371
372 // ensure the 'email' was updated while other fields were preserved
e5eb3437 373 $get = $this->callAPISuccess('email', 'get', array(
374 'contact_id' => $this->_contactID,
375 'location_type_id' => $this->_locationType->id,
376 ));
377
6a488035
TO
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