Only show templates upgrade message when templates have changed
authorlarssandergreen <lars@wildsight.ca>
Wed, 26 Jul 2023 21:04:01 +0000 (15:04 -0600)
committerlarssandergreen <lars@wildsight.ca>
Wed, 26 Jul 2023 21:04:35 +0000 (15:04 -0600)
CRM/Upgrade/Incremental/MessageTemplates.php
tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php

index e16d00bff9940a0079d846b3865ff61cd9504b6c..58e605a1e9dd1315b3f275a55614fd1985586719 100644 (file)
@@ -476,6 +476,15 @@ class CRM_Upgrade_Incremental_MessageTemplates {
    */
   public function getUpgradeMessages() {
     $updates = $this->getTemplatesToUpdate();
+
+    // workflow_name is not available pre-5.26, so we won't check if templates are changed or not pre-5.26
+    try {
+      $uneditedTemplates = $this->getUneditedTemplates();
+    }
+    catch (Exception $e) {
+      $uneditedTemplates = [];
+    }
+
     $messages = [];
     $templateLabel = '';
     foreach ($updates as $key => $value) {
@@ -491,7 +500,9 @@ class CRM_Upgrade_Incremental_MessageTemplates {
           $templateLabel = $value['label'];
         }
       }
-      $messages[$templateLabel] = $value['upgrade_descriptor'];
+      if (!(array_key_exists($key, $uneditedTemplates))) {
+        $messages[$templateLabel] = $value['upgrade_descriptor'];
+      }
     }
     return $messages;
   }
@@ -592,8 +603,7 @@ class CRM_Upgrade_Incremental_MessageTemplates {
    * Get all the is_default templates that are the unchanged from their is_reserved counterpart, which
    * at the time this runs was the version shipped with core when it was last changed.
    *
-   * @todo have pulled this out since want to re-use it to get the preUpgrade message right. Currently
-   * it always tells you all the ones in the hardcoded per-version list have been customized.
+   * This cannot be used before 5.26, as workflow_name was only added in 5.26.
    *
    * @return array
    */
@@ -601,7 +611,7 @@ class CRM_Upgrade_Incremental_MessageTemplates {
     $templates = [];
     foreach (['html', 'text', 'subject'] as $type) {
       $dao = CRM_Core_DAO::executeQuery("
-        SELECT default_template.id, default_template.workflow_id FROM civicrm_msg_template reserved
+        SELECT default_template.id, default_template.workflow_id, default_template.workflow_name FROM civicrm_msg_template reserved
         LEFT JOIN civicrm_msg_template default_template
           ON reserved.workflow_id = default_template.workflow_id
         WHERE reserved.is_reserved = 1 AND default_template.is_default = 1 AND reserved.id <> default_template.id
@@ -609,10 +619,11 @@ class CRM_Upgrade_Incremental_MessageTemplates {
       ");
       while ($dao->fetch()) {
         // Note the same id can appear multiple times, e.g. you might change the html but not the subject.
-        $templates[] = [
+        $templates[$dao->workflow_name . '_' . $type] = [
           'id' => $dao->id,
           'type' => $type,
           'workflow_id' => $dao->workflow_id,
+          'workflow_name' => $dao->workflow_name,
         ];
       }
     }
index 2641b5b6d19ec495f9d6d843f1575a8074eafdaa..9aabcdc7afc7f7d2fc99cf16047ee53790a12d03 100644 (file)
@@ -133,12 +133,15 @@ class CRM_Upgrade_Incremental_BaseTest extends CiviUnitTestCase {
    * Test function for messages on upgrade.
    */
   public function testMessageTemplateGetUpgradeMessages() {
+    \Civi\Api4\MessageTemplate::update(FALSE)
+      ->addValue('msg_text', 'Edited text')
+      ->addWhere('workflow_name', '=', 'contribution_online_receipt')
+      ->addWhere('is_default', '=', TRUE)
+      ->execute();
     $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates('5.4.alpha1');
     $messages = $messageTemplateObject->getUpgradeMessages();
     $this->assertEquals([
-      'Memberships - Receipt (on-line)' => 'Use email greeting at top where available',
       'Contributions - Receipt (on-line)' => 'Use email greeting at top where available',
-      'Events - Registration Confirmation and Receipt (on-line)' => 'Use email greeting at top where available',
     ], $messages);
   }