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