3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 'contact_id' => $contactId,
194 'payment_instrument_id' => array_search('Check', $this->paymentInstruments
),
195 'contribution_status_id' => 3,
197 CRM_Core_Action
::UPDATE
);
199 $activity = $this->callAPISuccess('Activity', 'get', array(
200 'activity_type_id' => "Change Membership Status",
201 'source_record_id' => $membershipID,
203 $this->assertNotEmpty($activity['values']);
207 * Test membership get.
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
));
216 * Test civicrm_membership_get with params not array.
218 * Gets treated as contact_id, memberships expected.
220 public function testGetWithParamsContactId() {
221 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
223 'contact_id' => $this->_contactID
,
225 $membership = $this->callAPISuccess('membership', 'get', $params);
227 $result = $membership['values'][$this->_membershipID
];
228 $this->callAPISuccess('Membership', 'Delete', array(
229 'id' => $this->_membershipID
,
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__
);
242 * Test civicrm_membership_get with params not array.
244 * Gets treated as contact_id, memberships expected.
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
);
251 'id' => array('IN' => array($this->_membershipID
, $this->_membershipID3
)),
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']));
257 'id' => array('NOT IN' => array($this->_membershipID
, $this->_membershipID3
)),
259 $membership = $this->callAPISuccess('membership', 'get', $params);
260 $this->assertEquals(1, $membership['count']);
261 $this->assertEquals(array($this->_membershipID2
), array_keys($membership['values']));
265 * Test civicrm_membership_get with params not array.
266 * Gets treated as contact_id, memberships expected.
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)));
275 'contact_id' => array('IN' => array($this->_contactID
, $contact3)),
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']));
281 'contact_id' => array('NOT IN' => array($this->_contactID
, $contact3)),
283 $membership = $this->callAPISuccess('membership', 'get', $params);
284 $this->assertEquals(1, $membership['count']);
285 $this->assertEquals(array($this->_membershipID2
), array_keys($membership['values']));
289 * Test civicrm_membership_get with params not array.
291 * Gets treated as contact_id, memberships expected.
293 public function testGetWithParamsMemberShipTypeId() {
294 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
296 'membership_type_id' => $this->_membershipTypeID
,
298 $membership = $this->callAPISuccess('membership', 'get', $params);
299 $this->callAPISuccess('Membership', 'Delete', array(
300 'id' => $membership['id'],
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']);
315 * Test civicrm_membership_get with params not array.
316 * Gets treated as contact_id, memberships expected.
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);
325 'membership_type_id' => $this->_membershipTypeID
,
326 'contact_id' => $this->_contactID
,
328 $result = $this->callAPISuccess('membership', 'getsingle', $params);
329 $this->assertEquals($result['contact_id'], $this->_contactID
);
330 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID
);
333 'membership_type_id' => $this->_membershipTypeID2
,
334 'contact_id' => $this->_contactID
,
336 $result = $this->callAPISuccess('membership', 'getsingle', $params);
337 $this->assertEquals($result['contact_id'], $this->_contactID
);
338 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID2
);
342 * Check with complete array + custom field.
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
348 public function testGetWithParamsMemberShipIdAndCustom() {
349 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
351 $params = $this->_params
;
352 $params['custom_' . $ids['custom_field_id']] = "custom string";
354 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
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__
);
360 $this->callAPISuccess('Membership', 'Delete', array(
361 'id' => $result['id'],
366 * Test civicrm_membership_get with proper params.
367 * Memberships expected.
369 public function testGet() {
370 $membershipID = $this->contactMembershipCreate($this->_params
);
372 'contact_id' => $this->_contactID
,
375 $membership = $this->callAPISuccess('membership', 'get', $params);
376 $result = $membership['values'][$membershipID];
377 $this->callAPISuccess('Membership', 'Delete', array(
378 'id' => $membership['id'],
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
);
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);
393 * Test civicrm_membership_get with proper params.
394 * Memberships expected.
396 public function testGetWithId() {
397 $membershipID = $this->contactMembershipCreate($this->_params
);
399 'contact_id' => $this->_contactID
,
400 'id' => $this->_membershipID
,
403 $result = $this->callAPISuccess('membership', 'get', $params);
404 $this->assertEquals($membershipID, $result['id']);
406 'contact_id' => $this->_contactID
,
407 'membership_id' => $this->_membershipID
,
408 'return' => 'membership_id',
410 $result = $this->callAPISuccess('membership', 'get', $params);
411 $this->assertEquals($membershipID, $result['id']);
415 * Test civicrm_membership_get for only active.
416 * Memberships expected.
418 public function testGetOnlyActive() {
419 $description = "Demonstrates use of 'filter' active_only' param.";
420 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
422 'contact_id' => $this->_contactID
,
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
);
430 'contact_id' => $this->_contactID
,
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
);
440 $this->callAPISuccess('Membership', 'Delete', array('id' => $this->_membershipID
));
444 * Test civicrm_membership_get for non exist contact.
447 public function testGetNoContactExists() {
449 'contact_id' => 55555,
452 $membership = $this->callAPISuccess('membership', 'get', $params);
453 $this->assertEquals($membership['count'], 0);
457 * Test civicrm_membership_get with relationship.
460 public function testGetWithRelationship() {
461 $membershipOrgId = $this->organizationCreate(NULL);
462 $memberContactId = $this->individualCreate();
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',
473 $relTypeID = $this->relationshipTypeCreate($relTypeParams);
476 'name' => 'test General',
477 'duration_unit' => 'year',
478 'duration_interval' => 1,
479 'period_type' => 'rolling',
480 'member_of_contact_id' => $membershipOrgId,
482 'financial_type_id' => 1,
483 'relationship_type_id' => $relTypeID,
484 'relationship_direction' => 'b_a',
487 $memType = $this->callAPISuccess('membership_type', 'create', $params);
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',
497 'status_id' => $this->_membershipStatusID
,
499 $membershipID = $this->contactMembershipCreate($params);
502 'contact_id' => $memberContactId,
503 'membership_type_id' => $memType['id'],
506 $result = $this->callAPISuccess('membership', 'get', $params);
508 $membership = $result['values'][$membershipID];
509 $this->assertEquals($this->_membershipStatusID
, $membership['status_id']);
510 $this->callAPISuccess('Membership', 'Delete', array(
511 'id' => $membership['id'],
513 $this->membershipTypeDelete(array('id' => $memType['id']));
514 $this->relationshipTypeDelete($relTypeID);
515 $this->contactDelete($membershipOrgId);
516 $this->contactDelete($memberContactId);
520 * Test civicrm_membership_create with relationships.
521 * create/get Memberships.
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
526 public function testCreateWithRelationship() {
527 // Create membership type: inherited through employment, max_related = 2
529 'name_a_b' => 'Employee of',
531 $result = $this->callAPISuccess('relationship_type', 'get', $params);
532 $relationshipTypeId = $result['id'];
533 $membershipOrgId = $this->organizationCreate();
535 'name' => 'Corporate Membership',
536 'duration_unit' => 'year',
537 'duration_interval' => 1,
538 'period_type' => 'rolling',
539 'member_of_contact_id' => $membershipOrgId,
541 'financial_type_id' => 1,
542 'relationship_type_id' => $relationshipTypeId,
543 'relationship_direction' => 'b_a',
547 $result = $this->callAPISuccess('membership_type', 'create', $params);
548 $membershipTypeId = $result['id'];
550 // Create employer and first employee
551 $employerId[0] = $this->organizationCreate(array(), 1);
552 $memberContactId[0] = $this->individualCreate(array('employer_id' => $employerId[0]), 0);
554 // Create organization's membership
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",
562 $OrganizationMembershipID = $this->contactMembershipCreate($params);
564 // Check that the employee inherited the membership
566 'contact_id' => $memberContactId[0],
567 'membership_type_id' => $membershipTypeId,
570 $result = $this->callAPISuccess('membership', 'get', $params);
572 $this->assertEquals(1, $result['count']);
573 $result = $result['values'][$result['id']];
574 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
576 // Create second employee
577 $memberContactId[1] = $this->individualCreate(array('employer_id' => $employerId[0]), 1);
579 // Check that the employee inherited the membership
581 'contact_id' => $memberContactId[1],
582 'membership_type_id' => $membershipTypeId,
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']);
590 // Create third employee
591 $memberContactId[2] = $this->individualCreate(array('employer_id' => $employerId[0]), 2);
593 // Check that employee does NOT inherit the membership (max_related = 2)
595 'contact_id' => $memberContactId[2],
596 'membership_type_id' => $membershipTypeId,
598 $result = $this->callAPISuccess('membership', 'get', $params);
599 $this->assertEquals(0, $result['count']);
601 // Increase max_related for the employer's membership
603 'id' => $OrganizationMembershipID,
606 $this->callAPISuccess('Membership', 'create', $params);
608 // Check that the employee inherited the membership
610 'contact_id' => $memberContactId[2],
611 'membership_type_id' => $membershipTypeId,
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']);
618 // First employee moves to a new job
619 $employerId[1] = $this->organizationCreate(array(), 2);
621 'id' => $memberContactId[0],
622 'employer_id' => $employerId[1],
624 $this->callAPISuccess('contact', 'create', $params);
626 // Check that employee does NO LONGER inherit the membership
628 'contact_id' => $memberContactId[0],
629 'membership_type_id' => $membershipTypeId,
631 $result = $this->callAPISuccess('membership', 'get', $params);
632 $this->assertEquals(0, $result['count']);
634 // Set up params for enable/disable checks
635 $relationship1 = $this->callAPISuccess('relationship', 'get', array('contact_id_a' => $memberContactId[1]));
637 'contact_id' => $memberContactId[1],
638 'membership_type_id' => $membershipTypeId,
641 // Deactivate relationship using create and assert membership is not inherited
642 $this->callAPISuccess('relationship', 'create', array('id' => $relationship1['id'], 'is_active' => 0));
643 $result = $this->callAPISuccess('membership', 'get', $params);
644 $this->assertEquals(0, $result['count']);
646 // Re-enable relationship using create and assert membership is inherited
647 $this->callAPISuccess('relationship', 'create', array('id' => $relationship1['id'], 'is_active' => 1));
648 $result = $this->callAPISuccess('membership', 'get', $params);
649 $this->assertEquals(1, $result['count']);
651 // Deactivate relationship using setvalue and assert membership is not inherited
652 $this->callAPISuccess('relationship', 'setvalue', array('id' => $relationship1['id'], 'field' => 'is_active', 'value' => 0));
653 $result = $this->callAPISuccess('membership', 'get', $params);
654 $this->assertEquals(0, $result['count']);
656 // Re-enable relationship using setvalue and assert membership is inherited
657 $this->callAPISuccess('relationship', 'setvalue', array('id' => $relationship1['id'], 'field' => 'is_active', 'value' => 1));
658 $result = $this->callAPISuccess('membership', 'get', $params);
659 $this->assertEquals(1, $result['count']);
661 // Delete relationship and assert membership is not inherited
662 $this->callAPISuccess('relationship', 'delete', array('id' => $relationship1['id']));
663 $result = $this->callAPISuccess('membership', 'get', $params);
664 $this->assertEquals(0, $result['count']);
666 // Tear down - reverse of creation to be safe
667 $this->contactDelete($memberContactId[2]);
668 $this->contactDelete($memberContactId[1]);
669 $this->contactDelete($memberContactId[0]);
670 $this->contactDelete($employerId[1]);
671 $this->contactDelete($employerId[0]);
672 $this->membershipTypeDelete(array('id' => $membershipTypeId));
673 $this->contactDelete($membershipOrgId);
677 * We are checking for no e-notices + only id & end_date returned
679 public function testMembershipGetWithReturn() {
680 $this->contactMembershipCreate($this->_params
);
681 $result = $this->callAPISuccess('membership', 'get', array('return' => 'end_date'));
682 foreach ($result['values'] as $membership) {
683 $this->assertEquals(array('id', 'end_date'), array_keys($membership));
686 ///////////////// civicrm_membership_create methods
689 * Test civicrm_contact_memberships_create with empty params.
692 public function testCreateWithEmptyParams() {
694 $this->callAPIFailure('membership', 'create', $params);
698 * If is_overide is passed in status must also be passed in.
700 public function testCreateOverrideNoStatus() {
701 $params = $this->_params
;
702 unset($params['status_id']);
703 $this->callAPIFailure('membership', 'create', $params);
706 public function testMembershipCreateMissingRequired() {
708 'membership_type_id' => '1',
709 'join_date' => '2006-01-21',
710 'start_date' => '2006-01-21',
711 'end_date' => '2006-12-21',
712 'source' => 'Payment',
716 $this->callAPIFailure('membership', 'create', $params);
719 public function testMembershipCreate() {
721 'contact_id' => $this->_contactID
,
722 'membership_type_id' => $this->_membershipTypeID
,
723 'join_date' => '2006-01-21',
724 'start_date' => '2006-01-21',
725 'end_date' => '2006-12-21',
726 'source' => 'Payment',
728 'status_id' => $this->_membershipStatusID
,
731 $result = $this->callAPIAndDocument('membership', 'create', $params, __FUNCTION__
, __FILE__
);
732 $this->getAndCheck($params, $result['id'], $this->_entity
);
733 $this->assertNotNull($result['id']);
734 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['contact_id'], " in line " . __LINE__
);
735 $this->assertEquals($result['id'], $result['values'][$result['id']]['id'], " in line " . __LINE__
);
739 * Check for useful message if contact doesn't exist
741 public function testMembershipCreateWithInvalidContact() {
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',
750 'status_id' => $this->_membershipStatusID
,
753 $this->callAPIFailure('membership', 'create', $params,
754 'contact_id is not valid : 999'
758 public function testMembershipCreateWithInvalidStatus() {
759 $params = $this->_params
;
760 $params['status_id'] = 999;
761 $this->callAPIFailure('membership', 'create', $params,
762 "'999' is not a valid option for field status_id"
766 public function testMembershipCreateWithInvalidType() {
767 $params = $this->_params
;
768 $params['membership_type_id'] = 999;
770 $this->callAPIFailure('membership', 'create', $params,
771 "'999' is not a valid option for field membership_type_id"
776 * Check with complete array + custom field
777 * Note that the test is written on purpose without any
778 * variables specific to participant so it can be replicated into other entities
779 * and / or moved to the automated test suite
781 public function testCreateWithCustom() {
782 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
784 $params = $this->_params
;
785 $params['custom_' . $ids['custom_field_id']] = "custom string";
787 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
, NULL, 'CreateWithCustomData');
788 $check = $this->callAPISuccess($this->_entity
, 'get', array(
789 'id' => $result['id'],
790 'contact_id' => $this->_contactID
,
792 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
796 * Search on custom field value.
798 public function testSearchWithCustomDataCRM16036() {
799 // Create a custom field on membership
800 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
802 // Create a new membership, but don't assign anything to the custom field.
803 $params = $this->_params
;
804 $result = $this->callAPIAndDocument(
811 'SearchWithCustomData');
813 // search memberships with CRM-16036 as custom field value.
814 // Since we did not touch the custom field of any membership,
815 // this should not return any results.
816 $check = $this->callAPISuccess($this->_entity
, 'get', array(
817 'custom_' . $ids['custom_field_id'] => "CRM-16036",
821 $this->callAPISuccess($this->_entity
, 'delete', array(
822 'id' => $result['id'],
826 $this->assertEquals(0, $check['count']);
830 * Test civicrm_contact_memberships_create with membership id (edit
834 public function testMembershipCreateWithId() {
835 $membershipID = $this->contactMembershipCreate($this->_params
);
837 'id' => $membershipID,
838 'contact_id' => $this->_contactID
,
839 'membership_type_id' => $this->_membershipTypeID
,
840 'join_date' => '2006-01-21',
841 'start_date' => '2006-01-21',
842 'end_date' => '2006-12-21',
843 'source' => 'Payment',
845 'status_id' => $this->_membershipStatusID
,
848 $result = $this->callAPISuccess('membership', 'create', $params);
850 //Update Status and check activities created.
851 $updateStatus = array(
852 'id' => $result['id'],
853 'status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Member_BAO_Membership', 'status_id', 'Cancelled'),
855 $this->callAPISuccess('Membership', 'create', $updateStatus);
856 $activities = CRM_Activity_BAO_Activity
::getContactActivity($this->_contactID
);
857 $this->assertEquals(2, count($activities));
858 $activityNames = array_flip(CRM_Utils_Array
::collect('activity_name', $activities));
859 $this->assertArrayHasKey('Membership Signup', $activityNames);
860 $this->assertArrayHasKey('Change Membership Status', $activityNames);
862 $this->callAPISuccess('Membership', 'Delete', array(
863 'id' => $result['id'],
865 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
869 * Test civicrm_contact_memberships_create with membership id (edit
873 public function testMembershipCreateUpdateWithIdNoContact() {
874 $membershipID = $this->contactMembershipCreate($this->_params
);
876 'id' => $membershipID,
877 'membership_type_id' => $this->_membershipTypeID
,
878 'contact_id' => $this->_contactID
,
879 'join_date' => '2006-01-21',
880 'start_date' => '2006-01-21',
881 'end_date' => '2006-12-21',
882 'source' => 'Payment',
884 'status_id' => $this->_membershipStatusID
,
887 $result = $this->callAPISuccess('membership', 'create', $params);
888 $this->callAPISuccess('Membership', 'Delete', array(
889 'id' => $result['id'],
892 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
896 * Test civicrm_contact_memberships_create with membership id (edit
900 public function testMembershipCreateUpdateWithIdNoDates() {
901 $membershipID = $this->contactMembershipCreate($this->_params
);
903 'id' => $membershipID,
904 'contact_id' => $this->_contactID
,
905 'membership_type_id' => $this->_membershipTypeID
,
906 'source' => 'Payment',
908 'status_id' => $this->_membershipStatusID
,
911 $result = $this->callAPISuccess('membership', 'create', $params);
912 $this->callAPISuccess('Membership', 'Delete', array(
913 'id' => $result['id'],
915 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
919 * Test civicrm_contact_memberships_create with membership id (edit
923 public function testMembershipCreateUpdateWithIdNoDatesNoType() {
924 $membershipID = $this->contactMembershipCreate($this->_params
);
926 'id' => $membershipID,
927 'source' => 'not much here',
928 'contact_id' => $this->_contactID
,
930 'status_id' => $this->_membershipStatusID
,
933 $result = $this->callAPISuccess('membership', 'create', $params);
934 $this->callAPISuccess('Membership', 'Delete', array(
935 'id' => $result['id'],
937 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
941 * Test civicrm_contact_memberships_create with membership id (edit
945 public function testMembershipCreateUpdateWithIDAndSource() {
946 $membershipID = $this->contactMembershipCreate($this->_params
);
948 'id' => $membershipID,
949 'source' => 'changed',
950 'contact_id' => $this->_contactID
,
951 'status_id' => $this->_membershipStatusID
,
952 'membership_type_id' => $this->_membershipTypeID
,
953 'skipStatusCal' => 1,
955 $result = $this->callAPISuccess('membership', 'create', $params);
956 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
957 $this->callAPISuccess('Membership', 'Delete', array(
958 'id' => $result['id'],
963 * Change custom field using update.
965 public function testUpdateWithCustom() {
966 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
968 $params = $this->_params
;
969 $params['custom_' . $ids['custom_field_id']] = "custom string";
970 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
, NULL, 'UpdateCustomData');
971 $result = $this->callAPISuccess($this->_entity
, 'create', array(
972 'id' => $result['id'],
973 'custom_' . $ids['custom_field_id'] => "new custom",
975 $check = $this->callAPISuccess($this->_entity
, 'get', array(
976 'id' => $result['id'],
977 'contact_id' => $this->_contactID
,
980 $this->assertEquals("new custom", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
981 $this->callAPISuccess('Membership', 'Delete', array(
982 'id' => $check['id'],
985 $this->customFieldDelete($ids['custom_field_id']);
986 $this->customGroupDelete($ids['custom_group_id']);
990 * per CRM-15746 check that the id can be altered in an update hook
992 public function testMembershipUpdateCreateHookCRM15746() {
993 $this->hookClass
->setHook('civicrm_pre', array($this, 'hook_civicrm_pre_update_create_membership'));
994 $result = $this->callAPISuccess('membership', 'create', $this->_params
);
995 $this->callAPISuccess('membership', 'create', array('id' => $result['id'], 'end_date' => '1 year ago'));
996 $this->callAPISuccessGetCount('membership', array(), 2);
997 $this->hookClass
->reset();
998 $this->callAPISuccess('membership', 'create', array('id' => $result['id'], 'end_date' => '1 year ago'));
999 $this->callAPISuccessGetCount('membership', array(), 2);
1003 * Custom hook for update membership.
1006 * @param object $objectName
1008 * @param array $params
1010 * @throws \Exception
1012 public function hook_civicrm_pre_update_create_membership($op, $objectName, $id, &$params) {
1013 if ($objectName == 'Membership' && $op == 'edit') {
1014 $existingMembership = $this->callAPISuccessGetSingle('membership', array('id' => $params['id']));
1015 unset($params['id'], $params['membership_id']);
1016 $params['join_date'] = $params['membership_start_date'] = $params['start_date'] = date('Ymd000000', strtotime($existingMembership['start_date']));
1017 $params = array_merge($existingMembership, $params);
1018 $params['id'] = NULL;
1023 * Test civicrm_contact_memberships_create Invalid membership data.
1026 public function testMembershipCreateInvalidMemData() {
1027 //membership_contact_id as string
1029 'membership_contact_id' => 'Invalid',
1030 'membership_type_id' => $this->_membershipTypeID
,
1031 'join_date' => '2011-01-21',
1032 'start_date' => '2010-01-21',
1033 'end_date' => '2008-12-21',
1034 'source' => 'Payment',
1036 'status_id' => $this->_membershipStatusID
,
1039 $this->callAPIFailure('membership', 'create', $params);
1041 //membership_contact_id which is no in contact table
1042 $params['membership_contact_id'] = 999;
1043 $this->callAPIFailure('membership', 'create', $params);
1046 unset($params['membership_contact_id']);
1047 $params['join_date'] = "invalid";
1048 $this->callAPIFailure('Membership', 'Create', $params);
1052 * Test civicrm_contact_memberships_create with membership_contact_id
1056 public function testMembershipCreateWithMemContact() {
1058 'membership_contact_id' => $this->_contactID
,
1059 'membership_type_id' => $this->_membershipTypeID
,
1060 'join_date' => '2011-01-21',
1061 'start_date' => '2010-01-21',
1062 'end_date' => '2008-12-21',
1063 'source' => 'Payment',
1065 'status_id' => $this->_membershipStatusID
,
1068 $result = $this->callAPISuccess('membership', 'create', $params);
1070 $this->callAPISuccess('Membership', 'Delete', array(
1071 'id' => $result['id'],
1076 * Test civicrm_contact_memberships_create with membership_contact_id
1080 public function testMembershipCreateValidMembershipTypeString() {
1082 'membership_contact_id' => $this->_contactID
,
1083 'membership_type_id' => 'General',
1084 'join_date' => '2011-01-21',
1085 'start_date' => '2010-01-21',
1086 'end_date' => '2008-12-21',
1087 'source' => 'Payment',
1089 'status_id' => $this->_membershipStatusID
,
1092 $result = $this->callAPISuccess('membership', 'create', $params);
1093 $this->assertEquals($this->_membershipTypeID
, $result['values'][$result['id']]['membership_type_id']);
1094 $this->callAPISuccess('Membership', 'Delete', array(
1095 'id' => $result['id'],
1100 * Test civicrm_contact_memberships_create with membership_contact_id
1104 public function testMembershipCreateInValidMembershipTypeString() {
1106 'membership_contact_id' => $this->_contactID
,
1107 'membership_type_id' => 'invalid',
1108 'join_date' => '2011-01-21',
1109 'start_date' => '2010-01-21',
1110 'end_date' => '2008-12-21',
1111 'source' => 'Payment',
1113 'status_id' => $this->_membershipStatusID
,
1116 $this->callAPIFailure('membership', 'create', $params);
1120 * Test that if membership join date is not set it defaults to today.
1122 public function testEmptyJoinDate() {
1123 unset($this->_params
['join_date'], $this->_params
['is_override']);
1124 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1125 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1126 $this->assertEquals(date('Y-m-d', strtotime('now')), $result['join_date']);
1127 $this->assertEquals('2009-01-21', $result['start_date']);
1128 $this->assertEquals('2009-12-21', $result['end_date']);
1132 * Test that if membership start date is not set it defaults to correct end date.
1135 public function testEmptyStartDateFixed() {
1136 unset($this->_params
['start_date'], $this->_params
['is_override']);
1137 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1138 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1139 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1140 $this->assertEquals('2009-01-21', $result['join_date']);
1141 $this->assertEquals('2008-03-01', $result['start_date']);
1142 $this->assertEquals('2009-12-21', $result['end_date']);
1146 * Test that if membership start date is not set it defaults to correct end date
1149 public function testEmptyStartEndDateFixedOneYear() {
1150 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1151 $this->callAPISuccess('membership_type', 'create', array('id' => $this->_membershipTypeID2
, 'duration_interval' => 1));
1152 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1153 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1154 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1155 $this->assertEquals('2009-01-21', $result['join_date']);
1156 $this->assertEquals('2008-03-01', $result['start_date']);
1157 $this->assertEquals('2010-02-28', $result['end_date']);
1161 * Test that if membership start date is not set it defaults to correct end date for fixed multi year memberships.
1163 public function testEmptyStartEndDateFixedMultiYear() {
1164 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1165 $this->callAPISuccess('membership_type', 'create', array('id' => $this->_membershipTypeID2
, 'duration_interval' => 5));
1166 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1167 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1168 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1169 $this->assertEquals('2009-01-21', $result['join_date']);
1170 $this->assertEquals('2008-03-01', $result['start_date']);
1171 $this->assertEquals('2014-02-28', $result['end_date']);
1175 * CRM-18503 - Test membership join date is correctly set for fixed memberships.
1177 public function testMembershipJoinDateFixed() {
1178 $memStatus = CRM_Member_PseudoConstant
::membershipStatus();
1179 // Update the fixed membership type to 1 year duration.
1180 $this->callAPISuccess('membership_type', 'create', array('id' => $this->_membershipTypeID2
, 'duration_interval' => 1));
1181 $contactId = $this->createLoggedInUser();
1182 // Create membership with 'Pending' status.
1184 'contact_id' => $contactId,
1185 'membership_type_id' => $this->_membershipTypeID2
,
1186 'source' => 'test membership',
1187 'is_pay_later' => 0,
1188 'status_id' => array_search('Pending', $memStatus),
1189 'skipStatusCal' => 1,
1190 'is_for_organization' => 1,
1193 $membership = CRM_Member_BAO_Membership
::create($params, $ids);
1195 // Update membership to 'Completed' and check the dates.
1197 'id' => $membership->id
,
1198 'contact_id' => $contactId,
1200 'membership_type_id' => $this->_membershipTypeID2
,
1202 'status_id' => array_search('New', $memStatus),
1204 $result = $this->callAPISuccess('Membership', 'create', $memParams);
1206 // Extend duration interval if join_date exceeds the rollover period.
1207 $joinDate = date('Y-m-d');
1209 $startDate = date('Y-m-d', strtotime(date('Y-03-01')));
1211 if (strtotime($startDate) > time()) {
1213 $startDate = date('Y-m-d', strtotime(date('Y-03-01') . '- 1 year'));
1215 $membershipTypeDetails = CRM_Member_BAO_MembershipType
::getMembershipTypeDetails($this->_membershipTypeID2
);
1216 $fixedPeriodRollover = CRM_Member_BAO_MembershipType
::isDuringFixedAnnualRolloverPeriod($joinDate, $membershipTypeDetails, $year, $startDate);
1218 if ($fixedPeriodRollover && $rollOver) {
1222 $expectedDates = array(
1223 'join_date' => date('Ymd'),
1224 'start_date' => str_replace('-', '', $startDate),
1225 'end_date' => date('Ymd', strtotime(date('Y-03-01') . "+ {$y} year - 1 day")),
1227 foreach ($result['values'] as $values) {
1228 foreach ($expectedDates as $date => $val) {
1229 $this->assertEquals($val, $values[$date], "Failed asserting {$date} values");
1235 * Test correct end and start dates are calculated for fixed multi year memberships.
1237 * The empty start date is calculated to be the start_date (1 Jan prior to the join_date - so 1 Jan 15)
1239 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1240 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1241 * and we add on 4 years rather than 5 because we are not after the rollover day - so we calculate 31 Dec 2019
1243 public function testFixedMultiYearDateSetTwoEmptyStartEndDate() {
1244 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1246 $this->callAPISuccess('membership_type', 'create', array(
1247 'id' => $this->_membershipTypeID2
,
1248 'duration_interval' => 5,
1250 'fixed_period_start_day' => '101',
1252 'fixed_period_rollover_day' => '1101',
1254 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1256 'join_date' => '28-Jan 2015',
1258 $result = $this->callAPISuccess($this->_entity
, 'create', array_merge($this->_params
, $dates));
1259 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1260 $this->assertEquals('2015-01-28', $result['join_date']);
1261 $this->assertEquals('2015-01-01', $result['start_date']);
1262 $this->assertEquals('2019-12-31', $result['end_date']);
1266 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
1268 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1269 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1270 * and we add on 4 years rather than 5 because we are not after the rollover day - so we calculate 31 Dec 2019
1272 public function testFixedMultiYearDateSetTwoEmptyEndDate() {
1273 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1275 $this->callAPISuccess('membership_type', 'create', array(
1276 'id' => $this->_membershipTypeID2
,
1277 'duration_interval' => 5,
1279 'fixed_period_start_day' => '101',
1281 'fixed_period_rollover_day' => '1101',
1283 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1285 'start_date' => '28-Jan 2015',
1286 'join_date' => '28-Jan 2015',
1288 $result = $this->callAPISuccess($this->_entity
, 'create', array_merge($this->_params
, $dates));
1289 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1290 $this->assertEquals('2015-01-28', $result['join_date']);
1291 $this->assertEquals('2015-01-28', $result['start_date']);
1292 $this->assertEquals('2019-12-31', $result['end_date']);
1296 * Test correct end and start dates are calculated for fixed multi year memberships.
1298 * The empty start date is calculated to be the start_date (1 Jan prior to the join_date - so 1 Jan 15)
1300 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1301 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1302 * and we add on <1 years rather than > 1 because we are not after the rollover day - so we calculate 31 Dec 2015
1304 public function testFixedSingleYearDateSetTwoEmptyStartEndDate() {
1305 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1307 $this->callAPISuccess('membership_type', 'create', array(
1308 'id' => $this->_membershipTypeID2
,
1309 'duration_interval' => 1,
1311 'fixed_period_start_day' => '101',
1313 'fixed_period_rollover_day' => '1101',
1315 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1317 'join_date' => '28-Jan 2015',
1319 $result = $this->callAPISuccess($this->_entity
, 'create', array_merge($this->_params
, $dates));
1320 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1321 $this->assertEquals('2015-01-28', $result['join_date']);
1322 $this->assertEquals('2015-01-01', $result['start_date']);
1323 $this->assertEquals('2015-12-31', $result['end_date']);
1327 * Test correct end date for fixed single year memberships is calculated and start_date is not changed.
1329 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1330 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1331 * and we add on <1 years rather than > 1 because we are not after the rollover day - so we calculate 31 Dec 2015
1333 public function testFixedSingleYearDateSetTwoEmptyEndDate() {
1334 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1336 $this->callAPISuccess('membership_type', 'create', array(
1337 'id' => $this->_membershipTypeID2
,
1338 'duration_interval' => 1,
1340 'fixed_period_start_day' => '101',
1342 'fixed_period_rollover_day' => '1101',
1344 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1346 'start_date' => '28-Jan 2015',
1347 'join_date' => '28-Jan 2015',
1349 $result = $this->callAPISuccess($this->_entity
, 'create', array_merge($this->_params
, $dates));
1350 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1351 $this->assertEquals('2015-01-28', $result['join_date']);
1352 $this->assertEquals('2015-01-28', $result['start_date']);
1353 $this->assertEquals('2015-12-31', $result['end_date']);
1357 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
1359 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1360 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1361 * and we add on 1 year we are after the rollover day - so we calculate 31 Oct 2016
1363 public function testFixedSingleYearDateSetThreeEmptyEndDate() {
1364 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1366 $this->callAPISuccess('membership_type', 'create', array(
1367 'id' => $this->_membershipTypeID2
,
1368 'duration_interval' => 1,
1370 'fixed_period_start_day' => '1101',
1372 'fixed_period_rollover_day' => '101',
1374 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1376 'start_date' => '28-Jan 2015',
1377 'join_date' => '28-Jan 2015',
1379 $result = $this->callAPISuccess($this->_entity
, 'create', array_merge($this->_params
, $dates));
1380 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1381 $this->assertEquals('2015-01-28', $result['join_date']);
1382 $this->assertEquals('2015-01-28', $result['start_date']);
1383 $this->assertEquals('2016-10-31', $result['end_date']);
1387 * Test correct end and start dates are calculated for fixed multi year memberships.
1389 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
1391 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1392 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1393 * and we add on 1 year we are after the rollover day - so we calculate 31 Oct 2016
1395 public function testFixedSingleYearDateSetThreeEmptyStartEndDate() {
1396 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1398 $this->callAPISuccess('membership_type', 'create', array(
1399 'id' => $this->_membershipTypeID2
,
1400 'duration_interval' => 1,
1402 'fixed_period_start_day' => '1101',
1404 'fixed_period_rollover_day' => '101',
1406 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1408 'join_date' => '28-Jan 2015',
1410 $result = $this->callAPISuccess($this->_entity
, 'create', array_merge($this->_params
, $dates));
1411 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1412 $this->assertEquals('2015-01-28', $result['join_date']);
1413 $this->assertEquals('2014-11-01', $result['start_date']);
1414 $this->assertEquals('2016-10-31', $result['end_date']);
1418 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
1420 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1421 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1422 * and we add on 5 years we are after the rollover day - so we calculate 31 Oct 2020
1424 public function testFixedMultiYearDateSetThreeEmptyEndDate() {
1425 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1427 $this->callAPISuccess('membership_type', 'create', array(
1428 'id' => $this->_membershipTypeID2
,
1429 'duration_interval' => 5,
1431 'fixed_period_start_day' => '1101',
1433 'fixed_period_rollover_day' => '101',
1435 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1437 'start_date' => '28-Jan 2015',
1438 'join_date' => '28-Jan 2015',
1440 $result = $this->callAPISuccess($this->_entity
, 'create', array_merge($this->_params
, $dates));
1441 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1442 $this->assertEquals('2015-01-28', $result['join_date']);
1443 $this->assertEquals('2015-01-28', $result['start_date']);
1444 $this->assertEquals('2020-10-31', $result['end_date']);
1448 * Test correct end and start dates are calculated for fixed multi year memberships.
1450 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
1452 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
1453 * In this set our join date is after the start day and after the rollover day so we do get an extra year
1454 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1455 * and we add on 5 years we are after the rollover day - so we calculate 31 Oct 2020
1457 public function testFixedMultiYearDateSetThreeEmptyStartEndDate() {
1458 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1460 $this->callAPISuccess('membership_type', 'create', array(
1461 'id' => $this->_membershipTypeID2
,
1462 'duration_interval' => 5,
1464 'fixed_period_start_day' => '1101',
1466 'fixed_period_rollover_day' => '101',
1468 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1470 'join_date' => '28-Jan 2015',
1472 $result = $this->callAPISuccess($this->_entity
, 'create', array_merge($this->_params
, $dates));
1473 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1474 $this->assertEquals('2015-01-28', $result['join_date']);
1475 $this->assertEquals('2014-11-01', $result['start_date']);
1476 $this->assertEquals('2020-10-31', $result['end_date']);
1480 * Test that if membership start date is not set it defaults to correct end date for fixed single year memberships.
1482 public function testEmptyStartDateRolling() {
1483 unset($this->_params
['start_date'], $this->_params
['is_override']);
1484 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1485 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1486 $this->assertEquals('2009-01-21', $result['join_date']);
1487 $this->assertEquals('2009-01-21', $result['start_date']);
1488 $this->assertEquals('2009-12-21', $result['end_date']);
1492 * Test that if membership end date is not set it defaults to correct end date.
1495 public function testEmptyEndDateFixed() {
1496 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
1497 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
1498 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1499 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1500 $this->assertEquals('2009-01-21', $result['join_date']);
1501 $this->assertEquals('2008-03-01', $result['start_date']);
1502 $this->assertEquals('2010-02-28', $result['end_date']);
1506 * Test that if membership end date is not set it defaults to correct end date.
1509 public function testEmptyEndDateRolling() {
1510 unset($this->_params
['is_override'], $this->_params
['end_date']);
1511 $this->_params
['membership_type_id'] = $this->_membershipTypeID
;
1512 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1513 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1514 $this->assertEquals('2009-01-21', $result['join_date']);
1515 $this->assertEquals('2009-01-21', $result['start_date']);
1516 $this->assertEquals('2010-01-20', $result['end_date']);
1521 * Test that if dates are set they not over-ridden if id is passed in
1523 public function testMembershipDatesNotOverridden() {
1524 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1525 unset($this->_params
['end_date'], $this->_params
['start_date']);
1526 $this->_params
['id'] = $result['id'];
1527 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1528 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1529 $this->assertEquals('2009-01-21', $result['join_date']);
1530 $this->assertEquals('2009-01-21', $result['start_date']);
1531 $this->assertEquals('2009-12-21', $result['end_date']);
1536 * Test that all membership types are returned when getoptions is called.
1538 * This test locks in current behaviour where types for all domains are returned. It should possibly be domain
1539 * specific but that should only be done in conjunction with adding a hook to allow that to be altered as the
1540 * multisite use case expects the master domain to be able to see all sites.
1544 public function testGetOptionsMembershipTypeID() {
1545 $options = $this->callAPISuccess('Membership', 'getoptions', array('field' => 'membership_type_id'));
1546 $this->assertEquals('Another one', array_pop($options['values']));
1547 $this->assertEquals('General', array_pop($options['values']));
1548 $this->assertEquals(NULL, array_pop($options['values']));