Merge pull request #16133 from eileenmcnaughton/member_test
[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::membershipType(NULL, TRUE);
54 CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'name', TRUE);
55 CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name');
56
57 $this->_entity = 'Membership';
9099cab3 58 $this->_params = [
6a488035
TO
59 'contact_id' => $this->_contactID,
60 'membership_type_id' => $this->_membershipTypeID,
61 'join_date' => '2009-01-21',
62 'start_date' => '2009-01-21',
63 'end_date' => '2009-12-21',
64 'source' => 'Payment',
65 'is_override' => 1,
66 'status_id' => $this->_membershipStatusID,
9099cab3 67 ];
6a488035
TO
68 }
69
80d714d2 70 /**
71 * Clean up after tests.
72 *
73 * @throws \Exception
74 */
00be9182 75 public function tearDown() {
fda18dc3 76 $this->quickCleanUpFinancialEntities();
77 $this->quickCleanup(['civicrm_uf_match'], TRUE);
6a488035 78 $this->contactDelete($this->_contactID);
fda18dc3 79 parent::tearDown();
6a488035
TO
80 }
81
82 /**
2ea0abec 83 * Test membership deletion.
6a488035 84 */
00be9182 85 public function testMembershipDelete() {
6a488035 86 $membershipID = $this->contactMembershipCreate($this->_params);
3506b6cd 87 $this->assertDBRowExist('CRM_Member_DAO_Membership', $membershipID);
9099cab3 88 $params = [
21dfd5f5 89 'id' => $membershipID,
9099cab3 90 ];
d54576ed 91 $this->callAPIAndDocument('membership', 'delete', $params, __FUNCTION__, __FILE__);
3506b6cd 92 $this->assertDBRowNotExist('CRM_Member_DAO_Membership', $membershipID);
6a488035
TO
93 }
94
00be9182 95 public function testMembershipDeleteEmpty() {
9099cab3 96 $this->callAPIFailure('membership', 'delete', []);
6a488035
TO
97 }
98
00be9182 99 public function testMembershipDeleteInvalidID() {
9099cab3 100 $this->callAPIFailure('membership', 'delete', ['id' => 'blah']);
6a488035
TO
101 }
102
ed4cc29d
JT
103 /**
104 * Test membership deletion and with the preserve contribution param.
105 */
106 public function testMembershipDeletePreserveContribution() {
39b959db
SL
107 //DELETE
108 $membershipID = $this->contactMembershipCreate($this->_params);
109 //DELETE
110 $this->assertDBRowExist('CRM_Member_DAO_Membership', $membershipID);
9099cab3 111 $ContributionCreate = $this->callAPISuccess('Contribution', 'create', [
ed4cc29d
JT
112 'sequential' => 1,
113 'financial_type_id' => "Member Dues",
114 'total_amount' => 100,
115 'contact_id' => $this->_params['contact_id'],
9099cab3
CW
116 ]);
117 $membershipPaymentCreate = $this->callAPISuccess('MembershipPayment', 'create', [
ed4cc29d
JT
118 'sequential' => 1,
119 'contribution_id' => $ContributionCreate['values'][0]['id'],
120 'membership_id' => $membershipID,
9099cab3
CW
121 ]);
122 $memParams = [
ed4cc29d
JT
123 'id' => $membershipID,
124 'preserve_contribution' => 1,
9099cab3
CW
125 ];
126 $contribParams = [
ed4cc29d 127 'id' => $ContributionCreate['values'][0]['id'],
9099cab3 128 ];
ed4cc29d
JT
129 $this->callAPIAndDocument('membership', 'delete', $memParams, __FUNCTION__, __FILE__);
130 $this->assertDBRowNotExist('CRM_Member_DAO_Membership', $membershipID);
131 $this->assertDBRowExist('CRM_Contribute_DAO_Contribution', $ContributionCreate['values'][0]['id']);
132 $this->callAPISuccess('Contribution', 'delete', $contribParams);
133 $this->assertDBRowNotExist('CRM_Contribute_DAO_Contribution', $ContributionCreate['values'][0]['id']);
134 }
135
c277e79e
JP
136 /**
137 * Test Activity creation on cancellation of membership contribution.
138 */
139 public function testActivityForCancelledContribution() {
140 $contactId = $this->createLoggedInUser();
141 $membershipID = $this->contactMembershipCreate($this->_params);
142 $this->assertDBRowExist('CRM_Member_DAO_Membership', $membershipID);
143
9099cab3 144 $ContributionCreate = $this->callAPISuccess('Contribution', 'create', [
c277e79e
JP
145 'financial_type_id' => "Member Dues",
146 'total_amount' => 100,
147 'contact_id' => $this->_params['contact_id'],
9099cab3
CW
148 ]);
149 $membershipPaymentCreate = $this->callAPISuccess('MembershipPayment', 'create', [
c277e79e
JP
150 'sequential' => 1,
151 'contribution_id' => $ContributionCreate['id'],
152 'membership_id' => $membershipID,
9099cab3
CW
153 ]);
154 $instruments = $this->callAPISuccess('contribution', 'getoptions', ['field' => 'payment_instrument_id']);
c277e79e
JP
155 $this->paymentInstruments = $instruments['values'];
156
157 $form = new CRM_Contribute_Form_Contribution();
158 $form->_id = $ContributionCreate['id'];
9099cab3 159 $form->testSubmit([
c277e79e
JP
160 'total_amount' => 100,
161 'financial_type_id' => 1,
c277e79e
JP
162 'contact_id' => $contactId,
163 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
164 'contribution_status_id' => 3,
9099cab3 165 ],
c277e79e
JP
166 CRM_Core_Action::UPDATE);
167
9099cab3 168 $activity = $this->callAPISuccess('Activity', 'get', [
c277e79e
JP
169 'activity_type_id' => "Change Membership Status",
170 'source_record_id' => $membershipID,
9099cab3 171 ]);
c277e79e
JP
172 $this->assertNotEmpty($activity['values']);
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'),
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 ];
00538ec6
JP
616 $organizationMembership = CRM_Member_BAO_Membership::add($params);
617 $organizationMembershipID = $organizationMembership->id;
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 ];
00538ec6
JP
624 $result = $this->callAPISuccess('membership', 'get', $params);
625 $this->assertEquals(1, $result['count']);
626 $result = $result['values'][$result['id']];
627 $this->assertEquals($organizationMembershipID, $result['owner_membership_id']);
628
4a009ccf 629 // Set up params for enable/disable checks
9099cab3
CW
630 $relationship1 = $this->callAPISuccess('relationship', 'get', ['contact_id_a' => $memberContactId[1]]);
631 $params = [
4a009ccf
CW
632 'contact_id' => $memberContactId[1],
633 'membership_type_id' => $membershipTypeId,
9099cab3 634 ];
4a009ccf
CW
635
636 // Deactivate relationship using create and assert membership is not inherited
9099cab3 637 $this->callAPISuccess('relationship', 'create', ['id' => $relationship1['id'], 'is_active' => 0]);
4a009ccf
CW
638 $result = $this->callAPISuccess('membership', 'get', $params);
639 $this->assertEquals(0, $result['count']);
640
641 // Re-enable relationship using create and assert membership is inherited
9099cab3 642 $this->callAPISuccess('relationship', 'create', ['id' => $relationship1['id'], 'is_active' => 1]);
4a009ccf
CW
643 $result = $this->callAPISuccess('membership', 'get', $params);
644 $this->assertEquals(1, $result['count']);
645
646 // Deactivate relationship using setvalue and assert membership is not inherited
9099cab3 647 $this->callAPISuccess('relationship', 'setvalue', ['id' => $relationship1['id'], 'field' => 'is_active', 'value' => 0]);
4a009ccf
CW
648 $result = $this->callAPISuccess('membership', 'get', $params);
649 $this->assertEquals(0, $result['count']);
650
651 // Re-enable relationship using setvalue and assert membership is inherited
9099cab3 652 $this->callAPISuccess('relationship', 'setvalue', ['id' => $relationship1['id'], 'field' => 'is_active', 'value' => 1]);
4a009ccf
CW
653 $result = $this->callAPISuccess('membership', 'get', $params);
654 $this->assertEquals(1, $result['count']);
655
1b5fad8a 656 // Delete relationship and assert membership is not inherited
9099cab3 657 $this->callAPISuccess('relationship', 'delete', ['id' => $relationship1['id']]);
1b5fad8a
CW
658 $result = $this->callAPISuccess('membership', 'get', $params);
659 $this->assertEquals(0, $result['count']);
660
4cc99d00 661 // Tear down - reverse of creation to be safe
662 $this->contactDelete($memberContactId[2]);
663 $this->contactDelete($memberContactId[1]);
664 $this->contactDelete($memberContactId[0]);
665 $this->contactDelete($employerId[1]);
666 $this->contactDelete($employerId[0]);
9099cab3 667 $this->membershipTypeDelete(['id' => $membershipTypeId]);
4cc99d00 668 $this->contactDelete($membershipOrgId);
669 }
670
37eda84b 671 /**
0298287b 672 * We are checking for no e-notices + only id & end_date returned
310c2031 673 *
674 * @throws \CRM_Core_Exception
37eda84b 675 */
00be9182 676 public function testMembershipGetWithReturn() {
d54576ed 677 $this->contactMembershipCreate($this->_params);
9099cab3 678 $result = $this->callAPISuccess('membership', 'get', ['return' => 'end_date']);
6c6e6187 679 foreach ($result['values'] as $membership) {
9099cab3 680 $this->assertEquals(['id', 'end_date'], array_keys($membership));
37eda84b 681 }
682 }
39b959db 683
6a488035
TO
684 ///////////////// civicrm_membership_create methods
685
686 /**
687 * Test civicrm_contact_memberships_create with empty params.
688 * Error expected.
689 */
00be9182 690 public function testCreateWithEmptyParams() {
9099cab3 691 $params = [];
d54576ed 692 $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
693 }
694
695 /**
fe482240 696 * If is_overide is passed in status must also be passed in.
6a488035 697 */
00be9182 698 public function testCreateOverrideNoStatus() {
6a488035
TO
699 $params = $this->_params;
700 unset($params['status_id']);
d54576ed 701 $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
702 }
703
00be9182 704 public function testMembershipCreateMissingRequired() {
9099cab3 705 $params = [
6a488035
TO
706 'membership_type_id' => '1',
707 'join_date' => '2006-01-21',
708 'start_date' => '2006-01-21',
709 'end_date' => '2006-12-21',
710 'source' => 'Payment',
711 'status_id' => '2',
9099cab3 712 ];
6a488035 713
d54576ed 714 $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
715 }
716
00be9182 717 public function testMembershipCreate() {
9099cab3 718 $params = [
6a488035
TO
719 'contact_id' => $this->_contactID,
720 'membership_type_id' => $this->_membershipTypeID,
721 'join_date' => '2006-01-21',
722 'start_date' => '2006-01-21',
723 'end_date' => '2006-12-21',
724 'source' => 'Payment',
725 'is_override' => 1,
726 'status_id' => $this->_membershipStatusID,
9099cab3 727 ];
6a488035 728
771f3245 729 $result = $this->callAPIAndDocument('membership', 'create', $params, __FUNCTION__, __FILE__);
6a488035 730 $this->getAndCheck($params, $result['id'], $this->_entity);
6a488035
TO
731 $this->assertNotNull($result['id']);
732 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['contact_id'], " in line " . __LINE__);
733 $this->assertEquals($result['id'], $result['values'][$result['id']]['id'], " in line " . __LINE__);
734 }
5896d037 735
28a04ea9 736 /**
737 * Check for useful message if contact doesn't exist
738 */
00be9182 739 public function testMembershipCreateWithInvalidContact() {
9099cab3 740 $params = [
6a488035
TO
741 'contact_id' => 999,
742 'membership_type_id' => $this->_membershipTypeID,
743 'join_date' => '2006-01-21',
744 'start_date' => '2006-01-21',
745 'end_date' => '2006-12-21',
746 'source' => 'Payment',
747 'is_override' => 1,
748 'status_id' => $this->_membershipStatusID,
9099cab3 749 ];
6a488035 750
d54576ed 751 $this->callAPIFailure('membership', 'create', $params,
771f3245 752 'contact_id is not valid : 999'
753 );
6a488035 754 }
5896d037 755
00be9182 756 public function testMembershipCreateWithInvalidStatus() {
6a488035
TO
757 $params = $this->_params;
758 $params['status_id'] = 999;
d54576ed 759 $this->callAPIFailure('membership', 'create', $params,
771f3245 760 "'999' is not a valid option for field status_id"
761 );
6a488035
TO
762 }
763
00be9182 764 public function testMembershipCreateWithInvalidType() {
6a488035
TO
765 $params = $this->_params;
766 $params['membership_type_id'] = 999;
767
d54576ed 768 $this->callAPIFailure('membership', 'create', $params,
771f3245 769 "'999' is not a valid option for field membership_type_id"
770 );
6a488035
TO
771 }
772
773 /**
100fef9d 774 * Check with complete array + custom field
6a488035
TO
775 * Note that the test is written on purpose without any
776 * variables specific to participant so it can be replicated into other entities
777 * and / or moved to the automated test suite
778 */
00be9182 779 public function testCreateWithCustom() {
6a488035
TO
780 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
781
782 $params = $this->_params;
783 $params['custom_' . $ids['custom_field_id']] = "custom string";
784
a828d7b8 785 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, NULL, 'CreateWithCustomData');
9099cab3 786 $check = $this->callAPISuccess($this->_entity, 'get', [
5896d037 787 'id' => $result['id'],
21dfd5f5 788 'contact_id' => $this->_contactID,
9099cab3 789 ]);
6a488035 790 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
6a488035
TO
791 }
792
22e97101
JV
793 /**
794 * Search on custom field value.
795 */
796 public function testSearchWithCustomDataCRM16036() {
797 // Create a custom field on membership
798 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
799
800 // Create a new membership, but don't assign anything to the custom field.
801 $params = $this->_params;
802 $result = $this->callAPIAndDocument(
803 $this->_entity,
804 'create',
805 $params,
806 __FUNCTION__,
807 __FILE__,
808 NULL,
809 'SearchWithCustomData');
810
811 // search memberships with CRM-16036 as custom field value.
812 // Since we did not touch the custom field of any membership,
813 // this should not return any results.
9099cab3 814 $check = $this->callAPISuccess($this->_entity, 'get', [
22e97101 815 'custom_' . $ids['custom_field_id'] => "CRM-16036",
9099cab3 816 ]);
22e97101
JV
817
818 // Cleanup.
9099cab3 819 $this->callAPISuccess($this->_entity, 'delete', [
22e97101 820 'id' => $result['id'],
9099cab3 821 ]);
22e97101
JV
822
823 // Assert.
824 $this->assertEquals(0, $check['count']);
825 }
826
6a488035
TO
827 /**
828 * Test civicrm_contact_memberships_create with membership id (edit
829 * membership).
830 * success expected.
831 */
00be9182 832 public function testMembershipCreateWithId() {
6a488035 833 $membershipID = $this->contactMembershipCreate($this->_params);
9099cab3 834 $params = [
6a488035
TO
835 'id' => $membershipID,
836 'contact_id' => $this->_contactID,
837 'membership_type_id' => $this->_membershipTypeID,
838 'join_date' => '2006-01-21',
839 'start_date' => '2006-01-21',
840 'end_date' => '2006-12-21',
841 'source' => 'Payment',
842 'is_override' => 1,
843 'status_id' => $this->_membershipStatusID,
9099cab3 844 ];
6a488035 845
771f3245 846 $result = $this->callAPISuccess('membership', 'create', $params);
c329a76a
JP
847
848 //Update Status and check activities created.
9099cab3 849 $updateStatus = [
c329a76a 850 'id' => $result['id'],
1eae7f10 851 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'Cancelled'),
9099cab3 852 ];
c329a76a
JP
853 $this->callAPISuccess('Membership', 'create', $updateStatus);
854 $activities = CRM_Activity_BAO_Activity::getContactActivity($this->_contactID);
855 $this->assertEquals(2, count($activities));
856 $activityNames = array_flip(CRM_Utils_Array::collect('activity_name', $activities));
857 $this->assertArrayHasKey('Membership Signup', $activityNames);
858 $this->assertArrayHasKey('Change Membership Status', $activityNames);
859
9099cab3 860 $this->callAPISuccess('Membership', 'Delete', [
6a488035 861 'id' => $result['id'],
9099cab3 862 ]);
6a488035
TO
863 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
864 }
865
866 /**
867 * Test civicrm_contact_memberships_create with membership id (edit
868 * membership).
869 * success expected.
870 */
00be9182 871 public function testMembershipCreateUpdateWithIdNoContact() {
6a488035 872 $membershipID = $this->contactMembershipCreate($this->_params);
9099cab3 873 $params = [
6a488035
TO
874 'id' => $membershipID,
875 'membership_type_id' => $this->_membershipTypeID,
876 'contact_id' => $this->_contactID,
877 'join_date' => '2006-01-21',
878 'start_date' => '2006-01-21',
879 'end_date' => '2006-12-21',
880 'source' => 'Payment',
881 'is_override' => 1,
882 'status_id' => $this->_membershipStatusID,
9099cab3 883 ];
6a488035 884
771f3245 885 $result = $this->callAPISuccess('membership', 'create', $params);
9099cab3 886 $this->callAPISuccess('Membership', 'Delete', [
6a488035 887 'id' => $result['id'],
9099cab3 888 ]);
771f3245 889
6a488035
TO
890 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
891 }
892
893 /**
894 * Test civicrm_contact_memberships_create with membership id (edit
895 * membership).
896 * success expected.
897 */
00be9182 898 public function testMembershipCreateUpdateWithIdNoDates() {
6a488035 899 $membershipID = $this->contactMembershipCreate($this->_params);
9099cab3 900 $params = [
6a488035
TO
901 'id' => $membershipID,
902 'contact_id' => $this->_contactID,
903 'membership_type_id' => $this->_membershipTypeID,
904 'source' => 'Payment',
905 'is_override' => 1,
906 'status_id' => $this->_membershipStatusID,
9099cab3 907 ];
6a488035 908
771f3245 909 $result = $this->callAPISuccess('membership', 'create', $params);
9099cab3 910 $this->callAPISuccess('Membership', 'Delete', [
6a488035 911 'id' => $result['id'],
9099cab3 912 ]);
6a488035
TO
913 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
914 }
915
916 /**
917 * Test civicrm_contact_memberships_create with membership id (edit
918 * membership).
919 * success expected.
920 */
00be9182 921 public function testMembershipCreateUpdateWithIdNoDatesNoType() {
6a488035 922 $membershipID = $this->contactMembershipCreate($this->_params);
9099cab3 923 $params = [
6a488035
TO
924 'id' => $membershipID,
925 'source' => 'not much here',
926 'contact_id' => $this->_contactID,
927 'is_override' => 1,
928 'status_id' => $this->_membershipStatusID,
9099cab3 929 ];
6a488035 930
771f3245 931 $result = $this->callAPISuccess('membership', 'create', $params);
9099cab3 932 $this->callAPISuccess('Membership', 'Delete', [
6a488035 933 'id' => $result['id'],
9099cab3 934 ]);
6a488035
TO
935 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
936 }
937
938 /**
939 * Test civicrm_contact_memberships_create with membership id (edit
940 * membership).
941 * success expected.
942 */
00be9182 943 public function testMembershipCreateUpdateWithIDAndSource() {
6a488035 944 $membershipID = $this->contactMembershipCreate($this->_params);
9099cab3 945 $params = [
6a488035
TO
946 'id' => $membershipID,
947 'source' => 'changed',
948 'contact_id' => $this->_contactID,
6c6e6187 949 'status_id' => $this->_membershipStatusID,
5896d037 950 'membership_type_id' => $this->_membershipTypeID,
6a488035 951 'skipStatusCal' => 1,
9099cab3 952 ];
771f3245 953 $result = $this->callAPISuccess('membership', 'create', $params);
6a488035 954 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
9099cab3 955 $this->callAPISuccess('Membership', 'Delete', [
6a488035 956 'id' => $result['id'],
9099cab3 957 ]);
6a488035
TO
958 }
959
960 /**
eceb18cc 961 * Change custom field using update.
6a488035 962 */
00be9182 963 public function testUpdateWithCustom() {
6a488035
TO
964 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
965
966 $params = $this->_params;
967 $params['custom_' . $ids['custom_field_id']] = "custom string";
a828d7b8 968 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, NULL, 'UpdateCustomData');
9099cab3 969 $result = $this->callAPISuccess($this->_entity, 'create', [
5896d037 970 'id' => $result['id'],
21dfd5f5 971 'custom_' . $ids['custom_field_id'] => "new custom",
9099cab3
CW
972 ]);
973 $check = $this->callAPISuccess($this->_entity, 'get', [
5896d037 974 'id' => $result['id'],
21dfd5f5 975 'contact_id' => $this->_contactID,
9099cab3 976 ]);
6a488035
TO
977
978 $this->assertEquals("new custom", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
9099cab3 979 $this->callAPISuccess('Membership', 'Delete', [
6a488035 980 'id' => $check['id'],
9099cab3 981 ]);
6a488035
TO
982
983 $this->customFieldDelete($ids['custom_field_id']);
984 $this->customGroupDelete($ids['custom_group_id']);
985 }
986
93c482a4
EM
987 /**
988 * per CRM-15746 check that the id can be altered in an update hook
989 */
28a04ea9 990 public function testMembershipUpdateCreateHookCRM15746() {
9099cab3 991 $this->hookClass->setHook('civicrm_pre', [$this, 'hook_civicrm_pre_update_create_membership']);
93c482a4 992 $result = $this->callAPISuccess('membership', 'create', $this->_params);
9099cab3
CW
993 $this->callAPISuccess('membership', 'create', ['id' => $result['id'], 'end_date' => '1 year ago']);
994 $this->callAPISuccessGetCount('membership', [], 2);
93c482a4 995 $this->hookClass->reset();
9099cab3
CW
996 $this->callAPISuccess('membership', 'create', ['id' => $result['id'], 'end_date' => '1 year ago']);
997 $this->callAPISuccessGetCount('membership', [], 2);
93c482a4
EM
998 }
999
2ea0abec
EM
1000 /**
1001 * Custom hook for update membership.
1002 *
1003 * @param string $op
1004 * @param object $objectName
1005 * @param int $id
1006 * @param array $params
1007 *
1008 * @throws \Exception
1009 */
28a04ea9 1010 public function hook_civicrm_pre_update_create_membership($op, $objectName, $id, &$params) {
93c482a4 1011 if ($objectName == 'Membership' && $op == 'edit') {
9099cab3 1012 $existingMembership = $this->callAPISuccessGetSingle('membership', ['id' => $params['id']]);
93c482a4 1013 unset($params['id'], $params['membership_id']);
6c6e6187 1014 $params['join_date'] = $params['membership_start_date'] = $params['start_date'] = date('Ymd000000', strtotime($existingMembership['start_date']));
93c482a4
EM
1015 $params = array_merge($existingMembership, $params);
1016 $params['id'] = NULL;
1017 }
1018 }
1019
6a488035 1020 /**
fe482240 1021 * Test civicrm_contact_memberships_create Invalid membership data.
6a488035
TO
1022 * Error expected.
1023 */
00be9182 1024 public function testMembershipCreateInvalidMemData() {
6a488035 1025 //membership_contact_id as string
9099cab3 1026 $params = [
6a488035
TO
1027 'membership_contact_id' => 'Invalid',
1028 'membership_type_id' => $this->_membershipTypeID,
1029 'join_date' => '2011-01-21',
1030 'start_date' => '2010-01-21',
1031 'end_date' => '2008-12-21',
1032 'source' => 'Payment',
1033 'is_override' => 1,
5896d037 1034 'status_id' => $this->_membershipStatusID,
9099cab3 1035 ];
6a488035 1036
d54576ed 1037 $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
1038
1039 //membership_contact_id which is no in contact table
1040 $params['membership_contact_id'] = 999;
d54576ed 1041 $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
1042
1043 //invalid join date
1044 unset($params['membership_contact_id']);
1045 $params['join_date'] = "invalid";
d54576ed 1046 $this->callAPIFailure('Membership', 'Create', $params);
6a488035
TO
1047 }
1048
1049 /**
1050 * Test civicrm_contact_memberships_create with membership_contact_id
1051 * membership).
1052 * Success expected.
1053 */
00be9182 1054 public function testMembershipCreateWithMemContact() {
9099cab3 1055 $params = [
6a488035
TO
1056 'membership_contact_id' => $this->_contactID,
1057 'membership_type_id' => $this->_membershipTypeID,
1058 'join_date' => '2011-01-21',
1059 'start_date' => '2010-01-21',
1060 'end_date' => '2008-12-21',
1061 'source' => 'Payment',
1062 'is_override' => 1,
1063 'status_id' => $this->_membershipStatusID,
9099cab3 1064 ];
6a488035 1065
771f3245 1066 $result = $this->callAPISuccess('membership', 'create', $params);
6a488035 1067
9099cab3 1068 $this->callAPISuccess('Membership', 'Delete', [
6a488035 1069 'id' => $result['id'],
9099cab3 1070 ]);
6a488035 1071 }
5896d037 1072
cc73900e 1073 /**
1074 * Test civicrm_contact_memberships_create with membership_contact_id
1075 * membership).
1076 * Success expected.
1077 */
00be9182 1078 public function testMembershipCreateValidMembershipTypeString() {
9099cab3 1079 $params = [
cc73900e 1080 'membership_contact_id' => $this->_contactID,
1081 'membership_type_id' => 'General',
1082 'join_date' => '2011-01-21',
1083 'start_date' => '2010-01-21',
1084 'end_date' => '2008-12-21',
1085 'source' => 'Payment',
1086 'is_override' => 1,
1087 'status_id' => $this->_membershipStatusID,
9099cab3 1088 ];
cc73900e 1089
1090 $result = $this->callAPISuccess('membership', 'create', $params);
1091 $this->assertEquals($this->_membershipTypeID, $result['values'][$result['id']]['membership_type_id']);
9099cab3 1092 $this->callAPISuccess('Membership', 'Delete', [
cc73900e 1093 'id' => $result['id'],
9099cab3 1094 ]);
cc73900e 1095 }
1096
1097 /**
1098 * Test civicrm_contact_memberships_create with membership_contact_id
1099 * membership).
1100 * Success expected.
1101 */
00be9182 1102 public function testMembershipCreateInValidMembershipTypeString() {
9099cab3 1103 $params = [
cc73900e 1104 'membership_contact_id' => $this->_contactID,
1105 'membership_type_id' => 'invalid',
1106 'join_date' => '2011-01-21',
1107 'start_date' => '2010-01-21',
1108 'end_date' => '2008-12-21',
1109 'source' => 'Payment',
1110 'is_override' => 1,
1111 'status_id' => $this->_membershipStatusID,
9099cab3 1112 ];
cc73900e 1113
d54576ed 1114 $this->callAPIFailure('membership', 'create', $params);
cc73900e 1115 }
6a488035 1116
cc73900e 1117 /**
eceb18cc 1118 * Test that if membership join date is not set it defaults to today.
cc73900e 1119 */
00be9182 1120 public function testEmptyJoinDate() {
8c33a68c 1121 unset($this->_params['join_date'], $this->_params['is_override']);
1122 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
9099cab3 1123 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
8c33a68c 1124 $this->assertEquals(date('Y-m-d', strtotime('now')), $result['join_date']);
1125 $this->assertEquals('2009-01-21', $result['start_date']);
1126 $this->assertEquals('2009-12-21', $result['end_date']);
cc73900e 1127 }
5896d037 1128
cc73900e 1129 /**
fe482240 1130 * Test that if membership start date is not set it defaults to correct end date.
8c33a68c 1131 * - fixed
cc73900e 1132 */
00be9182 1133 public function testEmptyStartDateFixed() {
8c33a68c 1134 unset($this->_params['start_date'], $this->_params['is_override']);
1135 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1136 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
9099cab3 1137 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
8c33a68c 1138 $this->assertEquals('2009-01-21', $result['join_date']);
1139 $this->assertEquals('2008-03-01', $result['start_date']);
1140 $this->assertEquals('2009-12-21', $result['end_date']);
1141 }
cc73900e 1142
8c33a68c 1143 /**
1144 * Test that if membership start date is not set it defaults to correct end date
1145 * - fixed
1146 */
2ea0abec
EM
1147 public function testEmptyStartEndDateFixedOneYear() {
1148 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
9099cab3 1149 $this->callAPISuccess('membership_type', 'create', ['id' => $this->_membershipTypeID2, 'duration_interval' => 1]);
2ea0abec
EM
1150 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1151 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
9099cab3 1152 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
2ea0abec
EM
1153 $this->assertEquals('2009-01-21', $result['join_date']);
1154 $this->assertEquals('2008-03-01', $result['start_date']);
1155 $this->assertEquals('2010-02-28', $result['end_date']);
1156 }
1157
1158 /**
9398f167
EM
1159 * Test that if membership start date is not set it defaults to correct end date for fixed multi year memberships.
1160 */
1161 public function testEmptyStartEndDateFixedMultiYear() {
1162 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
9099cab3 1163 $this->callAPISuccess('membership_type', 'create', ['id' => $this->_membershipTypeID2, 'duration_interval' => 5]);
9398f167
EM
1164 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1165 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
9099cab3 1166 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
9398f167
EM
1167 $this->assertEquals('2009-01-21', $result['join_date']);
1168 $this->assertEquals('2008-03-01', $result['start_date']);
1169 $this->assertEquals('2014-02-28', $result['end_date']);
1170 }
1171
41dcb974 1172 /**
1173 * CRM-18503 - Test membership join date is correctly set for fixed memberships.
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 ];
f57cb50c 1190 // @todo stop passing empty $ids
9099cab3 1191 $ids = [];
41dcb974 1192 $membership = CRM_Member_BAO_Membership::create($params, $ids);
1193
1194 // Update membership to 'Completed' and check the dates.
9099cab3 1195 $memParams = [
41dcb974 1196 'id' => $membership->id,
1197 'contact_id' => $contactId,
1198 'is_test' => 0,
1199 'membership_type_id' => $this->_membershipTypeID2,
1200 'num_terms' => 1,
1201 'status_id' => array_search('New', $memStatus),
9099cab3 1202 ];
41dcb974 1203 $result = $this->callAPISuccess('Membership', 'create', $memParams);
1204
70a87708 1205 // Extend duration interval if join_date exceeds the rollover period.
1206 $joinDate = date('Y-m-d');
1207 $year = date('Y');
1208 $startDate = date('Y-m-d', strtotime(date('Y-03-01')));
581eb285 1209 $rollOver = TRUE;
1210 if (strtotime($startDate) > time()) {
1211 $rollOver = FALSE;
1212 $startDate = date('Y-m-d', strtotime(date('Y-03-01') . '- 1 year'));
1213 }
70a87708 1214 $membershipTypeDetails = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($this->_membershipTypeID2);
1215 $fixedPeriodRollover = CRM_Member_BAO_MembershipType::isDuringFixedAnnualRolloverPeriod($joinDate, $membershipTypeDetails, $year, $startDate);
1216 $y = 1;
581eb285 1217 if ($fixedPeriodRollover && $rollOver) {
70a87708 1218 $y += 1;
1219 }
1220
9099cab3 1221 $expectedDates = [
41dcb974 1222 'join_date' => date('Ymd'),
70a87708 1223 'start_date' => str_replace('-', '', $startDate),
1224 'end_date' => date('Ymd', strtotime(date('Y-03-01') . "+ {$y} year - 1 day")),
9099cab3 1225 ];
41dcb974 1226 foreach ($result['values'] as $values) {
1227 foreach ($expectedDates as $date => $val) {
70a87708 1228 $this->assertEquals($val, $values[$date], "Failed asserting {$date} values");
41dcb974 1229 }
1230 }
1231 }
1232
b1fc74f0 1233 /**
964a9e96
EM
1234 * Test correct end and start dates are calculated for fixed multi year memberships.
1235 *
1236 * The empty start date is calculated to be the start_date (1 Jan prior to the join_date - so 1 Jan 15)
1237 *
1238 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1239 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1240 * and we add on 4 years rather than 5 because we are not after the rollover day - so we calculate 31 Dec 2019
1241 */
1242 public function testFixedMultiYearDateSetTwoEmptyStartEndDate() {
1243 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1244
9099cab3 1245 $this->callAPISuccess('membership_type', 'create', [
964a9e96
EM
1246 'id' => $this->_membershipTypeID2,
1247 'duration_interval' => 5,
1248 // Ie 1 Jan.
1249 'fixed_period_start_day' => '101',
1250 // Ie. 1 Nov.
1251 'fixed_period_rollover_day' => '1101',
9099cab3 1252 ]);
964a9e96 1253 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
9099cab3 1254 $dates = [
964a9e96 1255 'join_date' => '28-Jan 2015',
9099cab3 1256 ];
964a9e96 1257 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
9099cab3 1258 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
964a9e96
EM
1259 $this->assertEquals('2015-01-28', $result['join_date']);
1260 $this->assertEquals('2015-01-01', $result['start_date']);
1261 $this->assertEquals('2019-12-31', $result['end_date']);
1262 }
1263
1264 /**
1265 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
b1fc74f0
EM
1266 *
1267 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1268 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1269 * and we add on 4 years rather than 5 because we are not after the rollover day - so we calculate 31 Dec 2019
1270 */
964a9e96 1271 public function testFixedMultiYearDateSetTwoEmptyEndDate() {
b1fc74f0
EM
1272 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1273
9099cab3 1274 $this->callAPISuccess('membership_type', 'create', [
b1fc74f0
EM
1275 'id' => $this->_membershipTypeID2,
1276 'duration_interval' => 5,
1277 // Ie 1 Jan.
1278 'fixed_period_start_day' => '101',
1279 // Ie. 1 Nov.
1280 'fixed_period_rollover_day' => '1101',
9099cab3 1281 ]);
b1fc74f0 1282 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
9099cab3 1283 $dates = [
b1fc74f0
EM
1284 'start_date' => '28-Jan 2015',
1285 'join_date' => '28-Jan 2015',
9099cab3 1286 ];
b1fc74f0 1287 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
9099cab3 1288 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
b1fc74f0
EM
1289 $this->assertEquals('2015-01-28', $result['join_date']);
1290 $this->assertEquals('2015-01-28', $result['start_date']);
1291 $this->assertEquals('2019-12-31', $result['end_date']);
1292 }
1293
1294 /**
964a9e96
EM
1295 * Test correct end and start dates are calculated for fixed multi year memberships.
1296 *
1297 * The empty start date is calculated to be the start_date (1 Jan prior to the join_date - so 1 Jan 15)
1298 *
1299 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1300 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1301 * and we add on <1 years rather than > 1 because we are not after the rollover day - so we calculate 31 Dec 2015
1302 */
1303 public function testFixedSingleYearDateSetTwoEmptyStartEndDate() {
1304 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1305
9099cab3 1306 $this->callAPISuccess('membership_type', 'create', [
964a9e96
EM
1307 'id' => $this->_membershipTypeID2,
1308 'duration_interval' => 1,
1309 // Ie 1 Jan.
1310 'fixed_period_start_day' => '101',
1311 // Ie. 1 Nov.
1312 'fixed_period_rollover_day' => '1101',
9099cab3 1313 ]);
964a9e96 1314 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
9099cab3 1315 $dates = [
964a9e96 1316 'join_date' => '28-Jan 2015',
9099cab3 1317 ];
964a9e96 1318 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
9099cab3 1319 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
964a9e96
EM
1320 $this->assertEquals('2015-01-28', $result['join_date']);
1321 $this->assertEquals('2015-01-01', $result['start_date']);
1322 $this->assertEquals('2015-12-31', $result['end_date']);
1323 }
1324
1325 /**
1326 * Test correct end date for fixed single year memberships is calculated and start_date is not changed.
b1fc74f0
EM
1327 *
1328 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1329 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1330 * and we add on <1 years rather than > 1 because we are not after the rollover day - so we calculate 31 Dec 2015
1331 */
964a9e96 1332 public function testFixedSingleYearDateSetTwoEmptyEndDate() {
b1fc74f0
EM
1333 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1334
9099cab3 1335 $this->callAPISuccess('membership_type', 'create', [
b1fc74f0
EM
1336 'id' => $this->_membershipTypeID2,
1337 'duration_interval' => 1,
1338 // Ie 1 Jan.
1339 'fixed_period_start_day' => '101',
1340 // Ie. 1 Nov.
1341 'fixed_period_rollover_day' => '1101',
9099cab3 1342 ]);
b1fc74f0 1343 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
9099cab3 1344 $dates = [
b1fc74f0
EM
1345 'start_date' => '28-Jan 2015',
1346 'join_date' => '28-Jan 2015',
9099cab3 1347 ];
b1fc74f0 1348 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
9099cab3 1349 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
b1fc74f0
EM
1350 $this->assertEquals('2015-01-28', $result['join_date']);
1351 $this->assertEquals('2015-01-28', $result['start_date']);
1352 $this->assertEquals('2015-12-31', $result['end_date']);
1353 }
1354
1355 /**
964a9e96 1356 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
b1fc74f0
EM
1357 *
1358 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1359 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
964a9e96 1360 * and we add on 1 year we are after the rollover day - so we calculate 31 Oct 2016
b1fc74f0 1361 */
964a9e96 1362 public function testFixedSingleYearDateSetThreeEmptyEndDate() {
b1fc74f0
EM
1363 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1364
9099cab3 1365 $this->callAPISuccess('membership_type', 'create', [
b1fc74f0
EM
1366 'id' => $this->_membershipTypeID2,
1367 'duration_interval' => 1,
1368 // Ie. 1 Nov.
1369 'fixed_period_start_day' => '1101',
1370 // Ie 1 Jan.
1371 'fixed_period_rollover_day' => '101',
9099cab3 1372 ]);
b1fc74f0 1373 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
9099cab3 1374 $dates = [
b1fc74f0
EM
1375 'start_date' => '28-Jan 2015',
1376 'join_date' => '28-Jan 2015',
9099cab3 1377 ];
b1fc74f0 1378 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
9099cab3 1379 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
b1fc74f0
EM
1380 $this->assertEquals('2015-01-28', $result['join_date']);
1381 $this->assertEquals('2015-01-28', $result['start_date']);
1382 $this->assertEquals('2016-10-31', $result['end_date']);
1383 }
1384
964a9e96
EM
1385 /**
1386 * Test correct end and start dates are calculated for fixed multi year memberships.
1387 *
d177a2a6 1388 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
964a9e96
EM
1389 *
1390 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1391 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1392 * and we add on 1 year we are after the rollover day - so we calculate 31 Oct 2016
1393 */
1394 public function testFixedSingleYearDateSetThreeEmptyStartEndDate() {
1395 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1396
9099cab3 1397 $this->callAPISuccess('membership_type', 'create', [
964a9e96
EM
1398 'id' => $this->_membershipTypeID2,
1399 'duration_interval' => 1,
1400 // Ie. 1 Nov.
1401 'fixed_period_start_day' => '1101',
1402 // Ie 1 Jan.
1403 'fixed_period_rollover_day' => '101',
9099cab3 1404 ]);
964a9e96 1405 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
9099cab3 1406 $dates = [
964a9e96 1407 'join_date' => '28-Jan 2015',
9099cab3 1408 ];
964a9e96 1409 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
9099cab3 1410 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
964a9e96
EM
1411 $this->assertEquals('2015-01-28', $result['join_date']);
1412 $this->assertEquals('2014-11-01', $result['start_date']);
1413 $this->assertEquals('2016-10-31', $result['end_date']);
1414 }
b1fc74f0
EM
1415
1416 /**
964a9e96 1417 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
b1fc74f0
EM
1418 *
1419 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1420 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1421 * and we add on 5 years we are after the rollover day - so we calculate 31 Oct 2020
1422 */
964a9e96 1423 public function testFixedMultiYearDateSetThreeEmptyEndDate() {
b1fc74f0
EM
1424 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1425
9099cab3 1426 $this->callAPISuccess('membership_type', 'create', [
b1fc74f0
EM
1427 'id' => $this->_membershipTypeID2,
1428 'duration_interval' => 5,
1429 // Ie. 1 Nov.
1430 'fixed_period_start_day' => '1101',
1431 // Ie 1 Jan.
1432 'fixed_period_rollover_day' => '101',
9099cab3 1433 ]);
b1fc74f0 1434 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
9099cab3 1435 $dates = [
b1fc74f0
EM
1436 'start_date' => '28-Jan 2015',
1437 'join_date' => '28-Jan 2015',
9099cab3 1438 ];
b1fc74f0 1439 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
9099cab3 1440 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
b1fc74f0
EM
1441 $this->assertEquals('2015-01-28', $result['join_date']);
1442 $this->assertEquals('2015-01-28', $result['start_date']);
1443 $this->assertEquals('2020-10-31', $result['end_date']);
1444 }
9398f167 1445
964a9e96
EM
1446 /**
1447 * Test correct end and start dates are calculated for fixed multi year memberships.
1448 *
d177a2a6 1449 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
964a9e96
EM
1450 *
1451 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
1452 * In this set our join date is after the start day and after the rollover day so we do get an extra year
1453 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1454 * and we add on 5 years we are after the rollover day - so we calculate 31 Oct 2020
1455 */
1456 public function testFixedMultiYearDateSetThreeEmptyStartEndDate() {
1457 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1458
9099cab3 1459 $this->callAPISuccess('membership_type', 'create', [
964a9e96
EM
1460 'id' => $this->_membershipTypeID2,
1461 'duration_interval' => 5,
1462 // Ie. 1 Nov.
1463 'fixed_period_start_day' => '1101',
1464 // Ie 1 Jan.
1465 'fixed_period_rollover_day' => '101',
9099cab3 1466 ]);
964a9e96 1467 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
9099cab3 1468 $dates = [
964a9e96 1469 'join_date' => '28-Jan 2015',
9099cab3 1470 ];
964a9e96 1471 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
9099cab3 1472 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
964a9e96
EM
1473 $this->assertEquals('2015-01-28', $result['join_date']);
1474 $this->assertEquals('2014-11-01', $result['start_date']);
1475 $this->assertEquals('2020-10-31', $result['end_date']);
1476 }
1477
9398f167
EM
1478 /**
1479 * Test that if membership start date is not set it defaults to correct end date for fixed single year memberships.
2ea0abec 1480 */
00be9182 1481 public function testEmptyStartDateRolling() {
8c33a68c 1482 unset($this->_params['start_date'], $this->_params['is_override']);
1483 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
9099cab3 1484 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
8c33a68c 1485 $this->assertEquals('2009-01-21', $result['join_date']);
1486 $this->assertEquals('2009-01-21', $result['start_date']);
1487 $this->assertEquals('2009-12-21', $result['end_date']);
cc73900e 1488 }
5896d037 1489
8c33a68c 1490 /**
eceb18cc 1491 * Test that if membership end date is not set it defaults to correct end date.
8c33a68c 1492 * - rolling
1493 */
00be9182 1494 public function testEmptyEndDateFixed() {
8c33a68c 1495 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1496 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1497 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
9099cab3 1498 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
8c33a68c 1499 $this->assertEquals('2009-01-21', $result['join_date']);
1500 $this->assertEquals('2008-03-01', $result['start_date']);
1501 $this->assertEquals('2010-02-28', $result['end_date']);
1502 }
5896d037 1503
8c33a68c 1504 /**
eceb18cc 1505 * Test that if membership end date is not set it defaults to correct end date.
8c33a68c 1506 * - rolling
1507 */
00be9182 1508 public function testEmptyEndDateRolling() {
8c33a68c 1509 unset($this->_params['is_override'], $this->_params['end_date']);
1510 $this->_params['membership_type_id'] = $this->_membershipTypeID;
1511 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
9099cab3 1512 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
8c33a68c 1513 $this->assertEquals('2009-01-21', $result['join_date']);
1514 $this->assertEquals('2009-01-21', $result['start_date']);
1515 $this->assertEquals('2010-01-20', $result['end_date']);
1516 }
1517
8c33a68c 1518 /**
452b9e04 1519 * Test that if dates are set they not over-ridden if id is passed in
8c33a68c 1520 */
6c6e6187 1521 public function testMembershipDatesNotOverridden() {
8c33a68c 1522 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1523 unset($this->_params['end_date'], $this->_params['start_date']);
1524 $this->_params['id'] = $result['id'];
1525 $this->callAPISuccess($this->_entity, 'create', $this->_params);
9099cab3 1526 $result = $this->callAPISuccess($this->_entity, 'getsingle', ['id' => $result['id']]);
8c33a68c 1527 $this->assertEquals('2009-01-21', $result['join_date']);
1528 $this->assertEquals('2009-01-21', $result['start_date']);
1529 $this->assertEquals('2009-12-21', $result['end_date']);
1530
6c6e6187 1531 }
96025800 1532
8b3df6dc 1533 /**
1534 * Test that all membership types are returned when getoptions is called.
1535 *
1536 * This test locks in current behaviour where types for all domains are returned. It should possibly be domain
1537 * specific but that should only be done in conjunction with adding a hook to allow that to be altered as the
1538 * multisite use case expects the master domain to be able to see all sites.
1539 *
1540 * See CRM-17075.
1541 */
1542 public function testGetOptionsMembershipTypeID() {
9099cab3 1543 $options = $this->callAPISuccess('Membership', 'getoptions', ['field' => 'membership_type_id']);
5d8b37be 1544 $this->assertEquals('Another one', array_pop($options['values']));
8b3df6dc 1545 $this->assertEquals('General', array_pop($options['values']));
1546 $this->assertEquals(NULL, array_pop($options['values']));
1547 }
1548
6a488035 1549}