Merge pull request #2844 from totten/4.4-handlePaymentNotification
[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 */
211
6a488035 212 function testGetWithParamsMemberShipTypeId() {
771f3245 213 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
6a488035
TO
214 $params = array(
215 'membership_type_id' => $this->_membershipTypeID,
6a488035 216 );
771f3245 217 $membership = $this->callAPISuccess('membership', 'get', $params);
218 $result = $this->callAPISuccess('Membership', 'Delete', array(
6a488035 219 'id' => $membership['id'],
771f3245 220 ));
6a488035
TO
221 $result = $membership['values'][$membership['id']];
222 $this->assertEquals($result['contact_id'], $this->_contactID, "In line " . __LINE__);
223 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID, "In line " . __LINE__);
224 $this->assertEquals($result['status_id'], $this->_membershipStatusID, "In line " . __LINE__);
225 $this->assertEquals($result['join_date'], '2009-01-21', "In line " . __LINE__);
226 $this->assertEquals($result['start_date'], '2009-01-21', "In line " . __LINE__);
227 $this->assertEquals($result['end_date'], '2009-12-21', "In line " . __LINE__);
228 $this->assertEquals($result['source'], 'Payment', "In line " . __LINE__);
229 $this->assertEquals($result['is_override'], 1, "In line " . __LINE__);
230 $this->assertEquals($result['id'], $membership['id']);
231 }
232
6a488035
TO
233 /**
234 * check with complete array + custom field
235 * Note that the test is written on purpose without any
236 * variables specific to participant so it can be replicated into other entities
237 * and / or moved to the automated test suite
238 */
239 function testGetWithParamsMemberShipIdAndCustom() {
240 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
241
242 $params = $this->_params;
243 $params['custom_' . $ids['custom_field_id']] = "custom string";
244
771f3245 245 $result = $this->callAPISuccess($this->_entity, 'create', $params);
6a488035 246
771f3245 247 $getParams = array('membership_type_id' => $params['membership_type_id']);
248 $check = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
6a488035
TO
249 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
250
771f3245 251 $result = $this->callAPISuccess('Membership', 'Delete', array(
6a488035 252 'id' => $result['id'],
771f3245 253 ));
6a488035
TO
254 }
255
256 /**
257 * Test civicrm_membership_get with proper params.
258 * Memberships expected.
259 */
260 function testGet() {
261 $membershipID = $this->contactMembershipCreate($this->_params);
262 $params = array(
263 'contact_id' => $this->_contactID,
6a488035
TO
264 );
265
771f3245 266 $membership = $this->callAPISuccess('membership', 'get', $params);
6a488035 267 $result = $membership['values'][$membershipID];
771f3245 268 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 269 'id' => $membership['id'],
771f3245 270 ));
6a488035
TO
271 $this->assertEquals($result['join_date'], '2009-01-21', "In line " . __LINE__);
272 $this->assertEquals($result['contact_id'], $this->_contactID, "In line " . __LINE__);
273 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID, "In line " . __LINE__);
274 $this->assertEquals($result['status_id'], $this->_membershipStatusID, "In line " . __LINE__);
275
276 $this->assertEquals($result['start_date'], '2009-01-21', "In line " . __LINE__);
277 $this->assertEquals($result['end_date'], '2009-12-21', "In line " . __LINE__);
278 $this->assertEquals($result['source'], 'Payment', "In line " . __LINE__);
279 $this->assertEquals($result['is_override'], 1, "In line " . __LINE__);
280 }
281
282
283 /**
284 * Test civicrm_membership_get with proper params.
285 * Memberships expected.
286 */
287 function testGetWithId() {
288 $membershipID = $this->contactMembershipCreate($this->_params);
289 $params = array(
290 'contact_id' => $this->_contactID,
6a488035
TO
291 'id' => $this->__membershipID,
292 'return' => 'id',
293 );
771f3245 294 $result = $this->callAPISuccess('membership', 'get', $params);
6a488035
TO
295 $this->assertEquals($membershipID, $result['id']);
296 $params = array(
297 'contact_id' => $this->_contactID,
6a488035
TO
298 'membership_id' => $this->__membershipID,
299 'return' => 'membership_id',
300 );
771f3245 301 $result = $this->callAPISuccess('membership', 'get', $params);
6a488035 302 $this->assertEquals($membershipID, $result['id']);
6a488035
TO
303 }
304
305 /**
306 * Test civicrm_membership_get for only active.
307 * Memberships expected.
308 */
309 function testGetOnlyActive() {
310 $description = "Demonstrates use of 'filter' active_only' param";
311 $this->_membershipID = $this->contactMembershipCreate($this->_params);
312 $subfile = 'filterIsCurrent';
313 $params = array(
314 'contact_id' => $this->_contactID,
315 'active_only' => 1,
6a488035
TO
316 );
317
771f3245 318 $membership = $this->callAPISuccess('membership', 'get', $params);
6a488035
TO
319 $result = $membership['values'][$this->_membershipID];
320 $this->assertEquals($membership['values'][$this->_membershipID]['status_id'], $this->_membershipStatusID, "In line " . __LINE__);
321 $this->assertEquals($membership['values'][$this->_membershipID]['contact_id'], $this->_contactID, "In line " . __LINE__);
322 $params = array(
323 'contact_id' => $this->_contactID,
324 'filters' => array(
325 'is_current' => 1,
326 ),
6a488035
TO
327 );
328
771f3245 329 $membership = $this->callAPIAndDocument('membership', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
330 $result = $membership['values'][$this->_membershipID];
331 $this->assertEquals($membership['values'][$this->_membershipID]['status_id'], $this->_membershipStatusID, "In line " . __LINE__);
332 $this->assertEquals($membership['values'][$this->_membershipID]['contact_id'], $this->_contactID, "In line " . __LINE__);
333
334
771f3245 335 $result = $this->callAPISuccess('Membership', 'Delete', array(
6a488035 336 'id' => $this->_membershipID,
771f3245 337 ));
6a488035
TO
338 }
339
340 /**
341 * Test civicrm_membership_get for non exist contact.
342 * empty Memberships.
343 */
344 function testGetNoContactExists() {
345 $params = array(
346 'contact_id' => 55555,
6a488035
TO
347 );
348
771f3245 349 $membership = $this->callAPISuccess('membership', 'get', $params);
6a488035
TO
350 $this->assertEquals($membership['count'], 0, "In line " . __LINE__);
351 }
352
353 /**
354 * Test civicrm_membership_get with relationship.
355 * get Memberships.
356 */
357 function testGetWithRelationship() {
358 $membershipOrgId = $this->organizationCreate(NULL);
e4d5f1e2 359 $memberContactId = $this->individualCreate();
6a488035
TO
360
361 $relTypeParams = array(
362 'name_a_b' => 'Relation 1',
363 'name_b_a' => 'Relation 2',
364 'description' => 'Testing relationship type',
365 'contact_type_a' => 'Organization',
366 'contact_type_b' => 'Individual',
367 'is_reserved' => 1,
368 'is_active' => 1,
6a488035
TO
369 );
370 $relTypeID = $this->relationshipTypeCreate($relTypeParams);
371
372 $params = array(
373 'name' => 'test General',
374 'duration_unit' => 'year',
375 'duration_interval' => 1,
376 'period_type' => 'rolling',
377 'member_of_contact_id' => $membershipOrgId,
378 'domain_id' => 1,
771f3245 379 'financial_type_id' => 1,
6a488035
TO
380 'relationship_type_id' => $relTypeID,
381 'relationship_direction' => 'b_a',
382 'is_active' => 1,
6a488035 383 );
771f3245 384 $memType = $this->callAPISuccess('membership_type', 'create', $params);
6a488035
TO
385
386 $params = array(
387 'contact_id' => $memberContactId,
388 'membership_type_id' => $memType['id'],
389 'join_date' => '2009-01-21',
390 'start_date' => '2009-01-21',
391 'end_date' => '2009-12-21',
392 'source' => 'Payment',
393 'is_override' => 1,
394 'status_id' => $this->_membershipStatusID,
6a488035
TO
395 );
396 $membershipID = $this->contactMembershipCreate($params);
397
398 $params = array(
399 'contact_id' => $memberContactId,
400 'membership_type_id' => $memType['id'],
6a488035
TO
401 );
402
771f3245 403 $result = $this->callAPISuccess('membership', 'get', $params);
6a488035
TO
404
405 $membership = $result['values'][$membershipID];
771f3245 406 $this->assertEquals($this->_membershipStatusID, $membership['status_id']);
407 $result = $this->callAPISuccess('Membership', 'Delete', array(
6a488035 408 'id' => $membership['id'],
6a488035
TO
409 ));
410 $this->membershipTypeDelete(array('id' => $memType['id']));
411 $this->relationshipTypeDelete($relTypeID);
412 $this->contactDelete($membershipOrgId);
413 $this->contactDelete($memberContactId);
414 }
415
37eda84b 416 /**
417 * We are checking for no enotices + only id & end_date returned
418 */
419 function testMembershipGetWithReturn() {
420 $membershipID = $this->contactMembershipCreate($this->_params);
421 $result = $this->callAPISuccess('membership', 'get', array('return' => 'end_date'));
422 foreach ($result['values'] as $membership) {
423 $this->assertEquals(array('id', 'end_date'), array_keys($membership));
424 }
425 }
6a488035
TO
426 ///////////////// civicrm_membership_create methods
427
428 /**
429 * Test civicrm_contact_memberships_create with empty params.
430 * Error expected.
431 */
432 function testCreateWithEmptyParams() {
433 $params = array();
d0e1eff2 434 $result = $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
435 }
436
437 /**
6a488035
TO
438 * If is_overide is passed in status must also be passed in
439 */
440 function testCreateOverrideNoStatus() {
441 $params = $this->_params;
442 unset($params['status_id']);
771f3245 443 $result = $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
444 }
445
446 function testMembershipCreateMissingRequired() {
447 $params = array(
448 'membership_type_id' => '1',
449 'join_date' => '2006-01-21',
450 'start_date' => '2006-01-21',
451 'end_date' => '2006-12-21',
452 'source' => 'Payment',
453 'status_id' => '2',
6a488035
TO
454 );
455
d0e1eff2 456 $result = $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
457 }
458
459 function testMembershipCreate() {
460 $params = array(
461 'contact_id' => $this->_contactID,
462 'membership_type_id' => $this->_membershipTypeID,
463 'join_date' => '2006-01-21',
464 'start_date' => '2006-01-21',
465 'end_date' => '2006-12-21',
466 'source' => 'Payment',
467 'is_override' => 1,
468 'status_id' => $this->_membershipStatusID,
6a488035
TO
469 );
470
771f3245 471 $result = $this->callAPIAndDocument('membership', 'create', $params, __FUNCTION__, __FILE__);
6a488035 472 $this->getAndCheck($params, $result['id'], $this->_entity);
6a488035
TO
473 $this->assertNotNull($result['id']);
474 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['contact_id'], " in line " . __LINE__);
475 $this->assertEquals($result['id'], $result['values'][$result['id']]['id'], " in line " . __LINE__);
476 }
477 /*
478 * Check for useful message if contact doesn't exist
479 */
480 function testMembershipCreateWithInvalidContact() {
481 $params = array(
482 'contact_id' => 999,
483 'membership_type_id' => $this->_membershipTypeID,
484 'join_date' => '2006-01-21',
485 'start_date' => '2006-01-21',
486 'end_date' => '2006-12-21',
487 'source' => 'Payment',
488 'is_override' => 1,
489 'status_id' => $this->_membershipStatusID,
6a488035
TO
490 );
491
771f3245 492 $result = $this->callAPIFailure('membership', 'create', $params,
493 'contact_id is not valid : 999'
494 );
6a488035
TO
495 }
496 function testMembershipCreateWithInvalidStatus() {
497 $params = $this->_params;
498 $params['status_id'] = 999;
771f3245 499 $result = $this->callAPIFailure('membership', 'create', $params,
500 "'999' is not a valid option for field status_id"
501 );
6a488035
TO
502 }
503
504 function testMembershipCreateWithInvalidType() {
505 $params = $this->_params;
506 $params['membership_type_id'] = 999;
507
771f3245 508 $result = $this->callAPIFailure('membership', 'create', $params,
509 "'999' is not a valid option for field membership_type_id"
510 );
6a488035
TO
511 }
512
513 /**
514 * check with complete array + custom field
515 * Note that the test is written on purpose without any
516 * variables specific to participant so it can be replicated into other entities
517 * and / or moved to the automated test suite
518 */
519 function testCreateWithCustom() {
520 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
521
522 $params = $this->_params;
523 $params['custom_' . $ids['custom_field_id']] = "custom string";
524
771f3245 525 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
526 $check = $this->callAPISuccess($this->_entity, 'get', array('id' => $result['id'], 'contact_id' => $this->_contactID));
6a488035 527 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
6a488035
TO
528 }
529
530 /**
531 * Test civicrm_contact_memberships_create with membership id (edit
532 * membership).
533 * success expected.
534 */
535 function testMembershipCreateWithId() {
536 $membershipID = $this->contactMembershipCreate($this->_params);
537 $params = array(
538 'id' => $membershipID,
539 'contact_id' => $this->_contactID,
540 'membership_type_id' => $this->_membershipTypeID,
541 'join_date' => '2006-01-21',
542 'start_date' => '2006-01-21',
543 'end_date' => '2006-12-21',
544 'source' => 'Payment',
545 'is_override' => 1,
546 'status_id' => $this->_membershipStatusID,
6a488035
TO
547 );
548
771f3245 549 $result = $this->callAPISuccess('membership', 'create', $params);
550 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 551 'id' => $result['id'],
771f3245 552 ));
6a488035
TO
553 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
554 }
555
556 /**
557 * Test civicrm_contact_memberships_create with membership id (edit
558 * membership).
559 * success expected.
560 */
561 function testMembershipCreateUpdateWithIdNoContact() {
562 $membershipID = $this->contactMembershipCreate($this->_params);
563 $params = array(
564 'id' => $membershipID,
565 'membership_type_id' => $this->_membershipTypeID,
566 'contact_id' => $this->_contactID,
567 'join_date' => '2006-01-21',
568 'start_date' => '2006-01-21',
569 'end_date' => '2006-12-21',
570 'source' => 'Payment',
571 'is_override' => 1,
572 'status_id' => $this->_membershipStatusID,
6a488035
TO
573 );
574
771f3245 575 $result = $this->callAPISuccess('membership', 'create', $params);
576 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 577 'id' => $result['id'],
6a488035 578 ));
771f3245 579
6a488035
TO
580 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
581 }
582
583 /**
584 * Test civicrm_contact_memberships_create with membership id (edit
585 * membership).
586 * success expected.
587 */
588 function testMembershipCreateUpdateWithIdNoDates() {
589 $membershipID = $this->contactMembershipCreate($this->_params);
590 $params = array(
591 'id' => $membershipID,
592 'contact_id' => $this->_contactID,
593 'membership_type_id' => $this->_membershipTypeID,
594 'source' => 'Payment',
595 'is_override' => 1,
596 'status_id' => $this->_membershipStatusID,
6a488035
TO
597 );
598
771f3245 599 $result = $this->callAPISuccess('membership', 'create', $params);
600 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 601 'id' => $result['id'],
771f3245 602 ));
6a488035
TO
603 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
604 }
605
606 /**
607 * Test civicrm_contact_memberships_create with membership id (edit
608 * membership).
609 * success expected.
610 */
611 function testMembershipCreateUpdateWithIdNoDatesNoType() {
612 $membershipID = $this->contactMembershipCreate($this->_params);
613 $params = array(
614 'id' => $membershipID,
615 'source' => 'not much here',
616 'contact_id' => $this->_contactID,
617 'is_override' => 1,
618 'status_id' => $this->_membershipStatusID,
6a488035
TO
619 );
620
771f3245 621 $result = $this->callAPISuccess('membership', 'create', $params);
622 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 623 'id' => $result['id'],
771f3245 624 ));
6a488035
TO
625 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
626 }
627
628 /**
629 * Test civicrm_contact_memberships_create with membership id (edit
630 * membership).
631 * success expected.
632 */
633 function testMembershipCreateUpdateWithIDAndSource() {
634 $membershipID = $this->contactMembershipCreate($this->_params);
635 $params = array(
636 'id' => $membershipID,
637 'source' => 'changed',
638 'contact_id' => $this->_contactID,
771f3245 639 'status_id' => $this->_membershipStatusID, 'membership_type_id' => $this->_membershipTypeID,
6a488035
TO
640 'skipStatusCal' => 1,
641 );
771f3245 642 $result = $this->callAPISuccess('membership', 'create', $params);
6a488035 643 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
771f3245 644 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 645 'id' => $result['id'],
6a488035
TO
646 ));
647 }
648
649 /**
650 * change custom field using update
651 */
652 function testUpdateWithCustom() {
653 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
654
655 $params = $this->_params;
656 $params['custom_' . $ids['custom_field_id']] = "custom string";
771f3245 657 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
658 $result = $this->callAPISuccess($this->_entity, 'create', array('id' => $result['id'], 'custom_' . $ids['custom_field_id'] => "new custom"));
659 $check = $this->callAPISuccess($this->_entity, 'get', array('id' => $result['id'], 'contact_id' => $this->_contactID));
6a488035
TO
660
661 $this->assertEquals("new custom", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
771f3245 662 $delete = $this->callAPISuccess('Membership', 'Delete', array(
6a488035 663 'id' => $check['id'],
6a488035
TO
664 ));
665
666 $this->customFieldDelete($ids['custom_field_id']);
667 $this->customGroupDelete($ids['custom_group_id']);
668 }
669
670 /**
671 * Test civicrm_contact_memberships_create Invalid membership data
672 * Error expected.
673 */
674 function testMembershipCreateInvalidMemData() {
675 //membership_contact_id as string
676 $params = array(
677 'membership_contact_id' => 'Invalid',
678 'membership_type_id' => $this->_membershipTypeID,
679 'join_date' => '2011-01-21',
680 'start_date' => '2010-01-21',
681 'end_date' => '2008-12-21',
682 'source' => 'Payment',
683 'is_override' => 1,
771f3245 684 'status_id' => $this->_membershipStatusID, );
6a488035 685
d0e1eff2 686 $result = $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
687
688 //membership_contact_id which is no in contact table
689 $params['membership_contact_id'] = 999;
d0e1eff2 690 $result = $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
691
692 //invalid join date
693 unset($params['membership_contact_id']);
694 $params['join_date'] = "invalid";
d0e1eff2 695 $result = $this->callAPIFailure('Membership', 'Create', $params);
6a488035
TO
696 }
697
698 /**
699 * Test civicrm_contact_memberships_create with membership_contact_id
700 * membership).
701 * Success expected.
702 */
703 function testMembershipCreateWithMemContact() {
704 $params = array(
705 'membership_contact_id' => $this->_contactID,
706 'membership_type_id' => $this->_membershipTypeID,
707 'join_date' => '2011-01-21',
708 'start_date' => '2010-01-21',
709 'end_date' => '2008-12-21',
710 'source' => 'Payment',
711 'is_override' => 1,
712 'status_id' => $this->_membershipStatusID,
6a488035
TO
713 );
714
771f3245 715 $result = $this->callAPISuccess('membership', 'create', $params);
6a488035 716
771f3245 717 $result = $this->callAPISuccess('Membership', 'Delete', array(
6a488035 718 'id' => $result['id'],
771f3245 719 ));
6a488035 720 }
cc73900e 721 /**
722 * Test civicrm_contact_memberships_create with membership_contact_id
723 * membership).
724 * Success expected.
725 */
726 function testMembershipCreateValidMembershipTypeString() {
727 $params = array(
728 'membership_contact_id' => $this->_contactID,
729 'membership_type_id' => 'General',
730 'join_date' => '2011-01-21',
731 'start_date' => '2010-01-21',
732 'end_date' => '2008-12-21',
733 'source' => 'Payment',
734 'is_override' => 1,
735 'status_id' => $this->_membershipStatusID,
736 );
737
738 $result = $this->callAPISuccess('membership', 'create', $params);
739 $this->assertEquals($this->_membershipTypeID, $result['values'][$result['id']]['membership_type_id']);
740 $result = $this->callAPISuccess('Membership', 'Delete', array(
741 'id' => $result['id'],
742 ));
743 }
744
745 /**
746 * Test civicrm_contact_memberships_create with membership_contact_id
747 * membership).
748 * Success expected.
749 */
750 function testMembershipCreateInValidMembershipTypeString() {
751 $params = array(
752 'membership_contact_id' => $this->_contactID,
753 'membership_type_id' => 'invalid',
754 'join_date' => '2011-01-21',
755 'start_date' => '2010-01-21',
756 'end_date' => '2008-12-21',
757 'source' => 'Payment',
758 'is_override' => 1,
759 'status_id' => $this->_membershipStatusID,
760 );
761
762 $result = $this->callAPIFailure('membership', 'create', $params);
763 }
6a488035 764
cc73900e 765 /**
8c33a68c 766 * Test that if membership join date is not set it defaults to today
cc73900e 767 */
8c33a68c 768 function testEmptyJoinDate() {
769 unset($this->_params['join_date'], $this->_params['is_override']);
770 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
771 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
772 $this->assertEquals(date('Y-m-d', strtotime('now')), $result['join_date']);
773 $this->assertEquals('2009-01-21', $result['start_date']);
774 $this->assertEquals('2009-12-21', $result['end_date']);
cc73900e 775 }
cc73900e 776 /**
8c33a68c 777 * Test that if membership start date is not set it defaults to correct end date
778 * - fixed
cc73900e 779 */
8c33a68c 780 function testEmptyStartDateFixed() {
781 unset($this->_params['start_date'], $this->_params['is_override']);
782 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
783 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
784 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
785 $this->assertEquals('2009-01-21', $result['join_date']);
786 $this->assertEquals('2008-03-01', $result['start_date']);
787 $this->assertEquals('2009-12-21', $result['end_date']);
788 }
cc73900e 789
8c33a68c 790 /**
791 * Test that if membership start date is not set it defaults to correct end date
792 * - fixed
793 */
794 function testEmptyStartDateRolling() {
795 unset($this->_params['start_date'], $this->_params['is_override']);
796 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
797 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
798 $this->assertEquals('2009-01-21', $result['join_date']);
799 $this->assertEquals('2009-01-21', $result['start_date']);
800 $this->assertEquals('2009-12-21', $result['end_date']);
cc73900e 801 }
8c33a68c 802 /**
803 * Test that if membership end date is not set it defaults to correct end date
804 * - rolling
805 */
806 function testEmptyEndDateFixed() {
807 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
808 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
809 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
810 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
811 $this->assertEquals('2009-01-21', $result['join_date']);
812 $this->assertEquals('2008-03-01', $result['start_date']);
813 $this->assertEquals('2010-02-28', $result['end_date']);
814 }
815 /**
816 * Test that if membership end date is not set it defaults to correct end date
817 * - rolling
818 */
819 function testEmptyEndDateRolling() {
820 unset($this->_params['is_override'], $this->_params['end_date']);
821 $this->_params['membership_type_id'] = $this->_membershipTypeID;
822 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
823 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
824 $this->assertEquals('2009-01-21', $result['join_date']);
825 $this->assertEquals('2009-01-21', $result['start_date']);
826 $this->assertEquals('2010-01-20', $result['end_date']);
827 }
828
829
830 /**
831 * Test that if datesdate are not set they not over-ridden if id is passed in
832 */
833 function testMembershipDatesNotOverridden() {
834 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
835 unset($this->_params['end_date'], $this->_params['start_date']);
836 $this->_params['id'] = $result['id'];
837 $this->callAPISuccess($this->_entity, 'create', $this->_params);
838 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
839 $this->assertEquals('2009-01-21', $result['join_date']);
840 $this->assertEquals('2009-01-21', $result['start_date']);
841 $this->assertEquals('2009-12-21', $result['end_date']);
842
843 }
6a488035
TO
844}
845