Merge pull request #19892 from colemanw/searchKitCreatedModified
[civicrm-core.git] / tests / phpunit / api / v3 / MembershipTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
7d61e75f
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035 11
696737b5 12use Civi\Api4\MembershipType;
13use Civi\Api4\Relationship;
14use Civi\Api4\RelationshipType;
15
6a488035
TO
16/**
17 * Test APIv3 civicrm_membership functions
18 *
6c6e6187
TO
19 * @package CiviCRM_APIv3
20 * @subpackage API_Member
6a488035
TO
21 */
22
4cbe18b8
EM
23/**
24 * Class api_v3_MembershipTest
acb109b7 25 * @group headless
4cbe18b8 26 */
6a488035 27class api_v3_MembershipTest extends CiviUnitTestCase {
89e65e3e 28
29 use CRMTraits_Financial_OrderTrait;
30
6a488035 31 protected $_contactID;
d54576ed
EM
32 protected $_membershipID;
33 protected $_membershipID2;
34 protected $_membershipID3;
6a488035 35 protected $_membershipTypeID;
8c33a68c 36 protected $_membershipTypeID2;
6a488035 37 protected $_membershipStatusID;
6a488035
TO
38 protected $_entity;
39 protected $_params;
b7c9bc4c 40
80d714d2 41 /**
42 * Set up for tests.
43 */
6a488035 44 public function setUp() {
6a488035 45 parent::setUp();
6a488035 46 $this->_contactID = $this->individualCreate();
9099cab3
CW
47 $this->_membershipTypeID = $this->membershipTypeCreate(['member_of_contact_id' => $this->_contactID]);
48 $this->_membershipTypeID2 = $this->membershipTypeCreate([
5896d037 49 'period_type' => 'fixed',
2ea0abec 50 // Ie. 1 March.
5896d037 51 'fixed_period_start_day' => '301',
2ea0abec 52 // Ie. 11 Nov.
21dfd5f5 53 'fixed_period_rollover_day' => '1111',
5d8b37be 54 'name' => 'Another one',
9099cab3 55 ]);
6a488035
TO
56 $this->_membershipStatusID = $this->membershipStatusCreate('test status');
57
6a488035
TO
58 CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'name', TRUE);
59 CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name');
60
61 $this->_entity = 'Membership';
9099cab3 62 $this->_params = [
6a488035
TO
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,
9099cab3 71 ];
6a488035
TO
72 }
73
80d714d2 74 /**
75 * Clean up after tests.
76 *
77 * @throws \Exception
78 */
00be9182 79 public function tearDown() {
fda18dc3 80 $this->quickCleanUpFinancialEntities();
81 $this->quickCleanup(['civicrm_uf_match'], TRUE);
6a488035 82 $this->contactDelete($this->_contactID);
fda18dc3 83 parent::tearDown();
6a488035
TO
84 }
85
86 /**
2ea0abec 87 * Test membership deletion.
6a488035 88 */
00be9182 89 public function testMembershipDelete() {
6a488035 90 $membershipID = $this->contactMembershipCreate($this->_params);
3506b6cd 91 $this->assertDBRowExist('CRM_Member_DAO_Membership', $membershipID);
9099cab3 92 $params = [
21dfd5f5 93 'id' => $membershipID,
9099cab3 94 ];
d54576ed 95 $this->callAPIAndDocument('membership', 'delete', $params, __FUNCTION__, __FILE__);
3506b6cd 96 $this->assertDBRowNotExist('CRM_Member_DAO_Membership', $membershipID);
6a488035
TO
97 }
98
00be9182 99 public function testMembershipDeleteEmpty() {
9099cab3 100 $this->callAPIFailure('membership', 'delete', []);
6a488035
TO
101 }
102
00be9182 103 public function testMembershipDeleteInvalidID() {
9099cab3 104 $this->callAPIFailure('membership', 'delete', ['id' => 'blah']);
6a488035
TO
105 }
106
ed4cc29d
JT
107 /**
108 * Test membership deletion and with the preserve contribution param.
109 */
110 public function testMembershipDeletePreserveContribution() {
39b959db
SL
111 //DELETE
112 $membershipID = $this->contactMembershipCreate($this->_params);
113 //DELETE
114 $this->assertDBRowExist('CRM_Member_DAO_Membership', $membershipID);
9099cab3 115 $ContributionCreate = $this->callAPISuccess('Contribution', 'create', [
ed4cc29d
JT
116 'sequential' => 1,
117 'financial_type_id' => "Member Dues",
118 'total_amount' => 100,
119 'contact_id' => $this->_params['contact_id'],
9099cab3 120 ]);
a89e0a3e 121 $this->callAPISuccess('MembershipPayment', 'create', [
ed4cc29d
JT
122 'sequential' => 1,
123 'contribution_id' => $ContributionCreate['values'][0]['id'],
124 'membership_id' => $membershipID,
9099cab3
CW
125 ]);
126 $memParams = [
ed4cc29d
JT
127 'id' => $membershipID,
128 'preserve_contribution' => 1,
9099cab3
CW
129 ];
130 $contribParams = [
ed4cc29d 131 'id' => $ContributionCreate['values'][0]['id'],
9099cab3 132 ];
ed4cc29d
JT
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
c277e79e
JP
140 /**
141 * Test Activity creation on cancellation of membership contribution.
311218aa 142 *
143 * @throws \CRM_Core_Exception
144 * @throws \CiviCRM_API3_Exception
c277e79e 145 */
311218aa 146 public function testActivityForCancelledContribution(): void {
c277e79e 147 $contactId = $this->createLoggedInUser();
c277e79e 148
311218aa 149 $this->createContributionAndMembershipOrder();
150 $membershipID = $this->callAPISuccessGetValue('MembershipPayment', ['return' => 'id']);
c277e79e 151 $form = new CRM_Contribute_Form_Contribution();
311218aa 152 $form->_id = $this->ids['Contribution'][0];
9099cab3 153 $form->testSubmit([
c277e79e
JP
154 'total_amount' => 100,
155 'financial_type_id' => 1,
c277e79e 156 'contact_id' => $contactId,
a89e0a3e 157 'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'),
c277e79e 158 'contribution_status_id' => 3,
9099cab3 159 ],
c277e79e
JP
160 CRM_Core_Action::UPDATE);
161
a89e0a3e 162 $this->callAPISuccessGetSingle('Activity', [
163 'activity_type_id' => 'Membership Signup',
164 'source_record_id' => $membershipID,
311218aa 165 'subject' => 'General - Payment - Status: Pending',
a89e0a3e 166 ]);
167 $this->callAPISuccessGetSingle('Activity', [
168 'activity_type_id' => 'Change Membership Status',
c277e79e 169 'source_record_id' => $membershipID,
9099cab3 170 ]);
c277e79e
JP
171 }
172
6e948f0d
SP
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
6a488035 271 /**
2ea0abec 272 * Test membership get.
6a488035 273 */
00be9182 274 public function testContactMembershipsGet() {
6a488035 275 $this->_membershipID = $this->contactMembershipCreate($this->_params);
9099cab3
CW
276 $this->callAPISuccess('membership', 'get', []);
277 $this->callAPISuccess('Membership', 'Delete', ['id' => $this->_membershipID]);
6a488035
TO
278 }
279
280 /**
281 * Test civicrm_membership_get with params not array.
2ea0abec 282 *
6a488035
TO
283 * Gets treated as contact_id, memberships expected.
284 */
00be9182 285 public function testGetWithParamsContactId() {
6a488035 286 $this->_membershipID = $this->contactMembershipCreate($this->_params);
9099cab3 287 $params = [
6a488035 288 'contact_id' => $this->_contactID,
9099cab3 289 ];
771f3245 290 $membership = $this->callAPISuccess('membership', 'get', $params);
6a488035
TO
291
292 $result = $membership['values'][$this->_membershipID];
9099cab3 293 $this->callAPISuccess('Membership', 'Delete', [
6a488035 294 'id' => $this->_membershipID,
9099cab3 295 ]);
8f67d99a 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);
6a488035
TO
304 }
305
b4529041 306 /**
307 * Test civicrm_membership_get with params not array.
2ea0abec 308 *
b4529041 309 * Gets treated as contact_id, memberships expected.
310 */
00be9182 311 public function testGetInSyntax() {
b4529041 312 $this->_membershipID = $this->contactMembershipCreate($this->_params);
313 $this->_membershipID2 = $this->contactMembershipCreate($this->_params);
314 $this->_membershipID3 = $this->contactMembershipCreate($this->_params);
9099cab3
CW
315 $params = [
316 'id' => ['IN' => [$this->_membershipID, $this->_membershipID3]],
317 ];
b4529041 318 $membership = $this->callAPISuccess('membership', 'get', $params);
319 $this->assertEquals(2, $membership['count']);
9099cab3
CW
320 $this->assertEquals([$this->_membershipID, $this->_membershipID3], array_keys($membership['values']));
321 $params = [
322 'id' => ['NOT IN' => [$this->_membershipID, $this->_membershipID3]],
323 ];
b4529041 324 $membership = $this->callAPISuccess('membership', 'get', $params);
325 $this->assertEquals(1, $membership['count']);
9099cab3 326 $this->assertEquals([$this->_membershipID2], array_keys($membership['values']));
b4529041 327 }
328
caca32ba 329 /**
330 * Test civicrm_membership_get with params not array.
331 * Gets treated as contact_id, memberships expected.
332 */
00be9182 333 public function testGetInSyntaxOnContactID() {
caca32ba 334 $this->_membershipID = $this->contactMembershipCreate($this->_params);
335 $contact2 = $this->individualCreate();
9099cab3
CW
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 ];
caca32ba 342 $membership = $this->callAPISuccess('membership', 'get', $params);
343 $this->assertEquals(2, $membership['count']);
9099cab3
CW
344 $this->assertEquals([$this->_membershipID, $this->_membershipID3], array_keys($membership['values']));
345 $params = [
346 'contact_id' => ['NOT IN' => [$this->_contactID, $contact3]],
347 ];
caca32ba 348 $membership = $this->callAPISuccess('membership', 'get', $params);
349 $this->assertEquals(1, $membership['count']);
9099cab3 350 $this->assertEquals([$this->_membershipID2], array_keys($membership['values']));
caca32ba 351 }
5896d037 352
caca32ba 353 /**
354 * Test civicrm_membership_get with params not array.
80d714d2 355 *
caca32ba 356 * Gets treated as contact_id, memberships expected.
357 */
00be9182 358 public function testGetWithParamsMemberShipTypeId() {
d54576ed 359 $this->callAPISuccess($this->_entity, 'create', $this->_params);
9099cab3 360 $params = [
6a488035 361 'membership_type_id' => $this->_membershipTypeID,
9099cab3 362 ];
771f3245 363 $membership = $this->callAPISuccess('membership', 'get', $params);
9099cab3 364 $this->callAPISuccess('Membership', 'Delete', [
6a488035 365 'id' => $membership['id'],
9099cab3 366 ]);
6a488035 367 $result = $membership['values'][$membership['id']];
80d714d2 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);
6a488035
TO
376 $this->assertEquals($result['id'], $membership['id']);
377 }
5896d037 378
a73daeff
E
379 /**
380 * Test civicrm_membership_get with params not array.
381 * Gets treated as contact_id, memberships expected.
382 */
00be9182 383 public function testGetWithParamsMemberShipTypeIdContactID() {
a73daeff
E
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);
9099cab3
CW
388 $this->callAPISuccessGetCount('membership', ['contact_id' => $this->_contactID], 2);
389 $params = [
a73daeff
E
390 'membership_type_id' => $this->_membershipTypeID,
391 'contact_id' => $this->_contactID,
9099cab3 392 ];
a73daeff
E
393 $result = $this->callAPISuccess('membership', 'getsingle', $params);
394 $this->assertEquals($result['contact_id'], $this->_contactID);
395 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID);
6a488035 396
9099cab3 397 $params = [
a73daeff
E
398 'membership_type_id' => $this->_membershipTypeID2,
399 'contact_id' => $this->_contactID,
9099cab3 400 ];
a73daeff
E
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 }
5896d037 405
6a488035 406 /**
80d714d2 407 * Check with complete array + custom field.
408 *
6a488035
TO
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 */
00be9182 413 public function testGetWithParamsMemberShipIdAndCustom() {
6a488035
TO
414 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
415
416 $params = $this->_params;
417 $params['custom_' . $ids['custom_field_id']] = "custom string";
418
771f3245 419 $result = $this->callAPISuccess($this->_entity, 'create', $params);
6a488035 420
9099cab3 421 $getParams = ['membership_type_id' => $params['membership_type_id']];
771f3245 422 $check = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
6a488035
TO
423 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
424
9099cab3 425 $this->callAPISuccess('Membership', 'Delete', [
6a488035 426 'id' => $result['id'],
9099cab3 427 ]);
6a488035
TO
428 }
429
430 /**
431 * Test civicrm_membership_get with proper params.
432 * Memberships expected.
433 */
00be9182 434 public function testGet() {
6a488035 435 $membershipID = $this->contactMembershipCreate($this->_params);
9099cab3 436 $params = [
6a488035 437 'contact_id' => $this->_contactID,
9099cab3 438 ];
6a488035 439
771f3245 440 $membership = $this->callAPISuccess('membership', 'get', $params);
6a488035 441 $result = $membership['values'][$membershipID];
9099cab3 442 $this->callAPISuccess('Membership', 'Delete', [
6a488035 443 'id' => $membership['id'],
9099cab3 444 ]);
80d714d2 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);
6a488035 449
80d714d2 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);
6a488035
TO
454 }
455
6a488035
TO
456 /**
457 * Test civicrm_membership_get with proper params.
458 * Memberships expected.
459 */
00be9182 460 public function testGetWithId() {
6a488035 461 $membershipID = $this->contactMembershipCreate($this->_params);
9099cab3 462 $params = [
6a488035 463 'contact_id' => $this->_contactID,
d54576ed 464 'id' => $this->_membershipID,
6a488035 465 'return' => 'id',
9099cab3 466 ];
771f3245 467 $result = $this->callAPISuccess('membership', 'get', $params);
6a488035 468 $this->assertEquals($membershipID, $result['id']);
9099cab3 469 $params = [
6a488035 470 'contact_id' => $this->_contactID,
d54576ed 471 'membership_id' => $this->_membershipID,
6a488035 472 'return' => 'membership_id',
9099cab3 473 ];
771f3245 474 $result = $this->callAPISuccess('membership', 'get', $params);
6a488035 475 $this->assertEquals($membershipID, $result['id']);
6a488035
TO
476 }
477
478 /**
479 * Test civicrm_membership_get for only active.
480 * Memberships expected.
481 */
00be9182 482 public function testGetOnlyActive() {
5c49fee0 483 $description = "Demonstrates use of 'filter' active_only' param.";
6a488035 484 $this->_membershipID = $this->contactMembershipCreate($this->_params);
9099cab3 485 $params = [
6a488035
TO
486 'contact_id' => $this->_contactID,
487 'active_only' => 1,
9099cab3 488 ];
6a488035 489
771f3245 490 $membership = $this->callAPISuccess('membership', 'get', $params);
a73daeff
E
491 $this->assertEquals($membership['values'][$this->_membershipID]['status_id'], $this->_membershipStatusID);
492 $this->assertEquals($membership['values'][$this->_membershipID]['contact_id'], $this->_contactID);
9099cab3 493 $params = [
6a488035 494 'contact_id' => $this->_contactID,
9099cab3 495 'filters' => [
6a488035 496 'is_current' => 1,
9099cab3
CW
497 ],
498 ];
6a488035 499
a828d7b8 500 $membership = $this->callAPIAndDocument('membership', 'get', $params, __FUNCTION__, __FILE__, $description, 'FilterIsCurrent');
a73daeff
E
501 $this->assertEquals($membership['values'][$this->_membershipID]['status_id'], $this->_membershipStatusID);
502 $this->assertEquals($membership['values'][$this->_membershipID]['contact_id'], $this->_contactID);
6a488035 503
9099cab3 504 $this->callAPISuccess('Membership', 'Delete', ['id' => $this->_membershipID]);
6a488035
TO
505 }
506
507 /**
508 * Test civicrm_membership_get for non exist contact.
509 * empty Memberships.
510 */
00be9182 511 public function testGetNoContactExists() {
9099cab3 512 $params = [
6a488035 513 'contact_id' => 55555,
9099cab3 514 ];
6a488035 515
771f3245 516 $membership = $this->callAPISuccess('membership', 'get', $params);
80d714d2 517 $this->assertEquals($membership['count'], 0);
6a488035
TO
518 }
519
520 /**
521 * Test civicrm_membership_get with relationship.
522 * get Memberships.
fda18dc3 523 *
524 * @throws \CRM_Core_Exception
6a488035 525 */
00be9182 526 public function testGetWithRelationship() {
6a488035 527 $membershipOrgId = $this->organizationCreate(NULL);
e4d5f1e2 528 $memberContactId = $this->individualCreate();
6a488035 529
9099cab3 530 $relTypeParams = [
6a488035
TO
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,
9099cab3 538 ];
6a488035
TO
539 $relTypeID = $this->relationshipTypeCreate($relTypeParams);
540
9099cab3 541 $params = [
6a488035
TO
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,
5896d037 548 'financial_type_id' => 1,
6a488035
TO
549 'relationship_type_id' => $relTypeID,
550 'relationship_direction' => 'b_a',
551 'is_active' => 1,
9099cab3 552 ];
771f3245 553 $memType = $this->callAPISuccess('membership_type', 'create', $params);
6a488035 554
9099cab3 555 $params = [
6a488035
TO
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,
9099cab3 564 ];
6a488035
TO
565 $membershipID = $this->contactMembershipCreate($params);
566
9099cab3 567 $params = [
6a488035
TO
568 'contact_id' => $memberContactId,
569 'membership_type_id' => $memType['id'],
9099cab3 570 ];
6a488035 571
771f3245 572 $result = $this->callAPISuccess('membership', 'get', $params);
6a488035
TO
573
574 $membership = $result['values'][$membershipID];
771f3245 575 $this->assertEquals($this->_membershipStatusID, $membership['status_id']);
9099cab3 576 $this->callAPISuccess('Membership', 'Delete', [
6a488035 577 'id' => $membership['id'],
9099cab3
CW
578 ]);
579 $this->membershipTypeDelete(['id' => $memType['id']]);
6a488035
TO
580 $this->relationshipTypeDelete($relTypeID);
581 $this->contactDelete($membershipOrgId);
582 $this->contactDelete($memberContactId);
583 }
584
4cc99d00 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
310c2031 591 *
592 * @throws \CRM_Core_Exception
4cc99d00 593 */
00be9182 594 public function testCreateWithRelationship() {
4cc99d00 595 // Create membership type: inherited through employment, max_related = 2
9099cab3 596 $params = [
4cc99d00 597 'name_a_b' => 'Employee of',
9099cab3 598 ];
4cc99d00 599 $result = $this->callAPISuccess('relationship_type', 'get', $params);
600 $relationshipTypeId = $result['id'];
601 $membershipOrgId = $this->organizationCreate();
9099cab3 602 $params = [
4cc99d00 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,
9099cab3 614 ];
4cc99d00 615 $result = $this->callAPISuccess('membership_type', 'create', $params);
616 $membershipTypeId = $result['id'];
617
618 // Create employer and first employee
9099cab3
CW
619 $employerId[0] = $this->organizationCreate([], 1);
620 $memberContactId[0] = $this->individualCreate(['employer_id' => $employerId[0]], 0);
4cc99d00 621
622 // Create organization's membership
9099cab3 623 $params = [
4cc99d00 624 'contact_id' => $employerId[0],
625 'membership_type_id' => $membershipTypeId,
626 'source' => 'Test suite',
627 'start_date' => date('Y-m-d'),
310dfe72 628 'end_date' => '+1 year',
9099cab3 629 ];
4cc99d00 630 $OrganizationMembershipID = $this->contactMembershipCreate($params);
631
632 // Check that the employee inherited the membership
9099cab3 633 $params = [
4cc99d00 634 'contact_id' => $memberContactId[0],
635 'membership_type_id' => $membershipTypeId,
9099cab3 636 ];
4cc99d00 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
9099cab3 645 $memberContactId[1] = $this->individualCreate(['employer_id' => $employerId[0]], 1);
4cc99d00 646
647 // Check that the employee inherited the membership
9099cab3 648 $params = [
4cc99d00 649 'contact_id' => $memberContactId[1],
650 'membership_type_id' => $membershipTypeId,
9099cab3 651 ];
4cc99d00 652 $result = $this->callAPISuccess('membership', 'get', $params);
4cc99d00 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
9099cab3 659 $memberContactId[2] = $this->individualCreate(['employer_id' => $employerId[0]], 2);
4cc99d00 660
661 // Check that employee does NOT inherit the membership (max_related = 2)
9099cab3 662 $params = [
4cc99d00 663 'contact_id' => $memberContactId[2],
664 'membership_type_id' => $membershipTypeId,
9099cab3 665 ];
4cc99d00 666 $result = $this->callAPISuccess('membership', 'get', $params);
667 $this->assertEquals(0, $result['count']);
668
669 // Increase max_related for the employer's membership
9099cab3 670 $params = [
4cc99d00 671 'id' => $OrganizationMembershipID,
672 'max_related' => 3,
9099cab3 673 ];
5d8b37be 674 $this->callAPISuccess('Membership', 'create', $params);
4cc99d00 675
676 // Check that the employee inherited the membership
9099cab3 677 $params = [
4cc99d00 678 'contact_id' => $memberContactId[2],
679 'membership_type_id' => $membershipTypeId,
9099cab3 680 ];
4cc99d00 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
9099cab3
CW
687 $employerId[1] = $this->organizationCreate([], 2);
688 $params = [
4cc99d00 689 'id' => $memberContactId[0],
690 'employer_id' => $employerId[1],
9099cab3 691 ];
4cc99d00 692 $this->callAPISuccess('contact', 'create', $params);
693
694 // Check that employee does NO LONGER inherit the membership
9099cab3 695 $params = [
4cc99d00 696 'contact_id' => $memberContactId[0],
697 'membership_type_id' => $membershipTypeId,
9099cab3 698 ];
4cc99d00 699 $result = $this->callAPISuccess('membership', 'get', $params);
700 $this->assertEquals(0, $result['count']);
701
00538ec6 702 //Create pay_later membership for organization.
9099cab3
CW
703 $employerId[2] = $this->organizationCreate([], 1);
704 $params = [
00538ec6
JP
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,
9099cab3 710 ];
310dfe72 711 $organizationMembershipID = $this->callAPISuccess('Membership', 'create', $params)['id'];
712
9099cab3 713 $memberContactId[3] = $this->individualCreate(['employer_id' => $employerId[2]], 0);
00538ec6 714 // Check that the employee inherited the membership
9099cab3 715 $params = [
00538ec6
JP
716 'contact_id' => $memberContactId[3],
717 'membership_type_id' => $membershipTypeId,
9099cab3 718 ];
310dfe72 719 $result = $this->callAPISuccessGetSingle('membership', $params);
00538ec6
JP
720 $this->assertEquals($organizationMembershipID, $result['owner_membership_id']);
721
4a009ccf 722 // Set up params for enable/disable checks
9099cab3
CW
723 $relationship1 = $this->callAPISuccess('relationship', 'get', ['contact_id_a' => $memberContactId[1]]);
724 $params = [
4a009ccf
CW
725 'contact_id' => $memberContactId[1],
726 'membership_type_id' => $membershipTypeId,
9099cab3 727 ];
4a009ccf
CW
728
729 // Deactivate relationship using create and assert membership is not inherited
9099cab3 730 $this->callAPISuccess('relationship', 'create', ['id' => $relationship1['id'], 'is_active' => 0]);
4a009ccf
CW
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
9099cab3 735 $this->callAPISuccess('relationship', 'create', ['id' => $relationship1['id'], 'is_active' => 1]);
4a009ccf
CW
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
9099cab3 740 $this->callAPISuccess('relationship', 'setvalue', ['id' => $relationship1['id'], 'field' => 'is_active', 'value' => 0]);
4a009ccf
CW
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
9099cab3 745 $this->callAPISuccess('relationship', 'setvalue', ['id' => $relationship1['id'], 'field' => 'is_active', 'value' => 1]);
4a009ccf
CW
746 $result = $this->callAPISuccess('membership', 'get', $params);
747 $this->assertEquals(1, $result['count']);
748
1b5fad8a 749 // Delete relationship and assert membership is not inherited
9099cab3 750 $this->callAPISuccess('relationship', 'delete', ['id' => $relationship1['id']]);
1b5fad8a
CW
751 $result = $this->callAPISuccess('membership', 'get', $params);
752 $this->assertEquals(0, $result['count']);
753
4cc99d00 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]);
9099cab3 760 $this->membershipTypeDelete(['id' => $membershipTypeId]);
4cc99d00 761 $this->contactDelete($membershipOrgId);
762 }
763
696737b5 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
37eda84b 833 /**
0298287b 834 * We are checking for no e-notices + only id & end_date returned
310c2031 835 *
836 * @throws \CRM_Core_Exception
37eda84b 837 */
00be9182 838 public function testMembershipGetWithReturn() {
d54576ed 839 $this->contactMembershipCreate($this->_params);
9099cab3 840 $result = $this->callAPISuccess('membership', 'get', ['return' => 'end_date']);
6c6e6187 841 foreach ($result['values'] as $membership) {
9099cab3 842 $this->assertEquals(['id', 'end_date'], array_keys($membership));
37eda84b 843 }
844 }
39b959db 845
6a488035
TO
846 ///////////////// civicrm_membership_create methods
847
848 /**
849 * Test civicrm_contact_memberships_create with empty params.
850 * Error expected.
851 */
00be9182 852 public function testCreateWithEmptyParams() {
9099cab3 853 $params = [];
d54576ed 854 $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
855 }
856
857 /**
fe482240 858 * If is_overide is passed in status must also be passed in.
6a488035 859 */
00be9182 860 public function testCreateOverrideNoStatus() {
6a488035
TO
861 $params = $this->_params;
862 unset($params['status_id']);
d54576ed 863 $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
864 }
865
00be9182 866 public function testMembershipCreateMissingRequired() {
9099cab3 867 $params = [
6a488035
TO
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',
9099cab3 874 ];
6a488035 875
d54576ed 876 $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
877 }
878
00be9182 879 public function testMembershipCreate() {
9099cab3 880 $params = [
6a488035
TO
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,
9099cab3 889 ];
6a488035 890
771f3245 891 $result = $this->callAPIAndDocument('membership', 'create', $params, __FUNCTION__, __FILE__);
6a488035 892 $this->getAndCheck($params, $result['id'], $this->_entity);
6a488035
TO
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 }
5896d037 897
28a04ea9 898 /**
899 * Check for useful message if contact doesn't exist
900 */
00be9182 901 public function testMembershipCreateWithInvalidContact() {
9099cab3 902 $params = [
6a488035
TO
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,
9099cab3 911 ];
6a488035 912
d54576ed 913 $this->callAPIFailure('membership', 'create', $params,
771f3245 914 'contact_id is not valid : 999'
915 );
6a488035 916 }
5896d037 917
00be9182 918 public function testMembershipCreateWithInvalidStatus() {
6a488035
TO
919 $params = $this->_params;
920 $params['status_id'] = 999;
d54576ed 921 $this->callAPIFailure('membership', 'create', $params,
771f3245 922 "'999' is not a valid option for field status_id"
923 );
6a488035
TO
924 }
925
00be9182 926 public function testMembershipCreateWithInvalidType() {
6a488035
TO
927 $params = $this->_params;
928 $params['membership_type_id'] = 999;
929
d54576ed 930 $this->callAPIFailure('membership', 'create', $params,
771f3245 931 "'999' is not a valid option for field membership_type_id"
932 );
6a488035
TO
933 }
934
935 /**
100fef9d 936 * Check with complete array + custom field
6a488035
TO
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 */
00be9182 941 public function testCreateWithCustom() {
6a488035
TO
942 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
943
944 $params = $this->_params;
945 $params['custom_' . $ids['custom_field_id']] = "custom string";
946
a828d7b8 947 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, NULL, 'CreateWithCustomData');
9099cab3 948 $check = $this->callAPISuccess($this->_entity, 'get', [
5896d037 949 'id' => $result['id'],
21dfd5f5 950 'contact_id' => $this->_contactID,
9099cab3 951 ]);
6a488035 952 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
6a488035
TO
953 }
954
22e97101
JV
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.
9099cab3 976 $check = $this->callAPISuccess($this->_entity, 'get', [
22e97101 977 'custom_' . $ids['custom_field_id'] => "CRM-16036",
9099cab3 978 ]);
22e97101
JV
979
980 // Cleanup.
9099cab3 981 $this->callAPISuccess($this->_entity, 'delete', [
22e97101 982 'id' => $result['id'],
9099cab3 983 ]);
22e97101
JV
984
985 // Assert.
986 $this->assertEquals(0, $check['count']);
987 }
988
6a488035
TO
989 /**
990 * Test civicrm_contact_memberships_create with membership id (edit
991 * membership).
992 * success expected.
993 */
00be9182 994 public function testMembershipCreateWithId() {
6a488035 995 $membershipID = $this->contactMembershipCreate($this->_params);
9099cab3 996 $params = [
6a488035
TO
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,
9099cab3 1006 ];
6a488035 1007
771f3245 1008 $result = $this->callAPISuccess('membership', 'create', $params);
c329a76a
JP
1009
1010 //Update Status and check activities created.
9099cab3 1011 $updateStatus = [
c329a76a 1012 'id' => $result['id'],
1eae7f10 1013 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'Cancelled'),
9099cab3 1014 ];
c329a76a
JP
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
9099cab3 1022 $this->callAPISuccess('Membership', 'Delete', [
6a488035 1023 'id' => $result['id'],
9099cab3 1024 ]);
6a488035
TO
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 */
00be9182 1033 public function testMembershipCreateUpdateWithIdNoContact() {
6a488035 1034 $membershipID = $this->contactMembershipCreate($this->_params);
9099cab3 1035 $params = [
6a488035
TO
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,
9099cab3 1045 ];
6a488035 1046
771f3245 1047 $result = $this->callAPISuccess('membership', 'create', $params);
9099cab3 1048 $this->callAPISuccess('Membership', 'Delete', [
6a488035 1049 'id' => $result['id'],
9099cab3 1050 ]);
771f3245 1051
6a488035
TO
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 */
00be9182 1060 public function testMembershipCreateUpdateWithIdNoDates() {
6a488035 1061 $membershipID = $this->contactMembershipCreate($this->_params);
9099cab3 1062 $params = [
6a488035
TO
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,
9099cab3 1069 ];
6a488035 1070
771f3245 1071 $result = $this->callAPISuccess('membership', 'create', $params);
9099cab3 1072 $this->callAPISuccess('Membership', 'Delete', [
6a488035 1073 'id' => $result['id'],
9099cab3 1074 ]);
6a488035
TO
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 */
00be9182 1083 public function testMembershipCreateUpdateWithIdNoDatesNoType() {
6a488035 1084 $membershipID = $this->contactMembershipCreate($this->_params);
9099cab3 1085 $params = [
6a488035
TO
1086 'id' => $membershipID,
1087 'source' => 'not much here',
1088 'contact_id' => $this->_contactID,
1089 'is_override' => 1,
1090 'status_id' => $this->_membershipStatusID,
9099cab3 1091 ];
6a488035 1092
771f3245 1093 $result = $this->callAPISuccess('membership', 'create', $params);
9099cab3 1094 $this->callAPISuccess('Membership', 'Delete', [
6a488035 1095 'id' => $result['id'],
9099cab3 1096 ]);
6a488035
TO
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 */
00be9182 1105 public function testMembershipCreateUpdateWithIDAndSource() {
6a488035 1106 $membershipID = $this->contactMembershipCreate($this->_params);
9099cab3 1107 $params = [
6a488035
TO
1108 'id' => $membershipID,
1109 'source' => 'changed',
1110 'contact_id' => $this->_contactID,
6c6e6187 1111 'status_id' => $this->_membershipStatusID,
5896d037 1112 'membership_type_id' => $this->_membershipTypeID,
6a488035 1113 'skipStatusCal' => 1,
9099cab3 1114 ];
771f3245 1115 $result = $this->callAPISuccess('membership', 'create', $params);
6a488035 1116 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
9099cab3 1117 $this->callAPISuccess('Membership', 'Delete', [
6a488035 1118 'id' => $result['id'],
9099cab3 1119 ]);
6a488035
TO
1120 }
1121
1122 /**
eceb18cc 1123 * Change custom field using update.
6a488035 1124 */
00be9182 1125 public function testUpdateWithCustom() {
6a488035
TO
1126 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
1127
1128 $params = $this->_params;
1129 $params['custom_' . $ids['custom_field_id']] = "custom string";
a828d7b8 1130 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, NULL, 'UpdateCustomData');
9099cab3 1131 $result = $this->callAPISuccess($this->_entity, 'create', [
5896d037 1132 'id' => $result['id'],
21dfd5f5 1133 'custom_' . $ids['custom_field_id'] => "new custom",
9099cab3
CW
1134 ]);
1135 $check = $this->callAPISuccess($this->_entity, 'get', [
5896d037 1136 'id' => $result['id'],
21dfd5f5 1137 'contact_id' => $this->_contactID,
9099cab3 1138 ]);
6a488035
TO
1139
1140 $this->assertEquals("new custom", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
9099cab3 1141 $this->callAPISuccess('Membership', 'Delete', [
6a488035 1142 'id' => $check['id'],
9099cab3 1143 ]);
6a488035
TO
1144
1145 $this->customFieldDelete($ids['custom_field_id']);
1146 $this->customGroupDelete($ids['custom_group_id']);
1147 }
1148
93c482a4
EM
1149 /**
1150 * per CRM-15746 check that the id can be altered in an update hook
1151 */
28a04ea9 1152 public function testMembershipUpdateCreateHookCRM15746() {
9099cab3 1153 $this->hookClass->setHook('civicrm_pre', [$this, 'hook_civicrm_pre_update_create_membership']);
93c482a4 1154 $result = $this->callAPISuccess('membership', 'create', $this->_params);
9099cab3
CW
1155 $this->callAPISuccess('membership', 'create', ['id' => $result['id'], 'end_date' => '1 year ago']);
1156 $this->callAPISuccessGetCount('membership', [], 2);
93c482a4 1157 $this->hookClass->reset();
9099cab3
CW
1158 $this->callAPISuccess('membership', 'create', ['id' => $result['id'], 'end_date' => '1 year ago']);
1159 $this->callAPISuccessGetCount('membership', [], 2);
93c482a4
EM
1160 }
1161
2ea0abec
EM
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 */
28a04ea9 1172 public function hook_civicrm_pre_update_create_membership($op, $objectName, $id, &$params) {
89e65e3e 1173 if ($objectName === 'Membership' && $op === 'edit') {
9099cab3 1174 $existingMembership = $this->callAPISuccessGetSingle('membership', ['id' => $params['id']]);
93c482a4 1175 unset($params['id'], $params['membership_id']);
6c6e6187 1176 $params['join_date'] = $params['membership_start_date'] = $params['start_date'] = date('Ymd000000', strtotime($existingMembership['start_date']));
93c482a4
EM
1177 $params = array_merge($existingMembership, $params);
1178 $params['id'] = NULL;
1179 }
1180 }
1181
6a488035 1182 /**
fe482240 1183 * Test civicrm_contact_memberships_create Invalid membership data.
6a488035
TO
1184 * Error expected.
1185 */
00be9182 1186 public function testMembershipCreateInvalidMemData() {
6a488035 1187 //membership_contact_id as string
9099cab3 1188 $params = [
6a488035
TO
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,
5896d037 1196 'status_id' => $this->_membershipStatusID,
9099cab3 1197 ];
6a488035 1198
d54576ed 1199 $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
1200
1201 //membership_contact_id which is no in contact table
1202 $params['membership_contact_id'] = 999;
d54576ed 1203 $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
1204
1205 //invalid join date
1206 unset($params['membership_contact_id']);
1207 $params['join_date'] = "invalid";
d54576ed 1208 $this->callAPIFailure('Membership', 'Create', $params);
6a488035
TO
1209 }
1210
1211 /**
1212 * Test civicrm_contact_memberships_create with membership_contact_id
1213 * membership).
1214 * Success expected.
1215 */
00be9182 1216 public function testMembershipCreateWithMemContact() {
9099cab3 1217 $params = [
6a488035
TO
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,
9099cab3 1226 ];
6a488035 1227
771f3245 1228 $result = $this->callAPISuccess('membership', 'create', $params);
6a488035 1229
9099cab3 1230 $this->callAPISuccess('Membership', 'Delete', [
6a488035 1231 'id' => $result['id'],
9099cab3 1232 ]);
6a488035 1233 }
5896d037 1234
cc73900e 1235 /**
1236 * Test civicrm_contact_memberships_create with membership_contact_id
1237 * membership).
1238 * Success expected.
1239 */
00be9182 1240 public function testMembershipCreateValidMembershipTypeString() {
9099cab3 1241 $params = [
cc73900e 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,
9099cab3 1250 ];
cc73900e 1251
1252 $result = $this->callAPISuccess('membership', 'create', $params);
1253 $this->assertEquals($this->_membershipTypeID, $result['values'][$result['id']]['membership_type_id']);
9099cab3 1254 $this->callAPISuccess('Membership', 'Delete', [
cc73900e 1255 'id' => $result['id'],
9099cab3 1256 ]);
cc73900e 1257 }
1258
1259 /**
1260 * Test civicrm_contact_memberships_create with membership_contact_id
1261 * membership).
1262 * Success expected.
1263 */
00be9182 1264 public function testMembershipCreateInValidMembershipTypeString() {
9099cab3 1265 $params = [
cc73900e 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,
9099cab3 1274 ];
cc73900e 1275
d54576ed 1276 $this->callAPIFailure('membership', 'create', $params);
cc73900e 1277 }
6a488035 1278
cc73900e 1279 /**
eceb18cc 1280 * Test that if membership join date is not set it defaults to today.
cc73900e 1281 */
00be9182 1282 public function testEmptyJoinDate() {
8c33a68c 1283 unset($this->_params['join_date'], $this->_params['is_override']);
1284 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
9099cab3 1285 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
8c33a68c 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']);
cc73900e 1289 }
5896d037 1290
cc73900e 1291 /**
fe482240 1292 * Test that if membership start date is not set it defaults to correct end date.
8c33a68c 1293 * - fixed
cc73900e 1294 */
00be9182 1295 public function testEmptyStartDateFixed() {
8c33a68c 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);
9099cab3 1299 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
8c33a68c 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 }
cc73900e 1304
8c33a68c 1305 /**
1306 * Test that if membership start date is not set it defaults to correct end date
1307 * - fixed
1308 */
2ea0abec
EM
1309 public function testEmptyStartEndDateFixedOneYear() {
1310 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
9099cab3 1311 $this->callAPISuccess('membership_type', 'create', ['id' => $this->_membershipTypeID2, 'duration_interval' => 1]);
2ea0abec
EM
1312 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1313 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
9099cab3 1314 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
2ea0abec
EM
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 /**
9398f167
EM
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']);
9099cab3 1325 $this->callAPISuccess('membership_type', 'create', ['id' => $this->_membershipTypeID2, 'duration_interval' => 5]);
9398f167
EM
1326 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1327 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
9099cab3 1328 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
9398f167
EM
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
41dcb974 1334 /**
1335 * CRM-18503 - Test membership join date is correctly set for fixed memberships.
c1265fe7 1336 *
1b815fb3 1337 * @throws \CRM_Core_Exception|\CiviCRM_API3_Exception
41dcb974 1338 */
1339 public function testMembershipJoinDateFixed() {
1340 $memStatus = CRM_Member_PseudoConstant::membershipStatus();
1341 // Update the fixed membership type to 1 year duration.
9099cab3 1342 $this->callAPISuccess('membership_type', 'create', ['id' => $this->_membershipTypeID2, 'duration_interval' => 1]);
41dcb974 1343 $contactId = $this->createLoggedInUser();
1344 // Create membership with 'Pending' status.
9099cab3 1345 $params = [
41dcb974 1346 'contact_id' => $contactId,
1347 'membership_type_id' => $this->_membershipTypeID2,
1348 'source' => 'test membership',
1349 'is_pay_later' => 0,
1b815fb3 1350 'status_id' => 'Pending',
41dcb974 1351 'skipStatusCal' => 1,
1352 'is_for_organization' => 1,
9099cab3 1353 ];
1b815fb3 1354 $membership = $this->callAPISuccess('Membership', 'create', $params);
41dcb974 1355
1356 // Update membership to 'Completed' and check the dates.
9099cab3 1357 $memParams = [
1b815fb3 1358 'id' => $membership['id'],
41dcb974 1359 'contact_id' => $contactId,
1360 'is_test' => 0,
1361 'membership_type_id' => $this->_membershipTypeID2,
1362 'num_terms' => 1,
1b815fb3 1363 'status_id' => 'New',
9099cab3 1364 ];
41dcb974 1365 $result = $this->callAPISuccess('Membership', 'create', $memParams);
1366
70a87708 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')));
581eb285 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 }
1b815fb3 1376 $membershipTypeDetails = CRM_Member_BAO_MembershipType::getMembershipType($this->_membershipTypeID2);
70a87708 1377 $fixedPeriodRollover = CRM_Member_BAO_MembershipType::isDuringFixedAnnualRolloverPeriod($joinDate, $membershipTypeDetails, $year, $startDate);
1378 $y = 1;
581eb285 1379 if ($fixedPeriodRollover && $rollOver) {
1b815fb3 1380 ++$y;
70a87708 1381 }
1382
9099cab3 1383 $expectedDates = [
41dcb974 1384 'join_date' => date('Ymd'),
70a87708 1385 'start_date' => str_replace('-', '', $startDate),
1386 'end_date' => date('Ymd', strtotime(date('Y-03-01') . "+ {$y} year - 1 day")),
9099cab3 1387 ];
41dcb974 1388 foreach ($result['values'] as $values) {
1389 foreach ($expectedDates as $date => $val) {
70a87708 1390 $this->assertEquals($val, $values[$date], "Failed asserting {$date} values");
41dcb974 1391 }
1392 }
1393 }
1394
b1fc74f0 1395 /**
964a9e96
EM
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
9099cab3 1407 $this->callAPISuccess('membership_type', 'create', [
964a9e96
EM
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',
9099cab3 1414 ]);
964a9e96 1415 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
9099cab3 1416 $dates = [
964a9e96 1417 'join_date' => '28-Jan 2015',
9099cab3 1418 ];
964a9e96 1419 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
9099cab3 1420 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
964a9e96
EM
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.
b1fc74f0
EM
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 */
964a9e96 1433 public function testFixedMultiYearDateSetTwoEmptyEndDate() {
b1fc74f0
EM
1434 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1435
9099cab3 1436 $this->callAPISuccess('membership_type', 'create', [
b1fc74f0
EM
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',
9099cab3 1443 ]);
b1fc74f0 1444 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
9099cab3 1445 $dates = [
b1fc74f0
EM
1446 'start_date' => '28-Jan 2015',
1447 'join_date' => '28-Jan 2015',
9099cab3 1448 ];
b1fc74f0 1449 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
9099cab3 1450 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
b1fc74f0
EM
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 /**
964a9e96
EM
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
9099cab3 1468 $this->callAPISuccess('membership_type', 'create', [
964a9e96
EM
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',
9099cab3 1475 ]);
964a9e96 1476 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
9099cab3 1477 $dates = [
964a9e96 1478 'join_date' => '28-Jan 2015',
9099cab3 1479 ];
964a9e96 1480 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
9099cab3 1481 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
964a9e96
EM
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.
b1fc74f0
EM
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 */
964a9e96 1494 public function testFixedSingleYearDateSetTwoEmptyEndDate() {
b1fc74f0
EM
1495 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1496
9099cab3 1497 $this->callAPISuccess('membership_type', 'create', [
b1fc74f0
EM
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',
9099cab3 1504 ]);
b1fc74f0 1505 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
9099cab3 1506 $dates = [
b1fc74f0
EM
1507 'start_date' => '28-Jan 2015',
1508 'join_date' => '28-Jan 2015',
9099cab3 1509 ];
b1fc74f0 1510 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
9099cab3 1511 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
b1fc74f0
EM
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 /**
964a9e96 1518 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
b1fc74f0
EM
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
964a9e96 1522 * and we add on 1 year we are after the rollover day - so we calculate 31 Oct 2016
b1fc74f0 1523 */
964a9e96 1524 public function testFixedSingleYearDateSetThreeEmptyEndDate() {
b1fc74f0
EM
1525 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1526
9099cab3 1527 $this->callAPISuccess('membership_type', 'create', [
b1fc74f0
EM
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',
9099cab3 1534 ]);
b1fc74f0 1535 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
9099cab3 1536 $dates = [
b1fc74f0
EM
1537 'start_date' => '28-Jan 2015',
1538 'join_date' => '28-Jan 2015',
9099cab3 1539 ];
b1fc74f0 1540 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
9099cab3 1541 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
b1fc74f0
EM
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
964a9e96
EM
1547 /**
1548 * Test correct end and start dates are calculated for fixed multi year memberships.
1549 *
d177a2a6 1550 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
964a9e96
EM
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
9099cab3 1559 $this->callAPISuccess('membership_type', 'create', [
964a9e96
EM
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',
9099cab3 1566 ]);
964a9e96 1567 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
9099cab3 1568 $dates = [
964a9e96 1569 'join_date' => '28-Jan 2015',
9099cab3 1570 ];
964a9e96 1571 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
9099cab3 1572 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
964a9e96
EM
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 }
b1fc74f0
EM
1577
1578 /**
964a9e96 1579 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
b1fc74f0
EM
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 */
964a9e96 1585 public function testFixedMultiYearDateSetThreeEmptyEndDate() {
b1fc74f0
EM
1586 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1587
9099cab3 1588 $this->callAPISuccess('membership_type', 'create', [
b1fc74f0
EM
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',
9099cab3 1595 ]);
b1fc74f0 1596 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
9099cab3 1597 $dates = [
b1fc74f0
EM
1598 'start_date' => '28-Jan 2015',
1599 'join_date' => '28-Jan 2015',
9099cab3 1600 ];
b1fc74f0 1601 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
9099cab3 1602 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
b1fc74f0
EM
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 }
9398f167 1607
964a9e96
EM
1608 /**
1609 * Test correct end and start dates are calculated for fixed multi year memberships.
1610 *
d177a2a6 1611 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
964a9e96
EM
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
9099cab3 1621 $this->callAPISuccess('membership_type', 'create', [
964a9e96
EM
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',
9099cab3 1628 ]);
964a9e96 1629 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
9099cab3 1630 $dates = [
964a9e96 1631 'join_date' => '28-Jan 2015',
9099cab3 1632 ];
964a9e96 1633 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
9099cab3 1634 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
964a9e96
EM
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
9398f167
EM
1640 /**
1641 * Test that if membership start date is not set it defaults to correct end date for fixed single year memberships.
2ea0abec 1642 */
00be9182 1643 public function testEmptyStartDateRolling() {
8c33a68c 1644 unset($this->_params['start_date'], $this->_params['is_override']);
1645 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
9099cab3 1646 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
8c33a68c 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']);
cc73900e 1650 }
5896d037 1651
8c33a68c 1652 /**
eceb18cc 1653 * Test that if membership end date is not set it defaults to correct end date.
8c33a68c 1654 * - rolling
1655 */
00be9182 1656 public function testEmptyEndDateFixed() {
8c33a68c 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);
9099cab3 1660 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
8c33a68c 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 }
5896d037 1665
8c33a68c 1666 /**
eceb18cc 1667 * Test that if membership end date is not set it defaults to correct end date.
8c33a68c 1668 * - rolling
1669 */
00be9182 1670 public function testEmptyEndDateRolling() {
8c33a68c 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);
9099cab3 1674 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
8c33a68c 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
8c33a68c 1680 /**
452b9e04 1681 * Test that if dates are set they not over-ridden if id is passed in
8c33a68c 1682 */
6c6e6187 1683 public function testMembershipDatesNotOverridden() {
8c33a68c 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);
9099cab3 1688 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
8c33a68c 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
6c6e6187 1693 }
96025800 1694
89e65e3e 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
8b3df6dc 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() {
9099cab3 1721 $options = $this->callAPISuccess('Membership', 'getoptions', ['field' => 'membership_type_id']);
5d8b37be 1722 $this->assertEquals('Another one', array_pop($options['values']));
8b3df6dc 1723 $this->assertEquals('General', array_pop($options['values']));
1724 $this->assertEquals(NULL, array_pop($options['values']));
1725 }
1726
6a488035 1727}