Skip to content

Commit 8111023

Browse files
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 <[email protected]>
1 parent 38371ab commit 8111023

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

src/Commands/ExportSites.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ public function handle()
3535
$sites = SiteModel::all()
3636
->mapWithKeys(function ($model) {
3737
return [
38-
$model->handle => [
38+
$model->handle => collect([
3939
'name' => $model->name,
40-
'lang' => $model->lang,
40+
'lang' => $model->lang ?? null,
4141
'locale' => $model->locale,
4242
'url' => $model->url,
4343
'attributes' => $model->attributes ?? [],
44-
],
44+
])->filter()->all(),
4545
];
4646
});
4747

tests/Commands/ExportSitesTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Commands;
4+
5+
use Illuminate\Foundation\Testing\RefreshDatabase;
6+
use PHPUnit\Framework\Attributes\Test;
7+
use Statamic\Eloquent\Sites\SiteModel;
8+
use Statamic\Sites\Sites;
9+
use Tests\TestCase;
10+
11+
class ExportSitesTest extends TestCase
12+
{
13+
use RefreshDatabase;
14+
15+
#[Test]
16+
public function it_exports_sites()
17+
{
18+
SiteModel::create(['handle' => 'en', 'name' => 'English', 'locale' => 'en_US', 'lang' => '', 'url' => 'http://test.com/', 'attributes' => []]);
19+
SiteModel::create(['handle' => 'fr', 'name' => 'French', 'locale' => 'fr_FR', 'lang' => '', 'url' => 'http://fr.test.com/', 'attributes' => []]);
20+
SiteModel::create(['handle' => 'es', 'name' => 'Spanish', 'locale' => 'es_ES', 'lang' => '', 'url' => 'http://test.com/es/', 'attributes' => ['foo' => 'bar']]);
21+
SiteModel::create(['handle' => 'de', 'name' => 'German', 'locale' => 'de_DE', 'lang' => 'de', 'url' => 'http://test.com/de/', 'attributes' => []]);
22+
23+
$this->artisan('statamic:eloquent:export-sites')
24+
->expectsOutputToContain('Sites exported')
25+
->assertExitCode(0);
26+
27+
$this->assertEquals([
28+
'en' => [
29+
'name' => 'English',
30+
'locale' => 'en_US',
31+
'url' => 'http://test.com/',
32+
],
33+
'fr' => [
34+
'name' => 'French',
35+
'locale' => 'fr_FR',
36+
'url' => 'http://fr.test.com/',
37+
],
38+
'es' => [
39+
'name' => 'Spanish',
40+
'locale' => 'es_ES',
41+
'url' => 'http://test.com/es/',
42+
'attributes' => ['foo' => 'bar'],
43+
],
44+
'de' => [
45+
'name' => 'German',
46+
'lang' => 'de',
47+
'locale' => 'de_DE',
48+
'url' => 'http://test.com/de/',
49+
],
50+
], (new Sites)->config());
51+
}
52+
}

0 commit comments

Comments
 (0)