diff --git a/src/QuickInstall/Sandbox/BoardService.php b/src/QuickInstall/Sandbox/BoardService.php index 7100f262..62e275be 100644 --- a/src/QuickInstall/Sandbox/BoardService.php +++ b/src/QuickInstall/Sandbox/BoardService.php @@ -158,6 +158,30 @@ public function create(string $name, string $version = 'latest', string $db = 'm /** Returns the host's PHP-supported timezone identifier, falling back to UTC. */ protected function hostTimezone(): string + { + foreach ($this->hostTimezoneCandidates() as $candidate) + { + if ($candidate === '') + { + continue; + } + + try + { + // Match phpBB's own timezone validation semantics. + new \DateTimeZone($candidate); + return $candidate; + } + catch (\Exception $e) + { + } + } + + return 'UTC'; + } + + /** Collects timezone candidates in host preference order. */ + protected function hostTimezoneCandidates(): array { $candidates = []; $environment = getenv('TZ'); @@ -182,25 +206,7 @@ protected function hostTimezone(): string } $candidates[] = date_default_timezone_get(); - foreach ($candidates as $candidate) - { - if ($candidate === '') - { - continue; - } - - try - { - // Match phpBB's own timezone validation semantics. - new \DateTimeZone($candidate); - return $candidate; - } - catch (\Exception $e) - { - } - } - - return 'UTC'; + return $candidates; } private function backupBoardState(string $name): array diff --git a/tests/Unit/BoardServiceTest.php b/tests/Unit/BoardServiceTest.php index bea132c2..735cf8d1 100644 --- a/tests/Unit/BoardServiceTest.php +++ b/tests/Unit/BoardServiceTest.php @@ -40,15 +40,16 @@ public function testCreateWritesBoardAndRuntimeConfig(): void self::assertFileExists($project->composePath('demo')); } - public function testHostTimezoneUsesPhpCompatibleEnvironmentValue(): void + /** @dataProvider hostTimezoneEnvironmentProvider */ + public function testHostTimezoneUsesPhpCompatibleEnvironmentValue(string $environment, string $expected): void { $previous = getenv('TZ'); - putenv('TZ=US/Pacific'); + putenv('TZ=' . $environment); try { $project = $this->projectWithSource('3.3.19'); - self::assertSame('US/Pacific', (new HostTimezoneTestBoardService($project))->detectedHostTimezone()); + self::assertSame($expected, (new HostTimezoneTestBoardService($project))->detectedHostTimezone()); } finally { @@ -56,6 +57,46 @@ public function testHostTimezoneUsesPhpCompatibleEnvironmentValue(): void } } + public function hostTimezoneEnvironmentProvider(): array + { + return [ + 'canonical identifier' => ['America/Los_Angeles', 'America/Los_Angeles'], + 'colon-prefixed identifier' => [':America/Los_Angeles', 'America/Los_Angeles'], + ]; + } + + public function testHostTimezoneHandlesAliasAccordingToRuntimeSupport(): void + { + $expected = 'Europe/Paris'; + try + { + new \DateTimeZone('US/Pacific'); + $expected = 'US/Pacific'; + } + catch (\Exception $e) + { + } + + $service = new HostTimezoneTestBoardService($this->projectWithSource('3.3.19'), ['US/Pacific', 'Europe/Paris']); + self::assertSame($expected, $service->detectedHostTimezone()); + } + + /** @dataProvider hostTimezoneFallbackProvider */ + public function testHostTimezoneFallsBackFromInvalidCandidates(array $candidates, string $expected): void + { + $service = new HostTimezoneTestBoardService($this->projectWithSource('3.3.19'), $candidates); + self::assertSame($expected, $service->detectedHostTimezone()); + } + + public function hostTimezoneFallbackProvider(): array + { + return [ + 'next valid candidate' => [['', 'Invalid/Timezone', 'Europe/Paris', 'America/Los_Angeles'], 'Europe/Paris'], + 'no valid candidates' => [['', 'Invalid/Timezone'], 'UTC'], + 'no candidates' => [[], 'UTC'], + ]; + } + public function testCreateRejectsDuplicateWithoutReplace(): void { $project = $this->projectWithSource('3.3.19'); @@ -266,10 +307,23 @@ private function projectWithSource(string $version, ?string $osFamily = null): P class HostTimezoneTestBoardService extends BoardService { + private ?array $timezoneCandidates; + + public function __construct(Project $project, ?array $timezoneCandidates = null) + { + parent::__construct($project); + $this->timezoneCandidates = $timezoneCandidates; + } + public function detectedHostTimezone(): string { return $this->hostTimezone(); } + + protected function hostTimezoneCandidates(): array + { + return $this->timezoneCandidates ?? parent::hostTimezoneCandidates(); + } } class TestBoardService extends BoardService