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