Merge pull request #5216 from atif-shaikh/CRM-15792
[civicrm-core.git] / tests / phpunit / api / v3 / MembershipTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Test APIv3 civicrm_membership functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Member
33 */
34
35
36 require_once 'CiviTest/CiviUnitTestCase.php';
37
38 /**
39 * Class api_v3_MembershipTest
40 */
41 class api_v3_MembershipTest extends CiviUnitTestCase {
42 protected $_apiversion;
43 protected $_contactID;
44 protected $_membershipID;
45 protected $_membershipID2;
46 protected $_membershipID3;
47 protected $_membershipTypeID;
48 protected $_membershipTypeID2;
49 protected $_membershipStatusID;
50 protected $_entity;
51 protected $_params;
52
53
54 public function setUp() {
55 // Connect to the database.
56 parent::setUp();
57 $this->_apiversion = 3;
58 $this->_contactID = $this->individualCreate();
59 $this->_membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID));
60 $this->_membershipTypeID2 = $this->membershipTypeCreate(array(
61 'period_type' => 'fixed',
62 // 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 // Tear down - reverse of creation to be safe
557 $this->contactDelete($memberContactId[2]);
558 $this->contactDelete($memberContactId[1]);
559 $this->contactDelete($memberContactId[0]);
560 $this->contactDelete($employerId[1]);
561 $this->contactDelete($employerId[0]);
562 $this->membershipTypeDelete(array('id' => $membershipTypeId));
563 $this->contactDelete($membershipOrgId);
564 }
565
566 /**
567 * We are checking for no enotices + only id & end_date returned
568 */
569 public function testMembershipGetWithReturn() {
570 $this->contactMembershipCreate($this->_params);
571 $result = $this->callAPISuccess('membership', 'get', array('return' => 'end_date'));
572 foreach ($result['values'] as $membership) {
573 $this->assertEquals(array('id', 'end_date'), array_keys($membership));
574 }
575 }
576 ///////////////// civicrm_membership_create methods
577
578 /**
579 * Test civicrm_contact_memberships_create with empty params.
580 * Error expected.
581 */
582 public function testCreateWithEmptyParams() {
583 $params = array();
584 $this->callAPIFailure('membership', 'create', $params);
585 }
586
587 /**
588 * If is_overide is passed in status must also be passed in.
589 */
590 public function testCreateOverrideNoStatus() {
591 $params = $this->_params;
592 unset($params['status_id']);
593 $this->callAPIFailure('membership', 'create', $params);
594 }
595
596 public function testMembershipCreateMissingRequired() {
597 $params = array(
598 'membership_type_id' => '1',
599 'join_date' => '2006-01-21',
600 'start_date' => '2006-01-21',
601 'end_date' => '2006-12-21',
602 'source' => 'Payment',
603 'status_id' => '2',
604 );
605
606 $this->callAPIFailure('membership', 'create', $params);
607 }
608
609 public function testMembershipCreate() {
610 $params = array(
611 'contact_id' => $this->_contactID,
612 'membership_type_id' => $this->_membershipTypeID,
613 'join_date' => '2006-01-21',
614 'start_date' => '2006-01-21',
615 'end_date' => '2006-12-21',
616 'source' => 'Payment',
617 'is_override' => 1,
618 'status_id' => $this->_membershipStatusID,
619 );
620
621 $result = $this->callAPIAndDocument('membership', 'create', $params, __FUNCTION__, __FILE__);
622 $this->getAndCheck($params, $result['id'], $this->_entity);
623 $this->assertNotNull($result['id']);
624 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['contact_id'], " in line " . __LINE__);
625 $this->assertEquals($result['id'], $result['values'][$result['id']]['id'], " in line " . __LINE__);
626 }
627
628 /**
629 * Check for useful message if contact doesn't exist
630 */
631 public function testMembershipCreateWithInvalidContact() {
632 $params = array(
633 'contact_id' => 999,
634 'membership_type_id' => $this->_membershipTypeID,
635 'join_date' => '2006-01-21',
636 'start_date' => '2006-01-21',
637 'end_date' => '2006-12-21',
638 'source' => 'Payment',
639 'is_override' => 1,
640 'status_id' => $this->_membershipStatusID,
641 );
642
643 $this->callAPIFailure('membership', 'create', $params,
644 'contact_id is not valid : 999'
645 );
646 }
647
648 public function testMembershipCreateWithInvalidStatus() {
649 $params = $this->_params;
650 $params['status_id'] = 999;
651 $this->callAPIFailure('membership', 'create', $params,
652 "'999' is not a valid option for field status_id"
653 );
654 }
655
656 public function testMembershipCreateWithInvalidType() {
657 $params = $this->_params;
658 $params['membership_type_id'] = 999;
659
660 $this->callAPIFailure('membership', 'create', $params,
661 "'999' is not a valid option for field membership_type_id"
662 );
663 }
664
665 /**
666 * Check with complete array + custom field
667 * Note that the test is written on purpose without any
668 * variables specific to participant so it can be replicated into other entities
669 * and / or moved to the automated test suite
670 */
671 public function testCreateWithCustom() {
672 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
673
674 $params = $this->_params;
675 $params['custom_' . $ids['custom_field_id']] = "custom string";
676
677 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, NULL, 'CreateWithCustomData');
678 $check = $this->callAPISuccess($this->_entity, 'get', array(
679 'id' => $result['id'],
680 'contact_id' => $this->_contactID,
681 ));
682 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
683 }
684
685 /**
686 * Test civicrm_contact_memberships_create with membership id (edit
687 * membership).
688 * success expected.
689 */
690 public function testMembershipCreateWithId() {
691 $membershipID = $this->contactMembershipCreate($this->_params);
692 $params = array(
693 'id' => $membershipID,
694 'contact_id' => $this->_contactID,
695 'membership_type_id' => $this->_membershipTypeID,
696 'join_date' => '2006-01-21',
697 'start_date' => '2006-01-21',
698 'end_date' => '2006-12-21',
699 'source' => 'Payment',
700 'is_override' => 1,
701 'status_id' => $this->_membershipStatusID,
702 );
703
704 $result = $this->callAPISuccess('membership', 'create', $params);
705 $this->callAPISuccess('Membership', 'Delete', array(
706 'id' => $result['id'],
707 ));
708 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
709 }
710
711 /**
712 * Test civicrm_contact_memberships_create with membership id (edit
713 * membership).
714 * success expected.
715 */
716 public function testMembershipCreateUpdateWithIdNoContact() {
717 $membershipID = $this->contactMembershipCreate($this->_params);
718 $params = array(
719 'id' => $membershipID,
720 'membership_type_id' => $this->_membershipTypeID,
721 'contact_id' => $this->_contactID,
722 'join_date' => '2006-01-21',
723 'start_date' => '2006-01-21',
724 'end_date' => '2006-12-21',
725 'source' => 'Payment',
726 'is_override' => 1,
727 'status_id' => $this->_membershipStatusID,
728 );
729
730 $result = $this->callAPISuccess('membership', 'create', $params);
731 $this->callAPISuccess('Membership', 'Delete', array(
732 'id' => $result['id'],
733 ));
734
735 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
736 }
737
738 /**
739 * Test civicrm_contact_memberships_create with membership id (edit
740 * membership).
741 * success expected.
742 */
743 public function testMembershipCreateUpdateWithIdNoDates() {
744 $membershipID = $this->contactMembershipCreate($this->_params);
745 $params = array(
746 'id' => $membershipID,
747 'contact_id' => $this->_contactID,
748 'membership_type_id' => $this->_membershipTypeID,
749 'source' => 'Payment',
750 'is_override' => 1,
751 'status_id' => $this->_membershipStatusID,
752 );
753
754 $result = $this->callAPISuccess('membership', 'create', $params);
755 $this->callAPISuccess('Membership', 'Delete', array(
756 'id' => $result['id'],
757 ));
758 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
759 }
760
761 /**
762 * Test civicrm_contact_memberships_create with membership id (edit
763 * membership).
764 * success expected.
765 */
766 public function testMembershipCreateUpdateWithIdNoDatesNoType() {
767 $membershipID = $this->contactMembershipCreate($this->_params);
768 $params = array(
769 'id' => $membershipID,
770 'source' => 'not much here',
771 'contact_id' => $this->_contactID,
772 'is_override' => 1,
773 'status_id' => $this->_membershipStatusID,
774 );
775
776 $result = $this->callAPISuccess('membership', 'create', $params);
777 $this->callAPISuccess('Membership', 'Delete', array(
778 'id' => $result['id'],
779 ));
780 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
781 }
782
783 /**
784 * Test civicrm_contact_memberships_create with membership id (edit
785 * membership).
786 * success expected.
787 */
788 public function testMembershipCreateUpdateWithIDAndSource() {
789 $membershipID = $this->contactMembershipCreate($this->_params);
790 $params = array(
791 'id' => $membershipID,
792 'source' => 'changed',
793 'contact_id' => $this->_contactID,
794 'status_id' => $this->_membershipStatusID,
795 'membership_type_id' => $this->_membershipTypeID,
796 'skipStatusCal' => 1,
797 );
798 $result = $this->callAPISuccess('membership', 'create', $params);
799 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
800 $this->callAPISuccess('Membership', 'Delete', array(
801 'id' => $result['id'],
802 ));
803 }
804
805 /**
806 * Change custom field using update.
807 */
808 public function testUpdateWithCustom() {
809 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
810
811 $params = $this->_params;
812 $params['custom_' . $ids['custom_field_id']] = "custom string";
813 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, NULL, 'UpdateCustomData');
814 $result = $this->callAPISuccess($this->_entity, 'create', array(
815 'id' => $result['id'],
816 'custom_' . $ids['custom_field_id'] => "new custom",
817 ));
818 $check = $this->callAPISuccess($this->_entity, 'get', array(
819 'id' => $result['id'],
820 'contact_id' => $this->_contactID,
821 ));
822
823 $this->assertEquals("new custom", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
824 $this->callAPISuccess('Membership', 'Delete', array(
825 'id' => $check['id'],
826 ));
827
828 $this->customFieldDelete($ids['custom_field_id']);
829 $this->customGroupDelete($ids['custom_group_id']);
830 }
831
832 /**
833 * per CRM-15746 check that the id can be altered in an update hook
834 */
835 public function testMembershipUpdateCreateHookCRM15746() {
836 $this->hookClass->setHook('civicrm_pre', array($this, 'hook_civicrm_pre_update_create_membership'));
837 $result = $this->callAPISuccess('membership', 'create', $this->_params);
838 $this->callAPISuccess('membership', 'create', array('id' => $result['id'], 'end_date' => '1 year ago'));
839 $this->callAPISuccessGetCount('membership', array(), 2);
840 $this->hookClass->reset();
841 $this->callAPISuccess('membership', 'create', array('id' => $result['id'], 'end_date' => '1 year ago'));
842 $this->callAPISuccessGetCount('membership', array(), 2);
843 }
844
845 /**
846 * Custom hook for update membership.
847 *
848 * @param string $op
849 * @param object $objectName
850 * @param int $id
851 * @param array $params
852 *
853 * @throws \Exception
854 */
855 public function hook_civicrm_pre_update_create_membership($op, $objectName, $id, &$params) {
856 if ($objectName == 'Membership' && $op == 'edit') {
857 $existingMembership = $this->callAPISuccessGetSingle('membership', array('id' => $params['id']));
858 unset($params['id'], $params['membership_id']);
859 $params['join_date'] = $params['membership_start_date'] = $params['start_date'] = date('Ymd000000', strtotime($existingMembership['start_date']));
860 $params = array_merge($existingMembership, $params);
861 $params['id'] = NULL;
862 }
863 }
864
865 /**
866 * Test civicrm_contact_memberships_create Invalid membership data.
867 * Error expected.
868 */
869 public function testMembershipCreateInvalidMemData() {
870 //membership_contact_id as string
871 $params = array(
872 'membership_contact_id' => 'Invalid',
873 'membership_type_id' => $this->_membershipTypeID,
874 'join_date' => '2011-01-21',
875 'start_date' => '2010-01-21',
876 'end_date' => '2008-12-21',
877 'source' => 'Payment',
878 'is_override' => 1,
879 'status_id' => $this->_membershipStatusID,
880 );
881
882 $this->callAPIFailure('membership', 'create', $params);
883
884 //membership_contact_id which is no in contact table
885 $params['membership_contact_id'] = 999;
886 $this->callAPIFailure('membership', 'create', $params);
887
888 //invalid join date
889 unset($params['membership_contact_id']);
890 $params['join_date'] = "invalid";
891 $this->callAPIFailure('Membership', 'Create', $params);
892 }
893
894 /**
895 * Test civicrm_contact_memberships_create with membership_contact_id
896 * membership).
897 * Success expected.
898 */
899 public function testMembershipCreateWithMemContact() {
900 $params = array(
901 'membership_contact_id' => $this->_contactID,
902 'membership_type_id' => $this->_membershipTypeID,
903 'join_date' => '2011-01-21',
904 'start_date' => '2010-01-21',
905 'end_date' => '2008-12-21',
906 'source' => 'Payment',
907 'is_override' => 1,
908 'status_id' => $this->_membershipStatusID,
909 );
910
911 $result = $this->callAPISuccess('membership', 'create', $params);
912
913 $this->callAPISuccess('Membership', 'Delete', array(
914 'id' => $result['id'],
915 ));
916 }
917
918 /**
919 * Test civicrm_contact_memberships_create with membership_contact_id
920 * membership).
921 * Success expected.
922 */
923 public function testMembershipCreateValidMembershipTypeString() {
924 $params = array(
925 'membership_contact_id' => $this->_contactID,
926 'membership_type_id' => 'General',
927 'join_date' => '2011-01-21',
928 'start_date' => '2010-01-21',
929 'end_date' => '2008-12-21',
930 'source' => 'Payment',
931 'is_override' => 1,
932 'status_id' => $this->_membershipStatusID,
933 );
934
935 $result = $this->callAPISuccess('membership', 'create', $params);
936 $this->assertEquals($this->_membershipTypeID, $result['values'][$result['id']]['membership_type_id']);
937 $this->callAPISuccess('Membership', 'Delete', array(
938 'id' => $result['id'],
939 ));
940 }
941
942 /**
943 * Test civicrm_contact_memberships_create with membership_contact_id
944 * membership).
945 * Success expected.
946 */
947 public function testMembershipCreateInValidMembershipTypeString() {
948 $params = array(
949 'membership_contact_id' => $this->_contactID,
950 'membership_type_id' => 'invalid',
951 'join_date' => '2011-01-21',
952 'start_date' => '2010-01-21',
953 'end_date' => '2008-12-21',
954 'source' => 'Payment',
955 'is_override' => 1,
956 'status_id' => $this->_membershipStatusID,
957 );
958
959 $this->callAPIFailure('membership', 'create', $params);
960 }
961
962 /**
963 * Test that if membership join date is not set it defaults to today.
964 */
965 public function testEmptyJoinDate() {
966 unset($this->_params['join_date'], $this->_params['is_override']);
967 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
968 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
969 $this->assertEquals(date('Y-m-d', strtotime('now')), $result['join_date']);
970 $this->assertEquals('2009-01-21', $result['start_date']);
971 $this->assertEquals('2009-12-21', $result['end_date']);
972 }
973
974 /**
975 * Test that if membership start date is not set it defaults to correct end date.
976 * - fixed
977 */
978 public function testEmptyStartDateFixed() {
979 unset($this->_params['start_date'], $this->_params['is_override']);
980 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
981 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
982 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
983 $this->assertEquals('2009-01-21', $result['join_date']);
984 $this->assertEquals('2008-03-01', $result['start_date']);
985 $this->assertEquals('2009-12-21', $result['end_date']);
986 }
987
988 /**
989 * Test that if membership start date is not set it defaults to correct end date
990 * - fixed
991 */
992 public function testEmptyStartEndDateFixedOneYear() {
993 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
994 $this->callAPISuccess('membership_type', 'create', array('id' => $this->_membershipTypeID2, 'duration_interval' => 1));
995 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
996 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
997 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
998 $this->assertEquals('2009-01-21', $result['join_date']);
999 $this->assertEquals('2008-03-01', $result['start_date']);
1000 $this->assertEquals('2010-02-28', $result['end_date']);
1001 }
1002
1003 /**
1004 * Test that if membership start date is not set it defaults to correct end date for fixed multi year memberships.
1005 */
1006 public function testEmptyStartEndDateFixedMultiYear() {
1007 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1008 $this->callAPISuccess('membership_type', 'create', array('id' => $this->_membershipTypeID2, 'duration_interval' => 5));
1009 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1010 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1011 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1012 $this->assertEquals('2009-01-21', $result['join_date']);
1013 $this->assertEquals('2008-03-01', $result['start_date']);
1014 $this->assertEquals('2014-02-28', $result['end_date']);
1015 }
1016
1017 /**
1018 * Test correct end and start dates are calculated for fixed multi year memberships.
1019 *
1020 * The empty start date is calculated to be the start_date (1 Jan prior to the join_date - so 1 Jan 15)
1021 *
1022 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1023 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1024 * and we add on 4 years rather than 5 because we are not after the rollover day - so we calculate 31 Dec 2019
1025 */
1026 public function testFixedMultiYearDateSetTwoEmptyStartEndDate() {
1027 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1028
1029 $this->callAPISuccess('membership_type', 'create', array(
1030 'id' => $this->_membershipTypeID2,
1031 'duration_interval' => 5,
1032 // Ie 1 Jan.
1033 'fixed_period_start_day' => '101',
1034 // Ie. 1 Nov.
1035 'fixed_period_rollover_day' => '1101',
1036 ));
1037 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1038 $dates = array(
1039 'join_date' => '28-Jan 2015',
1040 );
1041 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1042 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1043 $this->assertEquals('2015-01-28', $result['join_date']);
1044 $this->assertEquals('2015-01-01', $result['start_date']);
1045 $this->assertEquals('2019-12-31', $result['end_date']);
1046 }
1047
1048 /**
1049 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
1050 *
1051 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1052 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1053 * and we add on 4 years rather than 5 because we are not after the rollover day - so we calculate 31 Dec 2019
1054 */
1055 public function testFixedMultiYearDateSetTwoEmptyEndDate() {
1056 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1057
1058 $this->callAPISuccess('membership_type', 'create', array(
1059 'id' => $this->_membershipTypeID2,
1060 'duration_interval' => 5,
1061 // Ie 1 Jan.
1062 'fixed_period_start_day' => '101',
1063 // Ie. 1 Nov.
1064 'fixed_period_rollover_day' => '1101',
1065 ));
1066 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1067 $dates = array(
1068 'start_date' => '28-Jan 2015',
1069 'join_date' => '28-Jan 2015',
1070 );
1071 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1072 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1073 $this->assertEquals('2015-01-28', $result['join_date']);
1074 $this->assertEquals('2015-01-28', $result['start_date']);
1075 $this->assertEquals('2019-12-31', $result['end_date']);
1076 }
1077
1078 /**
1079 * Test correct end and start dates are calculated for fixed multi year memberships.
1080 *
1081 * The empty start date is calculated to be the start_date (1 Jan prior to the join_date - so 1 Jan 15)
1082 *
1083 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1084 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1085 * and we add on <1 years rather than > 1 because we are not after the rollover day - so we calculate 31 Dec 2015
1086 */
1087 public function testFixedSingleYearDateSetTwoEmptyStartEndDate() {
1088 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1089
1090 $this->callAPISuccess('membership_type', 'create', array(
1091 'id' => $this->_membershipTypeID2,
1092 'duration_interval' => 1,
1093 // Ie 1 Jan.
1094 'fixed_period_start_day' => '101',
1095 // Ie. 1 Nov.
1096 'fixed_period_rollover_day' => '1101',
1097 ));
1098 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1099 $dates = array(
1100 'join_date' => '28-Jan 2015',
1101 );
1102 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1103 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1104 $this->assertEquals('2015-01-28', $result['join_date']);
1105 $this->assertEquals('2015-01-01', $result['start_date']);
1106 $this->assertEquals('2015-12-31', $result['end_date']);
1107 }
1108
1109 /**
1110 * Test correct end date for fixed single year memberships is calculated and start_date is not changed.
1111 *
1112 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1113 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1114 * and we add on <1 years rather than > 1 because we are not after the rollover day - so we calculate 31 Dec 2015
1115 */
1116 public function testFixedSingleYearDateSetTwoEmptyEndDate() {
1117 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1118
1119 $this->callAPISuccess('membership_type', 'create', array(
1120 'id' => $this->_membershipTypeID2,
1121 'duration_interval' => 1,
1122 // Ie 1 Jan.
1123 'fixed_period_start_day' => '101',
1124 // Ie. 1 Nov.
1125 'fixed_period_rollover_day' => '1101',
1126 ));
1127 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1128 $dates = array(
1129 'start_date' => '28-Jan 2015',
1130 'join_date' => '28-Jan 2015',
1131 );
1132 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1133 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1134 $this->assertEquals('2015-01-28', $result['join_date']);
1135 $this->assertEquals('2015-01-28', $result['start_date']);
1136 $this->assertEquals('2015-12-31', $result['end_date']);
1137 }
1138
1139 /**
1140 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
1141 *
1142 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1143 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1144 * and we add on 1 year we are after the rollover day - so we calculate 31 Oct 2016
1145 */
1146 public function testFixedSingleYearDateSetThreeEmptyEndDate() {
1147 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1148
1149 $this->callAPISuccess('membership_type', 'create', array(
1150 'id' => $this->_membershipTypeID2,
1151 'duration_interval' => 1,
1152 // Ie. 1 Nov.
1153 'fixed_period_start_day' => '1101',
1154 // Ie 1 Jan.
1155 'fixed_period_rollover_day' => '101',
1156 ));
1157 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1158 $dates = array(
1159 'start_date' => '28-Jan 2015',
1160 'join_date' => '28-Jan 2015',
1161 );
1162 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1163 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1164 $this->assertEquals('2015-01-28', $result['join_date']);
1165 $this->assertEquals('2015-01-28', $result['start_date']);
1166 $this->assertEquals('2016-10-31', $result['end_date']);
1167 }
1168
1169 /**
1170 * Test correct end and start dates are calculated for fixed multi year memberships.
1171 *
1172 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
1173 *
1174 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1175 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1176 * and we add on 1 year we are after the rollover day - so we calculate 31 Oct 2016
1177 */
1178 public function testFixedSingleYearDateSetThreeEmptyStartEndDate() {
1179 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1180
1181 $this->callAPISuccess('membership_type', 'create', array(
1182 'id' => $this->_membershipTypeID2,
1183 'duration_interval' => 1,
1184 // Ie. 1 Nov.
1185 'fixed_period_start_day' => '1101',
1186 // Ie 1 Jan.
1187 'fixed_period_rollover_day' => '101',
1188 ));
1189 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1190 $dates = array(
1191 'join_date' => '28-Jan 2015',
1192 );
1193 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1194 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1195 $this->assertEquals('2015-01-28', $result['join_date']);
1196 $this->assertEquals('2014-11-01', $result['start_date']);
1197 $this->assertEquals('2016-10-31', $result['end_date']);
1198 }
1199
1200 /**
1201 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
1202 *
1203 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1204 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1205 * and we add on 5 years we are after the rollover day - so we calculate 31 Oct 2020
1206 */
1207 public function testFixedMultiYearDateSetThreeEmptyEndDate() {
1208 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1209
1210 $this->callAPISuccess('membership_type', 'create', array(
1211 'id' => $this->_membershipTypeID2,
1212 'duration_interval' => 5,
1213 // Ie. 1 Nov.
1214 'fixed_period_start_day' => '1101',
1215 // Ie 1 Jan.
1216 'fixed_period_rollover_day' => '101',
1217 ));
1218 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1219 $dates = array(
1220 'start_date' => '28-Jan 2015',
1221 'join_date' => '28-Jan 2015',
1222 );
1223 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1224 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1225 $this->assertEquals('2015-01-28', $result['join_date']);
1226 $this->assertEquals('2015-01-28', $result['start_date']);
1227 $this->assertEquals('2020-10-31', $result['end_date']);
1228 }
1229
1230 /**
1231 * Test correct end and start dates are calculated for fixed multi year memberships.
1232 *
1233 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
1234 *
1235 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
1236 * In this set our join date is after the start day and after the rollover day so we do get an extra year
1237 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1238 * and we add on 5 years we are after the rollover day - so we calculate 31 Oct 2020
1239 */
1240 public function testFixedMultiYearDateSetThreeEmptyStartEndDate() {
1241 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1242
1243 $this->callAPISuccess('membership_type', 'create', array(
1244 'id' => $this->_membershipTypeID2,
1245 'duration_interval' => 5,
1246 // Ie. 1 Nov.
1247 'fixed_period_start_day' => '1101',
1248 // Ie 1 Jan.
1249 'fixed_period_rollover_day' => '101',
1250 ));
1251 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1252 $dates = array(
1253 'join_date' => '28-Jan 2015',
1254 );
1255 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1256 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1257 $this->assertEquals('2015-01-28', $result['join_date']);
1258 $this->assertEquals('2014-11-01', $result['start_date']);
1259 $this->assertEquals('2020-10-31', $result['end_date']);
1260 }
1261
1262 /**
1263 * Test that if membership start date is not set it defaults to correct end date for fixed single year memberships.
1264 */
1265 public function testEmptyStartDateRolling() {
1266 unset($this->_params['start_date'], $this->_params['is_override']);
1267 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1268 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1269 $this->assertEquals('2009-01-21', $result['join_date']);
1270 $this->assertEquals('2009-01-21', $result['start_date']);
1271 $this->assertEquals('2009-12-21', $result['end_date']);
1272 }
1273
1274 /**
1275 * Test that if membership end date is not set it defaults to correct end date.
1276 * - rolling
1277 */
1278 public function testEmptyEndDateFixed() {
1279 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1280 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1281 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1282 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1283 $this->assertEquals('2009-01-21', $result['join_date']);
1284 $this->assertEquals('2008-03-01', $result['start_date']);
1285 $this->assertEquals('2010-02-28', $result['end_date']);
1286 }
1287
1288 /**
1289 * Test that if membership end date is not set it defaults to correct end date.
1290 * - rolling
1291 */
1292 public function testEmptyEndDateRolling() {
1293 unset($this->_params['is_override'], $this->_params['end_date']);
1294 $this->_params['membership_type_id'] = $this->_membershipTypeID;
1295 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1296 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1297 $this->assertEquals('2009-01-21', $result['join_date']);
1298 $this->assertEquals('2009-01-21', $result['start_date']);
1299 $this->assertEquals('2010-01-20', $result['end_date']);
1300 }
1301
1302
1303 /**
1304 * Test that if dates are set they not over-ridden if id is passed in
1305 */
1306 public function testMembershipDatesNotOverridden() {
1307 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1308 unset($this->_params['end_date'], $this->_params['start_date']);
1309 $this->_params['id'] = $result['id'];
1310 $this->callAPISuccess($this->_entity, 'create', $this->_params);
1311 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1312 $this->assertEquals('2009-01-21', $result['join_date']);
1313 $this->assertEquals('2009-01-21', $result['start_date']);
1314 $this->assertEquals('2009-12-21', $result['end_date']);
1315
1316 }
1317
1318 }