From 66125803ee97183d3f3950b26f1c22bf0d4ec67e Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 15 Sep 2021 03:45:25 -0700 Subject: [PATCH] RecurringEdit - Add example. Include test assertions. Fix undeclared/missing properties. This creates a new example of the `RecurringEdit` workflow message. Note that the example is tagged `phpunit` and defines a list of `asserts`. These assertions are evaluated using the default message-template. The test was not passing because some important properties were missing from `RecurringEdit`. --- .../WorkflowMessage/RecurringEdit.php | 34 ++++++++++++++++++- .../RecurringEdit/BasicEditExample.ex.php | 28 +++++++++++++++ .../WorkflowMessageExample.php | 7 ++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 CRM/Contribute/WorkflowMessage/RecurringEdit/BasicEditExample.ex.php diff --git a/CRM/Contribute/WorkflowMessage/RecurringEdit.php b/CRM/Contribute/WorkflowMessage/RecurringEdit.php index c736686cf4..ee94deec98 100644 --- a/CRM/Contribute/WorkflowMessage/RecurringEdit.php +++ b/CRM/Contribute/WorkflowMessage/RecurringEdit.php @@ -1,10 +1,10 @@ from` in the basic AddressingTrait. + * + * @var string|null + * @scope tplParams as receipt_from_email + */ + public $receiptFromEmail; + + public function setContributionRecur(array $contributionRecur) { + $this->contributionRecur = $contributionRecur; + if (!empty($contributionRecur['id'])) { + $this->contributionRecurId = $contributionRecur['id']; + } + return $this; + } + protected function exportExtraTokenContext(array &$export): void { $export['smartyTokenAlias']['installments'] = 'contribution_recur.installments'; $export['smartyTokenAlias']['amount'] = 'contribution_recur.amount'; @@ -38,4 +63,11 @@ class CRM_Contribute_WorkflowMessage_RecurringEdit extends Civi\WorkflowMessage\ $export['smartyTokenAlias']['recur_frequency_interval'] = 'contribution_recur.frequency_interval'; } + protected function exportExtraTplParams(array &$export): void { + if (empty($export['receipt_from_email']) && !empty($this->from)) { + // At a minimum, we can at least autofill 'receipt_from_email' in the case where it's missing. + $export['receipt_from_email'] = $this->getFrom('record')['email']; + } + } + } diff --git a/CRM/Contribute/WorkflowMessage/RecurringEdit/BasicEditExample.ex.php b/CRM/Contribute/WorkflowMessage/RecurringEdit/BasicEditExample.ex.php new file mode 100644 index 0000000000..f560e73e85 --- /dev/null +++ b/CRM/Contribute/WorkflowMessage/RecurringEdit/BasicEditExample.ex.php @@ -0,0 +1,28 @@ + "workflow/{$this->wfName}/{$this->exName}", + // This title is not very clear. When we have some more examples to compare against, feel free to change/clarify. + 'title' => ts('Recurring Edit: Basic Example'), + 'tags' => ['preview', 'phpunit'], + ]; + } + + public function build(array &$example): void { + $msg = (new CRM_Contribute_WorkflowMessage_RecurringEdit()) + ->setContact(\Civi\Test::example('entity/Contact/Alex')) + ->setContributionRecur(\Civi\Test::example('entity/ContributionRecur/Euro5990/cancelled')); + $example['data'] = $this->toArray($msg); + + $example['asserts'] = [ + 'default' => [ + ['for' => 'subject', 'regex' => '/Recurring Contribution Update.*Alex/'], + ['for' => 'text', 'regex' => '/Recurring contribution is for € 5,990.99, every 2 year.s. for 24 installments/'], + ], + ]; + } + +} diff --git a/Civi/WorkflowMessage/WorkflowMessageExample.php b/Civi/WorkflowMessage/WorkflowMessageExample.php index 899c0a9165..40aead12b4 100644 --- a/Civi/WorkflowMessage/WorkflowMessageExample.php +++ b/Civi/WorkflowMessage/WorkflowMessageExample.php @@ -76,4 +76,11 @@ abstract class WorkflowMessageExample implements ExampleDataInterface { return $base; } + protected function toArray(\Civi\WorkflowMessage\WorkflowMessageInterface $wfMsg) { + return [ + 'workflow' => $this->wfName, + 'modelProps' => $wfMsg->export('modelProps'), + ]; + } + } -- 2.25.1