From f935c7b49bbb85bb72b9eab3f7cb3d59719d990e Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 24 Jun 2023 22:59:07 -0700 Subject: [PATCH] Fix test to use created workflow Currently it uses a 'real' workflow & if we change the workflow the test breaks - which is not what the test is testing. The test checks loading & rendering from the database, not a specific workflow. This fixes to create a template for the test, rather than using a real one --- .../ExampleWorkflowMessageTest.php | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/tests/phpunit/Civi/WorkflowMessage/ExampleWorkflowMessageTest.php b/tests/phpunit/Civi/WorkflowMessage/ExampleWorkflowMessageTest.php index 470ccc8342..a2616c1d66 100644 --- a/tests/phpunit/Civi/WorkflowMessage/ExampleWorkflowMessageTest.php +++ b/tests/phpunit/Civi/WorkflowMessage/ExampleWorkflowMessageTest.php @@ -11,6 +11,7 @@ namespace Civi\WorkflowMessage; +use Civi\Api4\MessageTemplate; use Civi\Test\Invasive; /** @@ -235,9 +236,9 @@ class ExampleWorkflowMessageTest extends \CiviUnitTestCase { } /** - * Create an impromptu instance of `WorkflowMessage` for a new/unknown workflow. + * Create an impromptu instance of `WorkflowMessage` for a new/unknown workflow. */ - public function testImpromptuImportExport() { + public function testImpromptuImportExport(): void { /** @var \Civi\WorkflowMessage\WorkflowMessageInterface $ex */ $ex = WorkflowMessage::create('some_impromptu_wf', [ 'envelope' => ['from' => 'foo@example.com'], @@ -306,12 +307,23 @@ class ExampleWorkflowMessageTest extends \CiviUnitTestCase { $this->assertEquals('Hello Foo Bar' . $rand, $rendered['subject']); } - public function testRenderStoredTemplate() { + /** + * Test a stored template renders template values assigned via 'tplParams'. + * + * @throws \CRM_Core_Exception + */ + public function testRenderStoredTemplate(): void { $hookCount = 0; - $rand = rand(0, 1000); - $cid = $this->individualCreate(['first_name' => 'Foo', 'last_name' => 'Bar' . $rand, 'prefix_id' => NULL, 'suffix_id' => NULL]); - /** @var \Civi\WorkflowMessage\GenericWorkflowMessage $ex */ - $ex = WorkflowMessage::create('petition_sign', [ + $cid = $this->individualCreate(['first_name' => 'Foo', 'last_name' => 'Bar', 'prefix_id' => NULL, 'suffix_id' => NULL]); + MessageTemplate::create()->setValues([ + 'workflow_name' => 'temporary', + 'msg_subject' => '{contact.first_name} {contact.last_name}', + 'msg_text' => 'Thank you for signing {$petitionTitle}', + 'msg_html' => 'Thank you for signing {$petitionTitle}', + 'is_default' => TRUE, + ])->execute(); + /** @var \Civi\WorkflowMessage\GenericWorkflowMessage $example */ + $example = WorkflowMessage::create('temporary', [ 'tokenContext' => ['contactId' => $cid], 'tplParams' => [ 'greeting' => 'Greetings yo', @@ -323,12 +335,12 @@ class ExampleWorkflowMessageTest extends \CiviUnitTestCase { \Civi::dispatcher()->addListener('hook_civicrm_alterMailParams', function($e) use (&$hookCount) { $hookCount++; - $this->assertEquals('petition_sign', $e->params['workflow']); + $this->assertEquals('temporary', $e->params['workflow']); }); $this->assertEquals(0, $hookCount); - $rendered = $ex->renderTemplate(); + $rendered = $example->renderTemplate(); $this->assertEquals(1, $hookCount); - $this->assertStringContainsString('Foo Bar' . $rand, $rendered['subject']); + $this->assertStringContainsString('Foo Bar', $rendered['subject']); $this->assertStringContainsString('Thank you for signing The Fake Petition', $rendered['html']); $this->assertStringContainsString('Thank you for signing The Fake Petition', $rendered['text']); } -- 2.25.1