diff --git a/src/UpdateScripts/UpdateGlobalVariables.php b/src/UpdateScripts/UpdateGlobalVariables.php index 8fe0755f904..6fb734012e2 100644 --- a/src/UpdateScripts/UpdateGlobalVariables.php +++ b/src/UpdateScripts/UpdateGlobalVariables.php @@ -38,15 +38,19 @@ public function update() */ private function buildSitesArray(): void { - GlobalSet::all()->each(function ($globalSet) { + $siteOrder = Site::all()->keys()->flip(); + + GlobalSet::all()->each(function ($globalSet) use ($siteOrder) { $variables = GlobalVariables::whereSet($globalSet->handle()); - $sites = $variables->mapWithKeys(function ($variable) { - $contents = YAML::file($variable->path())->parse(); - $origin = Arr::get($contents, 'origin'); + $sites = $variables + ->sortBy(fn ($variable) => $siteOrder->get($variable->locale(), $siteOrder->count())) + ->mapWithKeys(function ($variable) { + $contents = YAML::file($variable->path())->parse(); + $origin = Arr::get($contents, 'origin'); - return [$variable->locale() => $origin]; - }); + return [$variable->locale() => $origin]; + }); $globalSet->sites($sites)->save(); diff --git a/tests/UpdateScripts/UpdateGlobalVariablesTest.php b/tests/UpdateScripts/UpdateGlobalVariablesTest.php index 9d74711bc03..fcde4ecf478 100644 --- a/tests/UpdateScripts/UpdateGlobalVariablesTest.php +++ b/tests/UpdateScripts/UpdateGlobalVariablesTest.php @@ -120,9 +120,12 @@ public function it_builds_the_sites_array_in_a_multi_site_install() File::ensureDirectoryExists($this->globalsPath.'/de'); File::put($this->globalsPath.'/test.yaml', Yaml::dump(['title' => 'Test'])); - File::put($this->globalsPath.'/en/test.yaml', Yaml::dump(['foo' => 'Bar', 'baz' => 'Qux'])); - File::put($this->globalsPath.'/fr/test.yaml', Yaml::dump(['origin' => 'en', 'foo' => 'Bar'])); + + // Written out of order on purpose. The Stache indexes variables by modification + // time, but the sites array should follow the order of the sites config. File::put($this->globalsPath.'/de/test.yaml', Yaml::dump(['origin' => 'fr'])); + File::put($this->globalsPath.'/fr/test.yaml', Yaml::dump(['origin' => 'en', 'foo' => 'Bar'])); + File::put($this->globalsPath.'/en/test.yaml', Yaml::dump(['foo' => 'Bar', 'baz' => 'Qux'])); $this->runUpdateScript(UpdateGlobalVariables::class); @@ -130,9 +133,9 @@ public function it_builds_the_sites_array_in_a_multi_site_install() $expected = <<<'YAML' title: Test sites: - de: fr en: null fr: en + de: fr YAML;