tests/phpunit/** - Remove unnecessary "require_once" statements
[civicrm-core.git] / tests / phpunit / api / v3 / MembershipTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Test APIv3 civicrm_membership functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Member
33 */
34
35 /**
36 * Class api_v3_MembershipTest
37 */
38 class api_v3_MembershipTest extends CiviUnitTestCase {
39 protected $_apiversion;
40 protected $_contactID;
41 protected $_membershipID;
42 protected $_membershipID2;
43 protected $_membershipID3;
44 protected $_membershipTypeID;
45 protected $_membershipTypeID2;
46 protected $_membershipStatusID;
47 protected $_entity;
48 protected $_params;
49
50 /**
51 * Set up for tests.
52 */
53 public function setUp() {
54 parent::setUp();
55 $this->_apiversion = 3;
56 $this->_contactID = $this->individualCreate();
57 $this->_membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID));
58 $this->_membershipTypeID2 = $this->membershipTypeCreate(array(
59 'period_type' => 'fixed',
60 // Ie. 1 March.
61 'fixed_period_start_day' => '301',
62 // Ie. 11 Nov.
63 'fixed_period_rollover_day' => '1111',
64 ));
65 $this->_membershipStatusID = $this->membershipStatusCreate('test status');
66
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,
81 );
82 }
83
84 /**
85 * Clean up after tests.
86 *
87 * @throws \Exception
88 */
89 public function tearDown() {
90 $this->quickCleanup(array(
91 'civicrm_membership',
92 'civicrm_membership_payment',
93 'civicrm_membership_log',
94 ),
95 TRUE
96 );
97 $this->membershipStatusDelete($this->_membershipStatusID);
98 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID2));
99 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID));
100 $this->contactDelete($this->_contactID);
101
102 }
103
104 /**
105 * Test membership deletion.
106 */
107 public function testMembershipDelete() {
108 $membershipID = $this->contactMembershipCreate($this->_params);
109 $this->assertDBRowExist('CRM_Member_DAO_Membership', $membershipID);
110 $params = array(
111 'id' => $membershipID,
112 );
113 $this->callAPIAndDocument('membership', 'delete', $params, __FUNCTION__, __FILE__);
114 $this->assertDBRowNotExist('CRM_Member_DAO_Membership', $membershipID);
115 }
116
117 public function testMembershipDeleteEmpty() {
118 $this->callAPIFailure('membership', 'delete', array());
119 }
120
121 public function testMembershipDeleteInvalidID() {
122 $this->callAPIFailure('membership', 'delete', array('id' => 'blah'));
123 }
124
125 /**
126 * Test civicrm_membership_delete() with invalid Membership Id.
127 */
128 public function testMembershipDeleteWithInvalidMembershipId() {
129 $membershipId = 'membership';
130 $this->callAPIFailure('membership', 'delete', $membershipId);
131 }
132
133 /**
134 * Test membership get.
135 */
136 public function testContactMembershipsGet() {
137 $this->_membershipID = $this->contactMembershipCreate($this->_params);
138 $this->callAPISuccess('membership', 'get', array());
139 $this->callAPISuccess('Membership', 'Delete', array('id' => $this->_membershipID));
140 }
141
142 /**
143 * Test civicrm_membership_get with params not array.
144 *
145 * Gets treated as contact_id, memberships expected.
146 */
147 public function testGetWithParamsContactId() {
148 $this->_membershipID = $this->contactMembershipCreate($this->_params);
149 $params = array(
150 'contact_id' => $this->_contactID,
151 );
152 $membership = $this->callAPISuccess('membership', 'get', $params);
153
154 $result = $membership['values'][$this->_membershipID];
155 $this->callAPISuccess('Membership', 'Delete', array(
156 'id' => $this->_membershipID,
157 ));
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
168 /**
169 * Test civicrm_membership_get with params not array.
170 *
171 * Gets treated as contact_id, memberships expected.
172 */
173 public function testGetInSyntax() {
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']));
189 }
190
191 /**
192 * Test civicrm_membership_get with params not array.
193 * Gets treated as contact_id, memberships expected.
194 */
195 public function testGetInSyntaxOnContactID() {
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 }
214
215 /**
216 * Test civicrm_membership_get with params not array.
217 *
218 * Gets treated as contact_id, memberships expected.
219 */
220 public function testGetWithParamsMemberShipTypeId() {
221 $this->callAPISuccess($this->_entity, 'create', $this->_params);
222 $params = array(
223 'membership_type_id' => $this->_membershipTypeID,
224 );
225 $membership = $this->callAPISuccess('membership', 'get', $params);
226 $this->callAPISuccess('Membership', 'Delete', array(
227 'id' => $membership['id'],
228 ));
229 $result = $membership['values'][$membership['id']];
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);
238 $this->assertEquals($result['id'], $membership['id']);
239 }
240
241 /**
242 * Test civicrm_membership_get with params not array.
243 * Gets treated as contact_id, memberships expected.
244 */
245 public function testGetWithParamsMemberShipTypeIdContactID() {
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);
258
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 }
267
268 /**
269 * Check with complete array + custom field.
270 *
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 */
275 public function testGetWithParamsMemberShipIdAndCustom() {
276 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
277
278 $params = $this->_params;
279 $params['custom_' . $ids['custom_field_id']] = "custom string";
280
281 $result = $this->callAPISuccess($this->_entity, 'create', $params);
282
283 $getParams = array('membership_type_id' => $params['membership_type_id']);
284 $check = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
285 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
286
287 $this->callAPISuccess('Membership', 'Delete', array(
288 'id' => $result['id'],
289 ));
290 }
291
292 /**
293 * Test civicrm_membership_get with proper params.
294 * Memberships expected.
295 */
296 public function testGet() {
297 $membershipID = $this->contactMembershipCreate($this->_params);
298 $params = array(
299 'contact_id' => $this->_contactID,
300 );
301
302 $membership = $this->callAPISuccess('membership', 'get', $params);
303 $result = $membership['values'][$membershipID];
304 $this->callAPISuccess('Membership', 'Delete', array(
305 'id' => $membership['id'],
306 ));
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);
311
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);
316 }
317
318
319 /**
320 * Test civicrm_membership_get with proper params.
321 * Memberships expected.
322 */
323 public function testGetWithId() {
324 $membershipID = $this->contactMembershipCreate($this->_params);
325 $params = array(
326 'contact_id' => $this->_contactID,
327 'id' => $this->_membershipID,
328 'return' => 'id',
329 );
330 $result = $this->callAPISuccess('membership', 'get', $params);
331 $this->assertEquals($membershipID, $result['id']);
332 $params = array(
333 'contact_id' => $this->_contactID,
334 'membership_id' => $this->_membershipID,
335 'return' => 'membership_id',
336 );
337 $result = $this->callAPISuccess('membership', 'get', $params);
338 $this->assertEquals($membershipID, $result['id']);
339 }
340
341 /**
342 * Test civicrm_membership_get for only active.
343 * Memberships expected.
344 */
345 public function testGetOnlyActive() {
346 $description = "Demonstrates use of 'filter' active_only' param.";
347 $this->_membershipID = $this->contactMembershipCreate($this->_params);
348 $params = array(
349 'contact_id' => $this->_contactID,
350 'active_only' => 1,
351 );
352
353 $membership = $this->callAPISuccess('membership', 'get', $params);
354 $this->assertEquals($membership['values'][$this->_membershipID]['status_id'], $this->_membershipStatusID);
355 $this->assertEquals($membership['values'][$this->_membershipID]['contact_id'], $this->_contactID);
356 $params = array(
357 'contact_id' => $this->_contactID,
358 'filters' => array(
359 'is_current' => 1,
360 ),
361 );
362
363 $membership = $this->callAPIAndDocument('membership', 'get', $params, __FUNCTION__, __FILE__, $description, 'FilterIsCurrent');
364 $this->assertEquals($membership['values'][$this->_membershipID]['status_id'], $this->_membershipStatusID);
365 $this->assertEquals($membership['values'][$this->_membershipID]['contact_id'], $this->_contactID);
366
367 $this->callAPISuccess('Membership', 'Delete', array('id' => $this->_membershipID));
368 }
369
370 /**
371 * Test civicrm_membership_get for non exist contact.
372 * empty Memberships.
373 */
374 public function testGetNoContactExists() {
375 $params = array(
376 'contact_id' => 55555,
377 );
378
379 $membership = $this->callAPISuccess('membership', 'get', $params);
380 $this->assertEquals($membership['count'], 0);
381 }
382
383 /**
384 * Test civicrm_membership_get with relationship.
385 * get Memberships.
386 */
387 public function testGetWithRelationship() {
388 $membershipOrgId = $this->organizationCreate(NULL);
389 $memberContactId = $this->individualCreate();
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,
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,
409 'financial_type_id' => 1,
410 'relationship_type_id' => $relTypeID,
411 'relationship_direction' => 'b_a',
412 'is_active' => 1,
413 );
414 $memType = $this->callAPISuccess('membership_type', 'create', $params);
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,
425 );
426 $membershipID = $this->contactMembershipCreate($params);
427
428 $params = array(
429 'contact_id' => $memberContactId,
430 'membership_type_id' => $memType['id'],
431 );
432
433 $result = $this->callAPISuccess('membership', 'get', $params);
434
435 $membership = $result['values'][$membershipID];
436 $this->assertEquals($this->_membershipStatusID, $membership['status_id']);
437 $this->callAPISuccess('Membership', 'Delete', array(
438 'id' => $membership['id'],
439 ));
440 $this->membershipTypeDelete(array('id' => $memType['id']));
441 $this->relationshipTypeDelete($relTypeID);
442 $this->contactDelete($membershipOrgId);
443 $this->contactDelete($memberContactId);
444 }
445
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 */
453 public function testCreateWithRelationship() {
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);
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
518 $memberContactId[2] = $this->individualCreate(array('employer_id' => $employerId[0]), 2);
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
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
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
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
603 /**
604 * We are checking for no e-notices + only id & end_date returned
605 */
606 public function testMembershipGetWithReturn() {
607 $this->contactMembershipCreate($this->_params);
608 $result = $this->callAPISuccess('membership', 'get', array('return' => 'end_date'));
609 foreach ($result['values'] as $membership) {
610 $this->assertEquals(array('end_date', 'id'), array_keys($membership));
611 }
612 }
613 ///////////////// civicrm_membership_create methods
614
615 /**
616 * Test civicrm_contact_memberships_create with empty params.
617 * Error expected.
618 */
619 public function testCreateWithEmptyParams() {
620 $params = array();
621 $this->callAPIFailure('membership', 'create', $params);
622 }
623
624 /**
625 * If is_overide is passed in status must also be passed in.
626 */
627 public function testCreateOverrideNoStatus() {
628 $params = $this->_params;
629 unset($params['status_id']);
630 $this->callAPIFailure('membership', 'create', $params);
631 }
632
633 public function testMembershipCreateMissingRequired() {
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',
641 );
642
643 $this->callAPIFailure('membership', 'create', $params);
644 }
645
646 public function testMembershipCreate() {
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,
656 );
657
658 $result = $this->callAPIAndDocument('membership', 'create', $params, __FUNCTION__, __FILE__);
659 $this->getAndCheck($params, $result['id'], $this->_entity);
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 }
664
665 /**
666 * Check for useful message if contact doesn't exist
667 */
668 public function testMembershipCreateWithInvalidContact() {
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,
678 );
679
680 $this->callAPIFailure('membership', 'create', $params,
681 'contact_id is not valid : 999'
682 );
683 }
684
685 public function testMembershipCreateWithInvalidStatus() {
686 $params = $this->_params;
687 $params['status_id'] = 999;
688 $this->callAPIFailure('membership', 'create', $params,
689 "'999' is not a valid option for field status_id"
690 );
691 }
692
693 public function testMembershipCreateWithInvalidType() {
694 $params = $this->_params;
695 $params['membership_type_id'] = 999;
696
697 $this->callAPIFailure('membership', 'create', $params,
698 "'999' is not a valid option for field membership_type_id"
699 );
700 }
701
702 /**
703 * Check with complete array + custom field
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 */
708 public function testCreateWithCustom() {
709 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
710
711 $params = $this->_params;
712 $params['custom_' . $ids['custom_field_id']] = "custom string";
713
714 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, NULL, 'CreateWithCustomData');
715 $check = $this->callAPISuccess($this->_entity, 'get', array(
716 'id' => $result['id'],
717 'contact_id' => $this->_contactID,
718 ));
719 $this->assertEquals("custom string", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
720 }
721
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
756 /**
757 * Test civicrm_contact_memberships_create with membership id (edit
758 * membership).
759 * success expected.
760 */
761 public function testMembershipCreateWithId() {
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,
773 );
774
775 $result = $this->callAPISuccess('membership', 'create', $params);
776 $this->callAPISuccess('Membership', 'Delete', array(
777 'id' => $result['id'],
778 ));
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 */
787 public function testMembershipCreateUpdateWithIdNoContact() {
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,
799 );
800
801 $result = $this->callAPISuccess('membership', 'create', $params);
802 $this->callAPISuccess('Membership', 'Delete', array(
803 'id' => $result['id'],
804 ));
805
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 */
814 public function testMembershipCreateUpdateWithIdNoDates() {
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,
823 );
824
825 $result = $this->callAPISuccess('membership', 'create', $params);
826 $this->callAPISuccess('Membership', 'Delete', array(
827 'id' => $result['id'],
828 ));
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 */
837 public function testMembershipCreateUpdateWithIdNoDatesNoType() {
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,
845 );
846
847 $result = $this->callAPISuccess('membership', 'create', $params);
848 $this->callAPISuccess('Membership', 'Delete', array(
849 'id' => $result['id'],
850 ));
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 */
859 public function testMembershipCreateUpdateWithIDAndSource() {
860 $membershipID = $this->contactMembershipCreate($this->_params);
861 $params = array(
862 'id' => $membershipID,
863 'source' => 'changed',
864 'contact_id' => $this->_contactID,
865 'status_id' => $this->_membershipStatusID,
866 'membership_type_id' => $this->_membershipTypeID,
867 'skipStatusCal' => 1,
868 );
869 $result = $this->callAPISuccess('membership', 'create', $params);
870 $this->assertEquals($result['id'], $membershipID, "in line " . __LINE__);
871 $this->callAPISuccess('Membership', 'Delete', array(
872 'id' => $result['id'],
873 ));
874 }
875
876 /**
877 * Change custom field using update.
878 */
879 public function testUpdateWithCustom() {
880 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
881
882 $params = $this->_params;
883 $params['custom_' . $ids['custom_field_id']] = "custom string";
884 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, NULL, 'UpdateCustomData');
885 $result = $this->callAPISuccess($this->_entity, 'create', array(
886 'id' => $result['id'],
887 'custom_' . $ids['custom_field_id'] => "new custom",
888 ));
889 $check = $this->callAPISuccess($this->_entity, 'get', array(
890 'id' => $result['id'],
891 'contact_id' => $this->_contactID,
892 ));
893
894 $this->assertEquals("new custom", $check['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
895 $this->callAPISuccess('Membership', 'Delete', array(
896 'id' => $check['id'],
897 ));
898
899 $this->customFieldDelete($ids['custom_field_id']);
900 $this->customGroupDelete($ids['custom_group_id']);
901 }
902
903 /**
904 * per CRM-15746 check that the id can be altered in an update hook
905 */
906 public function testMembershipUpdateCreateHookCRM15746() {
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
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 */
926 public function hook_civicrm_pre_update_create_membership($op, $objectName, $id, &$params) {
927 if ($objectName == 'Membership' && $op == 'edit') {
928 $existingMembership = $this->callAPISuccessGetSingle('membership', array('id' => $params['id']));
929 unset($params['id'], $params['membership_id']);
930 $params['join_date'] = $params['membership_start_date'] = $params['start_date'] = date('Ymd000000', strtotime($existingMembership['start_date']));
931 $params = array_merge($existingMembership, $params);
932 $params['id'] = NULL;
933 }
934 }
935
936 /**
937 * Test civicrm_contact_memberships_create Invalid membership data.
938 * Error expected.
939 */
940 public function testMembershipCreateInvalidMemData() {
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,
950 'status_id' => $this->_membershipStatusID,
951 );
952
953 $this->callAPIFailure('membership', 'create', $params);
954
955 //membership_contact_id which is no in contact table
956 $params['membership_contact_id'] = 999;
957 $this->callAPIFailure('membership', 'create', $params);
958
959 //invalid join date
960 unset($params['membership_contact_id']);
961 $params['join_date'] = "invalid";
962 $this->callAPIFailure('Membership', 'Create', $params);
963 }
964
965 /**
966 * Test civicrm_contact_memberships_create with membership_contact_id
967 * membership).
968 * Success expected.
969 */
970 public function testMembershipCreateWithMemContact() {
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,
980 );
981
982 $result = $this->callAPISuccess('membership', 'create', $params);
983
984 $this->callAPISuccess('Membership', 'Delete', array(
985 'id' => $result['id'],
986 ));
987 }
988
989 /**
990 * Test civicrm_contact_memberships_create with membership_contact_id
991 * membership).
992 * Success expected.
993 */
994 public function testMembershipCreateValidMembershipTypeString() {
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']);
1008 $this->callAPISuccess('Membership', 'Delete', array(
1009 'id' => $result['id'],
1010 ));
1011 }
1012
1013 /**
1014 * Test civicrm_contact_memberships_create with membership_contact_id
1015 * membership).
1016 * Success expected.
1017 */
1018 public function testMembershipCreateInValidMembershipTypeString() {
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
1030 $this->callAPIFailure('membership', 'create', $params);
1031 }
1032
1033 /**
1034 * Test that if membership join date is not set it defaults to today.
1035 */
1036 public function testEmptyJoinDate() {
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']);
1043 }
1044
1045 /**
1046 * Test that if membership start date is not set it defaults to correct end date.
1047 * - fixed
1048 */
1049 public function testEmptyStartDateFixed() {
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 }
1058
1059 /**
1060 * Test that if membership start date is not set it defaults to correct end date
1061 * - fixed
1062 */
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 /**
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
1088 /**
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.
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 */
1126 public function testFixedMultiYearDateSetTwoEmptyEndDate() {
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 /**
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.
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 */
1187 public function testFixedSingleYearDateSetTwoEmptyEndDate() {
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 /**
1211 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
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
1215 * and we add on 1 year we are after the rollover day - so we calculate 31 Oct 2016
1216 */
1217 public function testFixedSingleYearDateSetThreeEmptyEndDate() {
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
1240 /**
1241 * Test correct end and start dates are calculated for fixed multi year memberships.
1242 *
1243 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
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 }
1270
1271 /**
1272 * Test that correct end date is calculated for fixed multi year memberships and start date is not changed.
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 */
1278 public function testFixedMultiYearDateSetThreeEmptyEndDate() {
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 }
1300
1301 /**
1302 * Test correct end and start dates are calculated for fixed multi year memberships.
1303 *
1304 * The empty start date is calculated to be the start_date (1 Nov prior to the join_date - so 1 Nov 14)
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
1333 /**
1334 * Test that if membership start date is not set it defaults to correct end date for fixed single year memberships.
1335 */
1336 public function testEmptyStartDateRolling() {
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']);
1343 }
1344
1345 /**
1346 * Test that if membership end date is not set it defaults to correct end date.
1347 * - rolling
1348 */
1349 public function testEmptyEndDateFixed() {
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 }
1358
1359 /**
1360 * Test that if membership end date is not set it defaults to correct end date.
1361 * - rolling
1362 */
1363 public function testEmptyEndDateRolling() {
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 /**
1375 * Test that if dates are set they not over-ridden if id is passed in
1376 */
1377 public function testMembershipDatesNotOverridden() {
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
1387 }
1388
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
1405 }