From fe83c2517d94e8c4bd2e24dd413ecd54f9efc4d8 Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 29 May 2018 20:36:47 +1200 Subject: [PATCH] Add upgrade function for message templates that does not involve copying the whole template --- CRM/Upgrade/Form.php | 1 + CRM/Upgrade/Incremental/Base.php | 12 ++ CRM/Upgrade/Incremental/General.php | 26 +++- CRM/Upgrade/Incremental/MessageTemplates.php | 147 ++++++++++++++++++ .../CRM/Upgrade/Incremental/BaseTest.php | 79 ++++++++++ 5 files changed, 264 insertions(+), 1 deletion(-) create mode 100644 CRM/Upgrade/Incremental/MessageTemplates.php create mode 100644 tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php diff --git a/CRM/Upgrade/Form.php b/CRM/Upgrade/Form.php index b2b09f08f2..7a6ad203c8 100644 --- a/CRM/Upgrade/Form.php +++ b/CRM/Upgrade/Form.php @@ -769,6 +769,7 @@ SET version = '$version' foreach ($revisions as $rev) { if (version_compare($currentVer, $rev) < 0) { $versionObject = $this->incrementalPhpObject($rev); + CRM_Upgrade_Incremental_General::updateMessageTemplate($preUpgradeMessage, $rev); if (is_callable(array($versionObject, 'setPreUpgradeMessage'))) { $versionObject->setPreUpgradeMessage($preUpgradeMessage, $rev, $currentVer); } diff --git a/CRM/Upgrade/Incremental/Base.php b/CRM/Upgrade/Incremental/Base.php index 1cbf1cc3f5..b180904d70 100644 --- a/CRM/Upgrade/Incremental/Base.php +++ b/CRM/Upgrade/Incremental/Base.php @@ -183,6 +183,18 @@ class CRM_Upgrade_Incremental_Base { return TRUE; } + /** + * Do any relevant message template updates. + * + * @param CRM_Queue_TaskContext $ctx + * @param string $version + */ + public static function updateMessageTemplates($ctx, $version) { + $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates($version); + $messageTemplateObject->updateTemplates(); + + } + /** * Drop a column from a table if it exist. * diff --git a/CRM/Upgrade/Incremental/General.php b/CRM/Upgrade/Incremental/General.php index 9f0ebe39c9..3575ce91c9 100644 --- a/CRM/Upgrade/Incremental/General.php +++ b/CRM/Upgrade/Incremental/General.php @@ -117,13 +117,37 @@ class CRM_Upgrade_Incremental_General { } } + /** + * Perform any message template updates. 5.0+. + * @param $message + * @param $version + */ + public static function updateMessageTemplate(&$message, $version) { + if (version_compare($version, 5.0, '<')) { + return; + } + $messageObj = new CRM_Upgrade_Incremental_MessageTemplates($version); + $messages = $messageObj->getUpgradeMessages(); + if (empty($messages)) { + return; + } + $message .= '
' . ts("The default copies of the message templates listed below will be updated to handle new features or correct a problem. Your installation has customized versions of these message templates, and you will need to apply the updates manually after running this upgrade. Click here for detailed instructions. %2", array( + 1 => 'http://wiki.civicrm.org/confluence/display/CRMDOC/Message+Templates#MessageTemplates-UpgradesandCustomizedSystemWorkflowTemplates', + 2 => '', + )); + + $messageObj->updateTemplates(); + } + /** * @param $message * @param $latestVer * @param $currentVer */ public static function checkMessageTemplate(&$message, $latestVer, $currentVer) { - + if (version_compare($currentVer, 5.0, '>')) { + return; + } $sql = "SELECT orig.workflow_id as workflow_id, orig.msg_title as title FROM civicrm_msg_template diverted JOIN civicrm_msg_template orig ON ( diff --git a/CRM/Upgrade/Incremental/MessageTemplates.php b/CRM/Upgrade/Incremental/MessageTemplates.php new file mode 100644 index 0000000000..d6112275ac --- /dev/null +++ b/CRM/Upgrade/Incremental/MessageTemplates.php @@ -0,0 +1,147 @@ +upgradeVersion; + } + + /** + * @param string $upgradeVersion + */ + public function setUpgradeVersion($upgradeVersion) { + $this->upgradeVersion = $upgradeVersion; + } + + /** + * CRM_Upgrade_Incremental_MessageTemplates constructor. + * + * @param string $upgradeVersion + */ + public function __construct($upgradeVersion) { + $this->setUpgradeVersion($upgradeVersion); + } + + /** + * Get any templates that have been updated. + * + * @return array + */ + protected function getTemplateUpdates() { + return [ + [ + 'version' => '5.4.alpha1', + 'upgrade_descriptor' => ts('Use email greeting at top where available'), + 'templates' => [ + ['name' => 'membership_online_receipt', 'type' => 'text'], + ['name' => 'membership_online_receipt', 'type' => 'html'], + ] + ], + ]; + } + + /** + * Get any required template updates. + * + * @return array + */ + public function getTemplatesToUpdate() { + $templates = $this->getTemplateUpdates(); + $return = []; + foreach ($templates as $templateArray) { + if ($templateArray['version'] === $this->getUpgradeVersion()) { + foreach ($templateArray['templates'] as $template) { + $return[$template['name'] . '_' . $template['type']] = array_merge($template, $templateArray); + } + } + } + return $return; + } + + /** + * Get the upgrade messages. + */ + public function getUpgradeMessages() { + $updates = $this->getTemplatesToUpdate(); + $messages = []; + foreach ($updates as $key => $value) { + $templateLabel = civicrm_api3('OptionValue', 'getvalue', [ + 'return' => 'label', 'name' => $value['name'], 'options' => ['limit' => 1] + ]); + $messages[$templateLabel] = $value['upgrade_descriptor']; + } + return $messages; + } + + /** + * Update message templates. + */ + public function updateTemplates() { + $templates = $this->getTemplatesToUpdate(); + foreach ($templates as $template) { + $workFlowID = CRM_Core_DAO::singleValueQuery("SELECT MAX(id) as id FROM civicrm_option_value WHERE name = %1", [ + 1 => [$template['name'], 'String'], + ]); + $content = file_get_contents(\Civi::paths()->getPath('[civicrm.root]/xml/templates/message_templates/' . $template['name'] . '_' . $template['type'] . '.tpl')); + $templatesToUpdate = []; + $templatesToUpdate[] = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_msg_template WHERE workflow_id = $workFlowID AND is_reserved = 1"); + $defaultTemplateID = CRM_Core_DAO::singleValueQuery(" + SELECT default_template.id FROM civicrm_msg_template reserved + LEFT JOIN civicrm_msg_template default_template + ON reserved.workflow_id = default_template.workflow_id + WHERE reserved.workflow_id = $workFlowID + AND reserved.is_reserved = 1 AND default_template.is_default = 1 AND reserved.id <> default_template.id + "); + if ($defaultTemplateID) { + $templatesToUpdate[] = $defaultTemplateID; + } + + CRM_Core_DAO::executeQuery(" + UPDATE civicrm_msg_template SET msg_{$template['type']} = %1 WHERE id IN (" . implode(',', $templatesToUpdate) . ")", [ + 1 => [$content, 'String'] + ] + ); + } + } + +} diff --git a/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php b/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php new file mode 100644 index 0000000000..6acfaeb204 --- /dev/null +++ b/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php @@ -0,0 +1,79 @@ + 'id', 'name' => 'membership_online_receipt', 'options' => ['limit' => 1, 'sort' => 'id DESC']]); + + $templates = $this->callAPISuccess('MessageTemplate', 'get', ['workflow_id' => $workFlowID])['values']; + foreach ($templates as $template) { + $originalText = $template['msg_text']; + $this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => 'great what a cool member you are', 'id' => $template['id']]); + $msg_text = $this->callAPISuccessGetValue('MessageTemplate', ['id' => $template['id'], 'return' => 'msg_text']); + $this->assertEquals('great what a cool member you are', $msg_text); + } + $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates('5.4.alpha1'); + $messageTemplateObject->updateTemplates(); + + foreach ($templates as $template) { + $msg_text = $this->callAPISuccessGetValue('MessageTemplate', ['id' => $template['id'], 'return' => 'msg_text']); + $this->assertContains('{ts}Membership Information{/ts}', $msg_text); + if ($msg_text !== $originalText) { + // Reset value for future tests. + $this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => $originalText, 'id' => $template['id']]); + } + } + } + + /** + * Test message upgrade process only edits the default if the template is customised. + */ + public function testMessageTemplateUpgradeAlreadyCustomised() { + $workFlowID = civicrm_api3('OptionValue', 'getvalue', ['return' => 'id', 'name' => 'membership_online_receipt', 'options' => ['limit' => 1, 'sort' => 'id DESC']]); + + $templates = $this->callAPISuccess('MessageTemplate', 'get', ['workflow_id' => $workFlowID])['values']; + foreach ($templates as $template) { + if ($template['is_reserved']) { + $originalText = $template['msg_text']; + $this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => 'great what a cool member you are', 'id' => $template['id']]); + } + else { + $this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => 'great what a silly sausage you are', 'id' => $template['id']]); + } + } + $messageTemplateObject = new CRM_Upgrade_Incremental_MessageTemplates('5.4.alpha1'); + $messageTemplateObject->updateTemplates(); + + foreach ($templates as $template) { + $msg_text = $this->callAPISuccessGetValue('MessageTemplate', ['id' => $template['id'], 'return' => 'msg_text']); + if ($template['is_reserved']) { + $this->assertTrue(strstr($msg_text, '{ts}Membership Information{/ts}')); + } + else { + $this->assertEquals('great what a silly sausage you are', $msg_text); + } + + if ($msg_text !== $originalText) { + // Reset value for future tests. + $this->callAPISuccess('MessageTemplate', 'create', ['msg_text' => $originalText, 'id' => $template['id']]); + } + } + } + + /** + * Test function for messages on upgrade. + */ + public function testMessageTemplateGetUpgradeMessages() { + $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'], $messages); + } + +} -- 2.25.1