From 4d3743714f525b1a7310aaf04beed4eaee25be7c Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 1 Mar 2023 01:10:40 -0800 Subject: [PATCH] (REF) MessageTemplateTest - Extract method useFrozenTime() --- .../CRM/Core/BAO/MessageTemplateTest.php | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php index 095dd65b1b..4baada93f6 100644 --- a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php +++ b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php @@ -425,8 +425,8 @@ London, 90210 */ public function testContactTokens(): void { // Freeze the time at the start of the test, so checksums don't suffer from second rollovers. - putenv('TIME_FUNC=frozen'); - CRM_Utils_Time::setTime(date('Y-m-d H:i:s')); + $restoreTime = $this->useFrozenTime(); + $this->hookClass->setHook('civicrm_tokenValues', [$this, 'hookTokenValues']); $this->hookClass->setHook('civicrm_tokens', [$this, 'hookTokens']); @@ -483,10 +483,6 @@ emo $this->assertEquals($expected_parts[0], $returned_parts[0]); $this->assertApproxEquals($expected_parts[1], $returned_parts[1], 2); $this->assertEquals($expected_parts[2], $returned_parts[2]); - - // reset time - putenv('TIME_FUNC'); - CRM_Utils_Time::resetTime(); } /** @@ -868,6 +864,21 @@ primary_phone:123-456 ', $messageHtml); } + /** + * Temporarily freeze time, as perceived through `CRM_Utils_Time`. + * + * @return \CRM_Utils_AutoClean + */ + protected function useFrozenTime(): CRM_Utils_AutoClean { + $oldTimeFunc = getenv('TIME_FUNC'); + putenv('TIME_FUNC=frozen'); + CRM_Utils_Time::setTime(date('Y-m-d H:i:s')); + return CRM_Utils_AutoClean::with(function () use ($oldTimeFunc) { + putenv($oldTimeFunc === NULL ? 'TIME_FUNC' : "TIME_FUNC=$oldTimeFunc"); + CRM_Utils_Time::resetTime(); + }); + } + /** * @param array $tokenData * -- 2.25.1