From 3c88b532e55092256d226367bdb34a2a0f5d2719 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 1 Dec 2023 15:39:15 +1300 Subject: [PATCH] Fix for intermittent test fail on checksum time --- .../phpunit/CRM/Core/BAO/CustomFieldTest.php | 3 ++- .../CRM/Core/BAO/MessageTemplateTest.php | 23 ++----------------- tests/phpunit/CiviTest/CiviUnitTestCase.php | 19 +++++++++++++++ 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php index ca77e5441e..393f5cb6dc 100644 --- a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php +++ b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php @@ -982,6 +982,7 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { * Check that outputting the display value for a file field with No description doesn't generate error */ public function testFileDisplayValueNoDescription(): void { + $this->useFrozenTime(); $customGroup = $this->customGroupCreate([ 'extends' => 'Individual', 'title' => 'Test Contact File Custom Group', @@ -996,7 +997,7 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { $file = $this->callAPISuccess('File', 'create', [ 'uri' => $filePath, ]); - $individual = $this->individualCreate(['custom_' . $fileField['id'] => $file['id']]); + $this->individualCreate(['custom_' . $fileField['id'] => $file['id']]); $expectedDisplayValue = CRM_Core_BAO_File::paperIconAttachment('*', $file['id'])[$file['id']]; $this->assertEquals($expectedDisplayValue, CRM_Core_BAO_CustomField::displayValue($file['id'], $fileField['id'])); } diff --git a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php index 8b51e6f0eb..39c34d6c3c 100644 --- a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php +++ b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php @@ -516,7 +516,7 @@ London, 90210 */ public function testContactTokens(): void { // Freeze the time at the start of the test, so checksums don't suffer from second rollovers. - $restoreTime = $this->useFrozenTime(); + $this->useFrozenTime(); $this->hookClass->setHook('civicrm_tokenValues', [$this, 'hookTokenValues']); $this->hookClass->setHook('civicrm_tokens', [$this, 'hookTokens']); @@ -574,7 +574,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]); - $restoreTime->cleanup(); } /** @@ -585,10 +584,7 @@ emo */ public function testTokensIndividually(): void { // Freeze the time at the start of the test, so checksums don't suffer from second rollovers. - // This variable releases the time on destruct so needs to be assigned for the - // duration of the test. - /** @noinspection PhpUnusedLocalVariableInspection */ - $restoreTime = $this->useFrozenTime(); + $this->useFrozenTime(); $this->hookClass->setHook('civicrm_tokenValues', [$this, 'hookTokenValues']); $this->hookClass->setHook('civicrm_tokens', [$this, 'hookTokens']); @@ -1010,21 +1006,6 @@ 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 * diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 7c14de40f0..fd65764ef6 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -201,6 +201,11 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { */ protected $formController; + /** + * @var \CRM_Utils_AutoClean + */ + private $frozenTime; + /** * Constructor. * @@ -457,6 +462,7 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { protected function tearDown(): void { $this->_apiversion = 3; $this->resetLabels(); + $this->frozenTime = NULL; error_reporting(E_ALL & ~E_NOTICE); $this->resetHooks(); @@ -918,6 +924,19 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { return $result['values'][$result['id']]; } + /** + * Temporarily freeze time, as perceived through `CRM_Utils_Time`. + */ + protected function useFrozenTime(): void { + $oldTimeFunc = getenv('TIME_FUNC'); + putenv('TIME_FUNC=frozen'); + CRM_Utils_Time::setTime(date('Y-m-d H:i:s')); + $this->frozenTime = CRM_Utils_AutoClean::with(function () use ($oldTimeFunc) { + putenv($oldTimeFunc === NULL ? 'TIME_FUNC' : "TIME_FUNC=$oldTimeFunc"); + CRM_Utils_Time::resetTime(); + }); + } + /** * Delete Tag. * -- 2.25.1