From e987e6ed3b126276cc188c6ce070dcf4d7ba18c0 Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 2 Jan 2024 16:10:58 +1300 Subject: [PATCH] Use distinct test workflow message for workflow message tests The tests for the workflow message subsystem use an instance of a test case_activity template attached the real message workflow template. They test the text version - meaning that removing it, like we have with others, causes various fails. In addition adding a real template that does not match the test version causes mayhem. In this case we want to ensure we keep all the tests for the subsystem functionality but undo the tethering to a particular template to allow that template to have updates without having to go deep into this code --- .../Case/WorkflowMessage/CaseActivityTest.php | 14 ++-- ...ivity.php => CaseActivityTestWorkflow.php} | 11 ++- .../CaseAdhocExample.php | 4 +- .../CaseModelExample.php | 4 +- .../CRM/Core/BAO/MessageTemplateTest.php | 71 +++++++++++++++++-- .../api/v4/Entity/WorkflowMessageTest.php | 13 +++- 6 files changed, 92 insertions(+), 25 deletions(-) rename tests/phpunit/CRM/Case/WorkflowMessage/{CaseActivity.php => CaseActivityTestWorkflow.php} (87%) rename tests/phpunit/CRM/Case/WorkflowMessage/{CaseActivity => CaseActivityTestWorkflow}/CaseAdhocExample.php (85%) rename tests/phpunit/CRM/Case/WorkflowMessage/{CaseActivity => CaseActivityTestWorkflow}/CaseModelExample.php (87%) diff --git a/tests/phpunit/CRM/Case/WorkflowMessage/CaseActivityTest.php b/tests/phpunit/CRM/Case/WorkflowMessage/CaseActivityTest.php index 346503aa80..b13f43b5f3 100644 --- a/tests/phpunit/CRM/Case/WorkflowMessage/CaseActivityTest.php +++ b/tests/phpunit/CRM/Case/WorkflowMessage/CaseActivityTest.php @@ -10,6 +10,8 @@ +--------------------------------------------------------------------+ */ +use Civi\WorkflowMessage\WorkflowMessage; + /** * Class CRM_Case_WorkflowMessage_CaseActivityTest * @group msgtpl @@ -18,18 +20,18 @@ class CRM_Case_WorkflowMessage_CaseActivityTest extends CiviUnitTestCase { use \Civi\Test\WorkflowMessageTestTrait; public function getWorkflowClass(): string { - return CRM_Case_WorkflowMessage_CaseActivity::class; + return CRM_Case_WorkflowMessage_CaseActivityTestWorkflow::class; } public function testAdhocClassEquiv(): void { $examples = \Civi\Api4\ExampleData::get(0) ->setSelect(['name', 'data']) - ->addWhere('name', 'IN', ['workflow/case_activity/CaseAdhocExample', 'workflow/case_activity/CaseModelExample']) + ->addWhere('name', 'IN', ['workflow/case_activity_test/CaseAdhocExample', 'workflow/case_activity_test/CaseModelExample']) ->execute() ->indexBy('name') ->column('data'); - $byAdhoc = Civi\WorkflowMessage\WorkflowMessage::create('case_activity', $examples['workflow/case_activity/CaseAdhocExample']); - $byClass = new CRM_Case_WorkflowMessage_CaseActivity($examples['workflow/case_activity/CaseModelExample']); + $byAdhoc = WorkflowMessage::create('case_activity_test', $examples['workflow/case_activity_test/CaseAdhocExample']); + $byClass = new CRM_Case_WorkflowMessage_CaseActivityTestWorkflow($examples['workflow/case_activity_test/CaseModelExample']); $this->assertSameWorkflowMessage($byClass, $byAdhoc, 'Compare byClass and byAdhoc: '); } @@ -53,8 +55,8 @@ class CRM_Case_WorkflowMessage_CaseActivityTest extends CiviUnitTestCase { * @throws \Civi\API\Exception\UnauthorizedException */ public function testExampleGet(): void { - $file = \Civi::paths()->getPath('[civicrm.root]/tests/phpunit/CRM/Case/WorkflowMessage/CaseActivity/CaseModelExample.php'); - $name = 'workflow/case_activity/CaseModelExample'; + $file = \Civi::paths()->getPath('[civicrm.root]/tests/phpunit/CRM/Case/WorkflowMessage/CaseActivityTestWorkflow/CaseModelExample.php'); + $name = 'workflow/case_activity_test/CaseModelExample'; $this->assertTrue(file_exists($file), "Expect find canary file ($file)"); diff --git a/tests/phpunit/CRM/Case/WorkflowMessage/CaseActivity.php b/tests/phpunit/CRM/Case/WorkflowMessage/CaseActivityTestWorkflow.php similarity index 87% rename from tests/phpunit/CRM/Case/WorkflowMessage/CaseActivity.php rename to tests/phpunit/CRM/Case/WorkflowMessage/CaseActivityTestWorkflow.php index 738f57cd26..76f6f5be63 100644 --- a/tests/phpunit/CRM/Case/WorkflowMessage/CaseActivity.php +++ b/tests/phpunit/CRM/Case/WorkflowMessage/CaseActivityTestWorkflow.php @@ -9,18 +9,17 @@ +--------------------------------------------------------------------+ */ +use Civi\WorkflowMessage\GenericWorkflowMessage; + /** - * When an activity is created in a case, the "case_activity" email is sent. - * Generally, the email is sent to the assignee, although (depending on - * the configuration/add-ons) additional copies may be sent. + * Sample to test test model architecture. * * @see CRM_Case_BAO_Case::sendActivityCopy * @support template-only */ -class CRM_Case_WorkflowMessage_CaseActivity extends Civi\WorkflowMessage\GenericWorkflowMessage { +class CRM_Case_WorkflowMessage_CaseActivityTestWorkflow extends GenericWorkflowMessage { - const GROUP = 'msg_tpl_workflow_case'; - const WORKFLOW = 'case_activity'; + const WORKFLOW = 'case_activity_test'; /** * The recipient of the notification. The `{contact.*}` tokens will reference this person. diff --git a/tests/phpunit/CRM/Case/WorkflowMessage/CaseActivity/CaseAdhocExample.php b/tests/phpunit/CRM/Case/WorkflowMessage/CaseActivityTestWorkflow/CaseAdhocExample.php similarity index 85% rename from tests/phpunit/CRM/Case/WorkflowMessage/CaseActivity/CaseAdhocExample.php rename to tests/phpunit/CRM/Case/WorkflowMessage/CaseActivityTestWorkflow/CaseAdhocExample.php index fefd6868a9..a260960c37 100644 --- a/tests/phpunit/CRM/Case/WorkflowMessage/CaseActivity/CaseAdhocExample.php +++ b/tests/phpunit/CRM/Case/WorkflowMessage/CaseActivityTestWorkflow/CaseAdhocExample.php @@ -1,6 +1,6 @@ "workflow/{$this->wfName}/{$this->exName}", + 'name' => 'workflow/case_activity_test/CaseAdhocExample', 'title' => ts('Case Activity (Adhoc-style example)'), 'tags' => [], ]; diff --git a/tests/phpunit/CRM/Case/WorkflowMessage/CaseActivity/CaseModelExample.php b/tests/phpunit/CRM/Case/WorkflowMessage/CaseActivityTestWorkflow/CaseModelExample.php similarity index 87% rename from tests/phpunit/CRM/Case/WorkflowMessage/CaseActivity/CaseModelExample.php rename to tests/phpunit/CRM/Case/WorkflowMessage/CaseActivityTestWorkflow/CaseModelExample.php index a7a9919cc9..a8ded96390 100644 --- a/tests/phpunit/CRM/Case/WorkflowMessage/CaseActivity/CaseModelExample.php +++ b/tests/phpunit/CRM/Case/WorkflowMessage/CaseActivityTestWorkflow/CaseModelExample.php @@ -1,5 +1,5 @@ "workflow/{$this->wfName}/{$this->exName}", + 'name' => 'workflow/case_activity_test/CaseModelExample', 'title' => ts('Case Activity (Class-style example)'), 'tags' => ['phpunit', 'preview'], ]; diff --git a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php index 65da688e19..f4afc392a7 100644 --- a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php +++ b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php @@ -396,8 +396,11 @@ class CRM_Core_BAO_MessageTemplateTest extends CiviUnitTestCase { public function testCaseActivityCopyTemplate():void { $client_id = $this->individualCreate(); $contact_id = $this->individualCreate(); - - $msg = WorkflowMessage::create('case_activity', [ + \CRM_Core_DAO::executeQuery(" + INSERT INTO civicrm_msg_template (msg_text, msg_subject, workflow_name, is_active, is_default) + VALUES('" . $this->getActivityCaseText() . "', '" . $this->getActivityCaseSubject() . "', 'case_activity_test', 1, 1) + "); + $msg = WorkflowMessage::create('case_activity_test', [ 'modelProps' => [ 'contactID' => $contact_id, 'contact' => ['role' => 'Sand grain counter'], @@ -420,7 +423,7 @@ class CRM_Core_BAO_MessageTemplateTest extends CiviUnitTestCase { $this->assertEquals([], Invasive::get([$msg, '_extras'])); [, $subject, $message] = $msg->sendTemplate([ - 'workflow' => 'case_activity', + 'workflow' => 'case_activity_test', 'from' => 'admin@example.com', 'toName' => 'Demo', 'toEmail' => 'admin@example.com', @@ -434,9 +437,9 @@ class CRM_Core_BAO_MessageTemplateTest extends CiviUnitTestCase { public function testSendToEmail_variantA(): void { $mut = new CiviMailUtils($this, TRUE); - $cid = $this->individualCreate(); + $this->individualCreate(); - $msg = \Civi\WorkflowMessage\WorkflowMessage::create('petition_sign', [ + $msg = WorkflowMessage::create('petition_sign', [ 'from' => '"The Sender" ', 'toEmail' => 'demo-a@example.com', 'contactId' => 204, @@ -456,11 +459,67 @@ class CRM_Core_BAO_MessageTemplateTest extends CiviUnitTestCase { $mut->stop(); } + /** + * Get the contents of the case activity text template, from when the test was written. + * @return string + */ + public function getActivityCaseSubject(): string { + return '{if !empty($idHash)}[case #{$idHash}]{/if} {$activitySubject}'; + } + + /** + * Get the contents of the case activity text template, from when the test was written. + * @return string + */ + public function getActivityCaseText(): string { + return '=========================================================== +{ts}Activity Summary{/ts} - {$activityTypeName} +=========================================================== +{if !empty($isCaseActivity)} +{ts}Your Case Role(s){/ts} : {$contact.role|default:""} +{if !empty($manageCaseURL)} +{ts}Manage Case{/ts} : {$manageCaseURL} +{/if} +{/if} + +{if !empty($editActURL)} +{ts}Edit activity{/ts} : {$editActURL} +{/if} +{if !empty($viewActURL)} +{ts}View activity{/ts} : {$viewActURL} +{/if} + +{foreach from=$activity.fields item=field} +{if $field.type eq "Date"} +{$field.label} : {$field.value|crmDate:$config->dateformatDatetime} +{else} +{$field.label} : {$field.value} +{/if} +{/foreach} + +{if !empty($activity.customGroups)} +{foreach from=$activity.customGroups key=customGroupName item=customGroup} +========================================================== +{$customGroupName} +========================================================== +{foreach from=$customGroup item=field} +{if $field.type eq "Date"} +{$field.label} : {$field.value|crmDate:$config->dateformatDatetime} +{else} +{$field.label} : {$field.value} +{/if} +{/foreach} + +{/foreach} +{/if} +'; + } + public function testSendToEmail_variantB(): void { $mut = new CiviMailUtils($this, TRUE); $cid = $this->individualCreate(); - \Civi\WorkflowMessage\WorkflowMessage::create('petition_sign') + WorkflowMessage::create('petition_sign') ->setFrom(['name' => 'The Sender', 'email' => 'sender-b@example.com']) ->setTo(['name' => 'The Recipient', 'email' => 'demo-b@example.com']) ->setContactID($cid) diff --git a/tests/phpunit/api/v4/Entity/WorkflowMessageTest.php b/tests/phpunit/api/v4/Entity/WorkflowMessageTest.php index 82fd10bd05..e91c05c329 100644 --- a/tests/phpunit/api/v4/Entity/WorkflowMessageTest.php +++ b/tests/phpunit/api/v4/Entity/WorkflowMessageTest.php @@ -47,8 +47,15 @@ class WorkflowMessageTest extends Api4TestBase implements TransactionalInterface * @throws \CRM_Core_Exception */ public function testRenderDefaultTemplate(): void { + \CRM_Core_DAO::executeQuery(" + INSERT INTO civicrm_msg_template (msg_text, workflow_name, is_active, is_default) + VALUES('" . '{foreach from=$activity.fields item=field} +{$field.label} : {$field.value} +{/foreach}' . "', 'case_activity_test', 1, 1) + "); + $ex = ExampleData::get(FALSE) - ->addWhere('name', '=', 'workflow/case_activity/CaseModelExample') + ->addWhere('name', '=', 'workflow/case_activity_test/CaseModelExample') ->addSelect('data') ->addChain('render', WorkflowMessage::render() ->setWorkflow('$data.workflow') @@ -64,12 +71,12 @@ class WorkflowMessageTest extends Api4TestBase implements TransactionalInterface */ public function testRenderCustomTemplate(): void { $ex = ExampleData::get(0) - ->addWhere('name', '=', 'workflow/case_activity/CaseModelExample') + ->addWhere('name', '=', 'workflow/case_activity_test/CaseModelExample') ->addSelect('data') ->execute() ->single(); $result = WorkflowMessage::render(0) - ->setWorkflow('case_activity') + ->setWorkflow('case_activity_test') ->setValues($ex['data']['modelProps']) ->setMessageTemplate([ 'msg_text' => 'The role is {$contact.role}.', -- 2.25.1