From: colemanw Date: Tue, 23 Jan 2024 15:16:01 +0000 (-0500) Subject: Fix metadata cache clear event X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=4fa4edcc56cc3e0f3e2031f550745657f3cb3040;p=civicrm-core.git Fix metadata cache clear event --- diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index ce8572fc6c..ab8521b184 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -188,8 +188,7 @@ class Container { foreach ($basicCaches as $cacheSvc => $cacheGrp) { $definitionParams = [ 'name' => $cacheGrp . (in_array($cacheGrp, $verSuffixCaches) ? $verSuffix : ''), - // FIXME: Uncommenting below causes test failure - // 'service' => $cacheSvc, + 'service' => $cacheSvc, 'type' => ['*memory*', 'SqlGroup', 'ArrayCache'], ]; // For Caches that we don't really care about the ttl for and/or maybe accessed diff --git a/Civi/Core/MetadataFlush.php b/Civi/Core/MetadataFlush.php index 2e700be6b7..15af6334fa 100644 --- a/Civi/Core/MetadataFlush.php +++ b/Civi/Core/MetadataFlush.php @@ -31,7 +31,13 @@ class MetadataFlush extends Service\AutoSubscriber { * as it's being deleted. */ public static function onClearMetadata(): void { - \Civi::resources()->resetCacheCode(); + // This seems to cause problems during unit test setup + if (CIVICRM_UF === 'UnitTests') { + return; + } + if (\Civi\Core\Container::singleton()->has('resources')) { + \Civi::resources()->resetCacheCode(); + } } } diff --git a/tests/phpunit/Civi/Test/ExampleSubscriberTest.php b/tests/phpunit/Civi/Test/ExampleSubscriberTest.php index fac648ee62..adc63c89fc 100644 --- a/tests/phpunit/Civi/Test/ExampleSubscriberTest.php +++ b/tests/phpunit/Civi/Test/ExampleSubscriberTest.php @@ -50,6 +50,7 @@ class ExampleSubscriberTest extends \PHPUnit\Framework\TestCase implements Headl 'civi.api.prepare' => ['myCiviApiPrepare', 1234], 'hook_civicrm_alterContent' => ['myAlterContentObject', -7000], '&hook_civicrm_alterContent' => ['myAlterContentParams', -8000], + 'civi.cache.metadata.clear' => 'myCacheClear', ]; } @@ -69,6 +70,10 @@ class ExampleSubscriberTest extends \PHPUnit\Framework\TestCase implements Headl $content .= ' ' . __FUNCTION__; } + public function myCacheClear(GenericHookEvent $event): void { + $this->tracker['civi.cache.metadata.clear'][__FUNCTION__] = TRUE; + } + public function testPageOutput(): void { ob_start(); $p = new Main(); @@ -86,4 +91,10 @@ class ExampleSubscriberTest extends \PHPUnit\Framework\TestCase implements Headl $this->assertEquals(['myCiviApiPrepare' => TRUE], $this->tracker['civi.api.prepare']); } + public function testCacheClearEvent(): void { + $this->tracker['civi.cache.metadata.clear'] = NULL; + \Civi::cache('metadata')->clear(); + $this->assertEquals(['myCacheClear' => TRUE], $this->tracker['civi.cache.metadata.clear']); + } + }