Skip to content

Commit

Permalink
export-sites command: Prevent lang and attributes being exporte…
Browse files Browse the repository at this point in the history
…d 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]>
  • Loading branch information
duncanmcclean and duncanmcclean committed Sep 6, 2024
1 parent 38371ab commit 8111023
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Commands/ExportSites.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
];
});

Expand Down
52 changes: 52 additions & 0 deletions tests/Commands/ExportSitesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Commands;

use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Eloquent\Sites\SiteModel;
use Statamic\Sites\Sites;
use Tests\TestCase;

class ExportSitesTest extends TestCase
{
use RefreshDatabase;

#[Test]
public function it_exports_sites()
{
SiteModel::create(['handle' => '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());
}
}

0 comments on commit 8111023

Please sign in to comment.