fix import of participant role ID
authorJon Goldberg <jon@megaphonetech.com>
Tue, 28 Mar 2023 18:01:13 +0000 (14:01 -0400)
committerJon Goldberg <jon@megaphonetech.com>
Tue, 4 Apr 2023 22:32:47 +0000 (18:32 -0400)
CRM/Event/BAO/Participant.php
CRM/Event/Import/Parser/Participant.php
tests/phpunit/CRM/Event/Import/Parser/ParticipantTest.php

index 7d3758ea32647b5f4e04c48a6b44b57782f6e1eb..9d63b3b5cd88e04a64981c29bd5fbdff7ad1301f 100644 (file)
@@ -1893,6 +1893,17 @@ WHERE    civicrm_participant.contact_id = {$contactID} AND
    * @throws CRM_Core_Exception
    */
   public static function self_hook_civicrm_pre(\Civi\Core\Event\PreEvent $event) {
+    // Set the default role ID on create.
+    if ($event->entity === 'Participant' && $event->action === 'create' && empty($event->params['role_id'])) {
+      if (!empty($event->params['event_id'])) {
+        $event->params['role_id'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $event->params['event_id'], 'default_role_id');
+      }
+      else {
+        $params['role_id'] = CRM_Core_DAO::singleValueQuery('SELECT default_role_id FROM civicrm_event WHERE id = %1', [
+          1 => [$event->params['event_id'], 'Integer'],
+        ]);
+      }
+    }
     if ($event->entity === 'Participant' && $event->action === 'create' && empty($event->params['created_id'])) {
       // Set the "created_id" field if not already set.
       // The created_id should always be the person that actually did the registration.
index 3c50b9ced0669be91eff5e9d01d74eda81a5d210..ac4fee8715a442b945a4ce5d3b2d906406f55915 100644 (file)
@@ -108,18 +108,6 @@ class CRM_Event_Import_Parser_Participant extends CRM_Import_Parser {
       // don't add to recent items, CRM-4399
       $formatted['skipRecentView'] = TRUE;
 
-      if (!(!empty($params['participant_role_id']) || !empty($params['participant_role']))) {
-        if (!empty($params['event_id'])) {
-          $params['participant_role_id'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'default_role_id');
-        }
-        else {
-          $eventTitle = $params['event_title'];
-          $params['participant_role_id'] = CRM_Core_DAO::singleValueQuery('SELECT default_role_id FROM civicrm_event WHERE title = %1', [
-            1 => [$eventTitle, 'String'],
-          ]);
-        }
-      }
-
       $formatValues = [];
       foreach ($params as $key => $field) {
         if ($field == NULL || $field === '') {
index c2dced64e3b3ff1abacbdb4fd6334c536af83828..0eff6061af9ea7e5ba05cad5b699a1b130c2189c 100644 (file)
@@ -138,6 +138,42 @@ class CRM_Event_Import_Parser_ParticipantTest extends CiviUnitTestCase {
     $this->assertEquals('ERROR', $row['_status']);
   }
 
+  /**
+   * Test that imports work generally.
+   */
+  public function testImportParticipant() :void {
+    $this->eventCreate(['title' => 'Rain-forest Cup Youth Soccer Tournament']);
+    $contactID = $this->individualCreate(['external_identifier' => 'ref-77']);
+    $this->importCSV('participant_with_ext_id.csv', [
+      ['name' => 'event_id'],
+      ['name' => 'do_not_import'],
+      ['name' => 'external_identifier'],
+      ['name' => 'fee_amount'],
+      ['name' => 'fee_currency'],
+      ['name' => 'fee_level'],
+      ['name' => 'is_pay_later'],
+      ['name' => 'role_id'],
+      ['name' => 'source'],
+      ['name' => 'status_id'],
+      ['name' => 'register_date'],
+      ['name' => 'do_not_import'],
+    ]);
+    $dataSource = new CRM_Import_DataSource_CSV($this->userJobID);
+    $row = $dataSource->getRow();
+    $result = $this->callAPISuccess('Participant', 'get', [
+      'contact_id' => $contactID,
+      'sequential' => TRUE,
+    ])['values'][0];
+
+    $this->assertEquals($row['event_title'], $result['event_title']);
+    $this->assertEquals($row['fee_amount'], $result['participant_fee_amount']);
+    $this->assertEquals($row['participant_source'], $result['participant_source']);
+    $this->assertEquals($row['participant_status'], $result['participant_status']);
+    $this->assertEquals('2022-12-07 00:00:00', $result['participant_register_date']);
+    $this->assertEquals(['Attendee', 'Volunteer'], $result['participant_role']);
+    $this->assertEquals(0, $result['participant_is_pay_later']);
+  }
+
   /**
    * @param array $submittedValues
    *