Skip to content

Commit 1ea87f8

Browse files
authored
FIX Ensure default_locale is en_US for unit testing supported modules (#11570)
1 parent 338f0d9 commit 1ea87f8

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

src/Dev/SapphireTest.php

+38-11
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@
3737
use SilverStripe\Security\Member;
3838
use SilverStripe\Security\Permission;
3939
use SilverStripe\Security\Security;
40+
use SilverStripe\SupportedModules\MetaData;
4041
use SilverStripe\View\SSViewer;
4142
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
4243
use Symfony\Component\Mailer\MailerInterface;
4344
use Symfony\Component\Mailer\Transport\NullTransport;
45+
use SilverStripe\Core\Path;
4446

4547
/**
4648
* Test case class for the Silverstripe framework.
@@ -280,21 +282,13 @@ protected function setUp(): void
280282
// Call state helpers
281283
static::$state->setUp($this);
282284

283-
// i18n needs to be set to the defaults or tests fail
284-
if (class_exists(i18n::class)) {
285-
i18n::set_locale(i18n::config()->uninherited('default_locale'));
286-
}
285+
$this->setI18nLocale();
287286

288287
// Set default timezone consistently to avoid NZ-specific dependencies
289288
date_default_timezone_set('UTC');
290289

291-
if (class_exists(Member::class)) {
292-
Member::set_password_validator(null);
293-
}
294-
295-
if (class_exists(Cookie::class)) {
296-
Cookie::config()->set('report_errors', false);
297-
}
290+
Member::set_password_validator(null);
291+
Cookie::config()->set('report_errors', false);
298292

299293
if (class_exists(RootURLController::class)) {
300294
RootURLController::reset();
@@ -1239,4 +1233,37 @@ protected function mockSleep(int $seconds): DBDatetime
12391233

12401234
return $now;
12411235
}
1236+
1237+
/**
1238+
* Sets the locale which unit-tests should be run in
1239+
*/
1240+
private function setI18nLocale(): void
1241+
{
1242+
$path = $this->getCurrentRelativePath();
1243+
$packagistName = '';
1244+
if (preg_match('#(^|/)vendor/([^/]+/[^/]+)/.+#', $path, $matches)) {
1245+
// Running unit tests of a module in the vendor folder
1246+
$packagistName = $matches[2];
1247+
} else {
1248+
// Running unit tests of a module or project in the root folder
1249+
$file = Path::join(BASE_PATH, 'composer.json');
1250+
if (file_exists($file)) {
1251+
$json = json_decode(file_get_contents($file), true);
1252+
$packagistName = $json['name'] ?? '';
1253+
}
1254+
}
1255+
$metaData = MetaData::getMetaDataByPackagistName($packagistName);
1256+
$isSupportedModule = !empty($metaData);
1257+
if ($isSupportedModule) {
1258+
// Anything that is in silverstripe/supported-module has unit tests in en_US
1259+
// Update the default_locale config in case in case any config at the project level or any
1260+
// installed optional module has set it to a non-en_US locale
1261+
i18n::config()->set('default_locale', 'en_US');
1262+
i18n::set_locale('en_US');
1263+
} else {
1264+
// Set the locale to the default_locale, which may have been set at project level to a
1265+
// non-en_US locale and the project unit tests expect that locale to be set
1266+
i18n::set_locale(i18n::config()->get('default_locale'));
1267+
}
1268+
}
12421269
}

0 commit comments

Comments
 (0)