Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions src/QuickInstall/Sandbox/BoardService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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
Expand Down
60 changes: 57 additions & 3 deletions tests/Unit/BoardServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,63 @@ 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
{
$previous === false ? putenv('TZ') : putenv('TZ=' . $previous);
}
}

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');
Expand Down Expand Up @@ -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
Expand Down
Loading