From 581691871b358449d8e6f490e1446ca6298c65ed Mon Sep 17 00:00:00 2001
From: Eileen McNaughton
Date: Fri, 27 Aug 2021 13:21:43 +1200
Subject: [PATCH] Fix token deprecation to be a check not an upgrade notice
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 | 32 ----------
CRM/Upgrade/Incremental/php/FiveFortyOne.php | 12 +---
CRM/Utils/Check/Component/Tokens.php | 63 +++++++++++++++++++
CRM/Utils/Token.php | 15 +++++
.../CRM/Upgrade/Incremental/BaseTest.php | 9 +--
5 files changed, 86 insertions(+), 45 deletions(-)
create mode 100644 CRM/Utils/Check/Component/Tokens.php
diff --git a/CRM/Upgrade/Incremental/MessageTemplates.php b/CRM/Upgrade/Incremental/MessageTemplates.php
index 944a1e69da..f2a8b8c618 100644
--- a/CRM/Upgrade/Incremental/MessageTemplates.php
+++ b/CRM/Upgrade/Incremental/MessageTemplates.php
@@ -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.
*/
diff --git a/CRM/Upgrade/Incremental/php/FiveFortyOne.php b/CRM/Upgrade/Incremental/php/FiveFortyOne.php
index f672c3031e..027f0be9c1 100644
--- a/CRM/Upgrade/Incremental/php/FiveFortyOne.php
+++ b/CRM/Upgrade/Incremental/php/FiveFortyOne.php
@@ -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 .= '- ' . htmlspecialchars($upgradeMessage) . '
';
+ public function setPostUpgradeMessage(&$postUpgradeMessage, $rev): void {
+ if ($rev === '5.41.alpha1') {
+ $postUpgradeMessage .= '
' . 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 .= '
' . 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
index 0000000000..07472f3c0f
--- /dev/null
+++ b/CRM/Utils/Check/Component/Tokens.php
@@ -0,0 +1,63 @@
+ $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)),
+ '' .
+ ts('You are using tokens that have been removed or deprecated.') .
+ '
' .
+ '- ' .
+ implode('
- ', $problems) .
+ '
',
+ ts('Outdated tokens in use'),
+ \Psr\Log\LogLevel::WARNING
+ );
+ }
+ return $messages;
+ }
+
+}
diff --git a/CRM/Utils/Token.php b/CRM/Utils/Token.php
index 6e9ea5b403..67f3137c8a 100644
--- a/CRM/Utils/Token.php
+++ b/CRM/Utils/Token.php
@@ -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',
+ ],
+ ],
+ ];
+ }
+
}
diff --git a/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php b/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php
index b3dc1ca9ad..f2965df67f 100644
--- a/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php
+++ b/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php
@@ -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('You are using tokens that have been removed or deprecated.
- 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}
', $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');
}
--
2.25.1