Skip to content

Commit

Permalink
Fix vendor namespaces and handle for blueprints and fieldsets not imp…
Browse files Browse the repository at this point in the history
…orting correctly (#310)

* Handle vendor namespaces and handle for blueprints and fieldsets

* Test coverage
  • Loading branch information
ryanmitchell authored Jul 1, 2024
1 parent d05dea9 commit 0b15b88
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Commands/ImportBlueprints.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ private function importFieldsets(): void
$handle = Str::before($basename, '.yaml');
$handle = str_replace('/', '.', $handle);

// handle any add-on fieldsets
if (Str::startsWith($handle, 'vendor.')) {
$handle = Str::of($handle)->after('vendor.')->replaceFirst('.', '::');
}

$fieldset = Fieldset::make($handle)->setContents(YAML::file($path)->parse());

$lastModified = Carbon::createFromTimestamp(File::lastModified($path));
Expand All @@ -161,6 +166,10 @@ private function getNamespaceAndHandle(string $blueprint): array
$namespace = implode('.', $parts);
$namespace = empty($namespace) ? null : $namespace;

if (Str::startsWith($namespace, 'vendor.')) {
$namespace = Str::after($namespace, 'vendor.');
}

return [$namespace, $handle];
}

Expand Down
38 changes: 38 additions & 0 deletions tests/Commands/ImportBlueprintsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,42 @@ public function it_imports_blueprints_and_fieldsets_with_force_argument()
$this->assertCount(1, BlueprintModel::all());
$this->assertCount(1, FieldsetModel::all());
}

#[Test]
public function it_imports_namespaced_blueprints_and_fieldsets()
{
BlueprintFacade::addNamespace('myaddon', __DIR__.'/__fixtures__/blueprints');
FieldsetFacade::addNamespace('myaddon', __DIR__.'/__fixtures__/blueprints');

BlueprintFacade::make('test')
->setNamespace('myaddon')
->setContents([
'fields' => [
['handle' => 'name', 'field' => ['type' => 'text']],
['handle' => 'email', 'field' => ['type' => 'text'], 'validate' => 'required'],
],
])->save();

FieldsetFacade::make('myaddon::test')
->setContents([
'fields' => [
['handle' => 'foo', 'field' => ['type' => 'text']],
['handle' => 'bar', 'field' => ['type' => 'textarea', 'validate' => 'required']],
],
])->save();

$this->assertCount(0, BlueprintModel::all());
$this->assertCount(0, FieldsetModel::all());

$this->artisan('statamic:eloquent:import-blueprints', ['--force' => true])
->expectsOutputToContain('Blueprints imported successfully.')
->expectsOutputToContain('Fieldsets imported successfully.')
->assertExitCode(0);

$this->assertCount(1, BlueprintModel::all());
$this->assertCount(1, FieldsetModel::all());

$this->assertSame('myaddon', BlueprintModel::first()->namespace);
$this->assertStringContainsString('myaddon::', FieldsetModel::first()->handle);
}
}

0 comments on commit 0b15b88

Please sign in to comment.