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
2 changes: 1 addition & 1 deletion src/Stache/Stores/GlobalVariablesStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions src/UpdateScripts/UpdateGlobalVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')));
});
Expand All @@ -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();

Expand Down
11 changes: 11 additions & 0 deletions tests/Stache/Stores/GlobalVariablesStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
41 changes: 41 additions & 0 deletions tests/UpdateScripts/UpdateGlobalVariablesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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')));
}
}