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

121 lines
3.4 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;
use App\Models\User;
2024-02-13 12:03:27 -08:00
use App\Notifications\CheckinAssetNotification;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Notification;
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:15:59 -08:00
public function testAssetCheckedOutToAssetCanBeCheckedIn()
{
$this->markTestIncomplete();
}
public function testAssetCheckedOutToLocationCanBeCheckedIn()
{
$this->markTestIncomplete();
}
public function testAssetCheckedOutToUserCanBeCheckedIn()
2024-02-12 17:54:22 -08:00
{
2024-02-13 12:03:27 -08:00
Event::fake([CheckoutableCheckedIn::class]);
$admin = User::factory()->checkinAssets()->create();
$user = User::factory()->create();
2024-02-13 12:15:59 -08:00
$asset = Asset::factory()->assignedToUser($user)->create();
2024-02-13 12:03:27 -08:00
$this->assertTrue($asset->assignedTo->is($user));
$this->actingAs($admin)
->post(route('hardware.checkin.store', ['assetId' => $asset->id]))
->assertRedirect();
$this->assertNull($asset->fresh()->assignedTo);
Event::assertDispatched(CheckoutableCheckedIn::class, 1);
2024-02-12 17:54:22 -08:00
}
public function testCheckInEmailSentToUserIfSettingEnabled()
{
2024-02-13 12:03:27 -08:00
Notification::fake();
$user = User::factory()->create();
$asset = Asset::factory()->assignedToUser($user)->create();
$asset->model->category->update(['checkin_email' => true]);
event(new CheckoutableCheckedIn(
$asset,
$user,
User::factory()->checkinAssets()->create(),
''
));
Notification::assertSentTo(
[$user],
function (CheckinAssetNotification $notification, $channels) {
return in_array('mail', $channels);
},
);
2024-02-12 17:54:22 -08:00
}
public function testCheckInEmailNotSentToUserIfSettingDisabled()
{
2024-02-13 12:03:27 -08:00
Notification::fake();
$user = User::factory()->create();
$asset = Asset::factory()->assignedToUser($user)->create();
$asset->model->category->update(['checkin_email' => false]);
event(new CheckoutableCheckedIn(
$asset,
$user,
User::factory()->checkinAssets()->create(),
''
));
Notification::assertNotSentTo(
[$user],
function (CheckinAssetNotification $notification, $channels) {
return in_array('mail', $channels);
}
);
2024-02-12 17:54:22 -08:00
}
2023-08-21 12:31:51 -07:00
public function testLastCheckInFieldIsSetOnCheckin()
{
$admin = User::factory()->superuser()->create();
$asset = Asset::factory()->create(['last_checkin' => null]);
$asset->checkOut(User::factory()->create(), $admin, now());
$this->actingAs($admin)
->post(route('hardware.checkin.store', [
'assetId' => $asset->id,
2024-02-13 12:03:27 -08:00
]));
2023-08-21 12:31:51 -07:00
$this->assertNotNull(
$asset->fresh()->last_checkin,
'last_checkin field should be set on checkin'
);
}
}