Skip to content

Commit

Permalink
Merge pull request #20436 from eileenmcnaughton/mem_test_fix
Browse files Browse the repository at this point in the history
Fix separate payment membership test to create valid financial transa…
  • Loading branch information
seamuslee001 authored May 28, 2021
2 parents dde08cc + 765d02a commit ed642f8
Show file tree
Hide file tree
Showing 4 changed files with 529 additions and 488 deletions.
9 changes: 4 additions & 5 deletions CRM/Contribute/Form/Contribution/Confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2055,10 +2055,6 @@ public static function submit($params) {
$form->controller = new CRM_Contribute_Controller_Contribution();
$params['invoiceID'] = md5(uniqid(rand(), TRUE));

// We want to move away from passing in amount as it is calculated by the actually-submitted params.
if ($form->getMainContributionAmount($params)) {
$params['amount'] = $form->getMainContributionAmount($params);
}
$paramsProcessedForForm = $form->_params = self::getFormParams($params['id'], $params);

$order = new CRM_Financial_BAO_Order();
Expand All @@ -2069,7 +2065,10 @@ public static function submit($params) {
$order->setOverrideTotalAmount($params['amount']);
}
$amount = $order->getTotalAmount();
$form->_amount = $params['amount'] = $form->_params['amount'] = $params['amount'] ?? $amount;
if ($form->_separateMembershipPayment) {
$amount -= $order->getMembershipTotalAmount();
}
$form->_amount = $params['amount'] = $form->_params['amount'] = $amount;
// hack these in for test support.
$form->_fields['billing_first_name'] = 1;
$form->_fields['billing_last_name'] = 1;
Expand Down
17 changes: 16 additions & 1 deletion CRM/Financial/BAO/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ public function getTotalTaxAmount() :float {
}

/**
* Get the total tax amount for the order.
* Get the total amount for the order.
*
* @return float
*
Expand All @@ -484,6 +484,21 @@ public function getTotalAmount() :float {
return $amount;
}

/**
* Get the total amount relating to memberships for the order.
*
* @return float
*
* @throws \CiviCRM_API3_Exception
*/
public function getMembershipTotalAmount() :float {
$amount = 0.0;
foreach ($this->getMembershipLineItems() as $lineItem) {
$amount += ($lineItem['line_total'] ?? 0.0) + ($lineItem['tax_amount'] ?? 0.0);
}
return $amount;
}

/**
* Get the tax rate for the given financial type.
*
Expand Down
101 changes: 97 additions & 4 deletions tests/phpunit/CRMTraits/Financial/PriceSetTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
+--------------------------------------------------------------------+
*/

use Civi\Api4\PriceSet;

/**
* Trait PriceSetTrait
*
Expand All @@ -23,7 +25,7 @@ trait CRMTraits_Financial_PriceSetTrait {
*
* @return int
*/
protected function getPriceSetID($key = 'membership'):int {
protected function getPriceSetID(string $key = 'membership'):int {
return $this->ids['PriceSet'][$key];
}

Expand All @@ -34,7 +36,7 @@ protected function getPriceSetID($key = 'membership'):int {
*
* @return int
*/
protected function getPriceFieldID($key = 'membership'):int {
protected function getPriceFieldID(string $key = 'membership'):int {
return $this->ids['PriceField'][$key];
}

Expand All @@ -47,8 +49,8 @@ protected function getPriceFieldID($key = 'membership'):int {
* @param array $lineItemFinancialTypes
* Financial Types, if an override is intended.
*/
protected function createContributionWithTwoLineItemsAgainstPriceSet($params, $lineItemFinancialTypes = []): void {
$params = array_merge([
protected function createContributionWithTwoLineItemsAgainstPriceSet($params, array $lineItemFinancialTypes = []): void {
$params = (array) array_merge([
'total_amount' => 300,
'financial_type_id' => 'Donation',
'contribution_status_id' => 'Pending',
Expand Down Expand Up @@ -118,4 +120,95 @@ protected function createMembershipPriceSet(): array {
];
}

/**
* Set up a membership block (quick config) price set.
*
* This creates a price set consistent with a contribution
* page with non-quick config membership and an optional
* additional contribution non-membership amount.
*
* @param array $membershipTypeParams
*
* @throws \API_Exception
* @throws \CiviCRM_API3_Exception
*/
protected function setUpMembershipBlockPriceSet(array $membershipTypeParams = []): void {
$this->ids['PriceSet']['membership_block'] = PriceSet::create(FALSE)
->setValues([
'is_quick_config' => TRUE,
'extends' => 'CiviMember',
'name' => 'Membership Block',
'title' => 'Membership, not quick config',
])
->execute()->first()['id'];

if (empty($this->ids['MembershipType'])) {
$membershipTypeParams = array_merge([
'minimum_fee' => 2,
], $membershipTypeParams);
$this->ids['MembershipType'] = [$this->membershipTypeCreate($membershipTypeParams)];
}
$priceField = $this->callAPISuccess('price_field', 'create', [
'price_set_id' => $this->ids['PriceSet']['membership_block'],
'name' => 'membership_amount',
'label' => 'Membership Amount',
'html_type' => 'Radio',
'sequential' => 1,
]);
$this->ids['PriceField']['membership'] = $priceField['id'];

foreach ($this->ids['MembershipType'] as $membershipTypeID) {
$priceFieldValue = $this->callAPISuccess('price_field_value', 'create', [
'name' => 'membership_amount',
'label' => 'Membership Amount',
'amount' => CRM_Member_BAO_MembershipType::getMembershipType($membershipTypeID)['minimum_fee'],
'financial_type_id' => 'Donation',
'format.only_id' => TRUE,
'membership_type_id' => $membershipTypeID,
'price_field_id' => $priceField['id'],
]);
$key = 'membership_' . strtolower(CRM_Member_BAO_MembershipType::getMembershipType($membershipTypeID)['name']);
$this->ids['PriceFieldValue'][$key] = $priceFieldValue;
}
if (!empty($this->ids['MembershipType']['org2'])) {
$priceField = $this->callAPISuccess('price_field', 'create', [
'price_set_id' => reset($this->_ids['price_set']),
'name' => 'membership_org2',
'label' => 'Membership Org2',
'html_type' => 'Checkbox',
'sequential' => 1,
]);
$this->ids['PriceField']['org2'] = $priceField['id'];

$priceFieldValue = $this->callAPISuccess('price_field_value', 'create', [
'name' => 'membership_org2',
'label' => 'Membership org 2',
'amount' => 55,
'financial_type_id' => 'Member Dues',
'format.only_id' => TRUE,
'membership_type_id' => $this->ids['MembershipType']['org2'],
'price_field_id' => $priceField['id'],
]);
$this->ids['PriceFieldValue']['org2'] = $priceFieldValue;
}
$priceField = $this->callAPISuccess('price_field', 'create', [
'price_set_id' => $this->ids['PriceSet']['membership_block'],
'name' => 'Contribution',
'label' => 'Contribution',
'html_type' => 'Text',
'sequential' => 1,
'is_enter_qty' => 1,
]);
$this->ids['PriceField']['contribution'] = $priceField['id'];
$priceFieldValue = $this->callAPISuccess('price_field_value', 'create', [
'name' => 'contribution',
'label' => 'Give me money',
'amount' => 88,
'financial_type_id' => 'Donation',
'format.only_id' => TRUE,
'price_field_id' => $priceField['id'],
]);
$this->ids['PriceFieldValue']['contribution'] = $priceFieldValue;
}

}
Loading

0 comments on commit ed642f8

Please sign in to comment.