Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
b6708aeb | 2 | /* |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
b6708aeb | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 TO |
27 | |
28 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
e9479dcf EM |
29 | |
30 | /** | |
31 | * Class api_v3_EmailTest | |
32 | */ | |
6a488035 TO |
33 | class api_v3_EmailTest extends CiviUnitTestCase { |
34 | protected $_apiversion; | |
35 | protected $_contactID; | |
36 | protected $_locationType; | |
37 | protected $_entity; | |
38 | protected $_params; | |
b7c9bc4c | 39 | |
00be9182 | 40 | public function setUp() { |
6a488035 TO |
41 | $this->_apiversion = 3; |
42 | $this->_entity = 'Email'; | |
43 | parent::setUp(); | |
881567d2 TO |
44 | $this->useTransaction(TRUE); |
45 | ||
6a488035 TO |
46 | $this->_contactID = $this->organizationCreate(NULL); |
47 | $this->_locationType = $this->locationTypeCreate(NULL); | |
48 | $this->_locationType2 = $this->locationTypeCreate(array( | |
5896d037 TO |
49 | 'name' => 'New Location Type 2', |
50 | 'vcard_name' => 'New Location Type 2', | |
51 | 'description' => 'Another Location Type', | |
52 | 'is_active' => 1, | |
53 | )); | |
6a488035 TO |
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, | |
e5eb3437 | 59 | |
6a488035 TO |
60 | //TODO email_type_id |
61 | ); | |
62 | } | |
63 | ||
6a488035 TO |
64 | public function testCreateEmail() { |
65 | $params = $this->_params; | |
66 | //check there are no emails to start with | |
e5eb3437 | 67 | $get = $this->callAPISuccess('email', 'get', array( |
68 | 'location_type_id' => $this->_locationType->id, | |
69 | )); | |
6a488035 | 70 | $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__); |
6a488035 | 71 | |
e5eb3437 | 72 | $result = $this->callAPIAndDocument('email', 'create', $params, __FUNCTION__, __FILE__); |
ba4a1892 TM |
73 | $this->assertEquals(1, $result['count']); |
74 | $this->assertNotNull($result['id']); | |
75 | $this->assertNotNull($result['values'][$result['id']]['id']); | |
e5eb3437 | 76 | $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id'])); |
6a488035 | 77 | } |
5896d037 | 78 | |
8d7a9d07 | 79 | /** |
fe482240 | 80 | * If a new email is set to is_primary the prev should no longer be. |
6a488035 TO |
81 | * |
82 | * If is_primary is not set then it should become is_primary is no others exist | |
83 | */ | |
6a488035 TO |
84 | public function testCreateEmailPrimaryHandlingChangeToPrimary() { |
85 | $params = $this->_params; | |
86 | unset($params['is_primary']); | |
e5eb3437 | 87 | $email1 = $this->callAPISuccess('email', 'create', $params); |
6a488035 | 88 | //now we check & make sure it has been set to primary |
e5eb3437 | 89 | $expected = 1; |
90 | $check = $this->callAPISuccess('email', 'getcount', array( | |
5896d037 TO |
91 | 'is_primary' => 1, |
92 | 'id' => $email1['id'], | |
e5eb3437 | 93 | ), |
94 | $expected | |
5896d037 | 95 | ); |
6a488035 | 96 | } |
e5eb3437 | 97 | |
6a488035 | 98 | public function testCreateEmailPrimaryHandlingChangeExisting() { |
e5eb3437 | 99 | $email1 = $this->callAPISuccess('email', 'create', $this->_params); |
100 | $email2 = $this->callAPISuccess('email', 'create', $this->_params); | |
101 | $check = $this->callAPISuccess('email', 'getcount', array( | |
5896d037 TO |
102 | 'is_primary' => 1, |
103 | 'contact_id' => $this->_contactID, | |
104 | )); | |
6a488035 TO |
105 | $this->assertEquals(1, $check); |
106 | } | |
107 | ||
108 | public function testCreateEmailWithoutEmail() { | |
e5eb3437 | 109 | $result = $this->callAPIFailure('Email', 'Create', array('contact_id' => 4)); |
ba4a1892 TM |
110 | $this->assertContains('missing', $result['error_message']); |
111 | $this->assertContains('email', $result['error_message']); | |
6a488035 TO |
112 | } |
113 | ||
114 | public function testGetEmail() { | |
e5eb3437 | 115 | $result = $this->callAPISuccess('email', 'create', $this->_params); |
116 | $get = $this->callAPISuccess('email', 'create', $this->_params); | |
6a488035 | 117 | $this->assertEquals($get['count'], 1); |
e5eb3437 | 118 | $get = $this->callAPISuccess('email', 'create', $this->_params + array('debug' => 1)); |
6a488035 | 119 | $this->assertEquals($get['count'], 1); |
e5eb3437 | 120 | $get = $this->callAPISuccess('email', 'create', $this->_params + array('debug' => 1, 'action' => 'get')); |
6a488035 | 121 | $this->assertEquals($get['count'], 1); |
e5eb3437 | 122 | $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id'])); |
6a488035 | 123 | } |
5896d037 | 124 | |
6a488035 TO |
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( |
5896d037 | 136 | 'location_type_id' => $this->_locationType->id, |
e5eb3437 | 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 | |
6c6e6187 | 143 | $result = $this->callAPIAndDocument('email', 'delete', array('id' => $create['id']), __FUNCTION__, __FILE__); |
ba4a1892 | 144 | $this->assertEquals(1, $result['count']); |
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( |
5896d037 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__); |
ba4a1892 | 190 | $this->assertEquals(5, $replace1['count']); |
6a488035 TO |
191 | |
192 | // check emails at location #1 or #2 | |
e5eb3437 | 193 | $get = $this->callAPISuccess('email', 'get', array( |
5896d037 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); |
ba4a1892 | 210 | $this->assertEquals(1, $replace2['count']); |
6a488035 TO |
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); |
ba4a1892 | 233 | $this->assertEquals(0, $replace3['count']); |
6a488035 TO |
234 | |
235 | // check emails | |
e5eb3437 | 236 | $get = $this->callAPISuccess('email', 'get', array( |
237 | ||
5896d037 TO |
238 | 'contact_id' => $this->_contactID, |
239 | )); | |
a15773db | 240 | $this->assertAPISuccess($get); |
6a488035 TO |
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 | ||
5896d037 TO |
248 | 'contact_id' => $this->_contactID, |
249 | )); | |
a15773db | 250 | $this->assertAPISuccess($get); |
6a488035 | 251 | $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__); |
5c49fee0 | 252 | $description = "Demonstrates use of Replace in a nested API call."; |
6a488035 TO |
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); |
ba4a1892 | 289 | $this->assertEquals(5, $getReplace1['values'][$this->_contactID]['api.email.replace']['count']); |
6a488035 TO |
290 | |
291 | // check emails at location #1 or #2 | |
e5eb3437 | 292 | $get = $this->callAPISuccess('email', 'get', array( |
5896d037 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); |
ba4a1892 TM |
311 | $this->assertEquals(0, $getReplace2['values'][$this->_contactID]['api.email.replace']['is_error']); |
312 | $this->assertEquals(1, $getReplace2['values'][$this->_contactID]['api.email.replace']['count']); | |
6a488035 TO |
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( |
5896d037 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); |
ba4a1892 | 350 | $this->assertEquals(1, $replace1['count']); |
6a488035 TO |
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); |
ba4a1892 | 366 | $this->assertEquals(1, $replace2['count']); |
6a488035 TO |
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 | 374 | $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__); |
ba4a1892 TM |
375 | $this->assertEquals(1, $get['values'][$emailID]['is_primary']); |
376 | $this->assertEquals(1, $get['values'][$emailID]['on_hold']); | |
377 | $this->assertEquals('1-2@example.com', $get['values'][$emailID]['email']); | |
6a488035 | 378 | } |
96025800 | 379 | |
6a488035 | 380 | } |