3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 $_membershipTypeID;
45 protected $_membershipTypeID2;
46 protected $_membershipStatusID;
47 protected $__membershipID;
52 public function setUp() {
53 // Connect to the database
55 $this->_apiversion
= 3;
56 $this->_contactID
= $this->individualCreate();
57 $this->_membershipTypeID
= $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID
));
58 $this->_membershipTypeID2
= $this->membershipTypeCreate(array('period_type' => 'fixed','fixed_period_start_day' => '301', 'fixed_period_rollover_day' => '1111'));
59 $this->_membershipStatusID
= $this->membershipStatusCreate('test status');
61 CRM_Member_PseudoConstant
::membershipType(NULL, TRUE);
62 CRM_Member_PseudoConstant
::membershipStatus(NULL, NULL, 'name', TRUE);
63 CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, TRUE, 'name');
65 $this->_entity
= 'Membership';
66 $this->_params
= array(
67 'contact_id' => $this->_contactID
,
68 'membership_type_id' => $this->_membershipTypeID
,
69 'join_date' => '2009-01-21',
70 'start_date' => '2009-01-21',
71 'end_date' => '2009-12-21',
72 'source' => 'Payment',
74 'status_id' => $this->_membershipStatusID
,
79 $this->quickCleanup(array(
81 'civicrm_membership_payment',
82 'civicrm_membership_log',
86 $this->membershipStatusDelete($this->_membershipStatusID
);
87 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID2
));
88 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID
));
89 $this->contactDelete($this->_contactID
);
94 * Test civicrm_membership_delete()
96 function testMembershipDelete() {
97 $membershipID = $this->contactMembershipCreate($this->_params
);
98 $this->assertDBRowExist('CRM_Member_DAO_Membership', $membershipID);
100 'id' => $membershipID
102 $result = $this->callAPIAndDocument('membership', 'delete', $params, __FUNCTION__
, __FILE__
);
103 $this->assertDBRowNotExist('CRM_Member_DAO_Membership', $membershipID);
106 function testMembershipDeleteEmpty() {
108 $result = $this->callAPIFailure('membership', 'delete', $params);
111 function testMembershipDeleteInvalidID() {
112 $params = array('id' => 'blah');
113 $result = $this->callAPIFailure('membership', 'delete', $params);
117 * Test civicrm_membership_delete() with invalid Membership Id
119 function testMembershipDeleteWithInvalidMembershipId() {
120 $membershipId = 'membership';
121 $result = $this->callAPIFailure('membership', 'delete', $membershipId);
125 * All other methods calls MembershipType and MembershipContact
126 * api, but putting simple test methods to control existence of
127 * these methods for backwards compatibility, also verifying basic
128 * behaviour is the same as new methods.
130 function testContactMembershipsGet() {
131 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
133 $result = $this->callAPISuccess('membership', 'get', $params);
134 $result = $this->callAPISuccess('Membership', 'Delete', array(
135 'id' => $this->_membershipID
,
140 * Test civicrm_membership_get with params not array.
141 * Gets treated as contact_id, memberships expected.
143 function testGetWithParamsContactId() {
144 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
146 'contact_id' => $this->_contactID
,
148 $membership = $this->callAPISuccess('membership', 'get', $params);
150 $result = $membership['values'][$this->_membershipID
];
151 $this->callAPISuccess('Membership', 'Delete', array(
152 'id' => $this->_membershipID
,
154 $this->assertEquals($result['contact_id'], $this->_contactID
, "In line " . __LINE__
);
155 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID
, "In line " . __LINE__
);
156 $this->assertEquals($result['status_id'], $this->_membershipStatusID
, "In line " . __LINE__
);
157 $this->assertEquals($result['join_date'], '2009-01-21', "In line " . __LINE__
);
158 $this->assertEquals($result['start_date'], '2009-01-21', "In line " . __LINE__
);
159 $this->assertEquals($result['end_date'], '2009-12-21', "In line " . __LINE__
);
160 $this->assertEquals($result['source'], 'Payment', "In line " . __LINE__
);
161 $this->assertEquals($result['is_override'], 1, "In line " . __LINE__
);
165 * Test civicrm_membership_get with params not array.
166 * Gets treated as contact_id, memberships expected.
168 function testGetInSyntax() {
169 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
170 $this->_membershipID2
= $this->contactMembershipCreate($this->_params
);
171 $this->_membershipID3
= $this->contactMembershipCreate($this->_params
);
173 'id' => array('IN' => array($this->_membershipID
, $this->_membershipID3
)),
175 $membership = $this->callAPISuccess('membership', 'get', $params);
176 $this->assertEquals(2, $membership['count']);
177 $this->assertEquals(array($this->_membershipID
, $this->_membershipID3
), array_keys($membership['values']));
179 'id' => array('NOT IN' => array($this->_membershipID
, $this->_membershipID3
)),
181 $membership = $this->callAPISuccess('membership', 'get', $params);
182 $this->assertEquals(1, $membership['count']);
183 $this->assertEquals(array($this->_membershipID2
), array_keys($membership['values']));
188 * Test civicrm_membership_get with params not array.
189 * Gets treated as contact_id, memberships expected.
191 function testGetInSyntaxOnContactID() {
192 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
193 $contact2 = $this->individualCreate();
194 $contact3 = $this->individualCreate(array('first_name' => 'Scout', 'last_name' => 'Canine'));
195 $this->_membershipID2
= $this->contactMembershipCreate(array_merge($this->_params
, array('contact_id' => $contact2)));
196 $this->_membershipID3
= $this->contactMembershipCreate(array_merge($this->_params
, array('contact_id' => $contact3)));
198 'contact_id' => array('IN' => array($this->_contactID
, $contact3)),
200 $membership = $this->callAPISuccess('membership', 'get', $params);
201 $this->assertEquals(2, $membership['count']);
202 $this->assertEquals(array($this->_membershipID
, $this->_membershipID3
), array_keys($membership['values']));
204 'contact_id' => array('NOT IN' => array($this->_contactID
, $contact3)),
206 $membership = $this->callAPISuccess('membership', 'get', $params);
207 $this->assertEquals(1, $membership['count']);
208 $this->assertEquals(array($this->_membershipID2
), array_keys($membership['values']));
211 * Test civicrm_membership_get with params not array.
212 * Gets treated as contact_id, memberships expected.
214 function testGetWithParamsMemberShipTypeId() {
215 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
217 'membership_type_id' => $this->_membershipTypeID
,
219 $membership = $this->callAPISuccess('membership', 'get', $params);
220 $result = $this->callAPISuccess('Membership', 'Delete', array(
221 'id' => $membership['id'],
223 $result = $membership['values'][$membership['id']];
224 $this->assertEquals($result['contact_id'], $this->_contactID
, "In line " . __LINE__
);
225 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID
, "In line " . __LINE__
);
226 $this->assertEquals($result['status_id'], $this->_membershipStatusID
, "In line " . __LINE__
);
227 $this->assertEquals($result['join_date'], '2009-01-21', "In line " . __LINE__
);
228 $this->assertEquals($result['start_date'], '2009-01-21', "In line " . __LINE__
);
229 $this->assertEquals($result['end_date'], '2009-12-21', "In line " . __LINE__
);
230 $this->assertEquals($result['source'], 'Payment', "In line " . __LINE__
);
231 $this->assertEquals($result['is_override'], 1, "In line " . __LINE__
);
232 $this->assertEquals($result['id'], $membership['id']);
235 * Test civicrm_membership_get with params not array.
236 * Gets treated as contact_id, memberships expected.
238 function testGetWithParamsMemberShipTypeIdContactID() {
239 $params = $this->_params
;
240 $this->callAPISuccess($this->_entity
, 'create', $params);
241 $params['membership_type_id'] = $this->_membershipTypeID2
;
242 $this->callAPISuccess($this->_entity
, 'create', $params);
243 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_contactID
), 2);
245 'membership_type_id' => $this->_membershipTypeID
,
246 'contact_id' => $this->_contactID
,
248 $result = $this->callAPISuccess('membership', 'getsingle', $params);
249 $this->assertEquals($result['contact_id'], $this->_contactID
);
250 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID
);
253 'membership_type_id' => $this->_membershipTypeID2
,
254 'contact_id' => $this->_contactID
,
256 $result = $this->callAPISuccess('membership', 'getsingle', $params);
257 $this->assertEquals($result['contact_id'], $this->_contactID
);
258 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID2
);
261 * check with complete array + custom field
262 * Note that the test is written on purpose without any
263 * variables specific to participant so it can be replicated into other entities
264 * and / or moved to the automated test suite
266 function testGetWithParamsMemberShipIdAndCustom() {
267 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
269 $params = $this->_params
;
270 $params['custom_' . $ids['custom_field_id']] = "custom string";
272 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
274 $getParams = array('membership_type_id' => $params['membership_type_id']);
275 $check = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
276 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
278 $result = $this->callAPISuccess('Membership', 'Delete', array(
279 'id' => $result['id'],
284 * Test civicrm_membership_get with proper params.
285 * Memberships expected.
288 $membershipID = $this->contactMembershipCreate($this->_params
);
290 'contact_id' => $this->_contactID
,
293 $membership = $this->callAPISuccess('membership', 'get', $params);
294 $result = $membership['values'][$membershipID];
295 $this->callAPISuccess('Membership', 'Delete', array(
296 'id' => $membership['id'],
298 $this->assertEquals($result['join_date'], '2009-01-21', "In line " . __LINE__
);
299 $this->assertEquals($result['contact_id'], $this->_contactID
, "In line " . __LINE__
);
300 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID
, "In line " . __LINE__
);
301 $this->assertEquals($result['status_id'], $this->_membershipStatusID
, "In line " . __LINE__
);
303 $this->assertEquals($result['start_date'], '2009-01-21', "In line " . __LINE__
);
304 $this->assertEquals($result['end_date'], '2009-12-21', "In line " . __LINE__
);
305 $this->assertEquals($result['source'], 'Payment', "In line " . __LINE__
);
306 $this->assertEquals($result['is_override'], 1, "In line " . __LINE__
);
311 * Test civicrm_membership_get with proper params.
312 * Memberships expected.
314 function testGetWithId() {
315 $membershipID = $this->contactMembershipCreate($this->_params
);
317 'contact_id' => $this->_contactID
,
318 'id' => $this->__membershipID
,
321 $result = $this->callAPISuccess('membership', 'get', $params);
322 $this->assertEquals($membershipID, $result['id']);
324 'contact_id' => $this->_contactID
,
325 'membership_id' => $this->__membershipID
,
326 'return' => 'membership_id',
328 $result = $this->callAPISuccess('membership', 'get', $params);
329 $this->assertEquals($membershipID, $result['id']);
333 * Test civicrm_membership_get for only active.
334 * Memberships expected.
336 function testGetOnlyActive() {
337 $description = "Demonstrates use of 'filter' active_only' param";
338 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
339 $subfile = 'filterIsCurrent';
341 'contact_id' => $this->_contactID
,
345 $membership = $this->callAPISuccess('membership', 'get', $params);
346 $result = $membership['values'][$this->_membershipID
];
347 $this->assertEquals($membership['values'][$this->_membershipID
]['status_id'], $this->_membershipStatusID
);
348 $this->assertEquals($membership['values'][$this->_membershipID
]['contact_id'], $this->_contactID
);
350 'contact_id' => $this->_contactID
,
356 $membership = $this->callAPIAndDocument('membership', 'get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
357 $result = $membership['values'][$this->_membershipID
];
358 $this->assertEquals($membership['values'][$this->_membershipID
]['status_id'], $this->_membershipStatusID
);
359 $this->assertEquals($membership['values'][$this->_membershipID
]['contact_id'], $this->_contactID
);
362 $result = $this->callAPISuccess('Membership', 'Delete', array(
363 'id' => $this->_membershipID
,
368 * Test civicrm_membership_get for non exist contact.
371 function testGetNoContactExists() {
373 'contact_id' => 55555,
376 $membership = $this->callAPISuccess('membership', 'get', $params);
377 $this->assertEquals($membership['count'], 0, "In line " . __LINE__
);
381 * Test civicrm_membership_get with relationship.
384 function testGetWithRelationship() {
385 $membershipOrgId = $this->organizationCreate(NULL);
386 $memberContactId = $this->individualCreate();
388 $relTypeParams = array(
389 'name_a_b' => 'Relation 1',
390 'name_b_a' => 'Relation 2',
391 'description' => 'Testing relationship type',
392 'contact_type_a' => 'Organization',
393 'contact_type_b' => 'Individual',
397 $relTypeID = $this->relationshipTypeCreate($relTypeParams);
400 'name' => 'test General',
401 'duration_unit' => 'year',
402 'duration_interval' => 1,
403 'period_type' => 'rolling',
404 'member_of_contact_id' => $membershipOrgId,
406 'financial_type_id' => 1,
407 'relationship_type_id' => $relTypeID,
408 'relationship_direction' => 'b_a',
411 $memType = $this->callAPISuccess('membership_type', 'create', $params);
414 'contact_id' => $memberContactId,
415 'membership_type_id' => $memType['id'],
416 'join_date' => '2009-01-21',
417 'start_date' => '2009-01-21',
418 'end_date' => '2009-12-21',
419 'source' => 'Payment',
421 'status_id' => $this->_membershipStatusID
,
423 $membershipID = $this->contactMembershipCreate($params);
426 'contact_id' => $memberContactId,
427 'membership_type_id' => $memType['id'],
430 $result = $this->callAPISuccess('membership', 'get', $params);
432 $membership = $result['values'][$membershipID];
433 $this->assertEquals($this->_membershipStatusID
, $membership['status_id']);
434 $result = $this->callAPISuccess('Membership', 'Delete', array(
435 'id' => $membership['id'],
437 $this->membershipTypeDelete(array('id' => $memType['id']));
438 $this->relationshipTypeDelete($relTypeID);
439 $this->contactDelete($membershipOrgId);
440 $this->contactDelete($memberContactId);
444 * Test civicrm_membership_create with relationships.
445 * create/get Memberships.
447 * Test suite for CRM-14758: API ( contact, create ) does not always create related membership
448 * and max_related property for Membership_Type and Membership entities
450 function testCreateWithRelationship() {
451 // Create membership type: inherited through employment, max_related = 2
453 'name_a_b' => 'Employee of',
455 $result = $this->callAPISuccess('relationship_type', 'get', $params);
456 $relationshipTypeId = $result['id'];
457 $membershipOrgId = $this->organizationCreate();
459 'name' => 'Corporate Membership',
460 'duration_unit' => 'year',
461 'duration_interval' => 1,
462 'period_type' => 'rolling',
463 'member_of_contact_id' => $membershipOrgId,
465 'financial_type_id' => 1,
466 'relationship_type_id' => $relationshipTypeId,
467 'relationship_direction' => 'b_a',
471 $result = $this->callAPISuccess('membership_type', 'create', $params);
472 $membershipTypeId = $result['id'];
474 // Create employer and first employee
475 $employerId[0] = $this->organizationCreate(array(), 1);
476 $memberContactId[0] = $this->individualCreate(array('employer_id' => $employerId[0]), 0);
478 // Create organization's membership
480 'contact_id' => $employerId[0],
481 'membership_type_id' => $membershipTypeId,
482 'source' => 'Test suite',
483 'start_date' => date('Y-m-d'),
484 'end_date' => "+1 year",
486 $OrganizationMembershipID = $this->contactMembershipCreate($params);
488 // Check that the employee inherited the membership
490 'contact_id' => $memberContactId[0],
491 'membership_type_id' => $membershipTypeId,
494 $result = $this->callAPISuccess('membership', 'get', $params);
496 $this->assertEquals(1, $result['count']);
497 $result = $result['values'][$result['id']];
498 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
500 // Create second employee
501 $memberContactId[1] = $this->individualCreate(array('employer_id' => $employerId[0]), 1);
503 // Check that the employee inherited the membership
505 'contact_id' => $memberContactId[1],
506 'membership_type_id' => $membershipTypeId,
508 $result = $this->callAPISuccess('membership', 'get', $params);
509 // If it fails here CRM-14758 is not fixed
510 $this->assertEquals(1, $result['count']);
511 $result = $result['values'][$result['id']];
512 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
514 // Create third employee
515 $memberContactId[2] = $this->individualCreate(array('employer_id' => $employerId[0]), 2);
517 // Check that employee does NOT inherit the membership (max_related = 2)
519 'contact_id' => $memberContactId[2],
520 'membership_type_id' => $membershipTypeId,
522 $result = $this->callAPISuccess('membership', 'get', $params);
523 $this->assertEquals(0, $result['count']);
525 // Increase max_related for the employer's membership
527 'id' => $OrganizationMembershipID,
530 $this->contactMembershipCreate($params);
532 // Check that the employee inherited the membership
534 'contact_id' => $memberContactId[2],
535 'membership_type_id' => $membershipTypeId,
537 $result = $this->callAPISuccess('membership', 'get', $params);
538 $this->assertEquals(1, $result['count']);
539 $result = $result['values'][$result['id']];
540 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
542 // First employee moves to a new job
543 $employerId[1] = $this->organizationCreate(array(), 2);
545 'id' => $memberContactId[0],
546 'employer_id' => $employerId[1],
548 $this->callAPISuccess('contact', 'create', $params);
550 // Check that employee does NO LONGER inherit the membership
552 'contact_id' => $memberContactId[0],
553 'membership_type_id' => $membershipTypeId,
555 $result = $this->callAPISuccess('membership', 'get', $params);
556 $this->assertEquals(0, $result['count']);
558 // Tear down - reverse of creation to be safe
559 $this->contactDelete($memberContactId[2]);
560 $this->contactDelete($memberContactId[1]);
561 $this->contactDelete($memberContactId[0]);
562 $this->contactDelete($employerId[1]);
563 $this->contactDelete($employerId[0]);
564 $this->membershipTypeDelete(array('id' => $membershipTypeId));
565 $this->contactDelete($membershipOrgId);
569 * We are checking for no enotices + only id & end_date returned
571 function testMembershipGetWithReturn() {
572 $membershipID = $this->contactMembershipCreate($this->_params
);
573 $result = $this->callAPISuccess('membership', 'get', array('return' => 'end_date'));
574 foreach ($result['values'] as $membership) {
575 $this->assertEquals(array('id', 'end_date'), array_keys($membership));
578 ///////////////// civicrm_membership_create methods
581 * Test civicrm_contact_memberships_create with empty params.
584 function testCreateWithEmptyParams() {
586 $result = $this->callAPIFailure('membership', 'create', $params);
590 * If is_overide is passed in status must also be passed in
592 function testCreateOverrideNoStatus() {
593 $params = $this->_params
;
594 unset($params['status_id']);
595 $result = $this->callAPIFailure('membership', 'create', $params);
598 function testMembershipCreateMissingRequired() {
600 'membership_type_id' => '1',
601 'join_date' => '2006-01-21',
602 'start_date' => '2006-01-21',
603 'end_date' => '2006-12-21',
604 'source' => 'Payment',
608 $result = $this->callAPIFailure('membership', 'create', $params);
611 function testMembershipCreate() {
613 'contact_id' => $this->_contactID
,
614 'membership_type_id' => $this->_membershipTypeID
,
615 'join_date' => '2006-01-21',
616 'start_date' => '2006-01-21',
617 'end_date' => '2006-12-21',
618 'source' => 'Payment',
620 'status_id' => $this->_membershipStatusID
,
623 $result = $this->callAPIAndDocument('membership', 'create', $params, __FUNCTION__
, __FILE__
);
624 $this->getAndCheck($params, $result['id'], $this->_entity
);
625 $this->assertNotNull($result['id']);
626 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['contact_id'], " in line " . __LINE__
);
627 $this->assertEquals($result['id'], $result['values'][$result['id']]['id'], " in line " . __LINE__
);
630 * Check for useful message if contact doesn't exist
632 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 $result = $this->callAPIFailure('membership', 'create', $params,
645 'contact_id is not valid : 999'
648 function testMembershipCreateWithInvalidStatus() {
649 $params = $this->_params
;
650 $params['status_id'] = 999;
651 $result = $this->callAPIFailure('membership', 'create', $params,
652 "'999' is not a valid option for field status_id"
656 function testMembershipCreateWithInvalidType() {
657 $params = $this->_params
;
658 $params['membership_type_id'] = 999;
660 $result = $this->callAPIFailure('membership', 'create', $params,
661 "'999' is not a valid option for field membership_type_id"
666 * check with complete array + custom field
667 * Note that the test is written on purpose without any
668 * variables specific to participant so it can be replicated into other entities
669 * and / or moved to the automated test suite
671 function testCreateWithCustom() {
672 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
674 $params = $this->_params
;
675 $params['custom_' . $ids['custom_field_id']] = "custom string";
677 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
678 $check = $this->callAPISuccess($this->_entity
, 'get', array('id' => $result['id'], 'contact_id' => $this->_contactID
));
679 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
683 * Test civicrm_contact_memberships_create with membership id (edit
687 function testMembershipCreateWithId() {
688 $membershipID = $this->contactMembershipCreate($this->_params
);
690 'id' => $membershipID,
691 'contact_id' => $this->_contactID
,
692 'membership_type_id' => $this->_membershipTypeID
,
693 'join_date' => '2006-01-21',
694 'start_date' => '2006-01-21',
695 'end_date' => '2006-12-21',
696 'source' => 'Payment',
698 'status_id' => $this->_membershipStatusID
,
701 $result = $this->callAPISuccess('membership', 'create', $params);
702 $this->callAPISuccess('Membership', 'Delete', array(
703 'id' => $result['id'],
705 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
709 * Test civicrm_contact_memberships_create with membership id (edit
713 function testMembershipCreateUpdateWithIdNoContact() {
714 $membershipID = $this->contactMembershipCreate($this->_params
);
716 'id' => $membershipID,
717 'membership_type_id' => $this->_membershipTypeID
,
718 'contact_id' => $this->_contactID
,
719 'join_date' => '2006-01-21',
720 'start_date' => '2006-01-21',
721 'end_date' => '2006-12-21',
722 'source' => 'Payment',
724 'status_id' => $this->_membershipStatusID
,
727 $result = $this->callAPISuccess('membership', 'create', $params);
728 $this->callAPISuccess('Membership', 'Delete', array(
729 'id' => $result['id'],
732 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
736 * Test civicrm_contact_memberships_create with membership id (edit
740 function testMembershipCreateUpdateWithIdNoDates() {
741 $membershipID = $this->contactMembershipCreate($this->_params
);
743 'id' => $membershipID,
744 'contact_id' => $this->_contactID
,
745 'membership_type_id' => $this->_membershipTypeID
,
746 'source' => 'Payment',
748 'status_id' => $this->_membershipStatusID
,
751 $result = $this->callAPISuccess('membership', 'create', $params);
752 $this->callAPISuccess('Membership', 'Delete', array(
753 'id' => $result['id'],
755 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
759 * Test civicrm_contact_memberships_create with membership id (edit
763 function testMembershipCreateUpdateWithIdNoDatesNoType() {
764 $membershipID = $this->contactMembershipCreate($this->_params
);
766 'id' => $membershipID,
767 'source' => 'not much here',
768 'contact_id' => $this->_contactID
,
770 'status_id' => $this->_membershipStatusID
,
773 $result = $this->callAPISuccess('membership', 'create', $params);
774 $this->callAPISuccess('Membership', 'Delete', array(
775 'id' => $result['id'],
777 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
781 * Test civicrm_contact_memberships_create with membership id (edit
785 function testMembershipCreateUpdateWithIDAndSource() {
786 $membershipID = $this->contactMembershipCreate($this->_params
);
788 'id' => $membershipID,
789 'source' => 'changed',
790 'contact_id' => $this->_contactID
,
791 'status_id' => $this->_membershipStatusID
, 'membership_type_id' => $this->_membershipTypeID
,
792 'skipStatusCal' => 1,
794 $result = $this->callAPISuccess('membership', 'create', $params);
795 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
796 $this->callAPISuccess('Membership', 'Delete', array(
797 'id' => $result['id'],
802 * change custom field using update
804 function testUpdateWithCustom() {
805 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
807 $params = $this->_params
;
808 $params['custom_' . $ids['custom_field_id']] = "custom string";
809 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
810 $result = $this->callAPISuccess($this->_entity
, 'create', array('id' => $result['id'], 'custom_' . $ids['custom_field_id'] => "new custom"));
811 $check = $this->callAPISuccess($this->_entity
, 'get', array('id' => $result['id'], 'contact_id' => $this->_contactID
));
813 $this->assertEquals("new custom", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
814 $delete = $this->callAPISuccess('Membership', 'Delete', array(
815 'id' => $check['id'],
818 $this->customFieldDelete($ids['custom_field_id']);
819 $this->customGroupDelete($ids['custom_group_id']);
823 * Test civicrm_contact_memberships_create Invalid membership data
826 function testMembershipCreateInvalidMemData() {
827 //membership_contact_id as string
829 'membership_contact_id' => 'Invalid',
830 'membership_type_id' => $this->_membershipTypeID
,
831 'join_date' => '2011-01-21',
832 'start_date' => '2010-01-21',
833 'end_date' => '2008-12-21',
834 'source' => 'Payment',
836 'status_id' => $this->_membershipStatusID
, );
838 $result = $this->callAPIFailure('membership', 'create', $params);
840 //membership_contact_id which is no in contact table
841 $params['membership_contact_id'] = 999;
842 $result = $this->callAPIFailure('membership', 'create', $params);
845 unset($params['membership_contact_id']);
846 $params['join_date'] = "invalid";
847 $result = $this->callAPIFailure('Membership', 'Create', $params);
851 * Test civicrm_contact_memberships_create with membership_contact_id
855 function testMembershipCreateWithMemContact() {
857 'membership_contact_id' => $this->_contactID
,
858 'membership_type_id' => $this->_membershipTypeID
,
859 'join_date' => '2011-01-21',
860 'start_date' => '2010-01-21',
861 'end_date' => '2008-12-21',
862 'source' => 'Payment',
864 'status_id' => $this->_membershipStatusID
,
867 $result = $this->callAPISuccess('membership', 'create', $params);
869 $result = $this->callAPISuccess('Membership', 'Delete', array(
870 'id' => $result['id'],
874 * Test civicrm_contact_memberships_create with membership_contact_id
878 function testMembershipCreateValidMembershipTypeString() {
880 'membership_contact_id' => $this->_contactID
,
881 'membership_type_id' => 'General',
882 'join_date' => '2011-01-21',
883 'start_date' => '2010-01-21',
884 'end_date' => '2008-12-21',
885 'source' => 'Payment',
887 'status_id' => $this->_membershipStatusID
,
890 $result = $this->callAPISuccess('membership', 'create', $params);
891 $this->assertEquals($this->_membershipTypeID
, $result['values'][$result['id']]['membership_type_id']);
892 $result = $this->callAPISuccess('Membership', 'Delete', array(
893 'id' => $result['id'],
898 * Test civicrm_contact_memberships_create with membership_contact_id
902 function testMembershipCreateInValidMembershipTypeString() {
904 'membership_contact_id' => $this->_contactID
,
905 'membership_type_id' => 'invalid',
906 'join_date' => '2011-01-21',
907 'start_date' => '2010-01-21',
908 'end_date' => '2008-12-21',
909 'source' => 'Payment',
911 'status_id' => $this->_membershipStatusID
,
914 $result = $this->callAPIFailure('membership', 'create', $params);
918 * Test that if membership join date is not set it defaults to today
920 function testEmptyJoinDate() {
921 unset($this->_params
['join_date'], $this->_params
['is_override']);
922 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
923 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
924 $this->assertEquals(date('Y-m-d', strtotime('now')), $result['join_date']);
925 $this->assertEquals('2009-01-21', $result['start_date']);
926 $this->assertEquals('2009-12-21', $result['end_date']);
929 * Test that if membership start date is not set it defaults to correct end date
932 function testEmptyStartDateFixed() {
933 unset($this->_params
['start_date'], $this->_params
['is_override']);
934 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
935 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
936 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
937 $this->assertEquals('2009-01-21', $result['join_date']);
938 $this->assertEquals('2008-03-01', $result['start_date']);
939 $this->assertEquals('2009-12-21', $result['end_date']);
943 * Test that if membership start date is not set it defaults to correct end date
946 function testEmptyStartDateRolling() {
947 unset($this->_params
['start_date'], $this->_params
['is_override']);
948 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
949 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
950 $this->assertEquals('2009-01-21', $result['join_date']);
951 $this->assertEquals('2009-01-21', $result['start_date']);
952 $this->assertEquals('2009-12-21', $result['end_date']);
955 * Test that if membership end date is not set it defaults to correct end date
958 function testEmptyEndDateFixed() {
959 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
960 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
961 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
962 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
963 $this->assertEquals('2009-01-21', $result['join_date']);
964 $this->assertEquals('2008-03-01', $result['start_date']);
965 $this->assertEquals('2010-02-28', $result['end_date']);
968 * Test that if membership end date is not set it defaults to correct end date
971 function testEmptyEndDateRolling() {
972 unset($this->_params
['is_override'], $this->_params
['end_date']);
973 $this->_params
['membership_type_id'] = $this->_membershipTypeID
;
974 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
975 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
976 $this->assertEquals('2009-01-21', $result['join_date']);
977 $this->assertEquals('2009-01-21', $result['start_date']);
978 $this->assertEquals('2010-01-20', $result['end_date']);
983 * Test that if datesdate are not set they not over-ridden if id is passed in
985 function testMembershipDatesNotOverridden() {
986 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
987 unset($this->_params
['end_date'], $this->_params
['start_date']);
988 $this->_params
['id'] = $result['id'];
989 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
990 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
991 $this->assertEquals('2009-01-21', $result['join_date']);
992 $this->assertEquals('2009-01-21', $result['start_date']);
993 $this->assertEquals('2009-12-21', $result['end_date']);