snipe-it/tests/Feature/Checkins/AssetCheckinTest.php

161 lines
5.8 KiB
PHP
Raw Normal View History

2023-08-21 12:31:51 -07:00
<?php
2024-02-12 17:54:22 -08:00
namespace Tests\Feature\Checkins;
2023-08-21 12:31:51 -07:00
2024-02-13 12:03:27 -08:00
use App\Events\CheckoutableCheckedIn;
2023-08-21 12:31:51 -07:00
use App\Models\Asset;
2024-02-13 14:30:26 -08:00
use App\Models\CheckoutAcceptance;
2024-02-13 17:04:53 -08:00
use App\Models\LicenseSeat;
use App\Models\Location;
2024-02-13 13:17:02 -08:00
use App\Models\Statuslabel;
2023-08-21 12:31:51 -07:00
use App\Models\User;
2024-02-13 12:03:27 -08:00
use Illuminate\Support\Facades\Event;
2023-08-21 12:31:51 -07:00
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
class AssetCheckinTest extends TestCase
{
use InteractsWithSettings;
2024-02-12 17:54:22 -08:00
public function testCheckingInAssetRequiresCorrectPermission()
{
$this->actingAs(User::factory()->create())
->post(route('hardware.checkin.store', [
'assetId' => Asset::factory()->assignedToUser()->create()->id,
]))
->assertForbidden();
}
2024-02-13 12:35:31 -08:00
public function testCannotCheckInAssetThatIsNotCheckedOut()
{
2024-02-13 13:34:55 -08:00
$this->actingAs(User::factory()->checkinAssets()->create())
->post(route('hardware.checkin.store', ['assetId' => Asset::factory()->create()->id]))
->assertSessionHas('error')
->assertRedirect(route('hardware.index'));
2024-02-13 12:35:31 -08:00
}
2024-02-13 13:29:54 -08:00
public function testAssetCanBeCheckedIn()
2024-02-12 17:54:22 -08:00
{
2024-02-13 12:03:27 -08:00
Event::fake([CheckoutableCheckedIn::class]);
$user = User::factory()->create();
2024-02-14 12:14:27 -08:00
$location = Location::factory()->create();
2024-02-13 13:17:02 -08:00
$status = Statuslabel::first() ?? Statuslabel::factory()->create();
$asset = Asset::factory()->assignedToUser($user)->create([
'expected_checkin' => now()->addDay(),
'last_checkin' => null,
'accepted' => 'accepted',
]);
2024-02-13 12:03:27 -08:00
$this->assertTrue($asset->assignedTo->is($user));
2024-02-14 10:48:49 -08:00
$this->actingAs(User::factory()->checkinAssets()->create())
2024-02-13 13:17:02 -08:00
->post(
route('hardware.checkin.store', ['assetId' => $asset->id, 'backto' => 'user']),
[
'name' => 'Changed Name',
'status_id' => $status->id,
2024-02-14 12:14:27 -08:00
'location_id' => $location->id,
2024-02-13 13:17:02 -08:00
],
)
2024-02-13 12:35:31 -08:00
->assertRedirect(route('users.show', $user));
2024-02-13 12:03:27 -08:00
Event::assertDispatched(CheckoutableCheckedIn::class, 1);
2024-02-13 13:17:02 -08:00
$this->assertNull($asset->refresh()->assignedTo);
$this->assertNull($asset->expected_checkin);
$this->assertNull($asset->last_checkout);
$this->assertNotNull($asset->last_checkin);
$this->assertNull($asset->assignedTo);
$this->assertNull($asset->assigned_type);
$this->assertNull($asset->accepted);
$this->assertEquals('Changed Name', $asset->name);
$this->assertEquals($status->id, $asset->status_id);
2024-02-14 12:14:27 -08:00
$this->assertTrue($asset->location()->is($location));
2024-02-13 13:17:02 -08:00
}
2024-02-13 17:04:53 -08:00
public function testLocationIsSetToRTDLocationByDefaultUponCheckin()
{
$rtdLocation = Location::factory()->create();
$asset = Asset::factory()->assignedToUser()->create([
'location_id' => Location::factory()->create()->id,
'rtd_location_id' => $rtdLocation->id,
]);
$this->actingAs(User::factory()->checkinAssets()->create())
->post(route('hardware.checkin.store', ['assetId' => $asset->id]));
$this->assertTrue($asset->refresh()->location()->is($rtdLocation));
}
public function testDefaultLocationCanBeUpdatedUponCheckin()
{
$location = Location::factory()->create();
$asset = Asset::factory()->assignedToUser()->create();
$this->actingAs(User::factory()->checkinAssets()->create())
->post(route('hardware.checkin.store', ['assetId' => $asset->id]), [
'location_id' => $location->id,
'update_default_location' => 0
]);
$this->assertTrue($asset->refresh()->defaultLoc()->is($location));
}
public function testAssetsLicenseSeatsAreClearedUponCheckin()
{
$asset = Asset::factory()->assignedToUser()->create();
LicenseSeat::factory()->assignedToUser()->for($asset)->create();
$this->assertNotNull($asset->licenseseats->first()->assigned_to);
$this->actingAs(User::factory()->checkinAssets()->create())
->post(route('hardware.checkin.store', ['assetId' => $asset->id]));
$this->assertNull($asset->refresh()->licenseseats->first()->assigned_to);
}
public function testLegacyLocationValuesSetToZeroAreUpdated()
{
$asset = Asset::factory()->canBeInvalidUponCreation()->assignedToUser()->create([
'rtd_location_id' => 0,
'location_id' => 0,
]);
$this->actingAs(User::factory()->checkinAssets()->create())
->post(route('hardware.checkin.store', ['assetId' => $asset->id]));
$this->assertNull($asset->refresh()->rtd_location_id);
2024-02-14 12:27:42 -08:00
$this->assertEquals($asset->location_id, $asset->rtd_location_id);
2024-02-13 17:04:53 -08:00
}
2024-02-13 12:35:31 -08:00
public function testPendingCheckoutAcceptancesAreClearedUponCheckin()
{
2024-02-13 14:30:26 -08:00
$asset = Asset::factory()->assignedToUser()->create();
$acceptance = CheckoutAcceptance::factory()->for($asset, 'checkoutable')->pending()->create();
$this->actingAs(User::factory()->checkinAssets()->create())
->post(route('hardware.checkin.store', ['assetId' => $asset->id]));
$this->assertFalse($acceptance->exists(), 'Acceptance was not deleted');
2024-02-13 12:35:31 -08:00
}
2024-02-13 13:34:55 -08:00
public function testCheckinTimeAndActionLogNoteCanBeSet()
{
Event::fake();
$this->actingAs(User::factory()->checkinAssets()->create())
->post(route(
'hardware.checkin.store',
['assetId' => Asset::factory()->assignedToUser()->create()->id]
), [
2024-02-14 11:33:03 -08:00
'checkin_at' => '2023-01-02 12:34:56',
2024-02-13 13:34:55 -08:00
'note' => 'hello'
]);
Event::assertDispatched(function (CheckoutableCheckedIn $event) {
2024-02-14 11:33:03 -08:00
return $event->action_date === '2023-01-02 12:34:56' && $event->note === 'hello';
2024-02-13 13:34:55 -08:00
}, 1);
}
2023-08-21 12:31:51 -07:00
}