From 8fa5b5b1d681a1ac378274486d99d4f1f7920913 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 16 Aug 2022 13:18:56 +1200 Subject: [PATCH] Dismbiguate init and flush --- CRM/Core/DAO/AllCoreTables.php | 20 ++++++++++++++----- .../CRM/Core/DAO/AllCoreTablesTest.php | 4 ++-- .../phpunit/CRM/Dedupe/BAO/RuleGroupTest.php | 6 +++--- .../Subscriber/DynamicFKAuthorizationTest.php | 4 ++-- .../Subscriber/WhitelistSubscriberTest.php | 2 +- tests/phpunit/api/v3/CustomApiTest.php | 4 ++-- 6 files changed, 25 insertions(+), 15 deletions(-) diff --git a/CRM/Core/DAO/AllCoreTables.php b/CRM/Core/DAO/AllCoreTables.php index bb1e808d6a..acf66cb837 100644 --- a/CRM/Core/DAO/AllCoreTables.php +++ b/CRM/Core/DAO/AllCoreTables.php @@ -24,13 +24,16 @@ class CRM_Core_DAO_AllCoreTables { /** * Initialise. * - * @param bool $fresh + * @param bool $fresh Deprecated parameter, use flush() to flush. */ - public static function init($fresh = FALSE) { - static $init = FALSE; - if ($init && !$fresh) { + public static function init(bool $fresh = FALSE): void { + if (!empty(Civi::$statics[__CLASS__]['initialised']) && !$fresh) { return; } + if ($fresh) { + CRM_Core_Error::deprecatedWarning('Use CRM_Core_DAO_AllCoreTables::flush()'); + } + Civi::$statics[__CLASS__]['initialised'] = TRUE; Civi::$statics[__CLASS__] = []; $file = preg_replace('/\.php$/', '.data.php', __FILE__); @@ -50,7 +53,14 @@ class CRM_Core_DAO_AllCoreTables { ); } - $init = TRUE; + Civi::$statics[__CLASS__]['initialised'] = TRUE; + } + + /** + * Flush class cache. + */ + public static function flush(): void { + Civi::$statics[__CLASS__]['initialised'] = FALSE; } /** diff --git a/tests/phpunit/CRM/Core/DAO/AllCoreTablesTest.php b/tests/phpunit/CRM/Core/DAO/AllCoreTablesTest.php index 612b2c3cf7..2f51e02728 100644 --- a/tests/phpunit/CRM/Core/DAO/AllCoreTablesTest.php +++ b/tests/phpunit/CRM/Core/DAO/AllCoreTablesTest.php @@ -26,7 +26,7 @@ class CRM_Core_DAO_AllCoreTablesTest extends CiviUnitTestCase { // 2. Now, let's hook into it... $this->hookClass->setHook('civicrm_entityTypes', [$this, '_hook_civicrm_entityTypes']); unset(Civi::$statics['CRM_Core_DAO_Email']); - CRM_Core_DAO_AllCoreTables::init(1); + CRM_Core_DAO_AllCoreTables::flush(); // 3. And see if the data has changed... $fields = CRM_Core_DAO_Email::fields(); @@ -50,7 +50,7 @@ class CRM_Core_DAO_AllCoreTablesTest extends CiviUnitTestCase { protected function tearDown(): void { CRM_Utils_Hook::singleton()->reset(); - CRM_Core_DAO_AllCoreTables::init(1); + CRM_Core_DAO_AllCoreTables::flush(); parent::tearDown(); } diff --git a/tests/phpunit/CRM/Dedupe/BAO/RuleGroupTest.php b/tests/phpunit/CRM/Dedupe/BAO/RuleGroupTest.php index 3c32268cea..2438468206 100644 --- a/tests/phpunit/CRM/Dedupe/BAO/RuleGroupTest.php +++ b/tests/phpunit/CRM/Dedupe/BAO/RuleGroupTest.php @@ -255,10 +255,10 @@ class CRM_Dedupe_BAO_RuleGroupTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - public function testHookDupeQueryMatch() { + public function testHookDupeQueryMatch(): void { $this->hookClass->setHook('civicrm_dupeQuery', [$this, 'hook_civicrm_dupeQuery']); - \CRM_Core_DAO_AllCoreTables::init(TRUE); + \CRM_Core_DAO_AllCoreTables::flush(); \CRM_Core_DAO_AllCoreTables::registerEntityType('TestEntity', 'CRM_Dedupe_DAO_TestEntity', 'civicrm_dedupe_test_table'); $this->apiKernel = \Civi::service('civi_api_kernel'); @@ -380,7 +380,7 @@ class CRM_Dedupe_BAO_RuleGroupTest extends CiviUnitTestCase { $this->assertCount(0, $foundDupes); CRM_Core_DAO::executeQuery('DROP TABLE civicrm_dedupe_test_table'); - \CRM_Core_DAO_AllCoreTables::init(TRUE); + \CRM_Core_DAO_AllCoreTables::flush(); } /** diff --git a/tests/phpunit/Civi/API/Subscriber/DynamicFKAuthorizationTest.php b/tests/phpunit/Civi/API/Subscriber/DynamicFKAuthorizationTest.php index c69c3e4fb2..85314e82ca 100644 --- a/tests/phpunit/Civi/API/Subscriber/DynamicFKAuthorizationTest.php +++ b/tests/phpunit/Civi/API/Subscriber/DynamicFKAuthorizationTest.php @@ -30,7 +30,7 @@ class DynamicFKAuthorizationTest extends \CiviUnitTestCase { protected function setUp(): void { parent::setUp(); - \CRM_Core_DAO_AllCoreTables::init(TRUE); + \CRM_Core_DAO_AllCoreTables::flush(); \CRM_Core_DAO_AllCoreTables::registerEntityType('FakeFile', 'CRM_Fake_DAO_FakeFile', 'fake_file'); $fileProvider = new StaticProvider( @@ -104,7 +104,7 @@ class DynamicFKAuthorizationTest extends \CiviUnitTestCase { protected function tearDown(): void { parent::tearDown(); - \CRM_Core_DAO_AllCoreTables::init(TRUE); + \CRM_Core_DAO_AllCoreTables::flush(); } /** diff --git a/tests/phpunit/Civi/API/Subscriber/WhitelistSubscriberTest.php b/tests/phpunit/Civi/API/Subscriber/WhitelistSubscriberTest.php index 768b8fe12a..0da270a689 100644 --- a/tests/phpunit/Civi/API/Subscriber/WhitelistSubscriberTest.php +++ b/tests/phpunit/Civi/API/Subscriber/WhitelistSubscriberTest.php @@ -349,7 +349,7 @@ class WhitelistSubscriberTest extends \CiviUnitTestCase { * @dataProvider restrictionCases */ public function testEach($apiRequest, $rules, $expectSuccess) { - \CRM_Core_DAO_AllCoreTables::init(TRUE); + \CRM_Core_DAO_AllCoreTables::flush(); $recs = $this->getFixtures(); diff --git a/tests/phpunit/api/v3/CustomApiTest.php b/tests/phpunit/api/v3/CustomApiTest.php index 1ff5095861..33c72bcb7c 100644 --- a/tests/phpunit/api/v3/CustomApiTest.php +++ b/tests/phpunit/api/v3/CustomApiTest.php @@ -69,10 +69,10 @@ class api_v3_CustomApiTest extends CiviUnitTestCase { /** * Install the custom api. */ - public function installApi() { + public function installApi(): void { require_once __DIR__ . '/custom_api/MailingProviderData.php'; $this->hookClass->setHook('civicrm_entityTypes', [$this, 'hookEntityTypes']); - CRM_Core_DAO_AllCoreTables::init(TRUE); + CRM_Core_DAO_AllCoreTables::flush(); CRM_Core_DAO::executeQuery( "CREATE TABLE IF NOT EXISTS `civicrm_mailing_provider_data` ( `contact_identifier` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '', -- 2.25.1