3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
12 use Civi\Api4\MembershipType
;
13 use Civi\Api4\Relationship
;
14 use Civi\Api4\RelationshipType
;
17 * Test APIv3 civicrm_membership functions
19 * @package CiviCRM_APIv3
20 * @subpackage API_Member
24 * Class api_v3_MembershipTest
27 class api_v3_MembershipTest
extends CiviUnitTestCase
{
29 use CRMTraits_Financial_OrderTrait
;
31 protected $_contactID;
32 protected $_membershipID;
33 protected $_membershipID2;
34 protected $_membershipID3;
35 protected $_membershipTypeID;
36 protected $_membershipTypeID2;
37 protected $_membershipStatusID;
44 public function setUp(): void
{
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',
51 'fixed_period_start_day' => '301',
53 'fixed_period_rollover_day' => '1111',
54 'name' => 'Another one',
56 $this->_membershipStatusID
= $this->membershipStatusCreate('test status');
58 CRM_Member_PseudoConstant
::membershipStatus(NULL, NULL, 'name', TRUE);
59 CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, TRUE, 'name');
61 $this->_entity
= 'Membership';
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',
70 'status_id' => $this->_membershipStatusID
,
75 * Clean up after tests.
79 public function tearDown(): void
{
80 $this->quickCleanUpFinancialEntities();
81 $this->quickCleanup(['civicrm_uf_match', 'civicrm_contact'], TRUE);
86 * Get the id for the given type.
92 public function getMembershipTypeID(string $name): int {
93 return CRM_Core_PseudoConstant
::getKey('CRM_Member_BAO_Membership', 'membership_type_id', $name);
97 * Test membership deletion.
99 public function testMembershipDelete(): void
{
100 $membershipID = $this->contactMembershipCreate($this->_params
);
101 $this->assertDBRowExist('CRM_Member_DAO_Membership', $membershipID);
103 'id' => $membershipID,
105 $this->callAPIAndDocument('membership', 'delete', $params, __FUNCTION__
, __FILE__
);
106 $this->assertDBRowNotExist('CRM_Member_DAO_Membership', $membershipID);
109 public function testMembershipDeleteEmpty() {
110 $this->callAPIFailure('membership', 'delete', []);
113 public function testMembershipDeleteInvalidID() {
114 $this->callAPIFailure('membership', 'delete', ['id' => 'blah']);
118 * Test membership deletion and with the preserve contribution param.
120 public function testMembershipDeletePreserveContribution() {
122 $membershipID = $this->contactMembershipCreate($this->_params
);
124 $this->assertDBRowExist('CRM_Member_DAO_Membership', $membershipID);
125 $ContributionCreate = $this->callAPISuccess('Contribution', 'create', [
127 'financial_type_id' => "Member Dues",
128 'total_amount' => 100,
129 'contact_id' => $this->_params
['contact_id'],
131 $this->callAPISuccess('MembershipPayment', 'create', [
133 'contribution_id' => $ContributionCreate['values'][0]['id'],
134 'membership_id' => $membershipID,
137 'id' => $membershipID,
138 'preserve_contribution' => 1,
141 'id' => $ContributionCreate['values'][0]['id'],
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']);
151 * Test Activity creation on cancellation of membership contribution.
153 * @throws \CRM_Core_Exception
154 * @throws \CiviCRM_API3_Exception
156 public function testActivityForCancelledContribution(): void
{
157 $contactId = $this->ids
['Contact']['order'] = $this->createLoggedInUser();
159 $this->createContributionAndMembershipOrder();
160 $membershipID = $this->callAPISuccessGetValue('MembershipPayment', ['return' => 'id']);
161 $form = new CRM_Contribute_Form_Contribution();
162 $form->_id
= $this->ids
['Contribution'][0];
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,
170 CRM_Core_Action
::UPDATE
);
172 $this->callAPISuccessGetSingle('Activity', [
173 'activity_type_id' => 'Membership Signup',
174 'source_record_id' => $membershipID,
175 'subject' => 'General - Payment - Status: Pending',
177 $this->callAPISuccessGetSingle('Activity', [
178 'activity_type_id' => 'Change Membership Status',
179 'source_record_id' => $membershipID,
184 * Test Multiple Membership Status for same contribution id.
186 public function testMultipleMembershipsContribution() {
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);
195 'membership_type_id' => 'General',
196 'source' => 'Webform Payment',
197 'status_id' => $pendingMembershipId,
199 'skipStatusCal' => 1,
203 $contactId1 = $this->individualCreate();
204 $membershipParam['contact_id'] = $contactId1;
205 $membershipID1 = $this->contactMembershipCreate($membershipParam);
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,
215 'receive_date' => date('Ymd'),
217 $this->callAPISuccess('MembershipPayment', 'create', [
219 'contribution_id' => $ContributionCreate['id'],
220 'membership_id' => $membershipID1,
224 $contactId2 = $this->individualCreate();
225 $membershipParam['contact_id'] = $contactId2;
226 $membershipID2 = $this->contactMembershipCreate($membershipParam);
227 $this->callAPISuccess('MembershipPayment', 'create', [
229 'contribution_id' => $ContributionCreate['id'],
230 'membership_id' => $membershipID2,
234 $contactId3 = $this->individualCreate();
235 $membershipParam['contact_id'] = $contactId3;
236 $membershipID3 = $this->contactMembershipCreate($membershipParam);
237 $this->callAPISuccess('MembershipPayment', 'create', [
239 'contribution_id' => $ContributionCreate['id'],
240 'membership_id' => $membershipID3,
243 // Change Payment Status to Completed
244 $form = new CRM_Contribute_Form_Contribution();
245 $form->_id
= $ContributionCreate['id'];
246 $params = ['id' => $ContributionCreate['id']];
248 CRM_Contribute_BAO_Contribution
::getValues($params, $values, $ids);
249 $form->_values
= $values;
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,
257 CRM_Core_Action
::UPDATE
);
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);
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);
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);
282 * Test membership get.
284 public function testContactMembershipsGet() {
285 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
286 $this->callAPISuccess('membership', 'get', []);
287 $this->callAPISuccess('Membership', 'Delete', ['id' => $this->_membershipID
]);
291 * Test civicrm_membership_get with params not array.
293 * Gets treated as contact_id, memberships expected.
295 public function testGetWithParamsContactId() {
296 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
298 'contact_id' => $this->_contactID
,
299 'return' => array_keys($this->_params
),
301 $membership = $this->callAPISuccess('membership', 'get', $params);
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']);
315 * Test civicrm_membership_get with params not array.
317 * Gets treated as contact_id, memberships expected.
319 public function testGetInSyntax() {
320 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
321 $this->_membershipID2
= $this->contactMembershipCreate($this->_params
);
322 $this->_membershipID3
= $this->contactMembershipCreate($this->_params
);
324 'id' => ['IN' => [$this->_membershipID
, $this->_membershipID3
]],
326 $membership = $this->callAPISuccess('membership', 'get', $params);
327 $this->assertEquals(2, $membership['count']);
328 $this->assertEquals([$this->_membershipID
, $this->_membershipID3
], array_keys($membership['values']));
330 'id' => ['NOT IN' => [$this->_membershipID
, $this->_membershipID3
]],
332 $membership = $this->callAPISuccess('membership', 'get', $params);
333 $this->assertEquals(1, $membership['count']);
334 $this->assertEquals([$this->_membershipID2
], array_keys($membership['values']));
338 * Test civicrm_membership_get with params not array.
339 * Gets treated as contact_id, memberships expected.
341 public function testGetInSyntaxOnContactID() {
342 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
343 $contact2 = $this->individualCreate();
344 $contact3 = $this->individualCreate(['first_name' => 'Scout', 'last_name' => 'Canine']);
345 $this->_membershipID2
= $this->contactMembershipCreate(array_merge($this->_params
, ['contact_id' => $contact2]));
346 $this->_membershipID3
= $this->contactMembershipCreate(array_merge($this->_params
, ['contact_id' => $contact3]));
348 'contact_id' => ['IN' => [$this->_contactID
, $contact3]],
350 $membership = $this->callAPISuccess('membership', 'get', $params);
351 $this->assertEquals(2, $membership['count']);
352 $this->assertEquals([$this->_membershipID
, $this->_membershipID3
], array_keys($membership['values']));
354 'contact_id' => ['NOT IN' => [$this->_contactID
, $contact3]],
356 $membership = $this->callAPISuccess('membership', 'get', $params);
357 $this->assertEquals(1, $membership['count']);
358 $this->assertEquals([$this->_membershipID2
], array_keys($membership['values']));
362 * Test civicrm_membership_get with params not array.
364 * Gets treated as contact_id, memberships expected.
366 public function testGetWithParamsMemberShipTypeId(): void
{
367 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
369 'membership_type_id' => 'General',
370 'return' => array_keys($this->_params
),
372 $membership = $this->callAPISuccess('membership', 'get', $params);
373 $result = $membership['values'][$membership['id']];
374 $this->assertEquals($result['contact_id'], $this->_contactID
);
375 $this->assertEquals($this->getMembershipTypeID('General'), $result['membership_type_id']);
376 $this->assertEquals($result['status_id'], $this->_membershipStatusID
);
377 $this->assertEquals($result['join_date'], '2009-01-21');
378 $this->assertEquals($result['start_date'], '2009-01-21');
379 $this->assertEquals($result['end_date'], '2009-12-21');
380 $this->assertEquals($result['source'], 'Payment');
381 $this->assertEquals($result['is_override'], 1);
382 $this->assertEquals($result['id'], $membership['id']);
386 * Test civicrm_membership_get with params not array.
387 * Gets treated as contact_id, memberships expected.
389 public function testGetWithParamsMemberShipTypeIdContactID() {
390 $params = $this->_params
;
391 $this->callAPISuccess($this->_entity
, 'create', $params);
392 $params['membership_type_id'] = $this->_membershipTypeID2
;
393 $this->callAPISuccess($this->_entity
, 'create', $params);
394 $this->callAPISuccessGetCount('membership', ['contact_id' => $this->_contactID
], 2);
396 'membership_type_id' => $this->_membershipTypeID
,
397 'contact_id' => $this->_contactID
,
399 $this->callAPISuccessGetCount('Membership', $params, 1);
402 'membership_type_id' => $this->_membershipTypeID2
,
403 'contact_id' => $this->_contactID
,
404 'return' => ['membership_type_id', 'contact_id'],
406 $result = $this->callAPISuccessGetSingle('Membership', $params);
407 $this->assertEquals($result['contact_id'], $this->_contactID
);
408 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID2
);
412 * Check with complete array + custom field.
414 * Note that the test is written on purpose without any
415 * variables specific to participant so it can be replicated into other entities
416 * and / or moved to the automated test suite
418 public function testGetWithParamsMemberShipIdAndCustom(): void
{
419 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
421 $params = $this->_params
;
422 $params['custom_' . $ids['custom_field_id']] = 'custom string';
424 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
426 $getParams = ['membership_type_id' => $params['membership_type_id'], 'return' => 'custom_' . $ids['custom_field_id']];
427 $check = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
428 $this->assertEquals('custom string', $check['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
432 * Test civicrm_membership_get with proper params.
433 * Memberships expected.
435 public function testGet() {
436 $this->contactMembershipCreate($this->_params
);
438 'contact_id' => $this->_contactID
,
439 'return' => array_keys($this->_params
),
442 $result = $this->callAPISuccessGetSingle('Membership', $params);
443 $this->assertEquals('2009-01-21', $result['join_date']);
444 $this->assertEquals($result['contact_id'], $this->_contactID
);
445 $this->assertEquals($result['membership_type_id'], $this->getMembershipTypeID('General'));
446 $this->assertEquals($result['status_id'], $this->_membershipStatusID
);
448 $this->assertEquals($result['start_date'], '2009-01-21');
449 $this->assertEquals($result['end_date'], '2009-12-21');
450 $this->assertEquals($result['source'], 'Payment');
451 $this->assertEquals($result['is_override'], 1);
455 * Test civicrm_membership_get with proper params.
456 * Memberships expected.
458 public function testGetWithId() {
459 $membershipID = $this->contactMembershipCreate($this->_params
);
461 'contact_id' => $this->_contactID
,
462 'id' => $this->_membershipID
,
465 $result = $this->callAPISuccess('membership', 'get', $params);
466 $this->assertEquals($membershipID, $result['id']);
468 'contact_id' => $this->_contactID
,
469 'membership_id' => $this->_membershipID
,
470 'return' => 'membership_id',
472 $result = $this->callAPISuccess('membership', 'get', $params);
473 $this->assertEquals($membershipID, $result['id']);
477 * Test civicrm_membership_get for only active.
478 * Memberships expected.
480 public function testGetOnlyActive() {
481 $description = "Demonstrates use of 'filter' active_only' param.";
482 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
484 'contact_id' => $this->_contactID
,
488 $membership = $this->callAPISuccess('membership', 'get', $params);
489 $this->assertEquals($membership['values'][$this->_membershipID
]['status_id'], $this->_membershipStatusID
);
490 $this->assertEquals($membership['values'][$this->_membershipID
]['contact_id'], $this->_contactID
);
492 'contact_id' => $this->_contactID
,
498 $membership = $this->callAPIAndDocument('membership', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'FilterIsCurrent');
499 $this->assertEquals($membership['values'][$this->_membershipID
]['status_id'], $this->_membershipStatusID
);
500 $this->assertEquals($membership['values'][$this->_membershipID
]['contact_id'], $this->_contactID
);
502 $this->callAPISuccess('Membership', 'Delete', ['id' => $this->_membershipID
]);
506 * Test civicrm_membership_get for non exist contact.
509 public function testGetNoContactExists() {
511 'contact_id' => 55555,
514 $membership = $this->callAPISuccess('membership', 'get', $params);
515 $this->assertEquals($membership['count'], 0);
519 * Test civicrm_membership_get with relationship.
522 * @throws \CRM_Core_Exception
524 public function testGetWithRelationship() {
525 $membershipOrgId = $this->organizationCreate(NULL);
526 $memberContactId = $this->individualCreate();
529 'name_a_b' => 'Relation 1',
530 'name_b_a' => 'Relation 2',
531 'description' => 'Testing relationship type',
532 'contact_type_a' => 'Organization',
533 'contact_type_b' => 'Individual',
537 $relTypeID = $this->relationshipTypeCreate($relTypeParams);
540 'name' => 'test General',
541 'duration_unit' => 'year',
542 'duration_interval' => 1,
543 'period_type' => 'rolling',
544 'member_of_contact_id' => $membershipOrgId,
546 'financial_type_id' => 1,
547 'relationship_type_id' => $relTypeID,
548 'relationship_direction' => 'b_a',
551 $memType = $this->callAPISuccess('membership_type', 'create', $params);
554 'contact_id' => $memberContactId,
555 'membership_type_id' => $memType['id'],
556 'join_date' => '2009-01-21',
557 'start_date' => '2009-01-21',
558 'end_date' => '2009-12-21',
559 'source' => 'Payment',
561 'status_id' => $this->_membershipStatusID
,
563 $membershipID = $this->contactMembershipCreate($params);
566 'contact_id' => $memberContactId,
567 'membership_type_id' => $memType['id'],
570 $result = $this->callAPISuccess('membership', 'get', $params);
572 $membership = $result['values'][$membershipID];
573 $this->assertEquals($this->_membershipStatusID
, $membership['status_id']);
574 $this->callAPISuccess('Membership', 'Delete', [
575 'id' => $membership['id'],
577 $this->membershipTypeDelete(['id' => $memType['id']]);
578 $this->relationshipTypeDelete($relTypeID);
579 $this->contactDelete($membershipOrgId);
580 $this->contactDelete($memberContactId);
584 * Test civicrm_membership_create with relationships.
585 * create/get Memberships.
587 * Test suite for CRM-14758: API ( contact, create ) does not always create related membership
588 * and max_related property for Membership_Type and Membership entities
590 * @throws \CRM_Core_Exception
592 public function testCreateWithRelationship() {
593 // Create membership type: inherited through employment, max_related = 2
595 'name_a_b' => 'Employee of',
597 $result = $this->callAPISuccess('relationship_type', 'get', $params);
598 $relationshipTypeId = $result['id'];
599 $membershipOrgId = $this->organizationCreate();
601 'name' => 'Corporate Membership',
602 'duration_unit' => 'year',
603 'duration_interval' => 1,
604 'period_type' => 'rolling',
605 'member_of_contact_id' => $membershipOrgId,
607 'financial_type_id' => 1,
608 'relationship_type_id' => $relationshipTypeId,
609 'relationship_direction' => 'b_a',
613 $result = $this->callAPISuccess('membership_type', 'create', $params);
614 $membershipTypeId = $result['id'];
616 // Create employer and first employee
617 $employerId[0] = $this->organizationCreate([], 1);
618 $memberContactId[0] = $this->individualCreate(['employer_id' => $employerId[0]], 0);
620 // Create organization's membership
622 'contact_id' => $employerId[0],
623 'membership_type_id' => $membershipTypeId,
624 'source' => 'Test suite',
625 'start_date' => date('Y-m-d'),
626 'end_date' => '+1 year',
628 $OrganizationMembershipID = $this->contactMembershipCreate($params);
630 // Check that the employee inherited the membership
632 'contact_id' => $memberContactId[0],
633 'membership_type_id' => $membershipTypeId,
636 $result = $this->callAPISuccess('membership', 'get', $params);
638 $this->assertEquals(1, $result['count']);
639 $result = $result['values'][$result['id']];
640 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
642 // Create second employee
643 $memberContactId[1] = $this->individualCreate(['employer_id' => $employerId[0]], 1);
645 // Check that the employee inherited the membership
647 'contact_id' => $memberContactId[1],
648 'membership_type_id' => $membershipTypeId,
650 $result = $this->callAPISuccess('membership', 'get', $params);
651 // If it fails here CRM-14758 is not fixed
652 $this->assertEquals(1, $result['count']);
653 $result = $result['values'][$result['id']];
654 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
656 // Create third employee
657 $memberContactId[2] = $this->individualCreate(['employer_id' => $employerId[0]], 2);
659 // Check that employee does NOT inherit the membership (max_related = 2)
661 'contact_id' => $memberContactId[2],
662 'membership_type_id' => $membershipTypeId,
664 $result = $this->callAPISuccess('membership', 'get', $params);
665 $this->assertEquals(0, $result['count']);
667 // Increase max_related for the employer's membership
669 'id' => $OrganizationMembershipID,
672 $this->callAPISuccess('Membership', 'create', $params);
674 // Check that the employee inherited the membership
676 'contact_id' => $memberContactId[2],
677 'membership_type_id' => $membershipTypeId,
679 $result = $this->callAPISuccess('membership', 'get', $params);
680 $this->assertEquals(1, $result['count']);
681 $result = $result['values'][$result['id']];
682 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
684 // First employee moves to a new job
685 $employerId[1] = $this->organizationCreate([], 2);
687 'id' => $memberContactId[0],
688 'employer_id' => $employerId[1],
690 $this->callAPISuccess('contact', 'create', $params);
692 // Check that employee does NO LONGER inherit the membership
694 'contact_id' => $memberContactId[0],
695 'membership_type_id' => $membershipTypeId,
697 $result = $this->callAPISuccess('membership', 'get', $params);
698 $this->assertEquals(0, $result['count']);
700 //Create pay_later membership for organization.
701 $employerId[2] = $this->organizationCreate([], 1);
703 'contact_id' => $employerId[2],
704 'membership_type_id' => $membershipTypeId,
705 'source' => 'Test pay later suite',
709 $organizationMembershipID = $this->callAPISuccess('Membership', 'create', $params)['id'];
711 $memberContactId[3] = $this->individualCreate(['employer_id' => $employerId[2]], 0);
712 // Check that the employee inherited the membership
714 'contact_id' => $memberContactId[3],
715 'membership_type_id' => $membershipTypeId,
717 $result = $this->callAPISuccessGetSingle('membership', $params);
718 $this->assertEquals($organizationMembershipID, $result['owner_membership_id']);
720 // Set up params for enable/disable checks
721 $relationship1 = $this->callAPISuccess('relationship', 'get', ['contact_id_a' => $memberContactId[1]]);
723 'contact_id' => $memberContactId[1],
724 'membership_type_id' => $membershipTypeId,
727 // Deactivate relationship using create and assert membership is not inherited
728 $this->callAPISuccess('relationship', 'create', ['id' => $relationship1['id'], 'is_active' => 0]);
729 $result = $this->callAPISuccess('membership', 'get', $params);
730 $this->assertEquals(0, $result['count']);
732 // Re-enable relationship using create and assert membership is inherited
733 $this->callAPISuccess('relationship', 'create', ['id' => $relationship1['id'], 'is_active' => 1]);
734 $result = $this->callAPISuccess('membership', 'get', $params);
735 $this->assertEquals(1, $result['count']);
737 // Deactivate relationship using setvalue and assert membership is not inherited
738 $this->callAPISuccess('relationship', 'setvalue', ['id' => $relationship1['id'], 'field' => 'is_active', 'value' => 0]);
739 $result = $this->callAPISuccess('membership', 'get', $params);
740 $this->assertEquals(0, $result['count']);
742 // Re-enable relationship using setvalue and assert membership is inherited
743 $this->callAPISuccess('relationship', 'setvalue', ['id' => $relationship1['id'], 'field' => 'is_active', 'value' => 1]);
744 $result = $this->callAPISuccess('membership', 'get', $params);
745 $this->assertEquals(1, $result['count']);
747 // Delete relationship and assert membership is not inherited
748 $this->callAPISuccess('relationship', 'delete', ['id' => $relationship1['id']]);
749 $result = $this->callAPISuccess('membership', 'get', $params);
750 $this->assertEquals(0, $result['count']);
752 // Tear down - reverse of creation to be safe
753 $this->contactDelete($memberContactId[2]);
754 $this->contactDelete($memberContactId[1]);
755 $this->contactDelete($memberContactId[0]);
756 $this->contactDelete($employerId[1]);
757 $this->contactDelete($employerId[0]);
758 $this->membershipTypeDelete(['id' => $membershipTypeId]);
759 $this->contactDelete($membershipOrgId);
763 * Test that loops are not created when adding spouse relationships.
765 * This add a test for https://issues.civicrm.org/jira/browse/CRM-4213 in the hope of removing
766 * the buggy fix for that without a resurgence.
768 * @throws \API_Exception
769 * @throws \CRM_Core_Exception
770 * @throws \Civi\API\Exception\UnauthorizedException
772 public function testCreateWithSpouseRelationship() {
773 $relationshipTypeID = RelationshipType
::get()->addSelect('id')->addWhere('name_a_b', '=', 'Spouse of')->execute()->first()['id'];
774 MembershipType
::update()->setValues([
775 'relationship_direction' => ['b_a', 'a_b'],
776 'relationship_type_id' => [$relationshipTypeID, $relationshipTypeID],
778 ->addWhere('name', '=', 'General')
779 ->execute()->first()['id'];
781 $spouse1ID = $this->individualCreate(['first_name' => 'him']);
782 $spouse2ID = $this->individualCreate(['first_name' => 'her']);
783 $spouse3ID = $this->individualCreate(['first_name' => 'they']);
784 $spouse4ID = $this->individualCreate(['first_name' => 'them']);
785 Relationship
::create()->setValues([
786 'contact_id_a' => $spouse1ID,
787 'contact_id_b' => $spouse2ID,
788 'relationship_type_id' => $relationshipTypeID,
791 $this->contactMembershipCreate([
792 'contact_id' => $spouse1ID,
793 'start_date' => date('Y-m-d'),
794 'end_date' => '+1 year',
797 $this->callAPISuccessGetSingle('Membership', [
798 'contact_id' => $spouse2ID,
799 'membership_type_id' => 'General',
802 $this->callAPISuccessGetSingle('Membership', [
803 'contact_id' => $spouse1ID,
804 'membership_type_id' => 'General',
806 // Add another Spouse
807 Relationship
::create()->setValues([
808 'contact_id_a' => $spouse3ID,
809 'contact_id_b' => $spouse1ID,
810 'relationship_type_id' => $relationshipTypeID,
812 $this->callAPISuccessGetSingle('Membership', [
813 'contact_id' => $spouse3ID,
814 'membership_type_id' => 'General',
816 $this->callAPISuccessGetCount('Membership', [], 3);
817 Relationship
::create()->setValues([
818 'contact_id_a' => $spouse1ID,
819 'contact_id_b' => $spouse4ID,
820 'relationship_type_id' => $relationshipTypeID,
823 $this->callAPISuccessGetSingle('Membership', [
824 'contact_id' => $spouse4ID,
825 'membership_type_id' => 'General',
828 $this->callAPISuccessGetCount('Membership', [], 4);
832 * We are checking for no e-notices + only id & end_date returned
834 * @throws \CRM_Core_Exception
836 public function testMembershipGetWithReturn() {
837 $this->contactMembershipCreate($this->_params
);
838 $result = $this->callAPISuccess('membership', 'get', ['return' => 'end_date']);
839 foreach ($result['values'] as $membership) {
840 $this->assertEquals(['id', 'end_date'], array_keys($membership));
844 ///////////////// civicrm_membership_create methods
847 * Test civicrm_contact_memberships_create with empty params.
850 public function testCreateWithEmptyParams() {
852 $this->callAPIFailure('membership', 'create', $params);
856 * If is_overide is passed in status must also be passed in.
858 public function testCreateOverrideNoStatus() {
859 $params = $this->_params
;
860 unset($params['status_id']);
861 $this->callAPIFailure('membership', 'create', $params);
864 public function testMembershipCreateMissingRequired() {
866 'membership_type_id' => '1',
867 'join_date' => '2006-01-21',
868 'start_date' => '2006-01-21',
869 'end_date' => '2006-12-21',
870 'source' => 'Payment',
874 $this->callAPIFailure('membership', 'create', $params);
877 public function testMembershipCreate() {
879 'contact_id' => $this->_contactID
,
880 'membership_type_id' => $this->getMembershipTypeID('General'),
881 'join_date' => '2006-01-21',
882 'start_date' => '2006-01-21',
883 'end_date' => '2006-12-21',
884 'source' => 'Payment',
886 'status_id' => $this->_membershipStatusID
,
889 $result = $this->callAPIAndDocument('membership', 'create', $params, __FUNCTION__
, __FILE__
);
890 $this->getAndCheck($params, $result['id'], $this->_entity
);
891 $this->assertNotNull($result['id']);
892 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['contact_id'], " in line " . __LINE__
);
893 $this->assertEquals($result['id'], $result['values'][$result['id']]['id'], " in line " . __LINE__
);
897 * Check for useful message if contact doesn't exist
899 public function testMembershipCreateWithInvalidContact(): void
{
902 'membership_type_id' => 'General',
903 'join_date' => '2006-01-21',
904 'start_date' => '2006-01-21',
905 'end_date' => '2006-12-21',
906 'source' => 'Payment',
908 'status_id' => $this->_membershipStatusID
,
911 $this->callAPIFailure('membership', 'create', $params,
912 'contact_id is not valid : 999'
916 public function testMembershipCreateWithInvalidStatus() {
917 $params = $this->_params
;
918 $params['status_id'] = 999;
919 $this->callAPIFailure('membership', 'create', $params,
920 "'999' is not a valid option for field status_id"
924 public function testMembershipCreateWithInvalidType() {
925 $params = $this->_params
;
926 $params['membership_type_id'] = 999;
928 $this->callAPIFailure('membership', 'create', $params,
929 "'999' is not a valid option for field membership_type_id"
934 * Check with complete array + custom field
935 * Note that the test is written on purpose without any
936 * variables specific to participant so it can be replicated into other entities
937 * and / or moved to the automated test suite
939 public function testCreateWithCustom() {
940 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
942 $params = $this->_params
;
943 $params['custom_' . $ids['custom_field_id']] = "custom string";
945 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
, NULL, 'CreateWithCustomData');
946 $check = $this->callAPISuccess($this->_entity
, 'get', [
947 'id' => $result['id'],
948 'contact_id' => $this->_contactID
,
950 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
954 * Search on custom field value.
956 public function testSearchWithCustomDataCRM16036(): void
{
957 // Create a custom field on membership
958 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
960 // Create a new membership, but don't assign anything to the custom field.
961 $params = $this->_params
;
962 $result = $this->callAPIAndDocument(
969 'SearchWithCustomData');
971 // search memberships with CRM-16036 as custom field value.
972 // Since we did not touch the custom field of any membership,
973 // this should not return any results.
974 $check = $this->callAPISuccess($this->_entity
, 'get', [
975 'custom_' . $ids['custom_field_id'] => 'CRM-16036',
977 $this->assertEquals(0, $check['count']);
981 * Test civicrm_contact_memberships_create with membership id (edit
985 public function testMembershipCreateWithId() {
986 $membershipID = $this->contactMembershipCreate($this->_params
);
988 'id' => $membershipID,
989 'contact_id' => $this->_contactID
,
990 'membership_type_id' => 'General',
991 'join_date' => '2006-01-21',
992 'start_date' => '2006-01-21',
993 'end_date' => '2006-12-21',
994 'source' => 'Payment',
996 'status_id' => $this->_membershipStatusID
,
999 $result = $this->callAPISuccess('membership', 'create', $params);
1001 //Update Status and check activities created.
1003 'id' => $result['id'],
1004 'status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Member_BAO_Membership', 'status_id', 'Cancelled'),
1006 $this->callAPISuccess('Membership', 'create', $updateStatus);
1007 $activities = CRM_Activity_BAO_Activity
::getContactActivity($this->_contactID
);
1008 $this->assertEquals(2, count($activities));
1009 $activityNames = array_flip(CRM_Utils_Array
::collect('activity_name', $activities));
1010 $this->assertArrayHasKey('Membership Signup', $activityNames);
1011 $this->assertArrayHasKey('Change Membership Status', $activityNames);
1013 $this->callAPISuccess('Membership', 'Delete', [
1014 'id' => $result['id'],
1016 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
1020 * Test civicrm_contact_memberships_create with membership id (edit
1024 public function testMembershipCreateUpdateWithIdNoContact() {
1025 $membershipID = $this->contactMembershipCreate($this->_params
);
1027 'id' => $membershipID,
1028 'membership_type_id' => $this->_membershipTypeID
,
1029 'contact_id' => $this->_contactID
,
1030 'join_date' => '2006-01-21',
1031 'start_date' => '2006-01-21',
1032 'end_date' => '2006-12-21',
1033 'source' => 'Payment',
1035 'status_id' => $this->_membershipStatusID
,
1038 $result = $this->callAPISuccess('membership', 'create', $params);
1039 $this->callAPISuccess('Membership', 'Delete', [
1040 'id' => $result['id'],
1043 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
1047 * Test civicrm_contact_memberships_create with membership id (edit
1051 public function testMembershipCreateUpdateWithIdNoDates() {
1052 $membershipID = $this->contactMembershipCreate($this->_params
);
1054 'id' => $membershipID,
1055 'contact_id' => $this->_contactID
,
1056 'membership_type_id' => $this->_membershipTypeID
,
1057 'source' => 'Payment',
1059 'status_id' => $this->_membershipStatusID
,
1062 $result = $this->callAPISuccess('membership', 'create', $params);
1063 $this->callAPISuccess('Membership', 'Delete', [
1064 'id' => $result['id'],
1066 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
1070 * Test civicrm_contact_memberships_create with membership id (edit
1074 public function testMembershipCreateUpdateWithIdNoDatesNoType() {
1075 $membershipID = $this->contactMembershipCreate($this->_params
);
1077 'id' => $membershipID,
1078 'source' => 'not much here',
1079 'contact_id' => $this->_contactID
,
1081 'status_id' => $this->_membershipStatusID
,
1084 $result = $this->callAPISuccess('membership', 'create', $params);
1085 $this->callAPISuccess('Membership', 'Delete', [
1086 'id' => $result['id'],
1088 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
1092 * Test civicrm_contact_memberships_create with membership id (edit
1096 public function testMembershipCreateUpdateWithIDAndSource() {
1097 $membershipID = $this->contactMembershipCreate($this->_params
);
1099 'id' => $membershipID,
1100 'source' => 'changed',
1101 'contact_id' => $this->_contactID
,
1102 'status_id' => $this->_membershipStatusID
,
1103 'membership_type_id' => $this->_membershipTypeID
,
1104 'skipStatusCal' => 1,
1106 $result = $this->callAPISuccess('membership', 'create', $params);
1107 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
1108 $this->callAPISuccess('Membership', 'Delete', [
1109 'id' => $result['id'],
1114 * Change custom field using update.
1116 public function testUpdateWithCustom() {
1117 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
1119 $params = $this->_params
;
1120 $params['custom_' . $ids['custom_field_id']] = "custom string";
1121 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
, NULL, 'UpdateCustomData');
1122 $result = $this->callAPISuccess($this->_entity
, 'create', [
1123 'id' => $result['id'],
1124 'custom_' . $ids['custom_field_id'] => "new custom",
1126 $check = $this->callAPISuccess($this->_entity
, 'get', [
1127 'id' => $result['id'],
1128 'contact_id' => $this->_contactID
,
1131 $this->assertEquals("new custom", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
1132 $this->callAPISuccess('Membership', 'Delete', [
1133 'id' => $check['id'],
1136 $this->customFieldDelete($ids['custom_field_id']);
1137 $this->customGroupDelete($ids['custom_group_id']);
1141 * per CRM-15746 check that the id can be altered in an update hook
1143 public function testMembershipUpdateCreateHookCRM15746() {
1144 $this->hookClass
->setHook('civicrm_pre', [$this, 'hook_civicrm_pre_update_create_membership']);
1145 $result = $this->callAPISuccess('membership', 'create', $this->_params
);
1146 $this->callAPISuccess('membership', 'create', ['id' => $result['id'], 'end_date' => '1 year ago']);
1147 $this->callAPISuccessGetCount('membership', [], 2);
1148 $this->hookClass
->reset();
1149 $this->callAPISuccess('membership', 'create', ['id' => $result['id'], 'end_date' => '1 year ago']);
1150 $this->callAPISuccessGetCount('membership', [], 2);
1154 * Custom hook for update membership.
1157 * @param object $objectName
1159 * @param array $params
1161 * @throws \Exception
1163 public function hook_civicrm_pre_update_create_membership($op, $objectName, $id, &$params) {
1164 if ($objectName === 'Membership' && $op === 'edit') {
1165 $existingMembership = $this->callAPISuccessGetSingle('membership', ['id' => $params['id']]);
1166 unset($params['id'], $params['membership_id']);
1167 $params['join_date'] = $params['membership_start_date'] = $params['start_date'] = date('Ymd000000', strtotime($existingMembership['start_date']));
1168 $params = array_merge($existingMembership, $params);
1169 $params['id'] = NULL;
1174 * Test civicrm_contact_memberships_create Invalid membership data.
1177 public function testMembershipCreateInvalidMemData() {
1178 //membership_contact_id as string
1180 'membership_contact_id' => 'Invalid',
1181 'membership_type_id' => $this->_membershipTypeID
,
1182 'join_date' => '2011-01-21',
1183 'start_date' => '2010-01-21',
1184 'end_date' => '2008-12-21',
1185 'source' => 'Payment',
1187 'status_id' => $this->_membershipStatusID
,
1190 $this->callAPIFailure('membership', 'create', $params);
1192 //membership_contact_id which is no in contact table
1193 $params['membership_contact_id'] = 999;
1194 $this->callAPIFailure('membership', 'create', $params);
1197 unset($params['membership_contact_id']);
1198 $params['join_date'] = "invalid";
1199 $this->callAPIFailure('Membership', 'Create', $params);
1203 * Test civicrm_contact_memberships_create with membership_contact_id
1207 public function testMembershipCreateWithMemContact() {
1209 'membership_contact_id' => $this->_contactID
,
1210 'membership_type_id' => $this->_membershipTypeID
,
1211 'join_date' => '2011-01-21',
1212 'start_date' => '2010-01-21',
1213 'end_date' => '2008-12-21',
1214 'source' => 'Payment',
1216 'status_id' => $this->_membershipStatusID
,
1219 $result = $this->callAPISuccess('membership', 'create', $params);
1221 $this->callAPISuccess('Membership', 'Delete', [
1222 'id' => $result['id'],
1227 * Test civicrm_contact_memberships_create with membership_contact_id
1231 public function testMembershipCreateValidMembershipTypeString(): void
{
1233 'membership_contact_id' => $this->_contactID
,
1234 'membership_type_id' => 'General',
1235 'join_date' => '2011-01-21',
1236 'start_date' => '2010-01-21',
1237 'end_date' => '2008-12-21',
1238 'source' => 'Payment',
1240 'status_id' => $this->_membershipStatusID
,
1243 $result = $this->callAPISuccess('membership', 'create', $params);
1244 $this->assertEquals($this->getMembershipTypeID('General'), $result['values'][$result['id']]['membership_type_id']);
1248 * Test civicrm_contact_memberships_create with membership_contact_id
1252 public function testMembershipCreateInValidMembershipTypeString() {
1254 'membership_contact_id' => $this->_contactID
,
1255 'membership_type_id' => 'invalid',
1256 'join_date' => '2011-01-21',
1257 'start_date' => '2010-01-21',
1258 'end_date' => '2008-12-21',
1259 'source' => 'Payment',
1261 'status_id' => $this->_membershipStatusID
,
1264 $this->callAPIFailure('membership', 'create', $params);
1268 * Test that if membership join date is not set it defaults to today.
1270 public function testEmptyJoinDate() {
1271 unset($this->_params
['join_date'], $this->_params
['is_override']);
1272 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1273 $result = $this->callAPISuccess($this->_entity
, 'getsingle', ['id' => $result['id']]);
1274 $this->assertEquals(date('Y-m-d', strtotime('now')), $result['join_date']);
1275 $this->assertEquals('2009-01-21', $result['start_date']);
1276 $this->assertEquals('2009-12-21', $result['end_date']);
1280 * Test that if membership start date is not set it defaults to correct end date.
1283 public function testEmptyStartDateFixed() {
1284 unset($this->_params
['start_date'], $this->_params
['is_override']);
1285 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1286 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1287 $result = $this->callAPISuccess($this->_entity
, 'getsingle', ['id' => $result['id']]);
1288 $this->assertEquals('2009-01-21', $result['join_date']);
1289 $this->assertEquals('2008-03-01', $result['start_date']);
1290 $this->assertEquals('2009-12-21', $result['end_date']);
1294 * Test that if membership start date is not set it defaults to correct end date
1297 public function testEmptyStartEndDateFixedOneYear() {
1298 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1299 $this->callAPISuccess('membership_type', 'create', ['id' => $this->_membershipTypeID2
, 'duration_interval' => 1]);
1300 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1301 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1302 $result = $this->callAPISuccess($this->_entity
, 'getsingle', ['id' => $result['id']]);
1303 $this->assertEquals('2009-01-21', $result['join_date']);
1304 $this->assertEquals('2008-03-01', $result['start_date']);
1305 $this->assertEquals('2010-02-28', $result['end_date']);
1309 * Test that if membership start date is not set it defaults to correct end date for fixed multi year memberships.
1311 public function testEmptyStartEndDateFixedMultiYear() {
1312 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1313 $this->callAPISuccess('membership_type', 'create', ['id' => $this->_membershipTypeID2
, 'duration_interval' => 5]);
1314 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1315 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1316 $result = $this->callAPISuccess($this->_entity
, 'getsingle', ['id' => $result['id']]);
1317 $this->assertEquals('2009-01-21', $result['join_date']);
1318 $this->assertEquals('2008-03-01', $result['start_date']);
1319 $this->assertEquals('2014-02-28', $result['end_date']);
1323 * CRM-18503 - Test membership join date is correctly set for fixed memberships.
1325 * @throws \CRM_Core_Exception|\CiviCRM_API3_Exception
1327 public function testMembershipJoinDateFixed() {
1328 $memStatus = CRM_Member_PseudoConstant
::membershipStatus();
1329 // Update the fixed membership type to 1 year duration.
1330 $this->callAPISuccess('membership_type', 'create', ['id' => $this->_membershipTypeID2
, 'duration_interval' => 1]);
1331 $contactId = $this->createLoggedInUser();
1332 // Create membership with 'Pending' status.
1334 'contact_id' => $contactId,
1335 'membership_type_id' => $this->_membershipTypeID2
,
1336 'source' => 'test membership',
1337 'is_pay_later' => 0,
1338 'status_id' => 'Pending',
1339 'skipStatusCal' => 1,
1340 'is_for_organization' => 1,
1342 $membership = $this->callAPISuccess('Membership', 'create', $params);
1344 // Update membership to 'Completed' and check the dates.
1346 'id' => $membership['id'],
1347 'contact_id' => $contactId,
1349 'membership_type_id' => $this->_membershipTypeID2
,
1351 'status_id' => 'New',
1353 $result = $this->callAPISuccess('Membership', 'create', $memParams);
1355 // Extend duration interval if join_date exceeds the rollover period.
1356 $joinDate = date('Y-m-d');
1358 $startDate = date('Y-m-d', strtotime(date('Y-03-01')));
1360 if (strtotime($startDate) > time()) {
1362 $startDate = date('Y-m-d', strtotime(date('Y-03-01') . '- 1 year'));
1364 $membershipTypeDetails = CRM_Member_BAO_MembershipType
::getMembershipType($this->_membershipTypeID2
);
1365 $fixedPeriodRollover = CRM_Member_BAO_MembershipType
::isDuringFixedAnnualRolloverPeriod($joinDate, $membershipTypeDetails, $year, $startDate);
1367 if ($fixedPeriodRollover && $rollOver) {
1372 'join_date' => date('Ymd'),
1373 'start_date' => str_replace('-', '', $startDate),
1374 'end_date' => date('Ymd', strtotime(date('Y-03-01') . "+ {$y} year - 1 day")),
1376 foreach ($result['values'] as $values) {
1377 foreach ($expectedDates as $date => $val) {
1378 $this->assertEquals($val, $values[$date], "Failed asserting {$date} values");
1384 * Test correct end and start dates are calculated for fixed multi year memberships.
1386 * The empty start date is calculated to be the start_date (1 Jan prior to the join_date - so 1 Jan 15)
1388 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1389 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1390 * and we add on 4 years rather than 5 because we are not after the rollover day - so we calculate 31 Dec 2019
1392 public function testFixedMultiYearDateSetTwoEmptyStartEndDate() {
1393 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1395 $this->callAPISuccess('membership_type', 'create', [
1396 'id' => $this->_membershipTypeID2
,
1397 'duration_interval' => 5,
1399 'fixed_period_start_day' => '101',
1401 'fixed_period_rollover_day' => '1101',
1403 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1405 'join_date' => '28-Jan 2015',
1407 $result = $this->callAPISuccess($this->_entity
, 'create', array_merge($this->_params
, $dates));
1408 $result = $this->callAPISuccess($this->_entity
, 'getsingle', ['id' => $result['id']]);
1409 $this->assertEquals('2015-01-28', $result['join_date']);
1410 $this->assertEquals('2015-01-01', $result['start_date']);
1411 $this->assertEquals('2019-12-31', $result['end_date']);
1415 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
1417 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1418 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1419 * and we add on 4 years rather than 5 because we are not after the rollover day - so we calculate 31 Dec 2019
1421 public function testFixedMultiYearDateSetTwoEmptyEndDate() {
1422 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1424 $this->callAPISuccess('membership_type', 'create', [
1425 'id' => $this->_membershipTypeID2
,
1426 'duration_interval' => 5,
1428 'fixed_period_start_day' => '101',
1430 'fixed_period_rollover_day' => '1101',
1432 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1434 'start_date' => '28-Jan 2015',
1435 'join_date' => '28-Jan 2015',
1437 $result = $this->callAPISuccess($this->_entity
, 'create', array_merge($this->_params
, $dates));
1438 $result = $this->callAPISuccess($this->_entity
, 'getsingle', ['id' => $result['id']]);
1439 $this->assertEquals('2015-01-28', $result['join_date']);
1440 $this->assertEquals('2015-01-28', $result['start_date']);
1441 $this->assertEquals('2019-12-31', $result['end_date']);
1445 * Test correct end and start dates are calculated for fixed multi year memberships.
1447 * The empty start date is calculated to be the start_date (1 Jan prior to the join_date - so 1 Jan 15)
1449 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1450 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1451 * and we add on <1 years rather than > 1 because we are not after the rollover day - so we calculate 31 Dec 2015
1453 public function testFixedSingleYearDateSetTwoEmptyStartEndDate() {
1454 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1456 $this->callAPISuccess('membership_type', 'create', [
1457 'id' => $this->_membershipTypeID2
,
1458 'duration_interval' => 1,
1460 'fixed_period_start_day' => '101',
1462 'fixed_period_rollover_day' => '1101',
1464 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1466 'join_date' => '28-Jan 2015',
1468 $result = $this->callAPISuccess($this->_entity
, 'create', array_merge($this->_params
, $dates));
1469 $result = $this->callAPISuccess($this->_entity
, 'getsingle', ['id' => $result['id']]);
1470 $this->assertEquals('2015-01-28', $result['join_date']);
1471 $this->assertEquals('2015-01-01', $result['start_date']);
1472 $this->assertEquals('2015-12-31', $result['end_date']);
1476 * Test correct end date for fixed single year memberships is calculated and start_date is not changed.
1478 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1479 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1480 * and we add on <1 years rather than > 1 because we are not after the rollover day - so we calculate 31 Dec 2015
1482 public function testFixedSingleYearDateSetTwoEmptyEndDate() {
1483 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1485 $this->callAPISuccess('membership_type', 'create', [
1486 'id' => $this->_membershipTypeID2
,
1487 'duration_interval' => 1,
1489 'fixed_period_start_day' => '101',
1491 'fixed_period_rollover_day' => '1101',
1493 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1495 'start_date' => '28-Jan 2015',
1496 'join_date' => '28-Jan 2015',
1498 $result = $this->callAPISuccess($this->_entity
, 'create', array_merge($this->_params
, $dates));
1499 $result = $this->callAPISuccess($this->_entity
, 'getsingle', ['id' => $result['id']]);
1500 $this->assertEquals('2015-01-28', $result['join_date']);
1501 $this->assertEquals('2015-01-28', $result['start_date']);
1502 $this->assertEquals('2015-12-31', $result['end_date']);
1506 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
1508 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1509 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1510 * and we add on 1 year we are after the rollover day - so we calculate 31 Oct 2016
1512 public function testFixedSingleYearDateSetThreeEmptyEndDate() {
1513 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1515 $this->callAPISuccess('membership_type', 'create', [
1516 'id' => $this->_membershipTypeID2
,
1517 'duration_interval' => 1,
1519 'fixed_period_start_day' => '1101',
1521 'fixed_period_rollover_day' => '101',
1523 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1525 'start_date' => '28-Jan 2015',
1526 'join_date' => '28-Jan 2015',
1528 $result = $this->callAPISuccess($this->_entity
, 'create', array_merge($this->_params
, $dates));
1529 $result = $this->callAPISuccess($this->_entity
, 'getsingle', ['id' => $result['id']]);
1530 $this->assertEquals('2015-01-28', $result['join_date']);
1531 $this->assertEquals('2015-01-28', $result['start_date']);
1532 $this->assertEquals('2016-10-31', $result['end_date']);
1536 * Test correct end and start dates are calculated for fixed multi year memberships.
1538 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
1540 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1541 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1542 * and we add on 1 year we are after the rollover day - so we calculate 31 Oct 2016
1544 public function testFixedSingleYearDateSetThreeEmptyStartEndDate() {
1545 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1547 $this->callAPISuccess('membership_type', 'create', [
1548 'id' => $this->_membershipTypeID2
,
1549 'duration_interval' => 1,
1551 'fixed_period_start_day' => '1101',
1553 'fixed_period_rollover_day' => '101',
1555 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1557 'join_date' => '28-Jan 2015',
1559 $result = $this->callAPISuccess($this->_entity
, 'create', array_merge($this->_params
, $dates));
1560 $result = $this->callAPISuccess($this->_entity
, 'getsingle', ['id' => $result['id']]);
1561 $this->assertEquals('2015-01-28', $result['join_date']);
1562 $this->assertEquals('2014-11-01', $result['start_date']);
1563 $this->assertEquals('2016-10-31', $result['end_date']);
1567 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
1569 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1570 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1571 * and we add on 5 years we are after the rollover day - so we calculate 31 Oct 2020
1573 public function testFixedMultiYearDateSetThreeEmptyEndDate() {
1574 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1576 $this->callAPISuccess('membership_type', 'create', [
1577 'id' => $this->_membershipTypeID2
,
1578 'duration_interval' => 5,
1580 'fixed_period_start_day' => '1101',
1582 'fixed_period_rollover_day' => '101',
1584 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1586 'start_date' => '28-Jan 2015',
1587 'join_date' => '28-Jan 2015',
1589 $result = $this->callAPISuccess($this->_entity
, 'create', array_merge($this->_params
, $dates));
1590 $result = $this->callAPISuccess($this->_entity
, 'getsingle', ['id' => $result['id']]);
1591 $this->assertEquals('2015-01-28', $result['join_date']);
1592 $this->assertEquals('2015-01-28', $result['start_date']);
1593 $this->assertEquals('2020-10-31', $result['end_date']);
1597 * Test correct end and start dates are calculated for fixed multi year memberships.
1599 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
1601 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
1602 * In this set our join date is after the start day and after the rollover day so we do get an extra year
1603 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1604 * and we add on 5 years we are after the rollover day - so we calculate 31 Oct 2020
1606 public function testFixedMultiYearDateSetThreeEmptyStartEndDate() {
1607 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1609 $this->callAPISuccess('membership_type', 'create', [
1610 'id' => $this->_membershipTypeID2
,
1611 'duration_interval' => 5,
1613 'fixed_period_start_day' => '1101',
1615 'fixed_period_rollover_day' => '101',
1617 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1619 'join_date' => '28-Jan 2015',
1621 $result = $this->callAPISuccess($this->_entity
, 'create', array_merge($this->_params
, $dates));
1622 $result = $this->callAPISuccess($this->_entity
, 'getsingle', ['id' => $result['id']]);
1623 $this->assertEquals('2015-01-28', $result['join_date']);
1624 $this->assertEquals('2014-11-01', $result['start_date']);
1625 $this->assertEquals('2020-10-31', $result['end_date']);
1629 * Test that if membership start date is not set it defaults to correct end date for fixed single year memberships.
1631 public function testEmptyStartDateRolling() {
1632 unset($this->_params
['start_date'], $this->_params
['is_override']);
1633 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1634 $result = $this->callAPISuccess($this->_entity
, 'getsingle', ['id' => $result['id']]);
1635 $this->assertEquals('2009-01-21', $result['join_date']);
1636 $this->assertEquals('2009-01-21', $result['start_date']);
1637 $this->assertEquals('2009-12-21', $result['end_date']);
1641 * Test that if membership end date is not set it defaults to correct end date.
1644 public function testEmptyEndDateFixed() {
1645 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1646 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1647 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1648 $result = $this->callAPISuccess($this->_entity
, 'getsingle', ['id' => $result['id']]);
1649 $this->assertEquals('2009-01-21', $result['join_date']);
1650 $this->assertEquals('2008-03-01', $result['start_date']);
1651 $this->assertEquals('2010-02-28', $result['end_date']);
1655 * Test that if membership end date is not set it defaults to correct end date.
1658 public function testEmptyEndDateRolling() {
1659 unset($this->_params
['is_override'], $this->_params
['end_date']);
1660 $this->_params
['membership_type_id'] = $this->_membershipTypeID
;
1661 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1662 $result = $this->callAPISuccess($this->_entity
, 'getsingle', ['id' => $result['id']]);
1663 $this->assertEquals('2009-01-21', $result['join_date']);
1664 $this->assertEquals('2009-01-21', $result['start_date']);
1665 $this->assertEquals('2010-01-20', $result['end_date']);
1669 * Test that if dates are set they not over-ridden if id is passed in
1671 public function testMembershipDatesNotOverridden() {
1672 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1673 unset($this->_params
['end_date'], $this->_params
['start_date']);
1674 $this->_params
['id'] = $result['id'];
1675 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1676 $result = $this->callAPISuccess($this->_entity
, 'getsingle', ['id' => $result['id']]);
1677 $this->assertEquals('2009-01-21', $result['join_date']);
1678 $this->assertEquals('2009-01-21', $result['start_date']);
1679 $this->assertEquals('2009-12-21', $result['end_date']);
1684 * Test that a contribution linked to multiple memberships results in all being updated.
1686 * @throws \CRM_Core_Exception
1688 public function testMultipleMembershipContribution() {
1689 $this->createMultipleMembershipOrder();
1690 $this->callAPISuccess('Payment', 'create', [
1691 'contribution_id' => $this->ids
['Contribution'][0],
1692 'payment_instrument_id' => 'Check',
1693 'total_amount' => 400,
1695 $memberships = $this->callAPISuccess('membership', 'get')['values'];
1696 $this->assertCount(2, $memberships);
1700 * Test that all membership types are returned when getoptions is called.
1702 * This test locks in current behaviour where types for all domains are returned. It should possibly be domain
1703 * specific but that should only be done in conjunction with adding a hook to allow that to be altered as the
1704 * multisite use case expects the master domain to be able to see all sites.
1708 public function testGetOptionsMembershipTypeID() {
1709 $options = $this->callAPISuccess('Membership', 'getoptions', ['field' => 'membership_type_id']);
1710 $this->assertEquals('Another one', array_pop($options['values']));
1711 $this->assertEquals('General', array_pop($options['values']));
1712 $this->assertEquals(NULL, array_pop($options['values']));