forked from civicrm/civicrm-core
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add upgrade function for message templates that does not involve copy…
…ing the whole template
- Loading branch information
1 parent
5f5b9c2
commit 7e0ee04
Showing
5 changed files
with
248 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php | ||
/* | ||
+--------------------------------------------------------------------+ | ||
| CiviCRM version 5 | | ||
+--------------------------------------------------------------------+ | ||
| Copyright CiviCRM LLC (c) 2004-2018 | | ||
+--------------------------------------------------------------------+ | ||
| This file is a part of CiviCRM. | | ||
| | | ||
| CiviCRM is free software; you can copy, modify, and distribute it | | ||
| under the terms of the GNU Affero General Public License | | ||
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | ||
| | | ||
| CiviCRM is distributed in the hope that it will be useful, but | | ||
| WITHOUT ANY WARRANTY; without even the implied warranty of | | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | ||
| See the GNU Affero General Public License for more details. | | ||
| | | ||
| You should have received a copy of the GNU Affero General Public | | ||
| License and the CiviCRM Licensing Exception along | | ||
| with this program; if not, contact CiviCRM LLC | | ||
| at info[AT]civicrm[DOT]org. If you have questions about the | | ||
| GNU Affero General Public License or the licensing of CiviCRM, | | ||
| see the CiviCRM license FAQ at http://civicrm.org/licensing | | ||
+--------------------------------------------------------------------+ | ||
*/ | ||
|
||
/** | ||
* | ||
* @package CRM | ||
* @copyright CiviCRM LLC (c) 2004-2018 | ||
*/ | ||
class CRM_Upgrade_Incremental_MessageTemplates { | ||
|
||
/** | ||
* Version we are upgrading to. | ||
* | ||
* @var string | ||
*/ | ||
protected $upgradeVersion; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getUpgradeVersion() { | ||
return $this->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', | ||
'template' => 'membership_online_receipt', | ||
'upgrade_descriptor' => ts('Use email greeting at top where available'), | ||
'type' => 'text', | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* Get any required template updates. | ||
* | ||
* @return array | ||
*/ | ||
public function getTemplatesToUpdate() { | ||
$templates = $this->getTemplateUpdates(); | ||
$return = []; | ||
foreach ($templates as $template) { | ||
if ($template['version'] === $this->getUpgradeVersion()) { | ||
$return[$template['template'] . '_' . $template['type']] = $template; | ||
} | ||
} | ||
return $return; | ||
} | ||
|
||
/** | ||
* Get the upgrade messages. | ||
*/ | ||
public function getUpgradeMessages() { | ||
$updates = $this->getTemplatesToUpdate(); | ||
$messages = []; | ||
foreach ($updates as $key => $value) { | ||
$messages[$key] = $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['template'], 'String'], | ||
]); | ||
$content = file_get_contents(\Civi::paths()->getPath('[civicrm.root]/xml/templates/message_templates/' . $template['template'] . '_' . $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'] | ||
] | ||
); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
/** | ||
* Class CRM_UF_Page_ProfileEditorTest | ||
* @group headless | ||
*/ | ||
class CRM_Upgrade_Incremental_Base_Test extends CiviUnitTestCase { | ||
|
||
/** | ||
* Test message upgrade process. | ||
*/ | ||
public function testMessageTemplateUpgrade() { | ||
$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) { | ||
$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']]); | ||
} | ||
} | ||
} | ||
|
||
} |