A free open source IT asset/license management system
Fixed #19429 - multi-line custom fields defaults improprly handling linebreak
snipef71af47d2470
diff --git a/resources/views/livewire/custom-field-set-default-values-for-model.blade.php b/resources/views/livewire/custom-field-set-default-values-for-model.blade.phpindex b3d19a040a..b378b1d7f7 100644--- a/resources/views/livewire/custom-field-set-default-values-for-model.blade.php+++ b/resources/views/livewire/custom-field-set-default-values-for-model.blade.php@@ -86,15 +86,21 @@@elseif($field->element == "listbox")-+ {{-- Iterate CustomField::formatFieldValuesAsArray so line-ending+ handling and the `key|label` split flow through the same+ helper the asset-edit form uses. Skip the '' key the helper+ prepends for listbox ("Select <format>") because on the+ model default-values page we want a truly blank "no default+ set" option, not a "please pick one" prompt. See #19429. --}}<select class="form-control" name="default_values[{{ $field->id }}]" wire:model="selectedValues.{{ $field->db_column }}"><option value=""></option>- @foreach(explode("\r\n", $field->field_values) as $field_value)+ @foreach($field->formatFieldValuesAsArray() as $field_value => $field_label)+ @continue($field_value === '')<option- value="{{$field_value}}"+ value="{{ $field_value }}"wire:key="listbox-{{ $field_value }}">- {{ $field_value }}+ {{ $field_label }}</option>@endforeach</select>@@ -102,14 +108,18 @@@elseif($field->element == "radio")- @foreach(explode("\r\n", $field->field_values) as $field_value)+ {{-- Radio/checkbox mirror the asset-edit form's convention:+ use the display label as both the submitted value and the+ visible text, so stored defaults match what the asset form+ will render as pre-selected. --}}+ @foreach($field->formatFieldValuesAsArray() as $field_value)<label class="col-md-3 form-control" for="{{ $field->db_column }}_{{ str_slug($field_value) }}" wire:key="radio-{{ $field_value }}"><inputid="{{ $field->db_column }}_{{ str_slug($field_value) }}"aria-label="{{ str_slug($field->name) }}"type="radio"name="default_values[{{ $field->id }}]"- value="{{$field_value}}"+ value="{{ $field_value }}"wire:model="selectedValues.{{ $field->db_column }}"/>{{ $field_value }}</label>@@ -117,7 +127,7 @@@elseif($field->element == "checkbox")- @foreach(explode("\r\n", $field->field_values) as $field_value)+ @foreach($field->formatFieldValuesAsArray() as $field_value)<label class="col-md-3 form-control" for="{{ $field->db_column }}_{{ str_slug($field_value) }}" wire:key="checkbox-{{ $field_value }}"><inputid="{{ $field->db_column }}_{{ str_slug($field_value) }}"diff --git a/tests/Feature/Livewire/CustomFieldSetDefaultValuesLineEndingsTest.php b/tests/Feature/Livewire/CustomFieldSetDefaultValuesLineEndingsTest.phpnew file mode 100644index 0000000000..0fa6fc1b11--- /dev/null+++ b/tests/Feature/Livewire/CustomFieldSetDefaultValuesLineEndingsTest.php@@ -0,0 +1,107 @@+<?php++namespace Tests\Feature\Livewire;++use App\Livewire\CustomFieldSetDefaultValuesForModel;+use App\Models\AssetModel;+use App\Models\CustomField;+use App\Models\CustomFieldset;+use App\Models\User;+use Livewire\Livewire;+use PHPUnit\Framework\Attributes\DataProvider;+use Tests\TestCase;++/**+ * Regression coverage for #19429. The model default-values wizard used+ * explode("\r\n", ...) on custom-field values, which only splits CRLF.+ * Fields whose values were saved by a browser sending LF only (macOS,+ * Linux, some Windows Chrome configurations) rendered as a single+ * bunched-together option with visible newline characters. The blade now+ * iterates CustomField::formatFieldValuesAsArray, which handles all three+ * line-ending variants and also picks up the key|label split convention+ * the asset-edit form supports.+ */+class CustomFieldSetDefaultValuesLineEndingsTest extends TestCase+{+ /** @return array<string, array{string, string}> */+ public static function lineEndingProvider(): array+ {+ return [+ 'LF' => ["Red\nGreen\nBlue", "\n"],+ 'CRLF' => ["Red\r\nGreen\r\nBlue", "\r\n"],+ 'CR' => ["Red\rGreen\rBlue", "\r"],+ ];+ }++ private function renderComponentWithField(string $element, string $fieldValues): \Livewire\Features\SupportTesting\Testable+ {+ $field = CustomField::factory()->create([+ 'element' => $element,+ 'field_values' => $fieldValues,+ ]);++ $fieldset = CustomFieldset::factory()->create();+ $fieldset->fields()->attach($field, ['order' => 1, 'required' => false]);++ $model = AssetModel::factory()->create(['fieldset_id' => $fieldset->id]);++ $this->actingAs(User::factory()->editAssetModels()->create());++ return Livewire::test(CustomFieldSetDefaultValuesForModel::class, ['model_id' => $model->id])+ ->set('add_default_values', true);+ }++ #[DataProvider('lineEndingProvider')]+ public function test_checkbox_options_render_as_separate_labels_regardless_of_line_endings(string $fieldValues, string $separator)+ {+ $this->renderComponentWithField('checkbox', $fieldValues)+ ->assertSeeHtml('value="Red"')+ ->assertSeeHtml('value="Green"')+ ->assertSeeHtml('value="Blue"')+ ->assertDontSeeHtml('value="Red'.$separator.'Green');+ }++ #[DataProvider('lineEndingProvider')]+ public function test_radio_options_render_as_separate_labels_regardless_of_line_endings(string $fieldValues, string $separator)+ {+ $this->renderComponentWithField('radio', $fieldValues)+ ->assertSeeHtml('value="Red"')+ ->assertSeeHtml('value="Green"')+ ->assertSeeHtml('value="Blue"')+ ->assertDontSeeHtml('value="Red'.$separator.'Green');+ }++ #[DataProvider('lineEndingProvider')]+ public function test_listbox_options_render_as_separate_options_regardless_of_line_endings(string $fieldValues, string $separator)+ {+ $this->renderComponentWithField('listbox', $fieldValues)+ ->assertSeeHtml('value="Red"')+ ->assertSeeHtml('value="Green"')+ ->assertSeeHtml('value="Blue"')+ ->assertDontSeeHtml('value="Red'.$separator.'Green');+ }++ public function test_checkbox_key_label_split_uses_label_for_both_value_and_display()+ {+ // The asset-edit form's checkbox/radio path uses the display label as+ // both the submitted value and the visible text. This UI must match+ // so stored defaults get pre-selected correctly on the asset form.+ $this->renderComponentWithField('checkbox', "red|Bright Red\ngreen|Forest Green")+ ->assertSeeHtml('value="Bright Red"')+ ->assertSeeHtml('value="Forest Green"')+ ->assertSee('Bright Red')+ ->assertSee('Forest Green');+ }++ public function test_listbox_key_label_split_uses_key_for_value_and_label_for_display()+ {+ // The listbox convention (matching Snipe-IT's x-input.select on the+ // asset-edit form) is: option value = key, option text = label. So a+ // stored default of "red" gets the "Bright Red" option pre-selected.+ $this->renderComponentWithField('listbox', "red|Bright Red\ngreen|Forest Green")+ ->assertSeeHtml('value="red"')+ ->assertSeeHtml('value="green"')+ ->assertSee('Bright Red')+ ->assertSee('Forest Green');+ }+}