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
{
41 public function setUp() {
43 // FIXME: something NULLs $GLOBALS['_HTML_QuickForm_registered_rules'] when the tests are ran all together
44 $GLOBALS['_HTML_QuickForm_registered_rules'] = array(
45 'required' => array('html_quickform_rule_required', 'HTML/QuickForm/Rule/Required.php'),
46 'maxlength' => array('html_quickform_rule_range', 'HTML/QuickForm/Rule/Range.php'),
47 'minlength' => array('html_quickform_rule_range', 'HTML/QuickForm/Rule/Range.php'),
48 'rangelength' => array('html_quickform_rule_range', 'HTML/QuickForm/Rule/Range.php'),
49 'email' => array('html_quickform_rule_email', 'HTML/QuickForm/Rule/Email.php'),
50 'regex' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'),
51 'lettersonly' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'),
52 'alphanumeric' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'),
53 'numeric' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'),
54 'nopunctuation' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'),
55 'nonzero' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'),
56 'callback' => array('html_quickform_rule_callback', 'HTML/QuickForm/Rule/Callback.php'),
57 'compare' => array('html_quickform_rule_compare', 'HTML/QuickForm/Rule/Compare.php'),
60 $this->_contactID
= $this->organizationCreate();
61 $this->_membershipTypeID
= $this->membershipTypeCreate(array('member_of_contact_id' => $this->_contactID
));
62 // add a random number to avoid silly conflicts with old data
63 $this->_membershipStatusID
= $this->membershipStatusCreate('test status' . rand(1, 1000));
67 * Tears down the fixture, for example, closes a network connection.
68 * This method is called after a test is executed.
71 public function tearDown() {
72 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID
));
73 $this->membershipStatusDelete($this->_membershipStatusID
);
74 Contact
::delete($this->_contactID
);
76 $this->_contactID
= $this->_membershipStatusID
= $this->_membershipTypeID
= NULL;
79 public function testCreate() {
81 $contactId = Contact
::createIndividual();
84 'contact_id' => $contactId,
85 'membership_type_id' => $this->_membershipTypeID
,
86 'join_date' => date('Ymd', strtotime('2006-01-21')),
87 'start_date' => date('Ymd', strtotime('2006-01-21')),
88 'end_date' => date('Ymd', strtotime('2006-12-21')),
89 'source' => 'Payment',
91 'status_id' => $this->_membershipStatusID
,
94 CRM_Member_BAO_Membership
::create($params, $ids);
96 $membershipId = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
97 'contact_id', 'Database check for created membership.'
100 // Now call create() to modify an existing Membership
104 'contact_id' => $contactId,
105 'membership_type_id' => $this->_membershipTypeID
,
106 'join_date' => date('Ymd', strtotime('2006-01-21')),
107 'start_date' => date('Ymd', strtotime('2006-01-21')),
108 'end_date' => date('Ymd', strtotime('2006-12-21')),
109 'source' => 'Payment',
111 'status_id' => $this->_membershipStatusID
,
114 'membership' => $membershipId,
116 CRM_Member_BAO_Membership
::create($params, $ids);
118 $membershipTypeId = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId,
119 'membership_type_id', 'contact_id',
120 'Database check on updated membership record.'
122 $this->assertEquals($membershipTypeId, $this->_membershipTypeID
, 'Verify membership type id is fetched.');
124 $this->membershipDelete($membershipId);
125 Contact
::delete($contactId);
128 public function testGetValues() {
129 // $this->markTestSkipped( 'causes mysterious exit, needs fixing!' );
130 // Calculate membership dates based on the current date
132 $year_from_now = $now +
(365 * 24 * 60 * 60);
133 $last_month = $now - (30 * 24 * 60 * 60);
134 $year_from_last_month = $last_month +
(365 * 24 * 60 * 60);
136 $contactId = Contact
::createIndividual();
139 'contact_id' => $contactId,
140 'membership_type_id' => $this->_membershipTypeID
,
141 'join_date' => date('Ymd'),
142 'start_date' => date('Ymd'),
143 'end_date' => date('Ymd', $year_from_now),
144 'source' => 'Payment',
146 'status_id' => $this->_membershipStatusID
,
150 CRM_Member_BAO_Membership
::create($params, $ids);
152 $membershipId1 = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
153 'contact_id', 'Database check for created membership.'
157 'contact_id' => $contactId,
158 'membership_type_id' => $this->_membershipTypeID
,
159 'join_date' => date('Ymd', $last_month),
160 'start_date' => date('Ymd', $last_month),
161 'end_date' => date('Ymd', $year_from_last_month),
162 'source' => 'Source123',
164 'status_id' => $this->_membershipStatusID
,
167 CRM_Member_BAO_Membership
::create($params, $ids);
169 $membershipId2 = $this->assertDBNotNull('CRM_Member_BAO_Membership', 'source123', 'id',
170 'source', 'Database check for created membership.'
173 $membership = array('contact_id' => $contactId);
174 $membershipValues = array();
175 CRM_Member_BAO_Membership
::getValues($membership, $membershipValues, TRUE);
177 $this->assertEquals($membershipValues[$membershipId1]['membership_id'], $membershipId1, 'Verify membership record 1 is fetched.');
179 $this->assertEquals($membershipValues[$membershipId2]['membership_id'], $membershipId2, 'Verify membership record 2 is fetched.');
181 $this->membershipDelete($membershipId1);
182 $this->membershipDelete($membershipId2);
183 Contact
::delete($contactId);
186 public function testRetrieve() {
187 $contactId = Contact
::createIndividual();
190 'contact_id' => $contactId,
191 'membership_type_id' => $this->_membershipTypeID
,
192 'join_date' => date('Ymd', strtotime('2006-01-21')),
193 'start_date' => date('Ymd', strtotime('2006-01-21')),
194 'end_date' => date('Ymd', strtotime('2006-12-21')),
195 'source' => 'Payment',
197 'status_id' => $this->_membershipStatusID
,
200 CRM_Member_BAO_Membership
::create($params, $ids);
202 $membershipId = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
203 'contact_id', 'Database check for created membership.'
205 $params = array('id' => $membershipId);
207 CRM_Member_BAO_Membership
::retrieve($params, $values);
208 $this->assertEquals($values['id'], $membershipId, 'Verify membership record is retrieved.');
210 $this->membershipDelete($membershipId);
211 $this->contactDelete($contactId);
214 public function testActiveMembers() {
215 $contactId = Contact
::createIndividual();
218 'contact_id' => $contactId,
219 'membership_type_id' => $this->_membershipTypeID
,
220 'join_date' => date('Ymd', strtotime('2006-01-21')),
221 'start_date' => date('Ymd', strtotime('2006-01-21')),
222 'end_date' => date('Ymd', strtotime('2006-12-21')),
223 'source' => 'Payment',
225 'status_id' => $this->_membershipStatusID
,
228 CRM_Member_BAO_Membership
::create($params, $ids);
230 $membershipId1 = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
231 'contact_id', 'Database check for created membership.'
234 $params = array('id' => $membershipId1);
236 CRM_Member_BAO_Membership
::retrieve($params, $values1);
237 $membership = array($membershipId1 => $values1);
240 'contact_id' => $contactId,
241 'membership_type_id' => $this->_membershipTypeID
,
242 'join_date' => date('Ymd', strtotime('2006-01-21')),
243 'start_date' => date('Ymd', strtotime('2006-01-21')),
244 'end_date' => date('Ymd', strtotime('2006-12-21')),
245 'source' => 'PaySource',
247 'status_id' => $this->_membershipStatusID
,
250 CRM_Member_BAO_Membership
::create($params, $ids);
252 $membershipId2 = $this->assertDBNotNull('CRM_Member_BAO_Membership', 'PaySource', 'id',
253 'source', 'Database check for created membership.'
256 $params = array('id' => $membershipId2);
258 CRM_Member_BAO_Membership
::retrieve($params, $values2);
259 $membership[$membershipId2] = $values2;
261 $activeMembers = CRM_Member_BAO_Membership
::activeMembers($membership);
262 $inActiveMembers = CRM_Member_BAO_Membership
::activeMembers($membership, 'inactive');
264 $this->assertEquals($activeMembers[$membershipId1]['id'], $membership[$membershipId1]['id'], 'Verify active membership record is retrieved.');
265 $this->assertEquals($activeMembers[$membershipId2]['id'], $membership[$membershipId2]['id'], 'Verify active membership record is retrieved.');
267 $this->assertEquals(0, count($inActiveMembers), 'Verify No inactive membership record is retrieved.');
269 $this->membershipDelete($membershipId1);
270 $this->membershipDelete($membershipId2);
271 Contact
::delete($contactId);
274 public function testDeleteMembership() {
275 $contactId = Contact
::createIndividual();
278 'contact_id' => $contactId,
279 'membership_type_id' => $this->_membershipTypeID
,
280 'join_date' => date('Ymd', strtotime('2006-01-21')),
281 'start_date' => date('Ymd', strtotime('2006-01-21')),
282 'end_date' => date('Ymd', strtotime('2006-12-21')),
283 'source' => 'Payment',
285 'status_id' => $this->_membershipStatusID
,
288 CRM_Member_BAO_Membership
::create($params, $ids);
290 $membershipId = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
291 'contact_id', 'Database check for created membership.'
293 CRM_Member_BAO_Membership
::del($membershipId);
295 $membershipId = $this->assertDBNull('CRM_Member_BAO_Membership', $contactId, 'id',
296 'contact_id', 'Database check for deleted membership.'
298 Contact
::delete($contactId);
301 public function testGetContactMembership() {
302 $contactId = Contact
::createIndividual();
305 'contact_id' => $contactId,
306 'membership_type_id' => $this->_membershipTypeID
,
307 'join_date' => date('Ymd', strtotime('2006-01-21')),
308 'start_date' => date('Ymd', strtotime('2006-01-21')),
309 'end_date' => date('Ymd', strtotime('2006-12-21')),
310 'source' => 'Payment',
312 'status_id' => $this->_membershipStatusID
,
315 CRM_Member_BAO_Membership
::create($params, $ids);
317 $membershipId = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
318 'contact_id', 'Database check for created membership.'
321 $membership = CRM_Member_BAO_Membership
::getContactMembership($contactId, $this->_membershipTypeID
, FALSE);
323 $this->assertEquals($membership['id'], $membershipId, 'Verify membership record is retrieved.');
325 $this->membershipDelete($membershipId);
326 Contact
::delete($contactId);
331 * Get the contribution
332 * page id from the membership record
334 public function testgetContributionPageId() {
335 $contactId = Contact
::createIndividual();
338 'contact_id' => $contactId,
339 'membership_type_id' => $this->_membershipTypeID
,
340 'join_date' => date('Ymd', strtotime('2006-01-21')),
341 'start_date' => date('Ymd', strtotime('2006-01-21')),
342 'end_date' => date('Ymd', strtotime('2006-12-21')),
343 'source' => 'Payment',
345 'status_id' => $this->_membershipStatusID
,
348 CRM_Member_BAO_Membership
::create($params, $ids);
350 $membershipId = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
351 'contact_id', 'Database check for created membership.'
353 $membership[$membershipId]['renewPageId'] = CRM_Member_BAO_Membership
::getContributionPageId($membershipId);
355 $this->membershipDelete($membershipId);
356 Contact
::delete($contactId);
359 * Get membership joins/renewals
360 * for a specified membership
364 public function testgetMembershipStarts() {
365 $contactId = Contact
::createIndividual();
368 'contact_id' => $contactId,
369 'membership_type_id' => $this->_membershipTypeID
,
370 'join_date' => date('Ymd', strtotime('2006-01-21')),
371 'start_date' => date('Ymd', strtotime('2006-01-21')),
372 'end_date' => date('Ymd', strtotime('2006-12-21')),
373 'source' => 'Payment',
375 'status_id' => $this->_membershipStatusID
,
378 CRM_Member_BAO_Membership
::create($params, $ids);
380 $membershipId = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
381 'contact_id', 'Database check for created membership.'
383 $yearStart = date('Y') . '0101';
384 $currentDate = date('Ymd');
385 CRM_Member_BAO_Membership
::getMembershipStarts($this->_membershipTypeID
, $yearStart, $currentDate);
387 $this->membershipDelete($membershipId);
388 Contact
::delete($contactId);
392 * Get a count of membership for a specified membership type,
393 * optionally for a specified date.
396 public function testGetMembershipCount() {
397 $contactId = Contact
::createIndividual();
400 'contact_id' => $contactId,
401 'membership_type_id' => $this->_membershipTypeID
,
402 'join_date' => date('Ymd', strtotime('2006-01-21')),
403 'start_date' => date('Ymd', strtotime('2006-01-21')),
404 'end_date' => date('Ymd', strtotime('2006-12-21')),
405 'source' => 'Payment',
407 'status_id' => $this->_membershipStatusID
,
410 CRM_Member_BAO_Membership
::create($params, $ids);
412 $membershipId = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
413 'contact_id', 'Database check for created membership.'
415 $currentDate = date('Ymd');
417 CRM_Member_BAO_Membership
::getMembershipCount($this->_membershipTypeID
, $currentDate, $test);
419 $this->membershipDelete($membershipId);
420 Contact
::delete($contactId);
425 * Take sort name of contact during
426 * batch update member via profile
429 public function testsortName() {
430 $contactId = Contact
::createIndividual();
433 'contact_id' => $contactId,
434 'membership_type_id' => $this->_membershipTypeID
,
435 'join_date' => date('Ymd', strtotime('2006-01-21')),
436 'start_date' => date('Ymd', strtotime('2006-01-21')),
437 'end_date' => date('Ymd', strtotime('2006-12-21')),
438 'source' => 'Payment',
440 'status_id' => $this->_membershipStatusID
,
444 CRM_Member_BAO_Membership
::create($params, $ids);
446 $membershipId = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
447 'contact_id', 'Database check for created membership.'
450 CRM_Member_BAO_Membership
::sortName($membershipId);
452 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'sort_name', 'id', 'Doe, John',
453 'Database check for sort name record.'
456 $this->membershipDelete($membershipId);
457 Contact
::delete($contactId);
461 * Delete related memberships
464 public function testdeleteRelatedMemberships() {
465 $contactId = Contact
::createIndividual();
468 'contact_id' => $contactId,
469 'membership_type_id' => $this->_membershipTypeID
,
470 'join_date' => date('Ymd', strtotime('2006-01-21')),
471 'start_date' => date('Ymd', strtotime('2006-01-21')),
472 'end_date' => date('Ymd', strtotime('2006-12-21')),
473 'source' => 'Payment',
475 'status_id' => $this->_membershipStatusID
,
479 CRM_Member_BAO_Membership
::create($params, $ids);
481 $membershipId = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
482 'contact_id', 'Database check for created membership.'
485 CRM_Member_BAO_Membership
::deleteRelatedMemberships($membershipId);
487 $this->membershipDelete($membershipId);
488 Contact
::delete($contactId);
492 * Renew membership with change in membership type
495 public function testRenewMembership() {
496 $contactId = Contact
::createIndividual();
497 $joinDate = $startDate = date("Ymd", strtotime(date("Ymd") . " -6 month"));
498 $endDate = date("Ymd", strtotime($joinDate . " +1 year -1 day"));
500 'contact_id' => $contactId,
501 'membership_type_id' => $this->_membershipTypeID
,
502 'join_date' => $joinDate,
503 'start_date' => $startDate,
504 'end_date' => $endDate,
505 'source' => 'Payment',
507 'status_id' => $this->_membershipStatusID
,
510 $membership = CRM_Member_BAO_Membership
::create($params, $ids);
511 $membershipId = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
512 'contact_id', 'Database check for created membership.'
515 $this->assertDBNotNull('CRM_Member_BAO_MembershipLog',
519 'Database checked on membershiplog record.'
522 // this is a test and we dont want qfKey generation / validation
523 // easier to suppress it, than change core code
524 $config = CRM_Core_Config
::singleton();
525 $config->keyDisable
= TRUE;
527 $membershipRenewal = new CRM_Core_Form
;
528 $membershipRenewal->controller
= new CRM_Core_Controller
;
529 $isTestMembership = 0;
531 CRM_Member_BAO_Membership
::renewMembershipFormWrapper(
533 $this->_membershipTypeID
,
539 $endDate = date("Y-m-d", strtotime($membership->end_date
. " +1 year"));
541 $this->assertDBNotNull('CRM_Member_BAO_MembershipLog',
542 $MembershipRenew->id
,
545 'Database checked on membershiplog record.'
547 $this->assertEquals($this->_membershipTypeID
, $MembershipRenew->membership_type_id
, 'Verify membership type is changed during renewal.');
548 $this->assertEquals($endDate, $MembershipRenew->end_date
, 'Verify correct end date is calculated after membership renewal');
550 $this->membershipDelete($membershipId);
551 Contact
::delete($contactId);
555 * Renew stale membership
558 public function testStaleMembership() {
560 $contactId = Contact
::createIndividual();
561 $joinDate = $startDate = date("Ymd", strtotime(date("Ymd") . " -1 year -15 days"));
562 $endDate = date("Ymd", strtotime($joinDate . " +1 year -1 day"));
564 'contact_id' => $contactId,
565 'membership_type_id' => $this->_membershipTypeID
,
566 'join_date' => $joinDate,
567 'start_date' => $startDate,
568 'end_date' => $endDate,
569 'source' => 'Payment',
570 'status_id' => $statusId,
574 $membership = CRM_Member_BAO_Membership
::create($params, $ids);
576 $membershipId = $this->assertDBNotNull('CRM_Member_BAO_Membership', $contactId, 'id',
577 'contact_id', 'Database check for created membership.'
580 $this->assertEquals($membership->status_id
, $statusId, 'Verify correct status id is calculated.');
581 $this->assertEquals($membership->membership_type_id
, $this->_membershipTypeID
,
582 'Verify correct membership type id.'
587 'startDate' => 'start_date',
588 'joinDate' => 'join_date',
589 'endDate' => 'end_date',
592 foreach ($dates as $date => $dbDate) {
593 $this->assertEquals($membership->$dbDate, $
$date,
594 "Verify correct {$date} is present."
598 $this->assertDBNotNull('CRM_Member_BAO_MembershipLog',
602 'Database checked on membershiplog record.'
605 // this is a test and we dont want qfKey generation / validation
606 // easier to suppress it, than change core code
607 $config = CRM_Core_Config
::singleton();
608 $config->keyDisable
= TRUE;
610 $membershipRenewal = new CRM_Core_Form
;
611 $membershipRenewal->controller
= new CRM_Core_Controller
;
612 $MembershipRenew = CRM_Member_BAO_Membership
::renewMembershipFormWrapper($contactId, $this->_membershipTypeID
, $isTestMembership = 0, $membershipRenewal, NULL, NULL);
614 $this->assertDBNotNull('CRM_Member_BAO_MembershipLog',
615 $MembershipRenew->id
,
618 'Database checked on membershiplog record.'
621 $this->membershipDelete($membershipId);
622 Contact
::delete($contactId);