3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 * File for the MembershipTest class
33 * @author Walt Haas <walt@dharmatech.org> (801) 534-1262
37 * Include class definitions
39 require_once 'CiviTest/CiviUnitTestCase.php';
41 require_once 'HTML/QuickForm/Page.php';
44 * Test CRM_Member_Form_Membership functions.
48 class CRM_Member_Form_MembershipTest
extends CiviUnitTestCase
{
51 * Assume empty database with just civicrm_data.
53 protected $_individualId;
54 protected $_contribution;
55 protected $_financialTypeId = 1;
56 protected $_apiversion;
57 protected $_entity = 'Membership';
59 protected $_ids = array();
60 protected $_paymentProcessorID;
63 * Membership type ID for annual fixed membership.
67 protected $membershipTypeAnnualFixedID;
70 * Parameters to create payment processor.
74 protected $_processorParams = array();
77 * ID of created membership.
81 protected $_membershipID;
84 * Payment instrument mapping.
88 protected $paymentInstruments = array();
91 * Test setup for every test.
93 * Connect to the database, truncate the tables that will be used
94 * and redirect stdin to a temporary file.
96 public function setUp() {
97 $this->_apiversion
= 3;
100 $this->_individualId
= $this->individualCreate();
101 $this->_paymentProcessorID
= $this->processorCreate();
103 $op = new PHPUnit_Extensions_Database_Operation_Insert();
104 $op->execute($this->_dbconn
,
105 $this->createFlatXMLDataSet(
106 dirname(__FILE__
) . '/dataset/data.xml'
109 $membershipTypeAnnualFixed = $this->callAPISuccess('membership_type', 'create', array(
111 'name' => "AnnualFixed",
112 'member_of_contact_id' => 23,
113 'duration_unit' => "year",
114 'duration_interval' => 1,
115 'period_type' => "fixed",
116 'fixed_period_start_day' => "101",
117 'fixed_period_rollover_day' => "1231",
118 'relationship_type_id' => 20,
119 'financial_type_id' => 2,
121 $this->membershipTypeAnnualFixedID
= $membershipTypeAnnualFixed['id'];
123 $instruments = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
124 $this->paymentInstruments
= $instruments['values'];
128 * Clean up after each test.
130 public function tearDown() {
131 $this->quickCleanUpFinancialEntities();
134 'civicrm_relationship',
135 'civicrm_membership_type',
136 'civicrm_membership',
140 $this->callAPISuccess('contact', 'delete', array('id' => 17, 'skip_undelete' => TRUE));
141 $this->callAPISuccess('contact', 'delete', array('id' => 23, 'skip_undelete' => TRUE));
142 $this->callAPISuccess('relationship_type', 'delete', array('id' => 20));
146 * Test CRM_Member_Form_Membership::buildQuickForm()
148 //function testCRMMemberFormMembershipBuildQuickForm()
150 // throw new PHPUnit_Framework_IncompleteTestError( "not implemented" );
154 * Test CRM_Member_Form_Membership::formRule() with a parameter
155 * that has an empty contact_select_id value
157 public function testFormRuleEmptyContact() {
159 'contact_select_id' => 0,
160 'membership_type_id' => array(1 => NULL),
163 $obj = new CRM_Member_Form_Membership();
164 $rc = $obj->formRule($params, $files, $obj);
165 $this->assertType('array', $rc);
166 $this->assertTrue(array_key_exists('membership_type_id', $rc));
168 $params['membership_type_id'] = array(1 => 3);
169 $rc = $obj->formRule($params, $files, $obj);
170 $this->assertType('array', $rc);
171 $this->assertTrue(array_key_exists('join_date', $rc));
175 * Test that form rule fails if start date is before join date.
177 * Test CRM_Member_Form_Membership::formRule() with a parameter
178 * that has an start date before the join date and a rolling
181 public function testFormRuleRollingEarlyStart() {
183 $ymdNow = date('m/d/Y', $unixNow);
184 $unixYesterday = $unixNow - (24 * 60 * 60);
185 $ymdYesterday = date('m/d/Y', $unixYesterday);
187 'join_date' => $ymdNow,
188 'start_date' => $ymdYesterday,
190 'membership_type_id' => array('23', '15'),
193 $obj = new CRM_Member_Form_Membership();
194 $rc = call_user_func(array('CRM_Member_Form_Membership', 'formRule'),
195 $params, $files, $obj
197 $this->assertType('array', $rc);
198 $this->assertTrue(array_key_exists('start_date', $rc));
202 * Test CRM_Member_Form_Membership::formRule() with a parameter
203 * that has an end date before the start date and a rolling
206 public function testFormRuleRollingEarlyEnd() {
208 $ymdNow = date('m/d/Y', $unixNow);
209 $unixYesterday = $unixNow - (24 * 60 * 60);
210 $ymdYesterday = date('m/d/Y', $unixYesterday);
212 'join_date' => $ymdNow,
213 'start_date' => $ymdNow,
214 'end_date' => $ymdYesterday,
215 'membership_type_id' => array('23', '15'),
218 $obj = new CRM_Member_Form_Membership();
219 $rc = $obj->formRule($params, $files, $obj);
220 $this->assertType('array', $rc);
221 $this->assertTrue(array_key_exists('end_date', $rc));
225 * Test CRM_Member_Form_Membership::formRule() with end date but no start date and a rolling membership type.
227 public function testFormRuleRollingEndNoStart() {
229 $ymdNow = date('m/d/Y', $unixNow);
230 $unixYearFromNow = $unixNow +
(365 * 24 * 60 * 60);
231 $ymdYearFromNow = date('m/d/Y', $unixYearFromNow);
233 'join_date' => $ymdNow,
235 'end_date' => $ymdYearFromNow,
236 'membership_type_id' => array('23', '15'),
239 $obj = new CRM_Member_Form_Membership();
240 $rc = $obj->formRule($params, $files, $obj);
241 $this->assertType('array', $rc);
242 $this->assertTrue(array_key_exists('start_date', $rc));
246 * Test CRM_Member_Form_Membership::formRule() with a parameter
247 * that has an end date and a lifetime membership type
249 public function testFormRuleRollingLifetimeEnd() {
251 $unixYearFromNow = $unixNow +
(365 * 24 * 60 * 60);
253 'join_date' => date('m/d/Y', $unixNow),
254 'start_date' => date('m/d/Y', $unixNow),
255 'end_date' => date('m/d/Y',
258 'membership_type_id' => array('23', '25'),
261 $obj = new CRM_Member_Form_Membership();
262 $rc = $obj->formRule($params, $files, $obj);
263 $this->assertType('array', $rc);
264 $this->assertTrue(array_key_exists('status_id', $rc));
268 * Test CRM_Member_Form_Membership::formRule() with a parameter
269 * that has an override and no status
271 public function testFormRuleOverrideNoStatus() {
274 'join_date' => date('m/d/Y', $unixNow),
275 'membership_type_id' => array('23', '25'),
276 'is_override' => TRUE,
279 $obj = new CRM_Member_Form_Membership();
280 $rc = $obj->formRule($params, $files, $obj);
281 $this->assertType('array', $rc);
282 $this->assertTrue(array_key_exists('status_id', $rc));
286 * Test CRM_Member_Form_Membership::formRule() with a join date
287 * of one month from now and a rolling membership type
289 public function testFormRuleRollingJoin1MonthFromNow() {
291 $unix1MFmNow = $unixNow +
(31 * 24 * 60 * 60);
293 'join_date' => date('m/d/Y', $unix1MFmNow),
296 'membership_type_id' => array('23', '15'),
299 $obj = new CRM_Member_Form_Membership();
300 $rc = $obj->formRule($params, $files, $obj);
302 // Should have found no valid membership status.
303 $this->assertType('array', $rc);
304 $this->assertTrue(array_key_exists('_qf_default', $rc));
308 * Test CRM_Member_Form_Membership::formRule() with a join date of today and a rolling membership type.
310 public function testFormRuleRollingJoinToday() {
313 'join_date' => date('m/d/Y', $unixNow),
316 'membership_type_id' => array('23', '15'),
319 $obj = new CRM_Member_Form_Membership();
320 $rc = $obj->formRule($params, $files, $obj);
322 // Should have found New membership status
323 $this->assertTrue($rc);
327 * Test CRM_Member_Form_Membership::formRule() with a join date
328 * of one month ago and a rolling membership type
330 public function testFormRuleRollingJoin1MonthAgo() {
332 $unix1MAgo = $unixNow - (31 * 24 * 60 * 60);
334 'join_date' => date('m/d/Y', $unix1MAgo),
337 'membership_type_id' => array('23', '15'),
340 $obj = new CRM_Member_Form_Membership();
341 $rc = $obj->formRule($params, $files, $obj);
343 // Should have found New membership status.
344 $this->assertTrue($rc);
348 * Test CRM_Member_Form_Membership::formRule() with a join date of six months ago and a rolling membership type.
350 public function testFormRuleRollingJoin6MonthsAgo() {
352 $unix6MAgo = $unixNow - (180 * 24 * 60 * 60);
354 'join_date' => date('m/d/Y', $unix6MAgo),
357 'membership_type_id' => array('23', '15'),
360 $obj = new CRM_Member_Form_Membership();
361 $rc = $obj->formRule($params, $files, $obj);
363 // Should have found Current membership status.
364 $this->assertTrue($rc);
368 * Test CRM_Member_Form_Membership::formRule() with a join date
369 * of one year+ ago and a rolling membership type
371 public function testFormRuleRollingJoin1YearAgo() {
373 $unix1YAgo = $unixNow - (370 * 24 * 60 * 60);
375 'join_date' => date('m/d/Y', $unix1YAgo),
378 'membership_type_id' => array('23', '15'),
381 $obj = new CRM_Member_Form_Membership();
382 $rc = $obj->formRule($params, $files, $obj);
384 // Should have found Grace membership status
385 $this->assertTrue($rc);
389 * Test CRM_Member_Form_Membership::formRule() with a join date
390 * of two years ago and a rolling membership type
392 public function testFormRuleRollingJoin2YearsAgo() {
394 $unix2YAgo = $unixNow - (2 * 365 * 24 * 60 * 60);
396 'join_date' => date('m/d/Y', $unix2YAgo),
399 'membership_type_id' => array('23', '15'),
402 $obj = new CRM_Member_Form_Membership();
403 $rc = $obj->formRule($params, $files, $obj);
405 // Should have found Expired membership status
406 $this->assertTrue($rc);
410 * Test CRM_Member_Form_Membership::formRule() with a join date
411 * of six months ago and a fixed membership type
413 public function testFormRuleFixedJoin6MonthsAgo() {
415 $unix6MAgo = $unixNow - (180 * 24 * 60 * 60);
417 'join_date' => date('m/d/Y', $unix6MAgo),
420 'membership_type_id' => array('23', '7'),
423 $obj = new CRM_Member_Form_Membership();
424 $rc = $obj->formRule($params, $files, $obj);
426 // Should have found Current membership status
427 $this->assertTrue($rc);
431 * Test the submit function of the membership form.
433 public function testSubmit() {
434 $form = $this->getForm();
435 $form->_mode
= 'test';
436 $this->createLoggedInUser();
438 'cid' => $this->_individualId
,
439 'join_date' => date('m/d/Y', time()),
442 // This format reflects the 23 being the organisation & the 25 being the type.
443 'membership_type_id' => array(23, $this->membershipTypeAnnualFixedID
),
448 'total_amount' => '50.00',
449 'financial_type_id' => '2', //Member dues, see data.xml
450 'soft_credit_type_id' => '',
451 'soft_credit_contact_id' => '',
452 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
453 'receipt_text_signup' => 'Thank you text',
454 'payment_processor_id' => $this->_paymentProcessorID
,
455 'credit_card_number' => '4111111111111111',
457 'credit_card_exp_date' => array(
459 'Y' => '2024', // TODO: Future proof
461 'credit_card_type' => 'Visa',
462 'billing_first_name' => 'Test',
463 'billing_middlename' => 'Last',
464 'billing_street_address-5' => '10 Test St',
465 'billing_city-5' => 'Test',
466 'billing_state_province_id-5' => '1003',
467 'billing_postal_code-5' => '90210',
468 'billing_country_id-5' => '1228',
470 $form->_contactID
= $this->_individualId
;
471 $form->testSubmit($params);
472 $membership = $this->callAPISuccessGetSingle('Membership', array('contact_id' => $this->_individualId
));
473 $this->callAPISuccessGetCount('ContributionRecur', array('contact_id' => $this->_individualId
), 0);
474 $contribution = $this->callAPISuccess('Contribution', 'get', array(
475 'contact_id' => $this->_individualId
,
479 $this->callAPISuccessGetCount('LineItem', array(
480 'entity_id' => $membership['id'],
481 'entity_table' => 'civicrm_membership',
482 'contribution_id' => $contribution['id'],
487 * Test the submit function of the membership form.
489 public function testSubmitRecur() {
490 $form = $this->getForm();
492 $this->callAPISuccess('MembershipType', 'create', array(
493 'id' => $this->membershipTypeAnnualFixedID
,
494 'duration_unit' => 'month',
495 'duration_interval' => 1,
496 'auto_renew' => TRUE,
499 $this->createLoggedInUser();
500 $params = $this->getBaseSubmitParams();
501 $form->_mode
= 'test';
502 $form->_contactID
= $this->_individualId
;
503 $form->testSubmit($params);
504 $membership = $this->callAPISuccessGetSingle('Membership', array('contact_id' => $this->_individualId
));
505 $this->callAPISuccessGetCount('ContributionRecur', array('contact_id' => $this->_individualId
), 1);
507 $contribution = $this->callAPISuccess('Contribution', 'get', array(
508 'contact_id' => $this->_individualId
,
513 $this->callAPISuccessGetCount('LineItem', array(
514 'entity_id' => $membership['id'],
515 'entity_table' => 'civicrm_membership',
516 'contribution_id' => $contribution['id'],
521 * Test the submit function of the membership form.
523 public function testSubmitPayLaterWithBilling() {
524 $form = $this->getForm(NULL);
525 $this->createLoggedInUser();
527 'cid' => $this->_individualId
,
528 'join_date' => date('m/d/Y', time()),
531 // This format reflects the 23 being the organisation & the 25 being the type.
532 'membership_type_id' => array(23, $this->membershipTypeAnnualFixedID
),
537 'total_amount' => '50.00',
538 //Member dues, see data.xml
539 'financial_type_id' => '2',
540 'soft_credit_type_id' => '',
541 'soft_credit_contact_id' => '',
542 'payment_instrument_id' => 4,
543 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
544 'receipt_text_signup' => 'Thank you text',
545 'payment_processor_id' => $this->_paymentProcessorID
,
546 'record_contribution' => TRUE,
548 'contribution_status_id' => 2,
549 'billing_first_name' => 'Test',
550 'billing_middlename' => 'Last',
551 'billing_street_address-5' => '10 Test St',
552 'billing_city-5' => 'Test',
553 'billing_state_province_id-5' => '1003',
554 'billing_postal_code-5' => '90210',
555 'billing_country_id-5' => '1228',
557 $form->_contactID
= $this->_individualId
;
559 $form->testSubmit($params);
560 $membership = $this->callAPISuccessGetSingle('Membership', array('contact_id' => $this->_individualId
));
561 $contribution = $this->callAPISuccessGetSingle('Contribution', array(
562 'contact_id' => $this->_individualId
,
563 'contribution_status_id' => 2,
565 $this->assertEquals($contribution['trxn_id'], 777);
567 $this->callAPISuccessGetCount('LineItem', array(
568 'entity_id' => $membership['id'],
569 'entity_table' => 'civicrm_membership',
570 'contribution_id' => $contribution['id'],
572 $this->callAPISuccessGetSingle('address', array(
573 'contact_id' => $this->_individualId
,
574 'street_address' => '10 Test St',
575 'postal_code' => 90210,
580 * Test the submit function of the membership form.
582 public function testSubmitRecurCompleteInstant() {
583 $form = $this->getForm();
585 $processor = Civi\Payment\System
::singleton()->getById($this->_paymentProcessorID
);
586 $processor->setDoDirectPaymentResult(array(
587 'payment_status_id' => 1,
588 'trxn_id' => 'kettles boil water',
591 $this->callAPISuccess('MembershipType', 'create', array(
592 'id' => $this->membershipTypeAnnualFixedID
,
593 'duration_unit' => 'month',
594 'duration_interval' => 1,
595 'auto_renew' => TRUE,
598 $this->createLoggedInUser();
599 $params = $this->getBaseSubmitParams();
600 $form->_mode
= 'test';
601 $form->_contactID
= $this->_individualId
;
602 $form->testSubmit($params);
603 $membership = $this->callAPISuccessGetSingle('Membership', array('contact_id' => $this->_individualId
));
604 $this->callAPISuccessGetCount('ContributionRecur', array('contact_id' => $this->_individualId
), 1);
606 $contribution = $this->callAPISuccess('Contribution', 'getsingle', array(
607 'contact_id' => $this->_individualId
,
611 $this->assertEquals(.14, $contribution['fee_amount']);
612 $this->assertEquals('kettles boil water', $contribution['trxn_id']);
614 $this->callAPISuccessGetCount('LineItem', array(
615 'entity_id' => $membership['id'],
616 'entity_table' => 'civicrm_membership',
617 'contribution_id' => $contribution['id'],
623 * Get a membership form object.
625 * We need to instantiate the form to run preprocess, which means we have to trick it about the request method.
627 * @return \CRM_Member_Form_Membership
629 protected function getForm() {
630 $form = new CRM_Member_Form_Membership();
631 $_SERVER['REQUEST_METHOD'] = 'GET';
632 $form->controller
= new CRM_Core_Controller();
640 protected function getBaseSubmitParams() {
642 'cid' => $this->_individualId
,
644 'join_date' => date('m/d/Y', time()),
648 // This format reflects the 23 being the organisation & the 25 being the type.
649 'membership_type_id' => array(23, $this->membershipTypeAnnualFixedID
),
655 'total_amount' => '77.00',
656 'financial_type_id' => '2', //Member dues, see data.xml
657 'soft_credit_type_id' => 11,
658 'soft_credit_contact_id' => '',
659 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
660 'receipt_text' => 'Thank you text',
661 'payment_processor_id' => $this->_paymentProcessorID
,
662 'credit_card_number' => '4111111111111111',
664 'credit_card_exp_date' => array(
666 'Y' => '2019', // TODO: Future proof
668 'credit_card_type' => 'Visa',
669 'billing_first_name' => 'Test',
670 'billing_middlename' => 'Last',
671 'billing_street_address-5' => '10 Test St',
672 'billing_city-5' => 'Test',
673 'billing_state_province_id-5' => '1003',
674 'billing_postal_code-5' => '90210',
675 'billing_country_id-5' => '1228',