3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Test APIv3 civicrm_membership functions
31 * @package CiviCRM_APIv3
32 * @subpackage API_Member
36 * Class api_v3_MembershipTest
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;
54 public function 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',
62 'fixed_period_start_day' => '301',
64 'fixed_period_rollover_day' => '1111',
65 'name' => 'Another one',
67 $this->_membershipStatusID
= $this->membershipStatusCreate('test status');
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');
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',
82 'status_id' => $this->_membershipStatusID
,
87 * Clean up after tests.
91 public function tearDown() {
92 $this->quickCleanup(array(
94 'civicrm_membership_payment',
95 'civicrm_membership_log',
100 $this->membershipStatusDelete($this->_membershipStatusID
);
101 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID2
));
102 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID
));
103 $this->contactDelete($this->_contactID
);
108 * Test membership deletion.
110 public function testMembershipDelete() {
111 $membershipID = $this->contactMembershipCreate($this->_params
);
112 $this->assertDBRowExist('CRM_Member_DAO_Membership', $membershipID);
114 'id' => $membershipID,
116 $this->callAPIAndDocument('membership', 'delete', $params, __FUNCTION__
, __FILE__
);
117 $this->assertDBRowNotExist('CRM_Member_DAO_Membership', $membershipID);
120 public function testMembershipDeleteEmpty() {
121 $this->callAPIFailure('membership', 'delete', array());
124 public function testMembershipDeleteInvalidID() {
125 $this->callAPIFailure('membership', 'delete', array('id' => 'blah'));
129 * Test civicrm_membership_delete() with invalid Membership Id.
131 public function testMembershipDeleteWithInvalidMembershipId() {
132 $membershipId = 'membership';
133 $this->callAPIFailure('membership', 'delete', $membershipId);
137 * Test membership deletion and with the preserve contribution param.
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(
144 'financial_type_id' => "Member Dues",
145 'total_amount' => 100,
146 'contact_id' => $this->_params
['contact_id'],
148 $membershipPaymentCreate = $this->callAPISuccess('MembershipPayment', 'create', array(
150 'contribution_id' => $ContributionCreate['values'][0]['id'],
151 'membership_id' => $membershipID,
154 'id' => $membershipID,
155 'preserve_contribution' => 1,
157 $contribParams = array(
158 'id' => $ContributionCreate['values'][0]['id'],
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']);
168 * Test Activity creation on cancellation of membership contribution.
170 public function testActivityForCancelledContribution() {
171 $contactId = $this->createLoggedInUser();
172 $membershipID = $this->contactMembershipCreate($this->_params
);
173 $this->assertDBRowExist('CRM_Member_DAO_Membership', $membershipID);
175 $ContributionCreate = $this->callAPISuccess('Contribution', 'create', array(
176 'financial_type_id' => "Member Dues",
177 'total_amount' => 100,
178 'contact_id' => $this->_params
['contact_id'],
180 $membershipPaymentCreate = $this->callAPISuccess('MembershipPayment', 'create', array(
182 'contribution_id' => $ContributionCreate['id'],
183 'membership_id' => $membershipID,
185 $instruments = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
186 $this->paymentInstruments
= $instruments['values'];
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 'receive_date' => '04/21/2015',
194 'receive_date_time' => '11:27PM',
195 'contact_id' => $contactId,
196 'payment_instrument_id' => array_search('Check', $this->paymentInstruments
),
197 'contribution_status_id' => 3,
199 CRM_Core_Action
::UPDATE
);
201 $activity = $this->callAPISuccess('Activity', 'get', array(
202 'activity_type_id' => "Change Membership Status",
203 'source_record_id' => $membershipID,
205 $this->assertNotEmpty($activity['values']);
209 * Test membership get.
211 public function testContactMembershipsGet() {
212 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
213 $this->callAPISuccess('membership', 'get', array());
214 $this->callAPISuccess('Membership', 'Delete', array('id' => $this->_membershipID
));
218 * Test civicrm_membership_get with params not array.
220 * Gets treated as contact_id, memberships expected.
222 public function testGetWithParamsContactId() {
223 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
225 'contact_id' => $this->_contactID
,
227 $membership = $this->callAPISuccess('membership', 'get', $params);
229 $result = $membership['values'][$this->_membershipID
];
230 $this->callAPISuccess('Membership', 'Delete', array(
231 'id' => $this->_membershipID
,
233 $this->assertEquals($result['contact_id'], $this->_contactID
, "In line " . __LINE__
);
234 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID
, "In line " . __LINE__
);
235 $this->assertEquals($result['status_id'], $this->_membershipStatusID
, "In line " . __LINE__
);
236 $this->assertEquals($result['join_date'], '2009-01-21', "In line " . __LINE__
);
237 $this->assertEquals($result['start_date'], '2009-01-21', "In line " . __LINE__
);
238 $this->assertEquals($result['end_date'], '2009-12-21', "In line " . __LINE__
);
239 $this->assertEquals($result['source'], 'Payment', "In line " . __LINE__
);
240 $this->assertEquals($result['is_override'], 1, "In line " . __LINE__
);
244 * Test civicrm_membership_get with params not array.
246 * Gets treated as contact_id, memberships expected.
248 public function testGetInSyntax() {
249 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
250 $this->_membershipID2
= $this->contactMembershipCreate($this->_params
);
251 $this->_membershipID3
= $this->contactMembershipCreate($this->_params
);
253 'id' => array('IN' => array($this->_membershipID
, $this->_membershipID3
)),
255 $membership = $this->callAPISuccess('membership', 'get', $params);
256 $this->assertEquals(2, $membership['count']);
257 $this->assertEquals(array($this->_membershipID
, $this->_membershipID3
), array_keys($membership['values']));
259 'id' => array('NOT IN' => array($this->_membershipID
, $this->_membershipID3
)),
261 $membership = $this->callAPISuccess('membership', 'get', $params);
262 $this->assertEquals(1, $membership['count']);
263 $this->assertEquals(array($this->_membershipID2
), array_keys($membership['values']));
267 * Test civicrm_membership_get with params not array.
268 * Gets treated as contact_id, memberships expected.
270 public function testGetInSyntaxOnContactID() {
271 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
272 $contact2 = $this->individualCreate();
273 $contact3 = $this->individualCreate(array('first_name' => 'Scout', 'last_name' => 'Canine'));
274 $this->_membershipID2
= $this->contactMembershipCreate(array_merge($this->_params
, array('contact_id' => $contact2)));
275 $this->_membershipID3
= $this->contactMembershipCreate(array_merge($this->_params
, array('contact_id' => $contact3)));
277 'contact_id' => array('IN' => array($this->_contactID
, $contact3)),
279 $membership = $this->callAPISuccess('membership', 'get', $params);
280 $this->assertEquals(2, $membership['count']);
281 $this->assertEquals(array($this->_membershipID
, $this->_membershipID3
), array_keys($membership['values']));
283 'contact_id' => array('NOT IN' => array($this->_contactID
, $contact3)),
285 $membership = $this->callAPISuccess('membership', 'get', $params);
286 $this->assertEquals(1, $membership['count']);
287 $this->assertEquals(array($this->_membershipID2
), array_keys($membership['values']));
291 * Test civicrm_membership_get with params not array.
293 * Gets treated as contact_id, memberships expected.
295 public function testGetWithParamsMemberShipTypeId() {
296 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
298 'membership_type_id' => $this->_membershipTypeID
,
300 $membership = $this->callAPISuccess('membership', 'get', $params);
301 $this->callAPISuccess('Membership', 'Delete', array(
302 'id' => $membership['id'],
304 $result = $membership['values'][$membership['id']];
305 $this->assertEquals($result['contact_id'], $this->_contactID
);
306 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID
);
307 $this->assertEquals($result['status_id'], $this->_membershipStatusID
);
308 $this->assertEquals($result['join_date'], '2009-01-21');
309 $this->assertEquals($result['start_date'], '2009-01-21');
310 $this->assertEquals($result['end_date'], '2009-12-21');
311 $this->assertEquals($result['source'], 'Payment');
312 $this->assertEquals($result['is_override'], 1);
313 $this->assertEquals($result['id'], $membership['id']);
317 * Test civicrm_membership_get with params not array.
318 * Gets treated as contact_id, memberships expected.
320 public function testGetWithParamsMemberShipTypeIdContactID() {
321 $params = $this->_params
;
322 $this->callAPISuccess($this->_entity
, 'create', $params);
323 $params['membership_type_id'] = $this->_membershipTypeID2
;
324 $this->callAPISuccess($this->_entity
, 'create', $params);
325 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_contactID
), 2);
327 'membership_type_id' => $this->_membershipTypeID
,
328 'contact_id' => $this->_contactID
,
330 $result = $this->callAPISuccess('membership', 'getsingle', $params);
331 $this->assertEquals($result['contact_id'], $this->_contactID
);
332 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID
);
335 'membership_type_id' => $this->_membershipTypeID2
,
336 'contact_id' => $this->_contactID
,
338 $result = $this->callAPISuccess('membership', 'getsingle', $params);
339 $this->assertEquals($result['contact_id'], $this->_contactID
);
340 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID2
);
344 * Check with complete array + custom field.
346 * Note that the test is written on purpose without any
347 * variables specific to participant so it can be replicated into other entities
348 * and / or moved to the automated test suite
350 public function testGetWithParamsMemberShipIdAndCustom() {
351 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
353 $params = $this->_params
;
354 $params['custom_' . $ids['custom_field_id']] = "custom string";
356 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
358 $getParams = array('membership_type_id' => $params['membership_type_id']);
359 $check = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
360 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
362 $this->callAPISuccess('Membership', 'Delete', array(
363 'id' => $result['id'],
368 * Test civicrm_membership_get with proper params.
369 * Memberships expected.
371 public function testGet() {
372 $membershipID = $this->contactMembershipCreate($this->_params
);
374 'contact_id' => $this->_contactID
,
377 $membership = $this->callAPISuccess('membership', 'get', $params);
378 $result = $membership['values'][$membershipID];
379 $this->callAPISuccess('Membership', 'Delete', array(
380 'id' => $membership['id'],
382 $this->assertEquals($result['join_date'], '2009-01-21');
383 $this->assertEquals($result['contact_id'], $this->_contactID
);
384 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID
);
385 $this->assertEquals($result['status_id'], $this->_membershipStatusID
);
387 $this->assertEquals($result['start_date'], '2009-01-21');
388 $this->assertEquals($result['end_date'], '2009-12-21');
389 $this->assertEquals($result['source'], 'Payment');
390 $this->assertEquals($result['is_override'], 1);
395 * Test civicrm_membership_get with proper params.
396 * Memberships expected.
398 public function testGetWithId() {
399 $membershipID = $this->contactMembershipCreate($this->_params
);
401 'contact_id' => $this->_contactID
,
402 'id' => $this->_membershipID
,
405 $result = $this->callAPISuccess('membership', 'get', $params);
406 $this->assertEquals($membershipID, $result['id']);
408 'contact_id' => $this->_contactID
,
409 'membership_id' => $this->_membershipID
,
410 'return' => 'membership_id',
412 $result = $this->callAPISuccess('membership', 'get', $params);
413 $this->assertEquals($membershipID, $result['id']);
417 * Test civicrm_membership_get for only active.
418 * Memberships expected.
420 public function testGetOnlyActive() {
421 $description = "Demonstrates use of 'filter' active_only' param.";
422 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
424 'contact_id' => $this->_contactID
,
428 $membership = $this->callAPISuccess('membership', 'get', $params);
429 $this->assertEquals($membership['values'][$this->_membershipID
]['status_id'], $this->_membershipStatusID
);
430 $this->assertEquals($membership['values'][$this->_membershipID
]['contact_id'], $this->_contactID
);
432 'contact_id' => $this->_contactID
,
438 $membership = $this->callAPIAndDocument('membership', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'FilterIsCurrent');
439 $this->assertEquals($membership['values'][$this->_membershipID
]['status_id'], $this->_membershipStatusID
);
440 $this->assertEquals($membership['values'][$this->_membershipID
]['contact_id'], $this->_contactID
);
442 $this->callAPISuccess('Membership', 'Delete', array('id' => $this->_membershipID
));
446 * Test civicrm_membership_get for non exist contact.
449 public function testGetNoContactExists() {
451 'contact_id' => 55555,
454 $membership = $this->callAPISuccess('membership', 'get', $params);
455 $this->assertEquals($membership['count'], 0);
459 * Test civicrm_membership_get with relationship.
462 public function testGetWithRelationship() {
463 $membershipOrgId = $this->organizationCreate(NULL);
464 $memberContactId = $this->individualCreate();
466 $relTypeParams = array(
467 'name_a_b' => 'Relation 1',
468 'name_b_a' => 'Relation 2',
469 'description' => 'Testing relationship type',
470 'contact_type_a' => 'Organization',
471 'contact_type_b' => 'Individual',
475 $relTypeID = $this->relationshipTypeCreate($relTypeParams);
478 'name' => 'test General',
479 'duration_unit' => 'year',
480 'duration_interval' => 1,
481 'period_type' => 'rolling',
482 'member_of_contact_id' => $membershipOrgId,
484 'financial_type_id' => 1,
485 'relationship_type_id' => $relTypeID,
486 'relationship_direction' => 'b_a',
489 $memType = $this->callAPISuccess('membership_type', 'create', $params);
492 'contact_id' => $memberContactId,
493 'membership_type_id' => $memType['id'],
494 'join_date' => '2009-01-21',
495 'start_date' => '2009-01-21',
496 'end_date' => '2009-12-21',
497 'source' => 'Payment',
499 'status_id' => $this->_membershipStatusID
,
501 $membershipID = $this->contactMembershipCreate($params);
504 'contact_id' => $memberContactId,
505 'membership_type_id' => $memType['id'],
508 $result = $this->callAPISuccess('membership', 'get', $params);
510 $membership = $result['values'][$membershipID];
511 $this->assertEquals($this->_membershipStatusID
, $membership['status_id']);
512 $this->callAPISuccess('Membership', 'Delete', array(
513 'id' => $membership['id'],
515 $this->membershipTypeDelete(array('id' => $memType['id']));
516 $this->relationshipTypeDelete($relTypeID);
517 $this->contactDelete($membershipOrgId);
518 $this->contactDelete($memberContactId);
522 * Test civicrm_membership_create with relationships.
523 * create/get Memberships.
525 * Test suite for CRM-14758: API ( contact, create ) does not always create related membership
526 * and max_related property for Membership_Type and Membership entities
528 public function testCreateWithRelationship() {
529 // Create membership type: inherited through employment, max_related = 2
531 'name_a_b' => 'Employee of',
533 $result = $this->callAPISuccess('relationship_type', 'get', $params);
534 $relationshipTypeId = $result['id'];
535 $membershipOrgId = $this->organizationCreate();
537 'name' => 'Corporate Membership',
538 'duration_unit' => 'year',
539 'duration_interval' => 1,
540 'period_type' => 'rolling',
541 'member_of_contact_id' => $membershipOrgId,
543 'financial_type_id' => 1,
544 'relationship_type_id' => $relationshipTypeId,
545 'relationship_direction' => 'b_a',
549 $result = $this->callAPISuccess('membership_type', 'create', $params);
550 $membershipTypeId = $result['id'];
552 // Create employer and first employee
553 $employerId[0] = $this->organizationCreate(array(), 1);
554 $memberContactId[0] = $this->individualCreate(array('employer_id' => $employerId[0]), 0);
556 // Create organization's membership
558 'contact_id' => $employerId[0],
559 'membership_type_id' => $membershipTypeId,
560 'source' => 'Test suite',
561 'start_date' => date('Y-m-d'),
562 'end_date' => "+1 year",
564 $OrganizationMembershipID = $this->contactMembershipCreate($params);
566 // Check that the employee inherited the membership
568 'contact_id' => $memberContactId[0],
569 'membership_type_id' => $membershipTypeId,
572 $result = $this->callAPISuccess('membership', 'get', $params);
574 $this->assertEquals(1, $result['count']);
575 $result = $result['values'][$result['id']];
576 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
578 // Create second employee
579 $memberContactId[1] = $this->individualCreate(array('employer_id' => $employerId[0]), 1);
581 // Check that the employee inherited the membership
583 'contact_id' => $memberContactId[1],
584 'membership_type_id' => $membershipTypeId,
586 $result = $this->callAPISuccess('membership', 'get', $params);
587 // If it fails here CRM-14758 is not fixed
588 $this->assertEquals(1, $result['count']);
589 $result = $result['values'][$result['id']];
590 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
592 // Create third employee
593 $memberContactId[2] = $this->individualCreate(array('employer_id' => $employerId[0]), 2);
595 // Check that employee does NOT inherit the membership (max_related = 2)
597 'contact_id' => $memberContactId[2],
598 'membership_type_id' => $membershipTypeId,
600 $result = $this->callAPISuccess('membership', 'get', $params);
601 $this->assertEquals(0, $result['count']);
603 // Increase max_related for the employer's membership
605 'id' => $OrganizationMembershipID,
608 $this->callAPISuccess('Membership', 'create', $params);
610 // Check that the employee inherited the membership
612 'contact_id' => $memberContactId[2],
613 'membership_type_id' => $membershipTypeId,
615 $result = $this->callAPISuccess('membership', 'get', $params);
616 $this->assertEquals(1, $result['count']);
617 $result = $result['values'][$result['id']];
618 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
620 // First employee moves to a new job
621 $employerId[1] = $this->organizationCreate(array(), 2);
623 'id' => $memberContactId[0],
624 'employer_id' => $employerId[1],
626 $this->callAPISuccess('contact', 'create', $params);
628 // Check that employee does NO LONGER inherit the membership
630 'contact_id' => $memberContactId[0],
631 'membership_type_id' => $membershipTypeId,
633 $result = $this->callAPISuccess('membership', 'get', $params);
634 $this->assertEquals(0, $result['count']);
636 // Set up params for enable/disable checks
637 $relationship1 = $this->callAPISuccess('relationship', 'get', array('contact_id_a' => $memberContactId[1]));
639 'contact_id' => $memberContactId[1],
640 'membership_type_id' => $membershipTypeId,
643 // Deactivate relationship using create and assert membership is not inherited
644 $this->callAPISuccess('relationship', 'create', array('id' => $relationship1['id'], 'is_active' => 0));
645 $result = $this->callAPISuccess('membership', 'get', $params);
646 $this->assertEquals(0, $result['count']);
648 // Re-enable relationship using create and assert membership is inherited
649 $this->callAPISuccess('relationship', 'create', array('id' => $relationship1['id'], 'is_active' => 1));
650 $result = $this->callAPISuccess('membership', 'get', $params);
651 $this->assertEquals(1, $result['count']);
653 // Deactivate relationship using setvalue and assert membership is not inherited
654 $this->callAPISuccess('relationship', 'setvalue', array('id' => $relationship1['id'], 'field' => 'is_active', 'value' => 0));
655 $result = $this->callAPISuccess('membership', 'get', $params);
656 $this->assertEquals(0, $result['count']);
658 // Re-enable relationship using setvalue and assert membership is inherited
659 $this->callAPISuccess('relationship', 'setvalue', array('id' => $relationship1['id'], 'field' => 'is_active', 'value' => 1));
660 $result = $this->callAPISuccess('membership', 'get', $params);
661 $this->assertEquals(1, $result['count']);
663 // Delete relationship and assert membership is not inherited
664 $this->callAPISuccess('relationship', 'delete', array('id' => $relationship1['id']));
665 $result = $this->callAPISuccess('membership', 'get', $params);
666 $this->assertEquals(0, $result['count']);
668 // Tear down - reverse of creation to be safe
669 $this->contactDelete($memberContactId[2]);
670 $this->contactDelete($memberContactId[1]);
671 $this->contactDelete($memberContactId[0]);
672 $this->contactDelete($employerId[1]);
673 $this->contactDelete($employerId[0]);
674 $this->membershipTypeDelete(array('id' => $membershipTypeId));
675 $this->contactDelete($membershipOrgId);
679 * We are checking for no e-notices + only id & end_date returned
681 public function testMembershipGetWithReturn() {
682 $this->contactMembershipCreate($this->_params
);
683 $result = $this->callAPISuccess('membership', 'get', array('return' => 'end_date'));
684 foreach ($result['values'] as $membership) {
685 $this->assertEquals(array('id', 'end_date'), array_keys($membership));
688 ///////////////// civicrm_membership_create methods
691 * Test civicrm_contact_memberships_create with empty params.
694 public function testCreateWithEmptyParams() {
696 $this->callAPIFailure('membership', 'create', $params);
700 * If is_overide is passed in status must also be passed in.
702 public function testCreateOverrideNoStatus() {
703 $params = $this->_params
;
704 unset($params['status_id']);
705 $this->callAPIFailure('membership', 'create', $params);
708 public function testMembershipCreateMissingRequired() {
710 'membership_type_id' => '1',
711 'join_date' => '2006-01-21',
712 'start_date' => '2006-01-21',
713 'end_date' => '2006-12-21',
714 'source' => 'Payment',
718 $this->callAPIFailure('membership', 'create', $params);
721 public function testMembershipCreate() {
723 'contact_id' => $this->_contactID
,
724 'membership_type_id' => $this->_membershipTypeID
,
725 'join_date' => '2006-01-21',
726 'start_date' => '2006-01-21',
727 'end_date' => '2006-12-21',
728 'source' => 'Payment',
730 'status_id' => $this->_membershipStatusID
,
733 $result = $this->callAPIAndDocument('membership', 'create', $params, __FUNCTION__
, __FILE__
);
734 $this->getAndCheck($params, $result['id'], $this->_entity
);
735 $this->assertNotNull($result['id']);
736 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['contact_id'], " in line " . __LINE__
);
737 $this->assertEquals($result['id'], $result['values'][$result['id']]['id'], " in line " . __LINE__
);
741 * Check for useful message if contact doesn't exist
743 public function testMembershipCreateWithInvalidContact() {
746 'membership_type_id' => $this->_membershipTypeID
,
747 'join_date' => '2006-01-21',
748 'start_date' => '2006-01-21',
749 'end_date' => '2006-12-21',
750 'source' => 'Payment',
752 'status_id' => $this->_membershipStatusID
,
755 $this->callAPIFailure('membership', 'create', $params,
756 'contact_id is not valid : 999'
760 public function testMembershipCreateWithInvalidStatus() {
761 $params = $this->_params
;
762 $params['status_id'] = 999;
763 $this->callAPIFailure('membership', 'create', $params,
764 "'999' is not a valid option for field status_id"
768 public function testMembershipCreateWithInvalidType() {
769 $params = $this->_params
;
770 $params['membership_type_id'] = 999;
772 $this->callAPIFailure('membership', 'create', $params,
773 "'999' is not a valid option for field membership_type_id"
778 * Check with complete array + custom field
779 * Note that the test is written on purpose without any
780 * variables specific to participant so it can be replicated into other entities
781 * and / or moved to the automated test suite
783 public function testCreateWithCustom() {
784 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
786 $params = $this->_params
;
787 $params['custom_' . $ids['custom_field_id']] = "custom string";
789 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
, NULL, 'CreateWithCustomData');
790 $check = $this->callAPISuccess($this->_entity
, 'get', array(
791 'id' => $result['id'],
792 'contact_id' => $this->_contactID
,
794 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
798 * Search on custom field value.
800 public function testSearchWithCustomDataCRM16036() {
801 // Create a custom field on membership
802 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
804 // Create a new membership, but don't assign anything to the custom field.
805 $params = $this->_params
;
806 $result = $this->callAPIAndDocument(
813 'SearchWithCustomData');
815 // search memberships with CRM-16036 as custom field value.
816 // Since we did not touch the custom field of any membership,
817 // this should not return any results.
818 $check = $this->callAPISuccess($this->_entity
, 'get', array(
819 'custom_' . $ids['custom_field_id'] => "CRM-16036",
823 $this->callAPISuccess($this->_entity
, 'delete', array(
824 'id' => $result['id'],
828 $this->assertEquals(0, $check['count']);
832 * Test civicrm_contact_memberships_create with membership id (edit
836 public function testMembershipCreateWithId() {
837 $membershipID = $this->contactMembershipCreate($this->_params
);
839 'id' => $membershipID,
840 'contact_id' => $this->_contactID
,
841 'membership_type_id' => $this->_membershipTypeID
,
842 'join_date' => '2006-01-21',
843 'start_date' => '2006-01-21',
844 'end_date' => '2006-12-21',
845 'source' => 'Payment',
847 'status_id' => $this->_membershipStatusID
,
850 $result = $this->callAPISuccess('membership', 'create', $params);
852 //Update Status and check activities created.
853 $updateStatus = array(
854 'id' => $result['id'],
855 'status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Member_BAO_Membership', 'status_id', 'Cancelled'),
857 $this->callAPISuccess('Membership', 'create', $updateStatus);
858 $activities = CRM_Activity_BAO_Activity
::getContactActivity($this->_contactID
);
859 $this->assertEquals(2, count($activities));
860 $activityNames = array_flip(CRM_Utils_Array
::collect('activity_name', $activities));
861 $this->assertArrayHasKey('Membership Signup', $activityNames);
862 $this->assertArrayHasKey('Change Membership Status', $activityNames);
864 $this->callAPISuccess('Membership', 'Delete', array(
865 'id' => $result['id'],
867 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
871 * Test civicrm_contact_memberships_create with membership id (edit
875 public function testMembershipCreateUpdateWithIdNoContact() {
876 $membershipID = $this->contactMembershipCreate($this->_params
);
878 'id' => $membershipID,
879 'membership_type_id' => $this->_membershipTypeID
,
880 'contact_id' => $this->_contactID
,
881 'join_date' => '2006-01-21',
882 'start_date' => '2006-01-21',
883 'end_date' => '2006-12-21',
884 'source' => 'Payment',
886 'status_id' => $this->_membershipStatusID
,
889 $result = $this->callAPISuccess('membership', 'create', $params);
890 $this->callAPISuccess('Membership', 'Delete', array(
891 'id' => $result['id'],
894 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
898 * Test civicrm_contact_memberships_create with membership id (edit
902 public function testMembershipCreateUpdateWithIdNoDates() {
903 $membershipID = $this->contactMembershipCreate($this->_params
);
905 'id' => $membershipID,
906 'contact_id' => $this->_contactID
,
907 'membership_type_id' => $this->_membershipTypeID
,
908 'source' => 'Payment',
910 'status_id' => $this->_membershipStatusID
,
913 $result = $this->callAPISuccess('membership', 'create', $params);
914 $this->callAPISuccess('Membership', 'Delete', array(
915 'id' => $result['id'],
917 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
921 * Test civicrm_contact_memberships_create with membership id (edit
925 public function testMembershipCreateUpdateWithIdNoDatesNoType() {
926 $membershipID = $this->contactMembershipCreate($this->_params
);
928 'id' => $membershipID,
929 'source' => 'not much here',
930 'contact_id' => $this->_contactID
,
932 'status_id' => $this->_membershipStatusID
,
935 $result = $this->callAPISuccess('membership', 'create', $params);
936 $this->callAPISuccess('Membership', 'Delete', array(
937 'id' => $result['id'],
939 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
943 * Test civicrm_contact_memberships_create with membership id (edit
947 public function testMembershipCreateUpdateWithIDAndSource() {
948 $membershipID = $this->contactMembershipCreate($this->_params
);
950 'id' => $membershipID,
951 'source' => 'changed',
952 'contact_id' => $this->_contactID
,
953 'status_id' => $this->_membershipStatusID
,
954 'membership_type_id' => $this->_membershipTypeID
,
955 'skipStatusCal' => 1,
957 $result = $this->callAPISuccess('membership', 'create', $params);
958 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
959 $this->callAPISuccess('Membership', 'Delete', array(
960 'id' => $result['id'],
965 * Change custom field using update.
967 public function testUpdateWithCustom() {
968 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
970 $params = $this->_params
;
971 $params['custom_' . $ids['custom_field_id']] = "custom string";
972 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
, NULL, 'UpdateCustomData');
973 $result = $this->callAPISuccess($this->_entity
, 'create', array(
974 'id' => $result['id'],
975 'custom_' . $ids['custom_field_id'] => "new custom",
977 $check = $this->callAPISuccess($this->_entity
, 'get', array(
978 'id' => $result['id'],
979 'contact_id' => $this->_contactID
,
982 $this->assertEquals("new custom", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
983 $this->callAPISuccess('Membership', 'Delete', array(
984 'id' => $check['id'],
987 $this->customFieldDelete($ids['custom_field_id']);
988 $this->customGroupDelete($ids['custom_group_id']);
992 * per CRM-15746 check that the id can be altered in an update hook
994 public function testMembershipUpdateCreateHookCRM15746() {
995 $this->hookClass
->setHook('civicrm_pre', array($this, 'hook_civicrm_pre_update_create_membership'));
996 $result = $this->callAPISuccess('membership', 'create', $this->_params
);
997 $this->callAPISuccess('membership', 'create', array('id' => $result['id'], 'end_date' => '1 year ago'));
998 $this->callAPISuccessGetCount('membership', array(), 2);
999 $this->hookClass
->reset();
1000 $this->callAPISuccess('membership', 'create', array('id' => $result['id'], 'end_date' => '1 year ago'));
1001 $this->callAPISuccessGetCount('membership', array(), 2);
1005 * Custom hook for update membership.
1008 * @param object $objectName
1010 * @param array $params
1012 * @throws \Exception
1014 public function hook_civicrm_pre_update_create_membership($op, $objectName, $id, &$params) {
1015 if ($objectName == 'Membership' && $op == 'edit') {
1016 $existingMembership = $this->callAPISuccessGetSingle('membership', array('id' => $params['id']));
1017 unset($params['id'], $params['membership_id']);
1018 $params['join_date'] = $params['membership_start_date'] = $params['start_date'] = date('Ymd000000', strtotime($existingMembership['start_date']));
1019 $params = array_merge($existingMembership, $params);
1020 $params['id'] = NULL;
1025 * Test civicrm_contact_memberships_create Invalid membership data.
1028 public function testMembershipCreateInvalidMemData() {
1029 //membership_contact_id as string
1031 'membership_contact_id' => 'Invalid',
1032 'membership_type_id' => $this->_membershipTypeID
,
1033 'join_date' => '2011-01-21',
1034 'start_date' => '2010-01-21',
1035 'end_date' => '2008-12-21',
1036 'source' => 'Payment',
1038 'status_id' => $this->_membershipStatusID
,
1041 $this->callAPIFailure('membership', 'create', $params);
1043 //membership_contact_id which is no in contact table
1044 $params['membership_contact_id'] = 999;
1045 $this->callAPIFailure('membership', 'create', $params);
1048 unset($params['membership_contact_id']);
1049 $params['join_date'] = "invalid";
1050 $this->callAPIFailure('Membership', 'Create', $params);
1054 * Test civicrm_contact_memberships_create with membership_contact_id
1058 public function testMembershipCreateWithMemContact() {
1060 'membership_contact_id' => $this->_contactID
,
1061 'membership_type_id' => $this->_membershipTypeID
,
1062 'join_date' => '2011-01-21',
1063 'start_date' => '2010-01-21',
1064 'end_date' => '2008-12-21',
1065 'source' => 'Payment',
1067 'status_id' => $this->_membershipStatusID
,
1070 $result = $this->callAPISuccess('membership', 'create', $params);
1072 $this->callAPISuccess('Membership', 'Delete', array(
1073 'id' => $result['id'],
1078 * Test civicrm_contact_memberships_create with membership_contact_id
1082 public function testMembershipCreateValidMembershipTypeString() {
1084 'membership_contact_id' => $this->_contactID
,
1085 'membership_type_id' => 'General',
1086 'join_date' => '2011-01-21',
1087 'start_date' => '2010-01-21',
1088 'end_date' => '2008-12-21',
1089 'source' => 'Payment',
1091 'status_id' => $this->_membershipStatusID
,
1094 $result = $this->callAPISuccess('membership', 'create', $params);
1095 $this->assertEquals($this->_membershipTypeID
, $result['values'][$result['id']]['membership_type_id']);
1096 $this->callAPISuccess('Membership', 'Delete', array(
1097 'id' => $result['id'],
1102 * Test civicrm_contact_memberships_create with membership_contact_id
1106 public function testMembershipCreateInValidMembershipTypeString() {
1108 'membership_contact_id' => $this->_contactID
,
1109 'membership_type_id' => 'invalid',
1110 'join_date' => '2011-01-21',
1111 'start_date' => '2010-01-21',
1112 'end_date' => '2008-12-21',
1113 'source' => 'Payment',
1115 'status_id' => $this->_membershipStatusID
,
1118 $this->callAPIFailure('membership', 'create', $params);
1122 * Test that if membership join date is not set it defaults to today.
1124 public function testEmptyJoinDate() {
1125 unset($this->_params
['join_date'], $this->_params
['is_override']);
1126 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1127 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1128 $this->assertEquals(date('Y-m-d', strtotime('now')), $result['join_date']);
1129 $this->assertEquals('2009-01-21', $result['start_date']);
1130 $this->assertEquals('2009-12-21', $result['end_date']);
1134 * Test that if membership start date is not set it defaults to correct end date.
1137 public function testEmptyStartDateFixed() {
1138 unset($this->_params
['start_date'], $this->_params
['is_override']);
1139 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1140 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1141 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1142 $this->assertEquals('2009-01-21', $result['join_date']);
1143 $this->assertEquals('2008-03-01', $result['start_date']);
1144 $this->assertEquals('2009-12-21', $result['end_date']);
1148 * Test that if membership start date is not set it defaults to correct end date
1151 public function testEmptyStartEndDateFixedOneYear() {
1152 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1153 $this->callAPISuccess('membership_type', 'create', array('id' => $this->_membershipTypeID2
, 'duration_interval' => 1));
1154 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1155 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1156 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1157 $this->assertEquals('2009-01-21', $result['join_date']);
1158 $this->assertEquals('2008-03-01', $result['start_date']);
1159 $this->assertEquals('2010-02-28', $result['end_date']);
1163 * Test that if membership start date is not set it defaults to correct end date for fixed multi year memberships.
1165 public function testEmptyStartEndDateFixedMultiYear() {
1166 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1167 $this->callAPISuccess('membership_type', 'create', array('id' => $this->_membershipTypeID2
, 'duration_interval' => 5));
1168 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1169 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1170 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1171 $this->assertEquals('2009-01-21', $result['join_date']);
1172 $this->assertEquals('2008-03-01', $result['start_date']);
1173 $this->assertEquals('2014-02-28', $result['end_date']);
1177 * CRM-18503 - Test membership join date is correctly set for fixed memberships.
1179 public function testMembershipJoinDateFixed() {
1180 $memStatus = CRM_Member_PseudoConstant
::membershipStatus();
1181 // Update the fixed membership type to 1 year duration.
1182 $this->callAPISuccess('membership_type', 'create', array('id' => $this->_membershipTypeID2
, 'duration_interval' => 1));
1183 $contactId = $this->createLoggedInUser();
1184 // Create membership with 'Pending' status.
1186 'contact_id' => $contactId,
1187 'membership_type_id' => $this->_membershipTypeID2
,
1188 'source' => 'test membership',
1189 'is_pay_later' => 0,
1190 'status_id' => array_search('Pending', $memStatus),
1191 'skipStatusCal' => 1,
1192 'is_for_organization' => 1,
1195 $membership = CRM_Member_BAO_Membership
::create($params, $ids);
1197 // Update membership to 'Completed' and check the dates.
1199 'id' => $membership->id
,
1200 'contact_id' => $contactId,
1202 'membership_type_id' => $this->_membershipTypeID2
,
1204 'status_id' => array_search('New', $memStatus),
1206 $result = $this->callAPISuccess('Membership', 'create', $memParams);
1208 // Extend duration interval if join_date exceeds the rollover period.
1209 $joinDate = date('Y-m-d');
1211 $startDate = date('Y-m-d', strtotime(date('Y-03-01')));
1213 if (strtotime($startDate) > time()) {
1215 $startDate = date('Y-m-d', strtotime(date('Y-03-01') . '- 1 year'));
1217 $membershipTypeDetails = CRM_Member_BAO_MembershipType
::getMembershipTypeDetails($this->_membershipTypeID2
);
1218 $fixedPeriodRollover = CRM_Member_BAO_MembershipType
::isDuringFixedAnnualRolloverPeriod($joinDate, $membershipTypeDetails, $year, $startDate);
1220 if ($fixedPeriodRollover && $rollOver) {
1224 $expectedDates = array(
1225 'join_date' => date('Ymd'),
1226 'start_date' => str_replace('-', '', $startDate),
1227 'end_date' => date('Ymd', strtotime(date('Y-03-01') . "+ {$y} year - 1 day")),
1229 foreach ($result['values'] as $values) {
1230 foreach ($expectedDates as $date => $val) {
1231 $this->assertEquals($val, $values[$date], "Failed asserting {$date} values");
1237 * Test correct end and start dates are calculated for fixed multi year memberships.
1239 * The empty start date is calculated to be the start_date (1 Jan prior to the join_date - so 1 Jan 15)
1241 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1242 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1243 * and we add on 4 years rather than 5 because we are not after the rollover day - so we calculate 31 Dec 2019
1245 public function testFixedMultiYearDateSetTwoEmptyStartEndDate() {
1246 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1248 $this->callAPISuccess('membership_type', 'create', array(
1249 'id' => $this->_membershipTypeID2
,
1250 'duration_interval' => 5,
1252 'fixed_period_start_day' => '101',
1254 'fixed_period_rollover_day' => '1101',
1256 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1258 'join_date' => '28-Jan 2015',
1260 $result = $this->callAPISuccess($this->_entity
, 'create', array_merge($this->_params
, $dates));
1261 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1262 $this->assertEquals('2015-01-28', $result['join_date']);
1263 $this->assertEquals('2015-01-01', $result['start_date']);
1264 $this->assertEquals('2019-12-31', $result['end_date']);
1268 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
1270 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1271 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1272 * and we add on 4 years rather than 5 because we are not after the rollover day - so we calculate 31 Dec 2019
1274 public function testFixedMultiYearDateSetTwoEmptyEndDate() {
1275 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1277 $this->callAPISuccess('membership_type', 'create', array(
1278 'id' => $this->_membershipTypeID2
,
1279 'duration_interval' => 5,
1281 'fixed_period_start_day' => '101',
1283 'fixed_period_rollover_day' => '1101',
1285 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1287 'start_date' => '28-Jan 2015',
1288 'join_date' => '28-Jan 2015',
1290 $result = $this->callAPISuccess($this->_entity
, 'create', array_merge($this->_params
, $dates));
1291 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1292 $this->assertEquals('2015-01-28', $result['join_date']);
1293 $this->assertEquals('2015-01-28', $result['start_date']);
1294 $this->assertEquals('2019-12-31', $result['end_date']);
1298 * Test correct end and start dates are calculated for fixed multi year memberships.
1300 * The empty start date is calculated to be the start_date (1 Jan prior to the join_date - so 1 Jan 15)
1302 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1303 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1304 * and we add on <1 years rather than > 1 because we are not after the rollover day - so we calculate 31 Dec 2015
1306 public function testFixedSingleYearDateSetTwoEmptyStartEndDate() {
1307 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1309 $this->callAPISuccess('membership_type', 'create', array(
1310 'id' => $this->_membershipTypeID2
,
1311 'duration_interval' => 1,
1313 'fixed_period_start_day' => '101',
1315 'fixed_period_rollover_day' => '1101',
1317 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1319 'join_date' => '28-Jan 2015',
1321 $result = $this->callAPISuccess($this->_entity
, 'create', array_merge($this->_params
, $dates));
1322 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1323 $this->assertEquals('2015-01-28', $result['join_date']);
1324 $this->assertEquals('2015-01-01', $result['start_date']);
1325 $this->assertEquals('2015-12-31', $result['end_date']);
1329 * Test correct end date for fixed single year memberships is calculated and start_date is not changed.
1331 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1332 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1333 * and we add on <1 years rather than > 1 because we are not after the rollover day - so we calculate 31 Dec 2015
1335 public function testFixedSingleYearDateSetTwoEmptyEndDate() {
1336 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1338 $this->callAPISuccess('membership_type', 'create', array(
1339 'id' => $this->_membershipTypeID2
,
1340 'duration_interval' => 1,
1342 'fixed_period_start_day' => '101',
1344 'fixed_period_rollover_day' => '1101',
1346 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1348 'start_date' => '28-Jan 2015',
1349 'join_date' => '28-Jan 2015',
1351 $result = $this->callAPISuccess($this->_entity
, 'create', array_merge($this->_params
, $dates));
1352 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1353 $this->assertEquals('2015-01-28', $result['join_date']);
1354 $this->assertEquals('2015-01-28', $result['start_date']);
1355 $this->assertEquals('2015-12-31', $result['end_date']);
1359 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
1361 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1362 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1363 * and we add on 1 year we are after the rollover day - so we calculate 31 Oct 2016
1365 public function testFixedSingleYearDateSetThreeEmptyEndDate() {
1366 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1368 $this->callAPISuccess('membership_type', 'create', array(
1369 'id' => $this->_membershipTypeID2
,
1370 'duration_interval' => 1,
1372 'fixed_period_start_day' => '1101',
1374 'fixed_period_rollover_day' => '101',
1376 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1378 'start_date' => '28-Jan 2015',
1379 'join_date' => '28-Jan 2015',
1381 $result = $this->callAPISuccess($this->_entity
, 'create', array_merge($this->_params
, $dates));
1382 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1383 $this->assertEquals('2015-01-28', $result['join_date']);
1384 $this->assertEquals('2015-01-28', $result['start_date']);
1385 $this->assertEquals('2016-10-31', $result['end_date']);
1389 * Test correct end and start dates are calculated for fixed multi year memberships.
1391 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
1393 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1394 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1395 * and we add on 1 year we are after the rollover day - so we calculate 31 Oct 2016
1397 public function testFixedSingleYearDateSetThreeEmptyStartEndDate() {
1398 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1400 $this->callAPISuccess('membership_type', 'create', array(
1401 'id' => $this->_membershipTypeID2
,
1402 'duration_interval' => 1,
1404 'fixed_period_start_day' => '1101',
1406 'fixed_period_rollover_day' => '101',
1408 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1410 'join_date' => '28-Jan 2015',
1412 $result = $this->callAPISuccess($this->_entity
, 'create', array_merge($this->_params
, $dates));
1413 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1414 $this->assertEquals('2015-01-28', $result['join_date']);
1415 $this->assertEquals('2014-11-01', $result['start_date']);
1416 $this->assertEquals('2016-10-31', $result['end_date']);
1420 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
1422 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1423 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1424 * and we add on 5 years we are after the rollover day - so we calculate 31 Oct 2020
1426 public function testFixedMultiYearDateSetThreeEmptyEndDate() {
1427 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1429 $this->callAPISuccess('membership_type', 'create', array(
1430 'id' => $this->_membershipTypeID2
,
1431 'duration_interval' => 5,
1433 'fixed_period_start_day' => '1101',
1435 'fixed_period_rollover_day' => '101',
1437 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1439 'start_date' => '28-Jan 2015',
1440 'join_date' => '28-Jan 2015',
1442 $result = $this->callAPISuccess($this->_entity
, 'create', array_merge($this->_params
, $dates));
1443 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1444 $this->assertEquals('2015-01-28', $result['join_date']);
1445 $this->assertEquals('2015-01-28', $result['start_date']);
1446 $this->assertEquals('2020-10-31', $result['end_date']);
1450 * Test correct end and start dates are calculated for fixed multi year memberships.
1452 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
1454 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
1455 * In this set our join date is after the start day and after the rollover day so we do get an extra year
1456 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1457 * and we add on 5 years we are after the rollover day - so we calculate 31 Oct 2020
1459 public function testFixedMultiYearDateSetThreeEmptyStartEndDate() {
1460 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1462 $this->callAPISuccess('membership_type', 'create', array(
1463 'id' => $this->_membershipTypeID2
,
1464 'duration_interval' => 5,
1466 'fixed_period_start_day' => '1101',
1468 'fixed_period_rollover_day' => '101',
1470 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1472 'join_date' => '28-Jan 2015',
1474 $result = $this->callAPISuccess($this->_entity
, 'create', array_merge($this->_params
, $dates));
1475 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1476 $this->assertEquals('2015-01-28', $result['join_date']);
1477 $this->assertEquals('2014-11-01', $result['start_date']);
1478 $this->assertEquals('2020-10-31', $result['end_date']);
1482 * Test that if membership start date is not set it defaults to correct end date for fixed single year memberships.
1484 public function testEmptyStartDateRolling() {
1485 unset($this->_params
['start_date'], $this->_params
['is_override']);
1486 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1487 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1488 $this->assertEquals('2009-01-21', $result['join_date']);
1489 $this->assertEquals('2009-01-21', $result['start_date']);
1490 $this->assertEquals('2009-12-21', $result['end_date']);
1494 * Test that if membership end date is not set it defaults to correct end date.
1497 public function testEmptyEndDateFixed() {
1498 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1499 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1500 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1501 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1502 $this->assertEquals('2009-01-21', $result['join_date']);
1503 $this->assertEquals('2008-03-01', $result['start_date']);
1504 $this->assertEquals('2010-02-28', $result['end_date']);
1508 * Test that if membership end date is not set it defaults to correct end date.
1511 public function testEmptyEndDateRolling() {
1512 unset($this->_params
['is_override'], $this->_params
['end_date']);
1513 $this->_params
['membership_type_id'] = $this->_membershipTypeID
;
1514 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1515 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1516 $this->assertEquals('2009-01-21', $result['join_date']);
1517 $this->assertEquals('2009-01-21', $result['start_date']);
1518 $this->assertEquals('2010-01-20', $result['end_date']);
1523 * Test that if dates are set they not over-ridden if id is passed in
1525 public function testMembershipDatesNotOverridden() {
1526 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1527 unset($this->_params
['end_date'], $this->_params
['start_date']);
1528 $this->_params
['id'] = $result['id'];
1529 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1530 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1531 $this->assertEquals('2009-01-21', $result['join_date']);
1532 $this->assertEquals('2009-01-21', $result['start_date']);
1533 $this->assertEquals('2009-12-21', $result['end_date']);
1538 * Test that all membership types are returned when getoptions is called.
1540 * This test locks in current behaviour where types for all domains are returned. It should possibly be domain
1541 * specific but that should only be done in conjunction with adding a hook to allow that to be altered as the
1542 * multisite use case expects the master domain to be able to see all sites.
1546 public function testGetOptionsMembershipTypeID() {
1547 $options = $this->callAPISuccess('Membership', 'getoptions', array('field' => 'membership_type_id'));
1548 $this->assertEquals('Another one', array_pop($options['values']));
1549 $this->assertEquals('General', array_pop($options['values']));
1550 $this->assertEquals(NULL, array_pop($options['values']));