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