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