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