Skip to content

Commit b0f5248

Browse files
committed
wip
1 parent 799cf35 commit b0f5248

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

src/Stache/Stores/GlobalVariablesStore.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected function makeVariablesFromFile($handle, $path, $data)
4242
{
4343
$variables = app(Variables::class)
4444
->initialPath($path)
45-
->data(Arr::except($data, 'origin'));
45+
->data(Arr::except($data ?? [], 'origin'));
4646

4747
$handle = explode('/', $handle);
4848

src/UpdateScripts/UpdateGlobalVariables.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private function buildSitesArray(): void
5151
$globalSet->sites($sites)->save();
5252

5353
$variables->each(function ($variable) {
54-
$data = YAML::file($variable->path())->parse();
54+
$data = YAML::file($variable->path())->parse() ?? [];
5555

5656
File::put($variable->path(), YAML::dump(Arr::except($data, 'origin')));
5757
});
@@ -72,7 +72,7 @@ private function migrateFileStructure(): void
7272
}
7373

7474
$contents = YAML::file($globalSet->path())->parse();
75-
$data = Arr::get($contents, 'data', []);
75+
$data = Arr::get($contents, 'data') ?? [];
7676

7777
$globalSet->save();
7878

tests/Stache/Stores/GlobalVariablesStoreTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ public function it_makes_global_variable_instances_from_files()
7070
$this->assertEquals('example', $item->handle());
7171
}
7272

73+
#[Test]
74+
public function it_makes_global_variable_instances_from_files_with_null_contents()
75+
{
76+
$item = $this->store->makeItemFromFile(Path::tidy($this->tempDir.'/en/example.yaml'), 'null');
77+
78+
$this->assertInstanceOf(Variables::class, $item);
79+
$this->assertEquals('example::en', $item->id());
80+
$this->assertEquals('example', $item->handle());
81+
$this->assertEquals([], $item->data()->all());
82+
}
83+
7384
#[Test]
7485
public function it_uses_the_id_as_the_item_key()
7586
{

tests/UpdateScripts/UpdateGlobalVariablesTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,26 @@ public function it_migrates_global_variables_in_a_single_site_install()
5959
unlink($this->globalsPath.'/en/test.yaml');
6060
}
6161

62+
#[Test]
63+
public function it_migrates_global_variables_with_empty_data_in_a_single_site_install()
64+
{
65+
File::put($this->globalsPath.'/test.yaml', "title: Test\ndata:");
66+
67+
$this->runUpdateScript(UpdateGlobalVariables::class);
68+
69+
$expected = <<<'YAML'
70+
title: Test
71+
72+
YAML;
73+
$this->assertEquals($expected, File::get($this->globalsPath.'/test.yaml'));
74+
75+
// Empty data should produce an empty array, not null
76+
$this->assertEquals([], YAML::parse(File::get($this->globalsPath.'/en/test.yaml')));
77+
78+
unlink($this->globalsPath.'/test.yaml');
79+
unlink($this->globalsPath.'/en/test.yaml');
80+
}
81+
6282
#[Test]
6383
public function it_builds_the_sites_array_in_a_multi_site_install()
6484
{
@@ -96,4 +116,25 @@ public function it_builds_the_sites_array_in_a_multi_site_install()
96116
$this->assertEquals(['foo' => 'Bar'], YAML::parse(File::get($this->globalsPath.'/fr/test.yaml')));
97117
$this->assertEquals([], YAML::parse(File::get($this->globalsPath.'/de/test.yaml')));
98118
}
119+
120+
#[Test]
121+
public function it_handles_null_content_in_global_variables_in_a_multi_site_install()
122+
{
123+
$this->setSites([
124+
'en' => ['url' => '/', 'locale' => 'en_US', 'name' => 'English'],
125+
'fr' => ['url' => '/', 'locale' => 'fr_FR', 'name' => 'French'],
126+
]);
127+
128+
File::ensureDirectoryExists($this->globalsPath.'/en');
129+
File::ensureDirectoryExists($this->globalsPath.'/fr');
130+
131+
File::put($this->globalsPath.'/test.yaml', Yaml::dump(['title' => 'Test']));
132+
File::put($this->globalsPath.'/en/test.yaml', Yaml::dump(['foo' => 'Bar']));
133+
File::put($this->globalsPath.'/fr/test.yaml', 'null');
134+
135+
$this->runUpdateScript(UpdateGlobalVariables::class);
136+
137+
// File with null content should result in empty array after update
138+
$this->assertEquals([], YAML::parse(File::get($this->globalsPath.'/fr/test.yaml')));
139+
}
99140
}

0 commit comments

Comments
 (0)