Skip to content

Commit

Permalink
RecurringEdit - Add example. Include test assertions. Fix undeclared/…
Browse files Browse the repository at this point in the history
…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`.
  • Loading branch information
totten committed Sep 15, 2021
1 parent b209717 commit 40df2ff
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
33 changes: 33 additions & 0 deletions CRM/Contribute/WorkflowMessage/RecurringEdit.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

/**
* @support template-only
* @method array getContact()
* @method $this setContact(array $contact)
* @method array getContributionRecur()
Expand Down Expand Up @@ -31,11 +32,43 @@ class CRM_Contribute_WorkflowMessage_RecurringEdit extends Civi\WorkflowMessage\
*/
public $contributionRecur;

/**
* @var int
* @scope tokenContext as contribution_recurId
*/
public $contributionRecurId;

/**
* Smarty template historically defined a property 'receipt_from_email'.
* (Note the asymmetric lack of 'receipt_from_name'.)
*
* TODO: This should probably be deprecated/converted/reconciled with `$this->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';
$export['smartyTokenAlias']['recur_frequency_unit'] = 'contribution_recur.frequency_unit';
$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'];
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

class CRM_Contribute_WorkflowMessage_RecurringEdit_BasicEditExample extends \Civi\WorkflowMessage\WorkflowMessageExample {

public function getExamples(): iterable {
yield [
'name' => "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('workflow/generic/Alex')['modelProps']['contact'])
->setContributionRecur(\Civi\Test::example('workflow/generic/ContributionRecurExample')['modelProps']['contributionRecur']);
// FIXME: We should have a better name for entity-examples. It's kind a weird that the two examples above
// are named "workflow/*" and use "modelProps".
$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/'],
],
];
}

protected function toArray(\Civi\WorkflowMessage\WorkflowMessageInterface $wfMsg) {
return [
'workflow' => $this->wfName,
'modelProps' => $wfMsg->export('modelProps'),
];
}

}

0 comments on commit 40df2ff

Please sign in to comment.