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 * @throws \CRM_Core_Exception
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
);
326 'id' => ['IN' => [$this->_membershipID
, $this->_membershipID3
]],
328 $membership = $this->callAPISuccess('membership', 'get', $params);
329 $this->assertEquals(2, $membership['count']);
330 $this->assertEquals([$this->_membershipID
, $this->_membershipID3
], array_keys($membership['values']));
332 'id' => ['NOT IN' => [$this->_membershipID
, $this->_membershipID3
]],
334 $membership = $this->callAPISuccess('membership', 'get', $params);
335 $this->assertEquals(1, $membership['count']);
336 $this->assertEquals([$this->_membershipID2
], array_keys($membership['values']));
340 * Test civicrm_membership_get with params not array.
341 * Gets treated as contact_id, memberships expected.
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]));
350 'contact_id' => ['IN' => [$this->_contactID
, $contact3]],
352 $membership = $this->callAPISuccess('membership', 'get', $params);
353 $this->assertEquals(2, $membership['count']);
354 $this->assertEquals([$this->_membershipID
, $this->_membershipID3
], array_keys($membership['values']));
356 'contact_id' => ['NOT IN' => [$this->_contactID
, $contact3]],
358 $membership = $this->callAPISuccess('membership', 'get', $params);
359 $this->assertEquals(1, $membership['count']);
360 $this->assertEquals([$this->_membershipID2
], array_keys($membership['values']));
364 * Test civicrm_membership_get with params not array.
366 * Gets treated as contact_id, memberships expected.
368 public function testGetWithParamsMemberShipTypeId(): void
{
369 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
371 'membership_type_id' => 'General',
372 'return' => array_keys($this->_params
),
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']);
388 * Test civicrm_membership_get with params not array.
389 * Gets treated as contact_id, memberships expected.
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);
398 'membership_type_id' => $this->_membershipTypeID
,
399 'contact_id' => $this->_contactID
,
401 $this->callAPISuccessGetCount('Membership', $params, 1);
404 'membership_type_id' => $this->_membershipTypeID2
,
405 'contact_id' => $this->_contactID
,
406 'return' => ['membership_type_id', 'contact_id'],
408 $result = $this->callAPISuccessGetSingle('Membership', $params);
409 $this->assertEquals($result['contact_id'], $this->_contactID
);
410 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID2
);
414 * Check with complete array + custom field.
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
420 public function testGetWithParamsMemberShipIdAndCustom(): void
{
421 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
423 $params = $this->_params
;
424 $params['custom_' . $ids['custom_field_id']] = 'custom string';
426 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
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']]);
434 * Test civicrm_membership_get with proper params.
435 * Memberships expected.
437 public function testGet() {
438 $this->contactMembershipCreate($this->_params
);
440 'contact_id' => $this->_contactID
,
441 'return' => array_keys($this->_params
),
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
);
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);
457 * Test civicrm_membership_get with proper params.
458 * Memberships expected.
460 public function testGetWithId() {
461 $membershipID = $this->contactMembershipCreate($this->_params
);
463 'contact_id' => $this->_contactID
,
464 'id' => $this->_membershipID
,
467 $result = $this->callAPISuccess('membership', 'get', $params);
468 $this->assertEquals($membershipID, $result['id']);
470 'contact_id' => $this->_contactID
,
471 'membership_id' => $this->_membershipID
,
472 'return' => 'membership_id',
474 $result = $this->callAPISuccess('membership', 'get', $params);
475 $this->assertEquals($membershipID, $result['id']);
479 * Test civicrm_membership_get for only active.
480 * Memberships expected.
482 public function testGetOnlyActive() {
483 $description = "Demonstrates use of 'filter' active_only' param.";
484 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
486 'contact_id' => $this->_contactID
,
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
);
494 'contact_id' => $this->_contactID
,
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
);
504 $this->callAPISuccess('Membership', 'Delete', ['id' => $this->_membershipID
]);
508 * Test civicrm_membership_get for non exist contact.
511 public function testGetNoContactExists() {
513 'contact_id' => 55555,
516 $membership = $this->callAPISuccess('membership', 'get', $params);
517 $this->assertEquals($membership['count'], 0);
521 * Test civicrm_membership_get with relationship.
524 * @throws \CRM_Core_Exception
526 public function testGetWithRelationship() {
527 $membershipOrgId = $this->organizationCreate(NULL);
528 $memberContactId = $this->individualCreate();
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',
539 $relTypeID = $this->relationshipTypeCreate($relTypeParams);
542 'name' => 'test General',
543 'duration_unit' => 'year',
544 'duration_interval' => 1,
545 'period_type' => 'rolling',
546 'member_of_contact_id' => $membershipOrgId,
548 'financial_type_id' => 1,
549 'relationship_type_id' => $relTypeID,
550 'relationship_direction' => 'b_a',
553 $memType = $this->callAPISuccess('membership_type', 'create', $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',
563 'status_id' => $this->_membershipStatusID
,
565 $membershipID = $this->contactMembershipCreate($params);
568 'contact_id' => $memberContactId,
569 'membership_type_id' => $memType['id'],
572 $result = $this->callAPISuccess('membership', 'get', $params);
574 $membership = $result['values'][$membershipID];
575 $this->assertEquals($this->_membershipStatusID
, $membership['status_id']);
576 $this->callAPISuccess('Membership', 'Delete', [
577 'id' => $membership['id'],
579 $this->membershipTypeDelete(['id' => $memType['id']]);
580 $this->relationshipTypeDelete($relTypeID);
581 $this->contactDelete($membershipOrgId);
582 $this->contactDelete($memberContactId);
586 * Test civicrm_membership_create with relationships.
587 * create/get Memberships.
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
592 * @throws \CRM_Core_Exception
594 public function testCreateWithRelationship() {
595 // Create membership type: inherited through employment, max_related = 2
597 'name_a_b' => 'Employee of',
599 $result = $this->callAPISuccess('relationship_type', 'get', $params);
600 $relationshipTypeId = $result['id'];
601 $membershipOrgId = $this->organizationCreate();
603 'name' => 'Corporate Membership',
604 'duration_unit' => 'year',
605 'duration_interval' => 1,
606 'period_type' => 'rolling',
607 'member_of_contact_id' => $membershipOrgId,
609 'financial_type_id' => 1,
610 'relationship_type_id' => $relationshipTypeId,
611 'relationship_direction' => 'b_a',
615 $result = $this->callAPISuccess('membership_type', 'create', $params);
616 $membershipTypeId = $result['id'];
618 // Create employer and first employee
619 $employerId[0] = $this->organizationCreate([], 1);
620 $memberContactId[0] = $this->individualCreate(['employer_id' => $employerId[0]], 0);
622 // Create organization's membership
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',
630 $OrganizationMembershipID = $this->contactMembershipCreate($params);
632 // Check that the employee inherited the membership
634 'contact_id' => $memberContactId[0],
635 'membership_type_id' => $membershipTypeId,
638 $result = $this->callAPISuccess('membership', 'get', $params);
640 $this->assertEquals(1, $result['count']);
641 $result = $result['values'][$result['id']];
642 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
644 // Create second employee
645 $memberContactId[1] = $this->individualCreate(['employer_id' => $employerId[0]], 1);
647 // Check that the employee inherited the membership
649 'contact_id' => $memberContactId[1],
650 'membership_type_id' => $membershipTypeId,
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']);
658 // Create third employee
659 $memberContactId[2] = $this->individualCreate(['employer_id' => $employerId[0]], 2);
661 // Check that employee does NOT inherit the membership (max_related = 2)
663 'contact_id' => $memberContactId[2],
664 'membership_type_id' => $membershipTypeId,
666 $result = $this->callAPISuccess('membership', 'get', $params);
667 $this->assertEquals(0, $result['count']);
669 // Increase max_related for the employer's membership
671 'id' => $OrganizationMembershipID,
674 $this->callAPISuccess('Membership', 'create', $params);
676 // Check that the employee inherited the membership
678 'contact_id' => $memberContactId[2],
679 'membership_type_id' => $membershipTypeId,
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']);
686 // First employee moves to a new job
687 $employerId[1] = $this->organizationCreate([], 2);
689 'id' => $memberContactId[0],
690 'employer_id' => $employerId[1],
692 $this->callAPISuccess('contact', 'create', $params);
694 // Check that employee does NO LONGER inherit the membership
696 'contact_id' => $memberContactId[0],
697 'membership_type_id' => $membershipTypeId,
699 $result = $this->callAPISuccess('membership', 'get', $params);
700 $this->assertEquals(0, $result['count']);
702 //Create pay_later membership for organization.
703 $employerId[2] = $this->organizationCreate([], 1);
705 'contact_id' => $employerId[2],
706 'membership_type_id' => $membershipTypeId,
707 'source' => 'Test pay later suite',
711 $organizationMembershipID = $this->callAPISuccess('Membership', 'create', $params)['id'];
713 $memberContactId[3] = $this->individualCreate(['employer_id' => $employerId[2]], 0);
714 // Check that the employee inherited the membership
716 'contact_id' => $memberContactId[3],
717 'membership_type_id' => $membershipTypeId,
719 $result = $this->callAPISuccessGetSingle('membership', $params);
720 $this->assertEquals($organizationMembershipID, $result['owner_membership_id']);
722 // Set up params for enable/disable checks
723 $relationship1 = $this->callAPISuccess('relationship', 'get', ['contact_id_a' => $memberContactId[1]]);
725 'contact_id' => $memberContactId[1],
726 'membership_type_id' => $membershipTypeId,
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']);
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']);
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']);
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']);
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']);
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);
765 * Test that loops are not created when adding spouse relationships.
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.
770 * @throws \API_Exception
771 * @throws \CRM_Core_Exception
772 * @throws \Civi\API\Exception\UnauthorizedException
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],
780 ->addWhere('name', '=', 'General')
781 ->execute()->first()['id'];
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,
793 $this->contactMembershipCreate([
794 'contact_id' => $spouse1ID,
795 'start_date' => date('Y-m-d'),
796 'end_date' => '+1 year',
799 $this->callAPISuccessGetSingle('Membership', [
800 'contact_id' => $spouse2ID,
801 'membership_type_id' => 'General',
804 $this->callAPISuccessGetSingle('Membership', [
805 'contact_id' => $spouse1ID,
806 'membership_type_id' => 'General',
808 // Add another Spouse
809 Relationship
::create()->setValues([
810 'contact_id_a' => $spouse3ID,
811 'contact_id_b' => $spouse1ID,
812 'relationship_type_id' => $relationshipTypeID,
814 $this->callAPISuccessGetSingle('Membership', [
815 'contact_id' => $spouse3ID,
816 'membership_type_id' => 'General',
818 $this->callAPISuccessGetCount('Membership', [], 3);
819 Relationship
::create()->setValues([
820 'contact_id_a' => $spouse1ID,
821 'contact_id_b' => $spouse4ID,
822 'relationship_type_id' => $relationshipTypeID,
825 $this->callAPISuccessGetSingle('Membership', [
826 'contact_id' => $spouse4ID,
827 'membership_type_id' => 'General',
830 $this->callAPISuccessGetCount('Membership', [], 4);
834 * We are checking for no e-notices + only id & end_date returned
836 * @throws \CRM_Core_Exception
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));
846 ///////////////// civicrm_membership_create methods
849 * Test civicrm_contact_memberships_create with empty params.
852 public function testCreateWithEmptyParams() {
854 $this->callAPIFailure('membership', 'create', $params);
858 * If is_overide is passed in status must also be passed in.
860 public function testCreateOverrideNoStatus() {
861 $params = $this->_params
;
862 unset($params['status_id']);
863 $this->callAPIFailure('membership', 'create', $params);
866 public function testMembershipCreateMissingRequired() {
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',
876 $this->callAPIFailure('membership', 'create', $params);
879 public function testMembershipCreate() {
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',
888 'status_id' => $this->_membershipStatusID
,
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__
);
899 * Check for useful message if contact doesn't exist
901 public function testMembershipCreateWithInvalidContact(): void
{
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',
910 'status_id' => $this->_membershipStatusID
,
913 $this->callAPIFailure('membership', 'create', $params,
914 'contact_id is not valid : 999'
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"
926 public function testMembershipCreateWithInvalidType() {
927 $params = $this->_params
;
928 $params['membership_type_id'] = 999;
930 $this->callAPIFailure('membership', 'create', $params,
931 "'999' is not a valid option for field membership_type_id"
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
941 public function testCreateWithCustom() {
942 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
944 $params = $this->_params
;
945 $params['custom_' . $ids['custom_field_id']] = "custom string";
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
,
952 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
956 * Search on custom field value.
958 public function testSearchWithCustomDataCRM16036(): void
{
959 // Create a custom field on membership
960 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
962 // Create a new membership, but don't assign anything to the custom field.
963 $params = $this->_params
;
964 $result = $this->callAPIAndDocument(
971 'SearchWithCustomData');
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',
979 $this->assertEquals(0, $check['count']);
983 * Test civicrm_contact_memberships_create with membership id (edit
987 public function testMembershipCreateWithId() {
988 $membershipID = $this->contactMembershipCreate($this->_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',
998 'status_id' => $this->_membershipStatusID
,
1001 $result = $this->callAPISuccess('membership', 'create', $params);
1003 //Update Status and check activities created.
1005 'id' => $result['id'],
1006 'status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Member_BAO_Membership', 'status_id', 'Cancelled'),
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);
1015 $this->callAPISuccess('Membership', 'Delete', [
1016 'id' => $result['id'],
1018 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
1022 * Test civicrm_contact_memberships_create with membership id (edit
1026 public function testMembershipCreateUpdateWithIdNoContact() {
1027 $membershipID = $this->contactMembershipCreate($this->_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',
1037 'status_id' => $this->_membershipStatusID
,
1040 $result = $this->callAPISuccess('membership', 'create', $params);
1041 $this->callAPISuccess('Membership', 'Delete', [
1042 'id' => $result['id'],
1045 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
1049 * Test civicrm_contact_memberships_create with membership id (edit
1053 public function testMembershipCreateUpdateWithIdNoDates() {
1054 $membershipID = $this->contactMembershipCreate($this->_params
);
1056 'id' => $membershipID,
1057 'contact_id' => $this->_contactID
,
1058 'membership_type_id' => $this->_membershipTypeID
,
1059 'source' => 'Payment',
1061 'status_id' => $this->_membershipStatusID
,
1064 $result = $this->callAPISuccess('membership', 'create', $params);
1065 $this->callAPISuccess('Membership', 'Delete', [
1066 'id' => $result['id'],
1068 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
1072 * Test civicrm_contact_memberships_create with membership id (edit
1076 public function testMembershipCreateUpdateWithIdNoDatesNoType() {
1077 $membershipID = $this->contactMembershipCreate($this->_params
);
1079 'id' => $membershipID,
1080 'source' => 'not much here',
1081 'contact_id' => $this->_contactID
,
1083 'status_id' => $this->_membershipStatusID
,
1086 $result = $this->callAPISuccess('membership', 'create', $params);
1087 $this->callAPISuccess('Membership', 'Delete', [
1088 'id' => $result['id'],
1090 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
1094 * Test civicrm_contact_memberships_create with membership id (edit
1098 public function testMembershipCreateUpdateWithIDAndSource() {
1099 $membershipID = $this->contactMembershipCreate($this->_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,
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'],
1116 * Change custom field using update.
1118 public function testUpdateWithCustom() {
1119 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
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",
1128 $check = $this->callAPISuccess($this->_entity
, 'get', [
1129 'id' => $result['id'],
1130 'contact_id' => $this->_contactID
,
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'],
1138 $this->customFieldDelete($ids['custom_field_id']);
1139 $this->customGroupDelete($ids['custom_group_id']);
1143 * per CRM-15746 check that the id can be altered in an update hook
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);
1156 * Custom hook for update membership.
1159 * @param object $objectName
1161 * @param array $params
1163 * @throws \Exception
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;
1176 * Test civicrm_contact_memberships_create Invalid membership data.
1179 public function testMembershipCreateInvalidMemData() {
1180 //membership_contact_id as string
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',
1189 'status_id' => $this->_membershipStatusID
,
1192 $this->callAPIFailure('membership', 'create', $params);
1194 //membership_contact_id which is no in contact table
1195 $params['membership_contact_id'] = 999;
1196 $this->callAPIFailure('membership', 'create', $params);
1199 unset($params['membership_contact_id']);
1200 $params['join_date'] = "invalid";
1201 $this->callAPIFailure('Membership', 'Create', $params);
1205 * Test civicrm_contact_memberships_create with membership_contact_id
1209 public function testMembershipCreateWithMemContact() {
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',
1218 'status_id' => $this->_membershipStatusID
,
1221 $result = $this->callAPISuccess('membership', 'create', $params);
1223 $this->callAPISuccess('Membership', 'Delete', [
1224 'id' => $result['id'],
1229 * Test civicrm_contact_memberships_create with membership_contact_id
1233 public function testMembershipCreateValidMembershipTypeString(): void
{
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',
1242 'status_id' => $this->_membershipStatusID
,
1245 $result = $this->callAPISuccess('membership', 'create', $params);
1246 $this->assertEquals($this->getMembershipTypeID('General'), $result['values'][$result['id']]['membership_type_id']);
1250 * Test civicrm_contact_memberships_create with membership_contact_id
1254 public function testMembershipCreateInValidMembershipTypeString() {
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',
1263 'status_id' => $this->_membershipStatusID
,
1266 $this->callAPIFailure('membership', 'create', $params);
1270 * Test that if membership join date is not set it defaults to today.
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']);
1282 * Test that if membership start date is not set it defaults to correct end date.
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']);
1296 * Test that if membership start date is not set it defaults to correct end date
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']);
1311 * Test that if membership start date is not set it defaults to correct end date for fixed multi year memberships.
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']);
1325 * CRM-18503 - Test membership join date is correctly set for fixed memberships.
1327 * @throws \CRM_Core_Exception|\CiviCRM_API3_Exception
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.
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,
1344 $membership = $this->callAPISuccess('Membership', 'create', $params);
1346 // Update membership to 'Completed' and check the dates.
1348 'id' => $membership['id'],
1349 'contact_id' => $contactId,
1351 'membership_type_id' => $this->_membershipTypeID2
,
1353 'status_id' => 'New',
1355 $result = $this->callAPISuccess('Membership', 'create', $memParams);
1357 // Extend duration interval if join_date exceeds the rollover period.
1358 $joinDate = date('Y-m-d');
1360 $startDate = date('Y-m-d', strtotime(date('Y-03-01')));
1362 if (strtotime($startDate) > time()) {
1364 $startDate = date('Y-m-d', strtotime(date('Y-03-01') . '- 1 year'));
1366 $membershipTypeDetails = CRM_Member_BAO_MembershipType
::getMembershipType($this->_membershipTypeID2
);
1367 $fixedPeriodRollover = CRM_Member_BAO_MembershipType
::isDuringFixedAnnualRolloverPeriod($joinDate, $membershipTypeDetails, $year, $startDate);
1369 if ($fixedPeriodRollover && $rollOver) {
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")),
1378 foreach ($result['values'] as $values) {
1379 foreach ($expectedDates as $date => $val) {
1380 $this->assertEquals($val, $values[$date], "Failed asserting {$date} values");
1386 * Test correct end and start dates are calculated for fixed multi year memberships.
1388 * The empty start date is calculated to be the start_date (1 Jan prior to the join_date - so 1 Jan 15)
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
1394 public function testFixedMultiYearDateSetTwoEmptyStartEndDate() {
1395 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1397 $this->callAPISuccess('membership_type', 'create', [
1398 'id' => $this->_membershipTypeID2
,
1399 'duration_interval' => 5,
1401 'fixed_period_start_day' => '101',
1403 'fixed_period_rollover_day' => '1101',
1405 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1407 'join_date' => '28-Jan 2015',
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']);
1417 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
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
1423 public function testFixedMultiYearDateSetTwoEmptyEndDate() {
1424 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1426 $this->callAPISuccess('membership_type', 'create', [
1427 'id' => $this->_membershipTypeID2
,
1428 'duration_interval' => 5,
1430 'fixed_period_start_day' => '101',
1432 'fixed_period_rollover_day' => '1101',
1434 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1436 'start_date' => '28-Jan 2015',
1437 'join_date' => '28-Jan 2015',
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']);
1447 * Test correct end and start dates are calculated for fixed multi year memberships.
1449 * The empty start date is calculated to be the start_date (1 Jan prior to the join_date - so 1 Jan 15)
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
1455 public function testFixedSingleYearDateSetTwoEmptyStartEndDate() {
1456 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1458 $this->callAPISuccess('membership_type', 'create', [
1459 'id' => $this->_membershipTypeID2
,
1460 'duration_interval' => 1,
1462 'fixed_period_start_day' => '101',
1464 'fixed_period_rollover_day' => '1101',
1466 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1468 'join_date' => '28-Jan 2015',
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']);
1478 * Test correct end date for fixed single year memberships is calculated and start_date is not changed.
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
1484 public function testFixedSingleYearDateSetTwoEmptyEndDate() {
1485 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1487 $this->callAPISuccess('membership_type', 'create', [
1488 'id' => $this->_membershipTypeID2
,
1489 'duration_interval' => 1,
1491 'fixed_period_start_day' => '101',
1493 'fixed_period_rollover_day' => '1101',
1495 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1497 'start_date' => '28-Jan 2015',
1498 'join_date' => '28-Jan 2015',
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']);
1508 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
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
1514 public function testFixedSingleYearDateSetThreeEmptyEndDate() {
1515 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1517 $this->callAPISuccess('membership_type', 'create', [
1518 'id' => $this->_membershipTypeID2
,
1519 'duration_interval' => 1,
1521 'fixed_period_start_day' => '1101',
1523 'fixed_period_rollover_day' => '101',
1525 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1527 'start_date' => '28-Jan 2015',
1528 'join_date' => '28-Jan 2015',
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']);
1538 * Test correct end and start dates are calculated for fixed multi year memberships.
1540 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
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
1546 public function testFixedSingleYearDateSetThreeEmptyStartEndDate() {
1547 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1549 $this->callAPISuccess('membership_type', 'create', [
1550 'id' => $this->_membershipTypeID2
,
1551 'duration_interval' => 1,
1553 'fixed_period_start_day' => '1101',
1555 'fixed_period_rollover_day' => '101',
1557 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1559 'join_date' => '28-Jan 2015',
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']);
1569 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
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
1575 public function testFixedMultiYearDateSetThreeEmptyEndDate() {
1576 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1578 $this->callAPISuccess('membership_type', 'create', [
1579 'id' => $this->_membershipTypeID2
,
1580 'duration_interval' => 5,
1582 'fixed_period_start_day' => '1101',
1584 'fixed_period_rollover_day' => '101',
1586 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1588 'start_date' => '28-Jan 2015',
1589 'join_date' => '28-Jan 2015',
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']);
1599 * Test correct end and start dates are calculated for fixed multi year memberships.
1601 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
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
1608 public function testFixedMultiYearDateSetThreeEmptyStartEndDate() {
1609 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1611 $this->callAPISuccess('membership_type', 'create', [
1612 'id' => $this->_membershipTypeID2
,
1613 'duration_interval' => 5,
1615 'fixed_period_start_day' => '1101',
1617 'fixed_period_rollover_day' => '101',
1619 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1621 'join_date' => '28-Jan 2015',
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']);
1631 * Test that if membership start date is not set it defaults to correct end date for fixed single year memberships.
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']);
1643 * Test that if membership end date is not set it defaults to correct end date.
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']);
1657 * Test that if membership end date is not set it defaults to correct end date.
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']);
1671 * Test that if dates are set they not over-ridden if id is passed in
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']);
1686 * Test that a contribution linked to multiple memberships results in all being updated.
1688 * @throws \CRM_Core_Exception
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,
1697 $memberships = $this->callAPISuccess('membership', 'get')['values'];
1698 $this->assertCount(2, $memberships);
1702 * Test that all membership types are returned when getoptions is called.
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.
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']));