snipe-it/tests/Feature/Api/Assets/AssetCheckinTest.php

124 lines
4 KiB
PHP
Raw Normal View History

2023-08-21 12:31:51 -07:00
<?php
namespace Tests\Feature\Api\Assets;
2024-02-14 10:48:49 -08:00
use App\Events\CheckoutableCheckedIn;
2023-08-21 12:31:51 -07:00
use App\Models\Asset;
2024-02-14 11:11:08 -08:00
use App\Models\Location;
2024-02-14 10:48:49 -08:00
use App\Models\Statuslabel;
2023-08-21 12:31:51 -07:00
use App\Models\User;
2024-02-14 10:48:49 -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-13 12:15:59 -08:00
public function testCheckingInAssetRequiresCorrectPermission()
{
2024-02-13 17:50:26 -08:00
$this->actingAsForApi(User::factory()->create())
->postJson(route('api.asset.checkin', Asset::factory()->assignedToUser()->create()))
->assertForbidden();
2024-02-13 12:15:59 -08:00
}
2024-02-13 12:35:31 -08:00
public function testCannotCheckInNonExistentAsset()
2024-02-13 12:15:59 -08:00
{
2024-02-13 17:50:26 -08:00
$this->actingAsForApi(User::factory()->checkinAssets()->create())
->postJson(route('api.asset.checkin', ['id' => 'does-not-exist']))
->assertStatusMessageIs('error');
2024-02-13 12:15:59 -08:00
}
2024-02-13 12:35:31 -08:00
public function testCannotCheckInAssetThatIsNotCheckedOut()
2024-02-13 12:15:59 -08:00
{
2024-02-13 17:50:26 -08:00
$this->actingAsForApi(User::factory()->checkinAssets()->create())
->postJson(route('api.asset.checkin', Asset::factory()->create()->id))
->assertStatusMessageIs('error');
2024-02-13 12:15:59 -08:00
}
2024-02-13 17:50:26 -08:00
public function testAssetCanBeCheckedIn()
2024-02-13 12:15:59 -08:00
{
2024-02-14 10:48:49 -08:00
Event::fake([CheckoutableCheckedIn::class]);
$user = User::factory()->create();
$status = Statuslabel::factory()->create();
$asset = Asset::factory()->assignedToUser($user)->create([
'expected_checkin' => now()->addDay(),
'last_checkin' => null,
'accepted' => 'accepted',
]);
$this->assertTrue($asset->assignedTo->is($user));
$this->actingAsForApi(User::factory()->checkinAssets()->create())
2024-02-14 11:11:08 -08:00
->postJson(route('api.asset.checkin', $asset), [
2024-02-14 10:48:49 -08:00
'name' => 'Changed Name',
'status_id' => $status->id,
])
->assertOk();
Event::assertDispatched(CheckoutableCheckedIn::class, 1);
$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-13 12:15:59 -08:00
}
2024-02-14 11:11:08 -08:00
public function testLocationIsSetToRTDLocationByDefaultUponCheckin()
2023-08-21 12:31:51 -07:00
{
2024-02-14 11:11:08 -08:00
$rtdLocation = Location::factory()->create();
$asset = Asset::factory()->assignedToUser()->create([
'location_id' => Location::factory()->create()->id,
'rtd_location_id' => $rtdLocation->id,
]);
2023-08-21 12:31:51 -07:00
2024-02-14 11:11:08 -08:00
$this->actingAsForApi(User::factory()->checkinAssets()->create())
->postJson(route('api.asset.checkin', $asset->id));
$this->assertTrue($asset->refresh()->location()->is($rtdLocation));
}
public function testLocationCanBeSetUponCheckin()
{
$location = Location::factory()->create();
$asset = Asset::factory()->assignedToUser()->create();
$this->actingAsForApi(User::factory()->checkinAssets()->create())
->postJson(route('api.asset.checkin', $asset->id), [
'location_id' => $location->id,
]);
$this->assertTrue($asset->refresh()->location()->is($location));
}
2023-08-21 12:31:51 -07:00
2024-02-14 11:11:08 -08:00
public function testDefaultLocationCanBeUpdatedUponCheckin()
{
$this->markTestIncomplete();
}
public function testAssetsLicenseSeatsAreClearedUponCheckin()
{
$this->markTestIncomplete();
}
public function testLegacyLocationValuesSetToZeroAreUpdated()
{
$this->markTestIncomplete();
2023-08-21 12:31:51 -07:00
}
2024-02-13 12:35:31 -08:00
public function testPendingCheckoutAcceptancesAreClearedUponCheckin()
2024-02-14 11:11:08 -08:00
{
$this->markTestIncomplete('Not currently in controller');
}
public function testCheckinTimeAndActionLogNoteCanBeSet()
2024-02-13 12:35:31 -08:00
{
$this->markTestIncomplete();
}
2023-08-21 12:31:51 -07:00
}