Merge pull request #6289 from yashodha/CRM-16879
[civicrm-core.git] / tests / phpunit / api / v3 / MembershipTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 * Test APIv3 civicrm_membership functions
30 *
6c6e6187
TO
31 * @package CiviCRM_APIv3
32 * @subpackage API_Member
6a488035
TO
33 */
34
35
36require_once 'CiviTest/CiviUnitTestCase.php';
37
4cbe18b8
EM
38/**
39 * Class api_v3_MembershipTest
40 */
6a488035
TO
41class api_v3_MembershipTest extends CiviUnitTestCase {
42 protected $_apiversion;
43 protected $_contactID;
d54576ed
EM
44 protected $_membershipID;
45 protected $_membershipID2;
46 protected $_membershipID3;
6a488035 47 protected $_membershipTypeID;
8c33a68c 48 protected $_membershipTypeID2;
6a488035 49 protected $_membershipStatusID;
6a488035
TO
50 protected $_entity;
51 protected $_params;
b7c9bc4c 52
6a488035
TO
53
54 public function setUp() {
2ea0abec 55 // Connect to the database.
6a488035
TO
56 parent::setUp();
57 $this->_apiversion = 3;
58 $this->_contactID = $this->individualCreate();
75638074 59 $this->_membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID));
5896d037
TO
60 $this->_membershipTypeID2 = $this->membershipTypeCreate(array(
61 'period_type' => 'fixed',
2ea0abec 62 // Ie. 1 March.
5896d037 63 'fixed_period_start_day' => '301',
2ea0abec 64 // Ie. 11 Nov.
21dfd5f5 65 'fixed_period_rollover_day' => '1111',
5896d037 66 ));
6a488035
TO
67 $this->_membershipStatusID = $this->membershipStatusCreate('test status');
68
6a488035
TO
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,
6a488035
TO
83 );
84 }
85
00be9182 86 public function tearDown() {
6a488035 87 $this->quickCleanup(array(
5896d037
TO
88 'civicrm_membership',
89 'civicrm_membership_payment',
90 'civicrm_membership_log',
771f3245 91 ),
92 TRUE
6a488035
TO
93 );
94 $this->membershipStatusDelete($this->_membershipStatusID);
8c33a68c 95 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID2));
6a488035
TO
96 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID));
97 $this->contactDelete($this->_contactID);
98
99 }
100
101 /**
2ea0abec 102 * Test membership deletion.
6a488035 103 */
00be9182 104 public function testMembershipDelete() {
6a488035 105 $membershipID = $this->contactMembershipCreate($this->_params);
3506b6cd 106 $this->assertDBRowExist('CRM_Member_DAO_Membership', $membershipID);
6a488035 107 $params = array(
21dfd5f5 108 'id' => $membershipID,
6a488035 109 );
d54576ed 110 $this->callAPIAndDocument('membership', 'delete', $params, __FUNCTION__, __FILE__);
3506b6cd 111 $this->assertDBRowNotExist('CRM_Member_DAO_Membership', $membershipID);
6a488035
TO
112 }
113
00be9182 114 public function testMembershipDeleteEmpty() {
d54576ed 115 $this->callAPIFailure('membership', 'delete', array());
6a488035
TO
116 }
117
00be9182 118 public function testMembershipDeleteInvalidID() {
d54576ed 119 $this->callAPIFailure('membership', 'delete', array('id' => 'blah'));
6a488035
TO
120 }
121
122 /**
2ea0abec 123 * Test civicrm_membership_delete() with invalid Membership Id.
6a488035 124 */
00be9182 125 public function testMembershipDeleteWithInvalidMembershipId() {
6a488035 126 $membershipId = 'membership';
d54576ed 127 $this->callAPIFailure('membership', 'delete', $membershipId);
6a488035
TO
128 }
129
130 /**
2ea0abec 131 * Test membership get.
6a488035 132 */
00be9182 133 public function testContactMembershipsGet() {
6a488035 134 $this->_membershipID = $this->contactMembershipCreate($this->_params);
2ea0abec 135 $this->callAPISuccess('membership', 'get', array());
6c6e6187 136 $this->callAPISuccess('Membership', 'Delete', array('id' => $this->_membershipID));
6a488035
TO
137 }
138
139 /**
140 * Test civicrm_membership_get with params not array.
2ea0abec 141 *
6a488035
TO
142 * Gets treated as contact_id, memberships expected.
143 */
00be9182 144 public function testGetWithParamsContactId() {
6a488035
TO
145 $this->_membershipID = $this->contactMembershipCreate($this->_params);
146 $params = array(
147 'contact_id' => $this->_contactID,
6a488035 148 );
771f3245 149 $membership = $this->callAPISuccess('membership', 'get', $params);
6a488035
TO
150
151 $result = $membership['values'][$this->_membershipID];
771f3245 152 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 153 'id' => $this->_membershipID,
5896d037 154 ));
6a488035
TO
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
b4529041 165 /**
166 * Test civicrm_membership_get with params not array.
2ea0abec 167 *
b4529041 168 * Gets treated as contact_id, memberships expected.
169 */
00be9182 170 public function testGetInSyntax() {
b4529041 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']));
b4529041 186 }
187
caca32ba 188 /**
189 * Test civicrm_membership_get with params not array.
190 * Gets treated as contact_id, memberships expected.
191 */
00be9182 192 public function testGetInSyntaxOnContactID() {
caca32ba 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 }
5896d037 211
caca32ba 212 /**
213 * Test civicrm_membership_get with params not array.
214 * Gets treated as contact_id, memberships expected.
215 */
00be9182 216 public function testGetWithParamsMemberShipTypeId() {
d54576ed 217 $this->callAPISuccess($this->_entity, 'create', $this->_params);
6a488035
TO
218 $params = array(
219 'membership_type_id' => $this->_membershipTypeID,
6a488035 220 );
771f3245 221 $membership = $this->callAPISuccess('membership', 'get', $params);
d54576ed 222 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 223 'id' => $membership['id'],
771f3245 224 ));
6a488035
TO
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 }
5896d037 236
a73daeff
E
237 /**
238 * Test civicrm_membership_get with params not array.
239 * Gets treated as contact_id, memberships expected.
240 */
00be9182 241 public function testGetWithParamsMemberShipTypeIdContactID() {
a73daeff
E
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);
6a488035 254
a73daeff
E
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 }
5896d037 263
6a488035 264 /**
100fef9d 265 * Check with complete array + custom field
6a488035
TO
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 */
00be9182 270 public function testGetWithParamsMemberShipIdAndCustom() {
6a488035
TO
271 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
272
273 $params = $this->_params;
274 $params['custom_' . $ids['custom_field_id']] = "custom string";
275
771f3245 276 $result = $this->callAPISuccess($this->_entity, 'create', $params);
6a488035 277
771f3245 278 $getParams = array('membership_type_id' => $params['membership_type_id']);
279 $check = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
6a488035
TO
280 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
281
d54576ed 282 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 283 'id' => $result['id'],
771f3245 284 ));
6a488035
TO
285 }
286
287 /**
288 * Test civicrm_membership_get with proper params.
289 * Memberships expected.
290 */
00be9182 291 public function testGet() {
6a488035
TO
292 $membershipID = $this->contactMembershipCreate($this->_params);
293 $params = array(
294 'contact_id' => $this->_contactID,
6a488035
TO
295 );
296
771f3245 297 $membership = $this->callAPISuccess('membership', 'get', $params);
6a488035 298 $result = $membership['values'][$membershipID];
771f3245 299 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 300 'id' => $membership['id'],
771f3245 301 ));
6a488035
TO
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 */
00be9182 318 public function testGetWithId() {
6a488035
TO
319 $membershipID = $this->contactMembershipCreate($this->_params);
320 $params = array(
321 'contact_id' => $this->_contactID,
d54576ed 322 'id' => $this->_membershipID,
6a488035
TO
323 'return' => 'id',
324 );
771f3245 325 $result = $this->callAPISuccess('membership', 'get', $params);
6a488035
TO
326 $this->assertEquals($membershipID, $result['id']);
327 $params = array(
328 'contact_id' => $this->_contactID,
d54576ed 329 'membership_id' => $this->_membershipID,
6a488035
TO
330 'return' => 'membership_id',
331 );
771f3245 332 $result = $this->callAPISuccess('membership', 'get', $params);
6a488035 333 $this->assertEquals($membershipID, $result['id']);
6a488035
TO
334 }
335
336 /**
337 * Test civicrm_membership_get for only active.
338 * Memberships expected.
339 */
00be9182 340 public function testGetOnlyActive() {
5c49fee0 341 $description = "Demonstrates use of 'filter' active_only' param.";
6a488035 342 $this->_membershipID = $this->contactMembershipCreate($this->_params);
5896d037 343 $params = array(
6a488035
TO
344 'contact_id' => $this->_contactID,
345 'active_only' => 1,
6a488035
TO
346 );
347
771f3245 348 $membership = $this->callAPISuccess('membership', 'get', $params);
a73daeff
E
349 $this->assertEquals($membership['values'][$this->_membershipID]['status_id'], $this->_membershipStatusID);
350 $this->assertEquals($membership['values'][$this->_membershipID]['contact_id'], $this->_contactID);
6a488035
TO
351 $params = array(
352 'contact_id' => $this->_contactID,
353 'filters' => array(
354 'is_current' => 1,
355 ),
6a488035
TO
356 );
357
a828d7b8 358 $membership = $this->callAPIAndDocument('membership', 'get', $params, __FUNCTION__, __FILE__, $description, 'FilterIsCurrent');
a73daeff
E
359 $this->assertEquals($membership['values'][$this->_membershipID]['status_id'], $this->_membershipStatusID);
360 $this->assertEquals($membership['values'][$this->_membershipID]['contact_id'], $this->_contactID);
6a488035 361
6c6e6187 362 $this->callAPISuccess('Membership', 'Delete', array('id' => $this->_membershipID));
6a488035
TO
363 }
364
365 /**
366 * Test civicrm_membership_get for non exist contact.
367 * empty Memberships.
368 */
00be9182 369 public function testGetNoContactExists() {
6a488035
TO
370 $params = array(
371 'contact_id' => 55555,
6a488035
TO
372 );
373
771f3245 374 $membership = $this->callAPISuccess('membership', 'get', $params);
6a488035
TO
375 $this->assertEquals($membership['count'], 0, "In line " . __LINE__);
376 }
377
378 /**
379 * Test civicrm_membership_get with relationship.
380 * get Memberships.
381 */
00be9182 382 public function testGetWithRelationship() {
6a488035 383 $membershipOrgId = $this->organizationCreate(NULL);
e4d5f1e2 384 $memberContactId = $this->individualCreate();
6a488035
TO
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,
6a488035
TO
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,
5896d037 404 'financial_type_id' => 1,
6a488035
TO
405 'relationship_type_id' => $relTypeID,
406 'relationship_direction' => 'b_a',
407 'is_active' => 1,
6a488035 408 );
771f3245 409 $memType = $this->callAPISuccess('membership_type', 'create', $params);
6a488035
TO
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,
6a488035
TO
420 );
421 $membershipID = $this->contactMembershipCreate($params);
422
423 $params = array(
424 'contact_id' => $memberContactId,
425 'membership_type_id' => $memType['id'],
6a488035
TO
426 );
427
771f3245 428 $result = $this->callAPISuccess('membership', 'get', $params);
6a488035
TO
429
430 $membership = $result['values'][$membershipID];
771f3245 431 $this->assertEquals($this->_membershipStatusID, $membership['status_id']);
d54576ed 432 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 433 'id' => $membership['id'],
5896d037 434 ));
6a488035
TO
435 $this->membershipTypeDelete(array('id' => $memType['id']));
436 $this->relationshipTypeDelete($relTypeID);
437 $this->contactDelete($membershipOrgId);
438 $this->contactDelete($memberContactId);
439 }
440
4cc99d00 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 */
00be9182 448 public function testCreateWithRelationship() {
4cc99d00 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);
4cc99d00 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
932a6904 513 $memberContactId[2] = $this->individualCreate(array('employer_id' => $employerId[0]), 2);
4cc99d00 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
4a009ccf
CW
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
4cc99d00 583 // Tear down - reverse of creation to be safe
584 $this->contactDelete($memberContactId[2]);
585 $this->contactDelete($memberContactId[1]);
586 $this->contactDelete($memberContactId[0]);
587 $this->contactDelete($employerId[1]);
588 $this->contactDelete($employerId[0]);
589 $this->membershipTypeDelete(array('id' => $membershipTypeId));
590 $this->contactDelete($membershipOrgId);
591 }
592
37eda84b 593 /**
594 * We are checking for no enotices + only id & end_date returned
595 */
00be9182 596 public function testMembershipGetWithReturn() {
d54576ed 597 $this->contactMembershipCreate($this->_params);
37eda84b 598 $result = $this->callAPISuccess('membership', 'get', array('return' => 'end_date'));
6c6e6187 599 foreach ($result['values'] as $membership) {
37eda84b 600 $this->assertEquals(array('id', 'end_date'), array_keys($membership));
601 }
602 }
6a488035
TO
603 ///////////////// civicrm_membership_create methods
604
605 /**
606 * Test civicrm_contact_memberships_create with empty params.
607 * Error expected.
608 */
00be9182 609 public function testCreateWithEmptyParams() {
6a488035 610 $params = array();
d54576ed 611 $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
612 }
613
614 /**
fe482240 615 * If is_overide is passed in status must also be passed in.
6a488035 616 */
00be9182 617 public function testCreateOverrideNoStatus() {
6a488035
TO
618 $params = $this->_params;
619 unset($params['status_id']);
d54576ed 620 $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
621 }
622
00be9182 623 public function testMembershipCreateMissingRequired() {
6a488035
TO
624 $params = array(
625 'membership_type_id' => '1',
626 'join_date' => '2006-01-21',
627 'start_date' => '2006-01-21',
628 'end_date' => '2006-12-21',
629 'source' => 'Payment',
630 'status_id' => '2',
6a488035
TO
631 );
632
d54576ed 633 $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
634 }
635
00be9182 636 public function testMembershipCreate() {
6a488035
TO
637 $params = array(
638 'contact_id' => $this->_contactID,
639 'membership_type_id' => $this->_membershipTypeID,
640 'join_date' => '2006-01-21',
641 'start_date' => '2006-01-21',
642 'end_date' => '2006-12-21',
643 'source' => 'Payment',
644 'is_override' => 1,
645 'status_id' => $this->_membershipStatusID,
6a488035
TO
646 );
647
771f3245 648 $result = $this->callAPIAndDocument('membership', 'create', $params, __FUNCTION__, __FILE__);
6a488035 649 $this->getAndCheck($params, $result['id'], $this->_entity);
6a488035
TO
650 $this->assertNotNull($result['id']);
651 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['contact_id'], " in line " . __LINE__);
652 $this->assertEquals($result['id'], $result['values'][$result['id']]['id'], " in line " . __LINE__);
653 }
5896d037 654
28a04ea9 655 /**
656 * Check for useful message if contact doesn't exist
657 */
00be9182 658 public function testMembershipCreateWithInvalidContact() {
6a488035
TO
659 $params = array(
660 'contact_id' => 999,
661 'membership_type_id' => $this->_membershipTypeID,
662 'join_date' => '2006-01-21',
663 'start_date' => '2006-01-21',
664 'end_date' => '2006-12-21',
665 'source' => 'Payment',
666 'is_override' => 1,
667 'status_id' => $this->_membershipStatusID,
6a488035
TO
668 );
669
d54576ed 670 $this->callAPIFailure('membership', 'create', $params,
771f3245 671 'contact_id is not valid : 999'
672 );
6a488035 673 }
5896d037 674
00be9182 675 public function testMembershipCreateWithInvalidStatus() {
6a488035
TO
676 $params = $this->_params;
677 $params['status_id'] = 999;
d54576ed 678 $this->callAPIFailure('membership', 'create', $params,
771f3245 679 "'999' is not a valid option for field status_id"
680 );
6a488035
TO
681 }
682
00be9182 683 public function testMembershipCreateWithInvalidType() {
6a488035
TO
684 $params = $this->_params;
685 $params['membership_type_id'] = 999;
686
d54576ed 687 $this->callAPIFailure('membership', 'create', $params,
771f3245 688 "'999' is not a valid option for field membership_type_id"
689 );
6a488035
TO
690 }
691
692 /**
100fef9d 693 * Check with complete array + custom field
6a488035
TO
694 * Note that the test is written on purpose without any
695 * variables specific to participant so it can be replicated into other entities
696 * and / or moved to the automated test suite
697 */
00be9182 698 public function testCreateWithCustom() {
6a488035
TO
699 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
700
701 $params = $this->_params;
702 $params['custom_' . $ids['custom_field_id']] = "custom string";
703
a828d7b8 704 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, NULL, 'CreateWithCustomData');
5896d037
TO
705 $check = $this->callAPISuccess($this->_entity, 'get', array(
706 'id' => $result['id'],
21dfd5f5 707 'contact_id' => $this->_contactID,
5896d037 708 ));
6a488035 709 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
6a488035
TO
710 }
711
712 /**
713 * Test civicrm_contact_memberships_create with membership id (edit
714 * membership).
715 * success expected.
716 */
00be9182 717 public function testMembershipCreateWithId() {
6a488035
TO
718 $membershipID = $this->contactMembershipCreate($this->_params);
719 $params = array(
720 'id' => $membershipID,
721 'contact_id' => $this->_contactID,
722 'membership_type_id' => $this->_membershipTypeID,
723 'join_date' => '2006-01-21',
724 'start_date' => '2006-01-21',
725 'end_date' => '2006-12-21',
726 'source' => 'Payment',
727 'is_override' => 1,
728 'status_id' => $this->_membershipStatusID,
6a488035
TO
729 );
730
771f3245 731 $result = $this->callAPISuccess('membership', 'create', $params);
732 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 733 'id' => $result['id'],
771f3245 734 ));
6a488035
TO
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 */
00be9182 743 public function testMembershipCreateUpdateWithIdNoContact() {
6a488035
TO
744 $membershipID = $this->contactMembershipCreate($this->_params);
745 $params = array(
746 'id' => $membershipID,
747 'membership_type_id' => $this->_membershipTypeID,
748 'contact_id' => $this->_contactID,
749 'join_date' => '2006-01-21',
750 'start_date' => '2006-01-21',
751 'end_date' => '2006-12-21',
752 'source' => 'Payment',
753 'is_override' => 1,
754 'status_id' => $this->_membershipStatusID,
6a488035
TO
755 );
756
771f3245 757 $result = $this->callAPISuccess('membership', 'create', $params);
758 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 759 'id' => $result['id'],
5896d037 760 ));
771f3245 761
6a488035
TO
762 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
763 }
764
765 /**
766 * Test civicrm_contact_memberships_create with membership id (edit
767 * membership).
768 * success expected.
769 */
00be9182 770 public function testMembershipCreateUpdateWithIdNoDates() {
6a488035
TO
771 $membershipID = $this->contactMembershipCreate($this->_params);
772 $params = array(
773 'id' => $membershipID,
774 'contact_id' => $this->_contactID,
775 'membership_type_id' => $this->_membershipTypeID,
776 'source' => 'Payment',
777 'is_override' => 1,
778 'status_id' => $this->_membershipStatusID,
6a488035
TO
779 );
780
771f3245 781 $result = $this->callAPISuccess('membership', 'create', $params);
782 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 783 'id' => $result['id'],
5896d037 784 ));
6a488035
TO
785 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
786 }
787
788 /**
789 * Test civicrm_contact_memberships_create with membership id (edit
790 * membership).
791 * success expected.
792 */
00be9182 793 public function testMembershipCreateUpdateWithIdNoDatesNoType() {
6a488035
TO
794 $membershipID = $this->contactMembershipCreate($this->_params);
795 $params = array(
796 'id' => $membershipID,
797 'source' => 'not much here',
798 'contact_id' => $this->_contactID,
799 'is_override' => 1,
800 'status_id' => $this->_membershipStatusID,
6a488035
TO
801 );
802
771f3245 803 $result = $this->callAPISuccess('membership', 'create', $params);
804 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 805 'id' => $result['id'],
771f3245 806 ));
6a488035
TO
807 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
808 }
809
810 /**
811 * Test civicrm_contact_memberships_create with membership id (edit
812 * membership).
813 * success expected.
814 */
00be9182 815 public function testMembershipCreateUpdateWithIDAndSource() {
6a488035
TO
816 $membershipID = $this->contactMembershipCreate($this->_params);
817 $params = array(
818 'id' => $membershipID,
819 'source' => 'changed',
820 'contact_id' => $this->_contactID,
6c6e6187 821 'status_id' => $this->_membershipStatusID,
5896d037 822 'membership_type_id' => $this->_membershipTypeID,
6a488035
TO
823 'skipStatusCal' => 1,
824 );
771f3245 825 $result = $this->callAPISuccess('membership', 'create', $params);
6a488035 826 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
771f3245 827 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 828 'id' => $result['id'],
5896d037 829 ));
6a488035
TO
830 }
831
832 /**
eceb18cc 833 * Change custom field using update.
6a488035 834 */
00be9182 835 public function testUpdateWithCustom() {
6a488035
TO
836 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
837
838 $params = $this->_params;
839 $params['custom_' . $ids['custom_field_id']] = "custom string";
a828d7b8 840 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, NULL, 'UpdateCustomData');
5896d037
TO
841 $result = $this->callAPISuccess($this->_entity, 'create', array(
842 'id' => $result['id'],
21dfd5f5 843 'custom_' . $ids['custom_field_id'] => "new custom",
5896d037
TO
844 ));
845 $check = $this->callAPISuccess($this->_entity, 'get', array(
846 'id' => $result['id'],
21dfd5f5 847 'contact_id' => $this->_contactID,
5896d037 848 ));
6a488035
TO
849
850 $this->assertEquals("new custom", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
d54576ed 851 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 852 'id' => $check['id'],
5896d037 853 ));
6a488035
TO
854
855 $this->customFieldDelete($ids['custom_field_id']);
856 $this->customGroupDelete($ids['custom_group_id']);
857 }
858
93c482a4
EM
859 /**
860 * per CRM-15746 check that the id can be altered in an update hook
861 */
28a04ea9 862 public function testMembershipUpdateCreateHookCRM15746() {
93c482a4
EM
863 $this->hookClass->setHook('civicrm_pre', array($this, 'hook_civicrm_pre_update_create_membership'));
864 $result = $this->callAPISuccess('membership', 'create', $this->_params);
865 $this->callAPISuccess('membership', 'create', array('id' => $result['id'], 'end_date' => '1 year ago'));
866 $this->callAPISuccessGetCount('membership', array(), 2);
867 $this->hookClass->reset();
868 $this->callAPISuccess('membership', 'create', array('id' => $result['id'], 'end_date' => '1 year ago'));
869 $this->callAPISuccessGetCount('membership', array(), 2);
870 }
871
2ea0abec
EM
872 /**
873 * Custom hook for update membership.
874 *
875 * @param string $op
876 * @param object $objectName
877 * @param int $id
878 * @param array $params
879 *
880 * @throws \Exception
881 */
28a04ea9 882 public function hook_civicrm_pre_update_create_membership($op, $objectName, $id, &$params) {
93c482a4
EM
883 if ($objectName == 'Membership' && $op == 'edit') {
884 $existingMembership = $this->callAPISuccessGetSingle('membership', array('id' => $params['id']));
885 unset($params['id'], $params['membership_id']);
6c6e6187 886 $params['join_date'] = $params['membership_start_date'] = $params['start_date'] = date('Ymd000000', strtotime($existingMembership['start_date']));
93c482a4
EM
887 $params = array_merge($existingMembership, $params);
888 $params['id'] = NULL;
889 }
890 }
891
6a488035 892 /**
fe482240 893 * Test civicrm_contact_memberships_create Invalid membership data.
6a488035
TO
894 * Error expected.
895 */
00be9182 896 public function testMembershipCreateInvalidMemData() {
6a488035
TO
897 //membership_contact_id as string
898 $params = array(
899 'membership_contact_id' => 'Invalid',
900 'membership_type_id' => $this->_membershipTypeID,
901 'join_date' => '2011-01-21',
902 'start_date' => '2010-01-21',
903 'end_date' => '2008-12-21',
904 'source' => 'Payment',
905 'is_override' => 1,
5896d037
TO
906 'status_id' => $this->_membershipStatusID,
907 );
6a488035 908
d54576ed 909 $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
910
911 //membership_contact_id which is no in contact table
912 $params['membership_contact_id'] = 999;
d54576ed 913 $this->callAPIFailure('membership', 'create', $params);
6a488035
TO
914
915 //invalid join date
916 unset($params['membership_contact_id']);
917 $params['join_date'] = "invalid";
d54576ed 918 $this->callAPIFailure('Membership', 'Create', $params);
6a488035
TO
919 }
920
921 /**
922 * Test civicrm_contact_memberships_create with membership_contact_id
923 * membership).
924 * Success expected.
925 */
00be9182 926 public function testMembershipCreateWithMemContact() {
6a488035
TO
927 $params = array(
928 'membership_contact_id' => $this->_contactID,
929 'membership_type_id' => $this->_membershipTypeID,
930 'join_date' => '2011-01-21',
931 'start_date' => '2010-01-21',
932 'end_date' => '2008-12-21',
933 'source' => 'Payment',
934 'is_override' => 1,
935 'status_id' => $this->_membershipStatusID,
6a488035
TO
936 );
937
771f3245 938 $result = $this->callAPISuccess('membership', 'create', $params);
6a488035 939
d54576ed 940 $this->callAPISuccess('Membership', 'Delete', array(
6a488035 941 'id' => $result['id'],
771f3245 942 ));
6a488035 943 }
5896d037 944
cc73900e 945 /**
946 * Test civicrm_contact_memberships_create with membership_contact_id
947 * membership).
948 * Success expected.
949 */
00be9182 950 public function testMembershipCreateValidMembershipTypeString() {
cc73900e 951 $params = array(
952 'membership_contact_id' => $this->_contactID,
953 'membership_type_id' => 'General',
954 'join_date' => '2011-01-21',
955 'start_date' => '2010-01-21',
956 'end_date' => '2008-12-21',
957 'source' => 'Payment',
958 'is_override' => 1,
959 'status_id' => $this->_membershipStatusID,
960 );
961
962 $result = $this->callAPISuccess('membership', 'create', $params);
963 $this->assertEquals($this->_membershipTypeID, $result['values'][$result['id']]['membership_type_id']);
d54576ed 964 $this->callAPISuccess('Membership', 'Delete', array(
cc73900e 965 'id' => $result['id'],
966 ));
967 }
968
969 /**
970 * Test civicrm_contact_memberships_create with membership_contact_id
971 * membership).
972 * Success expected.
973 */
00be9182 974 public function testMembershipCreateInValidMembershipTypeString() {
cc73900e 975 $params = array(
976 'membership_contact_id' => $this->_contactID,
977 'membership_type_id' => 'invalid',
978 'join_date' => '2011-01-21',
979 'start_date' => '2010-01-21',
980 'end_date' => '2008-12-21',
981 'source' => 'Payment',
982 'is_override' => 1,
983 'status_id' => $this->_membershipStatusID,
984 );
985
d54576ed 986 $this->callAPIFailure('membership', 'create', $params);
cc73900e 987 }
6a488035 988
cc73900e 989 /**
eceb18cc 990 * Test that if membership join date is not set it defaults to today.
cc73900e 991 */
00be9182 992 public function testEmptyJoinDate() {
8c33a68c 993 unset($this->_params['join_date'], $this->_params['is_override']);
994 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
995 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
996 $this->assertEquals(date('Y-m-d', strtotime('now')), $result['join_date']);
997 $this->assertEquals('2009-01-21', $result['start_date']);
998 $this->assertEquals('2009-12-21', $result['end_date']);
cc73900e 999 }
5896d037 1000
cc73900e 1001 /**
fe482240 1002 * Test that if membership start date is not set it defaults to correct end date.
8c33a68c 1003 * - fixed
cc73900e 1004 */
00be9182 1005 public function testEmptyStartDateFixed() {
8c33a68c 1006 unset($this->_params['start_date'], $this->_params['is_override']);
1007 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1008 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1009 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1010 $this->assertEquals('2009-01-21', $result['join_date']);
1011 $this->assertEquals('2008-03-01', $result['start_date']);
1012 $this->assertEquals('2009-12-21', $result['end_date']);
1013 }
cc73900e 1014
8c33a68c 1015 /**
1016 * Test that if membership start date is not set it defaults to correct end date
1017 * - fixed
1018 */
2ea0abec
EM
1019 public function testEmptyStartEndDateFixedOneYear() {
1020 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1021 $this->callAPISuccess('membership_type', 'create', array('id' => $this->_membershipTypeID2, 'duration_interval' => 1));
1022 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1023 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1024 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1025 $this->assertEquals('2009-01-21', $result['join_date']);
1026 $this->assertEquals('2008-03-01', $result['start_date']);
1027 $this->assertEquals('2010-02-28', $result['end_date']);
1028 }
1029
1030 /**
9398f167
EM
1031 * Test that if membership start date is not set it defaults to correct end date for fixed multi year memberships.
1032 */
1033 public function testEmptyStartEndDateFixedMultiYear() {
1034 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1035 $this->callAPISuccess('membership_type', 'create', array('id' => $this->_membershipTypeID2, 'duration_interval' => 5));
1036 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1037 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1038 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1039 $this->assertEquals('2009-01-21', $result['join_date']);
1040 $this->assertEquals('2008-03-01', $result['start_date']);
1041 $this->assertEquals('2014-02-28', $result['end_date']);
1042 }
1043
b1fc74f0 1044 /**
964a9e96
EM
1045 * Test correct end and start dates are calculated for fixed multi year memberships.
1046 *
1047 * The empty start date is calculated to be the start_date (1 Jan prior to the join_date - so 1 Jan 15)
1048 *
1049 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1050 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1051 * and we add on 4 years rather than 5 because we are not after the rollover day - so we calculate 31 Dec 2019
1052 */
1053 public function testFixedMultiYearDateSetTwoEmptyStartEndDate() {
1054 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1055
1056 $this->callAPISuccess('membership_type', 'create', array(
1057 'id' => $this->_membershipTypeID2,
1058 'duration_interval' => 5,
1059 // Ie 1 Jan.
1060 'fixed_period_start_day' => '101',
1061 // Ie. 1 Nov.
1062 'fixed_period_rollover_day' => '1101',
1063 ));
1064 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1065 $dates = array(
1066 'join_date' => '28-Jan 2015',
1067 );
1068 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1069 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1070 $this->assertEquals('2015-01-28', $result['join_date']);
1071 $this->assertEquals('2015-01-01', $result['start_date']);
1072 $this->assertEquals('2019-12-31', $result['end_date']);
1073 }
1074
1075 /**
1076 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
b1fc74f0
EM
1077 *
1078 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1079 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1080 * and we add on 4 years rather than 5 because we are not after the rollover day - so we calculate 31 Dec 2019
1081 */
964a9e96 1082 public function testFixedMultiYearDateSetTwoEmptyEndDate() {
b1fc74f0
EM
1083 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1084
1085 $this->callAPISuccess('membership_type', 'create', array(
1086 'id' => $this->_membershipTypeID2,
1087 'duration_interval' => 5,
1088 // Ie 1 Jan.
1089 'fixed_period_start_day' => '101',
1090 // Ie. 1 Nov.
1091 'fixed_period_rollover_day' => '1101',
1092 ));
1093 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1094 $dates = array(
1095 'start_date' => '28-Jan 2015',
1096 'join_date' => '28-Jan 2015',
1097 );
1098 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1099 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1100 $this->assertEquals('2015-01-28', $result['join_date']);
1101 $this->assertEquals('2015-01-28', $result['start_date']);
1102 $this->assertEquals('2019-12-31', $result['end_date']);
1103 }
1104
1105 /**
964a9e96
EM
1106 * Test correct end and start dates are calculated for fixed multi year memberships.
1107 *
1108 * The empty start date is calculated to be the start_date (1 Jan prior to the join_date - so 1 Jan 15)
1109 *
1110 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1111 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1112 * and we add on <1 years rather than > 1 because we are not after the rollover day - so we calculate 31 Dec 2015
1113 */
1114 public function testFixedSingleYearDateSetTwoEmptyStartEndDate() {
1115 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1116
1117 $this->callAPISuccess('membership_type', 'create', array(
1118 'id' => $this->_membershipTypeID2,
1119 'duration_interval' => 1,
1120 // Ie 1 Jan.
1121 'fixed_period_start_day' => '101',
1122 // Ie. 1 Nov.
1123 'fixed_period_rollover_day' => '1101',
1124 ));
1125 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1126 $dates = array(
1127 'join_date' => '28-Jan 2015',
1128 );
1129 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1130 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1131 $this->assertEquals('2015-01-28', $result['join_date']);
1132 $this->assertEquals('2015-01-01', $result['start_date']);
1133 $this->assertEquals('2015-12-31', $result['end_date']);
1134 }
1135
1136 /**
1137 * Test correct end date for fixed single year memberships is calculated and start_date is not changed.
b1fc74f0
EM
1138 *
1139 * In this set our start date is after the start day and before the rollover day so we don't get an extra year
1140 * and we end one day before the rollover day. Start day is 1 Jan so we end on 31 Dec
1141 * and we add on <1 years rather than > 1 because we are not after the rollover day - so we calculate 31 Dec 2015
1142 */
964a9e96 1143 public function testFixedSingleYearDateSetTwoEmptyEndDate() {
b1fc74f0
EM
1144 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1145
1146 $this->callAPISuccess('membership_type', 'create', array(
1147 'id' => $this->_membershipTypeID2,
1148 'duration_interval' => 1,
1149 // Ie 1 Jan.
1150 'fixed_period_start_day' => '101',
1151 // Ie. 1 Nov.
1152 'fixed_period_rollover_day' => '1101',
1153 ));
1154 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1155 $dates = array(
1156 'start_date' => '28-Jan 2015',
1157 'join_date' => '28-Jan 2015',
1158 );
1159 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1160 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1161 $this->assertEquals('2015-01-28', $result['join_date']);
1162 $this->assertEquals('2015-01-28', $result['start_date']);
1163 $this->assertEquals('2015-12-31', $result['end_date']);
1164 }
1165
1166 /**
964a9e96 1167 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
b1fc74f0
EM
1168 *
1169 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1170 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
964a9e96 1171 * and we add on 1 year we are after the rollover day - so we calculate 31 Oct 2016
b1fc74f0 1172 */
964a9e96 1173 public function testFixedSingleYearDateSetThreeEmptyEndDate() {
b1fc74f0
EM
1174 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1175
1176 $this->callAPISuccess('membership_type', 'create', array(
1177 'id' => $this->_membershipTypeID2,
1178 'duration_interval' => 1,
1179 // Ie. 1 Nov.
1180 'fixed_period_start_day' => '1101',
1181 // Ie 1 Jan.
1182 'fixed_period_rollover_day' => '101',
1183 ));
1184 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1185 $dates = array(
1186 'start_date' => '28-Jan 2015',
1187 'join_date' => '28-Jan 2015',
1188 );
1189 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1190 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1191 $this->assertEquals('2015-01-28', $result['join_date']);
1192 $this->assertEquals('2015-01-28', $result['start_date']);
1193 $this->assertEquals('2016-10-31', $result['end_date']);
1194 }
1195
964a9e96
EM
1196 /**
1197 * Test correct end and start dates are calculated for fixed multi year memberships.
1198 *
d177a2a6 1199 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
964a9e96
EM
1200 *
1201 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1202 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1203 * and we add on 1 year we are after the rollover day - so we calculate 31 Oct 2016
1204 */
1205 public function testFixedSingleYearDateSetThreeEmptyStartEndDate() {
1206 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1207
1208 $this->callAPISuccess('membership_type', 'create', array(
1209 'id' => $this->_membershipTypeID2,
1210 'duration_interval' => 1,
1211 // Ie. 1 Nov.
1212 'fixed_period_start_day' => '1101',
1213 // Ie 1 Jan.
1214 'fixed_period_rollover_day' => '101',
1215 ));
1216 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1217 $dates = array(
1218 'join_date' => '28-Jan 2015',
1219 );
1220 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1221 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1222 $this->assertEquals('2015-01-28', $result['join_date']);
1223 $this->assertEquals('2014-11-01', $result['start_date']);
1224 $this->assertEquals('2016-10-31', $result['end_date']);
1225 }
b1fc74f0
EM
1226
1227 /**
964a9e96 1228 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
b1fc74f0
EM
1229 *
1230 * In this set our start date is after the start day and after the rollover day so we do get an extra year
1231 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1232 * and we add on 5 years we are after the rollover day - so we calculate 31 Oct 2020
1233 */
964a9e96 1234 public function testFixedMultiYearDateSetThreeEmptyEndDate() {
b1fc74f0
EM
1235 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1236
1237 $this->callAPISuccess('membership_type', 'create', array(
1238 'id' => $this->_membershipTypeID2,
1239 'duration_interval' => 5,
1240 // Ie. 1 Nov.
1241 'fixed_period_start_day' => '1101',
1242 // Ie 1 Jan.
1243 'fixed_period_rollover_day' => '101',
1244 ));
1245 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1246 $dates = array(
1247 'start_date' => '28-Jan 2015',
1248 'join_date' => '28-Jan 2015',
1249 );
1250 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1251 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1252 $this->assertEquals('2015-01-28', $result['join_date']);
1253 $this->assertEquals('2015-01-28', $result['start_date']);
1254 $this->assertEquals('2020-10-31', $result['end_date']);
1255 }
9398f167 1256
964a9e96
EM
1257 /**
1258 * Test correct end and start dates are calculated for fixed multi year memberships.
1259 *
d177a2a6 1260 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
964a9e96
EM
1261 *
1262 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
1263 * In this set our join date is after the start day and after the rollover day so we do get an extra year
1264 * and we end one day before the rollover day. Start day is 1 Nov so we end on 31 Oct
1265 * and we add on 5 years we are after the rollover day - so we calculate 31 Oct 2020
1266 */
1267 public function testFixedMultiYearDateSetThreeEmptyStartEndDate() {
1268 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1269
1270 $this->callAPISuccess('membership_type', 'create', array(
1271 'id' => $this->_membershipTypeID2,
1272 'duration_interval' => 5,
1273 // Ie. 1 Nov.
1274 'fixed_period_start_day' => '1101',
1275 // Ie 1 Jan.
1276 'fixed_period_rollover_day' => '101',
1277 ));
1278 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1279 $dates = array(
1280 'join_date' => '28-Jan 2015',
1281 );
1282 $result = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, $dates));
1283 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1284 $this->assertEquals('2015-01-28', $result['join_date']);
1285 $this->assertEquals('2014-11-01', $result['start_date']);
1286 $this->assertEquals('2020-10-31', $result['end_date']);
1287 }
1288
9398f167
EM
1289 /**
1290 * Test that if membership start date is not set it defaults to correct end date for fixed single year memberships.
2ea0abec 1291 */
00be9182 1292 public function testEmptyStartDateRolling() {
8c33a68c 1293 unset($this->_params['start_date'], $this->_params['is_override']);
1294 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1295 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1296 $this->assertEquals('2009-01-21', $result['join_date']);
1297 $this->assertEquals('2009-01-21', $result['start_date']);
1298 $this->assertEquals('2009-12-21', $result['end_date']);
cc73900e 1299 }
5896d037 1300
8c33a68c 1301 /**
eceb18cc 1302 * Test that if membership end date is not set it defaults to correct end date.
8c33a68c 1303 * - rolling
1304 */
00be9182 1305 public function testEmptyEndDateFixed() {
8c33a68c 1306 unset($this->_params['start_date'], $this->_params['is_override'], $this->_params['end_date']);
1307 $this->_params['membership_type_id'] = $this->_membershipTypeID2;
1308 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1309 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1310 $this->assertEquals('2009-01-21', $result['join_date']);
1311 $this->assertEquals('2008-03-01', $result['start_date']);
1312 $this->assertEquals('2010-02-28', $result['end_date']);
1313 }
5896d037 1314
8c33a68c 1315 /**
eceb18cc 1316 * Test that if membership end date is not set it defaults to correct end date.
8c33a68c 1317 * - rolling
1318 */
00be9182 1319 public function testEmptyEndDateRolling() {
8c33a68c 1320 unset($this->_params['is_override'], $this->_params['end_date']);
1321 $this->_params['membership_type_id'] = $this->_membershipTypeID;
1322 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1323 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1324 $this->assertEquals('2009-01-21', $result['join_date']);
1325 $this->assertEquals('2009-01-21', $result['start_date']);
1326 $this->assertEquals('2010-01-20', $result['end_date']);
1327 }
1328
1329
1330 /**
452b9e04 1331 * Test that if dates are set they not over-ridden if id is passed in
8c33a68c 1332 */
6c6e6187 1333 public function testMembershipDatesNotOverridden() {
8c33a68c 1334 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
1335 unset($this->_params['end_date'], $this->_params['start_date']);
1336 $this->_params['id'] = $result['id'];
1337 $this->callAPISuccess($this->_entity, 'create', $this->_params);
1338 $result = $this->callAPISuccess($this->_entity, 'getsingle', array('id' => $result['id']));
1339 $this->assertEquals('2009-01-21', $result['join_date']);
1340 $this->assertEquals('2009-01-21', $result['start_date']);
1341 $this->assertEquals('2009-12-21', $result['end_date']);
1342
6c6e6187 1343 }
96025800 1344
6a488035 1345}