3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 require_once 'CiviTest/CiviUnitTestCase.php';
30 require_once 'CiviTest/Contact.php';
31 require_once 'CiviTest/Custom.php';
32 require_once 'CiviTest/ContributionPage.php';
33 require_once 'CiviTest/Membership.php';
34 require_once 'CRM/Core/Controller.php';
37 * Class CRM_Member_BAO_MembershipTest
39 class CRM_Member_BAO_MembershipTest
extends CiviUnitTestCase
{
45 'name' => 'Membership BAOs',
46 'description' => 'Test all Member_BAO_Membership methods.',
47 'group' => 'CiviCRM BAO Tests',
53 // FIXME: something NULLs $GLOBALS['_HTML_QuickForm_registered_rules'] when the tests are ran all together
54 $GLOBALS['_HTML_QuickForm_registered_rules'] = array(
55 'required' => array('html_quickform_rule_required', 'HTML/QuickForm/Rule/Required.php'),
56 'maxlength' => array('html_quickform_rule_range', 'HTML/QuickForm/Rule/Range.php'),
57 'minlength' => array('html_quickform_rule_range', 'HTML/QuickForm/Rule/Range.php'),
58 'rangelength' => array('html_quickform_rule_range', 'HTML/QuickForm/Rule/Range.php'),
59 'email' => array('html_quickform_rule_email', 'HTML/QuickForm/Rule/Email.php'),
60 'regex' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'),
61 'lettersonly' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'),
62 'alphanumeric' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'),
63 'numeric' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'),
64 'nopunctuation' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'),
65 'nonzero' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'),
66 'callback' => array('html_quickform_rule_callback', 'HTML/QuickForm/Rule/Callback.php'),
67 'compare' => array('html_quickform_rule_compare', 'HTML/QuickForm/Rule/Compare.php'),
70 $this->_contactID
= $this->organizationCreate();
71 $this->_membershipTypeID
= $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID
));
72 // add a random number to avoid silly conflicts with old data
73 $this->_membershipStatusID
= $this->membershipStatusCreate('test status' . rand(1, 1000));
77 * Tears down the fixture, for example, closes a network connection.
78 * This method is called after a test is executed.
82 $this->membershipTypeDelete( array( 'id' => $this->_membershipTypeID
) );
83 $this->membershipStatusDelete( $this->_membershipStatusID
);
84 Contact
::delete( $this->_contactID
);
86 $this->_contactID
= $this->_membershipStatusID
= $this->_membershipTypeID
= null;
89 function testCreate() {
91 $contactId = Contact
::createIndividual();
94 'contact_id' => $contactId,
95 'membership_type_id' => $this->_membershipTypeID
,
96 'join_date' => date('Ymd',strtotime('2006-01-21')),
97 'start_date' => date('Ymd',strtotime('2006-01-21')),
98 'end_date' => date('Ymd',strtotime('2006-12-21')),
99 'source' => 'Payment',
101 'status_id' => $this->_membershipStatusID
,
104 CRM_Member_BAO_Membership
::create($params, $ids);
106 $membershipId = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
107 'contact_id', 'Database check for created membership.'
110 // Now call create() to modify an existing Membership
114 'contact_id' => $contactId,
115 'membership_type_id' => $this->_membershipTypeID
,
116 'join_date' => date('Ymd',strtotime('2006-01-21')),
117 'start_date' => date('Ymd',strtotime('2006-01-21')),
118 'end_date' => date('Ymd',strtotime('2006-12-21')),
119 'source' => 'Payment',
121 'status_id' => $this->_membershipStatusID
,
124 'membership' => $membershipId,
126 CRM_Member_BAO_Membership
::create($params, $ids);
128 $membershipTypeId = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId,
129 'membership_type_id', 'contact_id',
130 'Database check on updated membership record.'
132 $this->assertEquals($membershipTypeId, $this->_membershipTypeID
, 'Verify membership type id is fetched.');
134 $this->membershipDelete($membershipId);
135 Contact
::delete($contactId);
138 function testGetValues() {
139 // $this->markTestSkipped( 'causes mysterious exit, needs fixing!' );
140 // Calculate membership dates based on the current date
142 $year_from_now = $now +
(365 * 24 * 60 * 60);
143 $last_month = $now - (30 * 24 * 60 * 60);
144 $year_from_last_month = $last_month +
(365 * 24 * 60 * 60);
146 $contactId = Contact
::createIndividual();
149 'contact_id' => $contactId,
150 'membership_type_id' => $this->_membershipTypeID
,
151 'join_date' => date('Ymd'),
152 'start_date' => date('Ymd'),
153 'end_date' => date('Ymd', $year_from_now),
154 'source' => 'Payment',
156 'status_id' => $this->_membershipStatusID
,
160 CRM_Member_BAO_Membership
::create($params, $ids);
162 $membershipId1 = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
163 'contact_id', 'Database check for created membership.'
167 'contact_id' => $contactId,
168 'membership_type_id' => $this->_membershipTypeID
,
169 'join_date' => date('Ymd', $last_month),
170 'start_date' => date('Ymd', $last_month),
171 'end_date' => date('Ymd', $year_from_last_month),
172 'source' => 'Source123',
174 'status_id' => $this->_membershipStatusID
,
177 CRM_Member_BAO_Membership
::create($params, $ids);
179 $membershipId2 = $this->assertDBNotNull('CRM_Member_BAO_Membership', 'source123', 'id',
180 'source', 'Database check for created membership.'
183 $membership = array('contact_id' => $contactId);
184 $membershipValues = array();
185 CRM_Member_BAO_Membership
::getValues($membership, $membershipValues, TRUE);
187 $this->assertEquals($membershipValues[$membershipId1]['membership_id'], $membershipId1, 'Verify membership record 1 is fetched.');
189 $this->assertEquals($membershipValues[$membershipId2]['membership_id'], $membershipId2, 'Verify membership record 2 is fetched.');
191 $this->membershipDelete($membershipId1);
192 $this->membershipDelete($membershipId2);
193 Contact
::delete($contactId);
196 function testRetrieve() {
197 $contactId = Contact
::createIndividual();
200 'contact_id' => $contactId,
201 'membership_type_id' => $this->_membershipTypeID
,
202 'join_date' => date('Ymd',strtotime('2006-01-21')),
203 'start_date' => date('Ymd',strtotime('2006-01-21')),
204 'end_date' => date('Ymd',strtotime('2006-12-21')),
205 'source' => 'Payment',
207 'status_id' => $this->_membershipStatusID
,
210 CRM_Member_BAO_Membership
::create($params, $ids);
212 $membershipId = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
213 'contact_id', 'Database check for created membership.'
215 $params = array('id' => $membershipId);
217 CRM_Member_BAO_Membership
::retrieve($params, $values);
218 $this->assertEquals($values['id'], $membershipId, 'Verify membership record is retrieved.');
220 $this->membershipDelete($membershipId);
221 $this->contactDelete($contactId);
224 function testActiveMembers() {
225 $contactId = Contact
::createIndividual();
228 'contact_id' => $contactId,
229 'membership_type_id' => $this->_membershipTypeID
,
230 'join_date' => date('Ymd',strtotime('2006-01-21')),
231 'start_date' => date('Ymd',strtotime('2006-01-21')),
232 'end_date' => date('Ymd',strtotime('2006-12-21')),
233 'source' => 'Payment',
235 'status_id' => $this->_membershipStatusID
,
238 CRM_Member_BAO_Membership
::create($params, $ids);
240 $membershipId1 = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
241 'contact_id', 'Database check for created membership.'
244 $params = array('id' => $membershipId1);
246 CRM_Member_BAO_Membership
::retrieve($params, $values1);
247 $membership = array($membershipId1 => $values1);
250 'contact_id' => $contactId,
251 'membership_type_id' => $this->_membershipTypeID
,
252 'join_date' => date('Ymd',strtotime('2006-01-21')),
253 'start_date' => date('Ymd',strtotime('2006-01-21')),
254 'end_date' => date('Ymd',strtotime('2006-12-21')),
255 'source' => 'PaySource',
257 'status_id' => $this->_membershipStatusID
,
260 CRM_Member_BAO_Membership
::create($params, $ids);
262 $membershipId2 = $this->assertDBNotNull('CRM_Member_BAO_Membership', 'PaySource', 'id',
263 'source', 'Database check for created membership.'
266 $params = array('id' => $membershipId2);
268 CRM_Member_BAO_Membership
::retrieve($params, $values2);
269 $membership[$membershipId2] = $values2;
271 $activeMembers = CRM_Member_BAO_Membership
::activeMembers($membership);
272 $inActiveMembers = CRM_Member_BAO_Membership
::activeMembers($membership, 'inactive');
274 $this->assertEquals($activeMembers[$membershipId1]['id'], $membership[$membershipId1]['id'], 'Verify active membership record is retrieved.');
275 $this->assertEquals($activeMembers[$membershipId2]['id'], $membership[$membershipId2]['id'], 'Verify active membership record is retrieved.');
277 $this->assertEquals(0, count($inActiveMembers), 'Verify No inactive membership record is retrieved.');
279 $this->membershipDelete($membershipId1);
280 $this->membershipDelete($membershipId2);
281 Contact
::delete($contactId);
284 function testDeleteMembership() {
285 $contactId = Contact
::createIndividual();
288 'contact_id' => $contactId,
289 'membership_type_id' => $this->_membershipTypeID
,
290 'join_date' => date('Ymd',strtotime('2006-01-21')),
291 'start_date' => date('Ymd',strtotime('2006-01-21')),
292 'end_date' => date('Ymd',strtotime('2006-12-21')),
293 'source' => 'Payment',
295 'status_id' => $this->_membershipStatusID
,
298 CRM_Member_BAO_Membership
::create($params, $ids);
300 $membershipId = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
301 'contact_id', 'Database check for created membership.'
303 CRM_Member_BAO_Membership
::del($membershipId);
305 $membershipId = $this->assertDBNull('CRM_Member_BAO_Membership', $contactId, 'id',
306 'contact_id', 'Database check for deleted membership.'
308 Contact
::delete($contactId);
311 function testGetContactMembership() {
312 $contactId = Contact
::createIndividual();
315 'contact_id' => $contactId,
316 'membership_type_id' => $this->_membershipTypeID
,
317 'join_date' => date('Ymd',strtotime('2006-01-21')),
318 'start_date' => date('Ymd',strtotime('2006-01-21')),
319 'end_date' => date('Ymd',strtotime('2006-12-21')),
320 'source' => 'Payment',
322 'status_id' => $this->_membershipStatusID
,
325 CRM_Member_BAO_Membership
::create($params, $ids);
327 $membershipId = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
328 'contact_id', 'Database check for created membership.'
331 $membership = CRM_Member_BAO_Membership
::getContactMembership($contactId, $this->_membershipTypeID
, FALSE);
333 $this->assertEquals($membership['id'], $membershipId, 'Verify membership record is retrieved.');
335 $this->membershipDelete($membershipId);
336 Contact
::delete($contactId);
341 * Get the contribution
342 * page id from the membership record
344 function testgetContributionPageId() {
345 $contactId = Contact
::createIndividual();
348 'contact_id' => $contactId,
349 'membership_type_id' => $this->_membershipTypeID
,
350 'join_date' => date('Ymd',strtotime('2006-01-21')),
351 'start_date' => date('Ymd',strtotime('2006-01-21')),
352 'end_date' => date('Ymd',strtotime('2006-12-21')),
353 'source' => 'Payment',
355 'status_id' => $this->_membershipStatusID
,
358 CRM_Member_BAO_Membership
::create($params, $ids);
361 $membershipId = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
362 'contact_id', 'Database check for created membership.'
364 $membership[$membershipId]['renewPageId'] = CRM_Member_BAO_Membership
::getContributionPageId($membershipId);
366 $this->membershipDelete($membershipId);
367 Contact
::delete($contactId);
370 * Get membership joins/renewals
371 * for a specified membership
375 function testgetMembershipStarts() {
376 $contactId = Contact
::createIndividual();
379 'contact_id' => $contactId,
380 'membership_type_id' => $this->_membershipTypeID
,
381 'join_date' => date('Ymd',strtotime('2006-01-21')),
382 'start_date' => date('Ymd',strtotime('2006-01-21')),
383 'end_date' => date('Ymd',strtotime('2006-12-21')),
384 'source' => 'Payment',
386 'status_id' => $this->_membershipStatusID
,
389 CRM_Member_BAO_Membership
::create($params, $ids);
391 $membershipId = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
392 'contact_id', 'Database check for created membership.'
394 $yearStart = date('Y') . '0101';
395 $currentDate = date('Ymd');
396 CRM_Member_BAO_Membership
::getMembershipStarts($this->_membershipTypeID
, $yearStart, $currentDate);
398 $this->membershipDelete($membershipId);
399 Contact
::delete($contactId);
403 * Get a count of membership for a specified membership type,
404 * optionally for a specified date.
407 function testGetMembershipCount() {
408 $contactId = Contact
::createIndividual();
411 'contact_id' => $contactId,
412 'membership_type_id' => $this->_membershipTypeID
,
413 'join_date' => date('Ymd',strtotime('2006-01-21')),
414 'start_date' => date('Ymd',strtotime('2006-01-21')),
415 'end_date' => date('Ymd',strtotime('2006-12-21')),
416 'source' => 'Payment',
418 'status_id' => $this->_membershipStatusID
,
421 CRM_Member_BAO_Membership
::create($params, $ids);
423 $membershipId = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
424 'contact_id', 'Database check for created membership.'
426 $currentDate = date('Ymd');
428 CRM_Member_BAO_Membership
::getMembershipCount($this->_membershipTypeID
, $currentDate, $test);
430 $this->membershipDelete($membershipId);
431 Contact
::delete($contactId);
436 * Take sort name of contact during
437 * batch update member via profile
440 function testsortName() {
441 $contactId = Contact
::createIndividual();
444 'contact_id' => $contactId,
445 'membership_type_id' => $this->_membershipTypeID
,
446 'join_date' => date('Ymd',strtotime('2006-01-21')),
447 'start_date' => date('Ymd',strtotime('2006-01-21')),
448 'end_date' => date('Ymd',strtotime('2006-12-21')),
449 'source' => 'Payment',
451 'status_id' => $this->_membershipStatusID
,
455 CRM_Member_BAO_Membership
::create($params, $ids);
457 $membershipId = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
458 'contact_id', 'Database check for created membership.'
461 CRM_Member_BAO_Membership
::sortName($membershipId);
463 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'sort_name', 'id', 'Doe, John',
464 'Database check for sort name record.'
467 $this->membershipDelete($membershipId);
468 Contact
::delete($contactId);
472 * Delete related memberships
475 function testdeleteRelatedMemberships() {
476 $contactId = Contact
::createIndividual();
479 'contact_id' => $contactId,
480 'membership_type_id' => $this->_membershipTypeID
,
481 'join_date' => date('Ymd',strtotime('2006-01-21')),
482 'start_date' => date('Ymd',strtotime('2006-01-21')),
483 'end_date' => date('Ymd',strtotime('2006-12-21')),
484 'source' => 'Payment',
486 'status_id' => $this->_membershipStatusID
,
490 CRM_Member_BAO_Membership
::create($params, $ids);
492 $membershipId = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
493 'contact_id', 'Database check for created membership.'
496 CRM_Member_BAO_Membership
::deleteRelatedMemberships($membershipId);
498 $this->membershipDelete($membershipId);
499 Contact
::delete($contactId);
503 * Renew membership with change in membership type
506 function testRenewMembership() {
507 $contactId = Contact
::createIndividual();
508 $joinDate = $startDate = date("Ymd", strtotime(date("Ymd") . " -6 month"));
509 $endDate = date("Ymd", strtotime($joinDate . " +1 year -1 day"));
511 'contact_id' => $contactId,
512 'membership_type_id' => $this->_membershipTypeID
,
513 'join_date' => $joinDate,
514 'start_date' => $startDate,
515 'end_date' => $endDate,
516 'source' => 'Payment',
518 'status_id' => $this->_membershipStatusID
,
521 $membership = CRM_Member_BAO_Membership
::create($params, $ids);
522 $membershipId = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
523 'contact_id', 'Database check for created membership.'
526 $this->assertDBNotNull('CRM_Member_BAO_MembershipLog',
530 'Database checked on membershiplog record.'
533 // this is a test and we dont want qfKey generation / validation
534 // easier to suppress it, than change core code
535 $config = CRM_Core_Config
::singleton();
536 $config->keyDisable
= TRUE;
538 $membershipRenewal = new CRM_Core_Form
;
539 $membershipRenewal->controller
= new CRM_Core_Controller
;
540 $isTestMembership = 0;
542 CRM_Member_BAO_Membership
::renewMembershipFormWrapper(
544 $this->_membershipTypeID
,
550 $endDate = date("Y-m-d", strtotime($membership->end_date
. " +1 year"));
552 $this->assertDBNotNull('CRM_Member_BAO_MembershipLog',
553 $MembershipRenew->id
,
556 'Database checked on membershiplog record.'
558 $this->assertEquals($this->_membershipTypeID
, $MembershipRenew->membership_type_id
, 'Verify membership type is changed during renewal.');
559 $this->assertEquals($endDate, $MembershipRenew->end_date
, 'Verify correct end date is calculated after membership renewal');
561 $this->membershipDelete($membershipId);
562 Contact
::delete($contactId);
566 * Renew stale membership
569 function testStaleMembership() {
571 $contactId = Contact
::createIndividual();
572 $joinDate = $startDate = date("Ymd", strtotime(date("Ymd") . " -1 year -15 days"));
573 $endDate = date("Ymd", strtotime($joinDate . " +1 year -1 day"));
575 'contact_id' => $contactId,
576 'membership_type_id' => $this->_membershipTypeID
,
577 'join_date' => $joinDate,
578 'start_date' => $startDate,
579 'end_date' => $endDate,
580 'source' => 'Payment',
581 'status_id' => $statusId,
585 $membership = CRM_Member_BAO_Membership
::create($params, $ids);
587 $membershipId = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
588 'contact_id', 'Database check for created membership.'
591 $this->assertEquals($membership->status_id
, $statusId, 'Verify correct status id is calculated.');
592 $this->assertEquals($membership->membership_type_id
, $this->_membershipTypeID
,
593 'Verify correct membership type id.'
598 'startDate' => 'start_date',
599 'joinDate' => 'join_date',
600 'endDate' => 'end_date',
603 foreach ($dates as $date => $dbDate) {
604 $this->assertEquals($membership->$dbDate, $
$date,
605 "Verify correct {$date} is present."
609 $this->assertDBNotNull('CRM_Member_BAO_MembershipLog',
613 'Database checked on membershiplog record.'
616 // this is a test and we dont want qfKey generation / validation
617 // easier to suppress it, than change core code
618 $config = CRM_Core_Config
::singleton();
619 $config->keyDisable
= TRUE;
621 $membershipRenewal = new CRM_Core_Form
;
622 $membershipRenewal->controller
= new CRM_Core_Controller
;
623 $MembershipRenew = CRM_Member_BAO_Membership
::renewMembershipFormWrapper($contactId, $this->_membershipTypeID
, $isTestMembership = 0, $membershipRenewal, NULL, NULL);
625 $this->assertDBNotNull('CRM_Member_BAO_MembershipLog',
626 $MembershipRenew->id
,
629 'Database checked on membershiplog record.'
632 $this->membershipDelete($membershipId);
633 Contact
::delete($contactId);