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