Merge pull request #12178 from jitendrapurohit/membership-4
[civicrm-core.git] / tests / phpunit / api / v3 / MembershipTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Test APIv3 civicrm_membership functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Member
33 */
34
35 /**
36 * Class api_v3_MembershipTest
37 * @group headless
38 */
39 class api_v3_MembershipTest extends CiviUnitTestCase {
40 protected $_apiversion;
41 protected $_contactID;
42 protected $_membershipID;
43 protected $_membershipID2;
44 protected $_membershipID3;
45 protected $_membershipTypeID;
46 protected $_membershipTypeID2;
47 protected $_membershipStatusID;
48 protected $_entity;
49 protected $_params;
50
51 /**
52 * Set up for tests.
53 */
54 public function setUp() {
55 parent::setUp();
56 $this->_apiversion = 3;
57 $this->_contactID = $this->individualCreate();
58 $this->_membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID));
59 $this->_membershipTypeID2 = $this->membershipTypeCreate(array(
60 'period_type' => 'fixed',
61 // Ie. 1 March.
62 'fixed_period_start_day' => '301',
63 // Ie. 11 Nov.
64 'fixed_period_rollover_day' => '1111',
65 'name' => 'Another one',
66 ));
67 $this->_membershipStatusID = $this->membershipStatusCreate('test status');
68
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,
83 );
84 }
85
86 /**
87 * Clean up after tests.
88 *
89 * @throws \Exception
90 */
91 public function tearDown() {
92 $this->quickCleanup(array(
93 'civicrm_membership',
94 'civicrm_membership_payment',
95 'civicrm_membership_log',
96 'civicrm_uf_match',
97 ),
98 TRUE
99 );
100 $this->membershipStatusDelete($this->_membershipStatusID);
101 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID2));
102 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID));
103 $this->contactDelete($this->_contactID);
104
105 }
106
107 /**
108 * Test membership deletion.
109 */
110 public function testMembershipDelete() {
111 $membershipID = $this->contactMembershipCreate($this->_params);
112 $this->assertDBRowExist('CRM_Member_DAO_Membership', $membershipID);
113 $params = array(
114 'id' => $membershipID,
115 );
116 $this->callAPIAndDocument('membership', 'delete', $params, __FUNCTION__, __FILE__);
117 $this->assertDBRowNotExist('CRM_Member_DAO_Membership', $membershipID);
118 }
119
120 public function testMembershipDeleteEmpty() {
121 $this->callAPIFailure('membership', 'delete', array());
122 }
123
124 public function testMembershipDeleteInvalidID() {
125 $this->callAPIFailure('membership', 'delete', array('id' => 'blah'));
126 }
127
128 /**
129 * Test civicrm_membership_delete() with invalid Membership Id.
130 */
131 public function testMembershipDeleteWithInvalidMembershipId() {
132 $membershipId = 'membership';
133 $this->callAPIFailure('membership', 'delete', $membershipId);
134 }
135
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
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,
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
206 /**
207 * Test membership get.
208 */
209 public function testContactMembershipsGet() {
210 $this->_membershipID = $this->contactMembershipCreate($this->_params);
211 $this->callAPISuccess('membership', 'get', array());
212 $this->callAPISuccess('Membership', 'Delete', array('id' => $this->_membershipID));
213 }
214
215 /**
216 * Test civicrm_membership_get with params not array.
217 *
218 * Gets treated as contact_id, memberships expected.
219 */
220 public function testGetWithParamsContactId() {
221 $this->_membershipID = $this->contactMembershipCreate($this->_params);
222 $params = array(
223 'contact_id' => $this->_contactID,
224 );
225 $membership = $this->callAPISuccess('membership', 'get', $params);
226
227 $result = $membership['values'][$this->_membershipID];
228 $this->callAPISuccess('Membership', 'Delete', array(
229 'id' => $this->_membershipID,
230 ));
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
241 /**
242 * Test civicrm_membership_get with params not array.
243 *
244 * Gets treated as contact_id, memberships expected.
245 */
246 public function testGetInSyntax() {
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']));
262 }
263
264 /**
265 * Test civicrm_membership_get with params not array.
266 * Gets treated as contact_id, memberships expected.
267 */
268 public function testGetInSyntaxOnContactID() {
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 }
287
288 /**
289 * Test civicrm_membership_get with params not array.
290 *
291 * Gets treated as contact_id, memberships expected.
292 */
293 public function testGetWithParamsMemberShipTypeId() {
294 $this->callAPISuccess($this->_entity, 'create', $this->_params);
295 $params = array(
296 'membership_type_id' => $this->_membershipTypeID,
297 );
298 $membership = $this->callAPISuccess('membership', 'get', $params);
299 $this->callAPISuccess('Membership', 'Delete', array(
300 'id' => $membership['id'],
301 ));
302 $result = $membership['values'][$membership['id']];
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);
311 $this->assertEquals($result['id'], $membership['id']);
312 }
313
314 /**
315 * Test civicrm_membership_get with params not array.
316 * Gets treated as contact_id, memberships expected.
317 */
318 public function testGetWithParamsMemberShipTypeIdContactID() {
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);
331
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 }
340
341 /**
342 * Check with complete array + custom field.
343 *
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 */
348 public function testGetWithParamsMemberShipIdAndCustom() {
349 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
350
351 $params = $this->_params;
352 $params['custom_' . $ids['custom_field_id']] = "custom string";
353
354 $result = $this->callAPISuccess($this->_entity, 'create', $params);
355
356 $getParams = array('membership_type_id' => $params['membership_type_id']);
357 $check = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
358 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
359
360 $this->callAPISuccess('Membership', 'Delete', array(
361 'id' => $result['id'],
362 ));
363 }
364
365 /**
366 * Test civicrm_membership_get with proper params.
367 * Memberships expected.
368 */
369 public function testGet() {
370 $membershipID = $this->contactMembershipCreate($this->_params);
371 $params = array(
372 'contact_id' => $this->_contactID,
373 );
374
375 $membership = $this->callAPISuccess('membership', 'get', $params);
376 $result = $membership['values'][$membershipID];
377 $this->callAPISuccess('Membership', 'Delete', array(
378 'id' => $membership['id'],
379 ));
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);
384
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);
389 }
390
391
392 /**
393 * Test civicrm_membership_get with proper params.
394 * Memberships expected.
395 */
396 public function testGetWithId() {
397 $membershipID = $this->contactMembershipCreate($this->_params);
398 $params = array(
399 'contact_id' => $this->_contactID,
400 'id' => $this->_membershipID,
401 'return' => 'id',
402 );
403 $result = $this->callAPISuccess('membership', 'get', $params);
404 $this->assertEquals($membershipID, $result['id']);
405 $params = array(
406 'contact_id' => $this->_contactID,
407 'membership_id' => $this->_membershipID,
408 'return' => 'membership_id',
409 );
410 $result = $this->callAPISuccess('membership', 'get', $params);
411 $this->assertEquals($membershipID, $result['id']);
412 }
413
414 /**
415 * Test civicrm_membership_get for only active.
416 * Memberships expected.
417 */
418 public function testGetOnlyActive() {
419 $description = "Demonstrates use of 'filter' active_only' param.";
420 $this->_membershipID = $this->contactMembershipCreate($this->_params);
421 $params = array(
422 'contact_id' => $this->_contactID,
423 'active_only' => 1,
424 );
425
426 $membership = $this->callAPISuccess('membership', 'get', $params);
427 $this->assertEquals($membership['values'][$this->_membershipID]['status_id'], $this->_membershipStatusID);
428 $this->assertEquals($membership['values'][$this->_membershipID]['contact_id'], $this->_contactID);
429 $params = array(
430 'contact_id' => $this->_contactID,
431 'filters' => array(
432 'is_current' => 1,
433 ),
434 );
435
436 $membership = $this->callAPIAndDocument('membership', 'get', $params, __FUNCTION__, __FILE__, $description, 'FilterIsCurrent');
437 $this->assertEquals($membership['values'][$this->_membershipID]['status_id'], $this->_membershipStatusID);
438 $this->assertEquals($membership['values'][$this->_membershipID]['contact_id'], $this->_contactID);
439
440 $this->callAPISuccess('Membership', 'Delete', array('id' => $this->_membershipID));
441 }
442
443 /**
444 * Test civicrm_membership_get for non exist contact.
445 * empty Memberships.
446 */
447 public function testGetNoContactExists() {
448 $params = array(
449 'contact_id' => 55555,
450 );
451
452 $membership = $this->callAPISuccess('membership', 'get', $params);
453 $this->assertEquals($membership['count'], 0);
454 }
455
456 /**
457 * Test civicrm_membership_get with relationship.
458 * get Memberships.
459 */
460 public function testGetWithRelationship() {
461 $membershipOrgId = $this->organizationCreate(NULL);
462 $memberContactId = $this->individualCreate();
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,
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,
482 'financial_type_id' => 1,
483 'relationship_type_id' => $relTypeID,
484 'relationship_direction' => 'b_a',
485 'is_active' => 1,
486 );
487 $memType = $this->callAPISuccess('membership_type', 'create', $params);
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,
498 );
499 $membershipID = $this->contactMembershipCreate($params);
500
501 $params = array(
502 'contact_id' => $memberContactId,
503 'membership_type_id' => $memType['id'],
504 );
505
506 $result = $this->callAPISuccess('membership', 'get', $params);
507
508 $membership = $result['values'][$membershipID];
509 $this->assertEquals($this->_membershipStatusID, $membership['status_id']);
510 $this->callAPISuccess('Membership', 'Delete', array(
511 'id' => $membership['id'],
512 ));
513 $this->membershipTypeDelete(array('id' => $memType['id']));
514 $this->relationshipTypeDelete($relTypeID);
515 $this->contactDelete($membershipOrgId);
516 $this->contactDelete($memberContactId);
517 }
518
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 */
526 public function testCreateWithRelationship() {
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);
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
591 $memberContactId[2] = $this->individualCreate(array('employer_id' => $employerId[0]), 2);
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 );
606 $this->callAPISuccess('Membership', 'create', $params);
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
634 //Create pay_later membership for organization.
635 $employerId[2] = $this->organizationCreate(array(), 1);
636 $params = array(
637 'contact_id' => $employerId[2],
638 'membership_type_id' => $membershipTypeId,
639 'source' => 'Test pay later suite',
640 'is_pay_later' => 1,
641 'status_id' => 5,
642 );
643 $organizationMembership = CRM_Member_BAO_Membership::add($params);
644 $organizationMembershipID = $organizationMembership->id;
645 $memberContactId[3] = $this->individualCreate(array('employer_id' => $employerId[2]), 0);
646 // Check that the employee inherited the membership
647 $params = array(
648 'contact_id' => $memberContactId[3],
649 'membership_type_id' => $membershipTypeId,
650 );
651 $result = $this->callAPISuccess('membership', 'get', $params);
652 $this->assertEquals(1, $result['count']);
653 $result = $result['values'][$result['id']];
654 $this->assertEquals($organizationMembershipID, $result['owner_membership_id']);
655
656 // Set up params for enable/disable checks
657 $relationship1 = $this->callAPISuccess('relationship', 'get', array('contact_id_a' => $memberContactId[1]));
658 $params = array(
659 'contact_id' => $memberContactId[1],
660 'membership_type_id' => $membershipTypeId,
661 );
662
663 // Deactivate relationship using create and assert membership is not inherited
664 $this->callAPISuccess('relationship', 'create', array('id' => $relationship1['id'], 'is_active' => 0));
665 $result = $this->callAPISuccess('membership', 'get', $params);
666 $this->assertEquals(0, $result['count']);
667
668 // Re-enable relationship using create and assert membership is inherited
669 $this->callAPISuccess('relationship', 'create', array('id' => $relationship1['id'], 'is_active' => 1));
670 $result = $this->callAPISuccess('membership', 'get', $params);
671 $this->assertEquals(1, $result['count']);
672
673 // Deactivate relationship using setvalue and assert membership is not inherited
674 $this->callAPISuccess('relationship', 'setvalue', array('id' => $relationship1['id'], 'field' => 'is_active', 'value' => 0));
675 $result = $this->callAPISuccess('membership', 'get', $params);
676 $this->assertEquals(0, $result['count']);
677
678 // Re-enable relationship using setvalue and assert membership is inherited
679 $this->callAPISuccess('relationship', 'setvalue', array('id' => $relationship1['id'], 'field' => 'is_active', 'value' => 1));
680 $result = $this->callAPISuccess('membership', 'get', $params);
681 $this->assertEquals(1, $result['count']);
682
683 // Delete relationship and assert membership is not inherited
684 $this->callAPISuccess('relationship', 'delete', array('id' => $relationship1['id']));
685 $result = $this->callAPISuccess('membership', 'get', $params);
686 $this->assertEquals(0, $result['count']);
687
688 // Tear down - reverse of creation to be safe
689 $this->contactDelete($memberContactId[2]);
690 $this->contactDelete($memberContactId[1]);
691 $this->contactDelete($memberContactId[0]);
692 $this->contactDelete($employerId[1]);
693 $this->contactDelete($employerId[0]);
694 $this->membershipTypeDelete(array('id' => $membershipTypeId));
695 $this->contactDelete($membershipOrgId);
696 }
697
698 /**
699 * We are checking for no e-notices + only id & end_date returned
700 */
701 public function testMembershipGetWithReturn() {
702 $this->contactMembershipCreate($this->_params);
703 $result = $this->callAPISuccess('membership', 'get', array('return' => 'end_date'));
704 foreach ($result['values'] as $membership) {
705 $this->assertEquals(array('id', 'end_date'), array_keys($membership));
706 }
707 }
708 ///////////////// civicrm_membership_create methods
709
710 /**
711 * Test civicrm_contact_memberships_create with empty params.
712 * Error expected.
713 */
714 public function testCreateWithEmptyParams() {
715 $params = array();
716 $this->callAPIFailure('membership', 'create', $params);
717 }
718
719 /**
720 * If is_overide is passed in status must also be passed in.
721 */
722 public function testCreateOverrideNoStatus() {
723 $params = $this->_params;
724 unset($params['status_id']);
725 $this->callAPIFailure('membership', 'create', $params);
726 }
727
728 public function testMembershipCreateMissingRequired() {
729 $params = array(
730 'membership_type_id' => '1',
731 'join_date' => '2006-01-21',
732 'start_date' => '2006-01-21',
733 'end_date' => '2006-12-21',
734 'source' => 'Payment',
735 'status_id' => '2',
736 );
737
738 $this->callAPIFailure('membership', 'create', $params);
739 }
740
741 public function testMembershipCreate() {
742 $params = array(
743 'contact_id' => $this->_contactID,
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,
751 );
752
753 $result = $this->callAPIAndDocument('membership', 'create', $params, __FUNCTION__, __FILE__);
754 $this->getAndCheck($params, $result['id'], $this->_entity);
755 $this->assertNotNull($result['id']);
756 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['contact_id'], " in line " . __LINE__);
757 $this->assertEquals($result['id'], $result['values'][$result['id']]['id'], " in line " . __LINE__);
758 }
759
760 /**
761 * Check for useful message if contact doesn't exist
762 */
763 public function testMembershipCreateWithInvalidContact() {
764 $params = array(
765 'contact_id' => 999,
766 'membership_type_id' => $this->_membershipTypeID,
767 'join_date' => '2006-01-21',
768 'start_date' => '2006-01-21',
769 'end_date' => '2006-12-21',
770 'source' => 'Payment',
771 'is_override' => 1,
772 'status_id' => $this->_membershipStatusID,
773 );
774
775 $this->callAPIFailure('membership', 'create', $params,
776 'contact_id is not valid : 999'
777 );
778 }
779
780 public function testMembershipCreateWithInvalidStatus() {
781 $params = $this->_params;
782 $params['status_id'] = 999;
783 $this->callAPIFailure('membership', 'create', $params,
784 "'999' is not a valid option for field status_id"
785 );
786 }
787
788 public function testMembershipCreateWithInvalidType() {
789 $params = $this->_params;
790 $params['membership_type_id'] = 999;
791
792 $this->callAPIFailure('membership', 'create', $params,
793 "'999' is not a valid option for field membership_type_id"
794 );
795 }
796
797 /**
798 * Check with complete array + custom field
799 * Note that the test is written on purpose without any
800 * variables specific to participant so it can be replicated into other entities
801 * and / or moved to the automated test suite
802 */
803 public function testCreateWithCustom() {
804 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
805
806 $params = $this->_params;
807 $params['custom_' . $ids['custom_field_id']] = "custom string";
808
809 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, NULL, 'CreateWithCustomData');
810 $check = $this->callAPISuccess($this->_entity, 'get', array(
811 'id' => $result['id'],
812 'contact_id' => $this->_contactID,
813 ));
814 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
815 }
816
817 /**
818 * Search on custom field value.
819 */
820 public function testSearchWithCustomDataCRM16036() {
821 // Create a custom field on membership
822 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
823
824 // Create a new membership, but don't assign anything to the custom field.
825 $params = $this->_params;
826 $result = $this->callAPIAndDocument(
827 $this->_entity,
828 'create',
829 $params,
830 __FUNCTION__,
831 __FILE__,
832 NULL,
833 'SearchWithCustomData');
834
835 // search memberships with CRM-16036 as custom field value.
836 // Since we did not touch the custom field of any membership,
837 // this should not return any results.
838 $check = $this->callAPISuccess($this->_entity, 'get', array(
839 'custom_' . $ids['custom_field_id'] => "CRM-16036",
840 ));
841
842 // Cleanup.
843 $this->callAPISuccess($this->_entity, 'delete', array(
844 'id' => $result['id'],
845 ));
846
847 // Assert.
848 $this->assertEquals(0, $check['count']);
849 }
850
851 /**
852 * Test civicrm_contact_memberships_create with membership id (edit
853 * membership).
854 * success expected.
855 */
856 public function testMembershipCreateWithId() {
857 $membershipID = $this->contactMembershipCreate($this->_params);
858 $params = array(
859 'id' => $membershipID,
860 'contact_id' => $this->_contactID,
861 'membership_type_id' => $this->_membershipTypeID,
862 'join_date' => '2006-01-21',
863 'start_date' => '2006-01-21',
864 'end_date' => '2006-12-21',
865 'source' => 'Payment',
866 'is_override' => 1,
867 'status_id' => $this->_membershipStatusID,
868 );
869
870 $result = $this->callAPISuccess('membership', 'create', $params);
871
872 //Update Status and check activities created.
873 $updateStatus = array(
874 'id' => $result['id'],
875 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'Cancelled'),
876 );
877 $this->callAPISuccess('Membership', 'create', $updateStatus);
878 $activities = CRM_Activity_BAO_Activity::getContactActivity($this->_contactID);
879 $this->assertEquals(2, count($activities));
880 $activityNames = array_flip(CRM_Utils_Array::collect('activity_name', $activities));
881 $this->assertArrayHasKey('Membership Signup', $activityNames);
882 $this->assertArrayHasKey('Change Membership Status', $activityNames);
883
884 $this->callAPISuccess('Membership', 'Delete', array(
885 'id' => $result['id'],
886 ));
887 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
888 }
889
890 /**
891 * Test civicrm_contact_memberships_create with membership id (edit
892 * membership).
893 * success expected.
894 */
895 public function testMembershipCreateUpdateWithIdNoContact() {
896 $membershipID = $this->contactMembershipCreate($this->_params);
897 $params = array(
898 'id' => $membershipID,
899 'membership_type_id' => $this->_membershipTypeID,
900 'contact_id' => $this->_contactID,
901 'join_date' => '2006-01-21',
902 'start_date' => '2006-01-21',
903 'end_date' => '2006-12-21',
904 'source' => 'Payment',
905 'is_override' => 1,
906 'status_id' => $this->_membershipStatusID,
907 );
908
909 $result = $this->callAPISuccess('membership', 'create', $params);
910 $this->callAPISuccess('Membership', 'Delete', array(
911 'id' => $result['id'],
912 ));
913
914 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
915 }
916
917 /**
918 * Test civicrm_contact_memberships_create with membership id (edit
919 * membership).
920 * success expected.
921 */
922 public function testMembershipCreateUpdateWithIdNoDates() {
923 $membershipID = $this->contactMembershipCreate($this->_params);
924 $params = array(
925 'id' => $membershipID,
926 'contact_id' => $this->_contactID,
927 'membership_type_id' => $this->_membershipTypeID,
928 'source' => 'Payment',
929 'is_override' => 1,
930 'status_id' => $this->_membershipStatusID,
931 );
932
933 $result = $this->callAPISuccess('membership', 'create', $params);
934 $this->callAPISuccess('Membership', 'Delete', array(
935 'id' => $result['id'],
936 ));
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 */
945 public function testMembershipCreateUpdateWithIdNoDatesNoType() {
946 $membershipID = $this->contactMembershipCreate($this->_params);
947 $params = array(
948 'id' => $membershipID,
949 'source' => 'not much here',
950 'contact_id' => $this->_contactID,
951 'is_override' => 1,
952 'status_id' => $this->_membershipStatusID,
953 );
954
955 $result = $this->callAPISuccess('membership', 'create', $params);
956 $this->callAPISuccess('Membership', 'Delete', array(
957 'id' => $result['id'],
958 ));
959 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
960 }
961
962 /**
963 * Test civicrm_contact_memberships_create with membership id (edit
964 * membership).
965 * success expected.
966 */
967 public function testMembershipCreateUpdateWithIDAndSource() {
968 $membershipID = $this->contactMembershipCreate($this->_params);
969 $params = array(
970 'id' => $membershipID,
971 'source' => 'changed',
972 'contact_id' => $this->_contactID,
973 'status_id' => $this->_membershipStatusID,
974 'membership_type_id' => $this->_membershipTypeID,
975 'skipStatusCal' => 1,
976 );
977 $result = $this->callAPISuccess('membership', 'create', $params);
978 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
979 $this->callAPISuccess('Membership', 'Delete', array(
980 'id' => $result['id'],
981 ));
982 }
983
984 /**
985 * Change custom field using update.
986 */
987 public function testUpdateWithCustom() {
988 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
989
990 $params = $this->_params;
991 $params['custom_' . $ids['custom_field_id']] = "custom string";
992 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, NULL, 'UpdateCustomData');
993 $result = $this->callAPISuccess($this->_entity, 'create', array(
994 'id' => $result['id'],
995 'custom_' . $ids['custom_field_id'] => "new custom",
996 ));
997 $check = $this->callAPISuccess($this->_entity, 'get', array(
998 'id' => $result['id'],
999 'contact_id' => $this->_contactID,
1000 ));
1001
1002 $this->assertEquals("new custom", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
1003 $this->callAPISuccess('Membership', 'Delete', array(
1004 'id' => $check['id'],
1005 ));
1006
1007 $this->customFieldDelete($ids['custom_field_id']);
1008 $this->customGroupDelete($ids['custom_group_id']);
1009 }
1010
1011 /**
1012 * per CRM-15746 check that the id can be altered in an update hook
1013 */
1014 public function testMembershipUpdateCreateHookCRM15746() {
1015 $this->hookClass->setHook('civicrm_pre', array($this, 'hook_civicrm_pre_update_create_membership'));
1016 $result = $this->callAPISuccess('membership', 'create', $this->_params);
1017 $this->callAPISuccess('membership', 'create', array('id' => $result['id'], 'end_date' => '1 year ago'));
1018 $this->callAPISuccessGetCount('membership', array(), 2);
1019 $this->hookClass->reset();
1020 $this->callAPISuccess('membership', 'create', array('id' => $result['id'], 'end_date' => '1 year ago'));
1021 $this->callAPISuccessGetCount('membership', array(), 2);
1022 }
1023
1024 /**
1025 * Custom hook for update membership.
1026 *
1027 * @param string $op
1028 * @param object $objectName
1029 * @param int $id
1030 * @param array $params
1031 *
1032 * @throws \Exception
1033 */
1034 public function hook_civicrm_pre_update_create_membership($op, $objectName, $id, &$params) {
1035 if ($objectName == 'Membership' && $op == 'edit') {
1036 $existingMembership = $this->callAPISuccessGetSingle('membership', array('id' => $params['id']));
1037 unset($params['id'], $params['membership_id']);
1038 $params['join_date'] = $params['membership_start_date'] = $params['start_date'] = date('Ymd000000', strtotime($existingMembership['start_date']));
1039 $params = array_merge($existingMembership, $params);
1040 $params['id'] = NULL;
1041 }
1042 }
1043
1044 /**
1045 * Test civicrm_contact_memberships_create Invalid membership data.
1046 * Error expected.
1047 */
1048 public function testMembershipCreateInvalidMemData() {
1049 //membership_contact_id as string
1050 $params = array(
1051 'membership_contact_id' => 'Invalid',
1052 'membership_type_id' => $this->_membershipTypeID,
1053 'join_date' => '2011-01-21',
1054 'start_date' => '2010-01-21',
1055 'end_date' => '2008-12-21',
1056 'source' => 'Payment',
1057 'is_override' => 1,
1058 'status_id' => $this->_membershipStatusID,
1059 );
1060
1061 $this->callAPIFailure('membership', 'create', $params);
1062
1063 //membership_contact_id which is no in contact table
1064 $params['membership_contact_id'] = 999;
1065 $this->callAPIFailure('membership', 'create', $params);
1066
1067 //invalid join date
1068 unset($params['membership_contact_id']);
1069 $params['join_date'] = "invalid";
1070 $this->callAPIFailure('Membership', 'Create', $params);
1071 }
1072
1073 /**
1074 * Test civicrm_contact_memberships_create with membership_contact_id
1075 * membership).
1076 * Success expected.
1077 */
1078 public function testMembershipCreateWithMemContact() {
1079 $params = array(
1080 'membership_contact_id' => $this->_contactID,
1081 'membership_type_id' => $this->_membershipTypeID,
1082 'join_date' => '2011-01-21',
1083 'start_date' => '2010-01-21',
1084 'end_date' => '2008-12-21',
1085 'source' => 'Payment',
1086 'is_override' => 1,
1087 'status_id' => $this->_membershipStatusID,
1088 );
1089
1090 $result = $this->callAPISuccess('membership', 'create', $params);
1091
1092 $this->callAPISuccess('Membership', 'Delete', array(
1093 'id' => $result['id'],
1094 ));
1095 }
1096
1097 /**
1098 * Test civicrm_contact_memberships_create with membership_contact_id
1099 * membership).
1100 * Success expected.
1101 */
1102 public function testMembershipCreateValidMembershipTypeString() {
1103 $params = array(
1104 'membership_contact_id' => $this->_contactID,
1105 'membership_type_id' => 'General',
1106 'join_date' => '2011-01-21',
1107 'start_date' => '2010-01-21',
1108 'end_date' => '2008-12-21',
1109 'source' => 'Payment',
1110 'is_override' => 1,
1111 'status_id' => $this->_membershipStatusID,
1112 );
1113
1114 $result = $this->callAPISuccess('membership', 'create', $params);
1115 $this->assertEquals($this->_membershipTypeID, $result['values'][$result['id']]['membership_type_id']);
1116 $this->callAPISuccess('Membership', 'Delete', array(
1117 'id' => $result['id'],
1118 ));
1119 }
1120
1121 /**
1122 * Test civicrm_contact_memberships_create with membership_contact_id
1123 * membership).
1124 * Success expected.
1125 */
1126 public function testMembershipCreateInValidMembershipTypeString() {
1127 $params = array(
1128 'membership_contact_id' => $this->_contactID,
1129 'membership_type_id' => 'invalid',
1130 'join_date' => '2011-01-21',
1131 'start_date' => '2010-01-21',
1132 'end_date' => '2008-12-21',
1133 'source' => 'Payment',
1134 'is_override' => 1,
1135 'status_id' => $this->_membershipStatusID,
1136 );
1137
1138 $this->callAPIFailure('membership', 'create', $params);
1139 }
1140
1141 /**
1142 * Test that if membership join date is not set it defaults to today.
1143 */
1144 public function testEmptyJoinDate() {
1145 unset($this->_params['join_date'], $this->_params['is_override']);
1146 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1147 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1148 $this->assertEquals(date('Y-m-d', strtotime('now')), $result['join_date']);
1149 $this->assertEquals('2009-01-21', $result['start_date']);
1150 $this->assertEquals('2009-12-21', $result['end_date']);
1151 }
1152
1153 /**
1154 * Test that if membership start date is not set it defaults to correct end date.
1155 * - fixed
1156 */
1157 public function testEmptyStartDateFixed() {
1158 unset($this->_params['start_date'], $this->_params['is_override']);
1159 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1160 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1161 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1162 $this->assertEquals('2009-01-21', $result['join_date']);
1163 $this->assertEquals('2008-03-01', $result['start_date']);
1164 $this->assertEquals('2009-12-21', $result['end_date']);
1165 }
1166
1167 /**
1168 * Test that if membership start date is not set it defaults to correct end date
1169 * - fixed
1170 */
1171 public function testEmptyStartEndDateFixedOneYear() {
1172 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1173 $this->callAPISuccess('membership_type', 'create', array('id' => $this->_membershipTypeID2, 'duration_interval' => 1));
1174 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1175 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1176 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1177 $this->assertEquals('2009-01-21', $result['join_date']);
1178 $this->assertEquals('2008-03-01', $result['start_date']);
1179 $this->assertEquals('2010-02-28', $result['end_date']);
1180 }
1181
1182 /**
1183 * Test that if membership start date is not set it defaults to correct end date for fixed multi year memberships.
1184 */
1185 public function testEmptyStartEndDateFixedMultiYear() {
1186 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1187 $this->callAPISuccess('membership_type', 'create', array('id' => $this->_membershipTypeID2, 'duration_interval' => 5));
1188 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1189 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1190 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1191 $this->assertEquals('2009-01-21', $result['join_date']);
1192 $this->assertEquals('2008-03-01', $result['start_date']);
1193 $this->assertEquals('2014-02-28', $result['end_date']);
1194 }
1195
1196 /**
1197 * CRM-18503 - Test membership join date is correctly set for fixed memberships.
1198 */
1199 public function testMembershipJoinDateFixed() {
1200 $memStatus = CRM_Member_PseudoConstant::membershipStatus();
1201 // Update the fixed membership type to 1 year duration.
1202 $this->callAPISuccess('membership_type', 'create', array('id' => $this->_membershipTypeID2, 'duration_interval' => 1));
1203 $contactId = $this->createLoggedInUser();
1204 // Create membership with 'Pending' status.
1205 $params = array(
1206 'contact_id' => $contactId,
1207 'membership_type_id' => $this->_membershipTypeID2,
1208 'source' => 'test membership',
1209 'is_pay_later' => 0,
1210 'status_id' => array_search('Pending', $memStatus),
1211 'skipStatusCal' => 1,
1212 'is_for_organization' => 1,
1213 );
1214 $ids = array();
1215 $membership = CRM_Member_BAO_Membership::create($params, $ids);
1216
1217 // Update membership to 'Completed' and check the dates.
1218 $memParams = array(
1219 'id' => $membership->id,
1220 'contact_id' => $contactId,
1221 'is_test' => 0,
1222 'membership_type_id' => $this->_membershipTypeID2,
1223 'num_terms' => 1,
1224 'status_id' => array_search('New', $memStatus),
1225 );
1226 $result = $this->callAPISuccess('Membership', 'create', $memParams);
1227
1228 // Extend duration interval if join_date exceeds the rollover period.
1229 $joinDate = date('Y-m-d');
1230 $year = date('Y');
1231 $startDate = date('Y-m-d', strtotime(date('Y-03-01')));
1232 $rollOver = TRUE;
1233 if (strtotime($startDate) > time()) {
1234 $rollOver = FALSE;
1235 $startDate = date('Y-m-d', strtotime(date('Y-03-01') . '- 1 year'));
1236 }
1237 $membershipTypeDetails = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($this->_membershipTypeID2);
1238 $fixedPeriodRollover = CRM_Member_BAO_MembershipType::isDuringFixedAnnualRolloverPeriod($joinDate, $membershipTypeDetails, $year, $startDate);
1239 $y = 1;
1240 if ($fixedPeriodRollover && $rollOver) {
1241 $y += 1;
1242 }
1243
1244 $expectedDates = array(
1245 'join_date' => date('Ymd'),
1246 'start_date' => str_replace('-', '', $startDate),
1247 'end_date' => date('Ymd', strtotime(date('Y-03-01') . "+ {$y} year - 1 day")),
1248 );
1249 foreach ($result['values'] as $values) {
1250 foreach ($expectedDates as $date => $val) {
1251 $this->assertEquals($val, $values[$date], "Failed asserting {$date} values");
1252 }
1253 }
1254 }
1255
1256 /**
1257 * Test correct end and start dates are calculated for fixed multi year memberships.
1258 *
1259 * The empty start date is calculated to be the start_date (1 Jan prior to the join_date - so 1 Jan 15)
1260 *
1261 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1262 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1263 * and we add on 4 years rather than 5 because we are not after the rollover day - so we calculate 31 Dec 2019
1264 */
1265 public function testFixedMultiYearDateSetTwoEmptyStartEndDate() {
1266 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1267
1268 $this->callAPISuccess('membership_type', 'create', array(
1269 'id' => $this->_membershipTypeID2,
1270 'duration_interval' => 5,
1271 // Ie 1 Jan.
1272 'fixed_period_start_day' => '101',
1273 // Ie. 1 Nov.
1274 'fixed_period_rollover_day' => '1101',
1275 ));
1276 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1277 $dates = array(
1278 'join_date' => '28-Jan 2015',
1279 );
1280 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1281 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1282 $this->assertEquals('2015-01-28', $result['join_date']);
1283 $this->assertEquals('2015-01-01', $result['start_date']);
1284 $this->assertEquals('2019-12-31', $result['end_date']);
1285 }
1286
1287 /**
1288 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
1289 *
1290 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1291 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1292 * and we add on 4 years rather than 5 because we are not after the rollover day - so we calculate 31 Dec 2019
1293 */
1294 public function testFixedMultiYearDateSetTwoEmptyEndDate() {
1295 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1296
1297 $this->callAPISuccess('membership_type', 'create', array(
1298 'id' => $this->_membershipTypeID2,
1299 'duration_interval' => 5,
1300 // Ie 1 Jan.
1301 'fixed_period_start_day' => '101',
1302 // Ie. 1 Nov.
1303 'fixed_period_rollover_day' => '1101',
1304 ));
1305 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1306 $dates = array(
1307 'start_date' => '28-Jan 2015',
1308 'join_date' => '28-Jan 2015',
1309 );
1310 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1311 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1312 $this->assertEquals('2015-01-28', $result['join_date']);
1313 $this->assertEquals('2015-01-28', $result['start_date']);
1314 $this->assertEquals('2019-12-31', $result['end_date']);
1315 }
1316
1317 /**
1318 * Test correct end and start dates are calculated for fixed multi year memberships.
1319 *
1320 * The empty start date is calculated to be the start_date (1 Jan prior to the join_date - so 1 Jan 15)
1321 *
1322 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1323 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1324 * and we add on <1 years rather than > 1 because we are not after the rollover day - so we calculate 31 Dec 2015
1325 */
1326 public function testFixedSingleYearDateSetTwoEmptyStartEndDate() {
1327 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1328
1329 $this->callAPISuccess('membership_type', 'create', array(
1330 'id' => $this->_membershipTypeID2,
1331 'duration_interval' => 1,
1332 // Ie 1 Jan.
1333 'fixed_period_start_day' => '101',
1334 // Ie. 1 Nov.
1335 'fixed_period_rollover_day' => '1101',
1336 ));
1337 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1338 $dates = array(
1339 'join_date' => '28-Jan 2015',
1340 );
1341 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1342 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1343 $this->assertEquals('2015-01-28', $result['join_date']);
1344 $this->assertEquals('2015-01-01', $result['start_date']);
1345 $this->assertEquals('2015-12-31', $result['end_date']);
1346 }
1347
1348 /**
1349 * Test correct end date for fixed single year memberships is calculated and start_date is not changed.
1350 *
1351 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1352 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1353 * and we add on <1 years rather than > 1 because we are not after the rollover day - so we calculate 31 Dec 2015
1354 */
1355 public function testFixedSingleYearDateSetTwoEmptyEndDate() {
1356 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1357
1358 $this->callAPISuccess('membership_type', 'create', array(
1359 'id' => $this->_membershipTypeID2,
1360 'duration_interval' => 1,
1361 // Ie 1 Jan.
1362 'fixed_period_start_day' => '101',
1363 // Ie. 1 Nov.
1364 'fixed_period_rollover_day' => '1101',
1365 ));
1366 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1367 $dates = array(
1368 'start_date' => '28-Jan 2015',
1369 'join_date' => '28-Jan 2015',
1370 );
1371 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1372 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1373 $this->assertEquals('2015-01-28', $result['join_date']);
1374 $this->assertEquals('2015-01-28', $result['start_date']);
1375 $this->assertEquals('2015-12-31', $result['end_date']);
1376 }
1377
1378 /**
1379 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
1380 *
1381 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1382 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1383 * and we add on 1 year we are after the rollover day - so we calculate 31 Oct 2016
1384 */
1385 public function testFixedSingleYearDateSetThreeEmptyEndDate() {
1386 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1387
1388 $this->callAPISuccess('membership_type', 'create', array(
1389 'id' => $this->_membershipTypeID2,
1390 'duration_interval' => 1,
1391 // Ie. 1 Nov.
1392 'fixed_period_start_day' => '1101',
1393 // Ie 1 Jan.
1394 'fixed_period_rollover_day' => '101',
1395 ));
1396 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1397 $dates = array(
1398 'start_date' => '28-Jan 2015',
1399 'join_date' => '28-Jan 2015',
1400 );
1401 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1402 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1403 $this->assertEquals('2015-01-28', $result['join_date']);
1404 $this->assertEquals('2015-01-28', $result['start_date']);
1405 $this->assertEquals('2016-10-31', $result['end_date']);
1406 }
1407
1408 /**
1409 * Test correct end and start dates are calculated for fixed multi year memberships.
1410 *
1411 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
1412 *
1413 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1414 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1415 * and we add on 1 year we are after the rollover day - so we calculate 31 Oct 2016
1416 */
1417 public function testFixedSingleYearDateSetThreeEmptyStartEndDate() {
1418 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1419
1420 $this->callAPISuccess('membership_type', 'create', array(
1421 'id' => $this->_membershipTypeID2,
1422 'duration_interval' => 1,
1423 // Ie. 1 Nov.
1424 'fixed_period_start_day' => '1101',
1425 // Ie 1 Jan.
1426 'fixed_period_rollover_day' => '101',
1427 ));
1428 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1429 $dates = array(
1430 'join_date' => '28-Jan 2015',
1431 );
1432 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1433 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1434 $this->assertEquals('2015-01-28', $result['join_date']);
1435 $this->assertEquals('2014-11-01', $result['start_date']);
1436 $this->assertEquals('2016-10-31', $result['end_date']);
1437 }
1438
1439 /**
1440 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
1441 *
1442 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1443 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1444 * and we add on 5 years we are after the rollover day - so we calculate 31 Oct 2020
1445 */
1446 public function testFixedMultiYearDateSetThreeEmptyEndDate() {
1447 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1448
1449 $this->callAPISuccess('membership_type', 'create', array(
1450 'id' => $this->_membershipTypeID2,
1451 'duration_interval' => 5,
1452 // Ie. 1 Nov.
1453 'fixed_period_start_day' => '1101',
1454 // Ie 1 Jan.
1455 'fixed_period_rollover_day' => '101',
1456 ));
1457 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1458 $dates = array(
1459 'start_date' => '28-Jan 2015',
1460 'join_date' => '28-Jan 2015',
1461 );
1462 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1463 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1464 $this->assertEquals('2015-01-28', $result['join_date']);
1465 $this->assertEquals('2015-01-28', $result['start_date']);
1466 $this->assertEquals('2020-10-31', $result['end_date']);
1467 }
1468
1469 /**
1470 * Test correct end and start dates are calculated for fixed multi year memberships.
1471 *
1472 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
1473 *
1474 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
1475 * In this set our join date is after the start day and after the rollover day so we do get an extra year
1476 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1477 * and we add on 5 years we are after the rollover day - so we calculate 31 Oct 2020
1478 */
1479 public function testFixedMultiYearDateSetThreeEmptyStartEndDate() {
1480 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1481
1482 $this->callAPISuccess('membership_type', 'create', array(
1483 'id' => $this->_membershipTypeID2,
1484 'duration_interval' => 5,
1485 // Ie. 1 Nov.
1486 'fixed_period_start_day' => '1101',
1487 // Ie 1 Jan.
1488 'fixed_period_rollover_day' => '101',
1489 ));
1490 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1491 $dates = array(
1492 'join_date' => '28-Jan 2015',
1493 );
1494 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1495 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1496 $this->assertEquals('2015-01-28', $result['join_date']);
1497 $this->assertEquals('2014-11-01', $result['start_date']);
1498 $this->assertEquals('2020-10-31', $result['end_date']);
1499 }
1500
1501 /**
1502 * Test that if membership start date is not set it defaults to correct end date for fixed single year memberships.
1503 */
1504 public function testEmptyStartDateRolling() {
1505 unset($this->_params['start_date'], $this->_params['is_override']);
1506 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1507 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1508 $this->assertEquals('2009-01-21', $result['join_date']);
1509 $this->assertEquals('2009-01-21', $result['start_date']);
1510 $this->assertEquals('2009-12-21', $result['end_date']);
1511 }
1512
1513 /**
1514 * Test that if membership end date is not set it defaults to correct end date.
1515 * - rolling
1516 */
1517 public function testEmptyEndDateFixed() {
1518 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1519 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1520 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1521 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1522 $this->assertEquals('2009-01-21', $result['join_date']);
1523 $this->assertEquals('2008-03-01', $result['start_date']);
1524 $this->assertEquals('2010-02-28', $result['end_date']);
1525 }
1526
1527 /**
1528 * Test that if membership end date is not set it defaults to correct end date.
1529 * - rolling
1530 */
1531 public function testEmptyEndDateRolling() {
1532 unset($this->_params['is_override'], $this->_params['end_date']);
1533 $this->_params['membership_type_id'] = $this->_membershipTypeID;
1534 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1535 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1536 $this->assertEquals('2009-01-21', $result['join_date']);
1537 $this->assertEquals('2009-01-21', $result['start_date']);
1538 $this->assertEquals('2010-01-20', $result['end_date']);
1539 }
1540
1541
1542 /**
1543 * Test that if dates are set they not over-ridden if id is passed in
1544 */
1545 public function testMembershipDatesNotOverridden() {
1546 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1547 unset($this->_params['end_date'], $this->_params['start_date']);
1548 $this->_params['id'] = $result['id'];
1549 $this->callAPISuccess($this->_entity, 'create', $this->_params);
1550 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1551 $this->assertEquals('2009-01-21', $result['join_date']);
1552 $this->assertEquals('2009-01-21', $result['start_date']);
1553 $this->assertEquals('2009-12-21', $result['end_date']);
1554
1555 }
1556
1557 /**
1558 * Test that all membership types are returned when getoptions is called.
1559 *
1560 * This test locks in current behaviour where types for all domains are returned. It should possibly be domain
1561 * specific but that should only be done in conjunction with adding a hook to allow that to be altered as the
1562 * multisite use case expects the master domain to be able to see all sites.
1563 *
1564 * See CRM-17075.
1565 */
1566 public function testGetOptionsMembershipTypeID() {
1567 $options = $this->callAPISuccess('Membership', 'getoptions', array('field' => 'membership_type_id'));
1568 $this->assertEquals('Another one', array_pop($options['values']));
1569 $this->assertEquals('General', array_pop($options['values']));
1570 $this->assertEquals(NULL, array_pop($options['values']));
1571 }
1572
1573 }