Skip to content

Commit

Permalink
Php8.2 test fix, remove one instance on undeclared parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Feb 19, 2023
1 parent aba06aa commit 24e6636
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 70 deletions.
90 changes: 42 additions & 48 deletions tests/phpunit/CRM/Event/Form/ParticipantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase {

use CRMTraits_Financial_OrderTrait;
use CRMTraits_Financial_PriceSetTrait;

/**
* Options on the from Email address array.
*
Expand All @@ -19,7 +21,7 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase {
protected $fromEmailAddressOptions = [];

public function setUp(): void {
$this->useTransaction(TRUE);
$this->useTransaction();
parent::setUp();
}

Expand All @@ -44,7 +46,7 @@ public function testSubmit(): void {
*
* @throws \Exception
*/
public function testSubmitUnpaidPriceChangeWhileStillPending() {
public function testSubmitUnpaidPriceChangeWhileStillPending(): void {
$form = $this->getForm(['is_monetary' => 1, 'financial_type_id' => 1]);
$form->_quickConfig = TRUE;

Expand Down Expand Up @@ -72,13 +74,13 @@ public function testSubmitUnpaidPriceChangeWhileStillPending() {
],
];
$form->setAction(CRM_Core_Action::ADD);
$form->_priceSetId = $this->getPriceSetID();
$form->_priceSetId = $this->getPriceSetID('event');
$form->submit([
'register_date' => date('Ymd'),
'status_id' => 5,
'role_id' => 1,
'event_id' => $form->_eventId,
'priceSetId' => $this->getPriceSetID(),
'priceSetId' => $this->getPriceSetID('event'),
$this->getPriceFieldKey() => $this->_ids['price_field_value'][0],
'is_pay_later' => 1,
'amount_level' => 'Too much',
Expand Down Expand Up @@ -106,9 +108,10 @@ public function testSubmitUnpaidPriceChangeWhileStillPending() {
}
$this->assertEquals(55, $sum);

CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $participant['id'], 'participant', $contribution['id'], $this->eventFeeBlock, $lineItem);
// Check that no payment records have been created.
// In https://lab.civicrm.org/dev/financial/issues/94 we had an issue where payments were created when none happend.
$priceSetID = $this->ids['PriceSet']['event'];
$eventFeeBlock = CRM_Price_BAO_PriceSet::getSetDetail($priceSetID)[$priceSetID]['fields'];
CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $participant['id'], 'participant', $contribution['id'], $eventFeeBlock, $lineItem); // Check that no payment records have been created.
// In https://lab.civicrm.org/dev/financial/issues/94 we had an issue where payments were created when none happened.
$payments = $this->callAPISuccess('Payment', 'get', [])['values'];
$this->assertCount(0, $payments);
$lineItem = CRM_Price_BAO_LineItem::getLineItems($participant['id'], 'participant');
Expand Down Expand Up @@ -138,13 +141,13 @@ public function testPaymentAllocationOnMultiLineItemEvent(): void {
// 5. Record the additional amount which $40 ($50-$10)
// Expected : Check the amount of new Financial Item created is $40
$this->createParticipantRecordsFromTwoFieldPriceSet();
$priceSetBlock = CRM_Price_BAO_PriceSet::getSetDetail($this->getPriceSetID(), TRUE, FALSE)[$this->getPriceSetID()]['fields'];
$priceSetBlock = CRM_Price_BAO_PriceSet::getSetDetail($this->getPriceSetID('event'), TRUE, FALSE)[$this->getPriceSetID('event')]['fields'];

$priceSetParams = [
'priceSetId' => $this->getPriceSetID(),
'priceSetId' => $this->getPriceSetID('event'),
// The 1 & 5 refer to qty as they are text fields.
'price_' . $this->_ids['price_field']['first_text_field'] => 5,
'price_' . $this->_ids['price_field']['second_text_field'] => 1,
'price_' . $this->ids['PriceField']['first_text_field'] => 5,
'price_' . $this->ids['PriceField']['second_text_field'] => 1,
];
$participant = $this->callAPISuccess('Participant', 'get', []);
$lineItem = CRM_Price_BAO_LineItem::getLineItems($participant['id'], 'participant');
Expand Down Expand Up @@ -185,7 +188,7 @@ public function testPaymentAllocationOnMultiLineItemEvent(): void {
*
* @throws \Exception
*/
public function testSubmitWithPayment($thousandSeparator) {
public function testSubmitWithPayment(string $thousandSeparator): void {
$this->setCurrencySeparators($thousandSeparator);
$form = $this->getForm(['is_monetary' => 1, 'financial_type_id' => 1]);
$form->_mode = 'Live';
Expand All @@ -207,7 +210,7 @@ public function testSubmitWithPayment($thousandSeparator) {
'unit_price' => 1550.55,
'line_total' => 1550.55,
'participant_count' => 0,
'price_field_id' => $this->_ids['price_field'][0],
'price_field_id' => $this->getPriceFieldID(),
'price_field_value_id' => $this->_ids['price_field_value'][1],
'tax_amount' => 0,
// Interestingly the financial_type_id set in this test is ignored but currently locking in what is happening with this test so setting to 'actual'
Expand All @@ -228,7 +231,7 @@ public function testSubmitWithPayment($thousandSeparator) {
*
* @throws \Exception
*/
public function testSubmitWithFailedPayment($thousandSeparator, $fromEmails = []) {
public function testSubmitWithFailedPayment(string $thousandSeparator, $fromEmails = []) {
$this->setCurrencySeparators($thousandSeparator);
$form = $this->getForm(['is_monetary' => 1, 'financial_type_id' => 1]);
$form->_mode = 'Live';
Expand Down Expand Up @@ -380,7 +383,7 @@ public function getFromEmailAddress() {
*
* @throws \CRM_Core_Exception
*/
protected function createParticipantRecordsFromTwoFieldPriceSet() {
protected function createParticipantRecordsFromTwoFieldPriceSet(): void {
// Create financial type - Event Fee 2
$form = $this->getForm(['is_monetary' => 1, 'financial_type_id' => 1]);

Expand All @@ -393,23 +396,23 @@ protected function createParticipantRecordsFromTwoFieldPriceSet() {
'option_amount' => ['1' => $fieldToCreate['amount']],
'option_weight' => ['1' => $fieldToCreate['amount']],
'is_display_amounts' => 1,
'price_set_id' => $this->_ids['price_set'],
'price_set_id' => $this->getPriceSetID('event'),
'is_enter_qty' => 1,
'html_type' => 'Text',
'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Campaign Contribution'),
];
$fieldParams['label'] = $fieldToCreate['label'];
$fieldParams['name'] = CRM_Utils_String::titleToVar($fieldToCreate['label']);
$fieldParams['price'] = $fieldToCreate['amount'];
$this->_ids['price_field'][strtolower(CRM_Utils_String::titleToVar($fieldToCreate['label']))] = $textPriceFieldID = $this->callAPISuccess('PriceField', 'create', $fieldParams)['id'];
$this->_ids['price_field_value'][strtolower(CRM_Utils_String::titleToVar($fieldToCreate['label']))] = (int) $this->callAPISuccess('PriceFieldValue', 'getsingle', ['price_field_id' => $textPriceFieldID])['id'];
$this->ids['PriceField'][strtolower($fieldParams['name'])] = $textPriceFieldID = $this->callAPISuccess('PriceField', 'create', $fieldParams)['id'];
$this->ids['PriceFieldValue'][strtolower($fieldParams['name'])] = (int) $this->callAPISuccess('PriceFieldValue', 'getsingle', ['price_field_id' => $textPriceFieldID])['id'];
}

$form->_lineItem = [
0 => [
13 => [
'price_field_id' => $this->_ids['price_field']['second_text_field'],
'price_field_value_id' => $this->_ids['price_field_value']['second_text_field'],
'price_field_id' => $this->ids['PriceField']['second_text_field'],
'price_field_value_id' => $this->ids['PriceFieldValue']['second_text_field'],
'label' => 'Event Fee 1',
'field_title' => 'Event Fee 1',
'description' => NULL,
Expand All @@ -427,8 +430,8 @@ protected function createParticipantRecordsFromTwoFieldPriceSet() {
'non_deductible_amount' => '0.00',
],
14 => [
'price_field_id' => $this->_ids['price_field']['first_text_field'],
'price_field_value_id' => $this->_ids['price_field_value']['first_text_field'],
'price_field_id' => $this->ids['PriceField']['first_text_field'],
'price_field_value_id' => $this->ids['PriceFieldValue']['first_text_field'],
'label' => 'Event Fee 2',
'field_title' => 'Event Fee 2',
'description' => NULL,
Expand All @@ -448,17 +451,17 @@ protected function createParticipantRecordsFromTwoFieldPriceSet() {
],
];
$form->setAction(CRM_Core_Action::ADD);
$form->_priceSetId = $this->_ids['price_set'];
$form->_priceSetId = $this->getPriceSetID('event');

$form->submit([
'register_date' => date('Ymd'),
'receive_date' => '2018-09-01',
'status_id' => 5,
'role_id' => 1,
'event_id' => $this->getEventID(),
'priceSetId' => $this->_ids['price_set'],
'price_' . $this->_ids['price_field']['first_text_field'] => [$this->_ids['price_field_value']['first_text_field'] => 1],
'price_' . $this->_ids['price_field']['second_text_field'] => [$this->_ids['price_field_value']['second_text_field'] => 1],
'priceSetId' => $this->getPriceSetID('event'),
'price_' . $this->ids['PriceField']['first_text_field'] => [$this->ids['PriceFieldValue']['first_text_field'] => 1],
'price_' . $this->ids['PriceField']['second_text_field'] => [$this->ids['PriceFieldValue']['second_text_field'] => 1],
'amount_level' => 'Too much',
'fee_amount' => 65,
'total_amount' => 65,
Expand Down Expand Up @@ -499,7 +502,7 @@ private function getSubmitParamsForCreditCardPayment(int $paymentProcessorID): a
'billing_postal_code-5' => 10545,
'billing_country_id-5' => 1228,
'payment_processor_id' => $paymentProcessorID,
'priceSetId' => $this->getPriceSetID(),
'priceSetId' => $this->getPriceSetID('event'),
$this->getPriceFieldKey() => $this->getPriceFieldValueID(),
'amount_level' => 'Too much',
'fee_amount' => $this->formatMoneyInput(1550.55),
Expand All @@ -526,7 +529,7 @@ public function testSubmitWithDeferredRecognition() {
'status_id' => 1,
'role_id' => 1,
$this->getPriceFieldKey() => $this->getPriceFieldValueID(),
'priceSetId' => $this->getPriceSetID(),
'priceSetId' => $this->getPriceSetID('event'),
'event_id' => $this->getEventID(),
'record_contribution' => TRUE,
'amount_level' => 'blah',
Expand All @@ -553,12 +556,12 @@ public function testSubmitWithDeferredRecognition() {
public function testSubmitPartialPayment($isQuickConfig) {
$mut = new CiviMailUtils($this, TRUE);
$form = $this->getForm(['is_monetary' => 1]);
$this->callAPISuccess('PriceSet', 'create', ['is_quick_config' => $isQuickConfig, 'id' => $this->getPriceSetID()]);
$this->callAPISuccess('PriceSet', 'create', ['is_quick_config' => $isQuickConfig, 'id' => $this->getPriceSetID('event')]);
$paymentInstrumentID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check');
$submitParams = [
'hidden_feeblock' => '1',
'hidden_eventFullMsg' => '',
'priceSetId' => $this->getPriceSetID(),
'priceSetId' => $this->getPriceSetID('event'),
$this->getPriceFieldKey() => $this->getPriceFieldValueID(),
'check_number' => '879',
'record_contribution' => '1',
Expand Down Expand Up @@ -599,10 +602,10 @@ public function testSubmitPartialPayment($isQuickConfig) {
*
* @throws \CRM_Core_Exception
*/
public function testSubmitPendingPartiallyPaidAddPayment($isQuickConfig) {
public function testSubmitPendingPartiallyPaidAddPayment(bool $isQuickConfig): void {
$mut = new CiviMailUtils($this, TRUE);
$form = $this->getForm(['is_monetary' => 1]);
$this->callAPISuccess('PriceSet', 'create', ['is_quick_config' => $isQuickConfig, 'id' => $this->getPriceSetID()]);
$this->callAPISuccess('PriceSet', 'create', ['is_quick_config' => $isQuickConfig, 'id' => $this->getPriceSetID('event')]);
$paymentInstrumentID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check');
$submitParams = $this->getRecordContributionParams('Partially paid', $form);
$form->submit($submitParams);
Expand All @@ -626,10 +629,10 @@ public function testSubmitPendingPartiallyPaidAddPayment($isQuickConfig) {
*
* @throws \CRM_Core_Exception
*/
public function testSubmitPendingAddPayment($isQuickConfig) {
public function testSubmitPendingAddPayment($isQuickConfig): void {
$mut = new CiviMailUtils($this, TRUE);
$form = $this->getForm(['is_monetary' => 1]);
$this->callAPISuccess('PriceSet', 'create', ['is_quick_config' => $isQuickConfig, 'id' => $this->getPriceSetID()]);
$this->callAPISuccess('PriceSet', 'create', ['is_quick_config' => $isQuickConfig, 'id' => $this->getPriceSetID('event')]);
$paymentInstrumentID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check');
$submitParams = $this->getRecordContributionParams('Pending from pay later', 'Pending');
// Create the pending contribution for the full amount to be paid.
Expand All @@ -650,7 +653,7 @@ public function testSubmitPendingAddPayment($isQuickConfig) {
* @param bool $isAmountPaidOnForm
* Was the amount paid entered on the form (if so this should be on the receipt)
*/
protected function assertPartialPaymentResult($isQuickConfig, CiviMailUtils $mut, $isAmountPaidOnForm = TRUE) {
protected function assertPartialPaymentResult(bool $isQuickConfig, CiviMailUtils $mut, $isAmountPaidOnForm = TRUE): void {
$paymentInstrumentID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check');
$contribution = $this->callAPISuccessGetSingle('Contribution', []);
$expected = [
Expand Down Expand Up @@ -740,22 +743,13 @@ protected function assertPartialPaymentResult($isQuickConfig, CiviMailUtils $mut
]);
}

/**
* Get the id of the configured price set.
*
* @return int
*/
protected function getPriceSetID() {
return (int) $this->_ids['price_set'];
}

/**
* Get the price field id that has been created for the test.
*
* @return int
*/
protected function getPriceFieldID() {
return (int) $this->_ids['price_field'][0];
protected function getPriceFieldID(): int {
return (int) reset($this->ids['PriceField']);
}

/**
Expand Down Expand Up @@ -784,11 +778,11 @@ protected function getPriceFieldValueID(): int {
*
* @return array
*/
protected function getRecordContributionParams($participantStatus, $contributionStatus): array {
protected function getRecordContributionParams(string $participantStatus, $contributionStatus): array {
$submitParams = [
'hidden_feeblock' => '1',
'hidden_eventFullMsg' => '',
'priceSetId' => $this->getPriceSetID(),
'priceSetId' => $this->getPriceSetID('event'),
$this->getPriceFieldKey() => $this->getPriceFieldValueID(),
'check_number' => '879',
'record_contribution' => '1',
Expand Down
42 changes: 20 additions & 22 deletions tests/phpunit/CiviTest/CiviUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Civi\Api4\MembershipType;
use Civi\Api4\OptionGroup;
use Civi\Api4\Phone;
use Civi\Api4\PriceSet;
use Civi\Api4\RelationshipType;
use Civi\Payment\System;
use Civi\Api4\OptionValue;
Expand Down Expand Up @@ -805,7 +806,7 @@ public function participantCreate(array $params = []) {
* @return int
* Id Payment Processor
*/
public function processorCreate($params = []) {
public function processorCreate($params = []): int {
$processorParams = [
'domain_id' => 1,
'name' => 'Dummy',
Expand Down Expand Up @@ -834,9 +835,9 @@ public function processorCreate($params = []) {
* @return \CRM_Core_Payment_Dummy
* Instance of Dummy Payment Processor
*
* @throws \CRM_Core_Exception
* @noinspection PhpIncompatibleReturnTypeInspection
*/
public function dummyProcessorCreate($processorParams = []) {
public function dummyProcessorCreate(array $processorParams = []): CRM_Core_Payment_Dummy {
$paymentProcessorID = $this->processorCreate($processorParams);
$this->ids['PaymentProcessor']['dummy_test'] = $paymentProcessorID;
// For the tests we don't need a live processor, but as core ALWAYS creates a processor in live mode and one in test mode we do need to create both
Expand Down Expand Up @@ -1072,10 +1073,8 @@ public function eventCreate(array $params = []): array {
* Index for storing event ID in ids array.
*
* @return array
*
* @throws \CRM_Core_Exception
*/
protected function eventCreatePaid(array $params, array $options = [['name' => 'hundy', 'amount' => 100]], $key = 'event'): array {
protected function eventCreatePaid(array $params = [], array $options = [['name' => 'hundred', 'amount' => 100]], $key = 'event'): array {
// @todo - uncomment these - but need to fix an e-notice first.
// $this->dummyProcessorCreate();
// $params['payment_processor'] = [$this->ids['PaymentProcessor']['dummy_live']];
Expand All @@ -1089,9 +1088,6 @@ protected function eventCreatePaid(array $params, array $options = [['name' => '
$this->ids['Event'][$key] = (int) $event['id'];
$this->ids['PriceSet'][$key] = $this->eventPriceSetCreate(55, 0, 'Radio', $options);
CRM_Price_BAO_PriceSet::addTo('civicrm_event', $event['id'], $this->ids['PriceSet'][$key]);
$priceSet = CRM_Price_BAO_PriceSet::getSetDetail($this->ids['PriceSet'][$key], TRUE, FALSE);
$priceSet = $priceSet[$this->ids['PriceSet'][$key]] ?? NULL;
$this->eventFeeBlock = $priceSet['fields'] ?? NULL;
return $event;
}

Expand Down Expand Up @@ -2520,22 +2516,21 @@ protected function assertLike($expectedSQL, $actualSQL, $message = 'different sq
*
* @return int
* Price Set ID.
* @throws \CRM_Core_Exception
* @noinspection PhpUnhandledExceptionInspection
*/
protected function eventPriceSetCreate($feeTotal, $minAmt = 0, $type = 'Text', $options = [['name' => 'hundy', 'amount' => 100]]) {
protected function eventPriceSetCreate($feeTotal, $minAmt = 0, string $type = 'Text', array $options = [['name' => 'hundred', 'amount' => 100]]): int {
// creating price set, price field
$paramsSet['title'] = 'Price Set';
$paramsSet['name'] = CRM_Utils_String::titleToVar('Price Set');
$paramsSet['name'] = 'price_set';
$paramsSet['is_active'] = FALSE;
$paramsSet['extends'] = 1;
$paramsSet['min_amount'] = $minAmt;

$priceSet = CRM_Price_BAO_PriceSet::create($paramsSet);
$this->_ids['price_set'] = $priceSet->id;
$priceSetID = PriceSet::create()->setValues($paramsSet)->execute()->first()['id'];

$paramsField = [
'label' => 'Price Field',
'name' => CRM_Utils_String::titleToVar('Price Field'),
'name' => 'price_field',
'html_type' => $type,
'price' => $feeTotal,
'option_label' => ['1' => 'Price Field'],
Expand All @@ -2547,7 +2542,7 @@ protected function eventPriceSetCreate($feeTotal, $minAmt = 0, $type = 'Text', $
'weight' => 1,
'options_per_line' => 1,
'is_active' => ['1' => 1],
'price_set_id' => $this->_ids['price_set'],
'price_set_id' => $priceSetID,
'is_enter_qty' => 1,
'financial_type_id' => $this->getFinancialTypeId('Event Fee'),
];
Expand All @@ -2560,13 +2555,16 @@ protected function eventPriceSetCreate($feeTotal, $minAmt = 0, $type = 'Text', $
}

}
$this->callAPISuccess('PriceField', 'create', $paramsField);
$fields = $this->callAPISuccess('PriceField', 'get', ['price_set_id' => $this->_ids['price_set']]);
$this->_ids['price_field'] = array_keys($fields['values']);
$fieldValues = $this->callAPISuccess('PriceFieldValue', 'get', ['price_field_id' => $this->_ids['price_field'][0]]);
$this->_ids['price_field_value'] = array_keys($fieldValues['values']);
$priceFieldID = $this->callAPISuccess('PriceField', 'create', $paramsField)['id'];
$this->ids['PriceField']['event_' . strtolower($type)] = $priceFieldID;
$this->_ids['price_field'] = [$priceFieldID];
$fieldValues = $this->callAPISuccess('PriceFieldValue', 'get', ['price_field_id' => $priceFieldID])['values'];
foreach ($fieldValues as $priceFieldValue) {
$this->ids['PriceFieldValue'][$priceFieldValue['name']] = $priceFieldValue['id'];
}
$this->_ids['price_field_value'] = array_keys($fieldValues);

return $this->_ids['price_set'];
return $priceSetID;
}

/**
Expand Down

0 comments on commit 24e6636

Please sign in to comment.