Skip to content

A free open source IT asset/license management system

Fixed #19980 - don’t null asset name on kit assignment

snipee1f7d0dab58f
diff --git a/app/Services/PredefinedKitCheckoutService.php b/app/Services/PredefinedKitCheckoutService.php
index 84bdb89246..112c988098 100644
--- a/app/Services/PredefinedKitCheckoutService.php
+++ b/app/Services/PredefinedKitCheckoutService.php
@@ -240,7 +240,13 @@ class PredefinedKitCheckoutService
continue;
}
- $error = $asset->checkOut($user, $admin, $checkout_at, $expected_checkin, $note, null);
+ // Pass the current name as the override so Asset::checkOut()
+ // does not null it out. The sixth arg is the admin-provided
+ // rename-at-checkout value and the method assigns it to
+ // $this->name unconditionally. Kit checkout has no form
+ // field for renaming, so passing the existing name
+ // preserves the asset's name across the save.
+ $error = $asset->checkOut($user, $admin, $checkout_at, $expected_checkin, $note, $asset->name);
if ($error) {
array_merge_recursive($errors, $asset->getErrors()->toArray());
}
diff --git a/tests/Feature/PredefinedKits/Ui/CheckoutKitPreservesAssetFieldsTest.php b/tests/Feature/PredefinedKits/Ui/CheckoutKitPreservesAssetFieldsTest.php
new file mode 100644
index 0000000000..08d8a26eb7
--- /dev/null
+++ b/tests/Feature/PredefinedKits/Ui/CheckoutKitPreservesAssetFieldsTest.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace Tests\Feature\PredefinedKits\Ui;
+
+use App\Models\Asset;
+use App\Models\AssetModel;
+use App\Models\PredefinedKit;
+use App\Models\Statuslabel;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * Regression coverage for #19980. Checking out a predefined kit
+ * nulled the `name` column on every included asset:
+ * PredefinedKitCheckoutService::saveToDb() called
+ * $asset->checkOut(..., $note, null). The sixth arg is the admin-
+ * provided rename-at-checkout value, which Asset::checkOut() assigns
+ * to $this->name unconditionally, so passing null wiped the asset's
+ * name. Passing $asset->name preserves it across the save.
+ */
+class CheckoutKitPreservesAssetFieldsTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+ Statuslabel::factory()->rtd()->create();
+ }
+
+ public function test_kit_checkout_preserves_asset_name(): void
+ {
+ $actor = User::factory()->checkoutAssets()->create();
+ $target = User::factory()->create();
+
+ $model = AssetModel::factory()->create();
+ $asset = Asset::factory()->for($model, 'model')->create(['name' => 'FinanceLaptop-07']);
+
+ $kit = PredefinedKit::factory()->create();
+ $kit->models()->attach($model->id, ['quantity' => 1]);
+
+ $this->actingAs($actor)
+ ->post(route('kits.checkout.store', $kit), ['user_id' => $target->id])
+ ->assertSessionHas('success');
+
+ $this->assertSame(
+ 'FinanceLaptop-07',
+ $asset->fresh()->name,
+ 'Kit checkout must preserve the asset name across the save.',
+ );
+ }
+}