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