|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\UpdateScripts; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\File; |
| 6 | +use PHPUnit\Framework\Attributes\Test; |
| 7 | +use Statamic\Facades\YAML; |
| 8 | +use Statamic\UpdateScripts\UpdateGlobalVariables; |
| 9 | +use Tests\PreventSavingStacheItemsToDisk; |
| 10 | +use Tests\TestCase; |
| 11 | +use Tests\UpdateScripts\Concerns\RunsUpdateScripts; |
| 12 | + |
| 13 | +class UpdateGlobalVariablesTest extends TestCase |
| 14 | +{ |
| 15 | + use PreventSavingStacheItemsToDisk, RunsUpdateScripts; |
| 16 | + |
| 17 | + protected $globalsPath; |
| 18 | + |
| 19 | + public function setUp(): void |
| 20 | + { |
| 21 | + parent::setUp(); |
| 22 | + |
| 23 | + $this->globalsPath = $this->fakeStacheDirectory.'/content/globals'; |
| 24 | + |
| 25 | + File::ensureDirectoryExists($this->globalsPath); |
| 26 | + } |
| 27 | + |
| 28 | + #[Test] |
| 29 | + public function it_is_registered() |
| 30 | + { |
| 31 | + $this->assertUpdateScriptRegistered(UpdateGlobalVariables::class); |
| 32 | + } |
| 33 | + |
| 34 | + #[Test] |
| 35 | + public function it_migrates_global_variables_in_a_single_site_install() |
| 36 | + { |
| 37 | + File::put($this->globalsPath.'/test.yaml', Yaml::dump([ |
| 38 | + 'title' => 'Test', |
| 39 | + 'data' => [ |
| 40 | + 'foo' => 'Bar', |
| 41 | + ], |
| 42 | + ])); |
| 43 | + |
| 44 | + $this->runUpdateScript(UpdateGlobalVariables::class); |
| 45 | + |
| 46 | + $expected = <<<'YAML' |
| 47 | +title: Test |
| 48 | + |
| 49 | +YAML; |
| 50 | + $this->assertEquals($expected, File::get($this->globalsPath.'/test.yaml')); |
| 51 | + |
| 52 | + $expected = <<<'YAML' |
| 53 | +foo: Bar |
| 54 | + |
| 55 | +YAML; |
| 56 | + $this->assertEquals($expected, File::get($this->globalsPath.'/en/test.yaml')); |
| 57 | + |
| 58 | + unlink($this->globalsPath.'/test.yaml'); |
| 59 | + unlink($this->globalsPath.'/en/test.yaml'); |
| 60 | + } |
| 61 | + |
| 62 | + #[Test] |
| 63 | + public function it_builds_the_sites_array_in_a_multi_site_install() |
| 64 | + { |
| 65 | + $this->setSites([ |
| 66 | + 'en' => ['url' => '/', 'locale' => 'en_US', 'name' => 'English'], |
| 67 | + 'fr' => ['url' => '/', 'locale' => 'fr_FR', 'name' => 'French'], |
| 68 | + 'de' => ['url' => '/', 'locale' => 'de_DE', 'name' => 'German'], |
| 69 | + ]); |
| 70 | + |
| 71 | + File::ensureDirectoryExists($this->globalsPath.'/en'); |
| 72 | + File::ensureDirectoryExists($this->globalsPath.'/fr'); |
| 73 | + File::ensureDirectoryExists($this->globalsPath.'/de'); |
| 74 | + |
| 75 | + File::put($this->globalsPath.'/test.yaml', Yaml::dump(['title' => 'Test'])); |
| 76 | + File::put($this->globalsPath.'/en/test.yaml', Yaml::dump(['foo' => 'Bar', 'baz' => 'Qux'])); |
| 77 | + File::put($this->globalsPath.'/fr/test.yaml', Yaml::dump(['origin' => 'en', 'foo' => 'Bar'])); |
| 78 | + File::put($this->globalsPath.'/de/test.yaml', Yaml::dump(['origin' => 'fr'])); |
| 79 | + |
| 80 | + $this->runUpdateScript(UpdateGlobalVariables::class); |
| 81 | + |
| 82 | + // Ensures that the sites array is built correctly. |
| 83 | + $expected = <<<'YAML' |
| 84 | +title: Test |
| 85 | +sites: |
| 86 | + de: fr |
| 87 | + en: null |
| 88 | + fr: en |
| 89 | + |
| 90 | +YAML; |
| 91 | + |
| 92 | + $this->assertEquals($expected, File::get($this->globalsPath.'/test.yaml')); |
| 93 | + |
| 94 | + // Ensure the origin key has been removed from the global variables. |
| 95 | + $this->assertEquals(['foo' => 'Bar', 'baz' => 'Qux'], YAML::parse(File::get($this->globalsPath.'/en/test.yaml'))); |
| 96 | + $this->assertEquals(['foo' => 'Bar'], YAML::parse(File::get($this->globalsPath.'/fr/test.yaml'))); |
| 97 | + $this->assertEquals([], YAML::parse(File::get($this->globalsPath.'/de/test.yaml'))); |
| 98 | + } |
| 99 | +} |
0 commit comments