3
0
mirror of https://github.com/snipe/snipe-it.git synced 2026-08-18 03:06:23 +00:00

Fixed #19286 - cloned model not preserving fieldset

This commit is contained in:
snipe
2026-07-09 11:47:59 +01:00
parent e52d55410f
commit 9376629392
2 changed files with 59 additions and 1 deletions

View File

@ -290,6 +290,11 @@ class AssetModelsController extends Controller
$this->authorize('create', AssetModel::class);
$cloned_model = clone $model;
// Preserve the source model's id BEFORE we blank the working copy — the
// fieldset picker Livewire component uses model_id to look up the source
// model's fieldset and default values, so the clone form arrives with
// the same fieldset preselected. Regression: #19286.
$source_model_id = $model->id;
$model->id = null;
$model->deleted_at = null;
@ -297,7 +302,7 @@ class AssetModelsController extends Controller
return view('models/edit')
->with('depreciation_list', Helper::depreciationList())
->with('item', $model)
->with('model_id', $model->id)
->with('model_id', $source_model_id)
->with('cloned_model', $cloned_model);
}

View File

@ -0,0 +1,53 @@
<?php
namespace Tests\Feature\AssetModels\Ui;
use App\Livewire\CustomFieldSetDefaultValuesForModel;
use App\Models\AssetModel;
use App\Models\CustomFieldset;
use App\Models\User;
use Livewire\Livewire;
use Tests\TestCase;
class CloneAssetModelTest extends TestCase
{
public function test_clone_page_preselects_source_fieldset()
{
// Regression for #19286: cloning a model that has a fieldset was
// rendering the clone form with the fieldset selector empty, because
// getClone was passing model_id => null to the fieldset picker.
$fieldset = CustomFieldset::factory()->create();
$source = AssetModel::factory()->create(['fieldset_id' => $fieldset->id]);
$this->actingAs(User::factory()->superuser()->create())
->get(route('models.clone.create', $source))
->assertOk()
// The fieldset id is rendered inside the fieldset selector's
// <option selected> markup for the source fieldset.
->assertSee('value="'.$fieldset->id.'" selected', false);
}
public function test_livewire_fieldset_picker_receives_source_model_id_on_clone()
{
$fieldset = CustomFieldset::factory()->create();
$source = AssetModel::factory()->create(['fieldset_id' => $fieldset->id]);
$this->actingAs(User::factory()->superuser()->create());
Livewire::test(CustomFieldSetDefaultValuesForModel::class, ['model_id' => $source->id])
->assertSet('model_id', $source->id)
->assertSet('fieldset_id', $fieldset->id);
}
public function test_livewire_fieldset_picker_leaves_fieldset_empty_when_no_model_id()
{
// Baseline: without a source model_id (i.e. plain create form), the
// fieldset stays unset. Guards against a fix that would leak defaults
// into an unrelated create flow.
$this->actingAs(User::factory()->superuser()->create());
Livewire::test(CustomFieldSetDefaultValuesForModel::class, ['model_id' => null])
->assertSet('model_id', null)
->assertSet('fieldset_id', null);
}
}