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