From 15ed06b415483bbd9bfe3e561fd0c9324d9952a4 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 14 Oct 2021 21:21:04 +1300 Subject: [PATCH] Fix hookTokens to be clearable outside the class Tests can't unset the proerty in the class easily but just using the static makes it easier --- CRM/Contact/Tokens.php | 25 +++++++------------------ CRM/Utils/Hook/UnitTests.php | 3 +++ 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/CRM/Contact/Tokens.php b/CRM/Contact/Tokens.php index 03c4d37db108..7df4720e9368 100644 --- a/CRM/Contact/Tokens.php +++ b/CRM/Contact/Tokens.php @@ -32,13 +32,6 @@ protected function getApiEntityName(): string { return 'Contact'; } - /** - * Tokens defined by the legacy hook. - * - * @var array - */ - protected $hookTokens; - /** * @inheritDoc */ @@ -249,7 +242,7 @@ protected function getRelatedEntityTokenMetadata(): array { */ public function evaluateLegacyHookTokens(TokenValueEvent $e): void { $messageTokens = $e->getTokenProcessor()->getMessageTokens(); - if (!array_intersect(array_keys($this->getHookTokens()), array_keys($messageTokens))) { + if (empty($messageTokens) || !array_intersect(array_keys($this->getHookTokens()), array_keys($messageTokens))) { return; } @@ -694,17 +687,13 @@ protected function getBespokeTokens(): array { * @return array */ protected function getHookTokens(): array { - if ($this->hookTokens === NULL) { - if (isset(Civi::$statics[__CLASS__]['hook_tokens'])) { - $this->hookTokens = Civi::$statics[__CLASS__]['hook_tokens']; - } - else { - $this->hookTokens = []; - \CRM_Utils_Hook::tokens($this->hookTokens); - Civi::$statics[__CLASS__]['hook_tokens'] = $this->hookTokens; - } + if (isset(Civi::$statics[__CLASS__]['hook_tokens'])) { + return Civi::$statics[__CLASS__]['hook_tokens']; } - return $this->hookTokens; + $tokens = []; + \CRM_Utils_Hook::tokens($tokens); + Civi::$statics[__CLASS__]['hook_tokens'] = $tokens; + return $tokens; } } diff --git a/CRM/Utils/Hook/UnitTests.php b/CRM/Utils/Hook/UnitTests.php index 7b07c1b5ce46..71babb86f881 100644 --- a/CRM/Utils/Hook/UnitTests.php +++ b/CRM/Utils/Hook/UnitTests.php @@ -55,6 +55,9 @@ public function setMock($mockObject): void { */ public function setHook(string $hook, $callable): void { $this->adhocHooks[$hook] = $callable; + if (strpos($hook, 'token') !== FALSE) { + unset(Civi::$statics['CRM_Contact_Tokens']['hook_tokens']); + } } /**