From cf451308030c75ce46ba93414d8ed38d9bb85d9a Mon Sep 17 00:00:00 2001
From: Seamus Lee <seamuslee001@gmail.com>
Date: Fri, 1 Sep 2023 08:53:14 +1000
Subject: [PATCH] dev/core#4537 Ensure that Event Registration email works when
 CiviContribute component is disabled and add unit test

Shift to using action object provider and rename function to remove Component
---
 .../WorkflowMessage/ParticipantTrait.php      | 62 ++++++++++---------
 Civi/Test/EventTestTrait.php                  |  2 +-
 Civi/Token/TokenCompatSubscriber.php          |  7 ++-
 .../Event/Form/Registration/ConfirmTest.php   | 33 ++++++++++
 4 files changed, 74 insertions(+), 30 deletions(-)

diff --git a/CRM/Event/WorkflowMessage/ParticipantTrait.php b/CRM/Event/WorkflowMessage/ParticipantTrait.php
index 56853b647319..a739ba168086 100644
--- a/CRM/Event/WorkflowMessage/ParticipantTrait.php
+++ b/CRM/Event/WorkflowMessage/ParticipantTrait.php
@@ -67,6 +67,10 @@ trait CRM_Event_WorkflowMessage_ParticipantTrait {
    */
   protected $participantContacts;
 
+  private function isCiviContributeEnabled(): bool {
+    return array_key_exists('Contribution', \Civi::service('action_object_provider')->getEntities());
+  }
+
   /**
    * @param array $participantContacts
    *
@@ -93,7 +97,7 @@ public function setEventID(int $eventID): self {
    */
   public function setParticipantID(int $participantID) {
     $this->participantID = $participantID;
-    if (!$this->getContributionID()) {
+    if (!$this->getContributionID() && $this->isCiviContributeEnabled()) {
       $lineItem = LineItem::get(FALSE)
         ->addWhere('entity_table', '=', 'civicrm_participant')
         ->addWhere('entity_id', '=', $participantID)
@@ -186,34 +190,36 @@ public function getParticipants(): array {
       }
       // Initiate with the current participant to ensure they are first.
       $participants = [$this->participantID => ['id' => $this->participantID, 'tax_rate_breakdown' => []]];
-      foreach ($this->getLineItems() as $lineItem) {
-        if ($lineItem['entity_table'] === 'civicrm_participant') {
-          $participantID = $lineItem['entity_id'];
-        }
-        else {
-          // It is not clear if this could ever be true - testing the CiviCRM event
-          // form shows all line items assigned to participants but we should
-          // assign to primary if this can occur.
-          $participantID = $this->getPrimaryParticipantID();
-        }
-        $participants[$participantID]['line_items'][] = $lineItem;
-        if (!isset($participants[$participantID]['totals'])) {
-          $participants[$participantID]['totals'] = ['total_amount_exclusive' => 0, 'tax_amount' => 0, 'total_amount_inclusive' => 0];
-        }
-        $participants[$participantID]['totals']['total_amount_exclusive'] += $lineItem['line_total'];
-        $participants[$participantID]['totals']['tax_amount'] += $lineItem['tax_amount'];
-        $participants[$participantID]['totals']['total_amount_inclusive'] += ($lineItem['line_total'] + $lineItem['tax_amount']);
-        if (!isset($participants[$participantID]['tax_rate_breakdown'])) {
-          $participants[$participantID]['tax_rate_breakdown'] = [];
-        }
-        if (!isset($participants[$participantID]['tax_rate_breakdown'][$lineItem['tax_rate']])) {
-          $participants[$participantID]['tax_rate_breakdown'][$lineItem['tax_rate']] = [
-            'amount' => 0,
-            'rate' => $lineItem['tax_rate'],
-            'percentage' => sprintf('%.2f', $lineItem['tax_rate']),
-          ];
+      if ($this->isCiviContributeEnabled()) {
+        foreach ($this->getLineItems() as $lineItem) {
+          if ($lineItem['entity_table'] === 'civicrm_participant') {
+            $participantID = $lineItem['entity_id'];
+          }
+          else {
+            // It is not clear if this could ever be true - testing the CiviCRM event
+            // form shows all line items assigned to participants but we should
+            // assign to primary if this can occur.
+            $participantID = $this->getPrimaryParticipantID();
+          }
+          $participants[$participantID]['line_items'][] = $lineItem;
+          if (!isset($participants[$participantID]['totals'])) {
+            $participants[$participantID]['totals'] = ['total_amount_exclusive' => 0, 'tax_amount' => 0, 'total_amount_inclusive' => 0];
+          }
+          $participants[$participantID]['totals']['total_amount_exclusive'] += $lineItem['line_total'];
+          $participants[$participantID]['totals']['tax_amount'] += $lineItem['tax_amount'];
+          $participants[$participantID]['totals']['total_amount_inclusive'] += ($lineItem['line_total'] + $lineItem['tax_amount']);
+          if (!isset($participants[$participantID]['tax_rate_breakdown'])) {
+            $participants[$participantID]['tax_rate_breakdown'] = [];
+          }
+          if (!isset($participants[$participantID]['tax_rate_breakdown'][$lineItem['tax_rate']])) {
+            $participants[$participantID]['tax_rate_breakdown'][$lineItem['tax_rate']] = [
+              'amount' => 0,
+              'rate' => $lineItem['tax_rate'],
+              'percentage' => sprintf('%.2f', $lineItem['tax_rate']),
+            ];
+          }
+          $participants[$participantID]['tax_rate_breakdown'][$lineItem['tax_rate']]['amount'] += $lineItem['tax_amount'];
         }
-        $participants[$participantID]['tax_rate_breakdown'][$lineItem['tax_rate']]['amount'] += $lineItem['tax_amount'];
       }
 
       $count = 1;
diff --git a/Civi/Test/EventTestTrait.php b/Civi/Test/EventTestTrait.php
index 93b711faff92..73682c12a603 100644
--- a/Civi/Test/EventTestTrait.php
+++ b/Civi/Test/EventTestTrait.php
@@ -78,7 +78,7 @@ protected function eventCreatePaid(array $eventParameters = [], array $priceSetP
   }
 
   /**
-   * Create a paid event.
+   * Create an unpaid event.
    *
    * @param array $eventParameters
    *   Values to
diff --git a/Civi/Token/TokenCompatSubscriber.php b/Civi/Token/TokenCompatSubscriber.php
index 7aad811eac0f..2dd0f9e1ef01 100644
--- a/Civi/Token/TokenCompatSubscriber.php
+++ b/Civi/Token/TokenCompatSubscriber.php
@@ -56,7 +56,12 @@ public function setupSmartyAliases(TokenValueEvent $e) {
    */
   public function onRender(TokenRenderEvent $e): void {
     $useSmarty = !empty($e->context['smarty']);
-    $e->string = $e->getTokenProcessor()->visitTokens($e->string, function() {
+    $e->string = $e->getTokenProcessor()->visitTokens($e->string, function($token = NULL, $entity = NULL, $field = NULL, $filterParams = NULL) {
+      if ($filterParams && $filterParams[0] === 'boolean') {
+        // This token was missed during primary rendering, and it's supposed to be coerced to boolean.
+        // Treat an unknown token as false-y.
+        return 0;
+      }
       // For historical consistency, we filter out unrecognized tokens.
       return '';
     });
diff --git a/tests/phpunit/CRM/Event/Form/Registration/ConfirmTest.php b/tests/phpunit/CRM/Event/Form/Registration/ConfirmTest.php
index cd26c5508633..af05080acbc1 100644
--- a/tests/phpunit/CRM/Event/Form/Registration/ConfirmTest.php
+++ b/tests/phpunit/CRM/Event/Form/Registration/ConfirmTest.php
@@ -721,4 +721,37 @@ protected function submitPaidEvent(array $submitValues = []): void {
     ], $submitValues));
   }
 
+  public function testRegistrationWithoutCiviContributeEnabled(): void {
+    $mut = new CiviMailUtils($this, TRUE);
+    $event = $this->eventCreateUnpaid([
+      'has_waitlist' => 1,
+      'max_participants' => 1,
+      'start_date' => 20351021,
+      'end_date' => 20351023,
+      'registration_end_date' => 20351015,
+    ]);
+    CRM_Core_BAO_ConfigSetting::disableComponent('CiviContribute');
+    $this->submitForm(
+      $event['id'], [
+        [
+          'first_name' => 'Bruce No Contribute',
+          'last_name' => 'Wayne',
+          'email-Primary' => 'bruce@gotham.com',
+          'is_primary' => 1,
+          'is_pay_later' => 0,
+          'campaign_id' => NULL,
+          'defaultRole' => 1,
+          'participant_role_id' => '1',
+          'button' => '_qf_Register_upload',
+        ],
+      ]
+    );
+    $mut->checkMailLog([
+      'Dear Bruce No Contribute,  Thank you for your registration.  This is a confirmation that your registration has been received and your status has been updated to Registered.',
+    ]);
+    $mut->stop();
+    $mut->clearMessages();
+    CRM_Core_BAO_ConfigSetting::enableComponent('CiviContribute');
+  }
+
 }