From bca0f60e86a187cd145e0d6edb919683bdd0572c Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Mon, 15 May 2023 12:47:19 +1200 Subject: [PATCH] Fix Participant tests to use full form flow This doesn't get all of them but fixes a bunch --- CRM/Event/Form/Participant.php | 38 ++++++-- .../CRM/Event/Form/ParticipantTest.php | 87 +++++++------------ 2 files changed, 59 insertions(+), 66 deletions(-) diff --git a/CRM/Event/Form/Participant.php b/CRM/Event/Form/Participant.php index ab032fcd34..ea80920ca8 100644 --- a/CRM/Event/Form/Participant.php +++ b/CRM/Event/Form/Participant.php @@ -198,6 +198,8 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment /** * Event id. * + * @internal - use getEventID + * * @var int */ public $_eventId; @@ -229,6 +231,18 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment */ protected $createPaymentParams = []; + /** + * Get the selected Event ID. + * + * @api This function will not change in a minor release and is supported for + * use outside of core. + * + * @return int|null + */ + public function getEventID(): ?int { + return $this->_eventId ?: ($this->getSubmittedValue('event_id') ? (int) $this->getSubmittedValue('event_id') : NULL); + } + /** * Get params to create payments. * @@ -250,14 +264,14 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment /** * Explicitly declare the entity api name. */ - public function getDefaultEntity() { + public function getDefaultEntity(): string { return 'Participant'; } /** * Default form context used as part of addField() */ - public function getDefaultContext() { + public function getDefaultContext(): string { return 'create'; } @@ -363,7 +377,7 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment // when fee amount is included in form if (!empty($_POST['hidden_feeblock']) || !empty($_POST['send_receipt'])) { if ($this->_submitValues['event_id']) { - $this->_eventId = $this->_submitValues['event_id']; + $this->_eventId = (int) $this->_submitValues['event_id']; } CRM_Event_Form_EventFees::preProcess($this); $this->buildEventFeeForm($this); @@ -423,7 +437,7 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment * @return array * @throws \CRM_Core_Exception */ - public function setDefaultValues() { + public function setDefaultValues(): array { if ($this->_showFeeBlock) { return CRM_Event_Form_EventFees::setDefaultValues($this); } @@ -773,8 +787,8 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment * * @return void */ - public function addRules() { - $this->addFormRule(['CRM_Event_Form_Participant', 'formRule'], $this); + public function addRules(): void { + $this->addFormRule(['CRM_Event_Form_Participant', 'formRule']); } /** @@ -927,6 +941,9 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment /** * Submit form. * + * @internal will be made protected / decommissioned once tests + * in core & line item editor are fixed to not call it. + * * @param array $params * * @return string @@ -1370,8 +1387,11 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment /** * Set the various IDs relating to custom data types. + * + * @internal will be made protected once line item editor unit tests + * no longer call it. */ - public function setCustomDataTypes() { + public function setCustomDataTypes(): void { $customDataType = CRM_Core_OptionGroup::values('custom_data_type', FALSE, FALSE, FALSE, NULL, 'name'); $this->_roleCustomDataTypeID = array_search('ParticipantRole', $customDataType); $this->_eventNameCustomDataTypeID = array_search('ParticipantEventName', $customDataType); @@ -1426,9 +1446,11 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment /** * Build the form object. * + * @internal - this will be made protected, once some notice is provided to lineItem + * edit extension which calls it form tests. + * * @param \CRM_Event_Form_Participant $form * - * @return bool * @throws \CRM_Core_Exception * @throws \Exception */ diff --git a/tests/phpunit/CRM/Event/Form/ParticipantTest.php b/tests/phpunit/CRM/Event/Form/ParticipantTest.php index 05288e7979..feece194bf 100644 --- a/tests/phpunit/CRM/Event/Form/ParticipantTest.php +++ b/tests/phpunit/CRM/Event/Form/ParticipantTest.php @@ -13,13 +13,6 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase { use CRMTraits_Financial_OrderTrait; use CRMTraits_Financial_PriceSetTrait; - /** - * Options on the from Email address array. - * - * @var array - */ - protected $fromEmailAddressOptions = []; - public function setUp(): void { parent::setUp(); $this->useTransaction(); @@ -31,13 +24,13 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase { * @throws \Exception */ public function testSubmit(): void { - $form = $this->getForm(); - $form->submit([ + $form = $this->getForm([], [ 'register_date' => date('Ymd'), 'status_id' => 1, 'role_id' => 1, - 'event_id' => $form->_eventId, ]); + $form->postProcess(); + $this->assertEquals($this->getEventID(), $form->getEventID()); $this->callAPISuccessGetSingle('Participant', []); } @@ -129,7 +122,8 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase { } /** - * (dev/core#310) : Test to ensure payments are correctly allocated, when a event fee is changed for a mult-line item event registration + * (dev/core#310) : Test to ensure payments are correctly allocated, when + * an event fee is changed for a multi-line item event registration * * @throws \CRM_Core_Exception */ @@ -332,25 +326,29 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase { * Get prepared form object. * * @param array $eventParams + * @param array $submittedValues * * @return CRM_Event_Form_Participant * * @throws \CRM_Core_Exception */ - protected function getForm($eventParams = []) { + protected function getForm(array $eventParams = [], array $submittedValues = []): CRM_Event_Form_Participant { + $submittedValues['contact_id'] = $this->ids['Contact']['event'] = $this->individualCreate(); + if (!empty($eventParams['is_monetary'])) { $event = $this->eventCreatePaid($eventParams, [['name' => 'big', 'amount' => 1550.55]]); + $submittedValues = array_merge($this->getRecordContributionParams('Partially paid', 'Pending'), $submittedValues); } else { $event = $this->eventCreate($eventParams); } - + $submittedValues['event_id'] = $event['id']; + $_REQUEST['cid'] = $submittedValues['contact_id']; /** @var CRM_Event_Form_Participant $form */ - $form = $this->getFormObject('CRM_Event_Form_Participant'); - $form->_single = TRUE; - $form->_contactID = $form->_contactId = $this->getContactID(); - $form->setCustomDataTypes(); - $form->_eventId = $event['id']; + $form = $this->getFormObject('CRM_Event_Form_Participant', $submittedValues); + $form->preProcess(); + $form->buildForm(); + if (!empty($eventParams['is_monetary'])) { $form->_bltID = 5; $form->_isPaidEvent = TRUE; @@ -358,24 +356,9 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase { $form->assignProcessors(); $form->buildEventFeeForm($form); } - else { - $form->_fromEmails = [ - 'from_email_id' => ['abc@gmail.com' => 1], - ]; - } - $this->fromEmailAddressOptions = $form->_fromEmails['from_email_id']; return $form; } - /** - * Get a valid value for from_email_address. - * - * @return int|string - */ - public function getFromEmailAddress() { - return key($this->fromEmailAddressOptions); - } - /** * Create a Price set with two price field of type Text. * @@ -497,7 +480,7 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase { 'billing_middle_name' => '', 'billing_last_name' => 'Adams', 'billing_street_address-5' => '790L Lincoln St S', - 'billing_city-5' => 'Maryknoll', + 'billing_city-5' => 'Baltimore', 'billing_state_province_id-5' => 1031, 'billing_postal_code-5' => 10545, 'billing_country_id-5' => 1228, @@ -507,7 +490,7 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase { 'amount_level' => 'Too much', 'fee_amount' => $this->formatMoneyInput(1550.55), 'total_amount' => $this->formatMoneyInput(1550.55), - 'from_email_address' => $this->getFromEmailAddress(), + 'from_email_address' => '"FIXME" ', 'send_receipt' => 1, 'receipt_text' => '', ]; @@ -518,7 +501,7 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - public function testSubmitWithDeferredRecognition() { + public function testSubmitWithDeferredRecognition(): void { Civi::settings()->set('deferred_revenue_enabled', TRUE); $futureDate = date('Y') + 1 . '-09-20'; $form = $this->getForm(['is_monetary' => 1, 'financial_type_id' => 1, 'start_date' => $futureDate]); @@ -572,7 +555,7 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase { 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'), 'total_amount' => '20', 'send_receipt' => '1', - 'from_email_address' => $this->getFromEmailAddress(), + 'from_email_address' => '"FIXME" ', 'receipt_text' => 'Contact the Development Department if you need to make any changes to your registration.', 'hidden_custom' => '1', 'hidden_custom_group_count' => ['' => 1], @@ -594,8 +577,6 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase { /** * Test submitting a partially paid event registration, recording a pending contribution. * - * This tests - * * @dataProvider getBooleanDataProvider * * @param bool $isQuickConfig @@ -603,19 +584,18 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase { * @throws \CRM_Core_Exception */ public function testSubmitPendingPartiallyPaidAddPayment(bool $isQuickConfig): void { - $mut = new CiviMailUtils($this, TRUE); + $mailUtil = new CiviMailUtils($this, TRUE); $form = $this->getForm(['is_monetary' => 1]); $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); + $form->postProcess(); $this->callAPISuccess('Payment', 'create', [ 'contribution_id' => $this->callAPISuccessGetValue('Contribution', ['return' => 'id']), 'total_amount' => 20, 'check_number' => 879, 'payment_instrument_id' => $paymentInstrumentID, ]); - $this->assertPartialPaymentResult($isQuickConfig, $mut); + $this->assertPartialPaymentResult($isQuickConfig, $mailUtil); } /** @@ -629,15 +609,12 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - public function testSubmitPendingAddPayment($isQuickConfig): void { + public function testSubmitPendingAddPayment(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('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. - $submitParams['total_amount'] = 1550.55; - $form->submit($submitParams); + $form->postProcess(); $this->callAPISuccess('Payment', 'create', [ 'contribution_id' => $this->callAPISuccessGetValue('Contribution', ['return' => 'id']), 'total_amount' => 20, @@ -774,12 +751,11 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase { * Get the parameters for recording a contribution. * * @param string $participantStatus - * @param string $contributionStatus * * @return array */ - protected function getRecordContributionParams(string $participantStatus, $contributionStatus): array { - $submitParams = [ + protected function getRecordContributionParams(string $participantStatus): array { + return [ 'hidden_feeblock' => '1', 'hidden_eventFullMsg' => '', 'priceSetId' => $this->getPriceSetID('event'), @@ -790,25 +766,20 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase { 'receive_date' => '2020-01-31 00:51:00', 'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'), 'trxn_id' => '', - 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $contributionStatus), + 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'), 'total_amount' => '20', 'send_receipt' => '1', - 'from_email_address' => $this->getFromEmailAddress(), + 'from_email_address' => '"FIXME" ', 'receipt_text' => 'Contact the Development Department if you need to make any changes to your registration.', 'hidden_custom' => '1', 'hidden_custom_group_count' => ['' => 1], 'custom_4_-1' => '', - 'contact_id' => $this->getContactID(), - 'event_id' => $this->getEventID(), - 'campaign_id' => '', 'register_date' => '2020-01-31 00:50:00', 'role_id' => [0 => CRM_Core_PseudoConstant::getKey('CRM_Event_BAO_Participant', 'role_id', 'Attendee')], 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Event_BAO_Participant', 'status_id', $participantStatus), 'source' => 'I wrote this', 'note' => 'I wrote a note', - 'MAX_FILE_SIZE' => '33554432', ]; - return $submitParams; } /** -- 2.25.1