Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
b6708aeb | 2 | /* |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
b6708aeb | 5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | |
28 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
29 | class api_v3_EmailTest extends CiviUnitTestCase { | |
30 | protected $_apiversion; | |
31 | protected $_contactID; | |
32 | protected $_locationType; | |
33 | protected $_entity; | |
34 | protected $_params; | |
35 | public $_eNoticeCompliant = TRUE; | |
36 | function setUp() { | |
37 | $this->_apiversion = 3; | |
38 | $this->_entity = 'Email'; | |
39 | parent::setUp(); | |
40 | $this->_contactID = $this->organizationCreate(NULL); | |
41 | $this->_locationType = $this->locationTypeCreate(NULL); | |
42 | $this->_locationType2 = $this->locationTypeCreate(array( | |
43 | 'name' => 'New Location Type 2', | |
44 | 'vcard_name' => 'New Location Type 2', | |
45 | 'description' => 'Another Location Type', | |
46 | 'is_active' => 1, | |
47 | )); | |
48 | $this->_params = array( | |
49 | 'contact_id' => $this->_contactID, | |
50 | 'location_type_id' => $this->_locationType->id, | |
51 | 'email' => 'api@a-team.com', | |
52 | 'is_primary' => 1, | |
e5eb3437 | 53 | |
6a488035 TO |
54 | //TODO email_type_id |
55 | ); | |
56 | } | |
57 | ||
58 | function tearDown() { | |
59 | $this->contactDelete($this->_contactID); | |
60 | $this->locationTypeDelete($this->_locationType->id); | |
61 | $this->locationTypeDelete($this->_locationType2->id); | |
62 | } | |
63 | public function testCreateEmail() { | |
64 | $params = $this->_params; | |
65 | //check there are no emails to start with | |
e5eb3437 | 66 | $get = $this->callAPISuccess('email', 'get', array( |
67 | 'location_type_id' => $this->_locationType->id, | |
68 | )); | |
6a488035 | 69 | $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__); |
6a488035 | 70 | |
e5eb3437 | 71 | $result = $this->callAPIAndDocument('email', 'create', $params, __FUNCTION__, __FILE__); |
6a488035 TO |
72 | $this->assertEquals(1, $result['count'], 'In line ' . __LINE__); |
73 | $this->assertNotNull($result['id'], 'In line ' . __LINE__); | |
74 | $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__); | |
e5eb3437 | 75 | $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id'])); |
6a488035 TO |
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 | ||
83 | ||
84 | ||
85 | public function testCreateEmailPrimaryHandlingChangeToPrimary() { | |
86 | $params = $this->_params; | |
87 | unset($params['is_primary']); | |
e5eb3437 | 88 | $email1 = $this->callAPISuccess('email', 'create', $params); |
6a488035 | 89 | //now we check & make sure it has been set to primary |
e5eb3437 | 90 | $expected = 1; |
91 | $check = $this->callAPISuccess('email', 'getcount', array( | |
92 | 'is_primary' => 1, | |
93 | 'id' => $email1['id'], | |
94 | ), | |
95 | $expected | |
96 | ); | |
6a488035 | 97 | } |
e5eb3437 | 98 | |
6a488035 | 99 | public function testCreateEmailPrimaryHandlingChangeExisting() { |
e5eb3437 | 100 | $email1 = $this->callAPISuccess('email', 'create', $this->_params); |
101 | $email2 = $this->callAPISuccess('email', 'create', $this->_params); | |
102 | $check = $this->callAPISuccess('email', 'getcount', array( | |
6a488035 TO |
103 | 'is_primary' => 1, |
104 | 'contact_id' => $this->_contactID, | |
105 | )); | |
106 | $this->assertEquals(1, $check); | |
107 | } | |
108 | ||
109 | public function testCreateEmailWithoutEmail() { | |
e5eb3437 | 110 | $result = $this->callAPIFailure('Email', 'Create', array('contact_id' => 4)); |
6a488035 TO |
111 | $this->assertContains('missing', $result['error_message'], 'In line ' . __LINE__); |
112 | $this->assertContains('email', $result['error_message'], 'In line ' . __LINE__); | |
113 | } | |
114 | ||
115 | public function testGetEmail() { | |
e5eb3437 | 116 | $result = $this->callAPISuccess('email', 'create', $this->_params); |
117 | $get = $this->callAPISuccess('email', 'create', $this->_params); | |
6a488035 | 118 | $this->assertEquals($get['count'], 1); |
e5eb3437 | 119 | $get = $this->callAPISuccess('email', 'create', $this->_params + array('debug' => 1)); |
6a488035 | 120 | $this->assertEquals($get['count'], 1); |
e5eb3437 | 121 | $get = $this->callAPISuccess('email', 'create', $this->_params + array('debug' => 1, 'action' => 'get')); |
6a488035 | 122 | $this->assertEquals($get['count'], 1); |
e5eb3437 | 123 | $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id'])); |
6a488035 TO |
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, | |
e5eb3437 | 131 | |
6a488035 TO |
132 | //TODO email_type_id |
133 | ); | |
134 | //check there are no emails to start with | |
e5eb3437 | 135 | $get = $this->callAPISuccess('email', 'get', array( |
136 | 'location_type_id' => $this->_locationType->id, | |
137 | )); | |
6a488035 TO |
138 | $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__); |
139 | ||
140 | //create one | |
e5eb3437 | 141 | $create = $this->callAPISuccess('email', 'create', $params); |
6a488035 | 142 | |
e5eb3437 | 143 | $result = $this->callAPIAndDocument('email', 'delete', array('id' => $create['id'],), __FUNCTION__, __FILE__); |
6a488035 | 144 | $this->assertEquals(1, $result['count'], 'In line ' . __LINE__); |
e5eb3437 | 145 | $get = $this->callAPISuccess('email', 'get', array( |
146 | 'location_type_id' => $this->_locationType->id, | |
147 | )); | |
6a488035 TO |
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 | |
e5eb3437 | 153 | $get = $this->callAPISuccess('email', 'get', array( |
6a488035 TO |
154 | 'contact_id' => $this->_contactID, |
155 | )); | |
6a488035 TO |
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( | |
6a488035 TO |
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 | ); | |
e5eb3437 | 189 | $replace1 = $this->callAPIAndDocument('email', 'replace', $replace1Params, __FUNCTION__, __FILE__); |
6a488035 TO |
190 | $this->assertEquals(5, $replace1['count'], 'In line ' . __LINE__); |
191 | ||
192 | // check emails at location #1 or #2 | |
e5eb3437 | 193 | $get = $this->callAPISuccess('email', 'get', array( |
6a488035 TO |
194 | 'contact_id' => $this->_contactID, |
195 | )); | |
6a488035 TO |
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( | |
6a488035 TO |
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 | ); | |
e5eb3437 | 209 | $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params); |
6a488035 TO |
210 | $this->assertEquals(1, $replace2['count'], 'In line ' . __LINE__); |
211 | ||
212 | // check emails at location #1 -- all three replaced by one | |
e5eb3437 | 213 | $get = $this->callAPISuccess('email', 'get', array( |
214 | 'contact_id' => $this->_contactID, | |
215 | 'location_type_id' => $this->_locationType->id, | |
216 | )); | |
6a488035 TO |
217 | $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__); |
218 | ||
219 | // check emails at location #2 -- preserve the original two | |
e5eb3437 | 220 | $get = $this->callAPISuccess('email', 'get', array( |
221 | 'contact_id' => $this->_contactID, | |
222 | 'location_type_id' => $this->_locationType2->id, | |
223 | )); | |
224 | ||
6a488035 TO |
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( | |
6a488035 TO |
229 | 'contact_id' => $this->_contactID, |
230 | 'values' => array(), | |
231 | ); | |
e5eb3437 | 232 | $replace3 = $this->callAPISuccess('email', 'replace', $replace3Params); |
6a488035 TO |
233 | $this->assertEquals(0, $replace3['count'], 'In line ' . __LINE__); |
234 | ||
235 | // check emails | |
e5eb3437 | 236 | $get = $this->callAPISuccess('email', 'get', array( |
237 | ||
6a488035 TO |
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 | |
e5eb3437 | 246 | $get = $this->callAPISuccess('email', 'get', array( |
247 | ||
6a488035 TO |
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 = "example 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( | |
e5eb3437 | 256 | |
6a488035 TO |
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 | ); | |
e5eb3437 | 288 | $getReplace1 = $this->callAPIAndDocument('contact', 'get', $getReplace1Params, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
289 | $this->assertEquals(5, $getReplace1['values'][$this->_contactID]['api.email.replace']['count'], 'In line ' . __LINE__); |
290 | ||
291 | // check emails at location #1 or #2 | |
e5eb3437 | 292 | $get = $this->callAPISuccess('email', 'get', array( |
6a488035 TO |
293 | 'contact_id' => $this->_contactID, |
294 | )); | |
6a488035 TO |
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( | |
6a488035 TO |
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 | ); | |
e5eb3437 | 310 | $getReplace2 = $this->callAPISuccess('contact', 'get', $getReplace2Params); |
6a488035 TO |
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 | |
e5eb3437 | 315 | $get = $this->callAPISuccess('email', 'get', array( |
316 | 'contact_id' => $this->_contactID, | |
317 | 'location_type_id' => $this->_locationType->id, | |
318 | )); | |
6a488035 | 319 | |
6a488035 TO |
320 | $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__); |
321 | ||
322 | // check emails at location #2 -- preserve the original two | |
e5eb3437 | 323 | $get = $this->callAPISuccess('email', 'get', array( |
324 | 'contact_id' => $this->_contactID, | |
325 | 'location_type_id' => $this->_locationType2->id, | |
326 | )); | |
6a488035 TO |
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 | |
e5eb3437 | 332 | $get = $this->callAPISuccess('email', 'get', array( |
6a488035 TO |
333 | 'contact_id' => $this->_contactID, |
334 | )); | |
6a488035 TO |
335 | $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__); |
336 | ||
337 | // initialize email address | |
338 | $replace1Params = array( | |
6a488035 TO |
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 | ); | |
e5eb3437 | 349 | $replace1 = $this->callAPISuccess('email', 'replace', $replace1Params); |
6a488035 TO |
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( | |
6a488035 TO |
357 | 'contact_id' => $this->_contactID, |
358 | 'values' => array( | |
359 | array( | |
360 | 'id' => $emailID, | |
361 | 'email' => '1-2@example.com', | |
362 | ), | |
363 | ), | |
364 | ); | |
e5eb3437 | 365 | $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params); |
6a488035 TO |
366 | $this->assertEquals(1, $replace2['count'], 'In line ' . __LINE__); |
367 | ||
368 | // ensure the 'email' was updated while other fields were preserved | |
e5eb3437 | 369 | $get = $this->callAPISuccess('email', 'get', array( |
370 | 'contact_id' => $this->_contactID, | |
371 | 'location_type_id' => $this->_locationType->id, | |
372 | )); | |
373 | ||
6a488035 TO |
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 |