From: Eileen McNaughton Date: Mon, 3 Jul 2023 06:53:12 +0000 (+1200) Subject: Fix ChangeFeeSelectionTest to use eventCreatePaid X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=341ba2661e3f49ee3e8dec2956a283f22c837a4b;p=civicrm-core.git Fix ChangeFeeSelectionTest to use eventCreatePaid --- diff --git a/CRM/Price/BAO/LineItem.php b/CRM/Price/BAO/LineItem.php index 2e9d4e2fa5..31526ced77 100644 --- a/CRM/Price/BAO/LineItem.php +++ b/CRM/Price/BAO/LineItem.php @@ -582,7 +582,7 @@ WHERE li.contribution_id = %1"; * [5] => ['price_field_id' => 5, 'price_field_value_id' => x, 'label....qty...unit_price...line_total...financial_type_id] * */ - public static function buildLineItemsForSubmittedPriceField($priceParams, $overrideAmount = NULL, $financialTypeID = NULL) { + public static function buildLineItemsForSubmittedPriceField($priceParams, $overrideAmount = NULL, $financialTypeID = NULL): array { $lineItems = []; foreach ($priceParams as $key => $value) { $priceField = self::getPriceFieldMetaData($key); diff --git a/tests/phpunit/CRM/Event/BAO/ChangeFeeSelectionTest.php b/tests/phpunit/CRM/Event/BAO/ChangeFeeSelectionTest.php index 1e33cbcbfc..5b927de79b 100644 --- a/tests/phpunit/CRM/Event/BAO/ChangeFeeSelectionTest.php +++ b/tests/phpunit/CRM/Event/BAO/ChangeFeeSelectionTest.php @@ -1,7 +1,8 @@ _contactId = $this->individualCreate(); - $event = $this->eventCreate(['is_monetary' => 1]); + $event = $this->eventCreatePaid(); $this->_eventId = $event['id']; - $this->_priceSetID = $this->priceSetCreate(); - CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $this->_priceSetID); - $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($this->_priceSetID); - $priceSet = $priceSet[$this->_priceSetID] ?? NULL; + $this->_priceSetID = $this->getPriceSetID('PaidEvent'); + $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($this->ids['PriceSet']['PaidEvent']); + $priceSet = $priceSet[$this->ids['PriceSet']['PaidEvent']] ?? NULL; $this->_feeBlock = $priceSet['fields'] ?? NULL; } @@ -81,106 +81,16 @@ class CRM_Event_BAO_ChangeFeeSelectionTest extends CiviUnitTestCase { * Clean up after test. */ public function tearDown(): void { - $this->eventDelete($this->_eventId); $this->quickCleanUpFinancialEntities(); parent::tearDown(); } - /** - * Create an event with a price set. - * - * @param string $type - * @param array $params - * - * @return int - * @throws \CRM_Core_Exception - * @todo resolve this with parent function. - */ - protected function priceSetCreate(string $type = 'Radio', array $params = []): int { - $priceSetID = PriceSet::create()->setValues(array_merge([ - 'extends' => 1, - 'is_active' => FALSE, - 'title' => $type, - 'name' => $type, - ], $params))->execute()->first()['id']; - - if ($type === 'Text') { - $paramsField = [ - 'label' => 'Text Price Field', - 'name' => CRM_Utils_String::titleToVar('text_price_field'), - 'html_type' => 'Text', - 'option_label' => ['1' => 'Text Price Field'], - 'option_name' => ['1' => CRM_Utils_String::titleToVar('text_price_field')], - 'option_weight' => ['1' => 1], - 'option_amount' => ['1' => 10], - 'option_count' => [1 => 1], - 'is_display_amounts' => 1, - 'weight' => 1, - 'options_per_line' => 1, - 'is_active' => ['1' => 1], - 'price_set_id' => $priceSetID, - 'is_enter_qty' => 1, - 'financial_type_id' => $this->getFinancialTypeID('Event Fee'), - ]; - } - else { - /** @noinspection UnNecessaryDoubleQuotesInspection */ - $paramsField = [ - 'label' => 'Price Field', - 'name' => CRM_Utils_String::titleToVar('Two Options'), - 'html_type' => 'Radio', - //'price' => $feeTotal, - 'option_label' => ['1' => 'Expensive Room', '2' => 'Cheap Room', '3' => 'Very Expensive', '4' => 'No Fee'], - 'option_value' => ['1' => 'E', '2' => 'C', '3' => 'V', '4' => 'N'], - 'option_name' => ['1' => 'Expensive', '2' => 'Cheap', '3' => 'Very Expensive'], - 'option_weight' => ['1' => 1, '2' => 2, '3' => 3, '4' => 4], - 'option_amount' => ['1' => $this->_expensiveFee, '2' => $this->_cheapFee, '3' => $this->_veryExpensive, '4' => $this->_noFee], - 'option_count' => [1 => 1, 2 => 1, 3 => 1, 4 => 1], - 'is_display_amounts' => 1, - 'weight' => 1, - 'options_per_line' => 1, - 'is_active' => ['1' => 1], - 'price_set_id' => $priceSetID, - 'is_enter_qty' => 1, - 'financial_type_id' => $this->getFinancialTypeID('Event Fee'), - ]; - } - $field = CRM_Price_BAO_PriceField::create($paramsField); - $values = $this->callAPISuccess('PriceFieldValue', 'get', [ - 'price_field_id' => $field->id, - 'return' => ['id', 'label'], - ]); - foreach ($values['values'] as $value) { - switch ($value['label']) { - case 'Expensive Room': - $this->expensiveFeeValueID = $value['id']; - break; - - case 'Cheap Room': - $this->cheapFeeValueID = $value['id']; - break; - - case 'Very Expensive': - $this->veryExpensiveFeeValueID = $value['id']; - break; - - case 'No Fee': - $this->noFeeID = $value['id']; - break; - - } - } - - $this->priceSetFieldID = $field->id; - return $priceSetID; - } - /** * Get the total for the invoice. * * @param int $contributionID * - * @return mixed + * @return float * @noinspection PhpDocMissingThrowsInspection * @noinspection PhpUnhandledExceptionInspection */ @@ -236,13 +146,13 @@ class CRM_Event_BAO_ChangeFeeSelectionTest extends CiviUnitTestCase { */ public function registerParticipantAndPay($actualPaidAmt = NULL): void { $actualPaidAmt = $actualPaidAmt ?: $this->_expensiveFee; - $lineItems = CRM_Price_BAO_LineItem::buildLineItemsForSubmittedPriceField(['price_' . $this->priceSetFieldID => $this->expensiveFeeValueID]); + $lineItems = CRM_Price_BAO_LineItem::buildLineItemsForSubmittedPriceField(['price_' . $this->ids['PriceField']['PaidEvent'] => $this->getExpensiveValueID()]); $orderParams = [ 'total_amount' => $this->_expensiveFee, 'source' => 'Test set with information', 'currency' => 'USD', 'receipt_date' => date('Y-m-d') . ' 00:00:00', - 'contact_id' => $this->_contactId, + 'contact_id' => $this->ids['Contact']['individual_0'], 'financial_type_id' => 4, 'payment_instrument_id' => 4, 'contribution_status_id' => 'Pending', @@ -256,12 +166,12 @@ class CRM_Event_BAO_ChangeFeeSelectionTest extends CiviUnitTestCase { 'params' => [ 'send_receipt' => 1, 'is_pay_later' => 0, - 'event_id' => $this->_eventId, + 'event_id' => $this->getEventID(), 'register_date' => date('Y-m-d') . ' 00:00:00', 'role_id' => 1, 'status_id' => 1, - 'source' => 'Event_' . $this->_eventId, - 'contact_id' => $this->_contactId, + 'source' => 'Event_' . $this->getEventID(), + 'contact_id' => $this->ids['Contact']['individual_0'], ], ]; } @@ -287,17 +197,17 @@ class CRM_Event_BAO_ChangeFeeSelectionTest extends CiviUnitTestCase { $this->isValidateFinancialsOnPostAssert = FALSE; $this->registerParticipantAndPay(); - $priceSetParams['price_' . $this->priceSetFieldID] = $this->cheapFeeValueID; + $priceSetParams['price_' . $this->ids['PriceField']['PaidEvent']] = $this->getCheapFeeID(); CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->ids['Participant']['order'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock); $this->balanceCheck($this->_cheapFee); - $priceSetParams['price_' . $this->priceSetFieldID] = $this->expensiveFeeValueID; + $priceSetParams['price_' . $this->getPriceFieldID('PaidEvent')] = $this->getExpensiveValueID(); CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->ids['Participant']['order'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock); $this->balanceCheck($this->_expensiveFee); - $priceSetParams['price_' . $this->priceSetFieldID] = $this->veryExpensiveFeeValueID; + $priceSetParams[$this->getPriceFieldFormLabel('PaidEvent')] = $this->getVeryExpensiveID(); CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->ids['Participant']['order'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock); $this->balanceCheck($this->_veryExpensive); } @@ -312,7 +222,7 @@ class CRM_Event_BAO_ChangeFeeSelectionTest extends CiviUnitTestCase { $partiallyPaidContributionStatus = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Partially paid'); $this->assertEquals($this->callAPISuccessGetValue('Contribution', ['id' => $this->ids['Contribution']['order'], 'return' => 'contribution_status_id']), $partiallyPaidContributionStatus); - $priceSetParams['price_' . $this->priceSetFieldID] = $this->veryExpensiveFeeValueID; + $priceSetParams[$this->getPriceFieldFormLabel('PaidEvent')] = $this->getVeryExpensiveID(); CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->ids['Participant']['order'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock); $this->assertEquals($this->callAPISuccessGetValue('Contribution', ['id' => $this->ids['Contribution']['order'], 'return' => 'contribution_status_id']), $partiallyPaidContributionStatus); } @@ -325,13 +235,13 @@ class CRM_Event_BAO_ChangeFeeSelectionTest extends CiviUnitTestCase { public function testCRM20611(): void { $this->registerParticipantAndPay(); $actualPaidAmount = 100; - $priceSetParams['price_' . $this->priceSetFieldID] = $this->expensiveFeeValueID; + $priceSetParams[$this->getPriceFieldFormLabel('PaidEvent')] = $this->getExpensiveValueID(); CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->ids['Participant']['order'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock); $this->balanceCheck($this->_expensiveFee); $contributionBalance = ($this->_expensiveFee - $actualPaidAmount); $this->assertEquals($contributionBalance, CRM_Contribute_BAO_Contribution::getContributionBalance($this->ids['Contribution']['order'])); - $priceSetParams['price_' . $this->priceSetFieldID] = $this->cheapFeeValueID; + $priceSetParams[$this->getPriceFieldFormLabel('PaidEvent')] = $this->getCheapFeeID(); CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->ids['Participant']['order'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock); $this->balanceCheck($this->_cheapFee); $contributionBalance = ($this->_cheapFee - $actualPaidAmount); @@ -339,11 +249,11 @@ class CRM_Event_BAO_ChangeFeeSelectionTest extends CiviUnitTestCase { $this->callAPISuccess('Payment', 'create', [ 'contribution_id' => $this->ids['Contribution']['order'], - 'total_amount' => -120, + 'total_amount' => -300, 'payment_instrument_id' => 3, 'participant_id' => $this->ids['Participant']['order'], ]); - $contributionBalance += 120; + $contributionBalance += 300; $this->assertEquals($contributionBalance, CRM_Contribute_BAO_Contribution::getContributionBalance($this->ids['Contribution']['order'])); // retrieve the cancelled line-item information @@ -373,21 +283,28 @@ class CRM_Event_BAO_ChangeFeeSelectionTest extends CiviUnitTestCase { * @throws \CRM_Core_Exception */ public function testCRM21513(): void { - $this->_priceSetID = $this->priceSetCreate('Text'); - CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $this->_priceSetID); - $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($this->_priceSetID); - $priceSet = $priceSet[$this->_priceSetID] ?? NULL; - $this->_feeBlock = $priceSet['fields'] ?? NULL; + $textPriceFieldID = PriceField::create()->setValues([ + 'price_set_id' => $this->getPriceSetID('PaidEvent'), + 'label' => 'Text Price Field', + 'name' => 'text_price_field', + 'html_type' => 'Text', + 'is_enter_qty' => 1, + ])->execute()->first()['id']; + + PriceFieldValue::create()->setValues(['financial_type_id:name' => 'Event Fee', 'price_field_id' => $textPriceFieldID, 'amount' => 10, 'label' => 'ten'])->execute(); + $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($this->getPriceSetID('PaidEvent')); + $priceSet = $priceSet[$this->getPriceSetID('PaidEvent')]; + $feeBlock = $priceSet['fields'] ?? NULL; $params = [ 'send_receipt' => 1, 'is_test' => 0, 'is_pay_later' => 0, - 'event_id' => $this->_eventId, + 'event_id' => $this->getEventID(), 'register_date' => date('Y-m-d') . ' 00:00:00', 'role_id' => 1, 'status_id' => 1, - 'source' => 'Event_' . $this->_eventId, + 'source' => 'Event_' . $this->getEventID(), 'contact_id' => $this->_contactId, ]; $participant = $this->callAPISuccess('Participant', 'create', $params); @@ -408,25 +325,25 @@ class CRM_Event_BAO_ChangeFeeSelectionTest extends CiviUnitTestCase { $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams); $this->ids['Contribution']['order'] = $contribution['id']; - $this->callAPISuccess('participant_payment', 'create', [ + $this->callAPISuccess('ParticipantPayment', 'create', [ 'participant_id' => $this->ids['Participant']['order'], 'contribution_id' => $this->ids['Contribution']['order'], ]); // CASE 1: Choose text price qty 1 (x$10 = $10 amount) - $priceSetParams['price_' . $this->priceSetFieldID] = 1; + $priceSetParams['price_' . $textPriceFieldID] = 1; $lineItem = $this->getParticipantLineItems(); - CRM_Price_BAO_PriceSet::processAmount($this->_feeBlock, $priceSetParams, $lineItem); + CRM_Price_BAO_PriceSet::processAmount($feeBlock, $priceSetParams, $lineItem); $lineItemVal[$this->_priceSetID] = $lineItem; CRM_Price_BAO_LineItem::processPriceSet($this->ids['Participant']['order'], $lineItemVal, $this->getContributionObject($contribution['id']), 'civicrm_participant'); // CASE 2: Choose text price qty 3 (x$10 = $30 amount) - $priceSetParams['price_' . $this->priceSetFieldID] = 3; + $priceSetParams['price_' . $textPriceFieldID] = 3; $lineItem = $this->getParticipantLineItems(); - CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $participant['id'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock); + CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $participant['id'], 'participant', $this->ids['Contribution']['order'], $feeBlock); // CASE 3: Choose text price qty 2 (x$10 = $20 amount) - CRM_Price_BAO_LineItem::changeFeeSelections(['price_' . $this->priceSetFieldID => 2], $participant['id'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock); + CRM_Price_BAO_LineItem::changeFeeSelections(['price_' . $textPriceFieldID => 2], $participant['id'], 'participant', $this->ids['Contribution']['order'], $feeBlock); $financialItems = $this->callAPISuccess('FinancialItem', 'Get', [ 'entity_table' => 'civicrm_line_item', @@ -480,21 +397,21 @@ class CRM_Event_BAO_ChangeFeeSelectionTest extends CiviUnitTestCase { $this->registerParticipantAndPay(); $this->validateContribution($this->_expensiveFee, 'Completed'); - $priceSetParams['price_' . $this->priceSetFieldID] = $this->cheapFeeValueID; + $priceSetParams[$this->getPriceFieldFormLabel('PaidEvent')] = $this->getCheapFeeID(); $lineItem = CRM_Price_BAO_LineItem::getLineItems($this->ids['Participant']['order']); $this->assertEquals($this->_expensiveFee, $lineItem[1]['line_total']); CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->ids['Participant']['order'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock); $this->validateContribution($this->_cheapFee, 'Pending refund'); - $priceSetParams['price_' . $this->priceSetFieldID] = $this->expensiveFeeValueID; + $priceSetParams[$this->getPriceFieldFormLabel('PaidEvent')] = $this->getExpensiveValueID(); $lineItem = $this->getParticipantLineItems(); $this->assertEquals('0.00', $lineItem[1]['line_total']); $this->assertEquals($this->_cheapFee, $lineItem[2]['line_total']); CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->ids['Participant']['order'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock); $this->validateContribution($this->_cheapFee, 'Completed'); - $priceSetParams['price_' . $this->priceSetFieldID] = $this->veryExpensiveFeeValueID; + $priceSetParams[$this->getPriceFieldFormLabel('PaidEvent')] = $this->getVeryExpensiveID(); $lineItem = $this->getParticipantLineItems(); $this->assertEquals($this->_expensiveFee, $lineItem[1]['line_total']); $this->assertEquals('0.00', $lineItem[2]['line_total']); @@ -516,14 +433,14 @@ class CRM_Event_BAO_ChangeFeeSelectionTest extends CiviUnitTestCase { public function testRefundWithFeeAmount0(): void { $this->registerParticipantAndPay(); $actualPaidAmount = 100; - $priceSetParams['price_' . $this->priceSetFieldID] = $this->expensiveFeeValueID; + $priceSetParams[$this->getPriceFieldFormLabel('PaidEvent')] = $this->getExpensiveValueID(); CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->ids['Participant']['order'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock); $this->balanceCheck($this->_expensiveFee); $contributionBalance = ($this->_expensiveFee - $actualPaidAmount); $this->assertEquals($contributionBalance, CRM_Contribute_BAO_Contribution::getContributionBalance($this->ids['Contribution']['order'])); - $priceSetParams['price_' . $this->priceSetFieldID] = $this->noFeeID; + $priceSetParams[$this->getPriceFieldFormLabel('PaidEvent')] = $this->ids['PriceFieldValue']['PaidEvent_free']; CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->ids['Participant']['order'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock); $this->balanceCheck($this->_noFee); $contributionBalance = ($this->_noFee - $actualPaidAmount); @@ -566,7 +483,7 @@ class CRM_Event_BAO_ChangeFeeSelectionTest extends CiviUnitTestCase { */ public function testPartialPaymentEntries(): void { $this->registerParticipantAndPay($this->_expensiveFee); - $priceSetParams['price_' . $this->priceSetFieldID] = $this->veryExpensiveFeeValueID; + $priceSetParams[$this->getPriceFieldFormLabel('PaidEvent')] = $this->getVeryExpensiveID(); CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->ids['Participant']['order'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock); $actualResults = $this->callAPISuccess('EntityFinancialTrxn', 'get', ['sequential' => 1, 'entity_table' => 'civicrm_financial_item'])['values']; $this->assertCount(3, $actualResults); @@ -588,7 +505,7 @@ class CRM_Event_BAO_ChangeFeeSelectionTest extends CiviUnitTestCase { ], [ 'id' => 5, - 'amount' => 120.00, + 'amount' => 300.00, 'entity_id' => 3, 'financial_trxn_id' => 2, 'entity_table' => 'civicrm_financial_item', @@ -606,7 +523,7 @@ class CRM_Event_BAO_ChangeFeeSelectionTest extends CiviUnitTestCase { */ public function testRefundPaymentEntries(): void { $this->registerParticipantAndPay($this->_expensiveFee); - $priceSetParams['price_' . $this->priceSetFieldID] = $this->cheapFeeValueID; + $priceSetParams[$this->getPriceFieldFormLabel('PaidEvent')] = $this->getCheapFeeID(); CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $this->ids['Participant']['order'], 'participant', $this->ids['Contribution']['order'], $this->_feeBlock); $actualResults = $this->callAPISuccess('EntityFinancialTrxn', 'get', ['sequential' => 1, 'entity_table' => 'civicrm_financial_item', 'return' => ['amount', 'entity_id']])['values']; $expectedResults = [ @@ -623,7 +540,7 @@ class CRM_Event_BAO_ChangeFeeSelectionTest extends CiviUnitTestCase { ], [ 'id' => 5, - 'amount' => 80.00, + 'amount' => 50.00, 'entity_id' => 3, ], ]; @@ -663,4 +580,31 @@ class CRM_Event_BAO_ChangeFeeSelectionTest extends CiviUnitTestCase { return CRM_Price_BAO_LineItem::getLineItems($this->ids['Participant']['order']); } + /** + * Get the ID for the $50 price field value option. + * + * @return int + */ + protected function getCheapFeeID(): int { + return $this->ids['PriceFieldValue']['PaidEvent_student_early']; + } + + /** + * Get the ID for the $100 price field value option. + * + * @return int + */ + protected function getExpensiveValueID(): int { + return $this->ids['PriceFieldValue']['PaidEvent_student']; + } + + /** + * Get the ID for the $300 price field value option. + * + * @return int + */ + protected function getVeryExpensiveID(): int { + return $this->ids['PriceFieldValue']['PaidEvent_standard']; + } + }