Move scenario set up to trait
authorEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 6 Sep 2023 20:40:31 +0000 (08:40 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 15 Sep 2023 04:05:11 +0000 (16:05 +1200)
tests/phpunit/CRM/Event/Form/Registration/ConfirmTest.php
tests/phpunit/CRMTraits/Event/ScenarioTrait.php [new file with mode: 0644]

index b529b561af2f97ae3e8371cb09e143a6be0b661b..fa3bd7e005db00b3941945580cf2f55ad82d71ef 100644 (file)
@@ -10,6 +10,7 @@ use Civi\Test\FormTrait;
  */
 class CRM_Event_Form_Registration_ConfirmTest extends CiviUnitTestCase {
 
+  use CRMTraits_Event_ScenarioTrait;
   use CRMTraits_Financial_PriceSetTrait;
   use CRMTraits_Profile_ProfileTrait;
   use FormTrait;
@@ -293,35 +294,7 @@ class CRM_Event_Form_Registration_ConfirmTest extends CiviUnitTestCase {
   public function testTaxMultipleParticipant(): void {
     $this->swapMessageTemplateForTestTemplate('event_online_receipt', 'text');
     $this->createLoggedInUser();
-    $this->eventCreatePaid();
-    $this->addTaxAccountToFinancialType(CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Event Fee'));
-    $form = $this->getTestForm('CRM_Event_Form_Registration_Register', [
-      'first_name' => 'Participant1',
-      'last_name' => 'LastName',
-      'email-Primary' => 'participant1@example.com',
-      'additional_participants' => 2,
-      'payment_processor_id' => 0,
-      'priceSetId' => $this->getPriceSetID('PaidEvent'),
-      'price_' . $this->ids['PriceField']['PaidEvent'] => $this->ids['PriceFieldValue']['PaidEvent_standard'],
-      'defaultRole' => 1,
-      'participant_role_id' => '1',
-      'button' => '_qf_Register_upload',
-    ], ['id' => $this->getEventID()])
-      ->addSubsequentForm('CRM_Event_Form_Registration_AdditionalParticipant', [
-        'first_name' => 'Participant2',
-        'last_name' => 'LastName',
-        'email-Primary' => 'participant2@example.com',
-        'priceSetId' => $this->getPriceSetID('PaidEvent'),
-        'price_' . $this->ids['PriceField']['PaidEvent'] => $this->ids['PriceFieldValue']['PaidEvent_standard'],
-      ])->addSubsequentForm('CRM_Event_Form_Registration_AdditionalParticipant', [
-        'first_name' => 'Participant3',
-        'last_name' => 'LastName',
-        'email-Primary' => 'participant3@example.com',
-        'priceSetId' => $this->getPriceSetID('PaidEvent'),
-        'price_' . $this->ids['PriceField']['PaidEvent'] => $this->ids['PriceFieldValue']['PaidEvent_standard'],
-      ])
-      ->addSubsequentForm('CRM_Event_Form_Registration_Confirm')
-      ->processForm();
+    $this->createScenarioMultipleParticipantPendingWithTax();
 
     $participants = $this->callAPISuccess('Participant', 'get', [])['values'];
     $this->assertCount(3, $participants);
diff --git a/tests/phpunit/CRMTraits/Event/ScenarioTrait.php b/tests/phpunit/CRMTraits/Event/ScenarioTrait.php
new file mode 100644 (file)
index 0000000..1922e35
--- /dev/null
@@ -0,0 +1,80 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+use Civi\Api4\Participant;
+use Civi\Test\EventTestTrait;
+
+/**
+ * Trait to do set up for event scenarios.
+ *
+ * This allows multiple test classes to act on various scenarios. e.g a
+ * user might complete a pending registration using the back office form, an api
+ * call or the horrible front end form overload method.
+ *
+ * It might make sense to move this to Civi\Test for extensions to use
+ * but it might need to 'settle' a bit first as there is a balance to be found
+ * between accepting parameters & winding up with crazy complex functions.
+ */
+trait CRMTraits_Event_ScenarioTrait {
+  use EventTestTrait;
+
+  /**
+   * Create a participant registration with 2 registered_by participants.
+   *
+   * This follows the front end form multiple participant flow with tax enabled.
+   *
+   * @throws \Civi\API\Exception\UnauthorizedException
+   * @throws \CRM_Core_Exception
+   */
+  protected function createScenarioMultipleParticipantPendingWithTax(): void {
+    $this->eventCreatePaid();
+    $this->addTaxAccountToFinancialType(CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Event Fee'));
+    $this->getTestForm('CRM_Event_Form_Registration_Register', [
+      'first_name' => 'Participant1',
+      'last_name' => 'LastName',
+      'email-Primary' => 'participant1@example.com',
+      'additional_participants' => 2,
+      'payment_processor_id' => 0,
+      'priceSetId' => $this->getPriceSetID('PaidEvent'),
+      'price_' . $this->ids['PriceField']['PaidEvent'] => $this->ids['PriceFieldValue']['PaidEvent_standard'],
+      'defaultRole' => 1,
+      'participant_role_id' => '1',
+      'button' => '_qf_Register_upload',
+    ], ['id' => $this->getEventID()])
+      ->addSubsequentForm('CRM_Event_Form_Registration_AdditionalParticipant', [
+        'first_name' => 'Participant2',
+        'last_name' => 'LastName',
+        'email-Primary' => 'participant2@example.com',
+        'priceSetId' => $this->getPriceSetID('PaidEvent'),
+        'price_' . $this->ids['PriceField']['PaidEvent'] => $this->ids['PriceFieldValue']['PaidEvent_standard'],
+      ])
+      ->addSubsequentForm('CRM_Event_Form_Registration_AdditionalParticipant', [
+        'first_name' => 'Participant3',
+        'last_name' => 'LastName',
+        'email-Primary' => 'participant3@example.com',
+        'priceSetId' => $this->getPriceSetID('PaidEvent'),
+        'price_' . $this->ids['PriceField']['PaidEvent'] => $this->ids['PriceFieldValue']['PaidEvent_standard'],
+      ])
+      ->addSubsequentForm('CRM_Event_Form_Registration_Confirm')
+      ->processForm();
+    $participants = Participant::get(FALSE)
+      ->addWhere('event_id', '=', $this->getEventID('PaidEvent'))
+      ->addOrderBy('registered_by_id')
+      ->execute();
+    foreach ($participants as $index => $participant) {
+      $identifier = $participant['registered_by_id'] ? 'participant_' . $index : 'primary';
+      $this->setTestEntity('Participant', $participant, $identifier);
+      $this->setTestEntityID('Contact', $participant['contact_id'], $identifier);
+    }
+
+  }
+
+}