Merge pull request #23777 from darrick/fix_import_disableUSPS
[civicrm-core.git] / tests / events / hook_civicrm_alterMailParams.evch.php
index d7ae1bf5b939155a1f1e17c297c797f65538bccb..cc331f1aa254fd35201e3f2f1c835271304d037b 100644 (file)
@@ -1,5 +1,9 @@
 <?php
-return new class() extends \Civi\Test\EventCheck implements \Civi\Test\HookInterface {
+
+use Civi\Test\EventCheck;
+use Civi\Test\HookInterface;
+
+return new class() extends EventCheck implements HookInterface {
 
   private $paramSpecs = [
 
@@ -23,6 +27,7 @@ return new class() extends \Civi\Test\EventCheck implements \Civi\Test\HookInter
     'autoSubmitted' => ['type' => 'bool', 'for' => 'messageTemplate'],
     'Message-ID' => ['type' => 'string', 'for' => ['messageTemplate', 'singleEmail']],
     'messageId' => ['type' => 'string', 'for' => ['messageTemplate', 'singleEmail']],
+    'contactId' => ['type' => 'int|NULL', 'for' => ['messageTemplate' /* deprecated in favor of tokenContext[contactId] */, 'singleEmail']],
 
     // ## Envelope: CiviMail/Flexmailer
 
@@ -45,7 +50,11 @@ return new class() extends \Civi\Test\EventCheck implements \Civi\Test\HookInter
 
     'tokenContext' => ['type' => 'array', 'for' => 'messageTemplate'],
     'tplParams' => ['type' => 'array', 'for' => 'messageTemplate'],
-    'contactId' => ['type' => 'int|NULL', 'for' => 'messageTemplate' /* deprecated in favor of tokenContext[contactId] */],
+    'workflow' => [
+      'regex' => '/^([a-zA-Z_]+)$/',
+      'type' => 'string',
+      'for' => 'messageTemplate',
+    ],
     'valueName' => [
       'regex' => '/^([a-zA-Z_]+)$/',
       'type' => 'string',
@@ -80,7 +89,7 @@ return new class() extends \Civi\Test\EventCheck implements \Civi\Test\HookInter
     'disableSmarty' => ['type' => 'bool|int', 'for' => 'messageTemplate'],
   ];
 
-  public function isSupported($test) {
+  public function isSupported($test): bool {
     // MailTest does intentionally breaky things to provoke+ensure decent error-handling.
     //So we will not enforce generic rules on it.
     return !($test instanceof CRM_Utils_MailTest);
@@ -91,8 +100,8 @@ return new class() extends \Civi\Test\EventCheck implements \Civi\Test\HookInter
    *
    * @see \CRM_Utils_Hook::alterMailParams()
    */
-  public function hook_civicrm_alterMailParams(&$params, $context = NULL) {
-    $msg = "Non-conformant hook_civicrm_alterMailParams(..., $context)";
+  public function hook_civicrm_alterMailParams(&$params, $context = NULL): void {
+    $msg = 'Non-conforming hook_civicrm_alterMailParams(..., $context)';
     $dump = print_r($params, 1);
 
     $this->assertRegExp('/^(messageTemplate|civimail|singleEmail|flexmailer)$/',
@@ -124,7 +133,11 @@ return new class() extends \Civi\Test\EventCheck implements \Civi\Test\HookInter
     }
 
     if ($context === 'messageTemplate') {
-      $this->assertTrue(!empty($params['valueName']), "$msg: Message templates must always specify the name of the workflow step\n$dump");
+      $this->assertNotEmpty($params['workflow'], "$msg: Message templates must always specify a symbolic name of the step/task\n$dump");
+      if (isset($params['valueName'])) {
+        // This doesn't require that valueName be supplied - but if it is supplied, it must match the workflow name.
+        $this->assertEquals($params['workflow'], $params['valueName'], "$msg: If given, workflow and valueName must match\n$dump");
+      }
       $this->assertEquals($params['contactId'] ?? NULL, $params['tokenContext']['contactId'] ?? NULL, "$msg: contactId moved to tokenContext, but legacy value should be equivalent\n$dump");
 
       // This assertion is surprising -- yet true. We should perhaps check if it was true in past releases...
@@ -136,8 +149,8 @@ return new class() extends \Civi\Test\EventCheck implements \Civi\Test\HookInter
     }
 
     if (isset($params['groupName']) && $params['groupName'] === 'Scheduled Reminder Sender') {
-      $this->assertTrue(!empty($params['entity']), "$msg: Scheduled reminders should have entity\n$dump");
-      $this->assertTrue(!empty($params['entity_id']), "$msg: Scheduled reminders should have entity_id\n$dump");
+      $this->assertNotEmpty($params['entity'], "$msg: Scheduled reminders should have entity\n$dump");
+      $this->assertNotEmpty($params['entity_id'], "$msg: Scheduled reminders should have entity_id\n$dump");
     }
   }