Merge pull request #4764 from rohankatkar/CRM-15615
[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 260 /**
100fef9d 261 * Check with complete array + custom field
6a488035
TO
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);
4cc99d00 509 // If it fails here CRM-14758 is not fixed
510 $this->assertEquals(1, $result['count']);
511 $result = $result['values'][$result['id']];
512 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
513
514 // Create third employee
932a6904 515 $memberContactId[2] = $this->individualCreate(array('employer_id' => $employerId[0]), 2);
4cc99d00 516
517 // Check that employee does NOT inherit the membership (max_related = 2)
518 $params = array(
519 'contact_id' => $memberContactId[2],
520 'membership_type_id' => $membershipTypeId,
521 );
522 $result = $this->callAPISuccess('membership', 'get', $params);
523 $this->assertEquals(0, $result['count']);
524
525 // Increase max_related for the employer's membership
526 $params = array(
527 'id' => $OrganizationMembershipID,
528 'max_related' => 3,
529 );
530 $this->contactMembershipCreate($params);
531
532 // Check that the employee inherited the membership
533 $params = array(
534 'contact_id' => $memberContactId[2],
535 'membership_type_id' => $membershipTypeId,
536 );
537 $result = $this->callAPISuccess('membership', 'get', $params);
538 $this->assertEquals(1, $result['count']);
539 $result = $result['values'][$result['id']];
540 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
541
542 // First employee moves to a new job
543 $employerId[1] = $this->organizationCreate(array(), 2);
544 $params = array(
545 'id' => $memberContactId[0],
546 'employer_id' => $employerId[1],
547 );
548 $this->callAPISuccess('contact', 'create', $params);
549
550 // Check that employee does NO LONGER inherit the membership
551 $params = array(
552 'contact_id' => $memberContactId[0],
553 'membership_type_id' => $membershipTypeId,
554 );
555 $result = $this->callAPISuccess('membership', 'get', $params);
556 $this->assertEquals(0, $result['count']);
557
558 // Tear down - reverse of creation to be safe
559 $this->contactDelete($memberContactId[2]);
560 $this->contactDelete($memberContactId[1]);
561 $this->contactDelete($memberContactId[0]);
562 $this->contactDelete($employerId[1]);
563 $this->contactDelete($employerId[0]);
564 $this->membershipTypeDelete(array('id' => $membershipTypeId));
565 $this->contactDelete($membershipOrgId);
566 }
567
37eda84b 568 /**
569 * We are checking for no enotices + only id & end_date returned
570 */
571 function testMembershipGetWithReturn() {
572 $membershipID = $this->contactMembershipCreate($this->_params);
573 $result = $this->callAPISuccess('membership', 'get', array('return' => 'end_date'));
574 foreach ($result['values'] as $membership) {
575 $this->assertEquals(array('id', 'end_date'), array_keys($membership));
576 }
577 }
6a488035
TO
578 ///////////////// civicrm_membership_create methods
579
580 /**
581 * Test civicrm_contact_memberships_create with empty params.
582 * Error expected.
583 */
584 function testCreateWithEmptyParams() {
585 $params = array();
d0e1eff2 586 $result = $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
587 }
588
589 /**
6a488035
TO
590 * If is_overide is passed in status must also be passed in
591 */
592 function testCreateOverrideNoStatus() {
593 $params = $this->_params;
594 unset($params['status_id']);
771f3245 595 $result = $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
596 }
597
598 function testMembershipCreateMissingRequired() {
599 $params = array(
600 'membership_type_id' => '1',
601 'join_date' => '2006-01-21',
602 'start_date' => '2006-01-21',
603 'end_date' => '2006-12-21',
604 'source' => 'Payment',
605 'status_id' => '2',
6a488035
TO
606 );
607
d0e1eff2 608 $result = $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
609 }
610
611 function testMembershipCreate() {
612 $params = array(
613 'contact_id' => $this->_contactID,
614 'membership_type_id' => $this->_membershipTypeID,
615 'join_date' => '2006-01-21',
616 'start_date' => '2006-01-21',
617 'end_date' => '2006-12-21',
618 'source' => 'Payment',
619 'is_override' => 1,
620 'status_id' => $this->_membershipStatusID,
6a488035
TO
621 );
622
771f3245 623 $result = $this->callAPIAndDocument('membership', 'create', $params, __FUNCTION__, __FILE__);
6a488035 624 $this->getAndCheck($params, $result['id'], $this->_entity);
6a488035
TO
625 $this->assertNotNull($result['id']);
626 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['contact_id'], " in line " . __LINE__);
627 $this->assertEquals($result['id'], $result['values'][$result['id']]['id'], " in line " . __LINE__);
628 }
629 /*
630 * Check for useful message if contact doesn't exist
631 */
632 function testMembershipCreateWithInvalidContact() {
633 $params = array(
634 'contact_id' => 999,
635 'membership_type_id' => $this->_membershipTypeID,
636 'join_date' => '2006-01-21',
637 'start_date' => '2006-01-21',
638 'end_date' => '2006-12-21',
639 'source' => 'Payment',
640 'is_override' => 1,
641 'status_id' => $this->_membershipStatusID,
6a488035
TO
642 );
643
771f3245 644 $result = $this->callAPIFailure('membership', 'create', $params,
645 'contact_id is not valid : 999'
646 );
6a488035
TO
647 }
648 function testMembershipCreateWithInvalidStatus() {
649 $params = $this->_params;
650 $params['status_id'] = 999;
771f3245 651 $result = $this->callAPIFailure('membership', 'create', $params,
652 "'999' is not a valid option for field status_id"
653 );
6a488035
TO
654 }
655
656 function testMembershipCreateWithInvalidType() {
657 $params = $this->_params;
658 $params['membership_type_id'] = 999;
659
771f3245 660 $result = $this->callAPIFailure('membership', 'create', $params,
661 "'999' is not a valid option for field membership_type_id"
662 );
6a488035
TO
663 }
664
665 /**
100fef9d 666 * Check with complete array + custom field
6a488035
TO
667 * Note that the test is written on purpose without any
668 * variables specific to participant so it can be replicated into other entities
669 * and / or moved to the automated test suite
670 */
671 function testCreateWithCustom() {
672 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
673
674 $params = $this->_params;
675 $params['custom_' . $ids['custom_field_id']] = "custom string";
676
771f3245 677 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
678 $check = $this->callAPISuccess($this->_entity, 'get', array('id' => $result['id'], 'contact_id' => $this->_contactID));
6a488035 679 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
6a488035
TO
680 }
681
682 /**
683 * Test civicrm_contact_memberships_create with membership id (edit
684 * membership).
685 * success expected.
686 */
687 function testMembershipCreateWithId() {
688 $membershipID = $this->contactMembershipCreate($this->_params);
689 $params = array(
690 'id' => $membershipID,
691 'contact_id' => $this->_contactID,
692 'membership_type_id' => $this->_membershipTypeID,
693 'join_date' => '2006-01-21',
694 'start_date' => '2006-01-21',
695 'end_date' => '2006-12-21',
696 'source' => 'Payment',
697 'is_override' => 1,
698 'status_id' => $this->_membershipStatusID,
6a488035
TO
699 );
700
771f3245 701 $result = $this->callAPISuccess('membership', 'create', $params);
702 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 703 'id' => $result['id'],
771f3245 704 ));
6a488035
TO
705 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
706 }
707
708 /**
709 * Test civicrm_contact_memberships_create with membership id (edit
710 * membership).
711 * success expected.
712 */
713 function testMembershipCreateUpdateWithIdNoContact() {
714 $membershipID = $this->contactMembershipCreate($this->_params);
715 $params = array(
716 'id' => $membershipID,
717 'membership_type_id' => $this->_membershipTypeID,
718 'contact_id' => $this->_contactID,
719 'join_date' => '2006-01-21',
720 'start_date' => '2006-01-21',
721 'end_date' => '2006-12-21',
722 'source' => 'Payment',
723 'is_override' => 1,
724 'status_id' => $this->_membershipStatusID,
6a488035
TO
725 );
726
771f3245 727 $result = $this->callAPISuccess('membership', 'create', $params);
728 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 729 'id' => $result['id'],
6a488035 730 ));
771f3245 731
6a488035
TO
732 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
733 }
734
735 /**
736 * Test civicrm_contact_memberships_create with membership id (edit
737 * membership).
738 * success expected.
739 */
740 function testMembershipCreateUpdateWithIdNoDates() {
741 $membershipID = $this->contactMembershipCreate($this->_params);
742 $params = array(
743 'id' => $membershipID,
744 'contact_id' => $this->_contactID,
745 'membership_type_id' => $this->_membershipTypeID,
746 'source' => 'Payment',
747 'is_override' => 1,
748 'status_id' => $this->_membershipStatusID,
6a488035
TO
749 );
750
771f3245 751 $result = $this->callAPISuccess('membership', 'create', $params);
752 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 753 'id' => $result['id'],
771f3245 754 ));
6a488035
TO
755 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
756 }
757
758 /**
759 * Test civicrm_contact_memberships_create with membership id (edit
760 * membership).
761 * success expected.
762 */
763 function testMembershipCreateUpdateWithIdNoDatesNoType() {
764 $membershipID = $this->contactMembershipCreate($this->_params);
765 $params = array(
766 'id' => $membershipID,
767 'source' => 'not much here',
768 'contact_id' => $this->_contactID,
769 'is_override' => 1,
770 'status_id' => $this->_membershipStatusID,
6a488035
TO
771 );
772
771f3245 773 $result = $this->callAPISuccess('membership', 'create', $params);
774 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 775 'id' => $result['id'],
771f3245 776 ));
6a488035
TO
777 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
778 }
779
780 /**
781 * Test civicrm_contact_memberships_create with membership id (edit
782 * membership).
783 * success expected.
784 */
785 function testMembershipCreateUpdateWithIDAndSource() {
786 $membershipID = $this->contactMembershipCreate($this->_params);
787 $params = array(
788 'id' => $membershipID,
789 'source' => 'changed',
790 'contact_id' => $this->_contactID,
771f3245 791 'status_id' => $this->_membershipStatusID, 'membership_type_id' => $this->_membershipTypeID,
6a488035
TO
792 'skipStatusCal' => 1,
793 );
771f3245 794 $result = $this->callAPISuccess('membership', 'create', $params);
6a488035 795 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
771f3245 796 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 797 'id' => $result['id'],
6a488035
TO
798 ));
799 }
800
801 /**
100fef9d 802 * Change custom field using update
6a488035
TO
803 */
804 function testUpdateWithCustom() {
805 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
806
807 $params = $this->_params;
808 $params['custom_' . $ids['custom_field_id']] = "custom string";
771f3245 809 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
810 $result = $this->callAPISuccess($this->_entity, 'create', array('id' => $result['id'], 'custom_' . $ids['custom_field_id'] => "new custom"));
811 $check = $this->callAPISuccess($this->_entity, 'get', array('id' => $result['id'], 'contact_id' => $this->_contactID));
6a488035
TO
812
813 $this->assertEquals("new custom", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
771f3245 814 $delete = $this->callAPISuccess('Membership', 'Delete', array(
6a488035 815 'id' => $check['id'],
6a488035
TO
816 ));
817
818 $this->customFieldDelete($ids['custom_field_id']);
819 $this->customGroupDelete($ids['custom_group_id']);
820 }
821
822 /**
823 * Test civicrm_contact_memberships_create Invalid membership data
824 * Error expected.
825 */
826 function testMembershipCreateInvalidMemData() {
827 //membership_contact_id as string
828 $params = array(
829 'membership_contact_id' => 'Invalid',
830 'membership_type_id' => $this->_membershipTypeID,
831 'join_date' => '2011-01-21',
832 'start_date' => '2010-01-21',
833 'end_date' => '2008-12-21',
834 'source' => 'Payment',
835 'is_override' => 1,
771f3245 836 'status_id' => $this->_membershipStatusID, );
6a488035 837
d0e1eff2 838 $result = $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
839
840 //membership_contact_id which is no in contact table
841 $params['membership_contact_id'] = 999;
d0e1eff2 842 $result = $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
843
844 //invalid join date
845 unset($params['membership_contact_id']);
846 $params['join_date'] = "invalid";
d0e1eff2 847 $result = $this->callAPIFailure('Membership', 'Create', $params);
6a488035
TO
848 }
849
850 /**
851 * Test civicrm_contact_memberships_create with membership_contact_id
852 * membership).
853 * Success expected.
854 */
855 function testMembershipCreateWithMemContact() {
856 $params = array(
857 'membership_contact_id' => $this->_contactID,
858 'membership_type_id' => $this->_membershipTypeID,
859 'join_date' => '2011-01-21',
860 'start_date' => '2010-01-21',
861 'end_date' => '2008-12-21',
862 'source' => 'Payment',
863 'is_override' => 1,
864 'status_id' => $this->_membershipStatusID,
6a488035
TO
865 );
866
771f3245 867 $result = $this->callAPISuccess('membership', 'create', $params);
6a488035 868
771f3245 869 $result = $this->callAPISuccess('Membership', 'Delete', array(
6a488035 870 'id' => $result['id'],
771f3245 871 ));
6a488035 872 }
cc73900e 873 /**
874 * Test civicrm_contact_memberships_create with membership_contact_id
875 * membership).
876 * Success expected.
877 */
878 function testMembershipCreateValidMembershipTypeString() {
879 $params = array(
880 'membership_contact_id' => $this->_contactID,
881 'membership_type_id' => 'General',
882 'join_date' => '2011-01-21',
883 'start_date' => '2010-01-21',
884 'end_date' => '2008-12-21',
885 'source' => 'Payment',
886 'is_override' => 1,
887 'status_id' => $this->_membershipStatusID,
888 );
889
890 $result = $this->callAPISuccess('membership', 'create', $params);
891 $this->assertEquals($this->_membershipTypeID, $result['values'][$result['id']]['membership_type_id']);
892 $result = $this->callAPISuccess('Membership', 'Delete', array(
893 'id' => $result['id'],
894 ));
895 }
896
897 /**
898 * Test civicrm_contact_memberships_create with membership_contact_id
899 * membership).
900 * Success expected.
901 */
902 function testMembershipCreateInValidMembershipTypeString() {
903 $params = array(
904 'membership_contact_id' => $this->_contactID,
905 'membership_type_id' => 'invalid',
906 'join_date' => '2011-01-21',
907 'start_date' => '2010-01-21',
908 'end_date' => '2008-12-21',
909 'source' => 'Payment',
910 'is_override' => 1,
911 'status_id' => $this->_membershipStatusID,
912 );
913
914 $result = $this->callAPIFailure('membership', 'create', $params);
915 }
6a488035 916
cc73900e 917 /**
8c33a68c 918 * Test that if membership join date is not set it defaults to today
cc73900e 919 */
8c33a68c 920 function testEmptyJoinDate() {
921 unset($this->_params['join_date'], $this->_params['is_override']);
922 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
923 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
924 $this->assertEquals(date('Y-m-d', strtotime('now')), $result['join_date']);
925 $this->assertEquals('2009-01-21', $result['start_date']);
926 $this->assertEquals('2009-12-21', $result['end_date']);
cc73900e 927 }
cc73900e 928 /**
8c33a68c 929 * Test that if membership start date is not set it defaults to correct end date
930 * - fixed
cc73900e 931 */
8c33a68c 932 function testEmptyStartDateFixed() {
933 unset($this->_params['start_date'], $this->_params['is_override']);
934 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
935 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
936 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
937 $this->assertEquals('2009-01-21', $result['join_date']);
938 $this->assertEquals('2008-03-01', $result['start_date']);
939 $this->assertEquals('2009-12-21', $result['end_date']);
940 }
cc73900e 941
8c33a68c 942 /**
943 * Test that if membership start date is not set it defaults to correct end date
944 * - fixed
945 */
946 function testEmptyStartDateRolling() {
947 unset($this->_params['start_date'], $this->_params['is_override']);
948 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
949 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
950 $this->assertEquals('2009-01-21', $result['join_date']);
951 $this->assertEquals('2009-01-21', $result['start_date']);
952 $this->assertEquals('2009-12-21', $result['end_date']);
cc73900e 953 }
8c33a68c 954 /**
955 * Test that if membership end date is not set it defaults to correct end date
956 * - rolling
957 */
958 function testEmptyEndDateFixed() {
959 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
960 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
961 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
962 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
963 $this->assertEquals('2009-01-21', $result['join_date']);
964 $this->assertEquals('2008-03-01', $result['start_date']);
965 $this->assertEquals('2010-02-28', $result['end_date']);
966 }
967 /**
968 * Test that if membership end date is not set it defaults to correct end date
969 * - rolling
970 */
971 function testEmptyEndDateRolling() {
972 unset($this->_params['is_override'], $this->_params['end_date']);
973 $this->_params['membership_type_id'] = $this->_membershipTypeID;
974 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
975 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
976 $this->assertEquals('2009-01-21', $result['join_date']);
977 $this->assertEquals('2009-01-21', $result['start_date']);
978 $this->assertEquals('2010-01-20', $result['end_date']);
979 }
980
981
982 /**
983 * Test that if datesdate are not set they not over-ridden if id is passed in
984 */
985 function testMembershipDatesNotOverridden() {
986 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
987 unset($this->_params['end_date'], $this->_params['start_date']);
988 $this->_params['id'] = $result['id'];
989 $this->callAPISuccess($this->_entity, 'create', $this->_params);
990 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
991 $this->assertEquals('2009-01-21', $result['join_date']);
992 $this->assertEquals('2009-01-21', $result['start_date']);
993 $this->assertEquals('2009-12-21', $result['end_date']);
994
995 }
6a488035 996}