3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 * Test CRM_Member_Form_Membership functions.
42 class CRM_Member_Form_MembershipTest
extends CiviUnitTestCase
{
44 protected $_individualId;
45 protected $_contribution;
46 protected $_financialTypeId = 1;
47 protected $_entity = 'Membership';
50 protected $_paymentProcessorID;
53 * Membership type ID for annual fixed membership.
57 protected $membershipTypeAnnualFixedID;
60 * Parameters to create payment processor.
64 protected $_processorParams = [];
67 * ID of created membership.
71 protected $_membershipID;
74 * Payment instrument mapping.
78 protected $paymentInstruments = [];
86 * Test setup for every test.
88 * Connect to the database, truncate the tables that will be used
89 * and redirect stdin to a temporary file.
91 * @throws \CRM_Core_Exception
93 public function setUp() {
94 $this->_apiversion
= 3;
97 $this->_individualId
= $this->individualCreate();
98 $this->_paymentProcessorID
= $this->processorCreate();
100 $this->loadXMLDataSet(dirname(__FILE__
) . '/dataset/data.xml');
102 $membershipTypeAnnualFixed = $this->callAPISuccess('membership_type', 'create', [
104 'name' => "AnnualFixed",
105 'member_of_contact_id' => 23,
106 'duration_unit' => "year",
108 'duration_interval' => 1,
109 'period_type' => "fixed",
110 'fixed_period_start_day' => "101",
111 'fixed_period_rollover_day' => "1231",
112 'relationship_type_id' => 20,
113 'financial_type_id' => 2,
115 $this->membershipTypeAnnualFixedID
= $membershipTypeAnnualFixed['id'];
117 $instruments = $this->callAPISuccess('contribution', 'getoptions', ['field' => 'payment_instrument_id']);
118 $this->paymentInstruments
= $instruments['values'];
122 * Clean up after each test.
124 public function tearDown() {
125 $this->quickCleanUpFinancialEntities();
128 'civicrm_relationship',
129 'civicrm_membership_type',
130 'civicrm_membership',
135 foreach ([17, 18, 23, 32] as $contactID) {
136 $this->callAPISuccess('contact', 'delete', ['id' => $contactID, 'skip_undelete' => TRUE]);
138 $this->callAPISuccess('relationship_type', 'delete', ['id' => 20]);
142 * Test CRM_Member_Form_Membership::formRule() with a parameter
143 * that has an empty contact_select_id value
145 * @throws \CiviCRM_API3_Exception
147 public function testFormRuleEmptyContact() {
149 'contact_select_id' => 0,
150 'membership_type_id' => [1 => NULL],
153 $obj = new CRM_Member_Form_Membership();
154 $rc = $obj->formRule($params, $files, $obj);
155 $this->assertType('array', $rc);
156 $this->assertTrue(array_key_exists('membership_type_id', $rc));
158 $params['membership_type_id'] = [1 => 3];
159 $rc = $obj->formRule($params, $files, $obj);
160 $this->assertType('array', $rc);
161 $this->assertTrue(array_key_exists('join_date', $rc));
165 * Test that form rule fails if start date is before join date.
167 * Test CRM_Member_Form_Membership::formRule() with a parameter
168 * that has an start date before the join date and a rolling
171 public function testFormRuleRollingEarlyStart() {
173 $unixYesterday = $unixNow - (24 * 60 * 60);
174 $ymdYesterday = date('Y-m-d', $unixYesterday);
176 'join_date' => date('Y-m-d'),
177 'start_date' => $ymdYesterday,
179 'membership_type_id' => ['23', '15'],
182 $obj = new CRM_Member_Form_Membership();
183 $rc = call_user_func(['CRM_Member_Form_Membership', 'formRule'],
184 $params, $files, $obj
186 $this->assertType('array', $rc);
187 $this->assertTrue(array_key_exists('start_date', $rc));
191 * Test CRM_Member_Form_Membership::formRule() with a parameter
192 * that has an end date before the start date and a rolling
195 public function testFormRuleRollingEarlyEnd() {
197 $unixYesterday = $unixNow - (24 * 60 * 60);
198 $ymdYesterday = date('Y-m-d', $unixYesterday);
200 'join_date' => date('Y-m-d'),
201 'start_date' => date('Y-m-d'),
202 'end_date' => $ymdYesterday,
203 'membership_type_id' => ['23', '15'],
206 $obj = new CRM_Member_Form_Membership();
207 $rc = $obj->formRule($params, $files, $obj);
208 $this->assertType('array', $rc);
209 $this->assertTrue(array_key_exists('end_date', $rc));
213 * Test CRM_Member_Form_Membership::formRule() with end date but no start date and a rolling membership type.
215 public function testFormRuleRollingEndNoStart() {
217 $unixYearFromNow = $unixNow +
(365 * 24 * 60 * 60);
218 $ymdYearFromNow = date('Y-m-d', $unixYearFromNow);
220 'join_date' => date('Y-m-d'),
222 'end_date' => $ymdYearFromNow,
223 'membership_type_id' => ['23', '15'],
226 $obj = new CRM_Member_Form_Membership();
227 $rc = $obj->formRule($params, $files, $obj);
228 $this->assertType('array', $rc);
229 $this->assertTrue(array_key_exists('start_date', $rc));
233 * Test CRM_Member_Form_Membership::formRule() with a parameter
234 * that has an end date and a lifetime membership type
236 public function testFormRuleRollingLifetimeEnd() {
238 $unixYearFromNow = $unixNow +
(365 * 24 * 60 * 60);
240 'join_date' => date('Y-m-d'),
241 'start_date' => date('Y-m-d'),
242 'end_date' => date('Y-m-d',
245 'membership_type_id' => ['23', '25'],
248 $obj = new CRM_Member_Form_Membership();
249 $rc = $obj->formRule($params, $files, $obj);
250 $this->assertType('array', $rc);
251 $this->assertTrue(array_key_exists('status_id', $rc));
255 * Test CRM_Member_Form_Membership::formRule() with a parameter
256 * that has permanent override and no status
258 public function testFormRulePermanentOverrideWithNoStatus() {
261 'join_date' => date('Y-m-d'),
262 'membership_type_id' => ['23', '25'],
263 'is_override' => TRUE,
266 $obj = new CRM_Member_Form_Membership();
267 $rc = $obj->formRule($params, $files, $obj);
268 $this->assertType('array', $rc);
269 $this->assertTrue(array_key_exists('status_id', $rc));
272 public function testFormRuleUntilDateOverrideWithValidOverrideEndDate() {
274 'join_date' => date('Y-m-d'),
275 'membership_type_id' => ['23', '25'],
276 'is_override' => TRUE,
278 'status_override_end_date' => date('Y-m-d'),
281 $membershipForm = new CRM_Member_Form_Membership();
282 $validationResponse = $membershipForm->formRule($params, $files, $membershipForm);
283 $this->assertTrue($validationResponse);
286 public function testFormRuleUntilDateOverrideWithNoOverrideEndDate() {
288 'join_date' => date('Y-m-d'),
289 'membership_type_id' => ['23', '25'],
290 'is_override' => CRM_Member_StatusOverrideTypes
::UNTIL_DATE
,
294 $membershipForm = new CRM_Member_Form_Membership();
295 $validationResponse = $membershipForm->formRule($params, $files, $membershipForm);
296 $this->assertType('array', $validationResponse);
297 $this->assertEquals('Please enter the Membership override end date.', $validationResponse['status_override_end_date']);
301 * Test CRM_Member_Form_Membership::formRule() with a join date
302 * of one month from now and a rolling membership type
304 public function testFormRuleRollingJoin1MonthFromNow() {
306 $unix1MFmNow = $unixNow +
(31 * 24 * 60 * 60);
308 'join_date' => date('Y-m-d', $unix1MFmNow),
311 'membership_type_id' => ['23', '15'],
314 $obj = new CRM_Member_Form_Membership();
315 $rc = $obj->formRule($params, $files, $obj);
317 // Should have found no valid membership status.
318 $this->assertType('array', $rc);
319 $this->assertTrue(array_key_exists('_qf_default', $rc));
323 * Test CRM_Member_Form_Membership::formRule() with a join date of today and a rolling membership type.
325 public function testFormRuleRollingJoinToday() {
327 'join_date' => date('Y-m-d'),
330 'membership_type_id' => ['23', '15'],
333 $obj = new CRM_Member_Form_Membership();
334 $rc = $obj->formRule($params, $files, $obj);
336 // Should have found New membership status
337 $this->assertTrue($rc);
341 * Test CRM_Member_Form_Membership::formRule() with a join date
342 * of one month ago and a rolling membership type
344 public function testFormRuleRollingJoin1MonthAgo() {
346 $unix1MAgo = $unixNow - (31 * 24 * 60 * 60);
348 'join_date' => date('Y-m-d', $unix1MAgo),
351 'membership_type_id' => ['23', '15'],
354 $obj = new CRM_Member_Form_Membership();
355 $rc = $obj->formRule($params, $files, $obj);
357 // Should have found New membership status.
358 $this->assertTrue($rc);
362 * Test CRM_Member_Form_Membership::formRule() with a join date of six months ago and a rolling membership type.
364 public function testFormRuleRollingJoin6MonthsAgo() {
366 $unix6MAgo = $unixNow - (180 * 24 * 60 * 60);
368 'join_date' => date('Y-m-d', $unix6MAgo),
371 'membership_type_id' => ['23', '15'],
374 $obj = new CRM_Member_Form_Membership();
375 $rc = $obj->formRule($params, $files, $obj);
377 // Should have found Current membership status.
378 $this->assertTrue($rc);
382 * Test CRM_Member_Form_Membership::formRule() with a join date
383 * of one year+ ago and a rolling membership type
385 public function testFormRuleRollingJoin1YearAgo() {
387 $unix1YAgo = $unixNow - (370 * 24 * 60 * 60);
389 'join_date' => date('Y-m-d', $unix1YAgo),
392 'membership_type_id' => ['23', '15'],
395 $obj = new CRM_Member_Form_Membership();
396 $rc = $obj->formRule($params, $files, $obj);
398 // Should have found Grace membership status
399 $this->assertTrue($rc);
403 * Test CRM_Member_Form_Membership::formRule() with a join date
404 * of two years ago and a rolling membership type
406 public function testFormRuleRollingJoin2YearsAgo() {
408 $unix2YAgo = $unixNow - (2 * 365 * 24 * 60 * 60);
410 'join_date' => date('Y-m-d', $unix2YAgo),
413 'membership_type_id' => ['23', '15'],
416 $obj = new CRM_Member_Form_Membership();
417 $rc = $obj->formRule($params, $files, $obj);
419 // Should have found Expired membership status
420 $this->assertTrue($rc);
424 * Test CRM_Member_Form_Membership::formRule() with a join date
425 * of six months ago and a fixed membership type
427 public function testFormRuleFixedJoin6MonthsAgo() {
429 $unix6MAgo = $unixNow - (180 * 24 * 60 * 60);
431 'join_date' => date('Y-m-d', $unix6MAgo),
434 'membership_type_id' => ['23', '7'],
437 $obj = new CRM_Member_Form_Membership();
438 $rc = $obj->formRule($params, $files, $obj);
440 // Should have found Current membership status
441 $this->assertTrue($rc);
445 * Test the submit function of the membership form.
447 * @param string $thousandSeparator
449 * @dataProvider getThousandSeparators
451 public function testSubmit($thousandSeparator) {
452 CRM_Core_Session
::singleton()->getStatus(TRUE);
453 $this->setCurrencySeparators($thousandSeparator);
454 $form = $this->getForm();
456 $this->mut
= new CiviMailUtils($this, TRUE);
457 $form->_mode
= 'test';
458 $this->createLoggedInUser();
460 'cid' => $this->_individualId
,
461 'join_date' => date('2/d/Y', time()),
464 // This format reflects the 23 being the organisation & the 25 being the type.
465 'membership_type_id' => [23, $this->membershipTypeAnnualFixedID
],
470 'total_amount' => $this->formatMoneyInput(1234.56),
471 //Member dues, see data.xml
472 'financial_type_id' => '2',
473 'soft_credit_type_id' => '',
474 'soft_credit_contact_id' => '',
475 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
476 'payment_processor_id' => $this->_paymentProcessorID
,
477 'credit_card_number' => '4111111111111111',
479 'credit_card_exp_date' => [
481 // TODO: Future proof
484 'credit_card_type' => 'Visa',
485 'billing_first_name' => 'Test',
486 'billing_middlename' => 'Last',
487 'billing_street_address-5' => '10 Test St',
488 'billing_city-5' => 'Test',
489 'billing_state_province_id-5' => '1003',
490 'billing_postal_code-5' => '90210',
491 'billing_country_id-5' => '1228',
492 'send_receipt' => TRUE,
493 'receipt_text' => 'Receipt text',
495 $form->_contactID
= $this->_individualId
;
496 $form->testSubmit($params);
497 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId
]);
498 $this->callAPISuccessGetCount('ContributionRecur', ['contact_id' => $this->_individualId
], 0);
499 $contribution = $this->callAPISuccess('Contribution', 'get', [
500 'contact_id' => $this->_individualId
,
504 //CRM-20264 : Check that CC type and number (last 4 digit) is stored during backoffice membership payment
505 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($contribution['id'], 'DESC');
506 $financialTrxn = $this->callAPISuccessGetSingle(
509 'id' => $lastFinancialTrxnId['financialTrxnId'],
510 'return' => ['card_type_id', 'pan_truncation'],
513 $this->assertEquals(1, $financialTrxn['card_type_id']);
514 $this->assertEquals(1111, $financialTrxn['pan_truncation']);
516 $this->callAPISuccessGetCount('LineItem', [
517 'entity_id' => $membership['id'],
518 'entity_table' => 'civicrm_membership',
519 'contribution_id' => $contribution['id'],
522 $this->_checkFinancialRecords([
523 'id' => $contribution['id'],
524 'total_amount' => 1234.56,
525 'financial_account_id' => 2,
526 'payment_instrument_id' => $this->callAPISuccessGetValue('PaymentProcessor', [
527 'id' => $this->_paymentProcessorID
,
528 'return' => 'payment_instrument_id',
531 $this->mut
->checkMailLog([
532 CRM_Utils_Money
::format('1234.56'),
536 $this->assertEquals([
538 'text' => 'AnnualFixed membership for Mr. Anthony Anderson II has been added. The new membership End Date is December 31st, ' . date('Y') . '. A membership confirmation and receipt has been sent to anthony_anderson@civicrm.org.',
539 'title' => 'Complete',
543 ], CRM_Core_Session
::singleton()->getStatus());
547 * Test the submit function of the membership form on membership type change.
548 * Check if the related contribuion is also updated if the minimum_fee didn't match
550 public function testContributionUpdateOnMembershipTypeChange() {
551 // Step 1: Create a Membership via backoffice whose with 50.00 payment
552 $form = $this->getForm();
554 $this->mut
= new CiviMailUtils($this, TRUE);
555 $this->createLoggedInUser();
556 $priceSet = $this->callAPISuccess('PriceSet', 'Get', ["extends" => "CiviMember"]);
557 $form->set('priceSetId', $priceSet['id']);
558 CRM_Price_BAO_PriceSet
::buildPriceSet($form);
560 'cid' => $this->_individualId
,
561 'join_date' => date('Y-m-d'),
564 // This format reflects the 23 being the organisation & the 25 being the type.
565 'membership_type_id' => [23, $this->membershipTypeAnnualFixedID
],
566 'record_contribution' => 1,
567 'total_amount' => 50,
568 'receive_date' => date('Y-m-d', time()) . ' 20:36:00',
569 'payment_instrument_id' => array_search('Check', $this->paymentInstruments
),
570 'contribution_status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
571 //Member dues, see data.xml
572 'financial_type_id' => '2',
573 'payment_processor_id' => $this->_paymentProcessorID
,
575 $form->_contactID
= $this->_individualId
;
576 $form->testSubmit($params);
577 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId
]);
578 // check the membership status after partial payment, if its Pending
579 $this->assertEquals(array_search('New', CRM_Member_PseudoConstant
::membershipStatus()), $membership['status_id']);
580 $contribution = $this->callAPISuccessGetSingle('Contribution', [
581 'contact_id' => $this->_individualId
,
583 $this->assertEquals('Completed', $contribution['contribution_status']);
584 $this->assertEquals(50.00, $contribution['total_amount']);
585 $this->assertEquals(50.00, $contribution['net_amount']);
587 // Step 2: Change the membership type whose minimum free is less than earlier membership
588 $secondMembershipType = $this->callAPISuccess('membership_type', 'create', [
590 'name' => "Second Test Membership",
591 'member_of_contact_id' => 23,
592 'duration_unit' => "month",
594 'duration_interval' => 1,
595 'period_type' => "fixed",
596 'fixed_period_start_day' => "101",
597 'fixed_period_rollover_day' => "1231",
598 'relationship_type_id' => 20,
599 'financial_type_id' => 2,
601 Civi
::settings()->set('update_contribution_on_membership_type_change', TRUE);
602 $form = $this->getForm();
604 $form->_id
= $membership['id'];
605 $form->set('priceSetId', $priceSet['id']);
606 CRM_Price_BAO_PriceSet
::buildPriceSet($form);
607 $form->_action
= CRM_Core_Action
::UPDATE
;
609 'cid' => $this->_individualId
,
610 'join_date' => date('Y-m-d'),
613 // This format reflects the 23 being the organisation & the 25 being the type.
614 'membership_type_id' => [23, $secondMembershipType['id']],
615 'record_contribution' => 1,
617 'total_amount' => 25,
618 'receive_date' => date('Y-m-d', time()) . ' 20:36:00',
619 'payment_instrument_id' => array_search('Check', $this->paymentInstruments
),
620 //Member dues, see data.xml
621 'financial_type_id' => '2',
622 'payment_processor_id' => $this->_paymentProcessorID
,
624 $form->_contactID
= $this->_individualId
;
625 $form->testSubmit($params);
626 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId
]);
627 // check the membership status after partial payment, if its Pending
628 $contribution = $this->callAPISuccessGetSingle('Contribution', [
629 'contact_id' => $this->_individualId
,
631 $payment = CRM_Contribute_BAO_Contribution
::getPaymentInfo($membership['id'], 'membership', FALSE, TRUE);
632 // Check the contribution status on membership type change whose minimum fee was less than earlier memebership
633 $this->assertEquals('Pending refund', $contribution['contribution_status']);
634 // Earlier paid amount
635 $this->assertEquals(50, $payment['paid']);
637 $this->assertEquals(-25, $payment['balance']);
641 * Test the submit function of the membership form for partial payment.
643 * @param string $thousandSeparator
644 * punctuation used to refer to thousands.
646 * @dataProvider getThousandSeparators
648 public function testSubmitPartialPayment($thousandSeparator) {
649 $this->setCurrencySeparators($thousandSeparator);
650 // Step 1: submit a partial payment for a membership via backoffice
651 $form = $this->getForm();
653 $this->mut
= new CiviMailUtils($this, TRUE);
654 $this->createLoggedInUser();
655 $priceSet = $this->callAPISuccess('PriceSet', 'Get', ["extends" => "CiviMember"]);
656 $form->set('priceSetId', $priceSet['id']);
657 $partiallyPaidAmount = 25;
658 CRM_Price_BAO_PriceSet
::buildPriceSet($form);
660 'cid' => $this->_individualId
,
661 'join_date' => date('Y-m-d'),
664 // This format reflects the 23 being the organisation & the 25 being the type.
665 'membership_type_id' => [23, $this->membershipTypeAnnualFixedID
],
666 'receive_date' => date('Y-m-d', time()) . ' 20:36:00',
667 'record_contribution' => 1,
668 'total_amount' => $this->formatMoneyInput($partiallyPaidAmount),
669 'payment_instrument_id' => array_search('Check', $this->paymentInstruments
),
670 'contribution_status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Partially paid'),
671 //Member dues, see data.xml
672 'financial_type_id' => '2',
673 'payment_processor_id' => $this->_paymentProcessorID
,
675 $form->_contactID
= $this->_individualId
;
676 $form->testSubmit($params);
677 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId
]);
678 // check the membership status after partial payment, if its Pending
679 $this->assertEquals(array_search('Pending', CRM_Member_PseudoConstant
::membershipStatus()), $membership['status_id']);
680 $contribution = $this->callAPISuccessGetSingle('Contribution', [
681 'contact_id' => $this->_individualId
,
683 $this->assertEquals('Partially paid', $contribution['contribution_status']);
684 // $this->assertEquals(50.00, $contribution['total_amount']);
685 // $this->assertEquals(25.00, $contribution['net_amount']);
687 // Step 2: submit the other half of the partial payment
688 // via AdditionalPayment form to complete the related contribution
689 $form = new CRM_Contribute_Form_AdditionalPayment();
691 'contribution_id' => $contribution['contribution_id'],
692 'contact_id' => $this->_individualId
,
693 'total_amount' => $this->formatMoneyInput($partiallyPaidAmount),
695 'financial_type_id' => 2,
696 'receive_date' => '2015-04-21 23:27:00',
697 'trxn_date' => '2017-04-11 13:05:11',
698 'payment_processor_id' => 0,
699 'payment_instrument_id' => array_search('Check', $this->paymentInstruments
),
700 'check_number' => 'check-12345',
702 $form->cid
= $this->_individualId
;
703 $form->testSubmit($submitParams);
704 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId
]);
705 // check the membership status after additional payment, if its changed to 'New'
706 $this->assertEquals(array_search('New', CRM_Member_PseudoConstant
::membershipStatus()), $membership['status_id']);
708 // check the contribution status and net amount after additional payment
709 $contribution = $this->callAPISuccessGetSingle('Contribution', [
710 'contact_id' => $this->_individualId
,
712 $this->assertEquals('Completed', $contribution['contribution_status']);
713 // $this->assertEquals(50.00, $contribution['net_amount']);
717 * Test the submit function of the membership form.
719 * @throws \CRM_Core_Exception
721 public function testSubmitRecur() {
722 CRM_Core_Session
::singleton()->getStatus(TRUE);
723 $pendingVal = $this->callAPISuccessGetValue('OptionValue', [
725 'option_group_id' => "contribution_status",
726 'label' => "Pending Label**",
728 //Update label for Pending contribution status.
729 $this->callAPISuccess('OptionValue', 'create', [
731 'label' => "PendingEdited",
734 $form = $this->getForm();
736 $this->callAPISuccess('MembershipType', 'create', [
737 'id' => $this->membershipTypeAnnualFixedID
,
738 'duration_unit' => 'month',
739 'duration_interval' => 1,
740 'auto_renew' => TRUE,
743 $this->createLoggedInUser();
744 $params = $this->getBaseSubmitParams();
745 $form->_mode
= 'test';
746 $form->_contactID
= $this->_individualId
;
747 $form->testSubmit($params);
748 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId
]);
749 $this->callAPISuccessGetCount('ContributionRecur', ['contact_id' => $this->_individualId
], 1);
751 $contribution = $this->callAPISuccess('Contribution', 'get', [
752 'contact_id' => $this->_individualId
,
756 //Check if Membership Payment is recorded.
757 $this->callAPISuccessGetCount('MembershipPayment', [
758 'membership_id' => $membership['id'],
759 'contribution_id' => $contribution['id'],
763 $this->callAPISuccessGetCount('LineItem', [
764 'entity_id' => $membership['id'],
765 'entity_table' => 'civicrm_membership',
766 'contribution_id' => $contribution['id'],
768 $this->assertEquals([
770 'text' => 'AnnualFixed membership for Mr. Anthony Anderson II has been added. The new membership End Date is ' . date('F jS, Y', strtotime('last day of this month')) . ' 12:00 AM.',
771 'title' => 'Complete',
775 ], CRM_Core_Session
::singleton()->getStatus());
779 * CRM-20946: Test the financial entires especially the reversed amount,
780 * after related Contribution is cancelled
782 public function testFinancialEntiriesOnCancelledContribution() {
783 // Create two memberships for individual $this->_individualId, via a price set in the back end.
784 $this->createTwoMembershipsViaPriceSetInBackEnd($this->_individualId
);
786 // cancel the related contribution via API
787 $contribution = $this->callAPISuccessGetSingle('Contribution', [
788 'contact_id' => $this->_individualId
,
789 'contribution_status_id' => 2,
791 $this->callAPISuccess('Contribution', 'create', [
792 'id' => $contribution['id'],
793 'contribution_status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Contribute_DAO_Contribution', 'contribution_status_id', 'Cancelled'),
796 // fetch financial_trxn ID of the related contribution
797 $sql = "SELECT financial_trxn_id
798 FROM civicrm_entity_financial_trxn
799 WHERE entity_id = %1 AND entity_table = 'civicrm_contribution'
803 $financialTrxnID = CRM_Core_DAO
::singleValueQuery($sql, [1 => [$contribution['id'], 'Int']]);
805 // fetch entity_financial_trxn records and compare their cancelled records
806 $result = $this->callAPISuccess('EntityFinancialTrxn', 'Get', [
807 'financial_trxn_id' => $financialTrxnID,
808 'entity_table' => 'civicrm_financial_item',
810 // compare the reversed amounts of respective memberships after cancelling contribution
811 $cancelledMembershipAmounts = [
816 foreach ($result['values'] as $record) {
817 $this->assertEquals($cancelledMembershipAmounts[$count], $record['amount']);
823 * Test the submit function of the membership form.
825 public function testSubmitPayLaterWithBilling() {
826 $form = $this->getForm(NULL);
828 $this->createLoggedInUser();
830 'cid' => $this->_individualId
,
831 'join_date' => date('Y-m-d'),
834 // This format reflects the 23 being the organisation & the 25 being the type.
835 'membership_type_id' => [23, $this->membershipTypeAnnualFixedID
],
840 'total_amount' => '50.00',
841 //Member dues, see data.xml
842 'financial_type_id' => '2',
843 'soft_credit_type_id' => '',
844 'soft_credit_contact_id' => '',
845 'payment_instrument_id' => 4,
846 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
847 'receipt_text_signup' => 'Thank you text',
848 'payment_processor_id' => $this->_paymentProcessorID
,
849 'record_contribution' => TRUE,
851 'contribution_status_id' => 2,
852 'billing_first_name' => 'Test',
853 'billing_middlename' => 'Last',
854 'billing_street_address-5' => '10 Test St',
855 'billing_city-5' => 'Test',
856 'billing_state_province_id-5' => '1003',
857 'billing_postal_code-5' => '90210',
858 'billing_country_id-5' => '1228',
860 $form->_contactID
= $this->_individualId
;
862 $form->testSubmit($params);
863 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId
]);
864 $contribution = $this->callAPISuccessGetSingle('Contribution', [
865 'contact_id' => $this->_individualId
,
866 'contribution_status_id' => 2,
868 $this->assertEquals($contribution['trxn_id'], 777);
870 $this->callAPISuccessGetCount('LineItem', [
871 'entity_id' => $membership['id'],
872 'entity_table' => 'civicrm_membership',
873 'contribution_id' => $contribution['id'],
875 $this->callAPISuccessGetSingle('address', [
876 'contact_id' => $this->_individualId
,
877 'street_address' => '10 Test St',
878 'postal_code' => 90210,
883 * Test if membership is updated to New after contribution
884 * is updated from Partially paid to Completed.
886 public function testSubmitUpdateMembershipFromPartiallyPaid() {
887 $memStatus = CRM_Member_BAO_Membership
::buildOptions('status_id', 'validate');
889 //Perform a pay later membership contribution.
890 $this->testSubmitPayLaterWithBilling();
891 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId
]);
892 $this->assertEquals($membership['status_id'], array_search('Pending', $memStatus));
893 $contribution = $this->callAPISuccessGetSingle('MembershipPayment', [
894 'membership_id' => $membership['id'],
897 //Update contribution to Partially paid.
898 $prevContribution = $this->callAPISuccess('Contribution', 'create', [
899 'id' => $contribution['contribution_id'],
900 'contribution_status_id' => 'Partially paid',
902 $prevContribution = $prevContribution['values'][1];
904 //Complete the contribution from offline form.
905 $form = new CRM_Contribute_Form_Contribution();
907 'id' => $contribution['contribution_id'],
908 'contribution_status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
911 $fields = ['total_amount', 'net_amount', 'financial_type_id', 'receive_date', 'contact_id', 'payment_instrument_id'];
912 foreach ($fields as $val) {
913 $submitParams[$val] = $prevContribution[$val];
915 $form->testSubmit($submitParams, CRM_Core_Action
::UPDATE
);
917 //Check if Membership is updated to New.
918 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId
]);
919 $this->assertEquals($membership['status_id'], array_search('New', $memStatus));
923 * Test the submit function of the membership form.
925 public function testSubmitRecurCompleteInstant() {
926 $form = $this->getForm();
927 $mut = new CiviMailUtils($this, TRUE);
928 $processor = Civi\Payment\System
::singleton()->getById($this->_paymentProcessorID
);
929 $processor->setDoDirectPaymentResult([
930 'payment_status_id' => 1,
931 'trxn_id' => 'kettles boil water',
934 $processorDetail = $processor->getPaymentProcessor();
935 $this->callAPISuccess('MembershipType', 'create', [
936 'id' => $this->membershipTypeAnnualFixedID
,
937 'duration_unit' => 'month',
938 'duration_interval' => 1,
939 'auto_renew' => TRUE,
942 $this->createLoggedInUser();
943 $params = $this->getBaseSubmitParams();
944 $form->_mode
= 'test';
945 $form->_contactID
= $this->_individualId
;
946 $form->testSubmit($params);
947 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId
]);
948 $this->callAPISuccessGetCount('ContributionRecur', ['contact_id' => $this->_individualId
], 1);
950 $contribution = $this->callAPISuccess('Contribution', 'getsingle', [
951 'contact_id' => $this->_individualId
,
955 $this->assertEquals(.14, $contribution['fee_amount']);
956 $this->assertEquals('kettles boil water', $contribution['trxn_id']);
957 $this->assertEquals($processorDetail['payment_instrument_id'], $contribution['payment_instrument_id']);
959 $this->callAPISuccessGetCount('LineItem', [
960 'entity_id' => $membership['id'],
961 'entity_table' => 'civicrm_membership',
962 'contribution_id' => $contribution['id'],
965 '===========================================================
966 Billing Name and Address
967 ===========================================================
972 '===========================================================
973 Membership Information
974 ===========================================================
975 Membership Type: AnnualFixed
976 Membership Start Date: ',
977 '===========================================================
978 Credit Card Information
979 ===========================================================
989 * Test membership form with Failed Contribution.
991 public function testFormWithFailedContribution() {
992 $form = $this->getForm();
994 $this->createLoggedInUser();
995 $params = $this->getBaseSubmitParams();
996 unset($params['price_set_id']);
997 unset($params['credit_card_number']);
998 unset($params['cvv2']);
999 unset($params['credit_card_exp_date']);
1000 unset($params['credit_card_type']);
1001 unset($params['send_receipt']);
1002 unset($params['is_recur']);
1004 $params['record_contribution'] = TRUE;
1005 $params['contribution_status_id'] = array_search('Failed', CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name'));
1006 $form->_mode
= NULL;
1007 $form->_contactID
= $this->_individualId
;
1009 $form->testSubmit($params);
1010 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId
]);
1011 $form->testSubmit($params);
1012 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId
]);
1013 $this->assertEquals($membership['status_id'], array_search('Pending', CRM_Member_PseudoConstant
::membershipStatus()));
1017 * CRM-20955, CRM-20966:
1018 * Test creating two memberships with inheritance via price set in the back end,
1019 * checking that the correct primary & secondary memberships, contributions, line items
1020 * & membership_payment records are created.
1021 * Uses some data from tests/phpunit/CRM/Member/Form/dataset/data.xml .
1023 public function testTwoInheritedMembershipsViaPriceSetInBackend() {
1024 // Create an organization and give it a "Member of" relationship to $this->_individualId.
1025 $orgID = $this->organizationCreate();
1026 $relationship = $this->callAPISuccess('relationship', 'create', [
1027 'contact_id_a' => $this->_individualId
,
1028 'contact_id_b' => $orgID,
1029 'relationship_type_id' => 20,
1033 // Create two memberships for the organization, via a price set in the back end.
1034 $this->createTwoMembershipsViaPriceSetInBackEnd($orgID);
1036 // Check the primary memberships on the organization.
1037 $orgMembershipResult = $this->callAPISuccess('membership', 'get', [
1038 'contact_id' => $orgID,
1040 $this->assertEquals(2, $orgMembershipResult['count'], "2 primary memberships should have been created on the organization.");
1041 $primaryMembershipIds = [];
1042 foreach ($orgMembershipResult['values'] as $membership) {
1043 $primaryMembershipIds[] = $membership['id'];
1044 $this->assertTrue(empty($membership['owner_membership_id']), "Membership on the organization has owner_membership_id so is inherited.");
1047 // CRM-20955: check that correct inherited memberships were created for the individual,
1048 // for both of the primary memberships.
1049 $individualMembershipResult = $this->callAPISuccess('membership', 'get', [
1050 'contact_id' => $this->_individualId
,
1052 $this->assertEquals(2, $individualMembershipResult['count'], "2 inherited memberships should have been created on the individual.");
1053 foreach ($individualMembershipResult['values'] as $membership) {
1054 $this->assertNotEmpty($membership['owner_membership_id'], "Membership on the individual lacks owner_membership_id so is not inherited.");
1055 $this->assertNotContains($membership['id'], $primaryMembershipIds, "Inherited membership id should not be the id of a primary membership.");
1056 $this->assertContains($membership['owner_membership_id'], $primaryMembershipIds, "Inherited membership owner_membership_id should be the id of a primary membership.");
1059 // CRM-20966: check that the correct membership contribution, line items
1060 // & membership_payment records were created for the organization.
1061 $contributionResult = $this->callAPISuccess('contribution', 'get', [
1062 'contact_id' => $orgID,
1064 'api.line_item.get' => [],
1065 'api.membership_payment.get' => [],
1067 $this->assertEquals(1, $contributionResult['count'], "One contribution should have been created for the organization's memberships.");
1069 $this->assertEquals(2, $contributionResult['values'][0]['api.line_item.get']['count'], "2 line items should have been created for the organization's memberships.");
1070 foreach ($contributionResult['values'][0]['api.line_item.get']['values'] as $lineItem) {
1071 $this->assertEquals('civicrm_membership', $lineItem['entity_table'], "Membership line item's entity_table should be 'civicrm_membership'.");
1072 $this->assertContains($lineItem['entity_id'], $primaryMembershipIds, "Membership line item's entity_id should be the id of a primary membership.");
1075 $this->assertEquals(2, $contributionResult['values'][0]['api.membership_payment.get']['count'], "2 membership payment records should have been created for the organization's memberships.");
1076 foreach ($contributionResult['values'][0]['api.membership_payment.get']['values'] as $membershipPayment) {
1077 $this->assertEquals($contributionResult['values'][0]['id'], $membershipPayment['contribution_id'], "membership payment's contribution ID should be the ID of the organization's membership contribution.");
1078 $this->assertContains($membershipPayment['membership_id'], $primaryMembershipIds, "membership payment's membership ID should be the ID of a primary membership.");
1081 // CRM-20966: check that deleting relationship used for inheritance does not delete contribution.
1082 $this->callAPISuccess('relationship', 'delete', [
1083 'id' => $relationship['id'],
1086 $contributionResultAfterRelationshipDelete = $this->callAPISuccess('contribution', 'get', [
1087 'id' => $contributionResult['values'][0]['id'],
1088 'contact_id' => $orgID,
1090 $this->assertEquals(1, $contributionResultAfterRelationshipDelete['count'], "Contribution has been wrongly deleted.");
1094 * dev/core/issues/860:
1095 * Test creating two memberships via price set in the back end with a discount,
1096 * checking that the line items have correct amounts.
1098 public function testTwoMembershipsViaPriceSetInBackendWithDiscount() {
1099 // Register buildAmount hook to apply discount.
1100 $this->hookClass
->setHook('civicrm_buildAmount', [$this, 'buildAmountMembershipDiscount']);
1102 // Create two memberships for individual $this->_individualId, via a price set in the back end.
1103 $this->createTwoMembershipsViaPriceSetInBackEnd($this->_individualId
);
1104 $contribution = $this->callAPISuccessGetSingle('Contribution', [
1105 'contact_id' => $this->_individualId
,
1107 // Note: we can't check for the contribution total being discounted, because the total is set
1108 // when the contribution is created via $form->testSubmit(), but buildAmount isn't called
1109 // until testSubmit() runs. Fixing that might involve making testSubmit() more sophisticated,
1110 // or just hacking total_amount for this case.
1112 $lineItemResult = $this->callAPISuccess('LineItem', 'get', [
1113 'contribution_id' => $contribution['id'],
1115 $this->assertEquals(2, $lineItemResult['count']);
1116 $discountedItems = 0;
1117 foreach ($lineItemResult['values'] as $lineItem) {
1118 if (CRM_Utils_String
::startsWith($lineItem['label'], 'Long Haired Goat')) {
1119 $this->assertEquals(15.0, $lineItem['line_total']);
1120 $this->assertEquals('Long Haired Goat - one leg free!', $lineItem['label']);
1124 $this->assertEquals(1, $discountedItems);
1128 * Implements hook_civicrm_buildAmount() for testTwoMembershipsViaPriceSetInBackendWithDiscount().
1130 public function buildAmountMembershipDiscount($pageType, &$form, &$amount) {
1131 foreach ($amount as $id => $priceField) {
1132 if (is_array($priceField['options'])) {
1133 foreach ($priceField['options'] as $optionId => $option) {
1134 if ($option['membership_type_id'] == 15) {
1135 // Long Haired Goat membership discount.
1136 $amount[$id]['options'][$optionId]['amount'] = $option['amount'] * 0.75;
1137 $amount[$id]['options'][$optionId]['label'] = $option['label'] . ' - one leg free!';
1145 * Get a membership form object.
1147 * We need to instantiate the form to run preprocess, which means we have to trick it about the request method.
1149 * @return \CRM_Member_Form_Membership
1151 protected function getForm() {
1152 if (isset($_REQUEST['cid'])) {
1153 unset($_REQUEST['cid']);
1155 $form = new CRM_Member_Form_Membership();
1156 $_SERVER['REQUEST_METHOD'] = 'GET';
1157 $form->controller
= new CRM_Core_Controller();
1164 protected function getBaseSubmitParams() {
1166 'cid' => $this->_individualId
,
1167 'price_set_id' => 0,
1168 'join_date' => date('Y-m-d'),
1171 'campaign_id' => '',
1172 // This format reflects the 23 being the organisation & the 25 being the type.
1173 'membership_type_id' => [23, $this->membershipTypeAnnualFixedID
],
1174 'auto_renew' => '1',
1179 'total_amount' => '77.00',
1180 //Member dues, see data.xml
1181 'financial_type_id' => '2',
1182 'soft_credit_type_id' => 11,
1183 'soft_credit_contact_id' => '',
1184 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
1185 'receipt_text' => 'Thank you text',
1186 'payment_processor_id' => $this->_paymentProcessorID
,
1187 'credit_card_number' => '4111111111111111',
1189 'credit_card_exp_date' => [
1191 // TODO: Future proof
1194 'credit_card_type' => 'Visa',
1195 'billing_first_name' => 'Test',
1196 'billing_middlename' => 'Last',
1197 'billing_street_address-5' => '10 Test St',
1198 'billing_city-5' => 'Test',
1199 'billing_state_province_id-5' => '1003',
1200 'billing_postal_code-5' => '90210',
1201 'billing_country_id-5' => '1228',
1202 'send_receipt' => 1,
1209 * create two memberships for the same individual, via a price set in the back end.
1211 * @param int $contactId Id of contact on which the memberships will be created.
1213 protected function createTwoMembershipsViaPriceSetInBackEnd($contactId) {
1214 $form = $this->getForm(NULL);
1215 $form->preProcess();
1216 $this->createLoggedInUser();
1218 // create a price-set of price-field of type checkbox and each price-option corresponds to a membership type
1219 $priceSet = $this->callAPISuccess('price_set', 'create', [
1220 'is_quick_config' => 0,
1221 'extends' => 'CiviMember',
1222 'financial_type_id' => 1,
1223 'title' => 'my Page',
1225 $priceSetID = $priceSet['id'];
1226 // create respective checkbox price-field
1227 $priceField = $this->callAPISuccess('price_field', 'create', [
1228 'price_set_id' => $priceSetID,
1229 'label' => 'Memberships',
1230 'html_type' => 'Checkbox',
1232 $priceFieldID = $priceField['id'];
1233 // create two price options, each represent a membership type of amount 20 and 10 respectively
1234 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', [
1235 'price_set_id' => $priceSetID,
1236 'price_field_id' => $priceField['id'],
1237 'label' => 'Long Haired Goat',
1239 'financial_type_id' => 'Donation',
1240 'membership_type_id' => 15,
1241 'membership_num_terms' => 1,
1243 $pfvIDs = [$priceFieldValue['id'] => 1];
1244 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', [
1245 'price_set_id' => $priceSetID,
1246 'price_field_id' => $priceField['id'],
1247 'label' => 'Shoe-eating Goat',
1249 'financial_type_id' => 'Donation',
1250 'membership_type_id' => 35,
1251 'membership_num_terms' => 2,
1253 $pfvIDs[$priceFieldValue['id']] = 1;
1255 // register for both of these memberships via backoffice membership form submission
1257 'cid' => $contactId,
1258 'join_date' => date('Y-m-d'),
1261 // This format reflects the 23 being the organisation & the 25 being the type.
1262 "price_$priceFieldID" => $pfvIDs,
1263 "price_set_id" => $priceSetID,
1264 'membership_type_id' => [1 => 0],
1265 'auto_renew' => '0',
1266 'max_related' => '',
1269 'total_amount' => '30.00',
1270 //Member dues, see data.xml
1271 'financial_type_id' => '2',
1272 'soft_credit_type_id' => '',
1273 'soft_credit_contact_id' => '',
1274 'payment_instrument_id' => 4,
1275 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
1276 'receipt_text_signup' => 'Thank you text',
1277 'payment_processor_id' => $this->_paymentProcessorID
,
1278 'record_contribution' => TRUE,
1280 'contribution_status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Contribute_DAO_Contribution', 'contribution_status_id', 'Pending'),
1281 'billing_first_name' => 'Test',
1282 'billing_middlename' => 'Last',
1283 'billing_street_address-5' => '10 Test St',
1284 'billing_city-5' => 'Test',
1285 'billing_state_province_id-5' => '1003',
1286 'billing_postal_code-5' => '90210',
1287 'billing_country_id-5' => '1228',
1289 $form->testSubmit($params);
1293 * Test membership status overrides when contribution is cancelled.
1295 public function testContributionFormStatusUpdate() {
1296 $form = new CRM_Contribute_Form_Contribution();
1298 //Create a membership with status = 'New'.
1299 $this->_individualId
= $this->createLoggedInUser();
1301 'contact_id' => $this->_individualId
,
1302 'membership_type_id' => $this->membershipTypeAnnualFixedID
,
1303 'status_id' => array_search('New', CRM_Member_PseudoConstant
::membershipStatus()),
1305 $cancelledStatusId = $this->callAPISuccessGetValue('OptionValue', [
1306 'return' => "value",
1307 'option_group_id' => "contribution_status",
1308 'name' => "Cancelled",
1311 'total_amount' => 50,
1312 'financial_type_id' => 2,
1313 'contact_id' => $this->_individualId
,
1314 'payment_instrument_id' => array_search('Check', $this->paymentInstruments
),
1315 'contribution_status_id' => $cancelledStatusId,
1317 $membershipId = $this->contactMembershipCreate($memParams);
1320 'membership_id' => $membershipId,
1321 'total_amount' => 50,
1322 'financial_type_id' => 2,
1323 'contact_id' => $this->_individualId
,
1325 $contribution = CRM_Member_BAO_Membership
::recordMembershipContribution($contriParams);
1327 //Update Contribution to Cancelled.
1328 $form->_id
= $params['id'] = $contribution->id
;
1329 $form->_mode
= NULL;
1330 $form->_contactID
= $this->_individualId
;
1331 $form->testSubmit($params, CRM_Core_Action
::UPDATE
);
1332 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId
]);
1334 //Assert membership status overrides when the contribution cancelled.
1335 $this->assertEquals($membership['is_override'], TRUE);
1336 $this->assertEquals($membership['status_id'], $this->callAPISuccessGetValue('MembershipStatus', [
1338 'name' => "Cancelled",
1343 * CRM-21656: Test the submit function of the membership form if Sales Tax is enabled.
1344 * This test simulates what happens when one hits Edit on a Contribution that has both LineItems and Sales Tax components
1345 * Without making any Edits -> check that the LineItem data remain the same
1346 * In addition (a data-integrity check) -> check that the LineItem data add up to the data at the Contribution level
1348 public function testLineItemAmountOnSalesTax() {
1349 $this->enableTaxAndInvoicing();
1350 $this->relationForFinancialTypeWithFinancialAccount(2);
1351 $form = $this->getForm();
1352 $form->preProcess();
1353 $this->mut
= new CiviMailUtils($this, TRUE);
1354 $this->createLoggedInUser();
1355 $priceSet = $this->callAPISuccess('PriceSet', 'Get', ["extends" => "CiviMember"]);
1356 $form->set('priceSetId', $priceSet['id']);
1357 // we are simulating the creation of a Price Set in Administer -> CiviContribute -> Manage Price Sets so set is_quick_config = 0
1358 $this->callAPISuccess('PriceSet', 'Create', ["id" => $priceSet['id'], 'is_quick_config' => 0]);
1359 // clean the price options static variable to repopulate the options, in order to fetch tax information
1360 \Civi
::$statics['CRM_Price_BAO_PriceField']['priceOptions'] = NULL;
1361 CRM_Price_BAO_PriceSet
::buildPriceSet($form);
1362 // rebuild the price set form variable to include the tax information against each price options
1363 $form->_priceSet
= current(CRM_Price_BAO_PriceSet
::getSetDetail($priceSet['id']));
1365 'cid' => $this->_individualId
,
1366 'join_date' => date('Y-m-d'),
1369 // This format reflects the 23 being the organisation & the 25 being the type.
1370 'membership_type_id' => [23, $this->membershipTypeAnnualFixedID
],
1371 'record_contribution' => 1,
1372 'total_amount' => 55,
1373 'receive_date' => date('Y-m-d', time()) . ' 20:36:00',
1374 'payment_instrument_id' => array_search('Check', $this->paymentInstruments
),
1375 'contribution_status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
1376 //Member dues, see data.xml
1377 'financial_type_id' => 2,
1378 'payment_processor_id' => $this->_paymentProcessorID
,
1380 $form->_contactID
= $this->_individualId
;
1381 $form->testSubmit($params);
1383 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId
]);
1384 $lineItem = $this->callAPISuccessGetSingle('LineItem', ['entity_id' => $membership['id'], 'entity_table' => 'civicrm_membership']);
1385 $this->assertEquals(1, $lineItem['qty']);
1386 $this->assertEquals(50.00, $lineItem['unit_price']);
1387 $this->assertEquals(50.00, $lineItem['line_total']);
1388 $this->assertEquals(5.00, $lineItem['tax_amount']);
1390 // Simply save the 'Edit Contribution' form
1391 $form = new CRM_Contribute_Form_Contribution();
1392 $form->_context
= 'membership';
1393 $form->_values
= $this->callAPISuccessGetSingle('Contribution', ['id' => $lineItem['contribution_id'], 'return' => ['total_amount', 'net_amount', 'fee_amount', 'tax_amount']]);
1395 'contact_id' => $this->_individualId
,
1396 'id' => $lineItem['contribution_id'],
1397 'financial_type_id' => 2,
1398 'contribution_status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
1400 CRM_Core_Action
::UPDATE
);
1402 // ensure that the LineItem data remain the same
1403 $lineItem = $this->callAPISuccessGetSingle('LineItem', ['entity_id' => $membership['id'], 'entity_table' => 'civicrm_membership']);
1404 $this->assertEquals(1, $lineItem['qty']);
1405 $this->assertEquals(50.00, $lineItem['unit_price']);
1406 $this->assertEquals(50.00, $lineItem['line_total']);
1407 $this->assertEquals(5.00, $lineItem['tax_amount']);
1409 // ensure that the LineItem data add up to the data at the Contribution level
1410 $contribution = $this->callAPISuccessGetSingle('Contribution',
1412 'contribution_id' => 1,
1413 'return' => ['tax_amount', 'total_amount'],
1416 $this->assertEquals($contribution['total_amount'], $lineItem['line_total'] +
$lineItem['tax_amount']);
1417 $this->assertEquals($contribution['tax_amount'], $lineItem['tax_amount']);
1419 $financialItems = $this->callAPISuccess('FinancialItem', 'get', []);
1420 $financialItems_sum = 0;
1421 foreach ($financialItems['values'] as $financialItem) {
1422 $financialItems_sum +
= $financialItem['amount'];
1424 $this->assertEquals($contribution['total_amount'], $financialItems_sum);
1426 // reset the price options static variable so not leave any dummy data, that might hamper other unit tests
1427 \Civi
::$statics['CRM_Price_BAO_PriceField']['priceOptions'] = NULL;
1428 $this->disableTaxAndInvoicing();
1432 * Test that membership end_date is correct for multiple terms for pending contribution
1434 * @throws CiviCRM_API3_Exception
1435 * @throws \CRM_Core_Exception
1437 public function testCreatePendingWithMultipleTerms() {
1438 CRM_Core_Session
::singleton()->getStatus(TRUE);
1439 $form = $this->getForm();
1440 $form->preProcess();
1441 $this->mut
= new CiviMailUtils($this, TRUE);
1442 $this->createLoggedInUser();
1443 $membershipTypeAnnualRolling = $this->callAPISuccess('membership_type', 'create', [
1445 'name' => "AnnualRollingNew",
1446 'member_of_contact_id' => 23,
1447 'duration_unit' => "year",
1448 'minimum_fee' => 50,
1449 'duration_interval' => 1,
1450 'period_type' => "rolling",
1451 'relationship_type_id' => 20,
1452 'relationship_direction' => 'b_a',
1453 'financial_type_id' => 2,
1456 'cid' => $this->_individualId
,
1457 'join_date' => date('Y-m-d'),
1460 'contribution_status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'),
1461 'membership_type_id' => [23, $membershipTypeAnnualRolling['id']],
1462 'max_related' => '',
1464 'record_contribution' => 1,
1466 'total_amount' => $this->formatMoneyInput(150.00),
1467 'financial_type_id' => '2',
1468 'soft_credit_type_id' => '11',
1469 'soft_credit_contact_id' => '',
1470 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
1471 'receipt_text' => '',
1473 $form->_contactID
= $this->_individualId
;
1474 $form->testSubmit($params);
1475 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId
]);
1476 $contribution = $this->callAPISuccess('Contribution', 'get', [
1477 'contact_id' => $this->_individualId
,
1479 $endDate = (new DateTime(date('Y-m-d')))->modify("+3 years")->modify("-1 day");
1480 $endDate = $endDate->format("Y-m-d");
1482 $this->assertEquals($endDate, $membership['end_date'], 'Membership end date should be ' . $endDate);
1483 $this->assertEquals(1, count($contribution['values']), 'Pending contribution should be created.');
1484 $contribution = $contribution['values'][$contribution['id']];
1485 $additionalPaymentForm = new CRM_Contribute_Form_AdditionalPayment();
1486 $additionalPaymentForm->testSubmit([
1487 'total_amount' => 150.00,
1488 'trxn_date' => date("Y-m-d H:i:s"),
1489 'payment_instrument_id' => array_search('Check', $this->paymentInstruments
),
1490 'check_number' => 'check-12345',
1492 'currency' => 'USD',
1494 'financial_type_id' => 1,
1496 'payment_processor_id' => 0,
1497 'contact_id' => $this->_individualId
,
1498 'contribution_id' => $contribution['id'],
1500 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId
]);
1501 $contribution = $this->callAPISuccess('Contribution', 'get', [
1502 'contact_id' => $this->_individualId
,
1503 'contribution_status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
1505 $this->assertEquals($endDate, $membership['end_date'], 'Membership end date should be same (' . $endDate . ') after payment');
1506 $this->assertEquals(1, count($contribution['values']), 'Completed contribution should be fetched.');