3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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';
38 class api_v3_MembershipTest
extends CiviUnitTestCase
{
39 protected $_apiversion;
40 protected $_contactID;
41 protected $_membershipTypeID;
42 protected $_membershipTypeID2;
43 protected $_membershipStatusID;
44 protected $__membershipID;
49 public function setUp() {
50 // Connect to the database
52 $this->_apiversion
= 3;
53 $this->_contactID
= $this->individualCreate();
54 $this->_membershipTypeID
= $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID
));
55 $this->_membershipTypeID2
= $this->membershipTypeCreate(array('period_type' => 'fixed','fixed_period_start_day' => '301', 'fixed_period_rollover_day' => '1111'));
56 $this->_membershipStatusID
= $this->membershipStatusCreate('test status');
58 CRM_Member_PseudoConstant
::membershipType(NULL, TRUE);
59 CRM_Member_PseudoConstant
::membershipStatus(NULL, NULL, 'name', TRUE);
60 CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, TRUE, 'name');
62 $this->_entity
= 'Membership';
63 $this->_params
= array(
64 'contact_id' => $this->_contactID
,
65 'membership_type_id' => $this->_membershipTypeID
,
66 'join_date' => '2009-01-21',
67 'start_date' => '2009-01-21',
68 'end_date' => '2009-12-21',
69 'source' => 'Payment',
71 'status_id' => $this->_membershipStatusID
,
76 $this->quickCleanup(array(
78 'civicrm_membership_payment',
79 'civicrm_membership_log',
83 $this->membershipStatusDelete($this->_membershipStatusID
);
84 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID2
));
85 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID
));
86 $this->contactDelete($this->_contactID
);
91 * Test civicrm_membership_delete()
93 function testMembershipDelete() {
94 $membershipID = $this->contactMembershipCreate($this->_params
);
95 $this->assertDBRowExist('CRM_Member_DAO_Membership', $membershipID);
99 $result = $this->callAPIAndDocument('membership', 'delete', $params, __FUNCTION__
, __FILE__
);
100 $this->assertDBRowNotExist('CRM_Member_DAO_Membership', $membershipID);
103 function testMembershipDeleteEmpty() {
105 $result = $this->callAPIFailure('membership', 'delete', $params);
108 function testMembershipDeleteInvalidID() {
109 $params = array('id' => 'blah');
110 $result = $this->callAPIFailure('membership', 'delete', $params);
114 * Test civicrm_membership_delete() with invalid Membership Id
116 function testMembershipDeleteWithInvalidMembershipId() {
117 $membershipId = 'membership';
118 $result = $this->callAPIFailure('membership', 'delete', $membershipId);
122 * All other methods calls MembershipType and MembershipContact
123 * api, but putting simple test methods to control existence of
124 * these methods for backwards compatibility, also verifying basic
125 * behaviour is the same as new methods.
127 function testContactMembershipsGet() {
128 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
130 $result = $this->callAPISuccess('membership', 'get', $params);
131 $result = $this->callAPISuccess('Membership', 'Delete', array(
132 'id' => $this->_membershipID
,
137 * Test civicrm_membership_get with params not array.
138 * Gets treated as contact_id, memberships expected.
140 function testGetWithParamsContactId() {
141 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
143 'contact_id' => $this->_contactID
,
145 $membership = $this->callAPISuccess('membership', 'get', $params);
147 $result = $membership['values'][$this->_membershipID
];
148 $this->callAPISuccess('Membership', 'Delete', array(
149 'id' => $this->_membershipID
,
151 $this->assertEquals($result['contact_id'], $this->_contactID
, "In line " . __LINE__
);
152 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID
, "In line " . __LINE__
);
153 $this->assertEquals($result['status_id'], $this->_membershipStatusID
, "In line " . __LINE__
);
154 $this->assertEquals($result['join_date'], '2009-01-21', "In line " . __LINE__
);
155 $this->assertEquals($result['start_date'], '2009-01-21', "In line " . __LINE__
);
156 $this->assertEquals($result['end_date'], '2009-12-21', "In line " . __LINE__
);
157 $this->assertEquals($result['source'], 'Payment', "In line " . __LINE__
);
158 $this->assertEquals($result['is_override'], 1, "In line " . __LINE__
);
162 * Test civicrm_membership_get with params not array.
163 * Gets treated as contact_id, memberships expected.
165 function testGetInSyntax() {
166 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
167 $this->_membershipID2
= $this->contactMembershipCreate($this->_params
);
168 $this->_membershipID3
= $this->contactMembershipCreate($this->_params
);
170 'id' => array('IN' => array($this->_membershipID
, $this->_membershipID3
)),
172 $membership = $this->callAPISuccess('membership', 'get', $params);
173 $this->assertEquals(2, $membership['count']);
174 $this->assertEquals(array($this->_membershipID
, $this->_membershipID3
), array_keys($membership['values']));
176 'id' => array('NOT IN' => array($this->_membershipID
, $this->_membershipID3
)),
178 $membership = $this->callAPISuccess('membership', 'get', $params);
179 $this->assertEquals(1, $membership['count']);
180 $this->assertEquals(array($this->_membershipID2
), array_keys($membership['values']));
185 * Test civicrm_membership_get with params not array.
186 * Gets treated as contact_id, memberships expected.
188 function testGetInSyntaxOnContactID() {
189 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
190 $contact2 = $this->individualCreate();
191 $contact3 = $this->individualCreate(array('first_name' => 'Scout', 'last_name' => 'Canine'));
192 $this->_membershipID2
= $this->contactMembershipCreate(array_merge($this->_params
, array('contact_id' => $contact2)));
193 $this->_membershipID3
= $this->contactMembershipCreate(array_merge($this->_params
, array('contact_id' => $contact3)));
195 'contact_id' => array('IN' => array($this->_contactID
, $contact3)),
197 $membership = $this->callAPISuccess('membership', 'get', $params);
198 $this->assertEquals(2, $membership['count']);
199 $this->assertEquals(array($this->_membershipID
, $this->_membershipID3
), array_keys($membership['values']));
201 'contact_id' => array('NOT IN' => array($this->_contactID
, $contact3)),
203 $membership = $this->callAPISuccess('membership', 'get', $params);
204 $this->assertEquals(1, $membership['count']);
205 $this->assertEquals(array($this->_membershipID2
), array_keys($membership['values']));
208 * Test civicrm_membership_get with params not array.
209 * Gets treated as contact_id, memberships expected.
212 function testGetWithParamsMemberShipTypeId() {
213 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
215 'membership_type_id' => $this->_membershipTypeID
,
217 $membership = $this->callAPISuccess('membership', 'get', $params);
218 $result = $this->callAPISuccess('Membership', 'Delete', array(
219 'id' => $membership['id'],
221 $result = $membership['values'][$membership['id']];
222 $this->assertEquals($result['contact_id'], $this->_contactID
, "In line " . __LINE__
);
223 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID
, "In line " . __LINE__
);
224 $this->assertEquals($result['status_id'], $this->_membershipStatusID
, "In line " . __LINE__
);
225 $this->assertEquals($result['join_date'], '2009-01-21', "In line " . __LINE__
);
226 $this->assertEquals($result['start_date'], '2009-01-21', "In line " . __LINE__
);
227 $this->assertEquals($result['end_date'], '2009-12-21', "In line " . __LINE__
);
228 $this->assertEquals($result['source'], 'Payment', "In line " . __LINE__
);
229 $this->assertEquals($result['is_override'], 1, "In line " . __LINE__
);
230 $this->assertEquals($result['id'], $membership['id']);
234 * check with complete array + custom field
235 * Note that the test is written on purpose without any
236 * variables specific to participant so it can be replicated into other entities
237 * and / or moved to the automated test suite
239 function testGetWithParamsMemberShipIdAndCustom() {
240 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
242 $params = $this->_params
;
243 $params['custom_' . $ids['custom_field_id']] = "custom string";
245 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
247 $getParams = array('membership_type_id' => $params['membership_type_id']);
248 $check = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
249 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
251 $result = $this->callAPISuccess('Membership', 'Delete', array(
252 'id' => $result['id'],
257 * Test civicrm_membership_get with proper params.
258 * Memberships expected.
261 $membershipID = $this->contactMembershipCreate($this->_params
);
263 'contact_id' => $this->_contactID
,
266 $membership = $this->callAPISuccess('membership', 'get', $params);
267 $result = $membership['values'][$membershipID];
268 $this->callAPISuccess('Membership', 'Delete', array(
269 'id' => $membership['id'],
271 $this->assertEquals($result['join_date'], '2009-01-21', "In line " . __LINE__
);
272 $this->assertEquals($result['contact_id'], $this->_contactID
, "In line " . __LINE__
);
273 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID
, "In line " . __LINE__
);
274 $this->assertEquals($result['status_id'], $this->_membershipStatusID
, "In line " . __LINE__
);
276 $this->assertEquals($result['start_date'], '2009-01-21', "In line " . __LINE__
);
277 $this->assertEquals($result['end_date'], '2009-12-21', "In line " . __LINE__
);
278 $this->assertEquals($result['source'], 'Payment', "In line " . __LINE__
);
279 $this->assertEquals($result['is_override'], 1, "In line " . __LINE__
);
284 * Test civicrm_membership_get with proper params.
285 * Memberships expected.
287 function testGetWithId() {
288 $membershipID = $this->contactMembershipCreate($this->_params
);
290 'contact_id' => $this->_contactID
,
291 'id' => $this->__membershipID
,
294 $result = $this->callAPISuccess('membership', 'get', $params);
295 $this->assertEquals($membershipID, $result['id']);
297 'contact_id' => $this->_contactID
,
298 'membership_id' => $this->__membershipID
,
299 'return' => 'membership_id',
301 $result = $this->callAPISuccess('membership', 'get', $params);
302 $this->assertEquals($membershipID, $result['id']);
306 * Test civicrm_membership_get for only active.
307 * Memberships expected.
309 function testGetOnlyActive() {
310 $description = "Demonstrates use of 'filter' active_only' param";
311 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
312 $subfile = 'filterIsCurrent';
314 'contact_id' => $this->_contactID
,
318 $membership = $this->callAPISuccess('membership', 'get', $params);
319 $result = $membership['values'][$this->_membershipID
];
320 $this->assertEquals($membership['values'][$this->_membershipID
]['status_id'], $this->_membershipStatusID
, "In line " . __LINE__
);
321 $this->assertEquals($membership['values'][$this->_membershipID
]['contact_id'], $this->_contactID
, "In line " . __LINE__
);
323 'contact_id' => $this->_contactID
,
329 $membership = $this->callAPIAndDocument('membership', 'get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
330 $result = $membership['values'][$this->_membershipID
];
331 $this->assertEquals($membership['values'][$this->_membershipID
]['status_id'], $this->_membershipStatusID
, "In line " . __LINE__
);
332 $this->assertEquals($membership['values'][$this->_membershipID
]['contact_id'], $this->_contactID
, "In line " . __LINE__
);
335 $result = $this->callAPISuccess('Membership', 'Delete', array(
336 'id' => $this->_membershipID
,
341 * Test civicrm_membership_get for non exist contact.
344 function testGetNoContactExists() {
346 'contact_id' => 55555,
349 $membership = $this->callAPISuccess('membership', 'get', $params);
350 $this->assertEquals($membership['count'], 0, "In line " . __LINE__
);
354 * Test civicrm_membership_get with relationship.
357 function testGetWithRelationship() {
358 $membershipOrgId = $this->organizationCreate(NULL);
359 $memberContactId = $this->individualCreate();
361 $relTypeParams = array(
362 'name_a_b' => 'Relation 1',
363 'name_b_a' => 'Relation 2',
364 'description' => 'Testing relationship type',
365 'contact_type_a' => 'Organization',
366 'contact_type_b' => 'Individual',
370 $relTypeID = $this->relationshipTypeCreate($relTypeParams);
373 'name' => 'test General',
374 'duration_unit' => 'year',
375 'duration_interval' => 1,
376 'period_type' => 'rolling',
377 'member_of_contact_id' => $membershipOrgId,
379 'financial_type_id' => 1,
380 'relationship_type_id' => $relTypeID,
381 'relationship_direction' => 'b_a',
384 $memType = $this->callAPISuccess('membership_type', 'create', $params);
387 'contact_id' => $memberContactId,
388 'membership_type_id' => $memType['id'],
389 'join_date' => '2009-01-21',
390 'start_date' => '2009-01-21',
391 'end_date' => '2009-12-21',
392 'source' => 'Payment',
394 'status_id' => $this->_membershipStatusID
,
396 $membershipID = $this->contactMembershipCreate($params);
399 'contact_id' => $memberContactId,
400 'membership_type_id' => $memType['id'],
403 $result = $this->callAPISuccess('membership', 'get', $params);
405 $membership = $result['values'][$membershipID];
406 $this->assertEquals($this->_membershipStatusID
, $membership['status_id']);
407 $result = $this->callAPISuccess('Membership', 'Delete', array(
408 'id' => $membership['id'],
410 $this->membershipTypeDelete(array('id' => $memType['id']));
411 $this->relationshipTypeDelete($relTypeID);
412 $this->contactDelete($membershipOrgId);
413 $this->contactDelete($memberContactId);
417 * We are checking for no enotices + only id & end_date returned
419 function testMembershipGetWithReturn() {
420 $membershipID = $this->contactMembershipCreate($this->_params
);
421 $result = $this->callAPISuccess('membership', 'get', array('return' => 'end_date'));
422 foreach ($result['values'] as $membership) {
423 $this->assertEquals(array('id', 'end_date'), array_keys($membership));
426 ///////////////// civicrm_membership_create methods
429 * Test civicrm_contact_memberships_create with empty params.
432 function testCreateWithEmptyParams() {
434 $result = $this->callAPIFailure('membership', 'create', $params);
438 * If is_overide is passed in status must also be passed in
440 function testCreateOverrideNoStatus() {
441 $params = $this->_params
;
442 unset($params['status_id']);
443 $result = $this->callAPIFailure('membership', 'create', $params);
446 function testMembershipCreateMissingRequired() {
448 'membership_type_id' => '1',
449 'join_date' => '2006-01-21',
450 'start_date' => '2006-01-21',
451 'end_date' => '2006-12-21',
452 'source' => 'Payment',
456 $result = $this->callAPIFailure('membership', 'create', $params);
459 function testMembershipCreate() {
461 'contact_id' => $this->_contactID
,
462 'membership_type_id' => $this->_membershipTypeID
,
463 'join_date' => '2006-01-21',
464 'start_date' => '2006-01-21',
465 'end_date' => '2006-12-21',
466 'source' => 'Payment',
468 'status_id' => $this->_membershipStatusID
,
471 $result = $this->callAPIAndDocument('membership', 'create', $params, __FUNCTION__
, __FILE__
);
472 $this->getAndCheck($params, $result['id'], $this->_entity
);
473 $this->assertNotNull($result['id']);
474 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['contact_id'], " in line " . __LINE__
);
475 $this->assertEquals($result['id'], $result['values'][$result['id']]['id'], " in line " . __LINE__
);
478 * Check for useful message if contact doesn't exist
480 function testMembershipCreateWithInvalidContact() {
483 'membership_type_id' => $this->_membershipTypeID
,
484 'join_date' => '2006-01-21',
485 'start_date' => '2006-01-21',
486 'end_date' => '2006-12-21',
487 'source' => 'Payment',
489 'status_id' => $this->_membershipStatusID
,
492 $result = $this->callAPIFailure('membership', 'create', $params,
493 'contact_id is not valid : 999'
496 function testMembershipCreateWithInvalidStatus() {
497 $params = $this->_params
;
498 $params['status_id'] = 999;
499 $result = $this->callAPIFailure('membership', 'create', $params,
500 "'999' is not a valid option for field status_id"
504 function testMembershipCreateWithInvalidType() {
505 $params = $this->_params
;
506 $params['membership_type_id'] = 999;
508 $result = $this->callAPIFailure('membership', 'create', $params,
509 "'999' is not a valid option for field membership_type_id"
514 * check with complete array + custom field
515 * Note that the test is written on purpose without any
516 * variables specific to participant so it can be replicated into other entities
517 * and / or moved to the automated test suite
519 function testCreateWithCustom() {
520 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
522 $params = $this->_params
;
523 $params['custom_' . $ids['custom_field_id']] = "custom string";
525 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
526 $check = $this->callAPISuccess($this->_entity
, 'get', array('id' => $result['id'], 'contact_id' => $this->_contactID
));
527 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
531 * Test civicrm_contact_memberships_create with membership id (edit
535 function testMembershipCreateWithId() {
536 $membershipID = $this->contactMembershipCreate($this->_params
);
538 'id' => $membershipID,
539 'contact_id' => $this->_contactID
,
540 'membership_type_id' => $this->_membershipTypeID
,
541 'join_date' => '2006-01-21',
542 'start_date' => '2006-01-21',
543 'end_date' => '2006-12-21',
544 'source' => 'Payment',
546 'status_id' => $this->_membershipStatusID
,
549 $result = $this->callAPISuccess('membership', 'create', $params);
550 $this->callAPISuccess('Membership', 'Delete', array(
551 'id' => $result['id'],
553 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
557 * Test civicrm_contact_memberships_create with membership id (edit
561 function testMembershipCreateUpdateWithIdNoContact() {
562 $membershipID = $this->contactMembershipCreate($this->_params
);
564 'id' => $membershipID,
565 'membership_type_id' => $this->_membershipTypeID
,
566 'contact_id' => $this->_contactID
,
567 'join_date' => '2006-01-21',
568 'start_date' => '2006-01-21',
569 'end_date' => '2006-12-21',
570 'source' => 'Payment',
572 'status_id' => $this->_membershipStatusID
,
575 $result = $this->callAPISuccess('membership', 'create', $params);
576 $this->callAPISuccess('Membership', 'Delete', array(
577 'id' => $result['id'],
580 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
584 * Test civicrm_contact_memberships_create with membership id (edit
588 function testMembershipCreateUpdateWithIdNoDates() {
589 $membershipID = $this->contactMembershipCreate($this->_params
);
591 'id' => $membershipID,
592 'contact_id' => $this->_contactID
,
593 'membership_type_id' => $this->_membershipTypeID
,
594 'source' => 'Payment',
596 'status_id' => $this->_membershipStatusID
,
599 $result = $this->callAPISuccess('membership', 'create', $params);
600 $this->callAPISuccess('Membership', 'Delete', array(
601 'id' => $result['id'],
603 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
607 * Test civicrm_contact_memberships_create with membership id (edit
611 function testMembershipCreateUpdateWithIdNoDatesNoType() {
612 $membershipID = $this->contactMembershipCreate($this->_params
);
614 'id' => $membershipID,
615 'source' => 'not much here',
616 'contact_id' => $this->_contactID
,
618 'status_id' => $this->_membershipStatusID
,
621 $result = $this->callAPISuccess('membership', 'create', $params);
622 $this->callAPISuccess('Membership', 'Delete', array(
623 'id' => $result['id'],
625 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
629 * Test civicrm_contact_memberships_create with membership id (edit
633 function testMembershipCreateUpdateWithIDAndSource() {
634 $membershipID = $this->contactMembershipCreate($this->_params
);
636 'id' => $membershipID,
637 'source' => 'changed',
638 'contact_id' => $this->_contactID
,
639 'status_id' => $this->_membershipStatusID
, 'membership_type_id' => $this->_membershipTypeID
,
640 'skipStatusCal' => 1,
642 $result = $this->callAPISuccess('membership', 'create', $params);
643 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
644 $this->callAPISuccess('Membership', 'Delete', array(
645 'id' => $result['id'],
650 * change custom field using update
652 function testUpdateWithCustom() {
653 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
655 $params = $this->_params
;
656 $params['custom_' . $ids['custom_field_id']] = "custom string";
657 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
658 $result = $this->callAPISuccess($this->_entity
, 'create', array('id' => $result['id'], 'custom_' . $ids['custom_field_id'] => "new custom"));
659 $check = $this->callAPISuccess($this->_entity
, 'get', array('id' => $result['id'], 'contact_id' => $this->_contactID
));
661 $this->assertEquals("new custom", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
662 $delete = $this->callAPISuccess('Membership', 'Delete', array(
663 'id' => $check['id'],
666 $this->customFieldDelete($ids['custom_field_id']);
667 $this->customGroupDelete($ids['custom_group_id']);
671 * Test civicrm_contact_memberships_create Invalid membership data
674 function testMembershipCreateInvalidMemData() {
675 //membership_contact_id as string
677 'membership_contact_id' => 'Invalid',
678 'membership_type_id' => $this->_membershipTypeID
,
679 'join_date' => '2011-01-21',
680 'start_date' => '2010-01-21',
681 'end_date' => '2008-12-21',
682 'source' => 'Payment',
684 'status_id' => $this->_membershipStatusID
, );
686 $result = $this->callAPIFailure('membership', 'create', $params);
688 //membership_contact_id which is no in contact table
689 $params['membership_contact_id'] = 999;
690 $result = $this->callAPIFailure('membership', 'create', $params);
693 unset($params['membership_contact_id']);
694 $params['join_date'] = "invalid";
695 $result = $this->callAPIFailure('Membership', 'Create', $params);
699 * Test civicrm_contact_memberships_create with membership_contact_id
703 function testMembershipCreateWithMemContact() {
705 'membership_contact_id' => $this->_contactID
,
706 'membership_type_id' => $this->_membershipTypeID
,
707 'join_date' => '2011-01-21',
708 'start_date' => '2010-01-21',
709 'end_date' => '2008-12-21',
710 'source' => 'Payment',
712 'status_id' => $this->_membershipStatusID
,
715 $result = $this->callAPISuccess('membership', 'create', $params);
717 $result = $this->callAPISuccess('Membership', 'Delete', array(
718 'id' => $result['id'],
722 * Test civicrm_contact_memberships_create with membership_contact_id
726 function testMembershipCreateValidMembershipTypeString() {
728 'membership_contact_id' => $this->_contactID
,
729 'membership_type_id' => 'General',
730 'join_date' => '2011-01-21',
731 'start_date' => '2010-01-21',
732 'end_date' => '2008-12-21',
733 'source' => 'Payment',
735 'status_id' => $this->_membershipStatusID
,
738 $result = $this->callAPISuccess('membership', 'create', $params);
739 $this->assertEquals($this->_membershipTypeID
, $result['values'][$result['id']]['membership_type_id']);
740 $result = $this->callAPISuccess('Membership', 'Delete', array(
741 'id' => $result['id'],
746 * Test civicrm_contact_memberships_create with membership_contact_id
750 function testMembershipCreateInValidMembershipTypeString() {
752 'membership_contact_id' => $this->_contactID
,
753 'membership_type_id' => 'invalid',
754 'join_date' => '2011-01-21',
755 'start_date' => '2010-01-21',
756 'end_date' => '2008-12-21',
757 'source' => 'Payment',
759 'status_id' => $this->_membershipStatusID
,
762 $result = $this->callAPIFailure('membership', 'create', $params);
766 * Test that if membership join date is not set it defaults to today
768 function testEmptyJoinDate() {
769 unset($this->_params
['join_date'], $this->_params
['is_override']);
770 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
771 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
772 $this->assertEquals(date('Y-m-d', strtotime('now')), $result['join_date']);
773 $this->assertEquals('2009-01-21', $result['start_date']);
774 $this->assertEquals('2009-12-21', $result['end_date']);
777 * Test that if membership start date is not set it defaults to correct end date
780 function testEmptyStartDateFixed() {
781 unset($this->_params
['start_date'], $this->_params
['is_override']);
782 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
783 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
784 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
785 $this->assertEquals('2009-01-21', $result['join_date']);
786 $this->assertEquals('2008-03-01', $result['start_date']);
787 $this->assertEquals('2009-12-21', $result['end_date']);
791 * Test that if membership start date is not set it defaults to correct end date
794 function testEmptyStartDateRolling() {
795 unset($this->_params
['start_date'], $this->_params
['is_override']);
796 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
797 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
798 $this->assertEquals('2009-01-21', $result['join_date']);
799 $this->assertEquals('2009-01-21', $result['start_date']);
800 $this->assertEquals('2009-12-21', $result['end_date']);
803 * Test that if membership end date is not set it defaults to correct end date
806 function testEmptyEndDateFixed() {
807 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
808 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
809 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
810 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
811 $this->assertEquals('2009-01-21', $result['join_date']);
812 $this->assertEquals('2008-03-01', $result['start_date']);
813 $this->assertEquals('2010-02-28', $result['end_date']);
816 * Test that if membership end date is not set it defaults to correct end date
819 function testEmptyEndDateRolling() {
820 unset($this->_params
['is_override'], $this->_params
['end_date']);
821 $this->_params
['membership_type_id'] = $this->_membershipTypeID
;
822 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
823 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
824 $this->assertEquals('2009-01-21', $result['join_date']);
825 $this->assertEquals('2009-01-21', $result['start_date']);
826 $this->assertEquals('2010-01-20', $result['end_date']);
831 * Test that if datesdate are not set they not over-ridden if id is passed in
833 function testMembershipDatesNotOverridden() {
834 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
835 unset($this->_params
['end_date'], $this->_params
['start_date']);
836 $this->_params
['id'] = $result['id'];
837 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
838 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
839 $this->assertEquals('2009-01-21', $result['join_date']);
840 $this->assertEquals('2009-01-21', $result['start_date']);
841 $this->assertEquals('2009-12-21', $result['end_date']);