|
37 | 37 | use SilverStripe\Security\Member;
|
38 | 38 | use SilverStripe\Security\Permission;
|
39 | 39 | use SilverStripe\Security\Security;
|
| 40 | +use SilverStripe\SupportedModules\MetaData; |
40 | 41 | use SilverStripe\View\SSViewer;
|
41 | 42 | use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
42 | 43 | use Symfony\Component\Mailer\MailerInterface;
|
43 | 44 | use Symfony\Component\Mailer\Transport\NullTransport;
|
| 45 | +use SilverStripe\Core\Path; |
44 | 46 |
|
45 | 47 | /**
|
46 | 48 | * Test case class for the Silverstripe framework.
|
@@ -280,21 +282,13 @@ protected function setUp(): void
|
280 | 282 | // Call state helpers
|
281 | 283 | static::$state->setUp($this);
|
282 | 284 |
|
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(); |
287 | 286 |
|
288 | 287 | // Set default timezone consistently to avoid NZ-specific dependencies
|
289 | 288 | date_default_timezone_set('UTC');
|
290 | 289 |
|
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); |
298 | 292 |
|
299 | 293 | if (class_exists(RootURLController::class)) {
|
300 | 294 | RootURLController::reset();
|
@@ -1239,4 +1233,37 @@ protected function mockSleep(int $seconds): DBDatetime
|
1239 | 1233 |
|
1240 | 1234 | return $now;
|
1241 | 1235 | }
|
| 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 | + } |
1242 | 1269 | }
|
0 commit comments