-
-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25548 from eileenmcnaughton/559_token
(dev/core#4109) Fix tokens like `{contact.email_primary.email}`
- Loading branch information
Showing
3 changed files
with
62 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -272,6 +272,24 @@ public function getUnadvertisedTokens(): array { | |
]; | ||
} | ||
|
||
/** | ||
* Test the standard new location token format - which matches apiv4 return properties. | ||
* | ||
* @throws \CRM_Core_Exception | ||
*/ | ||
public function testLocationTokens(): void { | ||
$contactID = $this->individualCreate(['email' => '[email protected]']); | ||
Address::create()->setValues([ | ||
'contact_id' => $contactID, | ||
'is_primary' => TRUE, | ||
'street_address' => 'Heartbreak Hotel', | ||
'supplemental_address_1' => 'Lonely Street', | ||
])->execute(); | ||
$text = '{contact.first_name} {contact.email_primary.email} {contact.address_primary.street_address}'; | ||
$text = $this->renderText(['contactId' => $contactID], $text); | ||
$this->assertEquals('Anthony [email protected] Heartbreak Hotel', $text); | ||
} | ||
|
||
/** | ||
* Test tokens in 2 ways to ensure consistent handling. | ||
* | ||
|
@@ -563,6 +581,8 @@ protected function getMembershipID(): int { | |
/** | ||
* Get expected output from token parsing. | ||
* | ||
* @param int|null $participantCreatedID | ||
* | ||
* @return string | ||
*/ | ||
protected function getExpectedParticipantTokenOutput(int $participantCreatedID = NULL): string { | ||
|
@@ -811,13 +831,8 @@ public function testEventTokenConsistency(): void { | |
$mut = new CiviMailUtils($this); | ||
$this->setupParticipantScheduledReminder(); | ||
|
||
$tokens = CRM_Core_SelectValues::eventTokens(); | ||
$this->assertEquals(array_merge($this->getEventTokens()), $tokens); | ||
$tokenProcessor = new TokenProcessor(\Civi::dispatcher(), [ | ||
'controller' => __CLASS__, | ||
'smarty' => FALSE, | ||
'schema' => ['eventId'], | ||
]); | ||
$tokens = array_merge($this->getEventTokens()); | ||
$tokenProcessor = $this->getTokenProcessor(['schema' => ['eventId']]); | ||
$this->assertEquals(array_merge($tokens, $this->getDomainTokens()), $tokenProcessor->listTokens()); | ||
|
||
$expectedEventString = $this->getExpectedEventTokenOutput(); | ||
|
@@ -1026,4 +1041,36 @@ public function testEscaping() { | |
$this->assertEquals($expected, $rendered); | ||
} | ||
|
||
/** | ||
* @param array $override | ||
* | ||
* @return \Civi\Token\TokenProcessor | ||
*/ | ||
protected function getTokenProcessor(array $override): TokenProcessor { | ||
return new TokenProcessor(\Civi::dispatcher(), array_merge([ | ||
'controller' => __CLASS__, | ||
], $override)); | ||
} | ||
|
||
/** | ||
* Render the text via the token processor. | ||
* | ||
* @param array $rowContext | ||
* @param string $text | ||
* @param array $context | ||
* | ||
* @return string | ||
*/ | ||
protected function renderText(array $rowContext, string $text, array $context = []): string { | ||
$context['schema'] = $context['schema'] ?? []; | ||
foreach (array_keys($rowContext) as $key) { | ||
$context['schema'][] = $key; | ||
} | ||
$tokenProcessor = $this->getTokenProcessor($context); | ||
$tokenProcessor->addRow($rowContext); | ||
$tokenProcessor->addMessage('text', $text, 'text/html'); | ||
$tokenProcessor->evaluate(); | ||
return $tokenProcessor->getRow(0)->render('text'); | ||
} | ||
|
||
} |