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