Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
b6708aeb | 2 | /* |
3 | +--------------------------------------------------------------------+ | |
7d61e75f TO |
4 | | Copyright CiviCRM LLC. All rights reserved. | |
5 | | | | |
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 | +--------------------------------------------------------------------+ | |
e70a7fc0 | 10 | */ |
6a488035 | 11 | |
e9479dcf EM |
12 | /** |
13 | * Class api_v3_EmailTest | |
2ffdaed1 | 14 | * |
acb109b7 | 15 | * @group headless |
e9479dcf | 16 | */ |
6a488035 | 17 | class api_v3_EmailTest extends CiviUnitTestCase { |
6a488035 | 18 | protected $_contactID; |
d4d66593 | 19 | protected $_locationTypeID; |
20 | protected $locationType2ID; | |
6a488035 TO |
21 | protected $_entity; |
22 | protected $_params; | |
b7c9bc4c | 23 | |
7ef12efc | 24 | public function setUp(): void { |
6a488035 TO |
25 | $this->_entity = 'Email'; |
26 | parent::setUp(); | |
881567d2 TO |
27 | $this->useTransaction(TRUE); |
28 | ||
6a488035 | 29 | $this->_contactID = $this->organizationCreate(NULL); |
d4d66593 | 30 | $this->_locationTypeID = $this->locationTypeCreate(); |
31 | $this->locationType2ID = $this->locationTypeCreate([ | |
5896d037 TO |
32 | 'name' => 'New Location Type 2', |
33 | 'vcard_name' => 'New Location Type 2', | |
34 | 'description' => 'Another Location Type', | |
35 | 'is_active' => 1, | |
9099cab3 CW |
36 | ]); |
37 | $this->_params = [ | |
6a488035 | 38 | 'contact_id' => $this->_contactID, |
d4d66593 | 39 | 'location_type_id' => $this->_locationTypeID, |
6a488035 TO |
40 | 'email' => 'api@a-team.com', |
41 | 'is_primary' => 1, | |
e5eb3437 | 42 | |
6a488035 | 43 | //TODO email_type_id |
9099cab3 | 44 | ]; |
6a488035 TO |
45 | } |
46 | ||
2d932085 | 47 | /** |
2ffdaed1 | 48 | * Test create email. |
49 | * | |
2d932085 | 50 | * @param int $version |
2ffdaed1 | 51 | * |
2d932085 | 52 | * @dataProvider versionThreeAndFour |
2ffdaed1 | 53 | * @throws \CRM_Core_Exception |
2d932085 CW |
54 | */ |
55 | public function testCreateEmail($version) { | |
56 | $this->_apiversion = $version; | |
6a488035 TO |
57 | $params = $this->_params; |
58 | //check there are no emails to start with | |
9099cab3 | 59 | $get = $this->callAPISuccess('email', 'get', [ |
d4d66593 | 60 | 'location_type_id' => $this->_locationTypeID, |
9099cab3 | 61 | ]); |
2ffdaed1 | 62 | $this->assertEquals(0, $get['count'], 'Contact not successfully deleted.'); |
6a488035 | 63 | |
e5eb3437 | 64 | $result = $this->callAPIAndDocument('email', 'create', $params, __FUNCTION__, __FILE__); |
ba4a1892 TM |
65 | $this->assertEquals(1, $result['count']); |
66 | $this->assertNotNull($result['id']); | |
67 | $this->assertNotNull($result['values'][$result['id']]['id']); | |
2ffdaed1 | 68 | $this->callAPISuccess('email', 'delete', ['id' => $result['id']]); |
6a488035 | 69 | } |
5896d037 | 70 | |
2890c0f9 AS |
71 | /** |
72 | * If no location is specified when creating a new email, it should default to | |
73 | * the LocationType default | |
74 | * | |
cd976ae6 | 75 | * @param int $version |
2ffdaed1 | 76 | * |
cd976ae6 | 77 | * @dataProvider versionThreeAndFour |
2ffdaed1 | 78 | * @throws \CRM_Core_Exception |
2890c0f9 | 79 | */ |
cd976ae6 CW |
80 | public function testCreateEmailDefaultLocation($version) { |
81 | $this->_apiversion = $version; | |
2890c0f9 AS |
82 | $params = $this->_params; |
83 | unset($params['location_type_id']); | |
0e70279a | 84 | $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__); |
2890c0f9 | 85 | $this->assertEquals(CRM_Core_BAO_LocationType::getDefault()->id, $result['values'][$result['id']]['location_type_id']); |
0e70279a | 86 | $this->callAPISuccess($this->_entity, 'delete', ['id' => $result['id']]); |
2890c0f9 AS |
87 | } |
88 | ||
8d7a9d07 | 89 | /** |
fe482240 | 90 | * If a new email is set to is_primary the prev should no longer be. |
6a488035 TO |
91 | * |
92 | * If is_primary is not set then it should become is_primary is no others exist | |
2ffdaed1 | 93 | * |
2d932085 | 94 | * @param int $version |
2ffdaed1 | 95 | * |
2d932085 | 96 | * @dataProvider versionThreeAndFour |
2ffdaed1 | 97 | * @throws \CRM_Core_Exception |
6a488035 | 98 | */ |
2d932085 CW |
99 | public function testCreateEmailPrimaryHandlingChangeToPrimary($version) { |
100 | $this->_apiversion = $version; | |
6a488035 TO |
101 | $params = $this->_params; |
102 | unset($params['is_primary']); | |
e5eb3437 | 103 | $email1 = $this->callAPISuccess('email', 'create', $params); |
6a488035 | 104 | //now we check & make sure it has been set to primary |
e5eb3437 | 105 | $expected = 1; |
2ffdaed1 | 106 | $this->callAPISuccess('email', 'getcount', [ |
39b959db SL |
107 | 'is_primary' => 1, |
108 | 'id' => $email1['id'], | |
9099cab3 | 109 | ], |
e5eb3437 | 110 | $expected |
5896d037 | 111 | ); |
6a488035 | 112 | } |
e5eb3437 | 113 | |
2d932085 CW |
114 | /** |
115 | * @param int $version | |
2ffdaed1 | 116 | * |
2d932085 | 117 | * @dataProvider versionThreeAndFour |
2ffdaed1 | 118 | * @throws \CRM_Core_Exception |
2d932085 CW |
119 | */ |
120 | public function testCreateEmailPrimaryHandlingChangeExisting($version) { | |
121 | $this->_apiversion = $version; | |
2ffdaed1 | 122 | $this->callAPISuccess('email', 'create', $this->_params); |
123 | $this->callAPISuccess('email', 'create', $this->_params); | |
9099cab3 | 124 | $check = $this->callAPISuccess('email', 'getcount', [ |
5896d037 TO |
125 | 'is_primary' => 1, |
126 | 'contact_id' => $this->_contactID, | |
9099cab3 | 127 | ]); |
6a488035 TO |
128 | $this->assertEquals(1, $check); |
129 | } | |
130 | ||
2d932085 CW |
131 | /** |
132 | * @param int $version | |
133 | * @dataProvider versionThreeAndFour | |
134 | */ | |
135 | public function testCreateEmailWithoutEmail($version) { | |
136 | $this->_apiversion = $version; | |
9099cab3 | 137 | $result = $this->callAPIFailure('Email', 'Create', ['contact_id' => 4]); |
ba4a1892 TM |
138 | $this->assertContains('missing', $result['error_message']); |
139 | $this->assertContains('email', $result['error_message']); | |
6a488035 TO |
140 | } |
141 | ||
2d932085 CW |
142 | /** |
143 | * @param int $version | |
2ffdaed1 | 144 | * |
2d932085 | 145 | * @dataProvider versionThreeAndFour |
2ffdaed1 | 146 | * @throws \CRM_Core_Exception |
2d932085 CW |
147 | */ |
148 | public function testGetEmail($version) { | |
149 | $this->_apiversion = $version; | |
e5eb3437 | 150 | $result = $this->callAPISuccess('email', 'create', $this->_params); |
151 | $get = $this->callAPISuccess('email', 'create', $this->_params); | |
6a488035 | 152 | $this->assertEquals($get['count'], 1); |
9099cab3 | 153 | $get = $this->callAPISuccess('email', 'create', $this->_params + ['debug' => 1]); |
6a488035 | 154 | $this->assertEquals($get['count'], 1); |
9099cab3 | 155 | $get = $this->callAPISuccess('email', 'create', $this->_params + ['debug' => 1, 'action' => 'get']); |
6a488035 | 156 | $this->assertEquals($get['count'], 1); |
2ffdaed1 | 157 | $this->callAPISuccess('email', 'delete', ['id' => $result['id']]); |
6a488035 | 158 | } |
5896d037 | 159 | |
2d932085 CW |
160 | /** |
161 | * @param int $version | |
2ffdaed1 | 162 | * |
2d932085 | 163 | * @dataProvider versionThreeAndFour |
2ffdaed1 | 164 | * @throws \CRM_Core_Exception |
2d932085 CW |
165 | */ |
166 | public function testDeleteEmail($version) { | |
167 | $this->_apiversion = $version; | |
9099cab3 | 168 | $params = [ |
6a488035 | 169 | 'contact_id' => $this->_contactID, |
d4d66593 | 170 | 'location_type_id' => $this->_locationTypeID, |
6a488035 TO |
171 | 'email' => 'api@a-team.com', |
172 | 'is_primary' => 1, | |
e5eb3437 | 173 | |
6a488035 | 174 | //TODO email_type_id |
9099cab3 | 175 | ]; |
6a488035 | 176 | //check there are no emails to start with |
9099cab3 | 177 | $get = $this->callAPISuccess('email', 'get', [ |
d4d66593 | 178 | 'location_type_id' => $this->_locationTypeID, |
9099cab3 | 179 | ]); |
2ffdaed1 | 180 | $this->assertEquals(0, $get['count'], 'email already exists'); |
6a488035 TO |
181 | |
182 | //create one | |
e5eb3437 | 183 | $create = $this->callAPISuccess('email', 'create', $params); |
6a488035 | 184 | |
9099cab3 | 185 | $result = $this->callAPIAndDocument('email', 'delete', ['id' => $create['id']], __FUNCTION__, __FILE__); |
ba4a1892 | 186 | $this->assertEquals(1, $result['count']); |
9099cab3 | 187 | $get = $this->callAPISuccess('email', 'get', [ |
d4d66593 | 188 | 'location_type_id' => $this->_locationTypeID, |
9099cab3 | 189 | ]); |
2ffdaed1 | 190 | $this->assertEquals(0, $get['count'], 'Contact not successfully deleted'); |
6a488035 TO |
191 | } |
192 | ||
2d932085 CW |
193 | /** |
194 | * @param int $version | |
2ffdaed1 | 195 | * |
2d932085 | 196 | * @dataProvider versionThreeAndFour |
2ffdaed1 | 197 | * @throws \CRM_Core_Exception |
2d932085 CW |
198 | */ |
199 | public function testReplaceEmail($version) { | |
200 | $this->_apiversion = $version; | |
6a488035 | 201 | // check there are no emails to start with |
9099cab3 | 202 | $get = $this->callAPISuccess('email', 'get', [ |
5896d037 | 203 | 'contact_id' => $this->_contactID, |
9099cab3 | 204 | ]); |
2ffdaed1 | 205 | $this->assertEquals(0, $get['count'], 'email already exists'); |
6a488035 TO |
206 | |
207 | // initialize email list with three emails at loc #1 and two emails at loc #2 | |
9099cab3 | 208 | $replace1Params = [ |
6a488035 | 209 | 'contact_id' => $this->_contactID, |
9099cab3 CW |
210 | 'values' => [ |
211 | [ | |
d4d66593 | 212 | 'location_type_id' => $this->_locationTypeID, |
6a488035 TO |
213 | 'email' => '1-1@example.com', |
214 | 'is_primary' => 1, | |
9099cab3 CW |
215 | ], |
216 | [ | |
d4d66593 | 217 | 'location_type_id' => $this->_locationTypeID, |
6a488035 TO |
218 | 'email' => '1-2@example.com', |
219 | 'is_primary' => 0, | |
9099cab3 CW |
220 | ], |
221 | [ | |
d4d66593 | 222 | 'location_type_id' => $this->_locationTypeID, |
6a488035 TO |
223 | 'email' => '1-3@example.com', |
224 | 'is_primary' => 0, | |
9099cab3 CW |
225 | ], |
226 | [ | |
d4d66593 | 227 | 'location_type_id' => $this->locationType2ID, |
6a488035 TO |
228 | 'email' => '2-1@example.com', |
229 | 'is_primary' => 0, | |
9099cab3 CW |
230 | ], |
231 | [ | |
d4d66593 | 232 | 'location_type_id' => $this->locationType2ID, |
6a488035 TO |
233 | 'email' => '2-2@example.com', |
234 | 'is_primary' => 0, | |
9099cab3 CW |
235 | ], |
236 | ], | |
237 | ]; | |
e5eb3437 | 238 | $replace1 = $this->callAPIAndDocument('email', 'replace', $replace1Params, __FUNCTION__, __FILE__); |
ba4a1892 | 239 | $this->assertEquals(5, $replace1['count']); |
6a488035 TO |
240 | |
241 | // check emails at location #1 or #2 | |
9099cab3 | 242 | $get = $this->callAPISuccess('email', 'get', [ |
5896d037 | 243 | 'contact_id' => $this->_contactID, |
9099cab3 | 244 | ]); |
2ffdaed1 | 245 | $this->assertEquals(5, $get['count'], 'Incorrect email count'); |
6a488035 TO |
246 | |
247 | // replace the subset of emails in location #1, but preserve location #2 | |
9099cab3 | 248 | $replace2Params = [ |
6a488035 | 249 | 'contact_id' => $this->_contactID, |
d4d66593 | 250 | 'location_type_id' => $this->_locationTypeID, |
9099cab3 CW |
251 | 'values' => [ |
252 | [ | |
6a488035 TO |
253 | 'email' => '1-4@example.com', |
254 | 'is_primary' => 1, | |
9099cab3 CW |
255 | ], |
256 | ], | |
257 | ]; | |
e5eb3437 | 258 | $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params); |
ba4a1892 | 259 | $this->assertEquals(1, $replace2['count']); |
6a488035 TO |
260 | |
261 | // check emails at location #1 -- all three replaced by one | |
9099cab3 | 262 | $get = $this->callAPISuccess('email', 'get', [ |
e5eb3437 | 263 | 'contact_id' => $this->_contactID, |
d4d66593 | 264 | 'location_type_id' => $this->_locationTypeID, |
9099cab3 | 265 | ]); |
2ffdaed1 | 266 | $this->assertEquals(1, $get['count'], 'Incorrect email count'); |
6a488035 TO |
267 | |
268 | // check emails at location #2 -- preserve the original two | |
9099cab3 | 269 | $get = $this->callAPISuccess('email', 'get', [ |
e5eb3437 | 270 | 'contact_id' => $this->_contactID, |
d4d66593 | 271 | 'location_type_id' => $this->locationType2ID, |
9099cab3 | 272 | ]); |
e5eb3437 | 273 | |
2ffdaed1 | 274 | $this->assertEquals(2, $get['count'], 'Incorrect email count'); |
6a488035 TO |
275 | |
276 | // replace the set of emails with an empty set | |
9099cab3 | 277 | $replace3Params = [ |
6a488035 | 278 | 'contact_id' => $this->_contactID, |
9099cab3 CW |
279 | 'values' => [], |
280 | ]; | |
e5eb3437 | 281 | $replace3 = $this->callAPISuccess('email', 'replace', $replace3Params); |
ba4a1892 | 282 | $this->assertEquals(0, $replace3['count']); |
6a488035 TO |
283 | |
284 | // check emails | |
9099cab3 | 285 | $get = $this->callAPISuccess('email', 'get', [ |
e5eb3437 | 286 | |
5896d037 | 287 | 'contact_id' => $this->_contactID, |
9099cab3 | 288 | ]); |
a15773db | 289 | $this->assertAPISuccess($get); |
2ffdaed1 | 290 | $this->assertEquals(0, $get['count'], 'Incorrect email count'); |
6a488035 TO |
291 | } |
292 | ||
2d932085 CW |
293 | /** |
294 | * @param int $version | |
2ffdaed1 | 295 | * |
2d932085 | 296 | * @dataProvider versionThreeAndFour |
2ffdaed1 | 297 | * @throws \CRM_Core_Exception |
2d932085 CW |
298 | */ |
299 | public function testReplaceEmailsInChain($version) { | |
300 | $this->_apiversion = $version; | |
6a488035 | 301 | // check there are no emails to start with |
9099cab3 | 302 | $get = $this->callAPISuccess('email', 'get', [ |
e5eb3437 | 303 | |
5896d037 | 304 | 'contact_id' => $this->_contactID, |
9099cab3 | 305 | ]); |
a15773db | 306 | $this->assertAPISuccess($get); |
2ffdaed1 | 307 | $this->assertEquals(0, $get['count'], 'email already exists'); |
308 | $description = 'Demonstrates use of Replace in a nested API call.'; | |
309 | $subfile = 'NestedReplaceEmail'; | |
6a488035 | 310 | // initialize email list with three emails at loc #1 and two emails at loc #2 |
9099cab3 | 311 | $getReplace1Params = [ |
e5eb3437 | 312 | |
6a488035 | 313 | 'id' => $this->_contactID, |
9099cab3 CW |
314 | 'api.email.replace' => [ |
315 | 'values' => [ | |
316 | [ | |
d4d66593 | 317 | 'location_type_id' => $this->_locationTypeID, |
6a488035 TO |
318 | 'email' => '1-1@example.com', |
319 | 'is_primary' => 1, | |
9099cab3 CW |
320 | ], |
321 | [ | |
d4d66593 | 322 | 'location_type_id' => $this->_locationTypeID, |
6a488035 TO |
323 | 'email' => '1-2@example.com', |
324 | 'is_primary' => 0, | |
9099cab3 CW |
325 | ], |
326 | [ | |
d4d66593 | 327 | 'location_type_id' => $this->_locationTypeID, |
6a488035 TO |
328 | 'email' => '1-3@example.com', |
329 | 'is_primary' => 0, | |
9099cab3 CW |
330 | ], |
331 | [ | |
d4d66593 | 332 | 'location_type_id' => $this->locationType2ID, |
6a488035 TO |
333 | 'email' => '2-1@example.com', |
334 | 'is_primary' => 0, | |
9099cab3 CW |
335 | ], |
336 | [ | |
d4d66593 | 337 | 'location_type_id' => $this->locationType2ID, |
6a488035 TO |
338 | 'email' => '2-2@example.com', |
339 | 'is_primary' => 0, | |
9099cab3 CW |
340 | ], |
341 | ], | |
342 | ], | |
343 | ]; | |
e5eb3437 | 344 | $getReplace1 = $this->callAPIAndDocument('contact', 'get', $getReplace1Params, __FUNCTION__, __FILE__, $description, $subfile); |
ba4a1892 | 345 | $this->assertEquals(5, $getReplace1['values'][$this->_contactID]['api.email.replace']['count']); |
6a488035 TO |
346 | |
347 | // check emails at location #1 or #2 | |
9099cab3 | 348 | $get = $this->callAPISuccess('email', 'get', [ |
5896d037 | 349 | 'contact_id' => $this->_contactID, |
9099cab3 | 350 | ]); |
2ffdaed1 | 351 | $this->assertEquals(5, $get['count'], 'Incorrect email count'); |
6a488035 TO |
352 | |
353 | // replace the subset of emails in location #1, but preserve location #2 | |
9099cab3 | 354 | $getReplace2Params = [ |
6a488035 | 355 | 'id' => $this->_contactID, |
9099cab3 | 356 | 'api.email.replace' => [ |
d4d66593 | 357 | 'location_type_id' => $this->_locationTypeID, |
9099cab3 CW |
358 | 'values' => [ |
359 | [ | |
6a488035 TO |
360 | 'email' => '1-4@example.com', |
361 | 'is_primary' => 1, | |
9099cab3 CW |
362 | ], |
363 | ], | |
364 | ], | |
365 | ]; | |
e5eb3437 | 366 | $getReplace2 = $this->callAPISuccess('contact', 'get', $getReplace2Params); |
ba4a1892 TM |
367 | $this->assertEquals(0, $getReplace2['values'][$this->_contactID]['api.email.replace']['is_error']); |
368 | $this->assertEquals(1, $getReplace2['values'][$this->_contactID]['api.email.replace']['count']); | |
6a488035 TO |
369 | |
370 | // check emails at location #1 -- all three replaced by one | |
9099cab3 | 371 | $get = $this->callAPISuccess('email', 'get', [ |
e5eb3437 | 372 | 'contact_id' => $this->_contactID, |
d4d66593 | 373 | 'location_type_id' => $this->_locationTypeID, |
9099cab3 | 374 | ]); |
6a488035 | 375 | |
2ffdaed1 | 376 | $this->assertEquals(1, $get['count'], 'Incorrect email count'); |
6a488035 TO |
377 | |
378 | // check emails at location #2 -- preserve the original two | |
9099cab3 | 379 | $get = $this->callAPISuccess('email', 'get', [ |
e5eb3437 | 380 | 'contact_id' => $this->_contactID, |
d4d66593 | 381 | 'location_type_id' => $this->locationType2ID, |
9099cab3 | 382 | ]); |
2ffdaed1 | 383 | $this->assertEquals(2, $get['count'], 'Incorrect email count'); |
6a488035 TO |
384 | } |
385 | ||
2d932085 CW |
386 | /** |
387 | * @param int $version | |
2ffdaed1 | 388 | * |
2d932085 | 389 | * @dataProvider versionThreeAndFour |
2ffdaed1 | 390 | * @throws \CRM_Core_Exception |
2d932085 CW |
391 | */ |
392 | public function testReplaceEmailWithId($version) { | |
393 | $this->_apiversion = $version; | |
6a488035 | 394 | // check there are no emails to start with |
9099cab3 | 395 | $get = $this->callAPISuccess('email', 'get', [ |
5896d037 | 396 | 'contact_id' => $this->_contactID, |
9099cab3 | 397 | ]); |
6a488035 TO |
398 | $this->assertEquals(0, $get['count'], 'email already exists ' . __LINE__); |
399 | ||
400 | // initialize email address | |
9099cab3 | 401 | $replace1Params = [ |
6a488035 | 402 | 'contact_id' => $this->_contactID, |
9099cab3 CW |
403 | 'values' => [ |
404 | [ | |
d4d66593 | 405 | 'location_type_id' => $this->_locationTypeID, |
6a488035 TO |
406 | 'email' => '1-1@example.com', |
407 | 'is_primary' => 1, | |
408 | 'on_hold' => 1, | |
9099cab3 CW |
409 | ], |
410 | ], | |
411 | ]; | |
e5eb3437 | 412 | $replace1 = $this->callAPISuccess('email', 'replace', $replace1Params); |
ba4a1892 | 413 | $this->assertEquals(1, $replace1['count']); |
6a488035 TO |
414 | |
415 | $keys = array_keys($replace1['values']); | |
416 | $emailID = array_shift($keys); | |
417 | ||
418 | // update the email address, but preserve any other fields | |
9099cab3 | 419 | $replace2Params = [ |
6a488035 | 420 | 'contact_id' => $this->_contactID, |
9099cab3 CW |
421 | 'values' => [ |
422 | [ | |
6a488035 TO |
423 | 'id' => $emailID, |
424 | 'email' => '1-2@example.com', | |
9099cab3 CW |
425 | ], |
426 | ], | |
427 | ]; | |
e5eb3437 | 428 | $replace2 = $this->callAPISuccess('email', 'replace', $replace2Params); |
ba4a1892 | 429 | $this->assertEquals(1, $replace2['count']); |
6a488035 TO |
430 | |
431 | // ensure the 'email' was updated while other fields were preserved | |
9099cab3 | 432 | $get = $this->callAPISuccess('email', 'get', [ |
e5eb3437 | 433 | 'contact_id' => $this->_contactID, |
d4d66593 | 434 | 'location_type_id' => $this->_locationTypeID, |
9099cab3 | 435 | ]); |
e5eb3437 | 436 | |
6a488035 | 437 | $this->assertEquals(1, $get['count'], 'Incorrect email count at ' . __LINE__); |
ba4a1892 TM |
438 | $this->assertEquals(1, $get['values'][$emailID]['is_primary']); |
439 | $this->assertEquals(1, $get['values'][$emailID]['on_hold']); | |
440 | $this->assertEquals('1-2@example.com', $get['values'][$emailID]['email']); | |
6a488035 | 441 | } |
96025800 | 442 | |
2ffdaed1 | 443 | /** |
444 | * Test updates affecting on hold emails. | |
445 | * | |
446 | * @throws \CRM_Core_Exception | |
447 | */ | |
5d32acf9 | 448 | public function testEmailOnHold() { |
9099cab3 | 449 | $params = [ |
5d32acf9 | 450 | 'contact_id' => $this->_contactID, |
451 | 'email' => 'api@a-team.com', | |
452 | 'on_hold' => '2', | |
9099cab3 | 453 | ]; |
5d32acf9 | 454 | $result = $this->callAPIAndDocument('email', 'create', $params, __FUNCTION__, __FILE__); |
455 | $this->assertEquals(1, $result['count']); | |
456 | $this->assertNotNull($result['id']); | |
457 | $this->assertNotNull($result['values'][$result['id']]['id']); | |
458 | $this->assertEquals(2, $result['values'][$result['id']]['on_hold']); | |
459 | $this->assertEquals(date('Y-m-d H:i'), date('Y-m-d H:i', strtotime($result['values'][$result['id']]['hold_date']))); | |
460 | ||
461 | // set on_hold is '0' | |
462 | // if isMultipleBulkMail is active, the value in On-hold select is string | |
9099cab3 | 463 | $params_change = [ |
5d32acf9 | 464 | 'id' => $result['id'], |
465 | 'contact_id' => $this->_contactID, | |
466 | 'email' => 'api@a-team.com', | |
467 | 'is_primary' => 1, | |
468 | 'on_hold' => '0', | |
9099cab3 CW |
469 | ]; |
470 | $result_change = $this->callAPISuccess('email', 'create', $params_change + ['action' => 'get']); | |
5d32acf9 | 471 | $this->assertEquals(1, $result_change['count']); |
472 | $this->assertEquals($result['id'], $result_change['id']); | |
473 | $this->assertEmpty($result_change['values'][$result_change['id']]['on_hold']); | |
474 | $this->assertEquals(date('Y-m-d H:i'), date('Y-m-d H:i', strtotime($result_change['values'][$result_change['id']]['reset_date']))); | |
475 | $this->assertEmpty($result_change['values'][$result_change['id']]['hold_date']); | |
476 | ||
2ffdaed1 | 477 | $this->callAPISuccess('email', 'delete', ['id' => $result['id']]); |
478 | } | |
479 | ||
480 | /** | |
481 | * Test setting a bulk email unsets others on the contact. | |
482 | * | |
483 | * @throws \CRM_Core_Exception | |
484 | */ | |
485 | public function testSetBulkEmail() { | |
486 | $individualID = $this->individualCreate([]); | |
487 | $email = $this->callAPISuccessGetSingle('Email', ['contact_id' => $individualID]); | |
488 | $this->assertEquals(0, $email['is_bulkmail']); | |
489 | $this->callAPISuccess('Email', 'create', ['id' => $email['id'], 'is_bulkmail' => 1]); | |
490 | $email = $this->callAPISuccessGetSingle('Email', ['contact_id' => $individualID]); | |
491 | $this->assertEquals(1, $email['is_bulkmail']); | |
492 | $email2 = $this->callAPISuccess('Email', 'create', ['contact_id' => $individualID, 'email' => 'mail@Example.com', 'is_bulkmail' => 1]); | |
493 | $emails = $this->callAPISuccess('Email', 'get', ['contact_id' => $individualID])['values']; | |
494 | $this->assertEquals(0, $emails[$email['id']]['is_bulkmail']); | |
495 | $this->assertEquals(1, $emails[$email2['id']]['is_bulkmail']); | |
5d32acf9 | 496 | } |
497 | ||
30420aa3 | 498 | /** |
499 | * Test getlist. | |
500 | * | |
501 | * @throws \CRM_Core_Exception | |
502 | */ | |
503 | public function testGetlist() { | |
504 | $name = 'Scarabée'; | |
505 | $emailMatchContactID = $this->individualCreate(['last_name' => $name, 'email' => 'bob@bob.com']); | |
506 | $emailMatchEmailID = $this->callAPISuccessGetValue('Email', ['return' => 'id', 'contact_id' => $emailMatchContactID]); | |
507 | $this->individualCreate(['last_name' => $name, 'email' => 'bob@bob.com', 'is_deceased' => 1]); | |
508 | $this->individualCreate(['last_name' => $name, 'email' => 'bob@bob.com', 'is_deleted' => 1]); | |
509 | $this->individualCreate(['last_name' => $name, 'api.email.create' => ['email' => 'bob@bob.com', 'on_hold' => 1]]); | |
510 | $this->individualCreate(['last_name' => $name, 'do_not_email' => 1, 'api.email.create' => ['email' => 'bob@bob.com']]); | |
511 | $nameMatchContactID = $this->individualCreate(['last_name' => 'bob', 'email' => 'blah@example.com']); | |
512 | $nameMatchEmailID = $this->callAPISuccessGetValue('Email', ['return' => 'id', 'contact_id' => $nameMatchContactID]); | |
513 | // We should get only the active live email-able contact. | |
514 | $result = $this->callAPISuccess('Email', 'getlist', ['input' => 'bob'])['values']; | |
515 | $this->assertCount(2, $result); | |
516 | $this->assertEquals($nameMatchEmailID, $result[0]['id']); | |
517 | $this->assertEquals($emailMatchEmailID, $result[1]['id']); | |
518 | } | |
519 | ||
6a488035 | 520 | } |