diff --git a/src/UpdateScripts/UpdateGlobalVariables.php b/src/UpdateScripts/UpdateGlobalVariables.php index 414261c2bc5..8fe0755f904 100644 --- a/src/UpdateScripts/UpdateGlobalVariables.php +++ b/src/UpdateScripts/UpdateGlobalVariables.php @@ -76,7 +76,7 @@ private function migrateFileStructure(): void $globalSet->save(); - File::put($variablesPath, YAML::dump($data)); + $globalSet->inDefaultSite()->data($data)->saveQuietly(); }); } } diff --git a/tests/UpdateScripts/UpdateGlobalVariablesTest.php b/tests/UpdateScripts/UpdateGlobalVariablesTest.php index 2cc3fc6bf21..9d74711bc03 100644 --- a/tests/UpdateScripts/UpdateGlobalVariablesTest.php +++ b/tests/UpdateScripts/UpdateGlobalVariablesTest.php @@ -4,6 +4,7 @@ use Illuminate\Support\Facades\File; use PHPUnit\Framework\Attributes\Test; +use Statamic\Facades\GlobalSet; use Statamic\Facades\YAML; use Statamic\UpdateScripts\UpdateGlobalVariables; use Tests\PreventSavingStacheItemsToDisk; @@ -79,6 +80,32 @@ public function it_migrates_global_variables_with_empty_data_in_a_single_site_in unlink($this->globalsPath.'/en/test.yaml'); } + #[Test] + public function migrated_variables_survive_being_saved_later_in_the_same_process() + { + File::put($this->globalsPath.'/test.yaml', Yaml::dump([ + 'title' => 'Test', + 'data' => [ + 'foo' => 'Bar', + ], + ])); + + $this->runUpdateScript(UpdateGlobalVariables::class); + + // Simulates an addon update script saving variables after the migration + $variables = GlobalSet::findByHandle('test')->inDefaultSite(); + $variables->set('baz', 'Qux'); + $variables->save(); + + $this->assertEquals( + ['foo' => 'Bar', 'baz' => 'Qux'], + YAML::parse(File::get($this->globalsPath.'/en/test.yaml')) + ); + + unlink($this->globalsPath.'/test.yaml'); + unlink($this->globalsPath.'/en/test.yaml'); + } + #[Test] public function it_builds_the_sites_array_in_a_multi_site_install() {