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('period_type' => 'fixed','fixed_period_start_day' => '301', 'fixed_period_rollover_day' => '1111'));
61 $this->_membershipStatusID
= $this->membershipStatusCreate('test status');
63 CRM_Member_PseudoConstant
::membershipType(NULL, TRUE);
64 CRM_Member_PseudoConstant
::membershipStatus(NULL, NULL, 'name', TRUE);
65 CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, TRUE, 'name');
67 $this->_entity
= 'Membership';
68 $this->_params
= array(
69 'contact_id' => $this->_contactID
,
70 'membership_type_id' => $this->_membershipTypeID
,
71 'join_date' => '2009-01-21',
72 'start_date' => '2009-01-21',
73 'end_date' => '2009-12-21',
74 'source' => 'Payment',
76 'status_id' => $this->_membershipStatusID
,
80 public function tearDown() {
81 $this->quickCleanup(array(
83 'civicrm_membership_payment',
84 'civicrm_membership_log',
88 $this->membershipStatusDelete($this->_membershipStatusID
);
89 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID2
));
90 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID
));
91 $this->contactDelete($this->_contactID
);
96 * Test civicrm_membership_delete()
98 public function testMembershipDelete() {
99 $membershipID = $this->contactMembershipCreate($this->_params
);
100 $this->assertDBRowExist('CRM_Member_DAO_Membership', $membershipID);
102 'id' => $membershipID
104 $this->callAPIAndDocument('membership', 'delete', $params, __FUNCTION__
, __FILE__
);
105 $this->assertDBRowNotExist('CRM_Member_DAO_Membership', $membershipID);
108 public function testMembershipDeleteEmpty() {
109 $this->callAPIFailure('membership', 'delete', array());
112 public function testMembershipDeleteInvalidID() {
113 $this->callAPIFailure('membership', 'delete', array('id' => 'blah'));
117 * Test civicrm_membership_delete() with invalid Membership Id
119 public function testMembershipDeleteWithInvalidMembershipId() {
120 $membershipId = 'membership';
121 $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 public function testContactMembershipsGet() {
131 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
133 $this->callAPISuccess('membership', 'get', $params);
134 $this->callAPISuccess('Membership', 'Delete', array('id' => $this->_membershipID
,));
138 * Test civicrm_membership_get with params not array.
139 * Gets treated as contact_id, memberships expected.
141 public function testGetWithParamsContactId() {
142 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
144 'contact_id' => $this->_contactID
,
146 $membership = $this->callAPISuccess('membership', 'get', $params);
148 $result = $membership['values'][$this->_membershipID
];
149 $this->callAPISuccess('Membership', 'Delete', array(
150 'id' => $this->_membershipID
,
152 $this->assertEquals($result['contact_id'], $this->_contactID
, "In line " . __LINE__
);
153 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID
, "In line " . __LINE__
);
154 $this->assertEquals($result['status_id'], $this->_membershipStatusID
, "In line " . __LINE__
);
155 $this->assertEquals($result['join_date'], '2009-01-21', "In line " . __LINE__
);
156 $this->assertEquals($result['start_date'], '2009-01-21', "In line " . __LINE__
);
157 $this->assertEquals($result['end_date'], '2009-12-21', "In line " . __LINE__
);
158 $this->assertEquals($result['source'], 'Payment', "In line " . __LINE__
);
159 $this->assertEquals($result['is_override'], 1, "In line " . __LINE__
);
163 * Test civicrm_membership_get with params not array.
164 * Gets treated as contact_id, memberships expected.
166 public function testGetInSyntax() {
167 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
168 $this->_membershipID2
= $this->contactMembershipCreate($this->_params
);
169 $this->_membershipID3
= $this->contactMembershipCreate($this->_params
);
171 'id' => array('IN' => array($this->_membershipID
, $this->_membershipID3
)),
173 $membership = $this->callAPISuccess('membership', 'get', $params);
174 $this->assertEquals(2, $membership['count']);
175 $this->assertEquals(array($this->_membershipID
, $this->_membershipID3
), array_keys($membership['values']));
177 'id' => array('NOT IN' => array($this->_membershipID
, $this->_membershipID3
)),
179 $membership = $this->callAPISuccess('membership', 'get', $params);
180 $this->assertEquals(1, $membership['count']);
181 $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 public 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.
211 public function testGetWithParamsMemberShipTypeId() {
212 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
214 'membership_type_id' => $this->_membershipTypeID
,
216 $membership = $this->callAPISuccess('membership', 'get', $params);
217 $this->callAPISuccess('Membership', 'Delete', array(
218 'id' => $membership['id'],
220 $result = $membership['values'][$membership['id']];
221 $this->assertEquals($result['contact_id'], $this->_contactID
, "In line " . __LINE__
);
222 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID
, "In line " . __LINE__
);
223 $this->assertEquals($result['status_id'], $this->_membershipStatusID
, "In line " . __LINE__
);
224 $this->assertEquals($result['join_date'], '2009-01-21', "In line " . __LINE__
);
225 $this->assertEquals($result['start_date'], '2009-01-21', "In line " . __LINE__
);
226 $this->assertEquals($result['end_date'], '2009-12-21', "In line " . __LINE__
);
227 $this->assertEquals($result['source'], 'Payment', "In line " . __LINE__
);
228 $this->assertEquals($result['is_override'], 1, "In line " . __LINE__
);
229 $this->assertEquals($result['id'], $membership['id']);
232 * Test civicrm_membership_get with params not array.
233 * Gets treated as contact_id, memberships expected.
235 public function testGetWithParamsMemberShipTypeIdContactID() {
236 $params = $this->_params
;
237 $this->callAPISuccess($this->_entity
, 'create', $params);
238 $params['membership_type_id'] = $this->_membershipTypeID2
;
239 $this->callAPISuccess($this->_entity
, 'create', $params);
240 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_contactID
), 2);
242 'membership_type_id' => $this->_membershipTypeID
,
243 'contact_id' => $this->_contactID
,
245 $result = $this->callAPISuccess('membership', 'getsingle', $params);
246 $this->assertEquals($result['contact_id'], $this->_contactID
);
247 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID
);
250 'membership_type_id' => $this->_membershipTypeID2
,
251 'contact_id' => $this->_contactID
,
253 $result = $this->callAPISuccess('membership', 'getsingle', $params);
254 $this->assertEquals($result['contact_id'], $this->_contactID
);
255 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID2
);
258 * Check with complete array + custom field
259 * Note that the test is written on purpose without any
260 * variables specific to participant so it can be replicated into other entities
261 * and / or moved to the automated test suite
263 public function testGetWithParamsMemberShipIdAndCustom() {
264 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
266 $params = $this->_params
;
267 $params['custom_' . $ids['custom_field_id']] = "custom string";
269 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
271 $getParams = array('membership_type_id' => $params['membership_type_id']);
272 $check = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
273 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
275 $this->callAPISuccess('Membership', 'Delete', array(
276 'id' => $result['id'],
281 * Test civicrm_membership_get with proper params.
282 * Memberships expected.
284 public function testGet() {
285 $membershipID = $this->contactMembershipCreate($this->_params
);
287 'contact_id' => $this->_contactID
,
290 $membership = $this->callAPISuccess('membership', 'get', $params);
291 $result = $membership['values'][$membershipID];
292 $this->callAPISuccess('Membership', 'Delete', array(
293 'id' => $membership['id'],
295 $this->assertEquals($result['join_date'], '2009-01-21', "In line " . __LINE__
);
296 $this->assertEquals($result['contact_id'], $this->_contactID
, "In line " . __LINE__
);
297 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID
, "In line " . __LINE__
);
298 $this->assertEquals($result['status_id'], $this->_membershipStatusID
, "In line " . __LINE__
);
300 $this->assertEquals($result['start_date'], '2009-01-21', "In line " . __LINE__
);
301 $this->assertEquals($result['end_date'], '2009-12-21', "In line " . __LINE__
);
302 $this->assertEquals($result['source'], 'Payment', "In line " . __LINE__
);
303 $this->assertEquals($result['is_override'], 1, "In line " . __LINE__
);
308 * Test civicrm_membership_get with proper params.
309 * Memberships expected.
311 public function testGetWithId() {
312 $membershipID = $this->contactMembershipCreate($this->_params
);
314 'contact_id' => $this->_contactID
,
315 'id' => $this->_membershipID
,
318 $result = $this->callAPISuccess('membership', 'get', $params);
319 $this->assertEquals($membershipID, $result['id']);
321 'contact_id' => $this->_contactID
,
322 'membership_id' => $this->_membershipID
,
323 'return' => 'membership_id',
325 $result = $this->callAPISuccess('membership', 'get', $params);
326 $this->assertEquals($membershipID, $result['id']);
330 * Test civicrm_membership_get for only active.
331 * Memberships expected.
333 public function testGetOnlyActive() {
334 $description = "Demonstrates use of 'filter' active_only' param";
335 $this->_membershipID
= $this->contactMembershipCreate($this->_params
);
336 $subfile = 'filterIsCurrent';
338 'contact_id' => $this->_contactID
,
342 $membership = $this->callAPISuccess('membership', 'get', $params);
343 $this->assertEquals($membership['values'][$this->_membershipID
]['status_id'], $this->_membershipStatusID
);
344 $this->assertEquals($membership['values'][$this->_membershipID
]['contact_id'], $this->_contactID
);
346 'contact_id' => $this->_contactID
,
352 $membership = $this->callAPIAndDocument('membership', 'get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
353 $this->assertEquals($membership['values'][$this->_membershipID
]['status_id'], $this->_membershipStatusID
);
354 $this->assertEquals($membership['values'][$this->_membershipID
]['contact_id'], $this->_contactID
);
357 $this->callAPISuccess('Membership', 'Delete', array('id' => $this->_membershipID
,));
361 * Test civicrm_membership_get for non exist contact.
364 public function testGetNoContactExists() {
366 'contact_id' => 55555,
369 $membership = $this->callAPISuccess('membership', 'get', $params);
370 $this->assertEquals($membership['count'], 0, "In line " . __LINE__
);
374 * Test civicrm_membership_get with relationship.
377 public function testGetWithRelationship() {
378 $membershipOrgId = $this->organizationCreate(NULL);
379 $memberContactId = $this->individualCreate();
381 $relTypeParams = array(
382 'name_a_b' => 'Relation 1',
383 'name_b_a' => 'Relation 2',
384 'description' => 'Testing relationship type',
385 'contact_type_a' => 'Organization',
386 'contact_type_b' => 'Individual',
390 $relTypeID = $this->relationshipTypeCreate($relTypeParams);
393 'name' => 'test General',
394 'duration_unit' => 'year',
395 'duration_interval' => 1,
396 'period_type' => 'rolling',
397 'member_of_contact_id' => $membershipOrgId,
399 'financial_type_id' => 1,
400 'relationship_type_id' => $relTypeID,
401 'relationship_direction' => 'b_a',
404 $memType = $this->callAPISuccess('membership_type', 'create', $params);
407 'contact_id' => $memberContactId,
408 'membership_type_id' => $memType['id'],
409 'join_date' => '2009-01-21',
410 'start_date' => '2009-01-21',
411 'end_date' => '2009-12-21',
412 'source' => 'Payment',
414 'status_id' => $this->_membershipStatusID
,
416 $membershipID = $this->contactMembershipCreate($params);
419 'contact_id' => $memberContactId,
420 'membership_type_id' => $memType['id'],
423 $result = $this->callAPISuccess('membership', 'get', $params);
425 $membership = $result['values'][$membershipID];
426 $this->assertEquals($this->_membershipStatusID
, $membership['status_id']);
427 $this->callAPISuccess('Membership', 'Delete', array(
428 'id' => $membership['id'],
430 $this->membershipTypeDelete(array('id' => $memType['id']));
431 $this->relationshipTypeDelete($relTypeID);
432 $this->contactDelete($membershipOrgId);
433 $this->contactDelete($memberContactId);
437 * Test civicrm_membership_create with relationships.
438 * create/get Memberships.
440 * Test suite for CRM-14758: API ( contact, create ) does not always create related membership
441 * and max_related property for Membership_Type and Membership entities
443 public function testCreateWithRelationship() {
444 // Create membership type: inherited through employment, max_related = 2
446 'name_a_b' => 'Employee of',
448 $result = $this->callAPISuccess('relationship_type', 'get', $params);
449 $relationshipTypeId = $result['id'];
450 $membershipOrgId = $this->organizationCreate();
452 'name' => 'Corporate Membership',
453 'duration_unit' => 'year',
454 'duration_interval' => 1,
455 'period_type' => 'rolling',
456 'member_of_contact_id' => $membershipOrgId,
458 'financial_type_id' => 1,
459 'relationship_type_id' => $relationshipTypeId,
460 'relationship_direction' => 'b_a',
464 $result = $this->callAPISuccess('membership_type', 'create', $params);
465 $membershipTypeId = $result['id'];
467 // Create employer and first employee
468 $employerId[0] = $this->organizationCreate(array(), 1);
469 $memberContactId[0] = $this->individualCreate(array('employer_id' => $employerId[0]), 0);
471 // Create organization's membership
473 'contact_id' => $employerId[0],
474 'membership_type_id' => $membershipTypeId,
475 'source' => 'Test suite',
476 'start_date' => date('Y-m-d'),
477 'end_date' => "+1 year",
479 $OrganizationMembershipID = $this->contactMembershipCreate($params);
481 // Check that the employee inherited the membership
483 'contact_id' => $memberContactId[0],
484 'membership_type_id' => $membershipTypeId,
487 $result = $this->callAPISuccess('membership', 'get', $params);
489 $this->assertEquals(1, $result['count']);
490 $result = $result['values'][$result['id']];
491 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
493 // Create second employee
494 $memberContactId[1] = $this->individualCreate(array('employer_id' => $employerId[0]), 1);
496 // Check that the employee inherited the membership
498 'contact_id' => $memberContactId[1],
499 'membership_type_id' => $membershipTypeId,
501 $result = $this->callAPISuccess('membership', 'get', $params);
502 // If it fails here CRM-14758 is not fixed
503 $this->assertEquals(1, $result['count']);
504 $result = $result['values'][$result['id']];
505 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
507 // Create third employee
508 $memberContactId[2] = $this->individualCreate(array('employer_id' => $employerId[0]), 2);
510 // Check that employee does NOT inherit the membership (max_related = 2)
512 'contact_id' => $memberContactId[2],
513 'membership_type_id' => $membershipTypeId,
515 $result = $this->callAPISuccess('membership', 'get', $params);
516 $this->assertEquals(0, $result['count']);
518 // Increase max_related for the employer's membership
520 'id' => $OrganizationMembershipID,
523 $this->contactMembershipCreate($params);
525 // Check that the employee inherited the membership
527 'contact_id' => $memberContactId[2],
528 'membership_type_id' => $membershipTypeId,
530 $result = $this->callAPISuccess('membership', 'get', $params);
531 $this->assertEquals(1, $result['count']);
532 $result = $result['values'][$result['id']];
533 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
535 // First employee moves to a new job
536 $employerId[1] = $this->organizationCreate(array(), 2);
538 'id' => $memberContactId[0],
539 'employer_id' => $employerId[1],
541 $this->callAPISuccess('contact', 'create', $params);
543 // Check that employee does NO LONGER inherit the membership
545 'contact_id' => $memberContactId[0],
546 'membership_type_id' => $membershipTypeId,
548 $result = $this->callAPISuccess('membership', 'get', $params);
549 $this->assertEquals(0, $result['count']);
551 // Tear down - reverse of creation to be safe
552 $this->contactDelete($memberContactId[2]);
553 $this->contactDelete($memberContactId[1]);
554 $this->contactDelete($memberContactId[0]);
555 $this->contactDelete($employerId[1]);
556 $this->contactDelete($employerId[0]);
557 $this->membershipTypeDelete(array('id' => $membershipTypeId));
558 $this->contactDelete($membershipOrgId);
562 * We are checking for no enotices + only id & end_date returned
564 public function testMembershipGetWithReturn() {
565 $this->contactMembershipCreate($this->_params
);
566 $result = $this->callAPISuccess('membership', 'get', array('return' => 'end_date'));
567 foreach ($result['values'] as $membership) {
568 $this->assertEquals(array('id', 'end_date'), array_keys($membership));
571 ///////////////// civicrm_membership_create methods
574 * Test civicrm_contact_memberships_create with empty params.
577 public function testCreateWithEmptyParams() {
579 $this->callAPIFailure('membership', 'create', $params);
583 * If is_overide is passed in status must also be passed in
585 public function testCreateOverrideNoStatus() {
586 $params = $this->_params
;
587 unset($params['status_id']);
588 $this->callAPIFailure('membership', 'create', $params);
591 public function testMembershipCreateMissingRequired() {
593 'membership_type_id' => '1',
594 'join_date' => '2006-01-21',
595 'start_date' => '2006-01-21',
596 'end_date' => '2006-12-21',
597 'source' => 'Payment',
601 $this->callAPIFailure('membership', 'create', $params);
604 public function testMembershipCreate() {
606 'contact_id' => $this->_contactID
,
607 'membership_type_id' => $this->_membershipTypeID
,
608 'join_date' => '2006-01-21',
609 'start_date' => '2006-01-21',
610 'end_date' => '2006-12-21',
611 'source' => 'Payment',
613 'status_id' => $this->_membershipStatusID
,
616 $result = $this->callAPIAndDocument('membership', 'create', $params, __FUNCTION__
, __FILE__
);
617 $this->getAndCheck($params, $result['id'], $this->_entity
);
618 $this->assertNotNull($result['id']);
619 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['contact_id'], " in line " . __LINE__
);
620 $this->assertEquals($result['id'], $result['values'][$result['id']]['id'], " in line " . __LINE__
);
623 * Check for useful message if contact doesn't exist
625 public function testMembershipCreateWithInvalidContact() {
628 'membership_type_id' => $this->_membershipTypeID
,
629 'join_date' => '2006-01-21',
630 'start_date' => '2006-01-21',
631 'end_date' => '2006-12-21',
632 'source' => 'Payment',
634 'status_id' => $this->_membershipStatusID
,
637 $this->callAPIFailure('membership', 'create', $params,
638 'contact_id is not valid : 999'
641 public function testMembershipCreateWithInvalidStatus() {
642 $params = $this->_params
;
643 $params['status_id'] = 999;
644 $this->callAPIFailure('membership', 'create', $params,
645 "'999' is not a valid option for field status_id"
649 public function testMembershipCreateWithInvalidType() {
650 $params = $this->_params
;
651 $params['membership_type_id'] = 999;
653 $this->callAPIFailure('membership', 'create', $params,
654 "'999' is not a valid option for field membership_type_id"
659 * Check with complete array + custom field
660 * Note that the test is written on purpose without any
661 * variables specific to participant so it can be replicated into other entities
662 * and / or moved to the automated test suite
664 public function testCreateWithCustom() {
665 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
667 $params = $this->_params
;
668 $params['custom_' . $ids['custom_field_id']] = "custom string";
670 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
671 $check = $this->callAPISuccess($this->_entity
, 'get', array('id' => $result['id'], 'contact_id' => $this->_contactID
));
672 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
676 * Test civicrm_contact_memberships_create with membership id (edit
680 public function testMembershipCreateWithId() {
681 $membershipID = $this->contactMembershipCreate($this->_params
);
683 'id' => $membershipID,
684 'contact_id' => $this->_contactID
,
685 'membership_type_id' => $this->_membershipTypeID
,
686 'join_date' => '2006-01-21',
687 'start_date' => '2006-01-21',
688 'end_date' => '2006-12-21',
689 'source' => 'Payment',
691 'status_id' => $this->_membershipStatusID
,
694 $result = $this->callAPISuccess('membership', 'create', $params);
695 $this->callAPISuccess('Membership', 'Delete', array(
696 'id' => $result['id'],
698 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
702 * Test civicrm_contact_memberships_create with membership id (edit
706 public function testMembershipCreateUpdateWithIdNoContact() {
707 $membershipID = $this->contactMembershipCreate($this->_params
);
709 'id' => $membershipID,
710 'membership_type_id' => $this->_membershipTypeID
,
711 'contact_id' => $this->_contactID
,
712 'join_date' => '2006-01-21',
713 'start_date' => '2006-01-21',
714 'end_date' => '2006-12-21',
715 'source' => 'Payment',
717 'status_id' => $this->_membershipStatusID
,
720 $result = $this->callAPISuccess('membership', 'create', $params);
721 $this->callAPISuccess('Membership', 'Delete', array(
722 'id' => $result['id'],
725 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
729 * Test civicrm_contact_memberships_create with membership id (edit
733 public function testMembershipCreateUpdateWithIdNoDates() {
734 $membershipID = $this->contactMembershipCreate($this->_params
);
736 'id' => $membershipID,
737 'contact_id' => $this->_contactID
,
738 'membership_type_id' => $this->_membershipTypeID
,
739 'source' => 'Payment',
741 'status_id' => $this->_membershipStatusID
,
744 $result = $this->callAPISuccess('membership', 'create', $params);
745 $this->callAPISuccess('Membership', 'Delete', array(
746 'id' => $result['id'],
748 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
752 * Test civicrm_contact_memberships_create with membership id (edit
756 public function testMembershipCreateUpdateWithIdNoDatesNoType() {
757 $membershipID = $this->contactMembershipCreate($this->_params
);
759 'id' => $membershipID,
760 'source' => 'not much here',
761 'contact_id' => $this->_contactID
,
763 'status_id' => $this->_membershipStatusID
,
766 $result = $this->callAPISuccess('membership', 'create', $params);
767 $this->callAPISuccess('Membership', 'Delete', array(
768 'id' => $result['id'],
770 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
774 * Test civicrm_contact_memberships_create with membership id (edit
778 public function testMembershipCreateUpdateWithIDAndSource() {
779 $membershipID = $this->contactMembershipCreate($this->_params
);
781 'id' => $membershipID,
782 'source' => 'changed',
783 'contact_id' => $this->_contactID
,
784 'status_id' => $this->_membershipStatusID
, 'membership_type_id' => $this->_membershipTypeID
,
785 'skipStatusCal' => 1,
787 $result = $this->callAPISuccess('membership', 'create', $params);
788 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__
);
789 $this->callAPISuccess('Membership', 'Delete', array(
790 'id' => $result['id'],
795 * Change custom field using update
797 public function testUpdateWithCustom() {
798 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
800 $params = $this->_params
;
801 $params['custom_' . $ids['custom_field_id']] = "custom string";
802 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
803 $result = $this->callAPISuccess($this->_entity
, 'create', array('id' => $result['id'], 'custom_' . $ids['custom_field_id'] => "new custom"));
804 $check = $this->callAPISuccess($this->_entity
, 'get', array('id' => $result['id'], 'contact_id' => $this->_contactID
));
806 $this->assertEquals("new custom", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
807 $this->callAPISuccess('Membership', 'Delete', array(
808 'id' => $check['id'],
811 $this->customFieldDelete($ids['custom_field_id']);
812 $this->customGroupDelete($ids['custom_group_id']);
816 * per CRM-15746 check that the id can be altered in an update hook
818 function testMembershipUpdateCreateHookCRM15746() {
819 $this->hookClass
->setHook('civicrm_pre', array($this, 'hook_civicrm_pre_update_create_membership'));
820 $result = $this->callAPISuccess('membership', 'create', $this->_params
);
821 $this->callAPISuccess('membership', 'create', array('id' => $result['id'], 'end_date' => '1 year ago'));
822 $this->callAPISuccessGetCount('membership', array(), 2);
823 $this->hookClass
->reset();
824 $this->callAPISuccess('membership', 'create', array('id' => $result['id'], 'end_date' => '1 year ago'));
825 $this->callAPISuccessGetCount('membership', array(), 2);
828 function hook_civicrm_pre_update_create_membership($op, $objectName, $id, &$params) {
829 if ($objectName == 'Membership' && $op == 'edit') {
830 $existingMembership = $this->callAPISuccessGetSingle('membership', array('id' => $params['id']));
831 unset($params['id'], $params['membership_id']);
832 $params['join_date'] = $params['membership_start_date'] = $params['start_date']= date('Ymd000000', strtotime($existingMembership['start_date']));
833 $params = array_merge($existingMembership, $params);
834 $params['id'] = NULL;
839 * Test civicrm_contact_memberships_create Invalid membership data
842 public function testMembershipCreateInvalidMemData() {
843 //membership_contact_id as string
845 'membership_contact_id' => 'Invalid',
846 'membership_type_id' => $this->_membershipTypeID
,
847 'join_date' => '2011-01-21',
848 'start_date' => '2010-01-21',
849 'end_date' => '2008-12-21',
850 'source' => 'Payment',
852 'status_id' => $this->_membershipStatusID
, );
854 $this->callAPIFailure('membership', 'create', $params);
856 //membership_contact_id which is no in contact table
857 $params['membership_contact_id'] = 999;
858 $this->callAPIFailure('membership', 'create', $params);
861 unset($params['membership_contact_id']);
862 $params['join_date'] = "invalid";
863 $this->callAPIFailure('Membership', 'Create', $params);
867 * Test civicrm_contact_memberships_create with membership_contact_id
871 public function testMembershipCreateWithMemContact() {
873 'membership_contact_id' => $this->_contactID
,
874 'membership_type_id' => $this->_membershipTypeID
,
875 'join_date' => '2011-01-21',
876 'start_date' => '2010-01-21',
877 'end_date' => '2008-12-21',
878 'source' => 'Payment',
880 'status_id' => $this->_membershipStatusID
,
883 $result = $this->callAPISuccess('membership', 'create', $params);
885 $this->callAPISuccess('Membership', 'Delete', array(
886 'id' => $result['id'],
890 * Test civicrm_contact_memberships_create with membership_contact_id
894 public function testMembershipCreateValidMembershipTypeString() {
896 'membership_contact_id' => $this->_contactID
,
897 'membership_type_id' => 'General',
898 'join_date' => '2011-01-21',
899 'start_date' => '2010-01-21',
900 'end_date' => '2008-12-21',
901 'source' => 'Payment',
903 'status_id' => $this->_membershipStatusID
,
906 $result = $this->callAPISuccess('membership', 'create', $params);
907 $this->assertEquals($this->_membershipTypeID
, $result['values'][$result['id']]['membership_type_id']);
908 $this->callAPISuccess('Membership', 'Delete', array(
909 'id' => $result['id'],
914 * Test civicrm_contact_memberships_create with membership_contact_id
918 public function testMembershipCreateInValidMembershipTypeString() {
920 'membership_contact_id' => $this->_contactID
,
921 'membership_type_id' => 'invalid',
922 'join_date' => '2011-01-21',
923 'start_date' => '2010-01-21',
924 'end_date' => '2008-12-21',
925 'source' => 'Payment',
927 'status_id' => $this->_membershipStatusID
,
930 $this->callAPIFailure('membership', 'create', $params);
934 * Test that if membership join date is not set it defaults to today
936 public function testEmptyJoinDate() {
937 unset($this->_params
['join_date'], $this->_params
['is_override']);
938 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
939 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
940 $this->assertEquals(date('Y-m-d', strtotime('now')), $result['join_date']);
941 $this->assertEquals('2009-01-21', $result['start_date']);
942 $this->assertEquals('2009-12-21', $result['end_date']);
945 * Test that if membership start date is not set it defaults to correct end date
948 public function testEmptyStartDateFixed() {
949 unset($this->_params
['start_date'], $this->_params
['is_override']);
950 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
951 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
952 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
953 $this->assertEquals('2009-01-21', $result['join_date']);
954 $this->assertEquals('2008-03-01', $result['start_date']);
955 $this->assertEquals('2009-12-21', $result['end_date']);
959 * Test that if membership start date is not set it defaults to correct end date
962 public function testEmptyStartDateRolling() {
963 unset($this->_params
['start_date'], $this->_params
['is_override']);
964 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
965 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
966 $this->assertEquals('2009-01-21', $result['join_date']);
967 $this->assertEquals('2009-01-21', $result['start_date']);
968 $this->assertEquals('2009-12-21', $result['end_date']);
971 * Test that if membership end date is not set it defaults to correct end date
974 public function testEmptyEndDateFixed() {
975 unset($this->_params
['start_date'], $this->_params
['is_override'], $this->_params
['end_date']);
976 $this->_params
['membership_type_id'] = $this->_membershipTypeID2
;
977 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
978 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
979 $this->assertEquals('2009-01-21', $result['join_date']);
980 $this->assertEquals('2008-03-01', $result['start_date']);
981 $this->assertEquals('2010-02-28', $result['end_date']);
984 * Test that if membership end date is not set it defaults to correct end date
987 public function testEmptyEndDateRolling() {
988 unset($this->_params
['is_override'], $this->_params
['end_date']);
989 $this->_params
['membership_type_id'] = $this->_membershipTypeID
;
990 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
991 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
992 $this->assertEquals('2009-01-21', $result['join_date']);
993 $this->assertEquals('2009-01-21', $result['start_date']);
994 $this->assertEquals('2010-01-20', $result['end_date']);
999 * Test that if datesdate are not set they not over-ridden if id is passed in
1001 public function testMembershipDatesNotOverridden() {
1002 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1003 unset($this->_params
['end_date'], $this->_params
['start_date']);
1004 $this->_params
['id'] = $result['id'];
1005 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1006 $result = $this->callAPISuccess($this->_entity
, 'getsingle', array('id' => $result['id']));
1007 $this->assertEquals('2009-01-21', $result['join_date']);
1008 $this->assertEquals('2009-01-21', $result['start_date']);
1009 $this->assertEquals('2009-12-21', $result['end_date']);