Merge pull request #20751 from artfulrobot/artfulrobot-fix-email-hold-reset
[civicrm-core.git] / tests / phpunit / api / v3 / MembershipTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
10 */
11
12 use Civi\Api4\MembershipType;
13 use Civi\Api4\Relationship;
14 use Civi\Api4\RelationshipType;
15
16 /**
17 * Test APIv3 civicrm_membership functions
18 *
19 * @package CiviCRM_APIv3
20 * @subpackage API_Member
21 */
22
23 /**
24 * Class api_v3_MembershipTest
25 * @group headless
26 */
27 class api_v3_MembershipTest extends CiviUnitTestCase {
28
29 use CRMTraits_Financial_OrderTrait;
30
31 protected $_contactID;
32 protected $_membershipID;
33 protected $_membershipID2;
34 protected $_membershipID3;
35 protected $_membershipTypeID;
36 protected $_membershipTypeID2;
37 protected $_membershipStatusID;
38 protected $_entity;
39 protected $_params;
40
41 /**
42 * Set up for tests.
43 */
44 public function setUp(): void {
45 parent::setUp();
46 $this->_contactID = $this->individualCreate();
47 $this->_membershipTypeID = $this->membershipTypeCreate(['member_of_contact_id' => $this->_contactID]);
48 $this->_membershipTypeID2 = $this->membershipTypeCreate([
49 'period_type' => 'fixed',
50 // Ie. 1 March.
51 'fixed_period_start_day' => '301',
52 // Ie. 11 Nov.
53 'fixed_period_rollover_day' => '1111',
54 'name' => 'Another one',
55 ]);
56 $this->_membershipStatusID = $this->membershipStatusCreate('test status');
57
58 CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'name', TRUE);
59 CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name');
60
61 $this->_entity = 'Membership';
62 $this->_params = [
63 'contact_id' => $this->_contactID,
64 'membership_type_id' => 'General',
65 'join_date' => '2009-01-21',
66 'start_date' => '2009-01-21',
67 'end_date' => '2009-12-21',
68 'source' => 'Payment',
69 'is_override' => 1,
70 'status_id' => $this->_membershipStatusID,
71 ];
72 }
73
74 /**
75 * Clean up after tests.
76 *
77 * @throws \Exception
78 */
79 public function tearDown(): void {
80 $this->quickCleanUpFinancialEntities();
81 $this->quickCleanup(['civicrm_uf_match', 'civicrm_contact'], TRUE);
82 parent::tearDown();
83 }
84
85 /**
86 * Get the id for the given type.
87 *
88 * @param string $name
89 *
90 * @return int
91 */
92 public function getMembershipTypeID(string $name): int {
93 return CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'membership_type_id', $name);
94 }
95
96 /**
97 * Test membership deletion.
98 */
99 public function testMembershipDelete(): void {
100 $membershipID = $this->contactMembershipCreate($this->_params);
101 $this->assertDBRowExist('CRM_Member_DAO_Membership', $membershipID);
102 $params = [
103 'id' => $membershipID,
104 ];
105 $this->callAPIAndDocument('membership', 'delete', $params, __FUNCTION__, __FILE__);
106 $this->assertDBRowNotExist('CRM_Member_DAO_Membership', $membershipID);
107 }
108
109 public function testMembershipDeleteEmpty() {
110 $this->callAPIFailure('membership', 'delete', []);
111 }
112
113 public function testMembershipDeleteInvalidID() {
114 $this->callAPIFailure('membership', 'delete', ['id' => 'blah']);
115 }
116
117 /**
118 * Test membership deletion and with the preserve contribution param.
119 */
120 public function testMembershipDeletePreserveContribution() {
121 //DELETE
122 $membershipID = $this->contactMembershipCreate($this->_params);
123 //DELETE
124 $this->assertDBRowExist('CRM_Member_DAO_Membership', $membershipID);
125 $ContributionCreate = $this->callAPISuccess('Contribution', 'create', [
126 'sequential' => 1,
127 'financial_type_id' => "Member Dues",
128 'total_amount' => 100,
129 'contact_id' => $this->_params['contact_id'],
130 ]);
131 $this->callAPISuccess('MembershipPayment', 'create', [
132 'sequential' => 1,
133 'contribution_id' => $ContributionCreate['values'][0]['id'],
134 'membership_id' => $membershipID,
135 ]);
136 $memParams = [
137 'id' => $membershipID,
138 'preserve_contribution' => 1,
139 ];
140 $contribParams = [
141 'id' => $ContributionCreate['values'][0]['id'],
142 ];
143 $this->callAPIAndDocument('membership', 'delete', $memParams, __FUNCTION__, __FILE__);
144 $this->assertDBRowNotExist('CRM_Member_DAO_Membership', $membershipID);
145 $this->assertDBRowExist('CRM_Contribute_DAO_Contribution', $ContributionCreate['values'][0]['id']);
146 $this->callAPISuccess('Contribution', 'delete', $contribParams);
147 $this->assertDBRowNotExist('CRM_Contribute_DAO_Contribution', $ContributionCreate['values'][0]['id']);
148 }
149
150 /**
151 * Test Activity creation on cancellation of membership contribution.
152 *
153 * @throws \CRM_Core_Exception
154 * @throws \CiviCRM_API3_Exception
155 */
156 public function testActivityForCancelledContribution(): void {
157 $contactId = $this->ids['Contact']['order'] = $this->createLoggedInUser();
158
159 $this->createContributionAndMembershipOrder();
160 $membershipID = $this->callAPISuccessGetValue('MembershipPayment', ['return' => 'id']);
161 $form = new CRM_Contribute_Form_Contribution();
162 $form->_id = $this->ids['Contribution'][0];
163 $form->testSubmit([
164 'total_amount' => 100,
165 'financial_type_id' => 1,
166 'contact_id' => $contactId,
167 'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'),
168 'contribution_status_id' => 3,
169 ],
170 CRM_Core_Action::UPDATE);
171
172 $this->callAPISuccessGetSingle('Activity', [
173 'activity_type_id' => 'Membership Signup',
174 'source_record_id' => $membershipID,
175 'subject' => 'General - Payment - Status: Pending',
176 ]);
177 $this->callAPISuccessGetSingle('Activity', [
178 'activity_type_id' => 'Change Membership Status',
179 'source_record_id' => $membershipID,
180 ]);
181 }
182
183 /**
184 * Test Multiple Membership Status for same contribution id.
185 */
186 public function testMultipleMembershipsContribution() {
187 // Main contact
188 $memStatus = CRM_Member_PseudoConstant::membershipStatus();
189 // Pending Membership Status
190 $pendingMembershipId = array_search('Pending', $memStatus);
191 // New Membership Status
192 $newMembershipId = array_search('test status', $memStatus);
193
194 $membershipParam = [
195 'membership_type_id' => 'General',
196 'source' => 'Webform Payment',
197 'status_id' => $pendingMembershipId,
198 'is_pay_later' => 1,
199 'skipStatusCal' => 1,
200 ];
201
202 // Contact 1
203 $contactId1 = $this->individualCreate();
204 $membershipParam['contact_id'] = $contactId1;
205 $membershipID1 = $this->contactMembershipCreate($membershipParam);
206
207 // Pending Payment Status
208 $ContributionCreate = $this->callAPISuccess('Contribution', 'create', [
209 'financial_type_id' => '1',
210 'total_amount' => 100,
211 'contact_id' => $contactId1,
212 'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'),
213 'contribution_status_id' => 2,
214 'is_pay_later' => 1,
215 'receive_date' => date('Ymd'),
216 ]);
217 $this->callAPISuccess('MembershipPayment', 'create', [
218 'sequential' => 1,
219 'contribution_id' => $ContributionCreate['id'],
220 'membership_id' => $membershipID1,
221 ]);
222
223 // Contact 2
224 $contactId2 = $this->individualCreate();
225 $membershipParam['contact_id'] = $contactId2;
226 $membershipID2 = $this->contactMembershipCreate($membershipParam);
227 $this->callAPISuccess('MembershipPayment', 'create', [
228 'sequential' => 1,
229 'contribution_id' => $ContributionCreate['id'],
230 'membership_id' => $membershipID2,
231 ]);
232
233 // Contact 3
234 $contactId3 = $this->individualCreate();
235 $membershipParam['contact_id'] = $contactId3;
236 $membershipID3 = $this->contactMembershipCreate($membershipParam);
237 $this->callAPISuccess('MembershipPayment', 'create', [
238 'sequential' => 1,
239 'contribution_id' => $ContributionCreate['id'],
240 'membership_id' => $membershipID3,
241 ]);
242
243 // Change Payment Status to Completed
244 $form = new CRM_Contribute_Form_Contribution();
245 $form->_id = $ContributionCreate['id'];
246 $params = ['id' => $ContributionCreate['id']];
247 $values = $ids = [];
248 CRM_Contribute_BAO_Contribution::getValues($params, $values, $ids);
249 $form->_values = $values;
250 $form->testSubmit([
251 'total_amount' => 100,
252 'financial_type_id' => '1',
253 'contact_id' => $contactId1,
254 'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'),
255 'contribution_status_id' => 1,
256 ],
257 CRM_Core_Action::UPDATE);
258
259 // check for Membership 1
260 $params = ['id' => $membershipID1, 'return' => ['contact_id', 'status_id']];
261 $membership1 = $this->callAPISuccess('Membership', 'get', $params);
262 $result1 = $membership1['values'][$membershipID1];
263 $this->assertEquals($result1['contact_id'], $contactId1);
264 $this->assertEquals($result1['status_id'], $newMembershipId);
265
266 // check for Membership 2
267 $params = ['id' => $membershipID2];
268 $membership2 = $this->callAPISuccess('membership', 'get', $params);
269 $result2 = $membership2['values'][$membershipID2];
270 $this->assertEquals($result2['contact_id'], $contactId2);
271 $this->assertEquals($result2['status_id'], $newMembershipId);
272
273 // check for Membership 3
274 $params = ['id' => $membershipID3];
275 $membership3 = $this->callAPISuccess('membership', 'get', $params);
276 $result3 = $membership3['values'][$membershipID3];
277 $this->assertEquals($result3['contact_id'], $contactId3);
278 $this->assertEquals($result3['status_id'], $newMembershipId);
279 }
280
281 /**
282 * Test membership get.
283 */
284 public function testContactMembershipsGet() {
285 $this->_membershipID = $this->contactMembershipCreate($this->_params);
286 $this->callAPISuccess('membership', 'get', []);
287 $this->callAPISuccess('Membership', 'Delete', ['id' => $this->_membershipID]);
288 }
289
290 /**
291 * Test civicrm_membership_get with params not array.
292 *
293 * Gets treated as contact_id, memberships expected.
294 */
295 public function testGetWithParamsContactId() {
296 $this->_membershipID = $this->contactMembershipCreate($this->_params);
297 $params = [
298 'contact_id' => $this->_contactID,
299 'return' => array_keys($this->_params),
300 ];
301 $membership = $this->callAPISuccess('membership', 'get', $params);
302
303 $result = $membership['values'][$this->_membershipID];
304 $this->assertEquals($result['contact_id'], $this->_contactID);
305 $this->assertEquals($this->getMembershipTypeID('General'), $result['membership_type_id']);
306 $this->assertEquals($result['status_id'], $this->_membershipStatusID);
307 $this->assertEquals($result['join_date'], '2009-01-21');
308 $this->assertEquals($result['start_date'], '2009-01-21');
309 $this->assertEquals($result['end_date'], '2009-12-21');
310 $this->assertEquals($result['source'], 'Payment');
311 $this->assertEquals(1, $result['is_override']);
312 }
313
314 /**
315 * Test civicrm_membership_get with params not array.
316 *
317 * Gets treated as contact_id, memberships expected.
318 *
319 * @throws \CRM_Core_Exception
320 */
321 public function testGetInSyntax(): void {
322 $this->_membershipID = $this->contactMembershipCreate($this->_params);
323 $this->_membershipID2 = $this->contactMembershipCreate($this->_params);
324 $this->_membershipID3 = $this->contactMembershipCreate($this->_params);
325 $params = [
326 'id' => ['IN' => [$this->_membershipID, $this->_membershipID3]],
327 ];
328 $membership = $this->callAPISuccess('membership', 'get', $params);
329 $this->assertEquals(2, $membership['count']);
330 $this->assertEquals([$this->_membershipID, $this->_membershipID3], array_keys($membership['values']));
331 $params = [
332 'id' => ['NOT IN' => [$this->_membershipID, $this->_membershipID3]],
333 ];
334 $membership = $this->callAPISuccess('membership', 'get', $params);
335 $this->assertEquals(1, $membership['count']);
336 $this->assertEquals([$this->_membershipID2], array_keys($membership['values']));
337 }
338
339 /**
340 * Test civicrm_membership_get with params not array.
341 * Gets treated as contact_id, memberships expected.
342 */
343 public function testGetInSyntaxOnContactID() {
344 $this->_membershipID = $this->contactMembershipCreate($this->_params);
345 $contact2 = $this->individualCreate();
346 $contact3 = $this->individualCreate(['first_name' => 'Scout', 'last_name' => 'Canine']);
347 $this->_membershipID2 = $this->contactMembershipCreate(array_merge($this->_params, ['contact_id' => $contact2]));
348 $this->_membershipID3 = $this->contactMembershipCreate(array_merge($this->_params, ['contact_id' => $contact3]));
349 $params = [
350 'contact_id' => ['IN' => [$this->_contactID, $contact3]],
351 ];
352 $membership = $this->callAPISuccess('membership', 'get', $params);
353 $this->assertEquals(2, $membership['count']);
354 $this->assertEquals([$this->_membershipID, $this->_membershipID3], array_keys($membership['values']));
355 $params = [
356 'contact_id' => ['NOT IN' => [$this->_contactID, $contact3]],
357 ];
358 $membership = $this->callAPISuccess('membership', 'get', $params);
359 $this->assertEquals(1, $membership['count']);
360 $this->assertEquals([$this->_membershipID2], array_keys($membership['values']));
361 }
362
363 /**
364 * Test civicrm_membership_get with params not array.
365 *
366 * Gets treated as contact_id, memberships expected.
367 */
368 public function testGetWithParamsMemberShipTypeId(): void {
369 $this->callAPISuccess($this->_entity, 'create', $this->_params);
370 $params = [
371 'membership_type_id' => 'General',
372 'return' => array_keys($this->_params),
373 ];
374 $membership = $this->callAPISuccess('membership', 'get', $params);
375 $result = $membership['values'][$membership['id']];
376 $this->assertEquals($result['contact_id'], $this->_contactID);
377 $this->assertEquals($this->getMembershipTypeID('General'), $result['membership_type_id']);
378 $this->assertEquals($result['status_id'], $this->_membershipStatusID);
379 $this->assertEquals($result['join_date'], '2009-01-21');
380 $this->assertEquals($result['start_date'], '2009-01-21');
381 $this->assertEquals($result['end_date'], '2009-12-21');
382 $this->assertEquals($result['source'], 'Payment');
383 $this->assertEquals($result['is_override'], 1);
384 $this->assertEquals($result['id'], $membership['id']);
385 }
386
387 /**
388 * Test civicrm_membership_get with params not array.
389 * Gets treated as contact_id, memberships expected.
390 */
391 public function testGetWithParamsMemberShipTypeIdContactID() {
392 $params = $this->_params;
393 $this->callAPISuccess($this->_entity, 'create', $params);
394 $params['membership_type_id'] = $this->_membershipTypeID2;
395 $this->callAPISuccess($this->_entity, 'create', $params);
396 $this->callAPISuccessGetCount('membership', ['contact_id' => $this->_contactID], 2);
397 $params = [
398 'membership_type_id' => $this->_membershipTypeID,
399 'contact_id' => $this->_contactID,
400 ];
401 $this->callAPISuccessGetCount('Membership', $params, 1);
402
403 $params = [
404 'membership_type_id' => $this->_membershipTypeID2,
405 'contact_id' => $this->_contactID,
406 'return' => ['membership_type_id', 'contact_id'],
407 ];
408 $result = $this->callAPISuccessGetSingle('Membership', $params);
409 $this->assertEquals($result['contact_id'], $this->_contactID);
410 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID2);
411 }
412
413 /**
414 * Check with complete array + custom field.
415 *
416 * Note that the test is written on purpose without any
417 * variables specific to participant so it can be replicated into other entities
418 * and / or moved to the automated test suite
419 */
420 public function testGetWithParamsMemberShipIdAndCustom(): void {
421 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
422
423 $params = $this->_params;
424 $params['custom_' . $ids['custom_field_id']] = 'custom string';
425
426 $result = $this->callAPISuccess($this->_entity, 'create', $params);
427
428 $getParams = ['membership_type_id' => $params['membership_type_id'], 'return' => 'custom_' . $ids['custom_field_id']];
429 $check = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
430 $this->assertEquals('custom string', $check['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
431 }
432
433 /**
434 * Test civicrm_membership_get with proper params.
435 * Memberships expected.
436 */
437 public function testGet() {
438 $this->contactMembershipCreate($this->_params);
439 $params = [
440 'contact_id' => $this->_contactID,
441 'return' => array_keys($this->_params),
442 ];
443
444 $result = $this->callAPISuccessGetSingle('Membership', $params);
445 $this->assertEquals('2009-01-21', $result['join_date']);
446 $this->assertEquals($result['contact_id'], $this->_contactID);
447 $this->assertEquals($result['membership_type_id'], $this->getMembershipTypeID('General'));
448 $this->assertEquals($result['status_id'], $this->_membershipStatusID);
449
450 $this->assertEquals($result['start_date'], '2009-01-21');
451 $this->assertEquals($result['end_date'], '2009-12-21');
452 $this->assertEquals($result['source'], 'Payment');
453 $this->assertEquals($result['is_override'], 1);
454 }
455
456 /**
457 * Test civicrm_membership_get with proper params.
458 * Memberships expected.
459 */
460 public function testGetWithId() {
461 $membershipID = $this->contactMembershipCreate($this->_params);
462 $params = [
463 'contact_id' => $this->_contactID,
464 'id' => $this->_membershipID,
465 'return' => 'id',
466 ];
467 $result = $this->callAPISuccess('membership', 'get', $params);
468 $this->assertEquals($membershipID, $result['id']);
469 $params = [
470 'contact_id' => $this->_contactID,
471 'membership_id' => $this->_membershipID,
472 'return' => 'membership_id',
473 ];
474 $result = $this->callAPISuccess('membership', 'get', $params);
475 $this->assertEquals($membershipID, $result['id']);
476 }
477
478 /**
479 * Test civicrm_membership_get for only active.
480 * Memberships expected.
481 */
482 public function testGetOnlyActive() {
483 $description = "Demonstrates use of 'filter' active_only' param.";
484 $this->_membershipID = $this->contactMembershipCreate($this->_params);
485 $params = [
486 'contact_id' => $this->_contactID,
487 'active_only' => 1,
488 ];
489
490 $membership = $this->callAPISuccess('membership', 'get', $params);
491 $this->assertEquals($membership['values'][$this->_membershipID]['status_id'], $this->_membershipStatusID);
492 $this->assertEquals($membership['values'][$this->_membershipID]['contact_id'], $this->_contactID);
493 $params = [
494 'contact_id' => $this->_contactID,
495 'filters' => [
496 'is_current' => 1,
497 ],
498 ];
499
500 $membership = $this->callAPIAndDocument('membership', 'get', $params, __FUNCTION__, __FILE__, $description, 'FilterIsCurrent');
501 $this->assertEquals($membership['values'][$this->_membershipID]['status_id'], $this->_membershipStatusID);
502 $this->assertEquals($membership['values'][$this->_membershipID]['contact_id'], $this->_contactID);
503
504 $this->callAPISuccess('Membership', 'Delete', ['id' => $this->_membershipID]);
505 }
506
507 /**
508 * Test civicrm_membership_get for non exist contact.
509 * empty Memberships.
510 */
511 public function testGetNoContactExists() {
512 $params = [
513 'contact_id' => 55555,
514 ];
515
516 $membership = $this->callAPISuccess('membership', 'get', $params);
517 $this->assertEquals($membership['count'], 0);
518 }
519
520 /**
521 * Test civicrm_membership_get with relationship.
522 * get Memberships.
523 *
524 * @throws \CRM_Core_Exception
525 */
526 public function testGetWithRelationship() {
527 $membershipOrgId = $this->organizationCreate(NULL);
528 $memberContactId = $this->individualCreate();
529
530 $relTypeParams = [
531 'name_a_b' => 'Relation 1',
532 'name_b_a' => 'Relation 2',
533 'description' => 'Testing relationship type',
534 'contact_type_a' => 'Organization',
535 'contact_type_b' => 'Individual',
536 'is_reserved' => 1,
537 'is_active' => 1,
538 ];
539 $relTypeID = $this->relationshipTypeCreate($relTypeParams);
540
541 $params = [
542 'name' => 'test General',
543 'duration_unit' => 'year',
544 'duration_interval' => 1,
545 'period_type' => 'rolling',
546 'member_of_contact_id' => $membershipOrgId,
547 'domain_id' => 1,
548 'financial_type_id' => 1,
549 'relationship_type_id' => $relTypeID,
550 'relationship_direction' => 'b_a',
551 'is_active' => 1,
552 ];
553 $memType = $this->callAPISuccess('membership_type', 'create', $params);
554
555 $params = [
556 'contact_id' => $memberContactId,
557 'membership_type_id' => $memType['id'],
558 'join_date' => '2009-01-21',
559 'start_date' => '2009-01-21',
560 'end_date' => '2009-12-21',
561 'source' => 'Payment',
562 'is_override' => 1,
563 'status_id' => $this->_membershipStatusID,
564 ];
565 $membershipID = $this->contactMembershipCreate($params);
566
567 $params = [
568 'contact_id' => $memberContactId,
569 'membership_type_id' => $memType['id'],
570 ];
571
572 $result = $this->callAPISuccess('membership', 'get', $params);
573
574 $membership = $result['values'][$membershipID];
575 $this->assertEquals($this->_membershipStatusID, $membership['status_id']);
576 $this->callAPISuccess('Membership', 'Delete', [
577 'id' => $membership['id'],
578 ]);
579 $this->membershipTypeDelete(['id' => $memType['id']]);
580 $this->relationshipTypeDelete($relTypeID);
581 $this->contactDelete($membershipOrgId);
582 $this->contactDelete($memberContactId);
583 }
584
585 /**
586 * Test civicrm_membership_create with relationships.
587 * create/get Memberships.
588 *
589 * Test suite for CRM-14758: API ( contact, create ) does not always create related membership
590 * and max_related property for Membership_Type and Membership entities
591 *
592 * @throws \CRM_Core_Exception
593 */
594 public function testCreateWithRelationship() {
595 // Create membership type: inherited through employment, max_related = 2
596 $params = [
597 'name_a_b' => 'Employee of',
598 ];
599 $result = $this->callAPISuccess('relationship_type', 'get', $params);
600 $relationshipTypeId = $result['id'];
601 $membershipOrgId = $this->organizationCreate();
602 $params = [
603 'name' => 'Corporate Membership',
604 'duration_unit' => 'year',
605 'duration_interval' => 1,
606 'period_type' => 'rolling',
607 'member_of_contact_id' => $membershipOrgId,
608 'domain_id' => 1,
609 'financial_type_id' => 1,
610 'relationship_type_id' => $relationshipTypeId,
611 'relationship_direction' => 'b_a',
612 'max_related' => 2,
613 'is_active' => 1,
614 ];
615 $result = $this->callAPISuccess('membership_type', 'create', $params);
616 $membershipTypeId = $result['id'];
617
618 // Create employer and first employee
619 $employerId[0] = $this->organizationCreate([], 1);
620 $memberContactId[0] = $this->individualCreate(['employer_id' => $employerId[0]], 0);
621
622 // Create organization's membership
623 $params = [
624 'contact_id' => $employerId[0],
625 'membership_type_id' => $membershipTypeId,
626 'source' => 'Test suite',
627 'start_date' => date('Y-m-d'),
628 'end_date' => '+1 year',
629 ];
630 $OrganizationMembershipID = $this->contactMembershipCreate($params);
631
632 // Check that the employee inherited the membership
633 $params = [
634 'contact_id' => $memberContactId[0],
635 'membership_type_id' => $membershipTypeId,
636 ];
637
638 $result = $this->callAPISuccess('membership', 'get', $params);
639
640 $this->assertEquals(1, $result['count']);
641 $result = $result['values'][$result['id']];
642 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
643
644 // Create second employee
645 $memberContactId[1] = $this->individualCreate(['employer_id' => $employerId[0]], 1);
646
647 // Check that the employee inherited the membership
648 $params = [
649 'contact_id' => $memberContactId[1],
650 'membership_type_id' => $membershipTypeId,
651 ];
652 $result = $this->callAPISuccess('membership', 'get', $params);
653 // If it fails here CRM-14758 is not fixed
654 $this->assertEquals(1, $result['count']);
655 $result = $result['values'][$result['id']];
656 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
657
658 // Create third employee
659 $memberContactId[2] = $this->individualCreate(['employer_id' => $employerId[0]], 2);
660
661 // Check that employee does NOT inherit the membership (max_related = 2)
662 $params = [
663 'contact_id' => $memberContactId[2],
664 'membership_type_id' => $membershipTypeId,
665 ];
666 $result = $this->callAPISuccess('membership', 'get', $params);
667 $this->assertEquals(0, $result['count']);
668
669 // Increase max_related for the employer's membership
670 $params = [
671 'id' => $OrganizationMembershipID,
672 'max_related' => 3,
673 ];
674 $this->callAPISuccess('Membership', 'create', $params);
675
676 // Check that the employee inherited the membership
677 $params = [
678 'contact_id' => $memberContactId[2],
679 'membership_type_id' => $membershipTypeId,
680 ];
681 $result = $this->callAPISuccess('membership', 'get', $params);
682 $this->assertEquals(1, $result['count']);
683 $result = $result['values'][$result['id']];
684 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
685
686 // First employee moves to a new job
687 $employerId[1] = $this->organizationCreate([], 2);
688 $params = [
689 'id' => $memberContactId[0],
690 'employer_id' => $employerId[1],
691 ];
692 $this->callAPISuccess('contact', 'create', $params);
693
694 // Check that employee does NO LONGER inherit the membership
695 $params = [
696 'contact_id' => $memberContactId[0],
697 'membership_type_id' => $membershipTypeId,
698 ];
699 $result = $this->callAPISuccess('membership', 'get', $params);
700 $this->assertEquals(0, $result['count']);
701
702 //Create pay_later membership for organization.
703 $employerId[2] = $this->organizationCreate([], 1);
704 $params = [
705 'contact_id' => $employerId[2],
706 'membership_type_id' => $membershipTypeId,
707 'source' => 'Test pay later suite',
708 'is_pay_later' => 1,
709 'status_id' => 5,
710 ];
711 $organizationMembershipID = $this->callAPISuccess('Membership', 'create', $params)['id'];
712
713 $memberContactId[3] = $this->individualCreate(['employer_id' => $employerId[2]], 0);
714 // Check that the employee inherited the membership
715 $params = [
716 'contact_id' => $memberContactId[3],
717 'membership_type_id' => $membershipTypeId,
718 ];
719 $result = $this->callAPISuccessGetSingle('membership', $params);
720 $this->assertEquals($organizationMembershipID, $result['owner_membership_id']);
721
722 // Set up params for enable/disable checks
723 $relationship1 = $this->callAPISuccess('relationship', 'get', ['contact_id_a' => $memberContactId[1]]);
724 $params = [
725 'contact_id' => $memberContactId[1],
726 'membership_type_id' => $membershipTypeId,
727 ];
728
729 // Deactivate relationship using create and assert membership is not inherited
730 $this->callAPISuccess('relationship', 'create', ['id' => $relationship1['id'], 'is_active' => 0]);
731 $result = $this->callAPISuccess('membership', 'get', $params);
732 $this->assertEquals(0, $result['count']);
733
734 // Re-enable relationship using create and assert membership is inherited
735 $this->callAPISuccess('relationship', 'create', ['id' => $relationship1['id'], 'is_active' => 1]);
736 $result = $this->callAPISuccess('membership', 'get', $params);
737 $this->assertEquals(1, $result['count']);
738
739 // Deactivate relationship using setvalue and assert membership is not inherited
740 $this->callAPISuccess('relationship', 'setvalue', ['id' => $relationship1['id'], 'field' => 'is_active', 'value' => 0]);
741 $result = $this->callAPISuccess('membership', 'get', $params);
742 $this->assertEquals(0, $result['count']);
743
744 // Re-enable relationship using setvalue and assert membership is inherited
745 $this->callAPISuccess('relationship', 'setvalue', ['id' => $relationship1['id'], 'field' => 'is_active', 'value' => 1]);
746 $result = $this->callAPISuccess('membership', 'get', $params);
747 $this->assertEquals(1, $result['count']);
748
749 // Delete relationship and assert membership is not inherited
750 $this->callAPISuccess('relationship', 'delete', ['id' => $relationship1['id']]);
751 $result = $this->callAPISuccess('membership', 'get', $params);
752 $this->assertEquals(0, $result['count']);
753
754 // Tear down - reverse of creation to be safe
755 $this->contactDelete($memberContactId[2]);
756 $this->contactDelete($memberContactId[1]);
757 $this->contactDelete($memberContactId[0]);
758 $this->contactDelete($employerId[1]);
759 $this->contactDelete($employerId[0]);
760 $this->membershipTypeDelete(['id' => $membershipTypeId]);
761 $this->contactDelete($membershipOrgId);
762 }
763
764 /**
765 * Test that loops are not created when adding spouse relationships.
766 *
767 * This add a test for https://issues.civicrm.org/jira/browse/CRM-4213 in the hope of removing
768 * the buggy fix for that without a resurgence.
769 *
770 * @throws \API_Exception
771 * @throws \CRM_Core_Exception
772 * @throws \Civi\API\Exception\UnauthorizedException
773 */
774 public function testCreateWithSpouseRelationship() {
775 $relationshipTypeID = RelationshipType::get()->addSelect('id')->addWhere('name_a_b', '=', 'Spouse of')->execute()->first()['id'];
776 MembershipType::update()->setValues([
777 'relationship_direction' => ['b_a', 'a_b'],
778 'relationship_type_id' => [$relationshipTypeID, $relationshipTypeID],
779 ])
780 ->addWhere('name', '=', 'General')
781 ->execute()->first()['id'];
782
783 $spouse1ID = $this->individualCreate(['first_name' => 'him']);
784 $spouse2ID = $this->individualCreate(['first_name' => 'her']);
785 $spouse3ID = $this->individualCreate(['first_name' => 'they']);
786 $spouse4ID = $this->individualCreate(['first_name' => 'them']);
787 Relationship::create()->setValues([
788 'contact_id_a' => $spouse1ID,
789 'contact_id_b' => $spouse2ID,
790 'relationship_type_id' => $relationshipTypeID,
791 ])->execute();
792
793 $this->contactMembershipCreate([
794 'contact_id' => $spouse1ID,
795 'start_date' => date('Y-m-d'),
796 'end_date' => '+1 year',
797 ]);
798
799 $this->callAPISuccessGetSingle('Membership', [
800 'contact_id' => $spouse2ID,
801 'membership_type_id' => 'General',
802 ]);
803
804 $this->callAPISuccessGetSingle('Membership', [
805 'contact_id' => $spouse1ID,
806 'membership_type_id' => 'General',
807 ]);
808 // Add another Spouse
809 Relationship::create()->setValues([
810 'contact_id_a' => $spouse3ID,
811 'contact_id_b' => $spouse1ID,
812 'relationship_type_id' => $relationshipTypeID,
813 ])->execute();
814 $this->callAPISuccessGetSingle('Membership', [
815 'contact_id' => $spouse3ID,
816 'membership_type_id' => 'General',
817 ]);
818 $this->callAPISuccessGetCount('Membership', [], 3);
819 Relationship::create()->setValues([
820 'contact_id_a' => $spouse1ID,
821 'contact_id_b' => $spouse4ID,
822 'relationship_type_id' => $relationshipTypeID,
823 ])->execute();
824
825 $this->callAPISuccessGetSingle('Membership', [
826 'contact_id' => $spouse4ID,
827 'membership_type_id' => 'General',
828 ]);
829
830 $this->callAPISuccessGetCount('Membership', [], 4);
831 }
832
833 /**
834 * We are checking for no e-notices + only id & end_date returned
835 *
836 * @throws \CRM_Core_Exception
837 */
838 public function testMembershipGetWithReturn() {
839 $this->contactMembershipCreate($this->_params);
840 $result = $this->callAPISuccess('membership', 'get', ['return' => 'end_date']);
841 foreach ($result['values'] as $membership) {
842 $this->assertEquals(['id', 'end_date'], array_keys($membership));
843 }
844 }
845
846 ///////////////// civicrm_membership_create methods
847
848 /**
849 * Test civicrm_contact_memberships_create with empty params.
850 * Error expected.
851 */
852 public function testCreateWithEmptyParams() {
853 $params = [];
854 $this->callAPIFailure('membership', 'create', $params);
855 }
856
857 /**
858 * If is_overide is passed in status must also be passed in.
859 */
860 public function testCreateOverrideNoStatus() {
861 $params = $this->_params;
862 unset($params['status_id']);
863 $this->callAPIFailure('membership', 'create', $params);
864 }
865
866 public function testMembershipCreateMissingRequired() {
867 $params = [
868 'membership_type_id' => '1',
869 'join_date' => '2006-01-21',
870 'start_date' => '2006-01-21',
871 'end_date' => '2006-12-21',
872 'source' => 'Payment',
873 'status_id' => '2',
874 ];
875
876 $this->callAPIFailure('membership', 'create', $params);
877 }
878
879 public function testMembershipCreate() {
880 $params = [
881 'contact_id' => $this->_contactID,
882 'membership_type_id' => $this->getMembershipTypeID('General'),
883 'join_date' => '2006-01-21',
884 'start_date' => '2006-01-21',
885 'end_date' => '2006-12-21',
886 'source' => 'Payment',
887 'is_override' => 1,
888 'status_id' => $this->_membershipStatusID,
889 ];
890
891 $result = $this->callAPIAndDocument('membership', 'create', $params, __FUNCTION__, __FILE__);
892 $this->getAndCheck($params, $result['id'], $this->_entity);
893 $this->assertNotNull($result['id']);
894 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['contact_id'], " in line " . __LINE__);
895 $this->assertEquals($result['id'], $result['values'][$result['id']]['id'], " in line " . __LINE__);
896 }
897
898 /**
899 * Check for useful message if contact doesn't exist
900 */
901 public function testMembershipCreateWithInvalidContact(): void {
902 $params = [
903 'contact_id' => 999,
904 'membership_type_id' => 'General',
905 'join_date' => '2006-01-21',
906 'start_date' => '2006-01-21',
907 'end_date' => '2006-12-21',
908 'source' => 'Payment',
909 'is_override' => 1,
910 'status_id' => $this->_membershipStatusID,
911 ];
912
913 $this->callAPIFailure('membership', 'create', $params,
914 'contact_id is not valid : 999'
915 );
916 }
917
918 public function testMembershipCreateWithInvalidStatus() {
919 $params = $this->_params;
920 $params['status_id'] = 999;
921 $this->callAPIFailure('membership', 'create', $params,
922 "'999' is not a valid option for field status_id"
923 );
924 }
925
926 public function testMembershipCreateWithInvalidType() {
927 $params = $this->_params;
928 $params['membership_type_id'] = 999;
929
930 $this->callAPIFailure('membership', 'create', $params,
931 "'999' is not a valid option for field membership_type_id"
932 );
933 }
934
935 /**
936 * Check with complete array + custom field
937 * Note that the test is written on purpose without any
938 * variables specific to participant so it can be replicated into other entities
939 * and / or moved to the automated test suite
940 */
941 public function testCreateWithCustom() {
942 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
943
944 $params = $this->_params;
945 $params['custom_' . $ids['custom_field_id']] = "custom string";
946
947 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, NULL, 'CreateWithCustomData');
948 $check = $this->callAPISuccess($this->_entity, 'get', [
949 'id' => $result['id'],
950 'contact_id' => $this->_contactID,
951 ]);
952 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
953 }
954
955 /**
956 * Search on custom field value.
957 */
958 public function testSearchWithCustomDataCRM16036(): void {
959 // Create a custom field on membership
960 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
961
962 // Create a new membership, but don't assign anything to the custom field.
963 $params = $this->_params;
964 $result = $this->callAPIAndDocument(
965 $this->_entity,
966 'create',
967 $params,
968 __FUNCTION__,
969 __FILE__,
970 NULL,
971 'SearchWithCustomData');
972
973 // search memberships with CRM-16036 as custom field value.
974 // Since we did not touch the custom field of any membership,
975 // this should not return any results.
976 $check = $this->callAPISuccess($this->_entity, 'get', [
977 'custom_' . $ids['custom_field_id'] => 'CRM-16036',
978 ]);
979 $this->assertEquals(0, $check['count']);
980 }
981
982 /**
983 * Test civicrm_contact_memberships_create with membership id (edit
984 * membership).
985 * success expected.
986 */
987 public function testMembershipCreateWithId() {
988 $membershipID = $this->contactMembershipCreate($this->_params);
989 $params = [
990 'id' => $membershipID,
991 'contact_id' => $this->_contactID,
992 'membership_type_id' => 'General',
993 'join_date' => '2006-01-21',
994 'start_date' => '2006-01-21',
995 'end_date' => '2006-12-21',
996 'source' => 'Payment',
997 'is_override' => 1,
998 'status_id' => $this->_membershipStatusID,
999 ];
1000
1001 $result = $this->callAPISuccess('membership', 'create', $params);
1002
1003 //Update Status and check activities created.
1004 $updateStatus = [
1005 'id' => $result['id'],
1006 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'Cancelled'),
1007 ];
1008 $this->callAPISuccess('Membership', 'create', $updateStatus);
1009 $activities = CRM_Activity_BAO_Activity::getContactActivity($this->_contactID);
1010 $this->assertEquals(2, count($activities));
1011 $activityNames = array_flip(CRM_Utils_Array::collect('activity_name', $activities));
1012 $this->assertArrayHasKey('Membership Signup', $activityNames);
1013 $this->assertArrayHasKey('Change Membership Status', $activityNames);
1014
1015 $this->callAPISuccess('Membership', 'Delete', [
1016 'id' => $result['id'],
1017 ]);
1018 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
1019 }
1020
1021 /**
1022 * Test civicrm_contact_memberships_create with membership id (edit
1023 * membership).
1024 * success expected.
1025 */
1026 public function testMembershipCreateUpdateWithIdNoContact() {
1027 $membershipID = $this->contactMembershipCreate($this->_params);
1028 $params = [
1029 'id' => $membershipID,
1030 'membership_type_id' => $this->_membershipTypeID,
1031 'contact_id' => $this->_contactID,
1032 'join_date' => '2006-01-21',
1033 'start_date' => '2006-01-21',
1034 'end_date' => '2006-12-21',
1035 'source' => 'Payment',
1036 'is_override' => 1,
1037 'status_id' => $this->_membershipStatusID,
1038 ];
1039
1040 $result = $this->callAPISuccess('membership', 'create', $params);
1041 $this->callAPISuccess('Membership', 'Delete', [
1042 'id' => $result['id'],
1043 ]);
1044
1045 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
1046 }
1047
1048 /**
1049 * Test civicrm_contact_memberships_create with membership id (edit
1050 * membership).
1051 * success expected.
1052 */
1053 public function testMembershipCreateUpdateWithIdNoDates() {
1054 $membershipID = $this->contactMembershipCreate($this->_params);
1055 $params = [
1056 'id' => $membershipID,
1057 'contact_id' => $this->_contactID,
1058 'membership_type_id' => $this->_membershipTypeID,
1059 'source' => 'Payment',
1060 'is_override' => 1,
1061 'status_id' => $this->_membershipStatusID,
1062 ];
1063
1064 $result = $this->callAPISuccess('membership', 'create', $params);
1065 $this->callAPISuccess('Membership', 'Delete', [
1066 'id' => $result['id'],
1067 ]);
1068 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
1069 }
1070
1071 /**
1072 * Test civicrm_contact_memberships_create with membership id (edit
1073 * membership).
1074 * success expected.
1075 */
1076 public function testMembershipCreateUpdateWithIdNoDatesNoType() {
1077 $membershipID = $this->contactMembershipCreate($this->_params);
1078 $params = [
1079 'id' => $membershipID,
1080 'source' => 'not much here',
1081 'contact_id' => $this->_contactID,
1082 'is_override' => 1,
1083 'status_id' => $this->_membershipStatusID,
1084 ];
1085
1086 $result = $this->callAPISuccess('membership', 'create', $params);
1087 $this->callAPISuccess('Membership', 'Delete', [
1088 'id' => $result['id'],
1089 ]);
1090 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
1091 }
1092
1093 /**
1094 * Test civicrm_contact_memberships_create with membership id (edit
1095 * membership).
1096 * success expected.
1097 */
1098 public function testMembershipCreateUpdateWithIDAndSource() {
1099 $membershipID = $this->contactMembershipCreate($this->_params);
1100 $params = [
1101 'id' => $membershipID,
1102 'source' => 'changed',
1103 'contact_id' => $this->_contactID,
1104 'status_id' => $this->_membershipStatusID,
1105 'membership_type_id' => $this->_membershipTypeID,
1106 'skipStatusCal' => 1,
1107 ];
1108 $result = $this->callAPISuccess('membership', 'create', $params);
1109 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
1110 $this->callAPISuccess('Membership', 'Delete', [
1111 'id' => $result['id'],
1112 ]);
1113 }
1114
1115 /**
1116 * Change custom field using update.
1117 */
1118 public function testUpdateWithCustom() {
1119 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
1120
1121 $params = $this->_params;
1122 $params['custom_' . $ids['custom_field_id']] = "custom string";
1123 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, NULL, 'UpdateCustomData');
1124 $result = $this->callAPISuccess($this->_entity, 'create', [
1125 'id' => $result['id'],
1126 'custom_' . $ids['custom_field_id'] => "new custom",
1127 ]);
1128 $check = $this->callAPISuccess($this->_entity, 'get', [
1129 'id' => $result['id'],
1130 'contact_id' => $this->_contactID,
1131 ]);
1132
1133 $this->assertEquals("new custom", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
1134 $this->callAPISuccess('Membership', 'Delete', [
1135 'id' => $check['id'],
1136 ]);
1137
1138 $this->customFieldDelete($ids['custom_field_id']);
1139 $this->customGroupDelete($ids['custom_group_id']);
1140 }
1141
1142 /**
1143 * per CRM-15746 check that the id can be altered in an update hook
1144 */
1145 public function testMembershipUpdateCreateHookCRM15746() {
1146 $this->hookClass->setHook('civicrm_pre', [$this, 'hook_civicrm_pre_update_create_membership']);
1147 $result = $this->callAPISuccess('membership', 'create', $this->_params);
1148 $this->callAPISuccess('membership', 'create', ['id' => $result['id'], 'end_date' => '1 year ago']);
1149 $this->callAPISuccessGetCount('membership', [], 2);
1150 $this->hookClass->reset();
1151 $this->callAPISuccess('membership', 'create', ['id' => $result['id'], 'end_date' => '1 year ago']);
1152 $this->callAPISuccessGetCount('membership', [], 2);
1153 }
1154
1155 /**
1156 * Custom hook for update membership.
1157 *
1158 * @param string $op
1159 * @param object $objectName
1160 * @param int $id
1161 * @param array $params
1162 *
1163 * @throws \Exception
1164 */
1165 public function hook_civicrm_pre_update_create_membership($op, $objectName, $id, &$params) {
1166 if ($objectName === 'Membership' && $op === 'edit') {
1167 $existingMembership = $this->callAPISuccessGetSingle('membership', ['id' => $params['id']]);
1168 unset($params['id'], $params['membership_id']);
1169 $params['join_date'] = $params['membership_start_date'] = $params['start_date'] = date('Ymd000000', strtotime($existingMembership['start_date']));
1170 $params = array_merge($existingMembership, $params);
1171 $params['id'] = NULL;
1172 }
1173 }
1174
1175 /**
1176 * Test civicrm_contact_memberships_create Invalid membership data.
1177 * Error expected.
1178 */
1179 public function testMembershipCreateInvalidMemData() {
1180 //membership_contact_id as string
1181 $params = [
1182 'membership_contact_id' => 'Invalid',
1183 'membership_type_id' => $this->_membershipTypeID,
1184 'join_date' => '2011-01-21',
1185 'start_date' => '2010-01-21',
1186 'end_date' => '2008-12-21',
1187 'source' => 'Payment',
1188 'is_override' => 1,
1189 'status_id' => $this->_membershipStatusID,
1190 ];
1191
1192 $this->callAPIFailure('membership', 'create', $params);
1193
1194 //membership_contact_id which is no in contact table
1195 $params['membership_contact_id'] = 999;
1196 $this->callAPIFailure('membership', 'create', $params);
1197
1198 //invalid join date
1199 unset($params['membership_contact_id']);
1200 $params['join_date'] = "invalid";
1201 $this->callAPIFailure('Membership', 'Create', $params);
1202 }
1203
1204 /**
1205 * Test civicrm_contact_memberships_create with membership_contact_id
1206 * membership).
1207 * Success expected.
1208 */
1209 public function testMembershipCreateWithMemContact() {
1210 $params = [
1211 'membership_contact_id' => $this->_contactID,
1212 'membership_type_id' => $this->_membershipTypeID,
1213 'join_date' => '2011-01-21',
1214 'start_date' => '2010-01-21',
1215 'end_date' => '2008-12-21',
1216 'source' => 'Payment',
1217 'is_override' => 1,
1218 'status_id' => $this->_membershipStatusID,
1219 ];
1220
1221 $result = $this->callAPISuccess('membership', 'create', $params);
1222
1223 $this->callAPISuccess('Membership', 'Delete', [
1224 'id' => $result['id'],
1225 ]);
1226 }
1227
1228 /**
1229 * Test civicrm_contact_memberships_create with membership_contact_id
1230 * membership).
1231 * Success expected.
1232 */
1233 public function testMembershipCreateValidMembershipTypeString(): void {
1234 $params = [
1235 'membership_contact_id' => $this->_contactID,
1236 'membership_type_id' => 'General',
1237 'join_date' => '2011-01-21',
1238 'start_date' => '2010-01-21',
1239 'end_date' => '2008-12-21',
1240 'source' => 'Payment',
1241 'is_override' => 1,
1242 'status_id' => $this->_membershipStatusID,
1243 ];
1244
1245 $result = $this->callAPISuccess('membership', 'create', $params);
1246 $this->assertEquals($this->getMembershipTypeID('General'), $result['values'][$result['id']]['membership_type_id']);
1247 }
1248
1249 /**
1250 * Test civicrm_contact_memberships_create with membership_contact_id
1251 * membership).
1252 * Success expected.
1253 */
1254 public function testMembershipCreateInValidMembershipTypeString() {
1255 $params = [
1256 'membership_contact_id' => $this->_contactID,
1257 'membership_type_id' => 'invalid',
1258 'join_date' => '2011-01-21',
1259 'start_date' => '2010-01-21',
1260 'end_date' => '2008-12-21',
1261 'source' => 'Payment',
1262 'is_override' => 1,
1263 'status_id' => $this->_membershipStatusID,
1264 ];
1265
1266 $this->callAPIFailure('membership', 'create', $params);
1267 }
1268
1269 /**
1270 * Test that if membership join date is not set it defaults to today.
1271 */
1272 public function testEmptyJoinDate() {
1273 unset($this->_params['join_date'], $this->_params['is_override']);
1274 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1275 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
1276 $this->assertEquals(date('Y-m-d', strtotime('now')), $result['join_date']);
1277 $this->assertEquals('2009-01-21', $result['start_date']);
1278 $this->assertEquals('2009-12-21', $result['end_date']);
1279 }
1280
1281 /**
1282 * Test that if membership start date is not set it defaults to correct end date.
1283 * - fixed
1284 */
1285 public function testEmptyStartDateFixed() {
1286 unset($this->_params['start_date'], $this->_params['is_override']);
1287 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1288 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1289 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
1290 $this->assertEquals('2009-01-21', $result['join_date']);
1291 $this->assertEquals('2008-03-01', $result['start_date']);
1292 $this->assertEquals('2009-12-21', $result['end_date']);
1293 }
1294
1295 /**
1296 * Test that if membership start date is not set it defaults to correct end date
1297 * - fixed
1298 */
1299 public function testEmptyStartEndDateFixedOneYear() {
1300 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1301 $this->callAPISuccess('membership_type', 'create', ['id' => $this->_membershipTypeID2, 'duration_interval' => 1]);
1302 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1303 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1304 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
1305 $this->assertEquals('2009-01-21', $result['join_date']);
1306 $this->assertEquals('2008-03-01', $result['start_date']);
1307 $this->assertEquals('2010-02-28', $result['end_date']);
1308 }
1309
1310 /**
1311 * Test that if membership start date is not set it defaults to correct end date for fixed multi year memberships.
1312 */
1313 public function testEmptyStartEndDateFixedMultiYear() {
1314 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1315 $this->callAPISuccess('membership_type', 'create', ['id' => $this->_membershipTypeID2, 'duration_interval' => 5]);
1316 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1317 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1318 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
1319 $this->assertEquals('2009-01-21', $result['join_date']);
1320 $this->assertEquals('2008-03-01', $result['start_date']);
1321 $this->assertEquals('2014-02-28', $result['end_date']);
1322 }
1323
1324 /**
1325 * CRM-18503 - Test membership join date is correctly set for fixed memberships.
1326 *
1327 * @throws \CRM_Core_Exception|\CiviCRM_API3_Exception
1328 */
1329 public function testMembershipJoinDateFixed() {
1330 $memStatus = CRM_Member_PseudoConstant::membershipStatus();
1331 // Update the fixed membership type to 1 year duration.
1332 $this->callAPISuccess('membership_type', 'create', ['id' => $this->_membershipTypeID2, 'duration_interval' => 1]);
1333 $contactId = $this->createLoggedInUser();
1334 // Create membership with 'Pending' status.
1335 $params = [
1336 'contact_id' => $contactId,
1337 'membership_type_id' => $this->_membershipTypeID2,
1338 'source' => 'test membership',
1339 'is_pay_later' => 0,
1340 'status_id' => 'Pending',
1341 'skipStatusCal' => 1,
1342 'is_for_organization' => 1,
1343 ];
1344 $membership = $this->callAPISuccess('Membership', 'create', $params);
1345
1346 // Update membership to 'Completed' and check the dates.
1347 $memParams = [
1348 'id' => $membership['id'],
1349 'contact_id' => $contactId,
1350 'is_test' => 0,
1351 'membership_type_id' => $this->_membershipTypeID2,
1352 'num_terms' => 1,
1353 'status_id' => 'New',
1354 ];
1355 $result = $this->callAPISuccess('Membership', 'create', $memParams);
1356
1357 // Extend duration interval if join_date exceeds the rollover period.
1358 $joinDate = date('Y-m-d');
1359 $year = date('Y');
1360 $startDate = date('Y-m-d', strtotime(date('Y-03-01')));
1361 $rollOver = TRUE;
1362 if (strtotime($startDate) > time()) {
1363 $rollOver = FALSE;
1364 $startDate = date('Y-m-d', strtotime(date('Y-03-01') . '- 1 year'));
1365 }
1366 $membershipTypeDetails = CRM_Member_BAO_MembershipType::getMembershipType($this->_membershipTypeID2);
1367 $fixedPeriodRollover = CRM_Member_BAO_MembershipType::isDuringFixedAnnualRolloverPeriod($joinDate, $membershipTypeDetails, $year, $startDate);
1368 $y = 1;
1369 if ($fixedPeriodRollover && $rollOver) {
1370 ++$y;
1371 }
1372
1373 $expectedDates = [
1374 'join_date' => date('Ymd'),
1375 'start_date' => str_replace('-', '', $startDate),
1376 'end_date' => date('Ymd', strtotime(date('Y-03-01') . "+ {$y} year - 1 day")),
1377 ];
1378 foreach ($result['values'] as $values) {
1379 foreach ($expectedDates as $date => $val) {
1380 $this->assertEquals($val, $values[$date], "Failed asserting {$date} values");
1381 }
1382 }
1383 }
1384
1385 /**
1386 * Test correct end and start dates are calculated for fixed multi year memberships.
1387 *
1388 * The empty start date is calculated to be the start_date (1 Jan prior to the join_date - so 1 Jan 15)
1389 *
1390 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1391 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1392 * and we add on 4 years rather than 5 because we are not after the rollover day - so we calculate 31 Dec 2019
1393 */
1394 public function testFixedMultiYearDateSetTwoEmptyStartEndDate() {
1395 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1396
1397 $this->callAPISuccess('membership_type', 'create', [
1398 'id' => $this->_membershipTypeID2,
1399 'duration_interval' => 5,
1400 // Ie 1 Jan.
1401 'fixed_period_start_day' => '101',
1402 // Ie. 1 Nov.
1403 'fixed_period_rollover_day' => '1101',
1404 ]);
1405 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1406 $dates = [
1407 'join_date' => '28-Jan 2015',
1408 ];
1409 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1410 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
1411 $this->assertEquals('2015-01-28', $result['join_date']);
1412 $this->assertEquals('2015-01-01', $result['start_date']);
1413 $this->assertEquals('2019-12-31', $result['end_date']);
1414 }
1415
1416 /**
1417 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
1418 *
1419 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1420 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1421 * and we add on 4 years rather than 5 because we are not after the rollover day - so we calculate 31 Dec 2019
1422 */
1423 public function testFixedMultiYearDateSetTwoEmptyEndDate() {
1424 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1425
1426 $this->callAPISuccess('membership_type', 'create', [
1427 'id' => $this->_membershipTypeID2,
1428 'duration_interval' => 5,
1429 // Ie 1 Jan.
1430 'fixed_period_start_day' => '101',
1431 // Ie. 1 Nov.
1432 'fixed_period_rollover_day' => '1101',
1433 ]);
1434 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1435 $dates = [
1436 'start_date' => '28-Jan 2015',
1437 'join_date' => '28-Jan 2015',
1438 ];
1439 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1440 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
1441 $this->assertEquals('2015-01-28', $result['join_date']);
1442 $this->assertEquals('2015-01-28', $result['start_date']);
1443 $this->assertEquals('2019-12-31', $result['end_date']);
1444 }
1445
1446 /**
1447 * Test correct end and start dates are calculated for fixed multi year memberships.
1448 *
1449 * The empty start date is calculated to be the start_date (1 Jan prior to the join_date - so 1 Jan 15)
1450 *
1451 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1452 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1453 * and we add on <1 years rather than > 1 because we are not after the rollover day - so we calculate 31 Dec 2015
1454 */
1455 public function testFixedSingleYearDateSetTwoEmptyStartEndDate() {
1456 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1457
1458 $this->callAPISuccess('membership_type', 'create', [
1459 'id' => $this->_membershipTypeID2,
1460 'duration_interval' => 1,
1461 // Ie 1 Jan.
1462 'fixed_period_start_day' => '101',
1463 // Ie. 1 Nov.
1464 'fixed_period_rollover_day' => '1101',
1465 ]);
1466 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1467 $dates = [
1468 'join_date' => '28-Jan 2015',
1469 ];
1470 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1471 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
1472 $this->assertEquals('2015-01-28', $result['join_date']);
1473 $this->assertEquals('2015-01-01', $result['start_date']);
1474 $this->assertEquals('2015-12-31', $result['end_date']);
1475 }
1476
1477 /**
1478 * Test correct end date for fixed single year memberships is calculated and start_date is not changed.
1479 *
1480 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1481 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1482 * and we add on <1 years rather than > 1 because we are not after the rollover day - so we calculate 31 Dec 2015
1483 */
1484 public function testFixedSingleYearDateSetTwoEmptyEndDate() {
1485 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1486
1487 $this->callAPISuccess('membership_type', 'create', [
1488 'id' => $this->_membershipTypeID2,
1489 'duration_interval' => 1,
1490 // Ie 1 Jan.
1491 'fixed_period_start_day' => '101',
1492 // Ie. 1 Nov.
1493 'fixed_period_rollover_day' => '1101',
1494 ]);
1495 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1496 $dates = [
1497 'start_date' => '28-Jan 2015',
1498 'join_date' => '28-Jan 2015',
1499 ];
1500 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1501 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
1502 $this->assertEquals('2015-01-28', $result['join_date']);
1503 $this->assertEquals('2015-01-28', $result['start_date']);
1504 $this->assertEquals('2015-12-31', $result['end_date']);
1505 }
1506
1507 /**
1508 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
1509 *
1510 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1511 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1512 * and we add on 1 year we are after the rollover day - so we calculate 31 Oct 2016
1513 */
1514 public function testFixedSingleYearDateSetThreeEmptyEndDate() {
1515 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1516
1517 $this->callAPISuccess('membership_type', 'create', [
1518 'id' => $this->_membershipTypeID2,
1519 'duration_interval' => 1,
1520 // Ie. 1 Nov.
1521 'fixed_period_start_day' => '1101',
1522 // Ie 1 Jan.
1523 'fixed_period_rollover_day' => '101',
1524 ]);
1525 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1526 $dates = [
1527 'start_date' => '28-Jan 2015',
1528 'join_date' => '28-Jan 2015',
1529 ];
1530 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1531 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
1532 $this->assertEquals('2015-01-28', $result['join_date']);
1533 $this->assertEquals('2015-01-28', $result['start_date']);
1534 $this->assertEquals('2016-10-31', $result['end_date']);
1535 }
1536
1537 /**
1538 * Test correct end and start dates are calculated for fixed multi year memberships.
1539 *
1540 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
1541 *
1542 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1543 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1544 * and we add on 1 year we are after the rollover day - so we calculate 31 Oct 2016
1545 */
1546 public function testFixedSingleYearDateSetThreeEmptyStartEndDate() {
1547 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1548
1549 $this->callAPISuccess('membership_type', 'create', [
1550 'id' => $this->_membershipTypeID2,
1551 'duration_interval' => 1,
1552 // Ie. 1 Nov.
1553 'fixed_period_start_day' => '1101',
1554 // Ie 1 Jan.
1555 'fixed_period_rollover_day' => '101',
1556 ]);
1557 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1558 $dates = [
1559 'join_date' => '28-Jan 2015',
1560 ];
1561 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1562 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
1563 $this->assertEquals('2015-01-28', $result['join_date']);
1564 $this->assertEquals('2014-11-01', $result['start_date']);
1565 $this->assertEquals('2016-10-31', $result['end_date']);
1566 }
1567
1568 /**
1569 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
1570 *
1571 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1572 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1573 * and we add on 5 years we are after the rollover day - so we calculate 31 Oct 2020
1574 */
1575 public function testFixedMultiYearDateSetThreeEmptyEndDate() {
1576 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1577
1578 $this->callAPISuccess('membership_type', 'create', [
1579 'id' => $this->_membershipTypeID2,
1580 'duration_interval' => 5,
1581 // Ie. 1 Nov.
1582 'fixed_period_start_day' => '1101',
1583 // Ie 1 Jan.
1584 'fixed_period_rollover_day' => '101',
1585 ]);
1586 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1587 $dates = [
1588 'start_date' => '28-Jan 2015',
1589 'join_date' => '28-Jan 2015',
1590 ];
1591 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1592 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
1593 $this->assertEquals('2015-01-28', $result['join_date']);
1594 $this->assertEquals('2015-01-28', $result['start_date']);
1595 $this->assertEquals('2020-10-31', $result['end_date']);
1596 }
1597
1598 /**
1599 * Test correct end and start dates are calculated for fixed multi year memberships.
1600 *
1601 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
1602 *
1603 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
1604 * In this set our join date is after the start day and after the rollover day so we do get an extra year
1605 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1606 * and we add on 5 years we are after the rollover day - so we calculate 31 Oct 2020
1607 */
1608 public function testFixedMultiYearDateSetThreeEmptyStartEndDate() {
1609 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1610
1611 $this->callAPISuccess('membership_type', 'create', [
1612 'id' => $this->_membershipTypeID2,
1613 'duration_interval' => 5,
1614 // Ie. 1 Nov.
1615 'fixed_period_start_day' => '1101',
1616 // Ie 1 Jan.
1617 'fixed_period_rollover_day' => '101',
1618 ]);
1619 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1620 $dates = [
1621 'join_date' => '28-Jan 2015',
1622 ];
1623 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1624 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
1625 $this->assertEquals('2015-01-28', $result['join_date']);
1626 $this->assertEquals('2014-11-01', $result['start_date']);
1627 $this->assertEquals('2020-10-31', $result['end_date']);
1628 }
1629
1630 /**
1631 * Test that if membership start date is not set it defaults to correct end date for fixed single year memberships.
1632 */
1633 public function testEmptyStartDateRolling() {
1634 unset($this->_params['start_date'], $this->_params['is_override']);
1635 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1636 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
1637 $this->assertEquals('2009-01-21', $result['join_date']);
1638 $this->assertEquals('2009-01-21', $result['start_date']);
1639 $this->assertEquals('2009-12-21', $result['end_date']);
1640 }
1641
1642 /**
1643 * Test that if membership end date is not set it defaults to correct end date.
1644 * - rolling
1645 */
1646 public function testEmptyEndDateFixed() {
1647 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1648 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1649 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1650 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
1651 $this->assertEquals('2009-01-21', $result['join_date']);
1652 $this->assertEquals('2008-03-01', $result['start_date']);
1653 $this->assertEquals('2010-02-28', $result['end_date']);
1654 }
1655
1656 /**
1657 * Test that if membership end date is not set it defaults to correct end date.
1658 * - rolling
1659 */
1660 public function testEmptyEndDateRolling() {
1661 unset($this->_params['is_override'], $this->_params['end_date']);
1662 $this->_params['membership_type_id'] = $this->_membershipTypeID;
1663 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1664 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
1665 $this->assertEquals('2009-01-21', $result['join_date']);
1666 $this->assertEquals('2009-01-21', $result['start_date']);
1667 $this->assertEquals('2010-01-20', $result['end_date']);
1668 }
1669
1670 /**
1671 * Test that if dates are set they not over-ridden if id is passed in
1672 */
1673 public function testMembershipDatesNotOverridden() {
1674 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1675 unset($this->_params['end_date'], $this->_params['start_date']);
1676 $this->_params['id'] = $result['id'];
1677 $this->callAPISuccess($this->_entity, 'create', $this->_params);
1678 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
1679 $this->assertEquals('2009-01-21', $result['join_date']);
1680 $this->assertEquals('2009-01-21', $result['start_date']);
1681 $this->assertEquals('2009-12-21', $result['end_date']);
1682
1683 }
1684
1685 /**
1686 * Test that a contribution linked to multiple memberships results in all being updated.
1687 *
1688 * @throws \CRM_Core_Exception
1689 */
1690 public function testMultipleMembershipContribution() {
1691 $this->createMultipleMembershipOrder();
1692 $this->callAPISuccess('Payment', 'create', [
1693 'contribution_id' => $this->ids['Contribution'][0],
1694 'payment_instrument_id' => 'Check',
1695 'total_amount' => 400,
1696 ]);
1697 $memberships = $this->callAPISuccess('membership', 'get')['values'];
1698 $this->assertCount(2, $memberships);
1699 }
1700
1701 /**
1702 * Test that all membership types are returned when getoptions is called.
1703 *
1704 * This test locks in current behaviour where types for all domains are returned. It should possibly be domain
1705 * specific but that should only be done in conjunction with adding a hook to allow that to be altered as the
1706 * multisite use case expects the master domain to be able to see all sites.
1707 *
1708 * See CRM-17075.
1709 */
1710 public function testGetOptionsMembershipTypeID() {
1711 $options = $this->callAPISuccess('Membership', 'getoptions', ['field' => 'membership_type_id']);
1712 $this->assertEquals('Another one', array_pop($options['values']));
1713 $this->assertEquals('General', array_pop($options['values']));
1714 $this->assertEquals(NULL, array_pop($options['values']));
1715 }
1716
1717 }