From e32d2cca4f9d1e81cb23e9dd857fb1a69cbd11bb Mon Sep 17 00:00:00 2001 From: Lionel Guichard Date: Thu, 15 Feb 2024 00:46:12 +0100 Subject: [PATCH] Feature - Field translated text (#1505) This PR fix definitely the TranslatedText component for the MVP. It's a same UX approach that legacy version. Let's get a simple solution working and consider a better solution later. The issue related to #1425 --------- Co-authored-by: Alec Ritson Co-authored-by: Glenn Jacobs --- .../admin/resources/lang/en/fieldtypes.php | 1 + .../components/translated-text.blade.php | 65 ++++++++++--- .../Resources/AttributeGroupResource.php | 2 +- .../AttributesRelationManager.php | 6 +- .../Resources/ProductOptionResource.php | 5 +- .../ValuesRelationManager.php | 4 +- .../Filament/Resources/ProductResource.php | 3 +- .../ProductResource/Pages/ListProducts.php | 14 +-- .../Collections/CreateChildCollection.php | 4 +- .../Collections/CreateRootCollection.php | 20 +--- .../Traits/CreatesChildCollections.php | 18 +--- .../src/Support/FieldTypes/TranslatedText.php | 6 +- .../Forms/Components/TranslatedRichEditor.php | 15 +++ .../Forms/Components/TranslatedText.php | 97 ++++++++++++++++++- .../Forms/Components/TranslatedTextInput.php | 15 +++ .../Synthesizers/TranslatedTextSynth.php | 10 +- .../Collections/CreateChildCollection.php | 4 +- packages/core/src/Models/ProductOption.php | 18 ---- .../Pages/EditAttributeGroupTest.php | 13 ++- .../AttributesRelationManagerTest.php | 9 +- .../Pages/ManageCollectionChildrenTest.php | 4 +- .../ProductResource/ListProductsTest.php | 2 +- .../Widgets/CollectionTreeViewTest.php | 18 ++-- 23 files changed, 246 insertions(+), 107 deletions(-) create mode 100644 packages/admin/src/Support/Forms/Components/TranslatedRichEditor.php create mode 100644 packages/admin/src/Support/Forms/Components/TranslatedTextInput.php diff --git a/packages/admin/resources/lang/en/fieldtypes.php b/packages/admin/resources/lang/en/fieldtypes.php index 8cecb4b0b3..df36e57660 100644 --- a/packages/admin/resources/lang/en/fieldtypes.php +++ b/packages/admin/resources/lang/en/fieldtypes.php @@ -26,6 +26,7 @@ 'richtext' => [ 'label' => 'Rich Text', ], + 'locales' => 'Locales', ], ], 'toggle' => [ diff --git a/packages/admin/resources/views/forms/components/translated-text.blade.php b/packages/admin/resources/views/forms/components/translated-text.blade.php index cfc5a78c28..537849cfce 100644 --- a/packages/admin/resources/views/forms/components/translated-text.blade.php +++ b/packages/admin/resources/views/forms/components/translated-text.blade.php @@ -1,21 +1,54 @@ - + -
- @foreach ($getLanguages() as $language) - -
- - - -
+
+
+ @if ($getMoreLanguages()->count()) + + {{ Str::upper($getDefaultLanguage()->code) }} + + @endif + + {{ $getComponentByLanguage($getDefaultLanguage()) }} + +
+ @if ($getMoreLanguages()->count()) + @foreach ($getMoreLanguages() as $language) +
+ + {{ Str::upper($language->code) }} + + + {{ $getComponentByLanguage($language) }} + +
@endforeach + @endif +
+ + @if ($getMoreLanguages()->count()) +
+ + + + {{ __('lunarpanel::fieldtypes.translatedtext.form.locales') }} + + +
+ @endif - + \ No newline at end of file diff --git a/packages/admin/src/Filament/Resources/AttributeGroupResource.php b/packages/admin/src/Filament/Resources/AttributeGroupResource.php index c6f3e323c8..85d1a62d6f 100644 --- a/packages/admin/src/Filament/Resources/AttributeGroupResource.php +++ b/packages/admin/src/Filament/Resources/AttributeGroupResource.php @@ -72,7 +72,7 @@ protected static function getAttributableTypeFormComponent(): Component protected static function getNameFormComponent(): Component { - return \Lunar\Admin\Support\Forms\Components\TranslatedText::make('name') // TODO: we need a custom field type for this + return \Lunar\Admin\Support\Forms\Components\TranslatedText::make('name') ->label(__('lunarpanel::attributegroup.form.name.label')) ->required() ->maxLength(255) diff --git a/packages/admin/src/Filament/Resources/AttributeGroupResource/RelationManagers/AttributesRelationManager.php b/packages/admin/src/Filament/Resources/AttributeGroupResource/RelationManagers/AttributesRelationManager.php index 332703a358..09ca8a6beb 100644 --- a/packages/admin/src/Filament/Resources/AttributeGroupResource/RelationManagers/AttributesRelationManager.php +++ b/packages/admin/src/Filament/Resources/AttributeGroupResource/RelationManagers/AttributesRelationManager.php @@ -11,6 +11,8 @@ use Illuminate\Support\Str; use Illuminate\Validation\Rules\Unique; use Lunar\Admin\Support\Facades\AttributeData; +use Lunar\Admin\Support\Forms\Components\TranslatedText; +use Lunar\Models\Language; class AttributesRelationManager extends RelationManager { @@ -27,7 +29,7 @@ public function form(Form $form): Form { return $form ->schema([ - Forms\Components\TextInput::make('name.en') // TODO: localise + TranslatedText::make('name') ->label( __('lunarpanel::attribute.form.name.label') ) @@ -38,7 +40,7 @@ public function form(Form $form): Form if ($operation !== 'create') { return; } - $set('handle', Str::slug($state)); + $set('handle', Str::slug($state[Language::getDefault()->code])); // TODO : create new global variable on LunarPanelManager with default language ? }), Forms\Components\TextInput::make('description.en') // TODO: localise ->label( diff --git a/packages/admin/src/Filament/Resources/ProductOptionResource.php b/packages/admin/src/Filament/Resources/ProductOptionResource.php index 871ee8e6b0..a231c84fac 100644 --- a/packages/admin/src/Filament/Resources/ProductOptionResource.php +++ b/packages/admin/src/Filament/Resources/ProductOptionResource.php @@ -9,6 +9,7 @@ use Filament\Tables\Table; use Lunar\Admin\Filament\Resources\ProductOptionResource\Pages; use Lunar\Admin\Filament\Resources\ProductOptionResource\RelationManagers; +use Lunar\Admin\Support\Forms\Components\TranslatedText; use Lunar\Admin\Support\Resources\BaseResource; use Lunar\Models\ProductOption; @@ -51,7 +52,7 @@ protected static function getMainFormComponents(): array protected static function getNameFormComponent(): Component { - return \Lunar\Admin\Support\Forms\Components\TranslatedText::make('name') // TODO: we need a custom field type for this + return TranslatedText::make('name') ->label(__('lunarpanel::productoption.form.name.label')) ->required() ->maxLength(255) @@ -60,7 +61,7 @@ protected static function getNameFormComponent(): Component protected static function getLabelFormComponent(): Component { - return \Lunar\Admin\Support\Forms\Components\TranslatedText::make('label') // TODO: we need a custom field type for this + return TranslatedText::make('label') ->label(__('lunarpanel::productoption.form.label.label')) ->required() ->maxLength(255) diff --git a/packages/admin/src/Filament/Resources/ProductOptionResource/RelationManagers/ValuesRelationManager.php b/packages/admin/src/Filament/Resources/ProductOptionResource/RelationManagers/ValuesRelationManager.php index 12e130a4e6..c4660de451 100644 --- a/packages/admin/src/Filament/Resources/ProductOptionResource/RelationManagers/ValuesRelationManager.php +++ b/packages/admin/src/Filament/Resources/ProductOptionResource/RelationManagers/ValuesRelationManager.php @@ -2,12 +2,12 @@ namespace Lunar\Admin\Filament\Resources\ProductOptionResource\RelationManagers; -use Filament\Forms; use Filament\Forms\Form; use Filament\Resources\RelationManagers\RelationManager; use Filament\Tables; use Filament\Tables\Table; use Illuminate\Database\Eloquent\Model; +use Lunar\Admin\Support\Forms\Components\TranslatedText; use Lunar\Models\ProductOptionValue; class ValuesRelationManager extends RelationManager @@ -23,7 +23,7 @@ public function form(Form $form): Form { return $form ->schema([ - Forms\Components\TextInput::make('name.en') + TranslatedText::make('name') ->required() ->maxLength(255), ]); diff --git a/packages/admin/src/Filament/Resources/ProductResource.php b/packages/admin/src/Filament/Resources/ProductResource.php index 34c7cfd442..acd3e16f70 100644 --- a/packages/admin/src/Filament/Resources/ProductResource.php +++ b/packages/admin/src/Filament/Resources/ProductResource.php @@ -22,6 +22,7 @@ use Lunar\Admin\Filament\Widgets\Products\VariantSwitcherTable; use Lunar\Admin\Support\Forms\Components\Attributes; use Lunar\Admin\Support\Forms\Components\Tags as TagsComponent; +use Lunar\Admin\Support\Forms\Components\TranslatedText; use Lunar\Admin\Support\RelationManagers\ChannelRelationManager; use Lunar\Admin\Support\RelationManagers\MediaRelationManager; use Lunar\Admin\Support\RelationManagers\PriceRelationManager; @@ -169,7 +170,7 @@ public static function getBasePriceFormComponent(): Component public static function getBaseNameFormComponent(): Component { - return Forms\Components\TextInput::make('name') + return TranslatedText::make('name') ->label(__('lunarpanel::product.form.name.label'))->required(); } diff --git a/packages/admin/src/Filament/Resources/ProductResource/Pages/ListProducts.php b/packages/admin/src/Filament/Resources/ProductResource/Pages/ListProducts.php index e8f26219f3..9846d55f9d 100644 --- a/packages/admin/src/Filament/Resources/ProductResource/Pages/ListProducts.php +++ b/packages/admin/src/Filament/Resources/ProductResource/Pages/ListProducts.php @@ -12,10 +12,8 @@ use Lunar\Admin\Filament\Resources\ProductResource; use Lunar\Admin\Support\Pages\BaseListRecords; use Lunar\Facades\DB; -use Lunar\FieldTypes\TranslatedText; use Lunar\Models\Attribute; use Lunar\Models\Currency; -use Lunar\Models\Language; use Lunar\Models\Product; use Lunar\Models\TaxClass; @@ -52,25 +50,19 @@ public static function createActionFormInputs(): array public static function createRecord(array $data, string $model): Model { - $language = Language::getDefault(); $currency = Currency::getDefault(); $nameAttribute = Attribute::whereAttributeType($model) ->whereHandle('name') - ->first(); - - $name = $data['name']; - - if ($nameAttribute->type == TranslatedText::class) { - $name = [$language->code => $name]; - } + ->first() + ->type; DB::beginTransaction(); $product = $model::create([ 'status' => 'draft', 'product_type_id' => $data['product_type_id'], 'attribute_data' => [ - 'name' => new $nameAttribute->type($name), + 'name' => new $nameAttribute($data['name']), ], ]); $variant = $product->variants()->create([ diff --git a/packages/admin/src/Support/Actions/Collections/CreateChildCollection.php b/packages/admin/src/Support/Actions/Collections/CreateChildCollection.php index a003f7f6b2..a8020eac31 100644 --- a/packages/admin/src/Support/Actions/Collections/CreateChildCollection.php +++ b/packages/admin/src/Support/Actions/Collections/CreateChildCollection.php @@ -3,9 +3,9 @@ namespace Lunar\Admin\Support\Actions\Collections; use Filament\Actions\CreateAction; -use Filament\Forms\Components\TextInput; use Illuminate\Database\Eloquent\Model; use Lunar\Admin\Support\Actions\Traits\CreatesChildCollections; +use Lunar\Admin\Support\Forms\Components\TranslatedText; use Lunar\Models\Collection; class CreateChildCollection extends CreateAction @@ -25,7 +25,7 @@ public function setUp(): void }); $this->form([ - TextInput::make('name')->required(), + TranslatedText::make('name')->required(), ]); $this->label( diff --git a/packages/admin/src/Support/Actions/Collections/CreateRootCollection.php b/packages/admin/src/Support/Actions/Collections/CreateRootCollection.php index 74ac091a35..09c6b03623 100644 --- a/packages/admin/src/Support/Actions/Collections/CreateRootCollection.php +++ b/packages/admin/src/Support/Actions/Collections/CreateRootCollection.php @@ -3,13 +3,11 @@ namespace Lunar\Admin\Support\Actions\Collections; use Filament\Actions\CreateAction; -use Filament\Forms\Components\TextInput; use Filament\Forms\Form; +use Lunar\Admin\Support\Forms\Components\TranslatedText; use Lunar\Facades\DB; -use Lunar\FieldTypes\TranslatedText; use Lunar\Models\Attribute; use Lunar\Models\Collection; -use Lunar\Models\Language; class CreateRootCollection extends CreateAction { @@ -23,22 +21,12 @@ public function setUp(): void DB::beginTransaction(); $record = $this->process(function (array $data) { - $attribute = Attribute::whereHandle('name')->whereAttributeType(Collection::class)->first(); - $nameValue = $data['name']; - - $fieldType = $attribute->type; - - if ($fieldType == TranslatedText::class) { - $language = Language::getDefault(); - $nameValue = collect([ - $language->code => $data['name'], - ]); - } + $attribute = Attribute::whereHandle('name')->whereAttributeType(Collection::class)->first()->type; return Collection::create([ 'collection_group_id' => $data['collection_group_id'], 'attribute_data' => [ - 'name' => new $fieldType($nameValue), + 'name' => new $attribute($data['name']), ], ]); }); @@ -68,7 +56,7 @@ public function setUp(): void }); $this->form([ - TextInput::make('name')->required(), + TranslatedText::make('name')->required(), ]); $this->label( diff --git a/packages/admin/src/Support/Actions/Traits/CreatesChildCollections.php b/packages/admin/src/Support/Actions/Traits/CreatesChildCollections.php index 9f302a47aa..2d8153bbc3 100644 --- a/packages/admin/src/Support/Actions/Traits/CreatesChildCollections.php +++ b/packages/admin/src/Support/Actions/Traits/CreatesChildCollections.php @@ -3,33 +3,21 @@ namespace Lunar\Admin\Support\Actions\Traits; use Lunar\Facades\DB; -use Lunar\FieldTypes\TranslatedText; use Lunar\Models\Attribute; use Lunar\Models\Collection; -use Lunar\Models\Language; trait CreatesChildCollections { - public function createChildCollection(Collection $parent, string $name) + public function createChildCollection(Collection $parent, array $name) { DB::beginTransaction(); - $attribute = Attribute::whereHandle('name')->whereAttributeType(Collection::class)->first(); - - $nameValue = $name; - $fieldType = $attribute->type; - - if ($fieldType == TranslatedText::class) { - $language = Language::getDefault(); - $nameValue = collect([ - $language->code => $name, - ]); - } + $attribute = Attribute::whereHandle('name')->whereAttributeType(Collection::class)->first()->type; $parent->appendNode(Collection::create([ 'collection_group_id' => $parent->collection_group_id, 'attribute_data' => [ - 'name' => new $fieldType($nameValue), + 'name' => new $attribute($name), ], ])); diff --git a/packages/admin/src/Support/FieldTypes/TranslatedText.php b/packages/admin/src/Support/FieldTypes/TranslatedText.php index 5dc3465332..04bbc85966 100644 --- a/packages/admin/src/Support/FieldTypes/TranslatedText.php +++ b/packages/admin/src/Support/FieldTypes/TranslatedText.php @@ -3,7 +3,7 @@ namespace Lunar\Admin\Support\FieldTypes; use Filament\Forms\Components\Component; -use Lunar\Admin\Support\Forms\Components\TranslatedText as TranslatedTextInput; +use Lunar\Admin\Support\Forms\Components\TranslatedText as TranslatedTextComponent; use Lunar\Admin\Support\Synthesizers\TranslatedTextSynth; use Lunar\Models\Attribute; @@ -18,7 +18,9 @@ public static function getConfigurationFields(): array public static function getFilamentComponent(Attribute $attribute): Component { - return TranslatedTextInput::make($attribute->handle) + return TranslatedTextComponent::make($attribute->handle) + ->optionRichtext((bool) $attribute->configuration->get('richtext')) + ->required((bool) $attribute->configuration->get('required')) ->helperText($attribute->translate('description')); } } diff --git a/packages/admin/src/Support/Forms/Components/TranslatedRichEditor.php b/packages/admin/src/Support/Forms/Components/TranslatedRichEditor.php new file mode 100644 index 0000000000..f780804e57 --- /dev/null +++ b/packages/admin/src/Support/Forms/Components/TranslatedRichEditor.php @@ -0,0 +1,15 @@ +hiddenLabel(); + } +} diff --git a/packages/admin/src/Support/Forms/Components/TranslatedText.php b/packages/admin/src/Support/Forms/Components/TranslatedText.php index ae7712191a..8526e43888 100644 --- a/packages/admin/src/Support/Forms/Components/TranslatedText.php +++ b/packages/admin/src/Support/Forms/Components/TranslatedText.php @@ -2,16 +2,109 @@ namespace Lunar\Admin\Support\Forms\Components; +use Filament\Forms\ComponentContainer; +use Filament\Forms\Components\Component; use Filament\Forms\Components\TextInput; +use Illuminate\Support\Collection; use Lunar\Models\Language; class TranslatedText extends TextInput { protected string $view = 'lunarpanel::forms.components.translated-text'; + public bool $expanded = false; + + public bool $optionRichtext = false; + + public Language $defaultLanguage; + + public Collection $components; + + public Collection $languages; + + public function setUp(): void + { + parent::setUp(); + + $this->languages = Language::orderBy('default', 'desc')->get(); + + $this->default(static function (TranslatedText $component): array { + return $component->getLanguageDefaults(); + }); + } + + public function prepareChildComponents() + { + $this->components = collect( + $this->getLanguages()->map(fn ($lang) => $this->getOptionRichtext() ? + TranslatedRichEditor::make($lang->code)->statePath($lang->code) : + TranslatedTextInput::make($lang->code)->statePath($lang->code) + ) + ); + } + + public function prepareTranslateLocaleComponent(Component $component, string $locale) + { + $localeComponent = clone $component; + + $localeComponent->name($component->getName()); + + $localeComponent->statePath($localeComponent->getName()); + + $localeComponent->required($this->isRequired && $locale == $this->getDefaultLanguage()->code); + + return $localeComponent; + } + + public function getComponentByLanguage(Language $language): ComponentContainer + { + $this->prepareChildComponents(); + + return ComponentContainer::make($this->getLivewire()) + ->parentComponent($this) + ->components( + $this->components + ->filter(fn ($component): bool => $component->getName() == $language->code) + ->map(fn ($component) => $this->prepareTranslateLocaleComponent($component, $language->code)) + ->all() + ) + ->getClone(); + } + + public function optionRichtext(bool $optionRichtext): static + { + $this->optionRichtext = $optionRichtext; + + return $this; + } + + public function getOptionRichtext(): bool + { + return $this->optionRichtext; + } + + public function getExpanded() + { + return $this->expanded; + } + + public function getDefaultLanguage() + { + return $this->languages->first(fn ($lang) => $lang->default); + } + + public function getMoreLanguages() + { + return $this->languages->filter(fn ($lang) => ! $lang->default); + } + + public function getLanguageDefaults(): array + { + return $this->getLanguages()->mapWithKeys(fn ($language) => [$language->code => ''])->toArray(); + } + public function getLanguages() { - // TODO: blink + sort - return Language::get(); + return $this->languages; } } diff --git a/packages/admin/src/Support/Forms/Components/TranslatedTextInput.php b/packages/admin/src/Support/Forms/Components/TranslatedTextInput.php new file mode 100644 index 0000000000..5ad233a690 --- /dev/null +++ b/packages/admin/src/Support/Forms/Components/TranslatedTextInput.php @@ -0,0 +1,15 @@ +hiddenLabel(); + } +} diff --git a/packages/admin/src/Support/Synthesizers/TranslatedTextSynth.php b/packages/admin/src/Support/Synthesizers/TranslatedTextSynth.php index 4066677c48..e1f5261478 100644 --- a/packages/admin/src/Support/Synthesizers/TranslatedTextSynth.php +++ b/packages/admin/src/Support/Synthesizers/TranslatedTextSynth.php @@ -2,7 +2,9 @@ namespace Lunar\Admin\Support\Synthesizers; +use Lunar\FieldTypes\Text; use Lunar\FieldTypes\TranslatedText; +use Lunar\Models\Language; class TranslatedTextSynth extends AbstractFieldSynth { @@ -12,7 +14,13 @@ class TranslatedTextSynth extends AbstractFieldSynth public function dehydrate($target) { - return [$target->getValue()->toArray(), []]; + $languages = Language::orderBy('default', 'desc')->get(); + + return [ + $languages->mapWithKeys(fn ($language) => [$language->code => new Text((string) $target->getValue()->get($language->code))] + )->toArray(), + [], + ]; } public function hydrate($value) diff --git a/packages/admin/src/Support/Tables/Actions/Collections/CreateChildCollection.php b/packages/admin/src/Support/Tables/Actions/Collections/CreateChildCollection.php index e1fc7b1a89..d4e3715b21 100644 --- a/packages/admin/src/Support/Tables/Actions/Collections/CreateChildCollection.php +++ b/packages/admin/src/Support/Tables/Actions/Collections/CreateChildCollection.php @@ -2,10 +2,10 @@ namespace Lunar\Admin\Support\Tables\Actions\Collections; -use Filament\Forms\Components\TextInput; use Filament\Tables\Actions\CreateAction; use Filament\Tables\Table; use Lunar\Admin\Support\Actions\Traits\CreatesChildCollections; +use Lunar\Admin\Support\Forms\Components\TranslatedText; class CreateChildCollection extends CreateAction { @@ -25,7 +25,7 @@ public function setUp(): void }); $this->form([ - TextInput::make('name')->required(), + TranslatedText::make('name')->required(), ]); $this->createAnother(false); diff --git a/packages/core/src/Models/ProductOption.php b/packages/core/src/Models/ProductOption.php index b10d97c13e..68c7bc98c1 100644 --- a/packages/core/src/Models/ProductOption.php +++ b/packages/core/src/Models/ProductOption.php @@ -52,24 +52,6 @@ protected static function newFactory(): ProductOptionFactory return ProductOptionFactory::new(); } - public function getNameAttribute(string $value): mixed - { - return json_decode($value); - } - - protected function setNameAttribute(mixed $value): void - { - $this->attributes['name'] = json_encode($value); - } - - protected function label(): Attribute - { - return Attribute::make( - get: fn (string $value) => json_decode($value), - set: fn ($value) => json_encode($value), - ); - } - /** * Define which attributes should be * protected from mass assignment. diff --git a/tests/admin/Feature/Filament/Resources/AttributeGroupResource/Pages/EditAttributeGroupTest.php b/tests/admin/Feature/Filament/Resources/AttributeGroupResource/Pages/EditAttributeGroupTest.php index 09d48b1dab..dd13fc882e 100644 --- a/tests/admin/Feature/Filament/Resources/AttributeGroupResource/Pages/EditAttributeGroupTest.php +++ b/tests/admin/Feature/Filament/Resources/AttributeGroupResource/Pages/EditAttributeGroupTest.php @@ -9,12 +9,23 @@ ->group('resource.attribute-group'); it('can render attribute group edit page', function () { + + \Lunar\Models\Language::factory()->create([ + 'default' => true, + ]); + $this->asStaff(admin: true) ->get(AttributeGroupResource::getUrl('edit', ['record' => AttributeGroup::factory()->create()])) ->assertSuccessful(); }); it('can retrieve attribute group data', function () { + + $lang = \Lunar\Models\Language::factory()->create([ + 'default' => true, + 'code' => 'en', + ]); + $this->asStaff(); $attributeGroup = AttributeGroup::factory()->create(); @@ -23,6 +34,6 @@ 'record' => $attributeGroup->getRouteKey(), ]) ->assertFormSet([ - 'name.en' => $attributeGroup->translate('name'), // TODO: Needs to be translated + 'name.'.$lang->code => $attributeGroup->translate('name', $lang->code), ]); }); diff --git a/tests/admin/Feature/Filament/Resources/AttributeGroupResource/RelationManagers/AttributesRelationManagerTest.php b/tests/admin/Feature/Filament/Resources/AttributeGroupResource/RelationManagers/AttributesRelationManagerTest.php index be134274d8..1dd7252222 100644 --- a/tests/admin/Feature/Filament/Resources/AttributeGroupResource/RelationManagers/AttributesRelationManagerTest.php +++ b/tests/admin/Feature/Filament/Resources/AttributeGroupResource/RelationManagers/AttributesRelationManagerTest.php @@ -9,6 +9,7 @@ ->group('resource.attribute-group'); it('can render relation manager', function () { + $this->asStaff(); $attributeGroup = AttributeGroup::factory()->create(); @@ -20,6 +21,12 @@ }); it('can create attributes', function ($type, $configuration = [], $expectedData = []) { + + $lang = \Lunar\Models\Language::factory()->create([ + 'default' => true, + 'code' => 'en', + ]); + $this->asStaff(); $attributeGroup = AttributeGroup::factory()->create(); @@ -28,7 +35,7 @@ 'ownerRecord' => $attributeGroup, 'pageClass' => EditAttributeGroup::class, ])->callTableAction(\Filament\Actions\CreateAction::class, data: [ - 'name.en' => 'Foobar', // TODO: Use translation + 'name.'.$lang->code => 'Foobar', 'type' => $type, 'handle' => 'foobar', 'configuration' => $configuration, diff --git a/tests/admin/Feature/Filament/Resources/CollectionResources/Pages/ManageCollectionChildrenTest.php b/tests/admin/Feature/Filament/Resources/CollectionResources/Pages/ManageCollectionChildrenTest.php index 20b5162deb..5388347bff 100644 --- a/tests/admin/Feature/Filament/Resources/CollectionResources/Pages/ManageCollectionChildrenTest.php +++ b/tests/admin/Feature/Filament/Resources/CollectionResources/Pages/ManageCollectionChildrenTest.php @@ -20,7 +20,7 @@ }); it('can create child categories', function () { - \Lunar\Models\Language::factory()->create([ + $language = \Lunar\Models\Language::factory()->create([ 'default' => true, ]); @@ -45,7 +45,7 @@ \Livewire\Livewire::test(ManageCollectionChildren::class, [ 'record' => $record->getKey(), ])->callTableAction('createChildCollection', data: [ - 'name' => 'Test Child Category', + 'name' => [$language->code => 'Test Child Category'], ])->assertHasNoErrors(); expect($record->children()->count())->toBe(1); diff --git a/tests/admin/Feature/Filament/Resources/ProductResource/ListProductsTest.php b/tests/admin/Feature/Filament/Resources/ProductResource/ListProductsTest.php index bc0ee2fc21..04f55ccc58 100644 --- a/tests/admin/Feature/Filament/Resources/ProductResource/ListProductsTest.php +++ b/tests/admin/Feature/Filament/Resources/ProductResource/ListProductsTest.php @@ -34,7 +34,7 @@ \Livewire\Livewire::test(ProductResource\Pages\ListProducts::class) ->callAction('create', data: [ - 'name' => 'Foo Bar', + 'name' => [$language->code => 'Foo Bar'], 'base_price' => 10.99, 'sku' => 'ABCABCAB', 'product_type_id' => $productType->id, diff --git a/tests/admin/Feature/Filament/Resources/ProductResource/Widgets/CollectionTreeViewTest.php b/tests/admin/Feature/Filament/Resources/ProductResource/Widgets/CollectionTreeViewTest.php index c464c64956..e85a76ddf2 100644 --- a/tests/admin/Feature/Filament/Resources/ProductResource/Widgets/CollectionTreeViewTest.php +++ b/tests/admin/Feature/Filament/Resources/ProductResource/Widgets/CollectionTreeViewTest.php @@ -36,18 +36,18 @@ \Lunar\Models\Attribute::factory()->create([ 'handle' => 'name', - 'type' => \Lunar\FieldTypes\Text::class, + 'type' => \Lunar\FieldTypes\TranslatedText::class, 'attribute_type' => \Lunar\Models\Collection::class, ]); - \Lunar\Models\Language::factory()->create([ + $language = \Lunar\Models\Language::factory()->create([ 'default' => true, ]); \Livewire\Livewire::test(CollectionTreeView::class, [ 'record' => $group, ])->callAction('createRootCollection', [ - 'name' => 'Foo Bar', + 'name' => [$language->code => 'Foo Bar'], ])->assertSet('nodes.0.name', 'Foo Bar') ->assertHasNoErrors(); }); @@ -86,11 +86,11 @@ \Lunar\Models\Attribute::factory()->create([ 'handle' => 'name', - 'type' => \Lunar\FieldTypes\Text::class, + 'type' => \Lunar\FieldTypes\TranslatedText::class, 'attribute_type' => \Lunar\Models\Collection::class, ]); - \Lunar\Models\Language::factory()->create([ + $language = \Lunar\Models\Language::factory()->create([ 'default' => true, ]); @@ -107,7 +107,7 @@ \Livewire\Livewire::test(CollectionTreeView::class, [ 'record' => $group, ])->callAction('addChildCollection', [ - 'name' => 'Sub Collection', + 'name' => [$language->code => 'Sub Collection'], ], ['id' => $collection->id]) ->assertCount('nodes', 1) ->assertSet('nodes.0.children.0.id', $child->id) @@ -122,11 +122,11 @@ \Lunar\Models\Attribute::factory()->create([ 'handle' => 'name', - 'type' => \Lunar\FieldTypes\Text::class, + 'type' => \Lunar\FieldTypes\TranslatedText::class, 'attribute_type' => \Lunar\Models\Collection::class, ]); - \Lunar\Models\Language::factory()->create([ + $language = \Lunar\Models\Language::factory()->create([ 'default' => true, ]); @@ -137,7 +137,7 @@ \Livewire\Livewire::test(CollectionTreeView::class, [ 'record' => $group, ])->callAction('addChildCollection', [ - 'name' => 'Sub Collection', + 'name' => [$language->code => 'Sub Collection'], ], ['id' => $collection->id]) ->assertSet('nodes.0.children.0.name', 'Sub Collection'); });