Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-07-14-13-42-39
[civicrm-core.git] / tests / phpunit / api / v3 / MembershipTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06a1bc01 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06a1bc01 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 * Test APIv3 civicrm_membership functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Member
33 */
34
35
36require_once 'CiviTest/CiviUnitTestCase.php';
37
4cbe18b8
EM
38/**
39 * Class api_v3_MembershipTest
40 */
6a488035
TO
41class api_v3_MembershipTest extends CiviUnitTestCase {
42 protected $_apiversion;
43 protected $_contactID;
44 protected $_membershipTypeID;
8c33a68c 45 protected $_membershipTypeID2;
6a488035
TO
46 protected $_membershipStatusID;
47 protected $__membershipID;
48 protected $_entity;
49 protected $_params;
b7c9bc4c 50
6a488035
TO
51
52 public function setUp() {
53 // Connect to the database
54 parent::setUp();
55 $this->_apiversion = 3;
56 $this->_contactID = $this->individualCreate();
75638074 57 $this->_membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID));
8c33a68c 58 $this->_membershipTypeID2 = $this->membershipTypeCreate(array('period_type' => 'fixed','fixed_period_start_day' => '301', 'fixed_period_rollover_day' => '1111'));
6a488035
TO
59 $this->_membershipStatusID = $this->membershipStatusCreate('test status');
60
6a488035
TO
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');
64
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',
73 'is_override' => 1,
74 'status_id' => $this->_membershipStatusID,
6a488035
TO
75 );
76 }
77
78 function tearDown() {
79 $this->quickCleanup(array(
80 'civicrm_membership',
771f3245 81 'civicrm_membership_payment',
82 'civicrm_membership_log',
83 ),
84 TRUE
6a488035
TO
85 );
86 $this->membershipStatusDelete($this->_membershipStatusID);
8c33a68c 87 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID2));
6a488035
TO
88 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID));
89 $this->contactDelete($this->_contactID);
90
91 }
92
93 /**
94 * Test civicrm_membership_delete()
95 */
96 function testMembershipDelete() {
97 $membershipID = $this->contactMembershipCreate($this->_params);
3506b6cd 98 $this->assertDBRowExist('CRM_Member_DAO_Membership', $membershipID);
6a488035 99 $params = array(
3506b6cd 100 'id' => $membershipID
6a488035 101 );
771f3245 102 $result = $this->callAPIAndDocument('membership', 'delete', $params, __FUNCTION__, __FILE__);
3506b6cd 103 $this->assertDBRowNotExist('CRM_Member_DAO_Membership', $membershipID);
6a488035
TO
104 }
105
106 function testMembershipDeleteEmpty() {
107 $params = array();
d0e1eff2 108 $result = $this->callAPIFailure('membership', 'delete', $params);
6a488035
TO
109 }
110
111 function testMembershipDeleteInvalidID() {
771f3245 112 $params = array('id' => 'blah');
d0e1eff2 113 $result = $this->callAPIFailure('membership', 'delete', $params);
6a488035
TO
114 }
115
116 /**
117 * Test civicrm_membership_delete() with invalid Membership Id
118 */
119 function testMembershipDeleteWithInvalidMembershipId() {
120 $membershipId = 'membership';
771f3245 121 $result = $this->callAPIFailure('membership', 'delete', $membershipId);
6a488035
TO
122 }
123
124 /**
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.
129 */
130 function testContactMembershipsGet() {
131 $this->_membershipID = $this->contactMembershipCreate($this->_params);
771f3245 132 $params = array();
133 $result = $this->callAPISuccess('membership', 'get', $params);
134 $result = $this->callAPISuccess('Membership', 'Delete', array(
6a488035 135 'id' => $this->_membershipID,
771f3245 136 ));
6a488035
TO
137 }
138
139 /**
140 * Test civicrm_membership_get with params not array.
141 * Gets treated as contact_id, memberships expected.
142 */
143 function testGetWithParamsContactId() {
144 $this->_membershipID = $this->contactMembershipCreate($this->_params);
145 $params = array(
146 'contact_id' => $this->_contactID,
6a488035 147 );
771f3245 148 $membership = $this->callAPISuccess('membership', 'get', $params);
6a488035
TO
149
150 $result = $membership['values'][$this->_membershipID];
771f3245 151 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 152 'id' => $this->_membershipID,
6a488035
TO
153 ));
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__);
162 }
163
b4529041 164 /**
165 * Test civicrm_membership_get with params not array.
166 * Gets treated as contact_id, memberships expected.
167 */
168 function testGetInSyntax() {
169 $this->_membershipID = $this->contactMembershipCreate($this->_params);
170 $this->_membershipID2 = $this->contactMembershipCreate($this->_params);
171 $this->_membershipID3 = $this->contactMembershipCreate($this->_params);
172 $params = array(
173 'id' => array('IN' => array($this->_membershipID, $this->_membershipID3)),
174 );
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']));
178 $params = array(
179 'id' => array('NOT IN' => array($this->_membershipID, $this->_membershipID3)),
180 );
181 $membership = $this->callAPISuccess('membership', 'get', $params);
182 $this->assertEquals(1, $membership['count']);
183 $this->assertEquals(array($this->_membershipID2), array_keys($membership['values']));
184
185 }
186
caca32ba 187 /**
188 * Test civicrm_membership_get with params not array.
189 * Gets treated as contact_id, memberships expected.
190 */
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)));
197 $params = array(
198 'contact_id' => array('IN' => array($this->_contactID, $contact3)),
199 );
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']));
203 $params = array(
204 'contact_id' => array('NOT IN' => array($this->_contactID, $contact3)),
205 );
206 $membership = $this->callAPISuccess('membership', 'get', $params);
207 $this->assertEquals(1, $membership['count']);
208 $this->assertEquals(array($this->_membershipID2), array_keys($membership['values']));
209 }
210 /**
211 * Test civicrm_membership_get with params not array.
212 * Gets treated as contact_id, memberships expected.
213 */
6a488035 214 function testGetWithParamsMemberShipTypeId() {
771f3245 215 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
6a488035
TO
216 $params = array(
217 'membership_type_id' => $this->_membershipTypeID,
6a488035 218 );
771f3245 219 $membership = $this->callAPISuccess('membership', 'get', $params);
220 $result = $this->callAPISuccess('Membership', 'Delete', array(
6a488035 221 'id' => $membership['id'],
771f3245 222 ));
6a488035
TO
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']);
233 }
a73daeff
E
234 /**
235 * Test civicrm_membership_get with params not array.
236 * Gets treated as contact_id, memberships expected.
237 */
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);
244 $params = array(
245 'membership_type_id' => $this->_membershipTypeID,
246 'contact_id' => $this->_contactID,
247 );
248 $result = $this->callAPISuccess('membership', 'getsingle', $params);
249 $this->assertEquals($result['contact_id'], $this->_contactID);
250 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID);
6a488035 251
a73daeff
E
252 $params = array(
253 'membership_type_id' => $this->_membershipTypeID2,
254 'contact_id' => $this->_contactID,
255 );
256 $result = $this->callAPISuccess('membership', 'getsingle', $params);
257 $this->assertEquals($result['contact_id'], $this->_contactID);
258 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID2);
259 }
6a488035
TO
260 /**
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
265 */
266 function testGetWithParamsMemberShipIdAndCustom() {
267 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
268
269 $params = $this->_params;
270 $params['custom_' . $ids['custom_field_id']] = "custom string";
271
771f3245 272 $result = $this->callAPISuccess($this->_entity, 'create', $params);
6a488035 273
771f3245 274 $getParams = array('membership_type_id' => $params['membership_type_id']);
275 $check = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
6a488035
TO
276 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
277
771f3245 278 $result = $this->callAPISuccess('Membership', 'Delete', array(
6a488035 279 'id' => $result['id'],
771f3245 280 ));
6a488035
TO
281 }
282
283 /**
284 * Test civicrm_membership_get with proper params.
285 * Memberships expected.
286 */
287 function testGet() {
288 $membershipID = $this->contactMembershipCreate($this->_params);
289 $params = array(
290 'contact_id' => $this->_contactID,
6a488035
TO
291 );
292
771f3245 293 $membership = $this->callAPISuccess('membership', 'get', $params);
6a488035 294 $result = $membership['values'][$membershipID];
771f3245 295 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 296 'id' => $membership['id'],
771f3245 297 ));
6a488035
TO
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__);
302
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__);
307 }
308
309
310 /**
311 * Test civicrm_membership_get with proper params.
312 * Memberships expected.
313 */
314 function testGetWithId() {
315 $membershipID = $this->contactMembershipCreate($this->_params);
316 $params = array(
317 'contact_id' => $this->_contactID,
6a488035
TO
318 'id' => $this->__membershipID,
319 'return' => 'id',
320 );
771f3245 321 $result = $this->callAPISuccess('membership', 'get', $params);
6a488035
TO
322 $this->assertEquals($membershipID, $result['id']);
323 $params = array(
324 'contact_id' => $this->_contactID,
6a488035
TO
325 'membership_id' => $this->__membershipID,
326 'return' => 'membership_id',
327 );
771f3245 328 $result = $this->callAPISuccess('membership', 'get', $params);
6a488035 329 $this->assertEquals($membershipID, $result['id']);
6a488035
TO
330 }
331
332 /**
333 * Test civicrm_membership_get for only active.
334 * Memberships expected.
335 */
336 function testGetOnlyActive() {
337 $description = "Demonstrates use of 'filter' active_only' param";
338 $this->_membershipID = $this->contactMembershipCreate($this->_params);
339 $subfile = 'filterIsCurrent';
340 $params = array(
341 'contact_id' => $this->_contactID,
342 'active_only' => 1,
6a488035
TO
343 );
344
771f3245 345 $membership = $this->callAPISuccess('membership', 'get', $params);
6a488035 346 $result = $membership['values'][$this->_membershipID];
a73daeff
E
347 $this->assertEquals($membership['values'][$this->_membershipID]['status_id'], $this->_membershipStatusID);
348 $this->assertEquals($membership['values'][$this->_membershipID]['contact_id'], $this->_contactID);
6a488035
TO
349 $params = array(
350 'contact_id' => $this->_contactID,
351 'filters' => array(
352 'is_current' => 1,
353 ),
6a488035
TO
354 );
355
771f3245 356 $membership = $this->callAPIAndDocument('membership', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile);
6a488035 357 $result = $membership['values'][$this->_membershipID];
a73daeff
E
358 $this->assertEquals($membership['values'][$this->_membershipID]['status_id'], $this->_membershipStatusID);
359 $this->assertEquals($membership['values'][$this->_membershipID]['contact_id'], $this->_contactID);
6a488035
TO
360
361
771f3245 362 $result = $this->callAPISuccess('Membership', 'Delete', array(
6a488035 363 'id' => $this->_membershipID,
771f3245 364 ));
6a488035
TO
365 }
366
367 /**
368 * Test civicrm_membership_get for non exist contact.
369 * empty Memberships.
370 */
371 function testGetNoContactExists() {
372 $params = array(
373 'contact_id' => 55555,
6a488035
TO
374 );
375
771f3245 376 $membership = $this->callAPISuccess('membership', 'get', $params);
6a488035
TO
377 $this->assertEquals($membership['count'], 0, "In line " . __LINE__);
378 }
379
380 /**
381 * Test civicrm_membership_get with relationship.
382 * get Memberships.
383 */
384 function testGetWithRelationship() {
385 $membershipOrgId = $this->organizationCreate(NULL);
e4d5f1e2 386 $memberContactId = $this->individualCreate();
6a488035
TO
387
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',
394 'is_reserved' => 1,
395 'is_active' => 1,
6a488035
TO
396 );
397 $relTypeID = $this->relationshipTypeCreate($relTypeParams);
398
399 $params = array(
400 'name' => 'test General',
401 'duration_unit' => 'year',
402 'duration_interval' => 1,
403 'period_type' => 'rolling',
404 'member_of_contact_id' => $membershipOrgId,
405 'domain_id' => 1,
771f3245 406 'financial_type_id' => 1,
6a488035
TO
407 'relationship_type_id' => $relTypeID,
408 'relationship_direction' => 'b_a',
409 'is_active' => 1,
6a488035 410 );
771f3245 411 $memType = $this->callAPISuccess('membership_type', 'create', $params);
6a488035
TO
412
413 $params = array(
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',
420 'is_override' => 1,
421 'status_id' => $this->_membershipStatusID,
6a488035
TO
422 );
423 $membershipID = $this->contactMembershipCreate($params);
424
425 $params = array(
426 'contact_id' => $memberContactId,
427 'membership_type_id' => $memType['id'],
6a488035
TO
428 );
429
771f3245 430 $result = $this->callAPISuccess('membership', 'get', $params);
6a488035
TO
431
432 $membership = $result['values'][$membershipID];
771f3245 433 $this->assertEquals($this->_membershipStatusID, $membership['status_id']);
434 $result = $this->callAPISuccess('Membership', 'Delete', array(
6a488035 435 'id' => $membership['id'],
6a488035
TO
436 ));
437 $this->membershipTypeDelete(array('id' => $memType['id']));
438 $this->relationshipTypeDelete($relTypeID);
439 $this->contactDelete($membershipOrgId);
440 $this->contactDelete($memberContactId);
441 }
442
4cc99d00 443 /**
444 * Test civicrm_membership_create with relationships.
445 * create/get Memberships.
446 *
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
449 */
450 function testCreateWithRelationship() {
451 // Create membership type: inherited through employment, max_related = 2
452 $params = array(
453 'name_a_b' => 'Employee of',
454 );
455 $result = $this->callAPISuccess('relationship_type', 'get', $params);
456 $relationshipTypeId = $result['id'];
457 $membershipOrgId = $this->organizationCreate();
458 $params = array(
459 'name' => 'Corporate Membership',
460 'duration_unit' => 'year',
461 'duration_interval' => 1,
462 'period_type' => 'rolling',
463 'member_of_contact_id' => $membershipOrgId,
464 'domain_id' => 1,
465 'financial_type_id' => 1,
466 'relationship_type_id' => $relationshipTypeId,
467 'relationship_direction' => 'b_a',
468 'max_related' => 2,
469 'is_active' => 1,
470 );
471 $result = $this->callAPISuccess('membership_type', 'create', $params);
472 $membershipTypeId = $result['id'];
473
474 // Create employer and first employee
475 $employerId[0] = $this->organizationCreate(array(), 1);
476 $memberContactId[0] = $this->individualCreate(array('employer_id' => $employerId[0]), 0);
477
478 // Create organization's membership
479 $params = array(
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",
485 );
486 $OrganizationMembershipID = $this->contactMembershipCreate($params);
487
488 // Check that the employee inherited the membership
489 $params = array(
490 'contact_id' => $memberContactId[0],
491 'membership_type_id' => $membershipTypeId,
492 );
493
494 $result = $this->callAPISuccess('membership', 'get', $params);
495
496 $this->assertEquals(1, $result['count']);
497 $result = $result['values'][$result['id']];
498 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
499
500 // Create second employee
501 $memberContactId[1] = $this->individualCreate(array('employer_id' => $employerId[0]), 1);
502
503 // Check that the employee inherited the membership
504 $params = array(
505 'contact_id' => $memberContactId[1],
506 'membership_type_id' => $membershipTypeId,
507 );
508 $result = $this->callAPISuccess('membership', 'get', $params);
509 //exit;
510 // If it fails here CRM-14758 is not fixed
511 $this->assertEquals(1, $result['count']);
512 $result = $result['values'][$result['id']];
513 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
514
515 // Create third employee
516 $memberContactId[2] = $this->individualCreate(array('current_employer' => $employerId[0]), 2);
517
518 // Check that employee does NOT inherit the membership (max_related = 2)
519 $params = array(
520 'contact_id' => $memberContactId[2],
521 'membership_type_id' => $membershipTypeId,
522 );
523 $result = $this->callAPISuccess('membership', 'get', $params);
524 $this->assertEquals(0, $result['count']);
525
526 // Increase max_related for the employer's membership
527 $params = array(
528 'id' => $OrganizationMembershipID,
529 'max_related' => 3,
530 );
531 $this->contactMembershipCreate($params);
532
533 // Check that the employee inherited the membership
534 $params = array(
535 'contact_id' => $memberContactId[2],
536 'membership_type_id' => $membershipTypeId,
537 );
538 $result = $this->callAPISuccess('membership', 'get', $params);
539 $this->assertEquals(1, $result['count']);
540 $result = $result['values'][$result['id']];
541 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
542
543 // First employee moves to a new job
544 $employerId[1] = $this->organizationCreate(array(), 2);
545 $params = array(
546 'id' => $memberContactId[0],
547 'employer_id' => $employerId[1],
548 );
549 $this->callAPISuccess('contact', 'create', $params);
550
551 // Check that employee does NO LONGER inherit the membership
552 $params = array(
553 'contact_id' => $memberContactId[0],
554 'membership_type_id' => $membershipTypeId,
555 );
556 $result = $this->callAPISuccess('membership', 'get', $params);
557 $this->assertEquals(0, $result['count']);
558
559 // Tear down - reverse of creation to be safe
560 $this->contactDelete($memberContactId[2]);
561 $this->contactDelete($memberContactId[1]);
562 $this->contactDelete($memberContactId[0]);
563 $this->contactDelete($employerId[1]);
564 $this->contactDelete($employerId[0]);
565 $this->membershipTypeDelete(array('id' => $membershipTypeId));
566 $this->contactDelete($membershipOrgId);
567 }
568
37eda84b 569 /**
570 * We are checking for no enotices + only id & end_date returned
571 */
572 function testMembershipGetWithReturn() {
573 $membershipID = $this->contactMembershipCreate($this->_params);
574 $result = $this->callAPISuccess('membership', 'get', array('return' => 'end_date'));
575 foreach ($result['values'] as $membership) {
576 $this->assertEquals(array('id', 'end_date'), array_keys($membership));
577 }
578 }
6a488035
TO
579 ///////////////// civicrm_membership_create methods
580
581 /**
582 * Test civicrm_contact_memberships_create with empty params.
583 * Error expected.
584 */
585 function testCreateWithEmptyParams() {
586 $params = array();
d0e1eff2 587 $result = $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
588 }
589
590 /**
6a488035
TO
591 * If is_overide is passed in status must also be passed in
592 */
593 function testCreateOverrideNoStatus() {
594 $params = $this->_params;
595 unset($params['status_id']);
771f3245 596 $result = $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
597 }
598
599 function testMembershipCreateMissingRequired() {
600 $params = array(
601 'membership_type_id' => '1',
602 'join_date' => '2006-01-21',
603 'start_date' => '2006-01-21',
604 'end_date' => '2006-12-21',
605 'source' => 'Payment',
606 'status_id' => '2',
6a488035
TO
607 );
608
d0e1eff2 609 $result = $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
610 }
611
612 function testMembershipCreate() {
613 $params = array(
614 'contact_id' => $this->_contactID,
615 'membership_type_id' => $this->_membershipTypeID,
616 'join_date' => '2006-01-21',
617 'start_date' => '2006-01-21',
618 'end_date' => '2006-12-21',
619 'source' => 'Payment',
620 'is_override' => 1,
621 'status_id' => $this->_membershipStatusID,
6a488035
TO
622 );
623
771f3245 624 $result = $this->callAPIAndDocument('membership', 'create', $params, __FUNCTION__, __FILE__);
6a488035 625 $this->getAndCheck($params, $result['id'], $this->_entity);
6a488035
TO
626 $this->assertNotNull($result['id']);
627 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['contact_id'], " in line " . __LINE__);
628 $this->assertEquals($result['id'], $result['values'][$result['id']]['id'], " in line " . __LINE__);
629 }
630 /*
631 * Check for useful message if contact doesn't exist
632 */
633 function testMembershipCreateWithInvalidContact() {
634 $params = array(
635 'contact_id' => 999,
636 'membership_type_id' => $this->_membershipTypeID,
637 'join_date' => '2006-01-21',
638 'start_date' => '2006-01-21',
639 'end_date' => '2006-12-21',
640 'source' => 'Payment',
641 'is_override' => 1,
642 'status_id' => $this->_membershipStatusID,
6a488035
TO
643 );
644
771f3245 645 $result = $this->callAPIFailure('membership', 'create', $params,
646 'contact_id is not valid : 999'
647 );
6a488035
TO
648 }
649 function testMembershipCreateWithInvalidStatus() {
650 $params = $this->_params;
651 $params['status_id'] = 999;
771f3245 652 $result = $this->callAPIFailure('membership', 'create', $params,
653 "'999' is not a valid option for field status_id"
654 );
6a488035
TO
655 }
656
657 function testMembershipCreateWithInvalidType() {
658 $params = $this->_params;
659 $params['membership_type_id'] = 999;
660
771f3245 661 $result = $this->callAPIFailure('membership', 'create', $params,
662 "'999' is not a valid option for field membership_type_id"
663 );
6a488035
TO
664 }
665
666 /**
667 * check with complete array + custom field
668 * Note that the test is written on purpose without any
669 * variables specific to participant so it can be replicated into other entities
670 * and / or moved to the automated test suite
671 */
672 function testCreateWithCustom() {
673 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
674
675 $params = $this->_params;
676 $params['custom_' . $ids['custom_field_id']] = "custom string";
677
771f3245 678 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
679 $check = $this->callAPISuccess($this->_entity, 'get', array('id' => $result['id'], 'contact_id' => $this->_contactID));
6a488035 680 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
6a488035
TO
681 }
682
683 /**
684 * Test civicrm_contact_memberships_create with membership id (edit
685 * membership).
686 * success expected.
687 */
688 function testMembershipCreateWithId() {
689 $membershipID = $this->contactMembershipCreate($this->_params);
690 $params = array(
691 'id' => $membershipID,
692 'contact_id' => $this->_contactID,
693 'membership_type_id' => $this->_membershipTypeID,
694 'join_date' => '2006-01-21',
695 'start_date' => '2006-01-21',
696 'end_date' => '2006-12-21',
697 'source' => 'Payment',
698 'is_override' => 1,
699 'status_id' => $this->_membershipStatusID,
6a488035
TO
700 );
701
771f3245 702 $result = $this->callAPISuccess('membership', 'create', $params);
703 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 704 'id' => $result['id'],
771f3245 705 ));
6a488035
TO
706 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
707 }
708
709 /**
710 * Test civicrm_contact_memberships_create with membership id (edit
711 * membership).
712 * success expected.
713 */
714 function testMembershipCreateUpdateWithIdNoContact() {
715 $membershipID = $this->contactMembershipCreate($this->_params);
716 $params = array(
717 'id' => $membershipID,
718 'membership_type_id' => $this->_membershipTypeID,
719 'contact_id' => $this->_contactID,
720 'join_date' => '2006-01-21',
721 'start_date' => '2006-01-21',
722 'end_date' => '2006-12-21',
723 'source' => 'Payment',
724 'is_override' => 1,
725 'status_id' => $this->_membershipStatusID,
6a488035
TO
726 );
727
771f3245 728 $result = $this->callAPISuccess('membership', 'create', $params);
729 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 730 'id' => $result['id'],
6a488035 731 ));
771f3245 732
6a488035
TO
733 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
734 }
735
736 /**
737 * Test civicrm_contact_memberships_create with membership id (edit
738 * membership).
739 * success expected.
740 */
741 function testMembershipCreateUpdateWithIdNoDates() {
742 $membershipID = $this->contactMembershipCreate($this->_params);
743 $params = array(
744 'id' => $membershipID,
745 'contact_id' => $this->_contactID,
746 'membership_type_id' => $this->_membershipTypeID,
747 'source' => 'Payment',
748 'is_override' => 1,
749 'status_id' => $this->_membershipStatusID,
6a488035
TO
750 );
751
771f3245 752 $result = $this->callAPISuccess('membership', 'create', $params);
753 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 754 'id' => $result['id'],
771f3245 755 ));
6a488035
TO
756 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
757 }
758
759 /**
760 * Test civicrm_contact_memberships_create with membership id (edit
761 * membership).
762 * success expected.
763 */
764 function testMembershipCreateUpdateWithIdNoDatesNoType() {
765 $membershipID = $this->contactMembershipCreate($this->_params);
766 $params = array(
767 'id' => $membershipID,
768 'source' => 'not much here',
769 'contact_id' => $this->_contactID,
770 'is_override' => 1,
771 'status_id' => $this->_membershipStatusID,
6a488035
TO
772 );
773
771f3245 774 $result = $this->callAPISuccess('membership', 'create', $params);
775 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 776 'id' => $result['id'],
771f3245 777 ));
6a488035
TO
778 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
779 }
780
781 /**
782 * Test civicrm_contact_memberships_create with membership id (edit
783 * membership).
784 * success expected.
785 */
786 function testMembershipCreateUpdateWithIDAndSource() {
787 $membershipID = $this->contactMembershipCreate($this->_params);
788 $params = array(
789 'id' => $membershipID,
790 'source' => 'changed',
791 'contact_id' => $this->_contactID,
771f3245 792 'status_id' => $this->_membershipStatusID, 'membership_type_id' => $this->_membershipTypeID,
6a488035
TO
793 'skipStatusCal' => 1,
794 );
771f3245 795 $result = $this->callAPISuccess('membership', 'create', $params);
6a488035 796 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
771f3245 797 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 798 'id' => $result['id'],
6a488035
TO
799 ));
800 }
801
802 /**
803 * change custom field using update
804 */
805 function testUpdateWithCustom() {
806 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
807
808 $params = $this->_params;
809 $params['custom_' . $ids['custom_field_id']] = "custom string";
771f3245 810 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
811 $result = $this->callAPISuccess($this->_entity, 'create', array('id' => $result['id'], 'custom_' . $ids['custom_field_id'] => "new custom"));
812 $check = $this->callAPISuccess($this->_entity, 'get', array('id' => $result['id'], 'contact_id' => $this->_contactID));
6a488035
TO
813
814 $this->assertEquals("new custom", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
771f3245 815 $delete = $this->callAPISuccess('Membership', 'Delete', array(
6a488035 816 'id' => $check['id'],
6a488035
TO
817 ));
818
819 $this->customFieldDelete($ids['custom_field_id']);
820 $this->customGroupDelete($ids['custom_group_id']);
821 }
822
823 /**
824 * Test civicrm_contact_memberships_create Invalid membership data
825 * Error expected.
826 */
827 function testMembershipCreateInvalidMemData() {
828 //membership_contact_id as string
829 $params = array(
830 'membership_contact_id' => 'Invalid',
831 'membership_type_id' => $this->_membershipTypeID,
832 'join_date' => '2011-01-21',
833 'start_date' => '2010-01-21',
834 'end_date' => '2008-12-21',
835 'source' => 'Payment',
836 'is_override' => 1,
771f3245 837 'status_id' => $this->_membershipStatusID, );
6a488035 838
d0e1eff2 839 $result = $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
840
841 //membership_contact_id which is no in contact table
842 $params['membership_contact_id'] = 999;
d0e1eff2 843 $result = $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
844
845 //invalid join date
846 unset($params['membership_contact_id']);
847 $params['join_date'] = "invalid";
d0e1eff2 848 $result = $this->callAPIFailure('Membership', 'Create', $params);
6a488035
TO
849 }
850
851 /**
852 * Test civicrm_contact_memberships_create with membership_contact_id
853 * membership).
854 * Success expected.
855 */
856 function testMembershipCreateWithMemContact() {
857 $params = array(
858 'membership_contact_id' => $this->_contactID,
859 'membership_type_id' => $this->_membershipTypeID,
860 'join_date' => '2011-01-21',
861 'start_date' => '2010-01-21',
862 'end_date' => '2008-12-21',
863 'source' => 'Payment',
864 'is_override' => 1,
865 'status_id' => $this->_membershipStatusID,
6a488035
TO
866 );
867
771f3245 868 $result = $this->callAPISuccess('membership', 'create', $params);
6a488035 869
771f3245 870 $result = $this->callAPISuccess('Membership', 'Delete', array(
6a488035 871 'id' => $result['id'],
771f3245 872 ));
6a488035 873 }
cc73900e 874 /**
875 * Test civicrm_contact_memberships_create with membership_contact_id
876 * membership).
877 * Success expected.
878 */
879 function testMembershipCreateValidMembershipTypeString() {
880 $params = array(
881 'membership_contact_id' => $this->_contactID,
882 'membership_type_id' => 'General',
883 'join_date' => '2011-01-21',
884 'start_date' => '2010-01-21',
885 'end_date' => '2008-12-21',
886 'source' => 'Payment',
887 'is_override' => 1,
888 'status_id' => $this->_membershipStatusID,
889 );
890
891 $result = $this->callAPISuccess('membership', 'create', $params);
892 $this->assertEquals($this->_membershipTypeID, $result['values'][$result['id']]['membership_type_id']);
893 $result = $this->callAPISuccess('Membership', 'Delete', array(
894 'id' => $result['id'],
895 ));
896 }
897
898 /**
899 * Test civicrm_contact_memberships_create with membership_contact_id
900 * membership).
901 * Success expected.
902 */
903 function testMembershipCreateInValidMembershipTypeString() {
904 $params = array(
905 'membership_contact_id' => $this->_contactID,
906 'membership_type_id' => 'invalid',
907 'join_date' => '2011-01-21',
908 'start_date' => '2010-01-21',
909 'end_date' => '2008-12-21',
910 'source' => 'Payment',
911 'is_override' => 1,
912 'status_id' => $this->_membershipStatusID,
913 );
914
915 $result = $this->callAPIFailure('membership', 'create', $params);
916 }
6a488035 917
cc73900e 918 /**
8c33a68c 919 * Test that if membership join date is not set it defaults to today
cc73900e 920 */
8c33a68c 921 function testEmptyJoinDate() {
922 unset($this->_params['join_date'], $this->_params['is_override']);
923 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
924 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
925 $this->assertEquals(date('Y-m-d', strtotime('now')), $result['join_date']);
926 $this->assertEquals('2009-01-21', $result['start_date']);
927 $this->assertEquals('2009-12-21', $result['end_date']);
cc73900e 928 }
cc73900e 929 /**
8c33a68c 930 * Test that if membership start date is not set it defaults to correct end date
931 * - fixed
cc73900e 932 */
8c33a68c 933 function testEmptyStartDateFixed() {
934 unset($this->_params['start_date'], $this->_params['is_override']);
935 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
936 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
937 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
938 $this->assertEquals('2009-01-21', $result['join_date']);
939 $this->assertEquals('2008-03-01', $result['start_date']);
940 $this->assertEquals('2009-12-21', $result['end_date']);
941 }
cc73900e 942
8c33a68c 943 /**
944 * Test that if membership start date is not set it defaults to correct end date
945 * - fixed
946 */
947 function testEmptyStartDateRolling() {
948 unset($this->_params['start_date'], $this->_params['is_override']);
949 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
950 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
951 $this->assertEquals('2009-01-21', $result['join_date']);
952 $this->assertEquals('2009-01-21', $result['start_date']);
953 $this->assertEquals('2009-12-21', $result['end_date']);
cc73900e 954 }
8c33a68c 955 /**
956 * Test that if membership end date is not set it defaults to correct end date
957 * - rolling
958 */
959 function testEmptyEndDateFixed() {
960 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
961 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
962 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
963 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
964 $this->assertEquals('2009-01-21', $result['join_date']);
965 $this->assertEquals('2008-03-01', $result['start_date']);
966 $this->assertEquals('2010-02-28', $result['end_date']);
967 }
968 /**
969 * Test that if membership end date is not set it defaults to correct end date
970 * - rolling
971 */
972 function testEmptyEndDateRolling() {
973 unset($this->_params['is_override'], $this->_params['end_date']);
974 $this->_params['membership_type_id'] = $this->_membershipTypeID;
975 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
976 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
977 $this->assertEquals('2009-01-21', $result['join_date']);
978 $this->assertEquals('2009-01-21', $result['start_date']);
979 $this->assertEquals('2010-01-20', $result['end_date']);
980 }
981
982
983 /**
984 * Test that if datesdate are not set they not over-ridden if id is passed in
985 */
986 function testMembershipDatesNotOverridden() {
987 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
988 unset($this->_params['end_date'], $this->_params['start_date']);
989 $this->_params['id'] = $result['id'];
990 $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('2009-12-21', $result['end_date']);
995
996 }
6a488035 997}