Add `master_id` calculated field
authorAidan Saunders <aidan.saunders@squiffle.uk>
Wed, 23 Nov 2022 16:50:15 +0000 (16:50 +0000)
committerAidan Saunders <aidan.saunders@squiffle.uk>
Wed, 23 Nov 2022 16:50:15 +0000 (16:50 +0000)
Add tet for `master_id`

Civi/Api4/Service/Spec/Provider/MessageTemplateGetSpecProvider.php [new file with mode: 0644]
tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php

diff --git a/Civi/Api4/Service/Spec/Provider/MessageTemplateGetSpecProvider.php b/Civi/Api4/Service/Spec/Provider/MessageTemplateGetSpecProvider.php
new file mode 100644 (file)
index 0000000..ca8714e
--- /dev/null
@@ -0,0 +1,55 @@
+<?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`
+        ))";
+  }
+
+}
index a7f51081782f89f89aeaabc9cb63fcce333b690f..24fb1e213f62de2d6857a16e4a2577d766264e5b 100644 (file)
@@ -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' => '<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.
    *