--- /dev/null
+<?php
+
+namespace Civi\Api4\Service\Spec\Provider;
+
+use Civi\Api4\Service\Spec\FieldSpec;
+use Civi\Api4\Service\Spec\RequestSpec;
+
+/**
+ * @service
+ * @internal
+ */
+class MessageTemplateGetSpecProvider extends \Civi\Core\Service\AutoService implements Generic\SpecProviderInterface {
+
+ /**
+ * @param \Civi\Api4\Service\Spec\RequestSpec $spec
+ */
+ public function modifySpec(RequestSpec $spec) {
+ $field = new FieldSpec('master_id', 'MessageTemplate', 'Integer');
+ $field->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`
+ ))";
+ }
+
+}
$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' => '<p>Hello world</p>',
+ ])
+ ->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.
*