Skip to content

A free open source IT asset/license management system

Compact extra vendor fields into dropdown widget

snipe744a3c367fd1
diff --git a/app/SyncAdapters/MappingTargets.php b/app/SyncAdapters/MappingTargets.php
index d7bafc3c21..dd75e65937 100644
--- a/app/SyncAdapters/MappingTargets.php
+++ b/app/SyncAdapters/MappingTargets.php
@@ -131,7 +131,7 @@ class MappingTargets
*/
public static function optionsForExtra(string $type = 'text', bool $adminDefined = false): array
{
- $options = ['skip' => trans('admin/settings/sync_adapters.target_skip')];
+ $options = [];
// Adapter-declared text-type extras can also route into native
// asset columns that hold arbitrary strings. Admins whose
diff --git a/app/SyncAdapters/SyncAdapter.php b/app/SyncAdapters/SyncAdapter.php
index cf72074f15..1b5a62649e 100644
--- a/app/SyncAdapters/SyncAdapter.php
+++ b/app/SyncAdapters/SyncAdapter.php
@@ -1364,13 +1364,17 @@ abstract class SyncAdapter
/**
* Migration aid: when on, the sync loop looks for an existing
* Snipe-IT asset with a matching serial before creating a new
- * shell asset for a first-seen vendor host. Default off, since
- * silently adopting existing assets is destructive when a
- * customer has multiple adapters or the serial isn't unique.
+ * shell asset for a first-time sync vendor host.
+ *
*/
public function adoptsBySerial(): bool
{
- return SyncAdapterConfig::get($this->instance->id, 'adopt_by_serial') === '1';
+ $stored = SyncAdapterConfig::get($this->instance->id, 'adopt_by_serial');
+ if ($stored !== null) {
+ return $stored === '1';
+ }
+
+ return $this->instance->last_synced_at === null;
}
/**
diff --git a/tests/Feature/SyncAdapters/AdoptBySerialDefaultTest.php b/tests/Feature/SyncAdapters/AdoptBySerialDefaultTest.php
new file mode 100644
index 0000000000..4db172578f
--- /dev/null
+++ b/tests/Feature/SyncAdapters/AdoptBySerialDefaultTest.php
@@ -0,0 +1,62 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters;
+
+use App\Models\SyncAdapterConfig;
+use App\Models\SyncAdapterInstance;
+use App\SyncAdapters\Fleet\FleetAdapter;
+use Tests\TestCase;
+
+
+class AdoptBySerialDefaultTest extends TestCase
+{
+ public function test_defaults_on_for_never_saved_never_synced_instance()
+ {
+ $instance = $this->makeInstance();
+
+ $adapter = new FleetAdapter($instance);
+ $this->assertTrue($adapter->adoptsBySerial());
+ }
+
+ public function test_defaults_off_once_the_instance_has_synced()
+ {
+ $instance = $this->makeInstance();
+ $instance->last_synced_at = now();
+ $instance->save();
+
+ $adapter = new FleetAdapter($instance->fresh());
+ $this->assertFalse($adapter->adoptsBySerial());
+ }
+
+ public function test_explicit_off_persists_before_first_sync()
+ {
+ // Admin unchecks the box and saves. That '0' is stored even
+ // though last_synced_at is still null, and adoptsBySerial()
+ // must honor the explicit value.
+ $instance = $this->makeInstance();
+ SyncAdapterConfig::put($instance->id, 'adopt_by_serial', '0');
+
+ $adapter = new FleetAdapter($instance->fresh());
+ $this->assertFalse($adapter->adoptsBySerial());
+ }
+
+ public function test_explicit_on_persists_after_first_sync()
+ {
+ $instance = $this->makeInstance();
+ SyncAdapterConfig::put($instance->id, 'adopt_by_serial', '1');
+ $instance->last_synced_at = now();
+ $instance->save();
+
+ $adapter = new FleetAdapter($instance->fresh());
+ $this->assertTrue($adapter->adoptsBySerial());
+ }
+
+ private function makeInstance(): SyncAdapterInstance
+ {
+ return SyncAdapterInstance::create([
+ 'adapter_type' => 'fleet',
+ 'label' => 'Fleet Test '.uniqid('', true),
+ 'active' => true,
+ ]);
+ }
+}
diff --git a/tests/Feature/SyncAdapters/ExtraFieldMappingTest.php b/tests/Feature/SyncAdapters/ExtraFieldMappingTest.php
index 56c1ec27be..aecfbc128d 100644
--- a/tests/Feature/SyncAdapters/ExtraFieldMappingTest.php
+++ b/tests/Feature/SyncAdapters/ExtraFieldMappingTest.php
@@ -108,13 +108,16 @@ class ExtraFieldMappingTest extends TestCase
]);
}
- public function test_extra_field_options_include_skip_native_and_custom_targets_for_text_type()
+ public function test_extra_field_options_include_native_and_custom_targets_for_text_type()
{
CustomField::factory()->create(['element' => 'text', 'name' => 'Team Assignment']);
$options = MappingTargets::optionsForExtra('text');
- $this->assertArrayHasKey('skip', $options);
+ // No 'skip' target: the mapping-picker widget models
+ // "unmapped" as the extra sitting in the picker's
+ // available list, not as a committed row with target='skip'.
+ $this->assertArrayNotHasKey('skip', $options);
// Text-type extras can route to native asset_tag / model /
// notes so admins whose vendor stores per-device metadata in
@@ -126,7 +129,7 @@ class ExtraFieldMappingTest extends TestCase
$this->assertArrayHasKey('native:notes', $options);
$this->assertArrayHasKey('native:purchase_date', $options);
$this->assertArrayHasKey('native:order_number', $options);
- $nativeTargets = ['skip', 'native:asset_tag', 'native:model', 'native:notes', 'native:purchase_date', 'native:order_number'];
+ $nativeTargets = ['native:asset_tag', 'native:model', 'native:notes', 'native:purchase_date', 'native:order_number'];
foreach (array_keys($options) as $target) {
if (in_array($target, $nativeTargets, true)) {
continue;
@@ -142,8 +145,9 @@ class ExtraFieldMappingTest extends TestCase
$options = MappingTargets::optionsForExtra('boolean');
// Boolean-typed extras stay custom-fields-only because no
- // native asset column carries a boolean semantic.
- $this->assertArrayHasKey('skip', $options);
+ // native asset column carries a boolean semantic. Also no
+ // 'skip' entry: unmap via ×, not via a stored 'skip' target.
+ $this->assertArrayNotHasKey('skip', $options);
$this->assertArrayNotHasKey('native:asset_tag', $options);
$this->assertArrayNotHasKey('native:model', $options);
$this->assertArrayNotHasKey('native:notes', $options);
diff --git a/tests/Feature/SyncAdapters/KaseyaVsa10/KaseyaVsa10CustomFieldsTest.php b/tests/Feature/SyncAdapters/KaseyaVsa10/KaseyaVsa10CustomFieldsTest.php
index 4d7ab74a90..a8bc6f349c 100644
--- a/tests/Feature/SyncAdapters/KaseyaVsa10/KaseyaVsa10CustomFieldsTest.php
+++ b/tests/Feature/SyncAdapters/KaseyaVsa10/KaseyaVsa10CustomFieldsTest.php
@@ -84,15 +84,10 @@ class KaseyaVsa10CustomFieldsTest extends TestCase
$this->assertArrayHasKey('native:model', $adapterExtras);
$this->assertArrayHasKey('native:notes', $adapterExtras);
- // Admin-defined extras drop them. Skip + custom fields only.
$this->assertArrayNotHasKey('native:asset_tag', $adminExtras);
$this->assertArrayNotHasKey('native:model', $adminExtras);
$this->assertArrayNotHasKey('native:notes', $adminExtras);
- $this->assertArrayHasKey('skip', $adminExtras);
foreach (array_keys($adminExtras) as $target) {
- if ($target === 'skip') {
- continue;
- }
$this->assertStringStartsWith('custom:', $target);
}
}
diff --git a/tests/Feature/SyncAdapters/Landscape/LandscapeAdapterTest.php b/tests/Feature/SyncAdapters/Landscape/LandscapeAdapterTest.php
index f5d39fffe9..6a955db684 100644
--- a/tests/Feature/SyncAdapters/Landscape/LandscapeAdapterTest.php
+++ b/tests/Feature/SyncAdapters/Landscape/LandscapeAdapterTest.php
@@ -13,13 +13,6 @@ use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
-/**
- * End-to-end coverage for the Landscape adapter through SyncAdapter.
- * Mocks Landscape's /api/v2/computers endpoint, asserts assets +
- * asset_external_sources rows land correctly, exercises the
- * grouped_hardware normalization for both flat and list shapes, and
- * verifies the `next` URL pagination loop terminates.
- */
class LandscapeAdapterTest extends TestCase
{
protected function setUp(): void
@@ -247,11 +240,10 @@ class LandscapeAdapterTest extends TestCase
public function test_does_not_adopt_when_toggle_is_off()
{
- // Default behavior: matching serial without an
- // asset_external_sources row does NOT auto-adopt. The
- // sync creates a fresh shell asset alongside the existing
- // one. Verifies we don't regress the safe default.
+
$adapter = $this->configuredLandscapeAdapter();
+ $instance = SyncAdapterInstance::where('slug', 'landscape')->firstOrFail();
+ SyncAdapterConfig::put($instance->id, 'adopt_by_serial', '0');
Asset::factory()->create(['serial' => 'NO-ADOPT-001', 'name' => 'pre-existing']);
Http::fake([