From 811102341cea649159c4c9c2c03ccb9f6e0aa068 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Fri, 6 Sep 2024 15:24:32 +0100 Subject: [PATCH] `export-sites` command: Prevent `lang` and `attributes` being exported with empty values (#349) * Add test to confirm site exporter works * Add failing test * Filter out empty values from site config * Simplify. * Fix styling --------- Co-authored-by: duncanmcclean --- src/Commands/ExportSites.php | 6 ++-- tests/Commands/ExportSitesTest.php | 52 ++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 tests/Commands/ExportSitesTest.php diff --git a/src/Commands/ExportSites.php b/src/Commands/ExportSites.php index 203377db..51ef08a9 100644 --- a/src/Commands/ExportSites.php +++ b/src/Commands/ExportSites.php @@ -35,13 +35,13 @@ public function handle() $sites = SiteModel::all() ->mapWithKeys(function ($model) { return [ - $model->handle => [ + $model->handle => collect([ 'name' => $model->name, - 'lang' => $model->lang, + 'lang' => $model->lang ?? null, 'locale' => $model->locale, 'url' => $model->url, 'attributes' => $model->attributes ?? [], - ], + ])->filter()->all(), ]; }); diff --git a/tests/Commands/ExportSitesTest.php b/tests/Commands/ExportSitesTest.php new file mode 100644 index 00000000..57c73332 --- /dev/null +++ b/tests/Commands/ExportSitesTest.php @@ -0,0 +1,52 @@ + 'en', 'name' => 'English', 'locale' => 'en_US', 'lang' => '', 'url' => 'http://test.com/', 'attributes' => []]); + SiteModel::create(['handle' => 'fr', 'name' => 'French', 'locale' => 'fr_FR', 'lang' => '', 'url' => 'http://fr.test.com/', 'attributes' => []]); + SiteModel::create(['handle' => 'es', 'name' => 'Spanish', 'locale' => 'es_ES', 'lang' => '', 'url' => 'http://test.com/es/', 'attributes' => ['foo' => 'bar']]); + SiteModel::create(['handle' => 'de', 'name' => 'German', 'locale' => 'de_DE', 'lang' => 'de', 'url' => 'http://test.com/de/', 'attributes' => []]); + + $this->artisan('statamic:eloquent:export-sites') + ->expectsOutputToContain('Sites exported') + ->assertExitCode(0); + + $this->assertEquals([ + 'en' => [ + 'name' => 'English', + 'locale' => 'en_US', + 'url' => 'http://test.com/', + ], + 'fr' => [ + 'name' => 'French', + 'locale' => 'fr_FR', + 'url' => 'http://fr.test.com/', + ], + 'es' => [ + 'name' => 'Spanish', + 'locale' => 'es_ES', + 'url' => 'http://test.com/es/', + 'attributes' => ['foo' => 'bar'], + ], + 'de' => [ + 'name' => 'German', + 'lang' => 'de', + 'locale' => 'de_DE', + 'url' => 'http://test.com/de/', + ], + ], (new Sites)->config()); + } +}