Fix token deprecation to be a check not an upgrade notice
authorEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 27 Aug 2021 01:21:43 +0000 (13:21 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 27 Aug 2021 01:24:56 +0000 (13:24 +1200)
It turns out the upgrade notice is calculated before the upgrade action runs
- this means that currently the upgrade notice is displayed even though
manual intervention will only be required in edge cases (since the
upgrade replaces the most common usage and it would require the
presence of an if or something like that for it to still exist).

CRM/Upgrade/Incremental/MessageTemplates.php
CRM/Upgrade/Incremental/php/FiveFortyOne.php
CRM/Utils/Check/Component/Tokens.php [new file with mode: 0644]
CRM/Utils/Token.php
tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php

index 944a1e69da06c82ca7606400ede00e63a7fd99ab..f2a8b8c6181b5bfedfd81564882e605b9a2c8942 100644 (file)
@@ -306,38 +306,6 @@ class CRM_Upgrade_Incremental_MessageTemplates {
     ");
   }
 
-  /**
-   * Get warnings for users if the replaced string is still present.
-   *
-   * This might be the case when used in an IF and for now we will recommend
-   * manual intervention.
-   *
-   * @param string $workflowName
-   * @param string $old
-   * @param string $new
-   *
-   * @return string
-   */
-  public function getMessageTemplateWarning(string $workflowName, string $old, string $new) {
-    if (CRM_Core_DAO::singleValueQuery("
-      SELECT COUNT(*)
-      FROM civicrm_msg_template
-      WHERE workflow_name = '$workflowName'
-      AND (
-        msg_html LIKE '%$old%'
-        OR msg_subject LIKE '%$old%'
-        OR civicrm_msg_template.msg_text LIKE '%$old%'
-      )
-    ")) {
-      return ts('Please review your %1 message template and remove references to the token %2 as it has been replaced by %3', [
-        1 => $workflowName,
-        2 => '{' . $old . '}',
-        3 => '{' . $new . '}',
-      ]);
-    }
-    return '';
-  }
-
   /**
    * Get the upgrade messages.
    */
index f672c3031ec449cd298b2d349d2efa5df8d67905..027f0be9c13215eac539909caf962fc6504c16b6 100644 (file)
@@ -42,16 +42,10 @@ class CRM_Upgrade_Incremental_php_FiveFortyOne extends CRM_Upgrade_Incremental_B
    * @param string $rev
    *   an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs.
    */
-  public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
-    $templateUpgrader = new CRM_Upgrade_Incremental_MessageTemplates($rev);
-    $upgradeMessage = $templateUpgrader->getMessageTemplateWarning('contribution_invoice_receipt', '$display_name', 'contact.display_name');
-    if (!empty($upgradeMessage)) {
-      $postUpgradeMessage .= '<ul><li>' . htmlspecialchars($upgradeMessage) . '</li></ul>';
+  public function setPostUpgradeMessage(&$postUpgradeMessage, $rev): void {
+    if ($rev === '5.41.alpha1') {
+      $postUpgradeMessage .= '<br /><br />' . ts('A token has been updated in the %1 template. Check the system checks page to see if any action is required.', [1 => 'invoice']);
     }
-    // Example: Generate a post-upgrade message.
-    // if ($rev == '5.12.34') {
-    //   $postUpgradeMessage .= '<br /><br />' . ts("By default, CiviCRM now disables the ability to import directly from SQL. To use this feature, you must explicitly grant permission 'import SQL datasource'.");
-    // }
   }
 
   /*
diff --git a/CRM/Utils/Check/Component/Tokens.php b/CRM/Utils/Check/Component/Tokens.php
new file mode 100644 (file)
index 0000000..07472f3
--- /dev/null
@@ -0,0 +1,63 @@
+<?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       |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC https://civicrm.org/licensing
+ */
+class CRM_Utils_Check_Component_Tokens extends CRM_Utils_Check_Component {
+
+  /**
+   * Check that deprecated and / or tokens no longer exist/
+   *
+   * @return CRM_Utils_Check_Message[]
+   */
+  public function checkTokens(): array {
+    $changes = CRM_Utils_Token::getTokenDeprecations();
+    $messages = $problems = [];
+    foreach ($changes['WorkFlowMessageTemplates'] as $workflowName => $workflowChanges) {
+      foreach ($workflowChanges as $old => $new) {
+        if (CRM_Core_DAO::singleValueQuery("
+          SELECT COUNT(*)
+          FROM civicrm_msg_template
+          WHERE workflow_name = '$workflowName'
+          AND (
+            msg_html LIKE '%$old%'
+            OR msg_subject LIKE '%$old%'
+            OR civicrm_msg_template.msg_text LIKE '%$old%'
+          )
+        ")) {
+          $problems[] = ts('Please review your %1 message template and remove references to the token %2 as it has been replaced by %3', [
+            1 => $workflowName,
+            2 => '{' . $old . '}',
+            3 => '{' . $new . '}',
+          ]);
+        }
+      }
+    }
+    if (!empty($problems)) {
+      $messages[] = new CRM_Utils_Check_Message(
+        __FUNCTION__ . md5(implode(',', $problems)),
+        '<p>' .
+        ts('You are using tokens that have been removed or deprecated.') .
+        '</p>' .
+        '<ul><li>' .
+        implode('</li><li>', $problems) .
+        '</li></ul></p>',
+        ts('Outdated tokens in use'),
+        \Psr\Log\LogLevel::WARNING
+      );
+    }
+    return $messages;
+  }
+
+}
index 6e9ea5b403e9e9f8a7e56bf6ba08f81ce3bc6041..67f3137c8ae1eb54ff336c348f9bd4447ada8b75 100644 (file)
@@ -1930,4 +1930,19 @@ class CRM_Utils_Token {
     return $value;
   }
 
+  /**
+   * Get token deprecation information.
+   *
+   * @return array
+   */
+  public static function getTokenDeprecations(): array {
+    return [
+      'WorkFlowMessageTemplates' => [
+        'contribution_invoice_receipt' => [
+          '$display_name' => 'contact.display_name',
+        ],
+      ],
+    ];
+  }
+
 }
index b3dc1ca9adfde6374b0090bdf7aa10962619943c..f2965df67f3378ef9cbca5c441530c4e99741f6f 100644 (file)
@@ -51,8 +51,9 @@ class CRM_Upgrade_Incremental_BaseTest extends CiviUnitTestCase {
       'workflow_name', '=', 'contribution_invoice_receipt'
     )->execute();
     $upgrader = new CRM_Upgrade_Incremental_MessageTemplates('5.41.0');
-    $messages = $upgrader->getMessageTemplateWarning('contribution_invoice_receipt', '$display_name', 'contact.display_name');
-    $this->assertEquals('Please review your contribution_invoice_receipt message template and remove references to the token {$display_name} as it has been replaced by {contact.display_name}', $messages);
+    $check = new CRM_Utils_Check_Component_Tokens();
+    $message = $check->checkTokens()[0];
+    $this->assertEquals('<p>You are using tokens that have been removed or deprecated.</p><ul><li>Please review your contribution_invoice_receipt message template and remove references to the token {$display_name} as it has been replaced by {contact.display_name}</li></ul></p>', $message->getMessage());
     $upgrader->replaceTokenInTemplate('contribution_invoice_receipt', '$display_name', 'contact.display_name');
     $templates = MessageTemplate::get()->addSelect('msg_html')
       ->addWhere(
@@ -61,8 +62,8 @@ class CRM_Upgrade_Incremental_BaseTest extends CiviUnitTestCase {
     foreach ($templates as $template) {
       $this->assertEquals('{contact.display_name}', $template['msg_html']);
     }
-    $messages = $upgrader->getMessageTemplateWarning('contribution_invoice_receipt', '$display_name', 'contact.display_name');
-    $this->assertEquals('', $messages);
+    $messages = $check->checkTokens();
+    $this->assertEmpty($messages);
     $this->revertTemplateToReservedTemplate('contribution_invoice_receipt');
   }