From 1ea87f88ff632f2c2b335a233efcfdbe8665ffde Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Fri, 24 Jan 2025 09:16:49 +1300 Subject: [PATCH] FIX Ensure default_locale is en_US for unit testing supported modules (#11570) --- src/Dev/SapphireTest.php | 49 +++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/src/Dev/SapphireTest.php b/src/Dev/SapphireTest.php index b54a58e5554..ab70eab6944 100644 --- a/src/Dev/SapphireTest.php +++ b/src/Dev/SapphireTest.php @@ -37,10 +37,12 @@ use SilverStripe\Security\Member; use SilverStripe\Security\Permission; use SilverStripe\Security\Security; +use SilverStripe\SupportedModules\MetaData; use SilverStripe\View\SSViewer; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Mailer\MailerInterface; use Symfony\Component\Mailer\Transport\NullTransport; +use SilverStripe\Core\Path; /** * Test case class for the Silverstripe framework. @@ -280,21 +282,13 @@ protected function setUp(): void // Call state helpers static::$state->setUp($this); - // i18n needs to be set to the defaults or tests fail - if (class_exists(i18n::class)) { - i18n::set_locale(i18n::config()->uninherited('default_locale')); - } + $this->setI18nLocale(); // Set default timezone consistently to avoid NZ-specific dependencies date_default_timezone_set('UTC'); - if (class_exists(Member::class)) { - Member::set_password_validator(null); - } - - if (class_exists(Cookie::class)) { - Cookie::config()->set('report_errors', false); - } + Member::set_password_validator(null); + Cookie::config()->set('report_errors', false); if (class_exists(RootURLController::class)) { RootURLController::reset(); @@ -1239,4 +1233,37 @@ protected function mockSleep(int $seconds): DBDatetime return $now; } + + /** + * Sets the locale which unit-tests should be run in + */ + private function setI18nLocale(): void + { + $path = $this->getCurrentRelativePath(); + $packagistName = ''; + if (preg_match('#(^|/)vendor/([^/]+/[^/]+)/.+#', $path, $matches)) { + // Running unit tests of a module in the vendor folder + $packagistName = $matches[2]; + } else { + // Running unit tests of a module or project in the root folder + $file = Path::join(BASE_PATH, 'composer.json'); + if (file_exists($file)) { + $json = json_decode(file_get_contents($file), true); + $packagistName = $json['name'] ?? ''; + } + } + $metaData = MetaData::getMetaDataByPackagistName($packagistName); + $isSupportedModule = !empty($metaData); + if ($isSupportedModule) { + // Anything that is in silverstripe/supported-module has unit tests in en_US + // Update the default_locale config in case in case any config at the project level or any + // installed optional module has set it to a non-en_US locale + i18n::config()->set('default_locale', 'en_US'); + i18n::set_locale('en_US'); + } else { + // Set the locale to the default_locale, which may have been set at project level to a + // non-en_US locale and the project unit tests expect that locale to be set + i18n::set_locale(i18n::config()->get('default_locale')); + } + } }