From 94d3032152bbde2382a1f76bf9259cf1224b9bbc Mon Sep 17 00:00:00 2001 From: Jitendra Purohit Date: Sun, 24 Sep 2023 12:35:07 +0530 Subject: [PATCH] Add setting to disable primary participant for Contact 1 --- src/AdminForm.php | 6 ++ src/AdminHelp.php | 9 ++ src/WebformCivicrmPostProcess.php | 6 +- tests/src/FunctionalJavascript/EventTest.php | 96 +++++++++++++++++++- 4 files changed, 113 insertions(+), 4 deletions(-) diff --git a/src/AdminForm.php b/src/AdminForm.php index c4e182c80..1d9ec8929 100644 --- a/src/AdminForm.php +++ b/src/AdminForm.php @@ -938,9 +938,15 @@ private function buildParticipantTab() { '#title' => t('Allow events to be autoloaded from URL'), '#default_value' => (bool) wf_crm_aval($this->data, 'reg_options:allow_url_load'), ]; + $this->form['participant']['reg_options']['disable_primary_participant'] = [ + '#type' => 'checkbox', + '#title' => t('Disable Contact 1 to be stored as Primary Participant'), + '#default_value' => (bool) wf_crm_aval($this->data, 'reg_options:disable_primary_participant'), + ]; $this->help($this->form['participant']['reg_options']['block_form'], 'reg_options_block_form'); $this->help($this->form['participant']['reg_options']['disable_unregister'], 'reg_options_disable_unregister'); $this->help($this->form['participant']['reg_options']['allow_url_load'], 'reg_options_allow_url_load'); + $this->help($this->form['participant']['reg_options']['disable_primary_participant'], 'reg_options_disable_primary_participant'); $this->addAjaxItem('participant', 'participant_reg_type', 'participants'); $this->addAjaxItem('participant', 'event_type', 'participants'); $this->addAjaxItem('participant', 'show_past_events', 'participants'); diff --git a/src/AdminHelp.php b/src/AdminHelp.php index 428581971..b63630c9b 100644 --- a/src/AdminHelp.php +++ b/src/AdminHelp.php @@ -269,6 +269,15 @@ protected function reg_options_allow_url_load() { '/{node.nid}?c1event1={event1.event_id},{event2.event_id}&c2event1={event3.event_id}

'; } + /** + * Help text for disable primary setting. + */ + protected function reg_options_disable_primary_participant() { + return '

' . + t('If enabled, Contact 1 will not be stored as primary participant for multiple registrations.') . + '

'; + } + protected function reg_options_show_past_events() { return '

' . t('To also display events that have ended, choose an option for how far in the past to search.') . diff --git a/src/WebformCivicrmPostProcess.php b/src/WebformCivicrmPostProcess.php index 47b96440b..d71593933 100644 --- a/src/WebformCivicrmPostProcess.php +++ b/src/WebformCivicrmPostProcess.php @@ -1190,7 +1190,9 @@ private function processParticipants($c, $cid) { unset($params['status_id']); } // Set the currency of the result to the currency type that was submitted. - $params['fee_currency'] = $this->data['contribution'][$n]['currency']; + if (isset($this->data['contribution'][$n]['currency'])) { + $params['fee_currency'] = $this->data['contribution'][$n]['currency']; + } $result = $this->utils->wf_civicrm_api('participant', 'create', $params); $this->ent['participant'][$n]['id'] = $result['id']; @@ -1205,7 +1207,7 @@ private function processParticipants($c, $cid) { } } // When registering contact 1, store id to apply to other contacts - if ($c == 1) { + if ($c == 1 && empty($this->data['reg_options']['disable_primary_participant'])) { $registered_by_id[$e][$i] = $result['id']; } } diff --git a/tests/src/FunctionalJavascript/EventTest.php b/tests/src/FunctionalJavascript/EventTest.php index 7ff9de9a2..1264e564a 100644 --- a/tests/src/FunctionalJavascript/EventTest.php +++ b/tests/src/FunctionalJavascript/EventTest.php @@ -140,6 +140,99 @@ function testParticipantContactReference() { $this->assertEquals($contactRef['id'], $participant["{$customKey}_id"]); } + /** + * Submit the form with values. + */ + function submitWebform() { + $this->drupalGet($this->webform->toUrl('canonical')); + $this->assertPageNoErrorMessages(); + $edit = [ + 'civicrm_1_contact_1_contact_first_name' => 'Frederick', + 'civicrm_1_contact_1_contact_last_name' => 'Pabst', + 'civicrm_2_contact_1_contact_first_name' => 'Mark', + 'civicrm_2_contact_1_contact_last_name' => 'Anthony' + ]; + $this->postSubmission($this->webform, $edit); + } + + /** + * Verify submission results. + * + * @param boolean $primary + * false if primary participant setting is disabled on the webform. + */ + function verifyResults($primary = true) { + // Ensure both contacts are added to the event. + $api_result = $this->utils->wf_civicrm_api('participant', 'get', [ + 'sequential' => 1, + ]); + $this->assertEquals(0, $api_result['is_error']); + $this->assertEquals(2, $api_result['count']); + + $values = $api_result['values']; + $this->assertEquals($this->_event['id'], $values[0]['event_id']); + $this->assertEquals($this->_event['id'], $values[1]['event_id']); + if ($primary) { + $this->assertEquals($values[0]['id'], $values[1]['participant_registered_by_id']); + } + else { + $this->assertEmpty($values[0]['participant_registered_by_id']); + $this->assertEmpty($values[1]['participant_registered_by_id']); + } + // Delete participants. + $this->utils->wf_civicrm_api('participant', 'delete', [ + 'id' => $values[0]['id'], + ]); + $this->utils->wf_civicrm_api('participant', 'delete', [ + 'id' => $values[1]['id'], + ]); + } + + /** + * Verify the submission of multiple participants. + */ + function testMultipleParticipants() { + $this->drupalLogin($this->adminUser); + $this->drupalGet(Url::fromRoute('entity.webform.civicrm', [ + 'webform' => $this->webform->id(), + ])); + $this->enableCivicrmOnWebform(); + + $this->getSession()->getPage()->selectFieldOption('number_of_contacts', 2); + $this->assertSession()->assertWaitOnAjaxRequest(); + $this->htmlOutput(); + + $this->getSession()->getPage()->clickLink('Event Registration'); + + // Configure Event tab. + $this->getSession()->getPage()->selectFieldOption('participant_reg_type', 'all'); + $this->assertSession()->assertWaitOnAjaxRequest(); + $this->htmlOutput(); + $this->getSession()->getPage()->selectFieldOption('participant_1_number_of_participant', 1); + $this->assertSession()->assertWaitOnAjaxRequest(); + $this->htmlOutput(); + $this->getSession()->getPage()->selectFieldOption('civicrm_1_participant_1_participant_event_id[]', 'Test Event'); + + $this->saveCiviCRMSettings(); + + $this->submitWebform(); + + // Ensure both contacts are added to the event. + $this->verifyResults(); + + // Disable primary participant on the webform. + $this->drupalGet(Url::fromRoute('entity.webform.civicrm', [ + 'webform' => $this->webform->id(), + ])); + $this->getSession()->getPage()->clickLink('Event Registration'); + $this->getSession()->getPage()->checkField('Disable Contact 1 to be stored as Primary Participant'); + $this->saveCiviCRMSettings(); + + // Resubmit the form and verify the results. + $this->submitWebform(); + $this->verifyResults(false); + } + /** * Event Participant submission. */ @@ -207,8 +300,7 @@ function testSubmitEventParticipant() { $this->assertSession()->pageTextContains('New submission added to CiviCRM Webform Test.'); //Assert if recur is attached to the created membership. - $utils = \Drupal::service('webform_civicrm.utils'); - $api_result = $utils->wf_civicrm_api('participant', 'get', [ + $api_result = $this->utils->wf_civicrm_api('participant', 'get', [ 'sequential' => 1, ]); $this->assertEquals(0, $api_result['is_error']);