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