Skip to content

Commit

Permalink
EntityTokens - Allow using mocked data
Browse files Browse the repository at this point in the history
Before: EntityTokens reads `$tokenContext['contributionId']` and fetches the corresponding `civicrm_contribution`. The
data must exist in that record.

After: EntityTokens reads both `$tokenContext['contributionId']` and `$tokenContext['contribution']`. Data in
`$tokenContext['contribution']` takes precedence, rendering the content of `civicrm_contribution` mute.
  • Loading branch information
totten committed Sep 15, 2021
1 parent 9e6fa02 commit b209717
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CRM/Core/EntityTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ public function getPseudoValue(string $realField, string $pseudoKey, $fieldValue
* @return string|int
*/
protected function getFieldValue(TokenRow $row, string $field) {
$entityName = $this->getEntityName();
if (isset($row->context[$entityName][$field])) {
return $row->context[$entityName][$field];
}

$actionSearchResult = $row->context['actionSearchResult'];
$aliasedField = $this->getEntityAlias() . $field;
if (isset($actionSearchResult->{$aliasedField})) {
Expand Down
80 changes: 80 additions & 0 deletions tests/phpunit/Civi/Token/TokenProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,86 @@ public function testHookTokenExtraChar() {
$this->assertEquals(2, $loops);
}

/**
* Process a message using mocked data.
*/
public function testMockData_Contribution() {
$this->dispatcher->addSubscriber(new \CRM_Contribute_Tokens());

$p = new TokenProcessor($this->dispatcher, [
'controller' => __CLASS__,
'schema' => ['contributionId'],
]);
$p->addMessage('example', 'Invoice #{contribution.invoice_id}!', 'text/plain');
$p->addRow([
'contributionId' => 111,
'contribution' => [
'id' => 111,
'receive_date' => '2012-01-02',
'invoice_id' => 11111,
],
]);
$p->addRow([
'contributionId' => 222,
'contribution' => [
'id' => 111,
'receive_date' => '2012-01-02',
'invoice_id' => 22222,
],
]);
$p->evaluate();

$outputs = [];
foreach ($p->getRows() as $row) {
$outputs[] = $row->render('example');
}
$this->assertEquals('Invoice #11111!', $outputs[0]);
$this->assertEquals('Invoice #22222!', $outputs[1]);
}

/**
* Process a message using mocked data, accessed through a Smarty alias.
*/
public function testMockData_SmartyAlias_Contribution() {
$this->dispatcher->addSubscriber(new TokenCompatSubscriber());
$this->dispatcher->addSubscriber(new \CRM_Contribute_Tokens());

$p = new TokenProcessor($this->dispatcher, [
'controller' => __CLASS__,
'schema' => ['contributionId'],
'smarty' => TRUE,
'smartyTokenAlias' => [
'theInvoiceId' => 'contribution.invoice_id',
],
]);
$p->addMessage('example', 'Invoice #{$theInvoiceId}!', 'text/plain');
$p->addRow([
'contributionId' => 333,
'contribution' => [
'id' => 333,
'receive_date' => '2012-01-02',
'invoice_id' => 33333,
],
]);
$p->addRow([
'contributionId' => 444,
'contribution' => [
'id' => 444,
'receive_date' => '2012-01-02',
'invoice_id' => 44444,
],
]);
$p->evaluate();

$outputs = [];
foreach ($p->getRows() as $row) {
$outputs[] = $row->render('example');
}
$this->assertEquals('Invoice #33333!', $outputs[0]);
$this->assertEquals('Invoice #44444!', $outputs[1]);

}

/**
* This defines a compatibility mechanism wherein an old Smarty expression can
* be evaluated based on a newer token expression.
Expand Down

0 comments on commit b209717

Please sign in to comment.