diff --git a/src/Stache/Stores/GlobalVariablesStore.php b/src/Stache/Stores/GlobalVariablesStore.php index ad82da04485..a204d851546 100644 --- a/src/Stache/Stores/GlobalVariablesStore.php +++ b/src/Stache/Stores/GlobalVariablesStore.php @@ -42,7 +42,7 @@ protected function makeVariablesFromFile($handle, $path, $data) { $variables = app(Variables::class) ->initialPath($path) - ->data(Arr::except($data, 'origin')); + ->data(Arr::except($data ?? [], 'origin')); $handle = explode('/', $handle); diff --git a/src/UpdateScripts/UpdateGlobalVariables.php b/src/UpdateScripts/UpdateGlobalVariables.php index d317dfb8839..414261c2bc5 100644 --- a/src/UpdateScripts/UpdateGlobalVariables.php +++ b/src/UpdateScripts/UpdateGlobalVariables.php @@ -51,7 +51,7 @@ private function buildSitesArray(): void $globalSet->sites($sites)->save(); $variables->each(function ($variable) { - $data = YAML::file($variable->path())->parse(); + $data = YAML::file($variable->path())->parse() ?? []; File::put($variable->path(), YAML::dump(Arr::except($data, 'origin'))); }); @@ -72,7 +72,7 @@ private function migrateFileStructure(): void } $contents = YAML::file($globalSet->path())->parse(); - $data = Arr::get($contents, 'data', []); + $data = Arr::get($contents, 'data') ?? []; $globalSet->save(); diff --git a/tests/Stache/Stores/GlobalVariablesStoreTest.php b/tests/Stache/Stores/GlobalVariablesStoreTest.php index e957de21ee0..39dda56d4ea 100644 --- a/tests/Stache/Stores/GlobalVariablesStoreTest.php +++ b/tests/Stache/Stores/GlobalVariablesStoreTest.php @@ -70,6 +70,17 @@ public function it_makes_global_variable_instances_from_files() $this->assertEquals('example', $item->handle()); } + #[Test] + public function it_makes_global_variable_instances_from_files_with_null_contents() + { + $item = $this->store->makeItemFromFile(Path::tidy($this->tempDir.'/en/example.yaml'), 'null'); + + $this->assertInstanceOf(Variables::class, $item); + $this->assertEquals('example::en', $item->id()); + $this->assertEquals('example', $item->handle()); + $this->assertEquals([], $item->data()->all()); + } + #[Test] public function it_uses_the_id_as_the_item_key() { diff --git a/tests/UpdateScripts/UpdateGlobalVariablesTest.php b/tests/UpdateScripts/UpdateGlobalVariablesTest.php index 4230b514b08..2cc3fc6bf21 100644 --- a/tests/UpdateScripts/UpdateGlobalVariablesTest.php +++ b/tests/UpdateScripts/UpdateGlobalVariablesTest.php @@ -59,6 +59,26 @@ public function it_migrates_global_variables_in_a_single_site_install() unlink($this->globalsPath.'/en/test.yaml'); } + #[Test] + public function it_migrates_global_variables_with_empty_data_in_a_single_site_install() + { + File::put($this->globalsPath.'/test.yaml', "title: Test\ndata:"); + + $this->runUpdateScript(UpdateGlobalVariables::class); + + $expected = <<<'YAML' +title: Test + +YAML; + $this->assertEquals($expected, File::get($this->globalsPath.'/test.yaml')); + + // Empty data should produce an empty array, not null + $this->assertEquals([], 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() { @@ -96,4 +116,25 @@ public function it_builds_the_sites_array_in_a_multi_site_install() $this->assertEquals(['foo' => 'Bar'], YAML::parse(File::get($this->globalsPath.'/fr/test.yaml'))); $this->assertEquals([], YAML::parse(File::get($this->globalsPath.'/de/test.yaml'))); } + + #[Test] + public function it_handles_null_content_in_global_variables_in_a_multi_site_install() + { + $this->setSites([ + 'en' => ['url' => '/', 'locale' => 'en_US', 'name' => 'English'], + 'fr' => ['url' => '/', 'locale' => 'fr_FR', 'name' => 'French'], + ]); + + File::ensureDirectoryExists($this->globalsPath.'/en'); + File::ensureDirectoryExists($this->globalsPath.'/fr'); + + File::put($this->globalsPath.'/test.yaml', Yaml::dump(['title' => 'Test'])); + File::put($this->globalsPath.'/en/test.yaml', Yaml::dump(['foo' => 'Bar'])); + File::put($this->globalsPath.'/fr/test.yaml', 'null'); + + $this->runUpdateScript(UpdateGlobalVariables::class); + + // File with null content should result in empty array after update + $this->assertEquals([], YAML::parse(File::get($this->globalsPath.'/fr/test.yaml'))); + } }