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