Merge pull request #11642 from JKingsnorth/CRM-21739
[civicrm-core.git] / tests / phpunit / api / v3 / MembershipTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 * Class api_v3_MembershipTest
37 * @group headless
38 */
39 class api_v3_MembershipTest extends CiviUnitTestCase {
40 protected $_apiversion;
41 protected $_contactID;
42 protected $_membershipID;
43 protected $_membershipID2;
44 protected $_membershipID3;
45 protected $_membershipTypeID;
46 protected $_membershipTypeID2;
47 protected $_membershipStatusID;
48 protected $_entity;
49 protected $_params;
50
51 /**
52 * Set up for tests.
53 */
54 public function setUp() {
55 parent::setUp();
56 $this->_apiversion = 3;
57 $this->_contactID = $this->individualCreate();
58 $this->_membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID));
59 $this->_membershipTypeID2 = $this->membershipTypeCreate(array(
60 'period_type' => 'fixed',
61 // Ie. 1 March.
62 'fixed_period_start_day' => '301',
63 // Ie. 11 Nov.
64 'fixed_period_rollover_day' => '1111',
65 'name' => 'Another one',
66 ));
67 $this->_membershipStatusID = $this->membershipStatusCreate('test status');
68
69 CRM_Member_PseudoConstant::membershipType(NULL, TRUE);
70 CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'name', TRUE);
71 CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name');
72
73 $this->_entity = 'Membership';
74 $this->_params = array(
75 'contact_id' => $this->_contactID,
76 'membership_type_id' => $this->_membershipTypeID,
77 'join_date' => '2009-01-21',
78 'start_date' => '2009-01-21',
79 'end_date' => '2009-12-21',
80 'source' => 'Payment',
81 'is_override' => 1,
82 'status_id' => $this->_membershipStatusID,
83 );
84 }
85
86 /**
87 * Clean up after tests.
88 *
89 * @throws \Exception
90 */
91 public function tearDown() {
92 $this->quickCleanup(array(
93 'civicrm_membership',
94 'civicrm_membership_payment',
95 'civicrm_membership_log',
96 'civicrm_uf_match',
97 ),
98 TRUE
99 );
100 $this->membershipStatusDelete($this->_membershipStatusID);
101 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID2));
102 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID));
103 $this->contactDelete($this->_contactID);
104
105 }
106
107 /**
108 * Test membership deletion.
109 */
110 public function testMembershipDelete() {
111 $membershipID = $this->contactMembershipCreate($this->_params);
112 $this->assertDBRowExist('CRM_Member_DAO_Membership', $membershipID);
113 $params = array(
114 'id' => $membershipID,
115 );
116 $this->callAPIAndDocument('membership', 'delete', $params, __FUNCTION__, __FILE__);
117 $this->assertDBRowNotExist('CRM_Member_DAO_Membership', $membershipID);
118 }
119
120 public function testMembershipDeleteEmpty() {
121 $this->callAPIFailure('membership', 'delete', array());
122 }
123
124 public function testMembershipDeleteInvalidID() {
125 $this->callAPIFailure('membership', 'delete', array('id' => 'blah'));
126 }
127
128 /**
129 * Test civicrm_membership_delete() with invalid Membership Id.
130 */
131 public function testMembershipDeleteWithInvalidMembershipId() {
132 $membershipId = 'membership';
133 $this->callAPIFailure('membership', 'delete', $membershipId);
134 }
135
136 /**
137 * Test membership deletion and with the preserve contribution param.
138 */
139 public function testMembershipDeletePreserveContribution() {
140 $membershipID = $this->contactMembershipCreate($this->_params); //DELETE
141 $this->assertDBRowExist('CRM_Member_DAO_Membership', $membershipID); //DELETE
142 $ContributionCreate = $this->callAPISuccess('Contribution', 'create', array(
143 'sequential' => 1,
144 'financial_type_id' => "Member Dues",
145 'total_amount' => 100,
146 'contact_id' => $this->_params['contact_id'],
147 ));
148 $membershipPaymentCreate = $this->callAPISuccess('MembershipPayment', 'create', array(
149 'sequential' => 1,
150 'contribution_id' => $ContributionCreate['values'][0]['id'],
151 'membership_id' => $membershipID,
152 ));
153 $memParams = array(
154 'id' => $membershipID,
155 'preserve_contribution' => 1,
156 );
157 $contribParams = array(
158 'id' => $ContributionCreate['values'][0]['id'],
159 );
160 $this->callAPIAndDocument('membership', 'delete', $memParams, __FUNCTION__, __FILE__);
161 $this->assertDBRowNotExist('CRM_Member_DAO_Membership', $membershipID);
162 $this->assertDBRowExist('CRM_Contribute_DAO_Contribution', $ContributionCreate['values'][0]['id']);
163 $this->callAPISuccess('Contribution', 'delete', $contribParams);
164 $this->assertDBRowNotExist('CRM_Contribute_DAO_Contribution', $ContributionCreate['values'][0]['id']);
165 }
166
167 /**
168 * Test Activity creation on cancellation of membership contribution.
169 */
170 public function testActivityForCancelledContribution() {
171 $contactId = $this->createLoggedInUser();
172 $membershipID = $this->contactMembershipCreate($this->_params);
173 $this->assertDBRowExist('CRM_Member_DAO_Membership', $membershipID);
174
175 $ContributionCreate = $this->callAPISuccess('Contribution', 'create', array(
176 'financial_type_id' => "Member Dues",
177 'total_amount' => 100,
178 'contact_id' => $this->_params['contact_id'],
179 ));
180 $membershipPaymentCreate = $this->callAPISuccess('MembershipPayment', 'create', array(
181 'sequential' => 1,
182 'contribution_id' => $ContributionCreate['id'],
183 'membership_id' => $membershipID,
184 ));
185 $instruments = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
186 $this->paymentInstruments = $instruments['values'];
187
188 $form = new CRM_Contribute_Form_Contribution();
189 $form->_id = $ContributionCreate['id'];
190 $form->testSubmit(array(
191 'total_amount' => 100,
192 'financial_type_id' => 1,
193 'contact_id' => $contactId,
194 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
195 'contribution_status_id' => 3,
196 ),
197 CRM_Core_Action::UPDATE);
198
199 $activity = $this->callAPISuccess('Activity', 'get', array(
200 'activity_type_id' => "Change Membership Status",
201 'source_record_id' => $membershipID,
202 ));
203 $this->assertNotEmpty($activity['values']);
204 }
205
206 /**
207 * Test membership get.
208 */
209 public function testContactMembershipsGet() {
210 $this->_membershipID = $this->contactMembershipCreate($this->_params);
211 $this->callAPISuccess('membership', 'get', array());
212 $this->callAPISuccess('Membership', 'Delete', array('id' => $this->_membershipID));
213 }
214
215 /**
216 * Test civicrm_membership_get with params not array.
217 *
218 * Gets treated as contact_id, memberships expected.
219 */
220 public function testGetWithParamsContactId() {
221 $this->_membershipID = $this->contactMembershipCreate($this->_params);
222 $params = array(
223 'contact_id' => $this->_contactID,
224 );
225 $membership = $this->callAPISuccess('membership', 'get', $params);
226
227 $result = $membership['values'][$this->_membershipID];
228 $this->callAPISuccess('Membership', 'Delete', array(
229 'id' => $this->_membershipID,
230 ));
231 $this->assertEquals($result['contact_id'], $this->_contactID, "In line " . __LINE__);
232 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID, "In line " . __LINE__);
233 $this->assertEquals($result['status_id'], $this->_membershipStatusID, "In line " . __LINE__);
234 $this->assertEquals($result['join_date'], '2009-01-21', "In line " . __LINE__);
235 $this->assertEquals($result['start_date'], '2009-01-21', "In line " . __LINE__);
236 $this->assertEquals($result['end_date'], '2009-12-21', "In line " . __LINE__);
237 $this->assertEquals($result['source'], 'Payment', "In line " . __LINE__);
238 $this->assertEquals($result['is_override'], 1, "In line " . __LINE__);
239 }
240
241 /**
242 * Test civicrm_membership_get with params not array.
243 *
244 * Gets treated as contact_id, memberships expected.
245 */
246 public function testGetInSyntax() {
247 $this->_membershipID = $this->contactMembershipCreate($this->_params);
248 $this->_membershipID2 = $this->contactMembershipCreate($this->_params);
249 $this->_membershipID3 = $this->contactMembershipCreate($this->_params);
250 $params = array(
251 'id' => array('IN' => array($this->_membershipID, $this->_membershipID3)),
252 );
253 $membership = $this->callAPISuccess('membership', 'get', $params);
254 $this->assertEquals(2, $membership['count']);
255 $this->assertEquals(array($this->_membershipID, $this->_membershipID3), array_keys($membership['values']));
256 $params = array(
257 'id' => array('NOT IN' => array($this->_membershipID, $this->_membershipID3)),
258 );
259 $membership = $this->callAPISuccess('membership', 'get', $params);
260 $this->assertEquals(1, $membership['count']);
261 $this->assertEquals(array($this->_membershipID2), array_keys($membership['values']));
262 }
263
264 /**
265 * Test civicrm_membership_get with params not array.
266 * Gets treated as contact_id, memberships expected.
267 */
268 public function testGetInSyntaxOnContactID() {
269 $this->_membershipID = $this->contactMembershipCreate($this->_params);
270 $contact2 = $this->individualCreate();
271 $contact3 = $this->individualCreate(array('first_name' => 'Scout', 'last_name' => 'Canine'));
272 $this->_membershipID2 = $this->contactMembershipCreate(array_merge($this->_params, array('contact_id' => $contact2)));
273 $this->_membershipID3 = $this->contactMembershipCreate(array_merge($this->_params, array('contact_id' => $contact3)));
274 $params = array(
275 'contact_id' => array('IN' => array($this->_contactID, $contact3)),
276 );
277 $membership = $this->callAPISuccess('membership', 'get', $params);
278 $this->assertEquals(2, $membership['count']);
279 $this->assertEquals(array($this->_membershipID, $this->_membershipID3), array_keys($membership['values']));
280 $params = array(
281 'contact_id' => array('NOT IN' => array($this->_contactID, $contact3)),
282 );
283 $membership = $this->callAPISuccess('membership', 'get', $params);
284 $this->assertEquals(1, $membership['count']);
285 $this->assertEquals(array($this->_membershipID2), array_keys($membership['values']));
286 }
287
288 /**
289 * Test civicrm_membership_get with params not array.
290 *
291 * Gets treated as contact_id, memberships expected.
292 */
293 public function testGetWithParamsMemberShipTypeId() {
294 $this->callAPISuccess($this->_entity, 'create', $this->_params);
295 $params = array(
296 'membership_type_id' => $this->_membershipTypeID,
297 );
298 $membership = $this->callAPISuccess('membership', 'get', $params);
299 $this->callAPISuccess('Membership', 'Delete', array(
300 'id' => $membership['id'],
301 ));
302 $result = $membership['values'][$membership['id']];
303 $this->assertEquals($result['contact_id'], $this->_contactID);
304 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID);
305 $this->assertEquals($result['status_id'], $this->_membershipStatusID);
306 $this->assertEquals($result['join_date'], '2009-01-21');
307 $this->assertEquals($result['start_date'], '2009-01-21');
308 $this->assertEquals($result['end_date'], '2009-12-21');
309 $this->assertEquals($result['source'], 'Payment');
310 $this->assertEquals($result['is_override'], 1);
311 $this->assertEquals($result['id'], $membership['id']);
312 }
313
314 /**
315 * Test civicrm_membership_get with params not array.
316 * Gets treated as contact_id, memberships expected.
317 */
318 public function testGetWithParamsMemberShipTypeIdContactID() {
319 $params = $this->_params;
320 $this->callAPISuccess($this->_entity, 'create', $params);
321 $params['membership_type_id'] = $this->_membershipTypeID2;
322 $this->callAPISuccess($this->_entity, 'create', $params);
323 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_contactID), 2);
324 $params = array(
325 'membership_type_id' => $this->_membershipTypeID,
326 'contact_id' => $this->_contactID,
327 );
328 $result = $this->callAPISuccess('membership', 'getsingle', $params);
329 $this->assertEquals($result['contact_id'], $this->_contactID);
330 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID);
331
332 $params = array(
333 'membership_type_id' => $this->_membershipTypeID2,
334 'contact_id' => $this->_contactID,
335 );
336 $result = $this->callAPISuccess('membership', 'getsingle', $params);
337 $this->assertEquals($result['contact_id'], $this->_contactID);
338 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID2);
339 }
340
341 /**
342 * Check with complete array + custom field.
343 *
344 * Note that the test is written on purpose without any
345 * variables specific to participant so it can be replicated into other entities
346 * and / or moved to the automated test suite
347 */
348 public function testGetWithParamsMemberShipIdAndCustom() {
349 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
350
351 $params = $this->_params;
352 $params['custom_' . $ids['custom_field_id']] = "custom string";
353
354 $result = $this->callAPISuccess($this->_entity, 'create', $params);
355
356 $getParams = array('membership_type_id' => $params['membership_type_id']);
357 $check = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
358 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
359
360 $this->callAPISuccess('Membership', 'Delete', array(
361 'id' => $result['id'],
362 ));
363 }
364
365 /**
366 * Test civicrm_membership_get with proper params.
367 * Memberships expected.
368 */
369 public function testGet() {
370 $membershipID = $this->contactMembershipCreate($this->_params);
371 $params = array(
372 'contact_id' => $this->_contactID,
373 );
374
375 $membership = $this->callAPISuccess('membership', 'get', $params);
376 $result = $membership['values'][$membershipID];
377 $this->callAPISuccess('Membership', 'Delete', array(
378 'id' => $membership['id'],
379 ));
380 $this->assertEquals($result['join_date'], '2009-01-21');
381 $this->assertEquals($result['contact_id'], $this->_contactID);
382 $this->assertEquals($result['membership_type_id'], $this->_membershipTypeID);
383 $this->assertEquals($result['status_id'], $this->_membershipStatusID);
384
385 $this->assertEquals($result['start_date'], '2009-01-21');
386 $this->assertEquals($result['end_date'], '2009-12-21');
387 $this->assertEquals($result['source'], 'Payment');
388 $this->assertEquals($result['is_override'], 1);
389 }
390
391
392 /**
393 * Test civicrm_membership_get with proper params.
394 * Memberships expected.
395 */
396 public function testGetWithId() {
397 $membershipID = $this->contactMembershipCreate($this->_params);
398 $params = array(
399 'contact_id' => $this->_contactID,
400 'id' => $this->_membershipID,
401 'return' => 'id',
402 );
403 $result = $this->callAPISuccess('membership', 'get', $params);
404 $this->assertEquals($membershipID, $result['id']);
405 $params = array(
406 'contact_id' => $this->_contactID,
407 'membership_id' => $this->_membershipID,
408 'return' => 'membership_id',
409 );
410 $result = $this->callAPISuccess('membership', 'get', $params);
411 $this->assertEquals($membershipID, $result['id']);
412 }
413
414 /**
415 * Test civicrm_membership_get for only active.
416 * Memberships expected.
417 */
418 public function testGetOnlyActive() {
419 $description = "Demonstrates use of 'filter' active_only' param.";
420 $this->_membershipID = $this->contactMembershipCreate($this->_params);
421 $params = array(
422 'contact_id' => $this->_contactID,
423 'active_only' => 1,
424 );
425
426 $membership = $this->callAPISuccess('membership', 'get', $params);
427 $this->assertEquals($membership['values'][$this->_membershipID]['status_id'], $this->_membershipStatusID);
428 $this->assertEquals($membership['values'][$this->_membershipID]['contact_id'], $this->_contactID);
429 $params = array(
430 'contact_id' => $this->_contactID,
431 'filters' => array(
432 'is_current' => 1,
433 ),
434 );
435
436 $membership = $this->callAPIAndDocument('membership', 'get', $params, __FUNCTION__, __FILE__, $description, 'FilterIsCurrent');
437 $this->assertEquals($membership['values'][$this->_membershipID]['status_id'], $this->_membershipStatusID);
438 $this->assertEquals($membership['values'][$this->_membershipID]['contact_id'], $this->_contactID);
439
440 $this->callAPISuccess('Membership', 'Delete', array('id' => $this->_membershipID));
441 }
442
443 /**
444 * Test civicrm_membership_get for non exist contact.
445 * empty Memberships.
446 */
447 public function testGetNoContactExists() {
448 $params = array(
449 'contact_id' => 55555,
450 );
451
452 $membership = $this->callAPISuccess('membership', 'get', $params);
453 $this->assertEquals($membership['count'], 0);
454 }
455
456 /**
457 * Test civicrm_membership_get with relationship.
458 * get Memberships.
459 */
460 public function testGetWithRelationship() {
461 $membershipOrgId = $this->organizationCreate(NULL);
462 $memberContactId = $this->individualCreate();
463
464 $relTypeParams = array(
465 'name_a_b' => 'Relation 1',
466 'name_b_a' => 'Relation 2',
467 'description' => 'Testing relationship type',
468 'contact_type_a' => 'Organization',
469 'contact_type_b' => 'Individual',
470 'is_reserved' => 1,
471 'is_active' => 1,
472 );
473 $relTypeID = $this->relationshipTypeCreate($relTypeParams);
474
475 $params = array(
476 'name' => 'test General',
477 'duration_unit' => 'year',
478 'duration_interval' => 1,
479 'period_type' => 'rolling',
480 'member_of_contact_id' => $membershipOrgId,
481 'domain_id' => 1,
482 'financial_type_id' => 1,
483 'relationship_type_id' => $relTypeID,
484 'relationship_direction' => 'b_a',
485 'is_active' => 1,
486 );
487 $memType = $this->callAPISuccess('membership_type', 'create', $params);
488
489 $params = array(
490 'contact_id' => $memberContactId,
491 'membership_type_id' => $memType['id'],
492 'join_date' => '2009-01-21',
493 'start_date' => '2009-01-21',
494 'end_date' => '2009-12-21',
495 'source' => 'Payment',
496 'is_override' => 1,
497 'status_id' => $this->_membershipStatusID,
498 );
499 $membershipID = $this->contactMembershipCreate($params);
500
501 $params = array(
502 'contact_id' => $memberContactId,
503 'membership_type_id' => $memType['id'],
504 );
505
506 $result = $this->callAPISuccess('membership', 'get', $params);
507
508 $membership = $result['values'][$membershipID];
509 $this->assertEquals($this->_membershipStatusID, $membership['status_id']);
510 $this->callAPISuccess('Membership', 'Delete', array(
511 'id' => $membership['id'],
512 ));
513 $this->membershipTypeDelete(array('id' => $memType['id']));
514 $this->relationshipTypeDelete($relTypeID);
515 $this->contactDelete($membershipOrgId);
516 $this->contactDelete($memberContactId);
517 }
518
519 /**
520 * Test civicrm_membership_create with relationships.
521 * create/get Memberships.
522 *
523 * Test suite for CRM-14758: API ( contact, create ) does not always create related membership
524 * and max_related property for Membership_Type and Membership entities
525 */
526 public function testCreateWithRelationship() {
527 // Create membership type: inherited through employment, max_related = 2
528 $params = array(
529 'name_a_b' => 'Employee of',
530 );
531 $result = $this->callAPISuccess('relationship_type', 'get', $params);
532 $relationshipTypeId = $result['id'];
533 $membershipOrgId = $this->organizationCreate();
534 $params = array(
535 'name' => 'Corporate Membership',
536 'duration_unit' => 'year',
537 'duration_interval' => 1,
538 'period_type' => 'rolling',
539 'member_of_contact_id' => $membershipOrgId,
540 'domain_id' => 1,
541 'financial_type_id' => 1,
542 'relationship_type_id' => $relationshipTypeId,
543 'relationship_direction' => 'b_a',
544 'max_related' => 2,
545 'is_active' => 1,
546 );
547 $result = $this->callAPISuccess('membership_type', 'create', $params);
548 $membershipTypeId = $result['id'];
549
550 // Create employer and first employee
551 $employerId[0] = $this->organizationCreate(array(), 1);
552 $memberContactId[0] = $this->individualCreate(array('employer_id' => $employerId[0]), 0);
553
554 // Create organization's membership
555 $params = array(
556 'contact_id' => $employerId[0],
557 'membership_type_id' => $membershipTypeId,
558 'source' => 'Test suite',
559 'start_date' => date('Y-m-d'),
560 'end_date' => "+1 year",
561 );
562 $OrganizationMembershipID = $this->contactMembershipCreate($params);
563
564 // Check that the employee inherited the membership
565 $params = array(
566 'contact_id' => $memberContactId[0],
567 'membership_type_id' => $membershipTypeId,
568 );
569
570 $result = $this->callAPISuccess('membership', 'get', $params);
571
572 $this->assertEquals(1, $result['count']);
573 $result = $result['values'][$result['id']];
574 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
575
576 // Create second employee
577 $memberContactId[1] = $this->individualCreate(array('employer_id' => $employerId[0]), 1);
578
579 // Check that the employee inherited the membership
580 $params = array(
581 'contact_id' => $memberContactId[1],
582 'membership_type_id' => $membershipTypeId,
583 );
584 $result = $this->callAPISuccess('membership', 'get', $params);
585 // If it fails here CRM-14758 is not fixed
586 $this->assertEquals(1, $result['count']);
587 $result = $result['values'][$result['id']];
588 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
589
590 // Create third employee
591 $memberContactId[2] = $this->individualCreate(array('employer_id' => $employerId[0]), 2);
592
593 // Check that employee does NOT inherit the membership (max_related = 2)
594 $params = array(
595 'contact_id' => $memberContactId[2],
596 'membership_type_id' => $membershipTypeId,
597 );
598 $result = $this->callAPISuccess('membership', 'get', $params);
599 $this->assertEquals(0, $result['count']);
600
601 // Increase max_related for the employer's membership
602 $params = array(
603 'id' => $OrganizationMembershipID,
604 'max_related' => 3,
605 );
606 $this->callAPISuccess('Membership', 'create', $params);
607
608 // Check that the employee inherited the membership
609 $params = array(
610 'contact_id' => $memberContactId[2],
611 'membership_type_id' => $membershipTypeId,
612 );
613 $result = $this->callAPISuccess('membership', 'get', $params);
614 $this->assertEquals(1, $result['count']);
615 $result = $result['values'][$result['id']];
616 $this->assertEquals($OrganizationMembershipID, $result['owner_membership_id']);
617
618 // First employee moves to a new job
619 $employerId[1] = $this->organizationCreate(array(), 2);
620 $params = array(
621 'id' => $memberContactId[0],
622 'employer_id' => $employerId[1],
623 );
624 $this->callAPISuccess('contact', 'create', $params);
625
626 // Check that employee does NO LONGER inherit the membership
627 $params = array(
628 'contact_id' => $memberContactId[0],
629 'membership_type_id' => $membershipTypeId,
630 );
631 $result = $this->callAPISuccess('membership', 'get', $params);
632 $this->assertEquals(0, $result['count']);
633
634 // Set up params for enable/disable checks
635 $relationship1 = $this->callAPISuccess('relationship', 'get', array('contact_id_a' => $memberContactId[1]));
636 $params = array(
637 'contact_id' => $memberContactId[1],
638 'membership_type_id' => $membershipTypeId,
639 );
640
641 // Deactivate relationship using create and assert membership is not inherited
642 $this->callAPISuccess('relationship', 'create', array('id' => $relationship1['id'], 'is_active' => 0));
643 $result = $this->callAPISuccess('membership', 'get', $params);
644 $this->assertEquals(0, $result['count']);
645
646 // Re-enable relationship using create and assert membership is inherited
647 $this->callAPISuccess('relationship', 'create', array('id' => $relationship1['id'], 'is_active' => 1));
648 $result = $this->callAPISuccess('membership', 'get', $params);
649 $this->assertEquals(1, $result['count']);
650
651 // Deactivate relationship using setvalue and assert membership is not inherited
652 $this->callAPISuccess('relationship', 'setvalue', array('id' => $relationship1['id'], 'field' => 'is_active', 'value' => 0));
653 $result = $this->callAPISuccess('membership', 'get', $params);
654 $this->assertEquals(0, $result['count']);
655
656 // Re-enable relationship using setvalue and assert membership is inherited
657 $this->callAPISuccess('relationship', 'setvalue', array('id' => $relationship1['id'], 'field' => 'is_active', 'value' => 1));
658 $result = $this->callAPISuccess('membership', 'get', $params);
659 $this->assertEquals(1, $result['count']);
660
661 // Delete relationship and assert membership is not inherited
662 $this->callAPISuccess('relationship', 'delete', array('id' => $relationship1['id']));
663 $result = $this->callAPISuccess('membership', 'get', $params);
664 $this->assertEquals(0, $result['count']);
665
666 // Tear down - reverse of creation to be safe
667 $this->contactDelete($memberContactId[2]);
668 $this->contactDelete($memberContactId[1]);
669 $this->contactDelete($memberContactId[0]);
670 $this->contactDelete($employerId[1]);
671 $this->contactDelete($employerId[0]);
672 $this->membershipTypeDelete(array('id' => $membershipTypeId));
673 $this->contactDelete($membershipOrgId);
674 }
675
676 /**
677 * We are checking for no e-notices + only id & end_date returned
678 */
679 public function testMembershipGetWithReturn() {
680 $this->contactMembershipCreate($this->_params);
681 $result = $this->callAPISuccess('membership', 'get', array('return' => 'end_date'));
682 foreach ($result['values'] as $membership) {
683 $this->assertEquals(array('id', 'end_date'), array_keys($membership));
684 }
685 }
686 ///////////////// civicrm_membership_create methods
687
688 /**
689 * Test civicrm_contact_memberships_create with empty params.
690 * Error expected.
691 */
692 public function testCreateWithEmptyParams() {
693 $params = array();
694 $this->callAPIFailure('membership', 'create', $params);
695 }
696
697 /**
698 * If is_overide is passed in status must also be passed in.
699 */
700 public function testCreateOverrideNoStatus() {
701 $params = $this->_params;
702 unset($params['status_id']);
703 $this->callAPIFailure('membership', 'create', $params);
704 }
705
706 public function testMembershipCreateMissingRequired() {
707 $params = array(
708 'membership_type_id' => '1',
709 'join_date' => '2006-01-21',
710 'start_date' => '2006-01-21',
711 'end_date' => '2006-12-21',
712 'source' => 'Payment',
713 'status_id' => '2',
714 );
715
716 $this->callAPIFailure('membership', 'create', $params);
717 }
718
719 public function testMembershipCreate() {
720 $params = array(
721 'contact_id' => $this->_contactID,
722 'membership_type_id' => $this->_membershipTypeID,
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->callAPIAndDocument('membership', 'create', $params, __FUNCTION__, __FILE__);
732 $this->getAndCheck($params, $result['id'], $this->_entity);
733 $this->assertNotNull($result['id']);
734 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['contact_id'], " in line " . __LINE__);
735 $this->assertEquals($result['id'], $result['values'][$result['id']]['id'], " in line " . __LINE__);
736 }
737
738 /**
739 * Check for useful message if contact doesn't exist
740 */
741 public function testMembershipCreateWithInvalidContact() {
742 $params = array(
743 'contact_id' => 999,
744 'membership_type_id' => $this->_membershipTypeID,
745 'join_date' => '2006-01-21',
746 'start_date' => '2006-01-21',
747 'end_date' => '2006-12-21',
748 'source' => 'Payment',
749 'is_override' => 1,
750 'status_id' => $this->_membershipStatusID,
751 );
752
753 $this->callAPIFailure('membership', 'create', $params,
754 'contact_id is not valid : 999'
755 );
756 }
757
758 public function testMembershipCreateWithInvalidStatus() {
759 $params = $this->_params;
760 $params['status_id'] = 999;
761 $this->callAPIFailure('membership', 'create', $params,
762 "'999' is not a valid option for field status_id"
763 );
764 }
765
766 public function testMembershipCreateWithInvalidType() {
767 $params = $this->_params;
768 $params['membership_type_id'] = 999;
769
770 $this->callAPIFailure('membership', 'create', $params,
771 "'999' is not a valid option for field membership_type_id"
772 );
773 }
774
775 /**
776 * Check with complete array + custom field
777 * Note that the test is written on purpose without any
778 * variables specific to participant so it can be replicated into other entities
779 * and / or moved to the automated test suite
780 */
781 public function testCreateWithCustom() {
782 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
783
784 $params = $this->_params;
785 $params['custom_' . $ids['custom_field_id']] = "custom string";
786
787 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, NULL, 'CreateWithCustomData');
788 $check = $this->callAPISuccess($this->_entity, 'get', array(
789 'id' => $result['id'],
790 'contact_id' => $this->_contactID,
791 ));
792 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
793 }
794
795 /**
796 * Search on custom field value.
797 */
798 public function testSearchWithCustomDataCRM16036() {
799 // Create a custom field on membership
800 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
801
802 // Create a new membership, but don't assign anything to the custom field.
803 $params = $this->_params;
804 $result = $this->callAPIAndDocument(
805 $this->_entity,
806 'create',
807 $params,
808 __FUNCTION__,
809 __FILE__,
810 NULL,
811 'SearchWithCustomData');
812
813 // search memberships with CRM-16036 as custom field value.
814 // Since we did not touch the custom field of any membership,
815 // this should not return any results.
816 $check = $this->callAPISuccess($this->_entity, 'get', array(
817 'custom_' . $ids['custom_field_id'] => "CRM-16036",
818 ));
819
820 // Cleanup.
821 $this->callAPISuccess($this->_entity, 'delete', array(
822 'id' => $result['id'],
823 ));
824
825 // Assert.
826 $this->assertEquals(0, $check['count']);
827 }
828
829 /**
830 * Test civicrm_contact_memberships_create with membership id (edit
831 * membership).
832 * success expected.
833 */
834 public function testMembershipCreateWithId() {
835 $membershipID = $this->contactMembershipCreate($this->_params);
836 $params = array(
837 'id' => $membershipID,
838 'contact_id' => $this->_contactID,
839 'membership_type_id' => $this->_membershipTypeID,
840 'join_date' => '2006-01-21',
841 'start_date' => '2006-01-21',
842 'end_date' => '2006-12-21',
843 'source' => 'Payment',
844 'is_override' => 1,
845 'status_id' => $this->_membershipStatusID,
846 );
847
848 $result = $this->callAPISuccess('membership', 'create', $params);
849
850 //Update Status and check activities created.
851 $updateStatus = array(
852 'id' => $result['id'],
853 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'Cancelled'),
854 );
855 $this->callAPISuccess('Membership', 'create', $updateStatus);
856 $activities = CRM_Activity_BAO_Activity::getContactActivity($this->_contactID);
857 $this->assertEquals(2, count($activities));
858 $activityNames = array_flip(CRM_Utils_Array::collect('activity_name', $activities));
859 $this->assertArrayHasKey('Membership Signup', $activityNames);
860 $this->assertArrayHasKey('Change Membership Status', $activityNames);
861
862 $this->callAPISuccess('Membership', 'Delete', array(
863 'id' => $result['id'],
864 ));
865 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
866 }
867
868 /**
869 * Test civicrm_contact_memberships_create with membership id (edit
870 * membership).
871 * success expected.
872 */
873 public function testMembershipCreateUpdateWithIdNoContact() {
874 $membershipID = $this->contactMembershipCreate($this->_params);
875 $params = array(
876 'id' => $membershipID,
877 'membership_type_id' => $this->_membershipTypeID,
878 'contact_id' => $this->_contactID,
879 'join_date' => '2006-01-21',
880 'start_date' => '2006-01-21',
881 'end_date' => '2006-12-21',
882 'source' => 'Payment',
883 'is_override' => 1,
884 'status_id' => $this->_membershipStatusID,
885 );
886
887 $result = $this->callAPISuccess('membership', 'create', $params);
888 $this->callAPISuccess('Membership', 'Delete', array(
889 'id' => $result['id'],
890 ));
891
892 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
893 }
894
895 /**
896 * Test civicrm_contact_memberships_create with membership id (edit
897 * membership).
898 * success expected.
899 */
900 public function testMembershipCreateUpdateWithIdNoDates() {
901 $membershipID = $this->contactMembershipCreate($this->_params);
902 $params = array(
903 'id' => $membershipID,
904 'contact_id' => $this->_contactID,
905 'membership_type_id' => $this->_membershipTypeID,
906 'source' => 'Payment',
907 'is_override' => 1,
908 'status_id' => $this->_membershipStatusID,
909 );
910
911 $result = $this->callAPISuccess('membership', 'create', $params);
912 $this->callAPISuccess('Membership', 'Delete', array(
913 'id' => $result['id'],
914 ));
915 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
916 }
917
918 /**
919 * Test civicrm_contact_memberships_create with membership id (edit
920 * membership).
921 * success expected.
922 */
923 public function testMembershipCreateUpdateWithIdNoDatesNoType() {
924 $membershipID = $this->contactMembershipCreate($this->_params);
925 $params = array(
926 'id' => $membershipID,
927 'source' => 'not much here',
928 'contact_id' => $this->_contactID,
929 'is_override' => 1,
930 'status_id' => $this->_membershipStatusID,
931 );
932
933 $result = $this->callAPISuccess('membership', 'create', $params);
934 $this->callAPISuccess('Membership', 'Delete', array(
935 'id' => $result['id'],
936 ));
937 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
938 }
939
940 /**
941 * Test civicrm_contact_memberships_create with membership id (edit
942 * membership).
943 * success expected.
944 */
945 public function testMembershipCreateUpdateWithIDAndSource() {
946 $membershipID = $this->contactMembershipCreate($this->_params);
947 $params = array(
948 'id' => $membershipID,
949 'source' => 'changed',
950 'contact_id' => $this->_contactID,
951 'status_id' => $this->_membershipStatusID,
952 'membership_type_id' => $this->_membershipTypeID,
953 'skipStatusCal' => 1,
954 );
955 $result = $this->callAPISuccess('membership', 'create', $params);
956 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
957 $this->callAPISuccess('Membership', 'Delete', array(
958 'id' => $result['id'],
959 ));
960 }
961
962 /**
963 * Change custom field using update.
964 */
965 public function testUpdateWithCustom() {
966 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
967
968 $params = $this->_params;
969 $params['custom_' . $ids['custom_field_id']] = "custom string";
970 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, NULL, 'UpdateCustomData');
971 $result = $this->callAPISuccess($this->_entity, 'create', array(
972 'id' => $result['id'],
973 'custom_' . $ids['custom_field_id'] => "new custom",
974 ));
975 $check = $this->callAPISuccess($this->_entity, 'get', array(
976 'id' => $result['id'],
977 'contact_id' => $this->_contactID,
978 ));
979
980 $this->assertEquals("new custom", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
981 $this->callAPISuccess('Membership', 'Delete', array(
982 'id' => $check['id'],
983 ));
984
985 $this->customFieldDelete($ids['custom_field_id']);
986 $this->customGroupDelete($ids['custom_group_id']);
987 }
988
989 /**
990 * per CRM-15746 check that the id can be altered in an update hook
991 */
992 public function testMembershipUpdateCreateHookCRM15746() {
993 $this->hookClass->setHook('civicrm_pre', array($this, 'hook_civicrm_pre_update_create_membership'));
994 $result = $this->callAPISuccess('membership', 'create', $this->_params);
995 $this->callAPISuccess('membership', 'create', array('id' => $result['id'], 'end_date' => '1 year ago'));
996 $this->callAPISuccessGetCount('membership', array(), 2);
997 $this->hookClass->reset();
998 $this->callAPISuccess('membership', 'create', array('id' => $result['id'], 'end_date' => '1 year ago'));
999 $this->callAPISuccessGetCount('membership', array(), 2);
1000 }
1001
1002 /**
1003 * Custom hook for update membership.
1004 *
1005 * @param string $op
1006 * @param object $objectName
1007 * @param int $id
1008 * @param array $params
1009 *
1010 * @throws \Exception
1011 */
1012 public function hook_civicrm_pre_update_create_membership($op, $objectName, $id, &$params) {
1013 if ($objectName == 'Membership' && $op == 'edit') {
1014 $existingMembership = $this->callAPISuccessGetSingle('membership', array('id' => $params['id']));
1015 unset($params['id'], $params['membership_id']);
1016 $params['join_date'] = $params['membership_start_date'] = $params['start_date'] = date('Ymd000000', strtotime($existingMembership['start_date']));
1017 $params = array_merge($existingMembership, $params);
1018 $params['id'] = NULL;
1019 }
1020 }
1021
1022 /**
1023 * Test civicrm_contact_memberships_create Invalid membership data.
1024 * Error expected.
1025 */
1026 public function testMembershipCreateInvalidMemData() {
1027 //membership_contact_id as string
1028 $params = array(
1029 'membership_contact_id' => 'Invalid',
1030 'membership_type_id' => $this->_membershipTypeID,
1031 'join_date' => '2011-01-21',
1032 'start_date' => '2010-01-21',
1033 'end_date' => '2008-12-21',
1034 'source' => 'Payment',
1035 'is_override' => 1,
1036 'status_id' => $this->_membershipStatusID,
1037 );
1038
1039 $this->callAPIFailure('membership', 'create', $params);
1040
1041 //membership_contact_id which is no in contact table
1042 $params['membership_contact_id'] = 999;
1043 $this->callAPIFailure('membership', 'create', $params);
1044
1045 //invalid join date
1046 unset($params['membership_contact_id']);
1047 $params['join_date'] = "invalid";
1048 $this->callAPIFailure('Membership', 'Create', $params);
1049 }
1050
1051 /**
1052 * Test civicrm_contact_memberships_create with membership_contact_id
1053 * membership).
1054 * Success expected.
1055 */
1056 public function testMembershipCreateWithMemContact() {
1057 $params = array(
1058 'membership_contact_id' => $this->_contactID,
1059 'membership_type_id' => $this->_membershipTypeID,
1060 'join_date' => '2011-01-21',
1061 'start_date' => '2010-01-21',
1062 'end_date' => '2008-12-21',
1063 'source' => 'Payment',
1064 'is_override' => 1,
1065 'status_id' => $this->_membershipStatusID,
1066 );
1067
1068 $result = $this->callAPISuccess('membership', 'create', $params);
1069
1070 $this->callAPISuccess('Membership', 'Delete', array(
1071 'id' => $result['id'],
1072 ));
1073 }
1074
1075 /**
1076 * Test civicrm_contact_memberships_create with membership_contact_id
1077 * membership).
1078 * Success expected.
1079 */
1080 public function testMembershipCreateValidMembershipTypeString() {
1081 $params = array(
1082 'membership_contact_id' => $this->_contactID,
1083 'membership_type_id' => 'General',
1084 'join_date' => '2011-01-21',
1085 'start_date' => '2010-01-21',
1086 'end_date' => '2008-12-21',
1087 'source' => 'Payment',
1088 'is_override' => 1,
1089 'status_id' => $this->_membershipStatusID,
1090 );
1091
1092 $result = $this->callAPISuccess('membership', 'create', $params);
1093 $this->assertEquals($this->_membershipTypeID, $result['values'][$result['id']]['membership_type_id']);
1094 $this->callAPISuccess('Membership', 'Delete', array(
1095 'id' => $result['id'],
1096 ));
1097 }
1098
1099 /**
1100 * Test civicrm_contact_memberships_create with membership_contact_id
1101 * membership).
1102 * Success expected.
1103 */
1104 public function testMembershipCreateInValidMembershipTypeString() {
1105 $params = array(
1106 'membership_contact_id' => $this->_contactID,
1107 'membership_type_id' => 'invalid',
1108 'join_date' => '2011-01-21',
1109 'start_date' => '2010-01-21',
1110 'end_date' => '2008-12-21',
1111 'source' => 'Payment',
1112 'is_override' => 1,
1113 'status_id' => $this->_membershipStatusID,
1114 );
1115
1116 $this->callAPIFailure('membership', 'create', $params);
1117 }
1118
1119 /**
1120 * Test that if membership join date is not set it defaults to today.
1121 */
1122 public function testEmptyJoinDate() {
1123 unset($this->_params['join_date'], $this->_params['is_override']);
1124 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1125 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1126 $this->assertEquals(date('Y-m-d', strtotime('now')), $result['join_date']);
1127 $this->assertEquals('2009-01-21', $result['start_date']);
1128 $this->assertEquals('2009-12-21', $result['end_date']);
1129 }
1130
1131 /**
1132 * Test that if membership start date is not set it defaults to correct end date.
1133 * - fixed
1134 */
1135 public function testEmptyStartDateFixed() {
1136 unset($this->_params['start_date'], $this->_params['is_override']);
1137 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1138 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1139 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1140 $this->assertEquals('2009-01-21', $result['join_date']);
1141 $this->assertEquals('2008-03-01', $result['start_date']);
1142 $this->assertEquals('2009-12-21', $result['end_date']);
1143 }
1144
1145 /**
1146 * Test that if membership start date is not set it defaults to correct end date
1147 * - fixed
1148 */
1149 public function testEmptyStartEndDateFixedOneYear() {
1150 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1151 $this->callAPISuccess('membership_type', 'create', array('id' => $this->_membershipTypeID2, 'duration_interval' => 1));
1152 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1153 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1154 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1155 $this->assertEquals('2009-01-21', $result['join_date']);
1156 $this->assertEquals('2008-03-01', $result['start_date']);
1157 $this->assertEquals('2010-02-28', $result['end_date']);
1158 }
1159
1160 /**
1161 * Test that if membership start date is not set it defaults to correct end date for fixed multi year memberships.
1162 */
1163 public function testEmptyStartEndDateFixedMultiYear() {
1164 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1165 $this->callAPISuccess('membership_type', 'create', array('id' => $this->_membershipTypeID2, 'duration_interval' => 5));
1166 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1167 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1168 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1169 $this->assertEquals('2009-01-21', $result['join_date']);
1170 $this->assertEquals('2008-03-01', $result['start_date']);
1171 $this->assertEquals('2014-02-28', $result['end_date']);
1172 }
1173
1174 /**
1175 * CRM-18503 - Test membership join date is correctly set for fixed memberships.
1176 */
1177 public function testMembershipJoinDateFixed() {
1178 $memStatus = CRM_Member_PseudoConstant::membershipStatus();
1179 // Update the fixed membership type to 1 year duration.
1180 $this->callAPISuccess('membership_type', 'create', array('id' => $this->_membershipTypeID2, 'duration_interval' => 1));
1181 $contactId = $this->createLoggedInUser();
1182 // Create membership with 'Pending' status.
1183 $params = array(
1184 'contact_id' => $contactId,
1185 'membership_type_id' => $this->_membershipTypeID2,
1186 'source' => 'test membership',
1187 'is_pay_later' => 0,
1188 'status_id' => array_search('Pending', $memStatus),
1189 'skipStatusCal' => 1,
1190 'is_for_organization' => 1,
1191 );
1192 $ids = array();
1193 $membership = CRM_Member_BAO_Membership::create($params, $ids);
1194
1195 // Update membership to 'Completed' and check the dates.
1196 $memParams = array(
1197 'id' => $membership->id,
1198 'contact_id' => $contactId,
1199 'is_test' => 0,
1200 'membership_type_id' => $this->_membershipTypeID2,
1201 'num_terms' => 1,
1202 'status_id' => array_search('New', $memStatus),
1203 );
1204 $result = $this->callAPISuccess('Membership', 'create', $memParams);
1205
1206 // Extend duration interval if join_date exceeds the rollover period.
1207 $joinDate = date('Y-m-d');
1208 $year = date('Y');
1209 $startDate = date('Y-m-d', strtotime(date('Y-03-01')));
1210 $rollOver = TRUE;
1211 if (strtotime($startDate) > time()) {
1212 $rollOver = FALSE;
1213 $startDate = date('Y-m-d', strtotime(date('Y-03-01') . '- 1 year'));
1214 }
1215 $membershipTypeDetails = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($this->_membershipTypeID2);
1216 $fixedPeriodRollover = CRM_Member_BAO_MembershipType::isDuringFixedAnnualRolloverPeriod($joinDate, $membershipTypeDetails, $year, $startDate);
1217 $y = 1;
1218 if ($fixedPeriodRollover && $rollOver) {
1219 $y += 1;
1220 }
1221
1222 $expectedDates = array(
1223 'join_date' => date('Ymd'),
1224 'start_date' => str_replace('-', '', $startDate),
1225 'end_date' => date('Ymd', strtotime(date('Y-03-01') . "+ {$y} year - 1 day")),
1226 );
1227 foreach ($result['values'] as $values) {
1228 foreach ($expectedDates as $date => $val) {
1229 $this->assertEquals($val, $values[$date], "Failed asserting {$date} values");
1230 }
1231 }
1232 }
1233
1234 /**
1235 * Test correct end and start dates are calculated for fixed multi year memberships.
1236 *
1237 * The empty start date is calculated to be the start_date (1 Jan prior to the join_date - so 1 Jan 15)
1238 *
1239 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1240 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1241 * and we add on 4 years rather than 5 because we are not after the rollover day - so we calculate 31 Dec 2019
1242 */
1243 public function testFixedMultiYearDateSetTwoEmptyStartEndDate() {
1244 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1245
1246 $this->callAPISuccess('membership_type', 'create', array(
1247 'id' => $this->_membershipTypeID2,
1248 'duration_interval' => 5,
1249 // Ie 1 Jan.
1250 'fixed_period_start_day' => '101',
1251 // Ie. 1 Nov.
1252 'fixed_period_rollover_day' => '1101',
1253 ));
1254 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1255 $dates = array(
1256 'join_date' => '28-Jan 2015',
1257 );
1258 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1259 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1260 $this->assertEquals('2015-01-28', $result['join_date']);
1261 $this->assertEquals('2015-01-01', $result['start_date']);
1262 $this->assertEquals('2019-12-31', $result['end_date']);
1263 }
1264
1265 /**
1266 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
1267 *
1268 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1269 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1270 * and we add on 4 years rather than 5 because we are not after the rollover day - so we calculate 31 Dec 2019
1271 */
1272 public function testFixedMultiYearDateSetTwoEmptyEndDate() {
1273 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1274
1275 $this->callAPISuccess('membership_type', 'create', array(
1276 'id' => $this->_membershipTypeID2,
1277 'duration_interval' => 5,
1278 // Ie 1 Jan.
1279 'fixed_period_start_day' => '101',
1280 // Ie. 1 Nov.
1281 'fixed_period_rollover_day' => '1101',
1282 ));
1283 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1284 $dates = array(
1285 'start_date' => '28-Jan 2015',
1286 'join_date' => '28-Jan 2015',
1287 );
1288 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1289 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1290 $this->assertEquals('2015-01-28', $result['join_date']);
1291 $this->assertEquals('2015-01-28', $result['start_date']);
1292 $this->assertEquals('2019-12-31', $result['end_date']);
1293 }
1294
1295 /**
1296 * Test correct end and start dates are calculated for fixed multi year memberships.
1297 *
1298 * The empty start date is calculated to be the start_date (1 Jan prior to the join_date - so 1 Jan 15)
1299 *
1300 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1301 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1302 * and we add on <1 years rather than > 1 because we are not after the rollover day - so we calculate 31 Dec 2015
1303 */
1304 public function testFixedSingleYearDateSetTwoEmptyStartEndDate() {
1305 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1306
1307 $this->callAPISuccess('membership_type', 'create', array(
1308 'id' => $this->_membershipTypeID2,
1309 'duration_interval' => 1,
1310 // Ie 1 Jan.
1311 'fixed_period_start_day' => '101',
1312 // Ie. 1 Nov.
1313 'fixed_period_rollover_day' => '1101',
1314 ));
1315 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1316 $dates = array(
1317 'join_date' => '28-Jan 2015',
1318 );
1319 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1320 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1321 $this->assertEquals('2015-01-28', $result['join_date']);
1322 $this->assertEquals('2015-01-01', $result['start_date']);
1323 $this->assertEquals('2015-12-31', $result['end_date']);
1324 }
1325
1326 /**
1327 * Test correct end date for fixed single year memberships is calculated and start_date is not changed.
1328 *
1329 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1330 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1331 * and we add on <1 years rather than > 1 because we are not after the rollover day - so we calculate 31 Dec 2015
1332 */
1333 public function testFixedSingleYearDateSetTwoEmptyEndDate() {
1334 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1335
1336 $this->callAPISuccess('membership_type', 'create', array(
1337 'id' => $this->_membershipTypeID2,
1338 'duration_interval' => 1,
1339 // Ie 1 Jan.
1340 'fixed_period_start_day' => '101',
1341 // Ie. 1 Nov.
1342 'fixed_period_rollover_day' => '1101',
1343 ));
1344 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1345 $dates = array(
1346 'start_date' => '28-Jan 2015',
1347 'join_date' => '28-Jan 2015',
1348 );
1349 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1350 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1351 $this->assertEquals('2015-01-28', $result['join_date']);
1352 $this->assertEquals('2015-01-28', $result['start_date']);
1353 $this->assertEquals('2015-12-31', $result['end_date']);
1354 }
1355
1356 /**
1357 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
1358 *
1359 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1360 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1361 * and we add on 1 year we are after the rollover day - so we calculate 31 Oct 2016
1362 */
1363 public function testFixedSingleYearDateSetThreeEmptyEndDate() {
1364 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1365
1366 $this->callAPISuccess('membership_type', 'create', array(
1367 'id' => $this->_membershipTypeID2,
1368 'duration_interval' => 1,
1369 // Ie. 1 Nov.
1370 'fixed_period_start_day' => '1101',
1371 // Ie 1 Jan.
1372 'fixed_period_rollover_day' => '101',
1373 ));
1374 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1375 $dates = array(
1376 'start_date' => '28-Jan 2015',
1377 'join_date' => '28-Jan 2015',
1378 );
1379 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1380 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1381 $this->assertEquals('2015-01-28', $result['join_date']);
1382 $this->assertEquals('2015-01-28', $result['start_date']);
1383 $this->assertEquals('2016-10-31', $result['end_date']);
1384 }
1385
1386 /**
1387 * Test correct end and start dates are calculated for fixed multi year memberships.
1388 *
1389 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
1390 *
1391 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1392 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1393 * and we add on 1 year we are after the rollover day - so we calculate 31 Oct 2016
1394 */
1395 public function testFixedSingleYearDateSetThreeEmptyStartEndDate() {
1396 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1397
1398 $this->callAPISuccess('membership_type', 'create', array(
1399 'id' => $this->_membershipTypeID2,
1400 'duration_interval' => 1,
1401 // Ie. 1 Nov.
1402 'fixed_period_start_day' => '1101',
1403 // Ie 1 Jan.
1404 'fixed_period_rollover_day' => '101',
1405 ));
1406 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1407 $dates = array(
1408 'join_date' => '28-Jan 2015',
1409 );
1410 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1411 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1412 $this->assertEquals('2015-01-28', $result['join_date']);
1413 $this->assertEquals('2014-11-01', $result['start_date']);
1414 $this->assertEquals('2016-10-31', $result['end_date']);
1415 }
1416
1417 /**
1418 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
1419 *
1420 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1421 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1422 * and we add on 5 years we are after the rollover day - so we calculate 31 Oct 2020
1423 */
1424 public function testFixedMultiYearDateSetThreeEmptyEndDate() {
1425 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1426
1427 $this->callAPISuccess('membership_type', 'create', array(
1428 'id' => $this->_membershipTypeID2,
1429 'duration_interval' => 5,
1430 // Ie. 1 Nov.
1431 'fixed_period_start_day' => '1101',
1432 // Ie 1 Jan.
1433 'fixed_period_rollover_day' => '101',
1434 ));
1435 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1436 $dates = array(
1437 'start_date' => '28-Jan 2015',
1438 'join_date' => '28-Jan 2015',
1439 );
1440 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1441 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1442 $this->assertEquals('2015-01-28', $result['join_date']);
1443 $this->assertEquals('2015-01-28', $result['start_date']);
1444 $this->assertEquals('2020-10-31', $result['end_date']);
1445 }
1446
1447 /**
1448 * Test correct end and start dates are calculated for fixed multi year memberships.
1449 *
1450 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
1451 *
1452 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
1453 * In this set our join date is after the start day and after the rollover day so we do get an extra year
1454 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1455 * and we add on 5 years we are after the rollover day - so we calculate 31 Oct 2020
1456 */
1457 public function testFixedMultiYearDateSetThreeEmptyStartEndDate() {
1458 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1459
1460 $this->callAPISuccess('membership_type', 'create', array(
1461 'id' => $this->_membershipTypeID2,
1462 'duration_interval' => 5,
1463 // Ie. 1 Nov.
1464 'fixed_period_start_day' => '1101',
1465 // Ie 1 Jan.
1466 'fixed_period_rollover_day' => '101',
1467 ));
1468 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1469 $dates = array(
1470 'join_date' => '28-Jan 2015',
1471 );
1472 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1473 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1474 $this->assertEquals('2015-01-28', $result['join_date']);
1475 $this->assertEquals('2014-11-01', $result['start_date']);
1476 $this->assertEquals('2020-10-31', $result['end_date']);
1477 }
1478
1479 /**
1480 * Test that if membership start date is not set it defaults to correct end date for fixed single year memberships.
1481 */
1482 public function testEmptyStartDateRolling() {
1483 unset($this->_params['start_date'], $this->_params['is_override']);
1484 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1485 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1486 $this->assertEquals('2009-01-21', $result['join_date']);
1487 $this->assertEquals('2009-01-21', $result['start_date']);
1488 $this->assertEquals('2009-12-21', $result['end_date']);
1489 }
1490
1491 /**
1492 * Test that if membership end date is not set it defaults to correct end date.
1493 * - rolling
1494 */
1495 public function testEmptyEndDateFixed() {
1496 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1497 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1498 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1499 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1500 $this->assertEquals('2009-01-21', $result['join_date']);
1501 $this->assertEquals('2008-03-01', $result['start_date']);
1502 $this->assertEquals('2010-02-28', $result['end_date']);
1503 }
1504
1505 /**
1506 * Test that if membership end date is not set it defaults to correct end date.
1507 * - rolling
1508 */
1509 public function testEmptyEndDateRolling() {
1510 unset($this->_params['is_override'], $this->_params['end_date']);
1511 $this->_params['membership_type_id'] = $this->_membershipTypeID;
1512 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1513 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1514 $this->assertEquals('2009-01-21', $result['join_date']);
1515 $this->assertEquals('2009-01-21', $result['start_date']);
1516 $this->assertEquals('2010-01-20', $result['end_date']);
1517 }
1518
1519
1520 /**
1521 * Test that if dates are set they not over-ridden if id is passed in
1522 */
1523 public function testMembershipDatesNotOverridden() {
1524 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1525 unset($this->_params['end_date'], $this->_params['start_date']);
1526 $this->_params['id'] = $result['id'];
1527 $this->callAPISuccess($this->_entity, 'create', $this->_params);
1528 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1529 $this->assertEquals('2009-01-21', $result['join_date']);
1530 $this->assertEquals('2009-01-21', $result['start_date']);
1531 $this->assertEquals('2009-12-21', $result['end_date']);
1532
1533 }
1534
1535 /**
1536 * Test that all membership types are returned when getoptions is called.
1537 *
1538 * This test locks in current behaviour where types for all domains are returned. It should possibly be domain
1539 * specific but that should only be done in conjunction with adding a hook to allow that to be altered as the
1540 * multisite use case expects the master domain to be able to see all sites.
1541 *
1542 * See CRM-17075.
1543 */
1544 public function testGetOptionsMembershipTypeID() {
1545 $options = $this->callAPISuccess('Membership', 'getoptions', array('field' => 'membership_type_id'));
1546 $this->assertEquals('Another one', array_pop($options['values']));
1547 $this->assertEquals('General', array_pop($options['values']));
1548 $this->assertEquals(NULL, array_pop($options['values']));
1549 }
1550
1551 }