Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public function dehydrate($target)
$languages = Language::orderBy('default', 'desc')->get();

return [
$languages->mapWithKeys(fn ($language) => [$language->code => new Text((string) $target->getValue()->get($language->code))]
$languages->mapWithKeys(
fn ($language) => [$language->code => $target->getValue()->get($language->code)?->getValue() ?? ''],
)->toArray(),
[],
];
Expand All @@ -34,7 +35,7 @@ public function hydrate($value)

public function get(&$target, $key)
{
return $target->{$key};
return $target->getValue()->get($key)?->getValue() ?? '';
}

public function set(&$target, $key, $value)
Expand All @@ -46,6 +47,10 @@ public function set(&$target, $key, $value)
$collectionValue = $target->getValue();
$field = $collectionValue->get($key);

if (! $field instanceof Text) {
$field = new Text;
}

$field->setValue($value);

$collectionValue->put($key, $field);
Expand Down
66 changes: 66 additions & 0 deletions tests/admin/Unit/Livewire/Synthesizers/TranslatedTextSynthTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

use Livewire\Mechanisms\HandleComponents\ComponentContext;
use Lunar\Admin\Support\Synthesizers\TranslatedTextSynth;
use Lunar\FieldTypes\Text;
use Lunar\FieldTypes\TranslatedText;
use Lunar\Models\Language;
use Lunar\Tests\Admin\Unit\Livewire\TestCase;
use stdClass;

uses(TestCase::class)
->group('support.synthesizers');

it('dehydrates translated text values as plain locale strings', function () {
Language::factory()->create([
'code' => 'en',
'default' => true,
]);

Language::factory()->create([
'code' => 'es',
'default' => false,
]);

$field = new TranslatedText(collect([
'en' => new Text('<p>English description</p>'),
'es' => new Text('Descripcion'),
]));

$synth = new TranslatedTextSynth(
new ComponentContext(new stdClass),
'attribute_data.description',
);
[$state] = $synth->dehydrate($field);

expect($state)->toBe([
'en' => '<p>English description</p>',
'es' => 'Descripcion',
])->and($synth->get($field, 'en'))->toBe('<p>English description</p>');
});

it('stores rich editor document payloads as html for translated text fields', function () {
$field = new TranslatedText(collect());

$synth = new TranslatedTextSynth(
new ComponentContext(new stdClass),
'attribute_data.description',
);
$synth->set($field, 'en', [
'type' => 'doc',
'content' => [
[
'type' => 'paragraph',
'content' => [
[
'type' => 'text',
'text' => 'Rich description',
],
],
],
],
]);

expect($field->getValue()->get('en'))->toBeInstanceOf(Text::class)
->and($field->getValue()->get('en')?->getValue())->toBe('<p>Rich description</p>');
});
Loading