diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index 6699fd87e6..373d2e3c17 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -545,7 +545,7 @@ class AssetsController extends Controller $asset->user_id = Auth::id(); // @todo: verify eol is working as expected - move to request? or do I want this at all? // might just remove this and then use the method from here https://github.com/snipe/snipe-it/pull/13846 - //$asset->asset_eol_date = $request->validated()['asset_eol_date'] ?? $asset->present()->eol_date(); + $asset->asset_eol_date = $request->validated()['asset_eol_date'] ?? $asset->present()->eol_date(); $asset->archived = '0'; $asset->physical = '1'; $asset->depreciate = '0'; diff --git a/tests/Feature/Api/Assets/AssetStoreTest.php b/tests/Feature/Api/Assets/AssetStoreTest.php index e423745176..4089dedc37 100644 --- a/tests/Feature/Api/Assets/AssetStoreTest.php +++ b/tests/Feature/Api/Assets/AssetStoreTest.php @@ -95,6 +95,66 @@ class AssetStoreTest extends TestCase $this->assertEquals(10, $asset->warranty_months); } + public function testAssetEolDateIsCalculatedIfPurchaseDateSet() + { + $model = AssetModel::factory()->mbp13Model()->create(); + $status = Statuslabel::factory()->create(); + + $this->settings->enableAutoIncrement(); + + $this->actingAsForApi(User::factory()->superuser()->create()) + ->postJson(route('api.assets.store'), [ + 'model_id' => $model->id, + 'purchase_date' => '2021-01-01', + 'status_id' => $status->id, + ]) + ->assertOk() + ->assertStatusMessageIs('success'); + + $asset = Asset::first(); + $this->assertEquals('2024-01-01', $asset->asset_eol_date); + } + + public function testAssetEolDateIsNotCalculatedIfPurchaseDateNotSet() + { + $model = AssetModel::factory()->mbp13Model()->create(); + $status = Statuslabel::factory()->create(); + + $this->settings->enableAutoIncrement(); + + $this->actingAsForApi(User::factory()->superuser()->create()) + ->postJson(route('api.assets.store'), [ + 'model_id' => $model->id, + 'status_id' => $status->id, + ]) + ->assertOk() + ->assertStatusMessageIs('success'); + + $asset = Asset::first(); + $this->assertNull($asset->asset_eol_date); + } + + public function testAssetEolExplicitIsSetIfAssetEolDateIsSet() + { + $model = AssetModel::factory()->mbp13Model()->create(); + $status = Statuslabel::factory()->create(); + + $this->settings->enableAutoIncrement(); + + $this->actingAsForApi(User::factory()->superuser()->create()) + ->postJson(route('api.assets.store'), [ + 'model_id' => $model->id, + 'asset_eol_date' => '2025-01-01', + 'status_id' => $status->id, + ]) + ->assertOk() + ->assertStatusMessageIs('success'); + + $asset = Asset::first(); + $this->assertEquals('2025-01-01', $asset->asset_eol_date); + $this->assertTrue($asset->eol_explicit); + } + public function testAssetGetsAssetTagWithAutoIncrement() { $model = AssetModel::factory()->create();