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-22 13:33:16 -08:00
|
|
|
use App\Models\CheckoutAcceptance;
|
2024-02-14 11:33:03 -08:00
|
|
|
use App\Models\LicenseSeat;
|
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-22 13:50:46 -08:00
|
|
|
use Illuminate\Support\Carbon;
|
2024-02-14 10:48:49 -08:00
|
|
|
use Illuminate\Support\Facades\Event;
|
2023-08-21 12:31:51 -07:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class AssetCheckinTest extends TestCase
|
|
|
|
{
|
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();
|
2024-02-14 12:14:27 -08:00
|
|
|
$location = Location::factory()->create();
|
2024-02-14 10:48:49 -08:00
|
|
|
$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));
|
|
|
|
|
2024-02-22 13:50:46 -08:00
|
|
|
$currentTimestamp = now();
|
|
|
|
|
2024-02-14 10:48:49 -08:00
|
|
|
$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,
|
2024-02-14 12:14:27 -08:00
|
|
|
'location_id' => $location->id,
|
2024-02-14 10:48:49 -08:00
|
|
|
])
|
|
|
|
->assertOk();
|
|
|
|
|
|
|
|
$this->assertNull($asset->refresh()->assignedTo);
|
|
|
|
$this->assertNull($asset->expected_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-22 13:50:46 -08:00
|
|
|
|
|
|
|
Event::assertDispatched(function (CheckoutableCheckedIn $event) use ($currentTimestamp) {
|
|
|
|
// this could be better mocked but is ok for now.
|
|
|
|
return Carbon::parse($event->action_date)->diffInSeconds($currentTimestamp) < 2;
|
|
|
|
}, 1);
|
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 testDefaultLocationCanBeUpdatedUponCheckin()
|
|
|
|
{
|
2024-02-14 11:33:03 -08:00
|
|
|
$location = Location::factory()->create();
|
|
|
|
$asset = Asset::factory()->assignedToUser()->create();
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->checkinAssets()->create())
|
|
|
|
->postJson(route('api.asset.checkin', $asset), [
|
|
|
|
'location_id' => $location->id,
|
2024-02-27 12:23:26 -08:00
|
|
|
'update_default_location' => true,
|
2024-02-14 11:33:03 -08:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertTrue($asset->refresh()->defaultLoc()->is($location));
|
2024-02-14 11:11:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAssetsLicenseSeatsAreClearedUponCheckin()
|
|
|
|
{
|
2024-02-14 11:33:03 -08:00
|
|
|
$asset = Asset::factory()->assignedToUser()->create();
|
|
|
|
LicenseSeat::factory()->assignedToUser()->for($asset)->create();
|
|
|
|
|
|
|
|
$this->assertNotNull($asset->licenseseats->first()->assigned_to);
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->checkinAssets()->create())
|
|
|
|
->postJson(route('api.asset.checkin', $asset));
|
|
|
|
|
|
|
|
$this->assertNull($asset->refresh()->licenseseats->first()->assigned_to);
|
2024-02-14 11:11:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLegacyLocationValuesSetToZeroAreUpdated()
|
|
|
|
{
|
2024-02-22 13:14:30 -08:00
|
|
|
$asset = Asset::factory()->canBeInvalidUponCreation()->assignedToUser()->create([
|
|
|
|
'rtd_location_id' => 0,
|
|
|
|
'location_id' => 0,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->checkinAssets()->create())
|
|
|
|
->postJson(route('api.asset.checkin', $asset));
|
|
|
|
|
|
|
|
$this->assertNull($asset->refresh()->rtd_location_id);
|
|
|
|
$this->assertEquals($asset->location_id, $asset->rtd_location_id);
|
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
|
|
|
{
|
2024-02-22 13:33:16 -08:00
|
|
|
$asset = Asset::factory()->assignedToUser()->create();
|
|
|
|
|
|
|
|
$acceptance = CheckoutAcceptance::factory()->for($asset, 'checkoutable')->pending()->create();
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->checkinAssets()->create())
|
|
|
|
->postJson(route('api.asset.checkin', $asset));
|
|
|
|
|
|
|
|
$this->assertFalse($acceptance->exists(), 'Acceptance was not deleted');
|
2024-02-14 11:11:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCheckinTimeAndActionLogNoteCanBeSet()
|
2024-02-13 12:35:31 -08:00
|
|
|
{
|
2024-02-14 11:33:03 -08:00
|
|
|
Event::fake();
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->checkinAssets()->create())
|
|
|
|
->postJson(route('api.asset.checkin', Asset::factory()->assignedToUser()->create()), [
|
2024-02-27 11:12:35 -08:00
|
|
|
// time is appended to the provided date in controller
|
|
|
|
'checkin_at' => '2023-01-02',
|
2024-02-14 11:33:03 -08:00
|
|
|
'note' => 'hi there',
|
|
|
|
]);
|
|
|
|
|
|
|
|
Event::assertDispatched(function (CheckoutableCheckedIn $event) {
|
2024-02-27 11:12:35 -08:00
|
|
|
return Carbon::parse('2023-01-02')->isSameDay(Carbon::parse($event->action_date))
|
|
|
|
&& $event->note === 'hi there';
|
2024-02-14 11:33:03 -08:00
|
|
|
}, 1);
|
2024-02-13 12:35:31 -08:00
|
|
|
}
|
2023-08-21 12:31:51 -07:00
|
|
|
}
|