From: Aidan Saunders Date: Wed, 23 Nov 2022 16:50:15 +0000 (+0000) Subject: Add `master_id` calculated field X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=ac13c0067765470ce083e076d71719ffa0ca87b7;p=civicrm-core.git Add `master_id` calculated field Add tet for `master_id` --- diff --git a/Civi/Api4/Service/Spec/Provider/MessageTemplateGetSpecProvider.php b/Civi/Api4/Service/Spec/Provider/MessageTemplateGetSpecProvider.php new file mode 100644 index 0000000000..ca8714e0e3 --- /dev/null +++ b/Civi/Api4/Service/Spec/Provider/MessageTemplateGetSpecProvider.php @@ -0,0 +1,55 @@ +setLabel(ts('Master ID')) + ->setTitle(ts('Master ID')) + ->setColumnName('id') + ->setDescription(ts('MessageID that this could revert to')) + ->setInputType('Select') + ->setReadonly(TRUE) + ->setFkEntity('MessageTemplate') + ->setSqlRenderer([__CLASS__, 'revertible']); + $spec->addFieldSpec($field); + } + + /** + * @param string $entity + * @param string $action + * + * @return bool + */ + public function applies($entity, $action) { + return $entity === 'MessageTemplate' && $action === 'get'; + } + + /** + * Callback for finding id of template to revert to + * Based on CRM_Admin_Page_MessageTemplates::__construct() + * + * @return string + */ + public static function revertible(): string { + return "(SELECT `id` FROM `civicrm_msg_template` `orig` + WHERE `a`.`workflow_name` = `orig`.`workflow_name` AND `orig`.`is_reserved` = 1 AND + ( `a`.`msg_subject` != `orig`.`msg_subject` OR + `a`.`msg_text` != `orig`.`msg_text` OR + `a`.`msg_html` != `orig`.`msg_html` + ))"; + } + +} diff --git a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php index a7f5108178..24fb1e213f 100644 --- a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php +++ b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php @@ -319,6 +319,45 @@ class CRM_Core_BAO_MessageTemplateTest extends CiviUnitTestCase { $this->assertStringContainsString('Case ID : 1234', $message); } + /** + * Test APIv4 calculated field master_id + */ + public function testMessageTemplateMasterID() { + CRM_Core_Transaction::create(TRUE)->run(function(CRM_Core_Transaction $tx) { + $tx->rollback(); + + $messageTemplateID = MessageTemplate::get() + ->addWhere('is_default', '=', 1) + ->addWhere('workflow_name', '=', 'contribution_offline_receipt') + ->addSelect('id') + ->execute()->first()['id']; + $messageTemplateIDReserved = MessageTemplate::get() + ->addWhere('is_reserved', '=', 1) + ->addWhere('workflow_name', '=', 'contribution_offline_receipt') + ->addSelect('id') + ->execute()->first()['id']; + $master_id = MessageTemplate::get() + ->addSelect('master_id') + ->addWhere('id', '=', $messageTemplateID) + ->execute()->first()['master_id']; + $this->assertNull($master_id); + + MessageTemplate::update() + ->addWhere('id', '=', $messageTemplateID) + ->setValues([ + 'msg_subject' => 'Hello world', + 'msg_text' => 'Hello world', + 'msg_html' => '

Hello world

', + ]) + ->execute(); + $master_id = MessageTemplate::get() + ->addSelect('master_id') + ->addWhere('id', '=', $messageTemplateID) + ->execute()->first()['master_id']; + $this->assertEquals($master_id, $messageTemplateIDReserved); + }); + } + /** * Test rendering of domain tokens. *