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