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