Further fix forms to full form flow
authorEileen McNaughton <emcnaughton@wikimedia.org>
Sat, 2 Sep 2023 00:09:57 +0000 (12:09 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Sat, 2 Sep 2023 02:53:08 +0000 (14:53 +1200)
tests/phpunit/CRM/Event/Form/ParticipantTest.php

index ccc9da5661a68490a2e5ac79c83135ee8c2f95e0..61b91adc168c8f038e34e5bfc3343c301db82dfc 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 
 use Civi\Api4\Address;
+use Civi\Api4\LineItem;
 use Civi\Api4\LocBlock;
 use Civi\Api4\Participant;
 use Civi\Api4\Phone;
@@ -43,77 +44,72 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase {
    * @throws \Exception
    */
   public function testSubmitUnpaidPriceChangeWhileStillPending(): void {
-    $form = $this->getForm(['is_monetary' => 1, 'financial_type_id' => 1], [], TRUE);
-
-    $form->_lineItem = [
-      0 => [
-        13 => [
-          'price_field_id' => $this->getPriceFieldID(),
-          'price_field_value_id' => $this->ids['PriceFieldValue']['PaidEvent_standard'],
-          'label' => 'Tiny-tots (ages 5-8)',
-          'field_title' => 'Tournament Fees',
-          'description' => NULL,
-          'qty' => 1,
-          'unit_price' => '800.000000000',
-          'line_total' => 800.0,
-          'participant_count' => 0,
-          'max_value' => NULL,
-          'membership_type_id' => NULL,
-          'membership_num_terms' => NULL,
-          'auto_renew' => NULL,
-          'html_type' => 'Radio',
-          'financial_type_id' => '4',
-          'tax_amount' => NULL,
-          'non_deductible_amount' => '0.00',
-        ],
-      ],
-    ];
-    $form->setAction(CRM_Core_Action::ADD);
-    $form->_priceSetId = $this->getPriceSetID('PaidEvent');
-    $form->submit([
+    $this->eventCreatePaid();
+    $_REQUEST['cid'] = $this->individualCreate();
+    $form = $this->getFormObject('CRM_Event_Form_Participant', [
+      'is_monetary' => 1,
       'register_date' => date('Ymd'),
-      'status_id' => 5,
-      'role_id' => 1,
-      'event_id' => $form->getEventID(),
-      'priceSetId' => $this->getPriceSetID('PaidEvent'),
-      $this->getPriceFieldKey() => $this->ids['PriceFieldValue']['PaidEvent_student'],
       'is_pay_later' => 1,
-      'amount_level' => 'Too much',
-      'fee_amount' => 55,
-      'total_amount' => 55,
       'payment_processor_id' => 0,
       'record_contribution' => TRUE,
       'financial_type_id' => 1,
-      'contribution_status_id' => 2,
-      'payment_instrument_id' => 1,
-      'receive_date' => date('Y-m-d'),
+      'priceSetId' => $this->getPriceSetID('PaidEvent'),
+      $this->getPriceFieldKey() => $this->ids['PriceFieldValue']['PaidEvent_student'],
+      'check_number' => '879',
+      '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', 'Pending'),
+      'total_amount' => '20',
+      'send_receipt' => '1',
+      'from_email_address' => '"FIXME" <info@EXAMPLE.ORG>',
+      'receipt_text' => 'Contact the Development Department if you need to make any changes to your registration.',
+      'hidden_custom' => '1',
+      'hidden_custom_group_count' => ['' => 1],
+      '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', 'Pending from pay later'),
+      'source' => 'I wrote this',
+      'note' => 'I wrote a note',
+      'event_id' => $this->getEventID(),
+
     ]);
+    $form->preProcess();
+    $form->buildForm();
+    $form->postProcess();
     $participant = $this->callAPISuccessGetSingle('Participant', []);
     $contribution = $this->callAPISuccessGetSingle('Contribution', []);
     $this->assertEquals(2, $contribution['contribution_status_id']);
     $this->callAPISuccessGetSingle('FinancialItem', []);
 
-    $priceSetParams[$this->getPriceFieldKey()] = $this->ids['PriceFieldValue']['PaidEvent_family_package'];
-    $lineItem = CRM_Price_BAO_LineItem::getLineItems($participant['id'], 'participant');
-    $this->assertEquals(55, $lineItem[1]['subTotal']);
+    $lineItem = LineItem::get()
+      ->addWhere('entity_id', '=', $participant['id'])
+      ->addWhere('entity_table', '=', 'civicrm_participant')
+      ->addOrderBy('id')
+      ->execute()->first();
+    $this->assertEquals(100, $lineItem['line_total']);
     $financialItems = $this->callAPISuccess('FinancialItem', 'get', []);
     $sum = 0;
     foreach ($financialItems['values'] as $financialItem) {
       $sum += $financialItem['amount'];
     }
-    $this->assertEquals(55, $sum);
+    $this->assertEquals(100, $sum);
 
     $priceSetID = $this->ids['PriceSet']['PaidEvent'];
     $eventFeeBlock = CRM_Price_BAO_PriceSet::getSetDetail($priceSetID)[$priceSetID]['fields'];
+    $priceSetParams[$this->getPriceFieldKey()] = $this->ids['PriceFieldValue']['PaidEvent_family_package'];
     CRM_Price_BAO_LineItem::changeFeeSelections($priceSetParams, $participant['id'], 'participant', $contribution['id'], $eventFeeBlock);
     // 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']);
+    $lineItems = LineItem::get()
+      ->addWhere('entity_id', '=', $participant['id'])
+      ->addWhere('entity_table', '=', 'civicrm_participant')
+      ->addOrderBy('id')
+      ->execute();
     // Participants is updated to 0 but line remains.
-    $this->assertEquals(0, $lineItem[1]['subTotal']);
-    $this->assertEquals(1550.55, $lineItem[2]['subTotal']);
+    $this->assertEquals(0, $lineItems[0]['line_total']);
+    $this->assertEquals(1550.55, $lineItems[1]['line_total']);
     $financialItems = $this->callAPISuccess('FinancialItem', 'get', []);
 
     $sum = 0;
@@ -138,7 +134,7 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase {
     // 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('PaidEvent'), TRUE, FALSE)[$this->getPriceSetID('PaidEvent')]['fields'];
+    $priceSetBlock = CRM_Price_BAO_PriceSet::getSetDetail($this->getPriceSetID('PaidEvent'))[$this->getPriceSetID('PaidEvent')]['fields'];
 
     $priceSetParams = [
       'priceSetId' => $this->getPriceSetID('PaidEvent'),
@@ -188,8 +184,7 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase {
     $this->setCurrencySeparators($thousandSeparator);
     $_REQUEST['mode'] = 'live';
     $paymentProcessorID = $this->processorCreate(['is_test' => 0]);
-    $form = $this->getForm(['is_monetary' => 1, 'financial_type_id' => 1], $this->getSubmitParamsForCreditCardPayment($paymentProcessorID), TRUE);
-    $form->postProcess();
+    $this->submitForm(['is_monetary' => 1, 'financial_type_id' => 1], $this->getSubmitParamsForCreditCardPayment($paymentProcessorID), TRUE);
     $participant = $this->callAPISuccessGetSingle('Participant', []);
     $this->assertEquals('2018-09-04 00:00:00', $participant['participant_register_date']);
     $this->assertEquals('Offline Registration for Event: Annual CiviCRM meet by: ', $participant['participant_source']);
@@ -229,11 +224,9 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase {
     $this->setCurrencySeparators($thousandSeparator);
     $paymentProcessorID = $this->processorCreate(['is_test' => 0]);
     $_REQUEST['mode'] = 'live';
-    $form = $this->getForm(['is_monetary' => 1, 'financial_type_id' => 1], $this->getSubmitParamsForCreditCardPayment($paymentProcessorID), TRUE);
-
     Civi\Payment\System::singleton()->getById($paymentProcessorID)->setDoDirectPaymentResult(['payment_status_id' => 'failed']);
     try {
-      $form->postProcess();
+      $this->submitForm(['is_monetary' => 1, 'financial_type_id' => 1], $this->getSubmitParamsForCreditCardPayment($paymentProcessorID), TRUE);
     }
     catch (CRM_Core_Exception_PrematureExitException $e) {
       return;
@@ -252,7 +245,7 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase {
   public function testParticipantOfflineReceipt(string $thousandSeparator): void {
     $this->setCurrencySeparators($thousandSeparator);
     $this->swapMessageTemplateForTestTemplate('event_offline_receipt', 'text');
-    $this->swapMessageTemplateForTestTemplate('event_offline_receipt', 'html');
+    $this->swapMessageTemplateForTestTemplate('event_offline_receipt');
     $mut = new CiviMailUtils($this, TRUE);
     // Create an email associated with the logged in contact
     $loggedInContactID = $this->createLoggedInUser();
@@ -291,8 +284,7 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase {
     $_REQUEST['mode'] = 'Live';
     $submitParams = $this->getSubmitParamsForCreditCardPayment($paymentProcessorID);
     $submitParams['from_email_address'] = $email['id'];
-    $form = $this->getForm(['is_monetary' => 1, 'financial_type_id' => 1, 'pay_later_receipt' => 'pay us'], $submitParams, TRUE);
-    $form->postProcess();
+    $this->submitForm(['is_monetary' => 1, 'financial_type_id' => 1, 'pay_later_receipt' => 'pay us'], $submitParams, TRUE);
     $participantID = Participant::get()->addWhere('event_id', '=', $this->getEventID('PaidEvent'))->execute()->first()['id'];
     //Check if type is correctly populated in mails.
     //Also check the string email is present not numeric from.
@@ -370,6 +362,20 @@ London,',
     return $form;
   }
 
+  /**
+   * Submit the participant form.
+   *
+   * @param array $eventParams
+   * @param array $submittedValues
+   * @param bool $isQuickConfig
+   *
+   * @throws \CRM_Core_Exception
+   */
+  protected function submitForm(array $eventParams = [], array $submittedValues = [], bool $isQuickConfig = FALSE): void {
+    $form = $this->getForm($eventParams, $submittedValues, $isQuickConfig);
+    $form->postProcess();
+  }
+
   /**
    * Create a Price set with two price field of type Text.
    *
@@ -378,12 +384,7 @@ London,',
    * @throws \CRM_Core_Exception
    */
   protected function createParticipantRecordsFromTwoFieldPriceSet(): void {
-    $submittedValues['contact_id'] = $this->ids['Contact']['event'] = $this->individualCreate();
-    $event = $this->eventCreatePaid([
-      'confirm_email_text' => "Just do it\n Now",
-      'is_show_location' => TRUE,
-    ]);
-
+    $this->eventCreatePaid();
     $textFieldsToCreate = [['amount' => 10, 'label' => 'First Text field'], ['amount' => 55, 'label' => 'Second Text field']];
     foreach ($textFieldsToCreate as $fieldToCreate) {
       $fieldParams = [
@@ -404,18 +405,15 @@ London,',
       $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'];
     }
-    $submittedValues = array_merge($this->getRecordContributionParams('Partially paid'), $submittedValues);
-    $submittedValues['event_id'] = $event['id'];
-    $_REQUEST['cid'] = $submittedValues['contact_id'];
+
+    $_REQUEST['cid'] = $this->individualCreate();
     /** @var CRM_Event_Form_Participant $form */
-    $form = $this->getFormObject('CRM_Event_Form_Participant', $submittedValues);
-    $form->preProcess();
-    $form->buildForm();
-    $form->submit([
+    $form = $this->getFormObject('CRM_Event_Form_Participant', [
       'register_date' => date('Ymd'),
+      'contact_id' => $this->ids['Contact']['individual_0'],
       'receive_date' => '2018-09-01',
-      'status_id' => 5,
-      'role_id' => 1,
+      'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Event_BAO_Participant', 'status_id', 'Pending from pay later'),
+      'role_id' => [1],
       'event_id' => $this->getEventID('PaidEvent'),
       'priceSetId' => $this->getPriceSetID('PaidEvent'),
       'price_' . $this->ids['PriceField']['first_text_field'] => [$this->ids['PriceFieldValue']['first_text_field'] => 1],
@@ -428,7 +426,13 @@ London,',
       'financial_type_id' => 1,
       'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
       'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'),
+      'check_number' => '879',
+      'send_receipt' => '1',
+      'from_email_address' => 'admin@example.com',
     ]);
+    $form->preProcess();
+    $form->buildForm();
+    $form->postProcess();
   }
 
   /**
@@ -472,11 +476,10 @@ London,',
   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], [
+    $this->submitForm(['is_monetary' => 1, 'financial_type_id' => 1, 'start_date' => $futureDate], [
       'record_contribution' => TRUE,
       'financial_type_id' => 1,
     ]);
-    $form->postProcess();
     $contribution = $this->callAPISuccessGetSingle('Contribution', []);
     // Api doesn't retrieve it & we don't much want to change that as we want to feature freeze BAO_Query.
     $this->assertEquals($futureDate . ' 00:00:00', CRM_Core_DAO::singleValueQuery("SELECT revenue_recognition_date FROM civicrm_contribution WHERE id = {$contribution['id']}"));
@@ -497,7 +500,7 @@ London,',
    */
   public function testSubmitPartialPayment(bool $isQuickConfig): void {
     $mailUtil = new CiviMailUtils($this, TRUE);
-    $form = $this->getForm(['is_monetary' => 1, 'start_date' => '2023-02-15 15:00', 'end_date' => '2023-02-15 18:00'], [
+    $this->submitForm(['is_monetary' => 1, 'start_date' => '2023-02-15 15:00', 'end_date' => '2023-02-15 18:00'], [
       'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
       'total_amount' => '20',
       'send_receipt' => '1',
@@ -508,8 +511,6 @@ London,',
       'source' => 'I wrote this',
       'note' => 'I wrote a note',
     ], $isQuickConfig);
-    $this->callAPISuccess('PriceSet', 'create', ['is_quick_config' => $isQuickConfig, 'id' => $this->getPriceSetID('PaidEvent')]);
-    $form->postProcess();
     $this->assertPartialPaymentResult($isQuickConfig, $mailUtil);
   }
 
@@ -524,14 +525,12 @@ London,',
    */
   public function testSubmitPendingPartiallyPaidAddPayment(bool $isQuickConfig): void {
     $mailUtil = new CiviMailUtils($this, TRUE);
-    $form = $this->getForm(['is_monetary' => 1, 'start_date' => '2023-02-15 15:00', 'end_date' => '2023-02-15 18:00'], [], $isQuickConfig);
-    $paymentInstrumentID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check');
-    $form->postProcess();
+    $this->submitForm(['is_monetary' => 1, 'start_date' => '2023-02-15 15:00', 'end_date' => '2023-02-15 18:00'], [], $isQuickConfig);
     $this->callAPISuccess('Payment', 'create', [
       'contribution_id' => $this->callAPISuccessGetValue('Contribution', ['return' => 'id']),
       'total_amount'  => 20,
       'check_number' => 879,
-      'payment_instrument_id' => $paymentInstrumentID,
+      'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'),
     ]);
     $this->assertPartialPaymentResult($isQuickConfig, $mailUtil, FALSE);
   }
@@ -549,14 +548,12 @@ London,',
    */
   public function testSubmitPendingAddPayment(bool $isQuickConfig): void {
     $mailUtil = new CiviMailUtils($this, TRUE);
-    $form = $this->getForm(['is_monetary' => 1, 'start_date' => '2023-02-15 15:00', 'end_date' => '2023-02-15 18:00'], [], $isQuickConfig);
-    $paymentInstrumentID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check');
-    $form->postProcess();
+    $this->submitForm(['is_monetary' => 1, 'start_date' => '2023-02-15 15:00', 'end_date' => '2023-02-15 18:00'], [], $isQuickConfig);
     $this->callAPISuccess('Payment', 'create', [
       'contribution_id' => $this->callAPISuccessGetValue('Contribution', ['return' => 'id']),
       'total_amount'  => 20,
       'check_number' => 879,
-      'payment_instrument_id' => $paymentInstrumentID,
+      'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'),
     ]);
     $this->assertPartialPaymentResult($isQuickConfig, $mailUtil, FALSE);
   }
@@ -712,7 +709,6 @@ London,',
       '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' => '',
       '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),