3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Class api_v3_EmailTest
16 class api_v3_EmailTest
extends CiviUnitTestCase
{
17 protected $_contactID;
18 protected $_locationType;
22 public function setUp() {
23 $this->_entity
= 'Email';
25 $this->useTransaction(TRUE);
27 $this->_contactID
= $this->organizationCreate(NULL);
28 $this->_locationType
= $this->locationTypeCreate(NULL);
29 $this->_locationType2
= $this->locationTypeCreate([
30 'name' => 'New Location Type 2',
31 'vcard_name' => 'New Location Type 2',
32 'description' => 'Another Location Type',
36 'contact_id' => $this->_contactID
,
37 'location_type_id' => $this->_locationType
->id
,
38 'email' => 'api@a-team.com',
47 * @dataProvider versionThreeAndFour
49 public function testCreateEmail($version) {
50 $this->_apiversion
= $version;
51 $params = $this->_params
;
52 //check there are no emails to start with
53 $get = $this->callAPISuccess('email', 'get', [
54 'location_type_id' => $this->_locationType
->id
,
56 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
58 $result = $this->callAPIAndDocument('email', 'create', $params, __FUNCTION__
, __FILE__
);
59 $this->assertEquals(1, $result['count']);
60 $this->assertNotNull($result['id']);
61 $this->assertNotNull($result['values'][$result['id']]['id']);
62 $delresult = $this->callAPISuccess('email', 'delete', ['id' => $result['id']]);
66 * If no location is specified when creating a new email, it should default to
67 * the LocationType default
70 * @dataProvider versionThreeAndFour
72 public function testCreateEmailDefaultLocation($version) {
73 $this->_apiversion
= $version;
74 $params = $this->_params
;
75 unset($params['location_type_id']);
76 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
77 $this->assertEquals(CRM_Core_BAO_LocationType
::getDefault()->id
, $result['values'][$result['id']]['location_type_id']);
78 $this->callAPISuccess($this->_entity
, 'delete', ['id' => $result['id']]);
82 * If a new email is set to is_primary the prev should no longer be.
84 * If is_primary is not set then it should become is_primary is no others exist
86 * @dataProvider versionThreeAndFour
88 public function testCreateEmailPrimaryHandlingChangeToPrimary($version) {
89 $this->_apiversion
= $version;
90 $params = $this->_params
;
91 unset($params['is_primary']);
92 $email1 = $this->callAPISuccess('email', 'create', $params);
93 //now we check & make sure it has been set to primary
95 $check = $this->callAPISuccess('email', 'getcount', [
97 'id' => $email1['id'],
104 * @param int $version
105 * @dataProvider versionThreeAndFour
107 public function testCreateEmailPrimaryHandlingChangeExisting($version) {
108 $this->_apiversion
= $version;
109 $email1 = $this->callAPISuccess('email', 'create', $this->_params
);
110 $email2 = $this->callAPISuccess('email', 'create', $this->_params
);
111 $check = $this->callAPISuccess('email', 'getcount', [
113 'contact_id' => $this->_contactID
,
115 $this->assertEquals(1, $check);
119 * @param int $version
120 * @dataProvider versionThreeAndFour
122 public function testCreateEmailWithoutEmail($version) {
123 $this->_apiversion
= $version;
124 $result = $this->callAPIFailure('Email', 'Create', ['contact_id' => 4]);
125 $this->assertContains('missing', $result['error_message']);
126 $this->assertContains('email', $result['error_message']);
130 * @param int $version
131 * @dataProvider versionThreeAndFour
133 public function testGetEmail($version) {
134 $this->_apiversion
= $version;
135 $result = $this->callAPISuccess('email', 'create', $this->_params
);
136 $get = $this->callAPISuccess('email', 'create', $this->_params
);
137 $this->assertEquals($get['count'], 1);
138 $get = $this->callAPISuccess('email', 'create', $this->_params +
['debug' => 1]);
139 $this->assertEquals($get['count'], 1);
140 $get = $this->callAPISuccess('email', 'create', $this->_params +
['debug' => 1, 'action' => 'get']);
141 $this->assertEquals($get['count'], 1);
142 $delresult = $this->callAPISuccess('email', 'delete', ['id' => $result['id']]);
146 * @param int $version
147 * @dataProvider versionThreeAndFour
149 public function testDeleteEmail($version) {
150 $this->_apiversion
= $version;
152 'contact_id' => $this->_contactID
,
153 'location_type_id' => $this->_locationType
->id
,
154 'email' => 'api@a-team.com',
159 //check there are no emails to start with
160 $get = $this->callAPISuccess('email', 'get', [
161 'location_type_id' => $this->_locationType
->id
,
163 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
166 $create = $this->callAPISuccess('email', 'create', $params);
168 $result = $this->callAPIAndDocument('email', 'delete', ['id' => $create['id']], __FUNCTION__
, __FILE__
);
169 $this->assertEquals(1, $result['count']);
170 $get = $this->callAPISuccess('email', 'get', [
171 'location_type_id' => $this->_locationType
->id
,
173 $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__
);
177 * @param int $version
178 * @dataProvider versionThreeAndFour
180 public function testReplaceEmail($version) {
181 $this->_apiversion
= $version;
182 // check there are no emails to start with
183 $get = $this->callAPISuccess('email', 'get', [
184 'contact_id' => $this->_contactID
,
186 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
188 // initialize email list with three emails at loc #1 and two emails at loc #2
190 'contact_id' => $this->_contactID
,
193 'location_type_id' => $this->_locationType
->id
,
194 'email' => '1-1@example.com',
198 'location_type_id' => $this->_locationType
->id
,
199 'email' => '1-2@example.com',
203 'location_type_id' => $this->_locationType
->id
,
204 'email' => '1-3@example.com',
208 'location_type_id' => $this->_locationType2
->id
,
209 'email' => '2-1@example.com',
213 'location_type_id' => $this->_locationType2
->id
,
214 'email' => '2-2@example.com',
219 $replace1 = $this->callAPIAndDocument('email', 'replace', $replace1Params, __FUNCTION__
, __FILE__
);
220 $this->assertEquals(5, $replace1['count']);
222 // check emails at location #1 or #2
223 $get = $this->callAPISuccess('email', 'get', [
224 'contact_id' => $this->_contactID
,
226 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__
);
228 // replace the subset of emails in location #1, but preserve location #2
230 'contact_id' => $this->_contactID
,
231 'location_type_id' => $this->_locationType
->id
,
234 'email' => '1-4@example.com',
239 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
240 $this->assertEquals(1, $replace2['count']);
242 // check emails at location #1 -- all three replaced by one
243 $get = $this->callAPISuccess('email', 'get', [
244 'contact_id' => $this->_contactID
,
245 'location_type_id' => $this->_locationType
->id
,
247 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
249 // check emails at location #2 -- preserve the original two
250 $get = $this->callAPISuccess('email', 'get', [
251 'contact_id' => $this->_contactID
,
252 'location_type_id' => $this->_locationType2
->id
,
255 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__
);
257 // replace the set of emails with an empty set
259 'contact_id' => $this->_contactID
,
262 $replace3 = $this->callAPISuccess('email', 'replace', $replace3Params);
263 $this->assertEquals(0, $replace3['count']);
266 $get = $this->callAPISuccess('email', 'get', [
268 'contact_id' => $this->_contactID
,
270 $this->assertAPISuccess($get);
271 $this->assertEquals(0, $get['count'], 'Incorrect email count at ' . __LINE__
);
275 * @param int $version
276 * @dataProvider versionThreeAndFour
278 public function testReplaceEmailsInChain($version) {
279 $this->_apiversion
= $version;
280 // check there are no emails to start with
281 $get = $this->callAPISuccess('email', 'get', [
283 'contact_id' => $this->_contactID
,
285 $this->assertAPISuccess($get);
286 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
287 $description = "Demonstrates use of Replace in a nested API call.";
288 $subfile = "NestedReplaceEmail";
289 // initialize email list with three emails at loc #1 and two emails at loc #2
290 $getReplace1Params = [
292 'id' => $this->_contactID
,
293 'api.email.replace' => [
296 'location_type_id' => $this->_locationType
->id
,
297 'email' => '1-1@example.com',
301 'location_type_id' => $this->_locationType
->id
,
302 'email' => '1-2@example.com',
306 'location_type_id' => $this->_locationType
->id
,
307 'email' => '1-3@example.com',
311 'location_type_id' => $this->_locationType2
->id
,
312 'email' => '2-1@example.com',
316 'location_type_id' => $this->_locationType2
->id
,
317 'email' => '2-2@example.com',
323 $getReplace1 = $this->callAPIAndDocument('contact', 'get', $getReplace1Params, __FUNCTION__
, __FILE__
, $description, $subfile);
324 $this->assertEquals(5, $getReplace1['values'][$this->_contactID
]['api.email.replace']['count']);
326 // check emails at location #1 or #2
327 $get = $this->callAPISuccess('email', 'get', [
328 'contact_id' => $this->_contactID
,
330 $this->assertEquals(5, $get['count'], 'Incorrect email count at ' . __LINE__
);
332 // replace the subset of emails in location #1, but preserve location #2
333 $getReplace2Params = [
334 'id' => $this->_contactID
,
335 'api.email.replace' => [
336 'location_type_id' => $this->_locationType
->id
,
339 'email' => '1-4@example.com',
345 $getReplace2 = $this->callAPISuccess('contact', 'get', $getReplace2Params);
346 $this->assertEquals(0, $getReplace2['values'][$this->_contactID
]['api.email.replace']['is_error']);
347 $this->assertEquals(1, $getReplace2['values'][$this->_contactID
]['api.email.replace']['count']);
349 // check emails at location #1 -- all three replaced by one
350 $get = $this->callAPISuccess('email', 'get', [
351 'contact_id' => $this->_contactID
,
352 'location_type_id' => $this->_locationType
->id
,
355 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
357 // check emails at location #2 -- preserve the original two
358 $get = $this->callAPISuccess('email', 'get', [
359 'contact_id' => $this->_contactID
,
360 'location_type_id' => $this->_locationType2
->id
,
362 $this->assertEquals(2, $get['count'], 'Incorrect email count at ' . __LINE__
);
366 * @param int $version
367 * @dataProvider versionThreeAndFour
369 public function testReplaceEmailWithId($version) {
370 $this->_apiversion
= $version;
371 // check there are no emails to start with
372 $get = $this->callAPISuccess('email', 'get', [
373 'contact_id' => $this->_contactID
,
375 $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__
);
377 // initialize email address
379 'contact_id' => $this->_contactID
,
382 'location_type_id' => $this->_locationType
->id
,
383 'email' => '1-1@example.com',
389 $replace1 = $this->callAPISuccess('email', 'replace', $replace1Params);
390 $this->assertEquals(1, $replace1['count']);
392 $keys = array_keys($replace1['values']);
393 $emailID = array_shift($keys);
395 // update the email address, but preserve any other fields
397 'contact_id' => $this->_contactID
,
401 'email' => '1-2@example.com',
405 $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params);
406 $this->assertEquals(1, $replace2['count']);
408 // ensure the 'email' was updated while other fields were preserved
409 $get = $this->callAPISuccess('email', 'get', [
410 'contact_id' => $this->_contactID
,
411 'location_type_id' => $this->_locationType
->id
,
414 $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__
);
415 $this->assertEquals(1, $get['values'][$emailID]['is_primary']);
416 $this->assertEquals(1, $get['values'][$emailID]['on_hold']);
417 $this->assertEquals('1-2@example.com', $get['values'][$emailID]['email']);
420 public function testEmailOnHold() {
424 'contact_id' => $this->_contactID
,
425 'email' => 'api@a-team.com',
428 $result = $this->callAPIAndDocument('email', 'create', $params, __FUNCTION__
, __FILE__
);
429 $this->assertEquals(1, $result['count']);
430 $this->assertNotNull($result['id']);
431 $this->assertNotNull($result['values'][$result['id']]['id']);
432 $this->assertEquals(2, $result['values'][$result['id']]['on_hold']);
433 $this->assertEquals(date('Y-m-d H:i'), date('Y-m-d H:i', strtotime($result['values'][$result['id']]['hold_date'])));
435 // set on_hold is '0'
436 // if isMultipleBulkMail is active, the value in On-hold select is string
438 'id' => $result['id'],
439 'contact_id' => $this->_contactID
,
440 'email' => 'api@a-team.com',
444 $result_change = $this->callAPISuccess('email', 'create', $params_change +
['action' => 'get']);
445 $this->assertEquals(1, $result_change['count']);
446 $this->assertEquals($result['id'], $result_change['id']);
447 $this->assertEmpty($result_change['values'][$result_change['id']]['on_hold']);
448 $this->assertEquals(date('Y-m-d H:i'), date('Y-m-d H:i', strtotime($result_change['values'][$result_change['id']]['reset_date'])));
449 $this->assertEmpty($result_change['values'][$result_change['id']]['hold_date']);
451 $delresult = $this->callAPISuccess('email', 'delete', ['id' => $result['id']]);