Skip to content

A free open source IT asset/license management system

Added tests

snipe6299fc09bf19
diff --git a/tests/Feature/Checkins/Ui/AssetCheckinTest.php b/tests/Feature/Checkins/Ui/AssetCheckinTest.php
index cb2cea3852..0419ada295 100644
--- a/tests/Feature/Checkins/Ui/AssetCheckinTest.php
+++ b/tests/Feature/Checkins/Ui/AssetCheckinTest.php
@@ -47,6 +47,79 @@ class AssetCheckinTest extends TestCase
->assertOk();
}
+ public function test_requestable_toggle_is_hidden_on_checkin_page_without_old_status_selection()
+ {
+ $deployableStatus = Statuslabel::factory()->readyToDeploy()->create();
+ $asset = Asset::factory()->assignedToUser()->create([
+ 'status_id' => $deployableStatus->id,
+ ]);
+
+ $response = $this->actingAs(User::factory()->checkinAssets()->create())
+ ->get(route('hardware.checkin.create', $asset))
+ ->assertOk();
+
+ $content = $response->getContent();
+
+ $this->assertStringContainsString('id="set-requestable-wrapper"', $content);
+ $this->assertMatchesRegularExpression(
+ '/id="set-requestable-wrapper"(?:(?!>).)*style="display:\s*none;"/s',
+ $content
+ );
+ }
+
+ public function test_requestable_toggle_is_hidden_on_checkin_page_for_non_deployable_status()
+ {
+ $nonDeployableStatus = Statuslabel::factory()->create(['deployable' => 0]);
+ $asset = Asset::factory()->assignedToUser()->create([
+ 'status_id' => $nonDeployableStatus->id,
+ ]);
+
+ $response = $this->actingAs(User::factory()->checkinAssets()->create())
+ ->get(route('hardware.checkin.create', $asset))
+ ->assertOk();
+
+ $content = $response->getContent();
+
+ $this->assertMatchesRegularExpression(
+ '/id="set-requestable-wrapper"(?:(?!>).)*style="display:\s*none;"/s',
+ $content
+ );
+ }
+
+ public function test_requestable_toggle_visibility_prefers_old_input_status_id_when_present()
+ {
+ $deployableStatus = Statuslabel::factory()->readyToDeploy()->create();
+ $nonDeployableStatus = Statuslabel::factory()->create(['deployable' => 0]);
+
+ $asset = Asset::factory()->assignedToUser()->create([
+ 'status_id' => $nonDeployableStatus->id,
+ ]);
+
+ $responseWithDeployableOldInput = $this->actingAs(User::factory()->checkinAssets()->create())
+ ->withSession(['_old_input' => ['status_id' => (string) $deployableStatus->id]])
+ ->get(route('hardware.checkin.create', $asset))
+ ->assertOk();
+
+ $this->assertDoesNotMatchRegularExpression(
+ '/id="set-requestable-wrapper"(?:(?!>).)*style="display:\s*none;"/s',
+ $responseWithDeployableOldInput->getContent()
+ );
+
+ $assetWithDeployableStatus = Asset::factory()->assignedToUser()->create([
+ 'status_id' => $deployableStatus->id,
+ ]);
+
+ $responseWithNonDeployableOldInput = $this->actingAs(User::factory()->checkinAssets()->create())
+ ->withSession(['_old_input' => ['status_id' => (string) $nonDeployableStatus->id]])
+ ->get(route('hardware.checkin.create', $assetWithDeployableStatus))
+ ->assertOk();
+
+ $this->assertMatchesRegularExpression(
+ '/id="set-requestable-wrapper"(?:(?!>).)*style="display:\s*none;"/s',
+ $responseWithNonDeployableOldInput->getContent()
+ );
+ }
+
public function test_asset_can_be_checked_in()
{
Event::fake([CheckoutableCheckedIn::class]);
@@ -175,6 +248,38 @@ class AssetCheckinTest extends TestCase
}, 1);
}
+ public function test_checkin_can_set_asset_to_requestable_when_status_is_deployable()
+ {
+ $deployableStatus = Statuslabel::factory()->readyToDeploy()->create();
+ $asset = Asset::factory()->assignedToUser()->create([
+ 'requestable' => 0,
+ ]);
+
+ $this->actingAs(User::factory()->checkinAssets()->create())
+ ->post(route('hardware.checkin.store', [$asset]), [
+ 'status_id' => $deployableStatus->id,
+ 'set_requestable' => 1,
+ ]);
+
+ $this->assertTrue((bool) $asset->fresh()->requestable);
+ }
+
+ public function test_checkin_does_not_set_asset_to_requestable_when_status_is_not_deployable()
+ {
+ $undeployableStatus = Statuslabel::factory()->create(['deployable' => 0]);
+ $asset = Asset::factory()->assignedToUser()->create([
+ 'requestable' => 0,
+ ]);
+
+ $this->actingAs(User::factory()->checkinAssets()->create())
+ ->post(route('hardware.checkin.store', [$asset]), [
+ 'status_id' => $undeployableStatus->id,
+ 'set_requestable' => 1,
+ ]);
+
+ $this->assertFalse((bool) $asset->fresh()->requestable);
+ }
+
public function test_asset_checkin_page_is_redirected_if_model_is_invalid()
{
diff --git a/tests/Feature/Checkouts/Api/AssetCheckoutTest.php b/tests/Feature/Checkouts/Api/AssetCheckoutTest.php
index 4feff25663..d83d8307b3 100644
--- a/tests/Feature/Checkouts/Api/AssetCheckoutTest.php
+++ b/tests/Feature/Checkouts/Api/AssetCheckoutTest.php
@@ -230,4 +230,35 @@ class AssetCheckoutTest extends TestCase
$this->assertTrue((int) Carbon::parse($asset->last_checkout)->diffInSeconds(now(), true) < 2);
}
+
+ public function test_api_checkout_can_update_requestable_when_field_is_passed()
+ {
+ $asset = Asset::factory()->create(['requestable' => 1]);
+ $targetUser = User::factory()->create();
+
+ $this->actingAsForApi(User::factory()->checkoutAssets()->create())
+ ->postJson(route('api.asset.checkout', $asset), [
+ 'checkout_to_type' => 'user',
+ 'assigned_user' => $targetUser->id,
+ 'requestable' => 0,
+ ])
+ ->assertStatusMessageIs('success');
+
+ $this->assertFalse((bool) $asset->fresh()->requestable);
+ }
+
+ public function test_api_checkout_leaves_requestable_unchanged_when_field_is_omitted()
+ {
+ $asset = Asset::factory()->create(['requestable' => 1]);
+ $targetUser = User::factory()->create();
+
+ $this->actingAsForApi(User::factory()->checkoutAssets()->create())
+ ->postJson(route('api.asset.checkout', $asset), [
+ 'checkout_to_type' => 'user',
+ 'assigned_user' => $targetUser->id,
+ ])
+ ->assertStatusMessageIs('success');
+
+ $this->assertTrue((bool) $asset->fresh()->requestable);
+ }
}
diff --git a/tests/Feature/Checkouts/Ui/AssetCheckoutTest.php b/tests/Feature/Checkouts/Ui/AssetCheckoutTest.php
index c776e9f4a7..a3f0b16826 100644
--- a/tests/Feature/Checkouts/Ui/AssetCheckoutTest.php
+++ b/tests/Feature/Checkouts/Ui/AssetCheckoutTest.php
@@ -239,6 +239,21 @@ class AssetCheckoutTest extends TestCase
$this->assertTrue($user->fresh()->licenses->contains($seat->license));
}
+ public function test_checkout_can_set_asset_to_not_requestable()
+ {
+ $asset = Asset::factory()->create(['requestable' => 1]);
+ $targetUser = User::factory()->create();
+
+ $this->actingAs(User::factory()->checkoutAssets()->create())
+ ->post(route('hardware.checkout.store', $asset), [
+ 'checkout_to_type' => 'user',
+ 'assigned_user' => $targetUser->id,
+ 'set_not_requestable' => 1,
+ ]);
+
+ $this->assertFalse((bool) $asset->fresh()->requestable);
+ }
+
public function test_last_checkout_uses_current_date_if_not_provided()
{
$asset = Asset::factory()->create(['last_checkout' => now()->subMonth()]);
diff --git a/tests/Feature/Checkouts/Ui/BulkAssetCheckoutTest.php b/tests/Feature/Checkouts/Ui/BulkAssetCheckoutTest.php
index 684cc05136..70bedd826c 100644
--- a/tests/Feature/Checkouts/Ui/BulkAssetCheckoutTest.php
+++ b/tests/Feature/Checkouts/Ui/BulkAssetCheckoutTest.php
@@ -100,6 +100,24 @@ class BulkAssetCheckoutTest extends TestCase
}
}
+ public function test_bulk_checkout_can_set_assets_to_not_requestable()
+ {
+ $assets = Asset::factory()->count(2)->create(['requestable' => 1]);
+ $targetUser = User::factory()->create();
+
+ $this->actingAs(User::factory()->checkoutAssets()->create())
+ ->post(route('hardware.bulkcheckout.store'), [
+ 'selected_assets' => $assets->pluck('id')->toArray(),
+ 'checkout_to_type' => 'user',
+ 'assigned_user' => $targetUser->id,
+ 'set_not_requestable' => 1,
+ ]);
+
+ $assets->each(function (Asset $asset) {
+ $this->assertFalse((bool) $asset->fresh()->requestable);
+ });
+ }
+
public static function checkoutTargets()
{
yield 'Checkout to user' => [