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