3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 require_once 'CiviTest/CiviUnitTestCase.php';
39 * Class api_v3_MembershipTest
41 class api_v3_MembershipTest
extends CiviUnitTestCase
{
42 protected $_apiversion;
43 protected $_contactID;
44 protected $_membershipID;
45 protected $_membershipID2;
46 protected $_membershipID3;
47 protected $_membershipTypeID;
48 protected $_membershipTypeID2;
49 protected $_membershipStatusID;
54 public function setUp() {
55 // Connect to the database
57 $this->_apiversion
= 3;
58 $this->_contactID
= $this->individualCreate();
59 $this->_membershipTypeID
= $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID
));
60 $this->_membershipTypeID2
= $this->membershipTypeCreate(array(
61 'period_type' => 'fixed',
62 'fixed_period_start_day' => '301',
63 'fixed_period_rollover_day' => '1111',
65 $this->_membershipStatusID
= $this->membershipStatusCreate('test status');
67 CRM_Member_PseudoConstant
::membershipType(NULL, TRUE);
68 CRM_Member_PseudoConstant
::membershipStatus(NULL, NULL, 'name', TRUE);
69 CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, TRUE, 'name');
71 $this->_entity
= 'Membership';
72 $this->_params
= array(
73 'contact_id' => $this->_contactID
,
74 'membership_type_id' => $this->_membershipTypeID
,
75 'join_date' => '2009-01-21',
76 'start_date' => '2009-01-21',
77 'end_date' => '2009-12-21',
78 'source' => 'Payment',
80 'status_id' => $this->_membershipStatusID
,
84 public function tearDown() {
85 $this->quickCleanup(array(
87 'civicrm_membership_payment',
88 'civicrm_membership_log',
92 $this->membershipStatusDelete($this->_membershipStatusID
);
93 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID2
));
94 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID
));
95 $this->contactDelete($this->_contactID
);
100 * Test civicrm_membership_delete()
102 public function testMembershipDelete() {
103 $membershipID = $this->contactMembershipCreate($this->_params
);
104 $this->assertDBRowExist('CRM_Member_DAO_Membership', $membershipID);
106 'id' => $membershipID,
108 $this->callAPIAndDocument('membership', 'delete', $params, __FUNCTION__
, __FILE__
);
109 $this->assertDBRowNotExist('CRM_Member_DAO_Membership', $membershipID);
112 public function testMembershipDeleteEmpty() {
113 $this->callAPIFailure('membership', 'delete', array());
116 public function testMembershipDeleteInvalidID() {
117 $this->callAPIFailure('membership', 'delete', array('id' => 'blah'));
121 * Test civicrm_membership_delete() with invalid Membership Id
123 public function testMembershipDeleteWithInvalidMembershipId() {
124 $membershipId = 'membership';
125 $this->callAPIFailure('membership', 'delete', $membershipId);
129 * All other methods calls MembershipType and MembershipContact
130 * api, but putting simple test methods to control existence of
131 * these methods for backwards compatibility, also verifying basic
132 * behaviour is the same as new methods.
134 public function testContactMembershipsGet() {
135 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
137 $this->callAPISuccess('membership', 'get', $params);
138 $this->callAPISuccess('Membership', 'Delete', array('id' => $this->_membershipID
));
142 * Test civicrm_membership_get with params not array.
143 * Gets treated as contact_id, memberships expected.
145 public function testGetWithParamsContactId() {
146 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
148 'contact_id' => $this->_contactID
,
150 $membership = $this->callAPISuccess('membership', 'get', $params);
152 $result = $membership['values'][$this->_membershipID
];
153 $this->callAPISuccess('Membership', 'Delete', array(
154 'id' => $this->_membershipID
,
156 $this->assertEquals($result['contact_id'], $this->_contactID
, "In line " . __LINE__
);
157 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID
, "In line " . __LINE__
);
158 $this->assertEquals($result['status_id'], $this->_membershipStatusID
, "In line " . __LINE__
);
159 $this->assertEquals($result['join_date'], '2009-01-21', "In line " . __LINE__
);
160 $this->assertEquals($result['start_date'], '2009-01-21', "In line " . __LINE__
);
161 $this->assertEquals($result['end_date'], '2009-12-21', "In line " . __LINE__
);
162 $this->assertEquals($result['source'], 'Payment', "In line " . __LINE__
);
163 $this->assertEquals($result['is_override'], 1, "In line " . __LINE__
);
167 * Test civicrm_membership_get with params not array.
168 * Gets treated as contact_id, memberships expected.
170 public function testGetInSyntax() {
171 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
172 $this->_membershipID2
= $this->contactMembershipCreate($this->_params
);
173 $this->_membershipID3
= $this->contactMembershipCreate($this->_params
);
175 'id' => array('IN' => array($this->_membershipID
, $this->_membershipID3
)),
177 $membership = $this->callAPISuccess('membership', 'get', $params);
178 $this->assertEquals(2, $membership['count']);
179 $this->assertEquals(array($this->_membershipID
, $this->_membershipID3
), array_keys($membership['values']));
181 'id' => array('NOT IN' => array($this->_membershipID
, $this->_membershipID3
)),
183 $membership = $this->callAPISuccess('membership', 'get', $params);
184 $this->assertEquals(1, $membership['count']);
185 $this->assertEquals(array($this->_membershipID2
), array_keys($membership['values']));
189 * Test civicrm_membership_get with params not array.
190 * Gets treated as contact_id, memberships expected.
192 public function testGetInSyntaxOnContactID() {
193 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
194 $contact2 = $this->individualCreate();
195 $contact3 = $this->individualCreate(array('first_name' => 'Scout', 'last_name' => 'Canine'));
196 $this->_membershipID2
= $this->contactMembershipCreate(array_merge($this->_params
, array('contact_id' => $contact2)));
197 $this->_membershipID3
= $this->contactMembershipCreate(array_merge($this->_params
, array('contact_id' => $contact3)));
199 'contact_id' => array('IN' => array($this->_contactID
, $contact3)),
201 $membership = $this->callAPISuccess('membership', 'get', $params);
202 $this->assertEquals(2, $membership['count']);
203 $this->assertEquals(array($this->_membershipID
, $this->_membershipID3
), array_keys($membership['values']));
205 'contact_id' => array('NOT IN' => array($this->_contactID
, $contact3)),
207 $membership = $this->callAPISuccess('membership', 'get', $params);
208 $this->assertEquals(1, $membership['count']);
209 $this->assertEquals(array($this->_membershipID2
), array_keys($membership['values']));
213 * Test civicrm_membership_get with params not array.
214 * Gets treated as contact_id, memberships expected.
216 public function testGetWithParamsMemberShipTypeId() {
217 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
219 'membership_type_id' => $this->_membershipTypeID
,
221 $membership = $this->callAPISuccess('membership', 'get', $params);
222 $this->callAPISuccess('Membership', 'Delete', array(
223 'id' => $membership['id'],
225 $result = $membership['values'][$membership['id']];
226 $this->assertEquals($result['contact_id'], $this->_contactID
, "In line " . __LINE__
);
227 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID
, "In line " . __LINE__
);
228 $this->assertEquals($result['status_id'], $this->_membershipStatusID
, "In line " . __LINE__
);
229 $this->assertEquals($result['join_date'], '2009-01-21', "In line " . __LINE__
);
230 $this->assertEquals($result['start_date'], '2009-01-21', "In line " . __LINE__
);
231 $this->assertEquals($result['end_date'], '2009-12-21', "In line " . __LINE__
);
232 $this->assertEquals($result['source'], 'Payment', "In line " . __LINE__
);
233 $this->assertEquals($result['is_override'], 1, "In line " . __LINE__
);
234 $this->assertEquals($result['id'], $membership['id']);
238 * Test civicrm_membership_get with params not array.
239 * Gets treated as contact_id, memberships expected.
241 public function testGetWithParamsMemberShipTypeIdContactID() {
242 $params = $this->_params
;
243 $this->callAPISuccess($this->_entity
, 'create', $params);
244 $params['membership_type_id'] = $this->_membershipTypeID2
;
245 $this->callAPISuccess($this->_entity
, 'create', $params);
246 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_contactID
), 2);
248 'membership_type_id' => $this->_membershipTypeID
,
249 'contact_id' => $this->_contactID
,
251 $result = $this->callAPISuccess('membership', 'getsingle', $params);
252 $this->assertEquals($result['contact_id'], $this->_contactID
);
253 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID
);
256 'membership_type_id' => $this->_membershipTypeID2
,
257 'contact_id' => $this->_contactID
,
259 $result = $this->callAPISuccess('membership', 'getsingle', $params);
260 $this->assertEquals($result['contact_id'], $this->_contactID
);
261 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID2
);
265 * Check with complete array + custom field
266 * Note that the test is written on purpose without any
267 * variables specific to participant so it can be replicated into other entities
268 * and / or moved to the automated test suite
270 public function testGetWithParamsMemberShipIdAndCustom() {
271 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
273 $params = $this->_params
;
274 $params['custom_' . $ids['custom_field_id']] = "custom string";
276 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
278 $getParams = array('membership_type_id' => $params['membership_type_id']);
279 $check = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
280 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
282 $this->callAPISuccess('Membership', 'Delete', array(
283 'id' => $result['id'],
288 * Test civicrm_membership_get with proper params.
289 * Memberships expected.
291 public function testGet() {
292 $membershipID = $this->contactMembershipCreate($this->_params
);
294 'contact_id' => $this->_contactID
,
297 $membership = $this->callAPISuccess('membership', 'get', $params);
298 $result = $membership['values'][$membershipID];
299 $this->callAPISuccess('Membership', 'Delete', array(
300 'id' => $membership['id'],
302 $this->assertEquals($result['join_date'], '2009-01-21', "In line " . __LINE__
);
303 $this->assertEquals($result['contact_id'], $this->_contactID
, "In line " . __LINE__
);
304 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID
, "In line " . __LINE__
);
305 $this->assertEquals($result['status_id'], $this->_membershipStatusID
, "In line " . __LINE__
);
307 $this->assertEquals($result['start_date'], '2009-01-21', "In line " . __LINE__
);
308 $this->assertEquals($result['end_date'], '2009-12-21', "In line " . __LINE__
);
309 $this->assertEquals($result['source'], 'Payment', "In line " . __LINE__
);
310 $this->assertEquals($result['is_override'], 1, "In line " . __LINE__
);
315 * Test civicrm_membership_get with proper params.
316 * Memberships expected.
318 public function testGetWithId() {
319 $membershipID = $this->contactMembershipCreate($this->_params
);
321 'contact_id' => $this->_contactID
,
322 'id' => $this->_membershipID
,
325 $result = $this->callAPISuccess('membership', 'get', $params);
326 $this->assertEquals($membershipID, $result['id']);
328 'contact_id' => $this->_contactID
,
329 'membership_id' => $this->_membershipID
,
330 'return' => 'membership_id',
332 $result = $this->callAPISuccess('membership', 'get', $params);
333 $this->assertEquals($membershipID, $result['id']);
337 * Test civicrm_membership_get for only active.
338 * Memberships expected.
340 public function testGetOnlyActive() {
341 $description = "Demonstrates use of 'filter' active_only' param";
342 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
343 $subfile = 'filterIsCurrent';
345 'contact_id' => $this->_contactID
,
349 $membership = $this->callAPISuccess('membership', 'get', $params);
350 $this->assertEquals($membership['values'][$this->_membershipID
]['status_id'], $this->_membershipStatusID
);
351 $this->assertEquals($membership['values'][$this->_membershipID
]['contact_id'], $this->_contactID
);
353 'contact_id' => $this->_contactID
,
359 $membership = $this->callAPIAndDocument('membership', 'get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
360 $this->assertEquals($membership['values'][$this->_membershipID
]['status_id'], $this->_membershipStatusID
);
361 $this->assertEquals($membership['values'][$this->_membershipID
]['contact_id'], $this->_contactID
);
363 $this->callAPISuccess('Membership', 'Delete', array('id' => $this->_membershipID
));
367 * Test civicrm_membership_get for non exist contact.
370 public function testGetNoContactExists() {
372 'contact_id' => 55555,
375 $membership = $this->callAPISuccess('membership', 'get', $params);
376 $this->assertEquals($membership['count'], 0, "In line " . __LINE__
);
380 * Test civicrm_membership_get with relationship.
383 public function testGetWithRelationship() {
384 $membershipOrgId = $this->organizationCreate(NULL);
385 $memberContactId = $this->individualCreate();
387 $relTypeParams = array(
388 'name_a_b' => 'Relation 1',
389 'name_b_a' => 'Relation 2',
390 'description' => 'Testing relationship type',
391 'contact_type_a' => 'Organization',
392 'contact_type_b' => 'Individual',
396 $relTypeID = $this->relationshipTypeCreate($relTypeParams);
399 'name' => 'test General',
400 'duration_unit' => 'year',
401 'duration_interval' => 1,
402 'period_type' => 'rolling',
403 'member_of_contact_id' => $membershipOrgId,
405 'financial_type_id' => 1,
406 'relationship_type_id' => $relTypeID,
407 'relationship_direction' => 'b_a',
410 $memType = $this->callAPISuccess('membership_type', 'create', $params);
413 'contact_id' => $memberContactId,
414 'membership_type_id' => $memType['id'],
415 'join_date' => '2009-01-21',
416 'start_date' => '2009-01-21',
417 'end_date' => '2009-12-21',
418 'source' => 'Payment',
420 'status_id' => $this->_membershipStatusID
,
422 $membershipID = $this->contactMembershipCreate($params);
425 'contact_id' => $memberContactId,
426 'membership_type_id' => $memType['id'],
429 $result = $this->callAPISuccess('membership', 'get', $params);
431 $membership = $result['values'][$membershipID];
432 $this->assertEquals($this->_membershipStatusID
, $membership['status_id']);
433 $this->callAPISuccess('Membership', 'Delete', array(
434 'id' => $membership['id'],
436 $this->membershipTypeDelete(array('id' => $memType['id']));
437 $this->relationshipTypeDelete($relTypeID);
438 $this->contactDelete($membershipOrgId);
439 $this->contactDelete($memberContactId);
443 * Test civicrm_membership_create with relationships.
444 * create/get Memberships.
446 * Test suite for CRM-14758: API ( contact, create ) does not always create related membership
447 * and max_related property for Membership_Type and Membership entities
449 public function testCreateWithRelationship() {
450 // Create membership type: inherited through employment, max_related = 2
452 'name_a_b' => 'Employee of',
454 $result = $this->callAPISuccess('relationship_type', 'get', $params);
455 $relationshipTypeId = $result['id'];
456 $membershipOrgId = $this->organizationCreate();
458 'name' => 'Corporate Membership',
459 'duration_unit' => 'year',
460 'duration_interval' => 1,
461 'period_type' => 'rolling',
462 'member_of_contact_id' => $membershipOrgId,
464 'financial_type_id' => 1,
465 'relationship_type_id' => $relationshipTypeId,
466 'relationship_direction' => 'b_a',
470 $result = $this->callAPISuccess('membership_type', 'create', $params);
471 $membershipTypeId = $result['id'];
473 // Create employer and first employee
474 $employerId[0] = $this->organizationCreate(array(), 1);
475 $memberContactId[0] = $this->individualCreate(array('employer_id' => $employerId[0]), 0);
477 // Create organization's membership
479 'contact_id' => $employerId[0],
480 'membership_type_id' => $membershipTypeId,
481 'source' => 'Test suite',
482 'start_date' => date('Y-m-d'),
483 'end_date' => "+1 year",
485 $OrganizationMembershipID = $this->contactMembershipCreate($params);
487 // Check that the employee inherited the membership
489 'contact_id' => $memberContactId[0],
490 'membership_type_id' => $membershipTypeId,
493 $result = $this->callAPISuccess('membership', 'get', $params);
495 $this->assertEquals(1, $result['count']);
496 $result = $result['values'][$result['id']];
497 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
499 // Create second employee
500 $memberContactId[1] = $this->individualCreate(array('employer_id' => $employerId[0]), 1);
502 // Check that the employee inherited the membership
504 'contact_id' => $memberContactId[1],
505 'membership_type_id' => $membershipTypeId,
507 $result = $this->callAPISuccess('membership', 'get', $params);
508 // If it fails here CRM-14758 is not fixed
509 $this->assertEquals(1, $result['count']);
510 $result = $result['values'][$result['id']];
511 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
513 // Create third employee
514 $memberContactId[2] = $this->individualCreate(array('employer_id' => $employerId[0]), 2);
516 // Check that employee does NOT inherit the membership (max_related = 2)
518 'contact_id' => $memberContactId[2],
519 'membership_type_id' => $membershipTypeId,
521 $result = $this->callAPISuccess('membership', 'get', $params);
522 $this->assertEquals(0, $result['count']);
524 // Increase max_related for the employer's membership
526 'id' => $OrganizationMembershipID,
529 $this->contactMembershipCreate($params);
531 // Check that the employee inherited the membership
533 'contact_id' => $memberContactId[2],
534 'membership_type_id' => $membershipTypeId,
536 $result = $this->callAPISuccess('membership', 'get', $params);
537 $this->assertEquals(1, $result['count']);
538 $result = $result['values'][$result['id']];
539 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
541 // First employee moves to a new job
542 $employerId[1] = $this->organizationCreate(array(), 2);
544 'id' => $memberContactId[0],
545 'employer_id' => $employerId[1],
547 $this->callAPISuccess('contact', 'create', $params);
549 // Check that employee does NO LONGER inherit the membership
551 'contact_id' => $memberContactId[0],
552 'membership_type_id' => $membershipTypeId,
554 $result = $this->callAPISuccess('membership', 'get', $params);
555 $this->assertEquals(0, $result['count']);
557 // Tear down - reverse of creation to be safe
558 $this->contactDelete($memberContactId[2]);
559 $this->contactDelete($memberContactId[1]);
560 $this->contactDelete($memberContactId[0]);
561 $this->contactDelete($employerId[1]);
562 $this->contactDelete($employerId[0]);
563 $this->membershipTypeDelete(array('id' => $membershipTypeId));
564 $this->contactDelete($membershipOrgId);
568 * We are checking for no enotices + only id & end_date returned
570 public function testMembershipGetWithReturn() {
571 $this->contactMembershipCreate($this->_params
);
572 $result = $this->callAPISuccess('membership', 'get', array('return' => 'end_date'));
573 foreach ($result['values'] as $membership) {
574 $this->assertEquals(array('id', 'end_date'), array_keys($membership));
577 ///////////////// civicrm_membership_create methods
580 * Test civicrm_contact_memberships_create with empty params.
583 public function testCreateWithEmptyParams() {
585 $this->callAPIFailure('membership', 'create', $params);
589 * If is_overide is passed in status must also be passed in
591 public function testCreateOverrideNoStatus() {
592 $params = $this->_params
;
593 unset($params['status_id']);
594 $this->callAPIFailure('membership', 'create', $params);
597 public function testMembershipCreateMissingRequired() {
599 'membership_type_id' => '1',
600 'join_date' => '2006-01-21',
601 'start_date' => '2006-01-21',
602 'end_date' => '2006-12-21',
603 'source' => 'Payment',
607 $this->callAPIFailure('membership', 'create', $params);
610 public function testMembershipCreate() {
612 'contact_id' => $this->_contactID
,
613 'membership_type_id' => $this->_membershipTypeID
,
614 'join_date' => '2006-01-21',
615 'start_date' => '2006-01-21',
616 'end_date' => '2006-12-21',
617 'source' => 'Payment',
619 'status_id' => $this->_membershipStatusID
,
622 $result = $this->callAPIAndDocument('membership', 'create', $params, __FUNCTION__
, __FILE__
);
623 $this->getAndCheck($params, $result['id'], $this->_entity
);
624 $this->assertNotNull($result['id']);
625 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['contact_id'], " in line " . __LINE__
);
626 $this->assertEquals($result['id'], $result['values'][$result['id']]['id'], " in line " . __LINE__
);
630 * Check for useful message if contact doesn't exist
632 public function testMembershipCreateWithInvalidContact() {
635 'membership_type_id' => $this->_membershipTypeID
,
636 'join_date' => '2006-01-21',
637 'start_date' => '2006-01-21',
638 'end_date' => '2006-12-21',
639 'source' => 'Payment',
641 'status_id' => $this->_membershipStatusID
,
644 $this->callAPIFailure('membership', 'create', $params,
645 'contact_id is not valid : 999'
649 public function testMembershipCreateWithInvalidStatus() {
650 $params = $this->_params
;
651 $params['status_id'] = 999;
652 $this->callAPIFailure('membership', 'create', $params,
653 "'999' is not a valid option for field status_id"
657 public function testMembershipCreateWithInvalidType() {
658 $params = $this->_params
;
659 $params['membership_type_id'] = 999;
661 $this->callAPIFailure('membership', 'create', $params,
662 "'999' is not a valid option for field membership_type_id"
667 * Check with complete array + custom field
668 * Note that the test is written on purpose without any
669 * variables specific to participant so it can be replicated into other entities
670 * and / or moved to the automated test suite
672 public function testCreateWithCustom() {
673 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
675 $params = $this->_params
;
676 $params['custom_' . $ids['custom_field_id']] = "custom string";
678 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
679 $check = $this->callAPISuccess($this->_entity
, 'get', array(
680 'id' => $result['id'],
681 'contact_id' => $this->_contactID
,
683 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
687 * Test civicrm_contact_memberships_create with membership id (edit
691 public function testMembershipCreateWithId() {
692 $membershipID = $this->contactMembershipCreate($this->_params
);
694 'id' => $membershipID,
695 'contact_id' => $this->_contactID
,
696 'membership_type_id' => $this->_membershipTypeID
,
697 'join_date' => '2006-01-21',
698 'start_date' => '2006-01-21',
699 'end_date' => '2006-12-21',
700 'source' => 'Payment',
702 'status_id' => $this->_membershipStatusID
,
705 $result = $this->callAPISuccess('membership', 'create', $params);
706 $this->callAPISuccess('Membership', 'Delete', array(
707 'id' => $result['id'],
709 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
713 * Test civicrm_contact_memberships_create with membership id (edit
717 public function testMembershipCreateUpdateWithIdNoContact() {
718 $membershipID = $this->contactMembershipCreate($this->_params
);
720 'id' => $membershipID,
721 'membership_type_id' => $this->_membershipTypeID
,
722 'contact_id' => $this->_contactID
,
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->callAPISuccess('membership', 'create', $params);
732 $this->callAPISuccess('Membership', 'Delete', array(
733 'id' => $result['id'],
736 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
740 * Test civicrm_contact_memberships_create with membership id (edit
744 public function testMembershipCreateUpdateWithIdNoDates() {
745 $membershipID = $this->contactMembershipCreate($this->_params
);
747 'id' => $membershipID,
748 'contact_id' => $this->_contactID
,
749 'membership_type_id' => $this->_membershipTypeID
,
750 'source' => 'Payment',
752 'status_id' => $this->_membershipStatusID
,
755 $result = $this->callAPISuccess('membership', 'create', $params);
756 $this->callAPISuccess('Membership', 'Delete', array(
757 'id' => $result['id'],
759 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
763 * Test civicrm_contact_memberships_create with membership id (edit
767 public function testMembershipCreateUpdateWithIdNoDatesNoType() {
768 $membershipID = $this->contactMembershipCreate($this->_params
);
770 'id' => $membershipID,
771 'source' => 'not much here',
772 'contact_id' => $this->_contactID
,
774 'status_id' => $this->_membershipStatusID
,
777 $result = $this->callAPISuccess('membership', 'create', $params);
778 $this->callAPISuccess('Membership', 'Delete', array(
779 'id' => $result['id'],
781 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
785 * Test civicrm_contact_memberships_create with membership id (edit
789 public function testMembershipCreateUpdateWithIDAndSource() {
790 $membershipID = $this->contactMembershipCreate($this->_params
);
792 'id' => $membershipID,
793 'source' => 'changed',
794 'contact_id' => $this->_contactID
,
795 'status_id' => $this->_membershipStatusID
,
796 'membership_type_id' => $this->_membershipTypeID
,
797 'skipStatusCal' => 1,
799 $result = $this->callAPISuccess('membership', 'create', $params);
800 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
801 $this->callAPISuccess('Membership', 'Delete', array(
802 'id' => $result['id'],
807 * Change custom field using update
809 public function testUpdateWithCustom() {
810 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
812 $params = $this->_params
;
813 $params['custom_' . $ids['custom_field_id']] = "custom string";
814 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
815 $result = $this->callAPISuccess($this->_entity
, 'create', array(
816 'id' => $result['id'],
817 'custom_' . $ids['custom_field_id'] => "new custom",
819 $check = $this->callAPISuccess($this->_entity
, 'get', array(
820 'id' => $result['id'],
821 'contact_id' => $this->_contactID
,
824 $this->assertEquals("new custom", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
825 $this->callAPISuccess('Membership', 'Delete', array(
826 'id' => $check['id'],
829 $this->customFieldDelete($ids['custom_field_id']);
830 $this->customGroupDelete($ids['custom_group_id']);
834 * per CRM-15746 check that the id can be altered in an update hook
836 function testMembershipUpdateCreateHookCRM15746() {
837 $this->hookClass
->setHook('civicrm_pre', array($this, 'hook_civicrm_pre_update_create_membership'));
838 $result = $this->callAPISuccess('membership', 'create', $this->_params
);
839 $this->callAPISuccess('membership', 'create', array('id' => $result['id'], 'end_date' => '1 year ago'));
840 $this->callAPISuccessGetCount('membership', array(), 2);
841 $this->hookClass
->reset();
842 $this->callAPISuccess('membership', 'create', array('id' => $result['id'], 'end_date' => '1 year ago'));
843 $this->callAPISuccessGetCount('membership', array(), 2);
846 function hook_civicrm_pre_update_create_membership($op, $objectName, $id, &$params) {
847 if ($objectName == 'Membership' && $op == 'edit') {
848 $existingMembership = $this->callAPISuccessGetSingle('membership', array('id' => $params['id']));
849 unset($params['id'], $params['membership_id']);
850 $params['join_date'] = $params['membership_start_date'] = $params['start_date'] = date('Ymd000000', strtotime($existingMembership['start_date']));
851 $params = array_merge($existingMembership, $params);
852 $params['id'] = NULL;
857 * Test civicrm_contact_memberships_create Invalid membership data
860 public function testMembershipCreateInvalidMemData() {
861 //membership_contact_id as string
863 'membership_contact_id' => 'Invalid',
864 'membership_type_id' => $this->_membershipTypeID
,
865 'join_date' => '2011-01-21',
866 'start_date' => '2010-01-21',
867 'end_date' => '2008-12-21',
868 'source' => 'Payment',
870 'status_id' => $this->_membershipStatusID
,
873 $this->callAPIFailure('membership', 'create', $params);
875 //membership_contact_id which is no in contact table
876 $params['membership_contact_id'] = 999;
877 $this->callAPIFailure('membership', 'create', $params);
880 unset($params['membership_contact_id']);
881 $params['join_date'] = "invalid";
882 $this->callAPIFailure('Membership', 'Create', $params);
886 * Test civicrm_contact_memberships_create with membership_contact_id
890 public function testMembershipCreateWithMemContact() {
892 'membership_contact_id' => $this->_contactID
,
893 'membership_type_id' => $this->_membershipTypeID
,
894 'join_date' => '2011-01-21',
895 'start_date' => '2010-01-21',
896 'end_date' => '2008-12-21',
897 'source' => 'Payment',
899 'status_id' => $this->_membershipStatusID
,
902 $result = $this->callAPISuccess('membership', 'create', $params);
904 $this->callAPISuccess('Membership', 'Delete', array(
905 'id' => $result['id'],
910 * Test civicrm_contact_memberships_create with membership_contact_id
914 public function testMembershipCreateValidMembershipTypeString() {
916 'membership_contact_id' => $this->_contactID
,
917 'membership_type_id' => 'General',
918 'join_date' => '2011-01-21',
919 'start_date' => '2010-01-21',
920 'end_date' => '2008-12-21',
921 'source' => 'Payment',
923 'status_id' => $this->_membershipStatusID
,
926 $result = $this->callAPISuccess('membership', 'create', $params);
927 $this->assertEquals($this->_membershipTypeID
, $result['values'][$result['id']]['membership_type_id']);
928 $this->callAPISuccess('Membership', 'Delete', array(
929 'id' => $result['id'],
934 * Test civicrm_contact_memberships_create with membership_contact_id
938 public function testMembershipCreateInValidMembershipTypeString() {
940 'membership_contact_id' => $this->_contactID
,
941 'membership_type_id' => 'invalid',
942 'join_date' => '2011-01-21',
943 'start_date' => '2010-01-21',
944 'end_date' => '2008-12-21',
945 'source' => 'Payment',
947 'status_id' => $this->_membershipStatusID
,
950 $this->callAPIFailure('membership', 'create', $params);
954 * Test that if membership join date is not set it defaults to today
956 public function testEmptyJoinDate() {
957 unset($this->_params
['join_date'], $this->_params
['is_override']);
958 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
959 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
960 $this->assertEquals(date('Y-m-d', strtotime('now')), $result['join_date']);
961 $this->assertEquals('2009-01-21', $result['start_date']);
962 $this->assertEquals('2009-12-21', $result['end_date']);
966 * Test that if membership start date is not set it defaults to correct end date
969 public function testEmptyStartDateFixed() {
970 unset($this->_params
['start_date'], $this->_params
['is_override']);
971 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
972 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
973 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
974 $this->assertEquals('2009-01-21', $result['join_date']);
975 $this->assertEquals('2008-03-01', $result['start_date']);
976 $this->assertEquals('2009-12-21', $result['end_date']);
980 * Test that if membership start date is not set it defaults to correct end date
983 public function testEmptyStartDateRolling() {
984 unset($this->_params
['start_date'], $this->_params
['is_override']);
985 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
986 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
987 $this->assertEquals('2009-01-21', $result['join_date']);
988 $this->assertEquals('2009-01-21', $result['start_date']);
989 $this->assertEquals('2009-12-21', $result['end_date']);
993 * Test that if membership end date is not set it defaults to correct end date
996 public function testEmptyEndDateFixed() {
997 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
998 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
999 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1000 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1001 $this->assertEquals('2009-01-21', $result['join_date']);
1002 $this->assertEquals('2008-03-01', $result['start_date']);
1003 $this->assertEquals('2010-02-28', $result['end_date']);
1007 * Test that if membership end date is not set it defaults to correct end date
1010 public function testEmptyEndDateRolling() {
1011 unset($this->_params
['is_override'], $this->_params
['end_date']);
1012 $this->_params
['membership_type_id'] = $this->_membershipTypeID
;
1013 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1014 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1015 $this->assertEquals('2009-01-21', $result['join_date']);
1016 $this->assertEquals('2009-01-21', $result['start_date']);
1017 $this->assertEquals('2010-01-20', $result['end_date']);
1022 * Test that if datesdate are not set they not over-ridden if id is passed in
1024 public function testMembershipDatesNotOverridden() {
1025 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1026 unset($this->_params
['end_date'], $this->_params
['start_date']);
1027 $this->_params
['id'] = $result['id'];
1028 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1029 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1030 $this->assertEquals('2009-01-21', $result['join_date']);
1031 $this->assertEquals('2009-01-21', $result['start_date']);
1032 $this->assertEquals('2009-12-21', $result['end_date']);