Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-08-10-18-35-58
[civicrm-core.git] / tests / phpunit / api / v3 / MembershipTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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
36 require_once 'CiviTest/CiviUnitTestCase.php';
37
38 class api_v3_MembershipTest extends CiviUnitTestCase {
39 protected $_apiversion;
40 protected $_contactID;
41 protected $_membershipTypeID;
42 protected $_membershipTypeID2;
43 protected $_membershipStatusID;
44 protected $__membershipID;
45 protected $_entity;
46 protected $_params;
47 public $_eNoticeCompliant = TRUE;
48
49 public function setUp() {
50 // Connect to the database
51 parent::setUp();
52 $this->_apiversion = 3;
53 $this->_contactID = $this->individualCreate();
54 $this->_membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID));
55 $this->_membershipTypeID2 = $this->membershipTypeCreate(array('period_type' => 'fixed','fixed_period_start_day' => '301', 'fixed_period_rollover_day' => '1111'));
56 $this->_membershipStatusID = $this->membershipStatusCreate('test status');
57
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,
72 );
73 }
74
75 function tearDown() {
76 $this->quickCleanup(array(
77 'civicrm_membership',
78 'civicrm_membership_payment',
79 'civicrm_membership_log',
80 ),
81 TRUE
82 );
83 $this->membershipStatusDelete($this->_membershipStatusID);
84 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID2));
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);
95 $this->assertDBRowExist('CRM_Member_DAO_Membership', $membershipID);
96 $params = array(
97 'id' => $membershipID
98 );
99 $result = $this->callAPIAndDocument('membership', 'delete', $params, __FUNCTION__, __FILE__);
100 $this->assertDBRowNotExist('CRM_Member_DAO_Membership', $membershipID);
101 }
102
103 function testMembershipDeleteEmpty() {
104 $params = array();
105 $result = $this->callAPIFailure('membership', 'delete', $params);
106 }
107
108 function testMembershipDeleteInvalidID() {
109 $params = array('id' => 'blah');
110 $result = $this->callAPIFailure('membership', 'delete', $params);
111 }
112
113 /**
114 * Test civicrm_membership_delete() with invalid Membership Id
115 */
116 function testMembershipDeleteWithInvalidMembershipId() {
117 $membershipId = 'membership';
118 $result = $this->callAPIFailure('membership', 'delete', $membershipId);
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);
129 $params = array();
130 $result = $this->callAPISuccess('membership', 'get', $params);
131 $result = $this->callAPISuccess('Membership', 'Delete', array(
132 'id' => $this->_membershipID,
133 ));
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,
144 );
145 $membership = $this->callAPISuccess('membership', 'get', $params);
146
147 $result = $membership['values'][$this->_membershipID];
148 $this->callAPISuccess('Membership', 'Delete', array(
149 'id' => $this->_membershipID,
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
161 /**
162 * Test civicrm_membership_get with params not array.
163 * Gets treated as contact_id, memberships expected.
164 */
165 function testGetWithParamsMemberShipTypeId() {
166 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
167 $params = array(
168 'membership_type_id' => $this->_membershipTypeID,
169 );
170 $membership = $this->callAPISuccess('membership', 'get', $params);
171 $result = $this->callAPISuccess('Membership', 'Delete', array(
172 'id' => $membership['id'],
173 ));
174 $result = $membership['values'][$membership['id']];
175 $this->assertEquals($result['contact_id'], $this->_contactID, "In line " . __LINE__);
176 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID, "In line " . __LINE__);
177 $this->assertEquals($result['status_id'], $this->_membershipStatusID, "In line " . __LINE__);
178 $this->assertEquals($result['join_date'], '2009-01-21', "In line " . __LINE__);
179 $this->assertEquals($result['start_date'], '2009-01-21', "In line " . __LINE__);
180 $this->assertEquals($result['end_date'], '2009-12-21', "In line " . __LINE__);
181 $this->assertEquals($result['source'], 'Payment', "In line " . __LINE__);
182 $this->assertEquals($result['is_override'], 1, "In line " . __LINE__);
183 $this->assertEquals($result['id'], $membership['id']);
184 }
185
186 /**
187 * check with complete array + custom field
188 * Note that the test is written on purpose without any
189 * variables specific to participant so it can be replicated into other entities
190 * and / or moved to the automated test suite
191 */
192 function testGetWithParamsMemberShipIdAndCustom() {
193 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
194
195 $params = $this->_params;
196 $params['custom_' . $ids['custom_field_id']] = "custom string";
197
198 $result = $this->callAPISuccess($this->_entity, 'create', $params);
199
200 $getParams = array('membership_type_id' => $params['membership_type_id']);
201 $check = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
202 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
203
204 $result = $this->callAPISuccess('Membership', 'Delete', array(
205 'id' => $result['id'],
206 ));
207 }
208
209 /**
210 * Test civicrm_membership_get with proper params.
211 * Memberships expected.
212 */
213 function testGet() {
214 $membershipID = $this->contactMembershipCreate($this->_params);
215 $params = array(
216 'contact_id' => $this->_contactID,
217 );
218
219 $membership = $this->callAPISuccess('membership', 'get', $params);
220 $result = $membership['values'][$membershipID];
221 $this->callAPISuccess('Membership', 'Delete', array(
222 'id' => $membership['id'],
223 ));
224 $this->assertEquals($result['join_date'], '2009-01-21', "In line " . __LINE__);
225 $this->assertEquals($result['contact_id'], $this->_contactID, "In line " . __LINE__);
226 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID, "In line " . __LINE__);
227 $this->assertEquals($result['status_id'], $this->_membershipStatusID, "In line " . __LINE__);
228
229 $this->assertEquals($result['start_date'], '2009-01-21', "In line " . __LINE__);
230 $this->assertEquals($result['end_date'], '2009-12-21', "In line " . __LINE__);
231 $this->assertEquals($result['source'], 'Payment', "In line " . __LINE__);
232 $this->assertEquals($result['is_override'], 1, "In line " . __LINE__);
233 }
234
235
236 /**
237 * Test civicrm_membership_get with proper params.
238 * Memberships expected.
239 */
240 function testGetWithId() {
241 $membershipID = $this->contactMembershipCreate($this->_params);
242 $params = array(
243 'contact_id' => $this->_contactID,
244 'id' => $this->__membershipID,
245 'return' => 'id',
246 );
247 $result = $this->callAPISuccess('membership', 'get', $params);
248 $this->assertEquals($membershipID, $result['id']);
249 $params = array(
250 'contact_id' => $this->_contactID,
251 'membership_id' => $this->__membershipID,
252 'return' => 'membership_id',
253 );
254 $result = $this->callAPISuccess('membership', 'get', $params);
255 $this->assertEquals($membershipID, $result['id']);
256 }
257
258 /**
259 * Test civicrm_membership_get for only active.
260 * Memberships expected.
261 */
262 function testGetOnlyActive() {
263 $description = "Demonstrates use of 'filter' active_only' param";
264 $this->_membershipID = $this->contactMembershipCreate($this->_params);
265 $subfile = 'filterIsCurrent';
266 $params = array(
267 'contact_id' => $this->_contactID,
268 'active_only' => 1,
269 );
270
271 $membership = $this->callAPISuccess('membership', 'get', $params);
272 $result = $membership['values'][$this->_membershipID];
273 $this->assertEquals($membership['values'][$this->_membershipID]['status_id'], $this->_membershipStatusID, "In line " . __LINE__);
274 $this->assertEquals($membership['values'][$this->_membershipID]['contact_id'], $this->_contactID, "In line " . __LINE__);
275 $params = array(
276 'contact_id' => $this->_contactID,
277 'filters' => array(
278 'is_current' => 1,
279 ),
280 );
281
282 $membership = $this->callAPIAndDocument('membership', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile);
283 $result = $membership['values'][$this->_membershipID];
284 $this->assertEquals($membership['values'][$this->_membershipID]['status_id'], $this->_membershipStatusID, "In line " . __LINE__);
285 $this->assertEquals($membership['values'][$this->_membershipID]['contact_id'], $this->_contactID, "In line " . __LINE__);
286
287
288 $result = $this->callAPISuccess('Membership', 'Delete', array(
289 'id' => $this->_membershipID,
290 ));
291 }
292
293 /**
294 * Test civicrm_membership_get for non exist contact.
295 * empty Memberships.
296 */
297 function testGetNoContactExists() {
298 $params = array(
299 'contact_id' => 55555,
300 );
301
302 $membership = $this->callAPISuccess('membership', 'get', $params);
303 $this->assertEquals($membership['count'], 0, "In line " . __LINE__);
304 }
305
306 /**
307 * Test civicrm_membership_get with relationship.
308 * get Memberships.
309 */
310 function testGetWithRelationship() {
311 $membershipOrgId = $this->organizationCreate(NULL);
312 $memberContactId = $this->individualCreate();
313
314 $relTypeParams = array(
315 'name_a_b' => 'Relation 1',
316 'name_b_a' => 'Relation 2',
317 'description' => 'Testing relationship type',
318 'contact_type_a' => 'Organization',
319 'contact_type_b' => 'Individual',
320 'is_reserved' => 1,
321 'is_active' => 1,
322 );
323 $relTypeID = $this->relationshipTypeCreate($relTypeParams);
324
325 $params = array(
326 'name' => 'test General',
327 'duration_unit' => 'year',
328 'duration_interval' => 1,
329 'period_type' => 'rolling',
330 'member_of_contact_id' => $membershipOrgId,
331 'domain_id' => 1,
332 'financial_type_id' => 1,
333 'relationship_type_id' => $relTypeID,
334 'relationship_direction' => 'b_a',
335 'is_active' => 1,
336 );
337 $memType = $this->callAPISuccess('membership_type', 'create', $params);
338
339 $params = array(
340 'contact_id' => $memberContactId,
341 'membership_type_id' => $memType['id'],
342 'join_date' => '2009-01-21',
343 'start_date' => '2009-01-21',
344 'end_date' => '2009-12-21',
345 'source' => 'Payment',
346 'is_override' => 1,
347 'status_id' => $this->_membershipStatusID,
348 );
349 $membershipID = $this->contactMembershipCreate($params);
350
351 $params = array(
352 'contact_id' => $memberContactId,
353 'membership_type_id' => $memType['id'],
354 );
355
356 $result = $this->callAPISuccess('membership', 'get', $params);
357
358 $membership = $result['values'][$membershipID];
359 $this->assertEquals($this->_membershipStatusID, $membership['status_id']);
360 $result = $this->callAPISuccess('Membership', 'Delete', array(
361 'id' => $membership['id'],
362 ));
363 $this->membershipTypeDelete(array('id' => $memType['id']));
364 $this->relationshipTypeDelete($relTypeID);
365 $this->contactDelete($membershipOrgId);
366 $this->contactDelete($memberContactId);
367 }
368
369 ///////////////// civicrm_membership_create methods
370
371 /**
372 * Test civicrm_contact_memberships_create with empty params.
373 * Error expected.
374 */
375 function testCreateWithEmptyParams() {
376 $params = array();
377 $result = $this->callAPIFailure('membership', 'create', $params);
378 }
379
380 /**
381 * If is_overide is passed in status must also be passed in
382 */
383 function testCreateOverrideNoStatus() {
384 $params = $this->_params;
385 unset($params['status_id']);
386 $result = $this->callAPIFailure('membership', 'create', $params);
387 }
388
389 function testMembershipCreateMissingRequired() {
390 $params = array(
391 'membership_type_id' => '1',
392 'join_date' => '2006-01-21',
393 'start_date' => '2006-01-21',
394 'end_date' => '2006-12-21',
395 'source' => 'Payment',
396 'status_id' => '2',
397 );
398
399 $result = $this->callAPIFailure('membership', 'create', $params);
400 }
401
402 function testMembershipCreate() {
403 $params = array(
404 'contact_id' => $this->_contactID,
405 'membership_type_id' => $this->_membershipTypeID,
406 'join_date' => '2006-01-21',
407 'start_date' => '2006-01-21',
408 'end_date' => '2006-12-21',
409 'source' => 'Payment',
410 'is_override' => 1,
411 'status_id' => $this->_membershipStatusID,
412 );
413
414 $result = $this->callAPIAndDocument('membership', 'create', $params, __FUNCTION__, __FILE__);
415 $this->getAndCheck($params, $result['id'], $this->_entity);
416 $this->assertNotNull($result['id']);
417 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['contact_id'], " in line " . __LINE__);
418 $this->assertEquals($result['id'], $result['values'][$result['id']]['id'], " in line " . __LINE__);
419 }
420 /*
421 * Check for useful message if contact doesn't exist
422 */
423 function testMembershipCreateWithInvalidContact() {
424 $params = array(
425 'contact_id' => 999,
426 'membership_type_id' => $this->_membershipTypeID,
427 'join_date' => '2006-01-21',
428 'start_date' => '2006-01-21',
429 'end_date' => '2006-12-21',
430 'source' => 'Payment',
431 'is_override' => 1,
432 'status_id' => $this->_membershipStatusID,
433 );
434
435 $result = $this->callAPIFailure('membership', 'create', $params,
436 'contact_id is not valid : 999'
437 );
438 }
439 function testMembershipCreateWithInvalidStatus() {
440 $params = $this->_params;
441 $params['status_id'] = 999;
442 $result = $this->callAPIFailure('membership', 'create', $params,
443 "'999' is not a valid option for field status_id"
444 );
445 }
446
447 function testMembershipCreateWithInvalidType() {
448 $params = $this->_params;
449 $params['membership_type_id'] = 999;
450
451 $result = $this->callAPIFailure('membership', 'create', $params,
452 "'999' is not a valid option for field membership_type_id"
453 );
454 }
455
456 /**
457 * check with complete array + custom field
458 * Note that the test is written on purpose without any
459 * variables specific to participant so it can be replicated into other entities
460 * and / or moved to the automated test suite
461 */
462 function testCreateWithCustom() {
463 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
464
465 $params = $this->_params;
466 $params['custom_' . $ids['custom_field_id']] = "custom string";
467
468 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
469 $check = $this->callAPISuccess($this->_entity, 'get', array('id' => $result['id'], 'contact_id' => $this->_contactID));
470 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
471 }
472
473 /**
474 * Test civicrm_contact_memberships_create with membership id (edit
475 * membership).
476 * success expected.
477 */
478 function testMembershipCreateWithId() {
479 $membershipID = $this->contactMembershipCreate($this->_params);
480 $params = array(
481 'id' => $membershipID,
482 'contact_id' => $this->_contactID,
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,
490 );
491
492 $result = $this->callAPISuccess('membership', 'create', $params);
493 $this->callAPISuccess('Membership', 'Delete', array(
494 'id' => $result['id'],
495 ));
496 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
497 }
498
499 /**
500 * Test civicrm_contact_memberships_create with membership id (edit
501 * membership).
502 * success expected.
503 */
504 function testMembershipCreateUpdateWithIdNoContact() {
505 $membershipID = $this->contactMembershipCreate($this->_params);
506 $params = array(
507 'id' => $membershipID,
508 'membership_type_id' => $this->_membershipTypeID,
509 'contact_id' => $this->_contactID,
510 'join_date' => '2006-01-21',
511 'start_date' => '2006-01-21',
512 'end_date' => '2006-12-21',
513 'source' => 'Payment',
514 'is_override' => 1,
515 'status_id' => $this->_membershipStatusID,
516 );
517
518 $result = $this->callAPISuccess('membership', 'create', $params);
519 $this->callAPISuccess('Membership', 'Delete', array(
520 'id' => $result['id'],
521 ));
522
523 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
524 }
525
526 /**
527 * Test civicrm_contact_memberships_create with membership id (edit
528 * membership).
529 * success expected.
530 */
531 function testMembershipCreateUpdateWithIdNoDates() {
532 $membershipID = $this->contactMembershipCreate($this->_params);
533 $params = array(
534 'id' => $membershipID,
535 'contact_id' => $this->_contactID,
536 'membership_type_id' => $this->_membershipTypeID,
537 'source' => 'Payment',
538 'is_override' => 1,
539 'status_id' => $this->_membershipStatusID,
540 );
541
542 $result = $this->callAPISuccess('membership', 'create', $params);
543 $this->callAPISuccess('Membership', 'Delete', array(
544 'id' => $result['id'],
545 ));
546 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
547 }
548
549 /**
550 * Test civicrm_contact_memberships_create with membership id (edit
551 * membership).
552 * success expected.
553 */
554 function testMembershipCreateUpdateWithIdNoDatesNoType() {
555 $membershipID = $this->contactMembershipCreate($this->_params);
556 $params = array(
557 'id' => $membershipID,
558 'source' => 'not much here',
559 'contact_id' => $this->_contactID,
560 'is_override' => 1,
561 'status_id' => $this->_membershipStatusID,
562 );
563
564 $result = $this->callAPISuccess('membership', 'create', $params);
565 $this->callAPISuccess('Membership', 'Delete', array(
566 'id' => $result['id'],
567 ));
568 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
569 }
570
571 /**
572 * Test civicrm_contact_memberships_create with membership id (edit
573 * membership).
574 * success expected.
575 */
576 function testMembershipCreateUpdateWithIDAndSource() {
577 $membershipID = $this->contactMembershipCreate($this->_params);
578 $params = array(
579 'id' => $membershipID,
580 'source' => 'changed',
581 'contact_id' => $this->_contactID,
582 'status_id' => $this->_membershipStatusID, 'membership_type_id' => $this->_membershipTypeID,
583 'skipStatusCal' => 1,
584 );
585 $result = $this->callAPISuccess('membership', 'create', $params);
586 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
587 $this->callAPISuccess('Membership', 'Delete', array(
588 'id' => $result['id'],
589 ));
590 }
591
592 /**
593 * change custom field using update
594 */
595 function testUpdateWithCustom() {
596 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
597
598 $params = $this->_params;
599 $params['custom_' . $ids['custom_field_id']] = "custom string";
600 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
601 $result = $this->callAPISuccess($this->_entity, 'create', array('id' => $result['id'], 'custom_' . $ids['custom_field_id'] => "new custom"));
602 $check = $this->callAPISuccess($this->_entity, 'get', array('id' => $result['id'], 'contact_id' => $this->_contactID));
603
604 $this->assertEquals("new custom", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
605 $delete = $this->callAPISuccess('Membership', 'Delete', array(
606 'id' => $check['id'],
607 ));
608
609 $this->customFieldDelete($ids['custom_field_id']);
610 $this->customGroupDelete($ids['custom_group_id']);
611 }
612
613 /**
614 * Test civicrm_contact_memberships_create Invalid membership data
615 * Error expected.
616 */
617 function testMembershipCreateInvalidMemData() {
618 //membership_contact_id as string
619 $params = array(
620 'membership_contact_id' => 'Invalid',
621 'membership_type_id' => $this->_membershipTypeID,
622 'join_date' => '2011-01-21',
623 'start_date' => '2010-01-21',
624 'end_date' => '2008-12-21',
625 'source' => 'Payment',
626 'is_override' => 1,
627 'status_id' => $this->_membershipStatusID, );
628
629 $result = $this->callAPIFailure('membership', 'create', $params);
630
631 //membership_contact_id which is no in contact table
632 $params['membership_contact_id'] = 999;
633 $result = $this->callAPIFailure('membership', 'create', $params);
634
635 //invalid join date
636 unset($params['membership_contact_id']);
637 $params['join_date'] = "invalid";
638 $result = $this->callAPIFailure('Membership', 'Create', $params);
639 }
640
641 /**
642 * Test civicrm_contact_memberships_create with membership_contact_id
643 * membership).
644 * Success expected.
645 */
646 function testMembershipCreateWithMemContact() {
647 $params = array(
648 'membership_contact_id' => $this->_contactID,
649 'membership_type_id' => $this->_membershipTypeID,
650 'join_date' => '2011-01-21',
651 'start_date' => '2010-01-21',
652 'end_date' => '2008-12-21',
653 'source' => 'Payment',
654 'is_override' => 1,
655 'status_id' => $this->_membershipStatusID,
656 );
657
658 $result = $this->callAPISuccess('membership', 'create', $params);
659
660 $result = $this->callAPISuccess('Membership', 'Delete', array(
661 'id' => $result['id'],
662 ));
663 }
664 /**
665 * Test civicrm_contact_memberships_create with membership_contact_id
666 * membership).
667 * Success expected.
668 */
669 function testMembershipCreateValidMembershipTypeString() {
670 $params = array(
671 'membership_contact_id' => $this->_contactID,
672 'membership_type_id' => 'General',
673 'join_date' => '2011-01-21',
674 'start_date' => '2010-01-21',
675 'end_date' => '2008-12-21',
676 'source' => 'Payment',
677 'is_override' => 1,
678 'status_id' => $this->_membershipStatusID,
679 );
680
681 $result = $this->callAPISuccess('membership', 'create', $params);
682 $this->assertEquals($this->_membershipTypeID, $result['values'][$result['id']]['membership_type_id']);
683 $result = $this->callAPISuccess('Membership', 'Delete', array(
684 'id' => $result['id'],
685 ));
686 }
687
688 /**
689 * Test civicrm_contact_memberships_create with membership_contact_id
690 * membership).
691 * Success expected.
692 */
693 function testMembershipCreateInValidMembershipTypeString() {
694 $params = array(
695 'membership_contact_id' => $this->_contactID,
696 'membership_type_id' => 'invalid',
697 'join_date' => '2011-01-21',
698 'start_date' => '2010-01-21',
699 'end_date' => '2008-12-21',
700 'source' => 'Payment',
701 'is_override' => 1,
702 'status_id' => $this->_membershipStatusID,
703 );
704
705 $result = $this->callAPIFailure('membership', 'create', $params);
706 }
707
708 /**
709 * Test that if membership join date is not set it defaults to today
710 */
711 function testEmptyJoinDate() {
712 unset($this->_params['join_date'], $this->_params['is_override']);
713 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
714 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
715 $this->assertEquals(date('Y-m-d', strtotime('now')), $result['join_date']);
716 $this->assertEquals('2009-01-21', $result['start_date']);
717 $this->assertEquals('2009-12-21', $result['end_date']);
718 }
719 /**
720 * Test that if membership start date is not set it defaults to correct end date
721 * - fixed
722 */
723 function testEmptyStartDateFixed() {
724 unset($this->_params['start_date'], $this->_params['is_override']);
725 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
726 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
727 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
728 $this->assertEquals('2009-01-21', $result['join_date']);
729 $this->assertEquals('2008-03-01', $result['start_date']);
730 $this->assertEquals('2009-12-21', $result['end_date']);
731 }
732
733 /**
734 * Test that if membership start date is not set it defaults to correct end date
735 * - fixed
736 */
737 function testEmptyStartDateRolling() {
738 unset($this->_params['start_date'], $this->_params['is_override']);
739 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
740 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
741 $this->assertEquals('2009-01-21', $result['join_date']);
742 $this->assertEquals('2009-01-21', $result['start_date']);
743 $this->assertEquals('2009-12-21', $result['end_date']);
744 }
745 /**
746 * Test that if membership end date is not set it defaults to correct end date
747 * - rolling
748 */
749 function testEmptyEndDateFixed() {
750 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
751 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
752 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
753 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
754 $this->assertEquals('2009-01-21', $result['join_date']);
755 $this->assertEquals('2008-03-01', $result['start_date']);
756 $this->assertEquals('2010-02-28', $result['end_date']);
757 }
758 /**
759 * Test that if membership end date is not set it defaults to correct end date
760 * - rolling
761 */
762 function testEmptyEndDateRolling() {
763 unset($this->_params['is_override'], $this->_params['end_date']);
764 $this->_params['membership_type_id'] = $this->_membershipTypeID;
765 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
766 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
767 $this->assertEquals('2009-01-21', $result['join_date']);
768 $this->assertEquals('2009-01-21', $result['start_date']);
769 $this->assertEquals('2010-01-20', $result['end_date']);
770 }
771
772
773 /**
774 * Test that if datesdate are not set they not over-ridden if id is passed in
775 */
776 function testMembershipDatesNotOverridden() {
777 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
778 unset($this->_params['end_date'], $this->_params['start_date']);
779 $this->_params['id'] = $result['id'];
780 $this->callAPISuccess($this->_entity, 'create', $this->_params);
781 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
782 $this->assertEquals('2009-01-21', $result['join_date']);
783 $this->assertEquals('2009-01-21', $result['start_date']);
784 $this->assertEquals('2009-12-21', $result['end_date']);
785
786 }
787 }
788