2024-03-20 13:29:25 -07:00
|
|
|
<?php
|
|
|
|
|
2024-06-04 10:48:53 -07:00
|
|
|
namespace Tests\Feature\Checkouts\Ui;
|
2024-03-20 13:29:25 -07:00
|
|
|
|
2024-08-06 13:25:21 -07:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
2024-03-20 13:29:25 -07:00
|
|
|
use App\Events\CheckoutableCheckedOut;
|
2024-07-26 07:07:43 -07:00
|
|
|
use App\Models\Accessory;
|
2024-03-20 13:29:25 -07:00
|
|
|
use App\Models\Asset;
|
2024-04-10 15:47:26 -07:00
|
|
|
use App\Models\Company;
|
2024-03-20 17:46:09 -07:00
|
|
|
use App\Models\LicenseSeat;
|
2024-03-20 14:28:27 -07:00
|
|
|
use App\Models\Location;
|
2024-03-20 13:29:25 -07:00
|
|
|
use App\Models\Statuslabel;
|
|
|
|
use App\Models\User;
|
2024-04-10 13:49:07 -07:00
|
|
|
use Illuminate\Support\Carbon;
|
2024-03-20 13:29:25 -07:00
|
|
|
use Illuminate\Support\Facades\Event;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
2024-08-06 13:31:17 -07:00
|
|
|
class AssetCheckoutTest extends TestCase
|
2024-03-20 13:29:25 -07:00
|
|
|
{
|
2024-04-10 16:19:04 -07:00
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
Event::fake([CheckoutableCheckedOut::class]);
|
|
|
|
}
|
|
|
|
|
2024-08-06 13:30:34 -07:00
|
|
|
public function testCheckingOutAssetRequiresCorrectPermission()
|
2024-03-20 13:29:25 -07:00
|
|
|
{
|
|
|
|
$this->actingAs(User::factory()->create())
|
|
|
|
->post(route('hardware.checkout.store', Asset::factory()->create()), [
|
|
|
|
'checkout_to_type' => 'user',
|
|
|
|
'assigned_user' => User::factory()->create()->id,
|
|
|
|
])
|
|
|
|
->assertForbidden();
|
|
|
|
}
|
|
|
|
|
2024-08-06 13:30:34 -07:00
|
|
|
public function testNonExistentAssetCannotBeCheckedOut()
|
2024-03-20 14:28:27 -07:00
|
|
|
{
|
|
|
|
$this->actingAs(User::factory()->checkoutAssets()->create())
|
|
|
|
->post(route('hardware.checkout.store', 1000), [
|
|
|
|
'checkout_to_type' => 'user',
|
|
|
|
'assigned_user' => User::factory()->create()->id,
|
|
|
|
'name' => 'Changed Name',
|
|
|
|
])
|
2024-04-10 16:01:15 -07:00
|
|
|
->assertSessionHas('error')
|
|
|
|
->assertRedirect(route('hardware.index'));
|
2024-03-20 14:28:27 -07:00
|
|
|
|
|
|
|
Event::assertNotDispatched(CheckoutableCheckedOut::class);
|
|
|
|
}
|
|
|
|
|
2024-08-06 13:30:34 -07:00
|
|
|
public function testAssetNotAvailableForCheckoutCannotBeCheckedOut()
|
2024-03-20 14:28:27 -07:00
|
|
|
{
|
2024-04-10 16:28:48 -07:00
|
|
|
$assetAlreadyCheckedOut = Asset::factory()->assignedToUser()->create();
|
2024-03-20 14:28:27 -07:00
|
|
|
|
|
|
|
$this->actingAs(User::factory()->checkoutAssets()->create())
|
2024-04-10 16:28:48 -07:00
|
|
|
->post(route('hardware.checkout.store', $assetAlreadyCheckedOut), [
|
2024-03-20 14:28:27 -07:00
|
|
|
'checkout_to_type' => 'user',
|
|
|
|
'assigned_user' => User::factory()->create()->id,
|
|
|
|
])
|
2024-04-10 16:01:15 -07:00
|
|
|
->assertSessionHas('error')
|
|
|
|
->assertRedirect(route('hardware.index'));
|
2024-03-20 14:28:27 -07:00
|
|
|
|
|
|
|
Event::assertNotDispatched(CheckoutableCheckedOut::class);
|
|
|
|
}
|
|
|
|
|
2024-08-06 13:30:34 -07:00
|
|
|
public function testAssetCannotBeCheckedOutToItself()
|
2024-04-10 17:27:07 -07:00
|
|
|
{
|
2024-05-22 17:21:29 -07:00
|
|
|
$asset = Asset::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->checkoutAssets()->create())
|
|
|
|
->post(route('hardware.checkout.store', $asset), [
|
|
|
|
'checkout_to_type' => 'asset',
|
|
|
|
'assigned_asset' => $asset->id,
|
|
|
|
])
|
|
|
|
->assertSessionHas('error');
|
|
|
|
|
|
|
|
Event::assertNotDispatched(CheckoutableCheckedOut::class);
|
2024-04-10 17:27:07 -07:00
|
|
|
}
|
|
|
|
|
2024-08-06 13:30:34 -07:00
|
|
|
public function testValidationWhenCheckingOutAsset()
|
2024-03-20 13:29:25 -07:00
|
|
|
{
|
|
|
|
$this->actingAs(User::factory()->create())
|
|
|
|
->post(route('hardware.checkout.store', Asset::factory()->create()), [
|
2024-04-10 14:02:25 -07:00
|
|
|
'status_id' => 'does-not-exist',
|
|
|
|
'checkout_at' => 'invalid-date',
|
|
|
|
'expected_checkin' => 'invalid-date',
|
2024-03-20 13:29:25 -07:00
|
|
|
])
|
2024-04-10 14:02:25 -07:00
|
|
|
->assertSessionHasErrors([
|
|
|
|
'assigned_user',
|
|
|
|
'assigned_asset',
|
|
|
|
'assigned_location',
|
|
|
|
'status_id',
|
|
|
|
'checkout_to_type',
|
|
|
|
'checkout_at',
|
|
|
|
'expected_checkin',
|
|
|
|
]);
|
2024-04-10 16:19:04 -07:00
|
|
|
|
|
|
|
Event::assertNotDispatched(CheckoutableCheckedOut::class);
|
|
|
|
}
|
|
|
|
|
2024-08-06 13:30:34 -07:00
|
|
|
public function testCannotCheckoutAcrossCompaniesWhenFullCompanySupportEnabled()
|
2024-04-10 16:19:04 -07:00
|
|
|
{
|
|
|
|
$this->settings->enableMultipleFullCompanySupport();
|
|
|
|
|
|
|
|
$assetCompany = Company::factory()->create();
|
|
|
|
$userCompany = Company::factory()->create();
|
|
|
|
|
|
|
|
$user = User::factory()->for($userCompany)->create();
|
|
|
|
$asset = Asset::factory()->for($assetCompany)->create();
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->superuser()->create())
|
|
|
|
->post(route('hardware.checkout.store', $asset), [
|
|
|
|
'checkout_to_type' => 'user',
|
|
|
|
'assigned_user' => $user->id,
|
|
|
|
])
|
|
|
|
->assertRedirect(route('hardware.checkout.store', $asset));
|
|
|
|
|
|
|
|
Event::assertNotDispatched(CheckoutableCheckedOut::class);
|
2024-03-20 13:29:25 -07:00
|
|
|
}
|
|
|
|
|
2024-04-10 16:28:48 -07:00
|
|
|
/**
|
|
|
|
* This data provider contains checkout targets along with the
|
|
|
|
* asset's expected location after the checkout process.
|
|
|
|
*/
|
2024-08-06 13:25:21 -07:00
|
|
|
public static function checkoutTargets(): array
|
2024-04-10 13:28:52 -07:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'User' => [function () {
|
|
|
|
$userLocation = Location::factory()->create();
|
|
|
|
$user = User::factory()->for($userLocation)->create();
|
|
|
|
|
|
|
|
return [
|
|
|
|
'checkout_type' => 'user',
|
|
|
|
'target' => $user,
|
|
|
|
'expected_location' => $userLocation,
|
|
|
|
];
|
|
|
|
}],
|
|
|
|
'Asset without location set' => [function () {
|
|
|
|
$rtdLocation = Location::factory()->create();
|
|
|
|
$asset = Asset::factory()->for($rtdLocation, 'defaultLoc')->create(['location_id' => null]);
|
|
|
|
|
|
|
|
return [
|
|
|
|
'checkout_type' => 'asset',
|
|
|
|
'target' => $asset,
|
|
|
|
'expected_location' => $rtdLocation,
|
|
|
|
];
|
|
|
|
}],
|
|
|
|
'Asset with location set' => [function () {
|
|
|
|
$rtdLocation = Location::factory()->create();
|
|
|
|
$location = Location::factory()->create();
|
|
|
|
$asset = Asset::factory()->for($location)->for($rtdLocation, 'defaultLoc')->create();
|
|
|
|
|
|
|
|
return [
|
|
|
|
'checkout_type' => 'asset',
|
|
|
|
'target' => $asset,
|
|
|
|
'expected_location' => $location,
|
|
|
|
];
|
|
|
|
}],
|
|
|
|
'Location' => [function () {
|
|
|
|
$location = Location::factory()->create();
|
|
|
|
|
|
|
|
return [
|
|
|
|
'checkout_type' => 'location',
|
|
|
|
'target' => $location,
|
|
|
|
'expected_location' => $location,
|
|
|
|
];
|
|
|
|
}],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-08-06 13:25:21 -07:00
|
|
|
#[DataProvider('checkoutTargets')]
|
2024-08-06 13:30:34 -07:00
|
|
|
public function testAssetCanBeCheckedOut($data)
|
2024-03-20 13:29:25 -07:00
|
|
|
{
|
2024-04-10 13:28:52 -07:00
|
|
|
['checkout_type' => $type, 'target' => $target, 'expected_location' => $expectedLocation] = $data();
|
|
|
|
|
2024-04-10 16:28:48 -07:00
|
|
|
$newStatus = Statuslabel::factory()->readyToDeploy()->create();
|
|
|
|
$asset = Asset::factory()->create();
|
2024-03-20 13:44:26 -07:00
|
|
|
$admin = User::factory()->checkoutAssets()->create();
|
2024-03-20 13:29:25 -07:00
|
|
|
|
|
|
|
$this->actingAs($admin)
|
|
|
|
->post(route('hardware.checkout.store', $asset), [
|
2024-04-10 13:28:52 -07:00
|
|
|
'checkout_to_type' => $type,
|
|
|
|
'assigned_' . $type => $target->id,
|
2024-03-20 13:29:25 -07:00
|
|
|
'name' => 'Changed Name',
|
2024-04-10 16:28:48 -07:00
|
|
|
'status_id' => $newStatus->id,
|
2024-03-20 13:29:25 -07:00
|
|
|
'checkout_at' => '2024-03-18',
|
|
|
|
'expected_checkin' => '2024-03-28',
|
|
|
|
'note' => 'An awesome note',
|
|
|
|
]);
|
|
|
|
|
2024-03-20 14:28:27 -07:00
|
|
|
$asset->refresh();
|
2024-04-10 13:28:52 -07:00
|
|
|
$this->assertTrue($asset->assignedTo()->is($target));
|
|
|
|
$this->assertTrue($asset->location->is($expectedLocation));
|
2024-04-10 11:43:50 -07:00
|
|
|
$this->assertEquals('Changed Name', $asset->name);
|
2024-04-10 16:28:48 -07:00
|
|
|
$this->assertTrue($asset->assetstatus->is($newStatus));
|
2024-04-10 11:43:50 -07:00
|
|
|
$this->assertEquals('2024-03-18 00:00:00', $asset->last_checkout);
|
|
|
|
$this->assertEquals('2024-03-28 00:00:00', (string)$asset->expected_checkin);
|
2024-03-20 17:46:09 -07:00
|
|
|
|
2024-04-10 11:52:59 -07:00
|
|
|
Event::assertDispatched(CheckoutableCheckedOut::class, 1);
|
2024-04-10 13:28:52 -07:00
|
|
|
Event::assertDispatched(function (CheckoutableCheckedOut $event) use ($admin, $asset, $target) {
|
2024-05-22 17:14:10 -07:00
|
|
|
$this->assertTrue($event->checkoutable->is($asset));
|
|
|
|
$this->assertTrue($event->checkedOutTo->is($target));
|
|
|
|
$this->assertTrue($event->checkedOutBy->is($admin));
|
|
|
|
$this->assertEquals('An awesome note', $event->note);
|
|
|
|
|
|
|
|
return true;
|
2024-03-20 13:44:26 -07:00
|
|
|
});
|
2024-03-20 13:29:25 -07:00
|
|
|
}
|
|
|
|
|
2024-08-06 13:30:34 -07:00
|
|
|
public function testLicenseSeatsAreAssignedToUserUponCheckout()
|
2024-03-20 14:28:27 -07:00
|
|
|
{
|
2024-03-20 17:46:09 -07:00
|
|
|
$asset = Asset::factory()->create();
|
|
|
|
$seat = LicenseSeat::factory()->assignedToAsset($asset)->create();
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
|
|
|
$this->assertFalse($user->licenses->contains($seat->license));
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->checkoutAssets()->create())
|
|
|
|
->post(route('hardware.checkout.store', $asset), [
|
|
|
|
'checkout_to_type' => 'user',
|
|
|
|
'assigned_user' => $user->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertTrue($user->fresh()->licenses->contains($seat->license));
|
2024-03-20 14:28:27 -07:00
|
|
|
}
|
|
|
|
|
2024-08-06 13:30:34 -07:00
|
|
|
public function testLastCheckoutUsesCurrentDateIfNotProvided()
|
2024-03-20 14:28:27 -07:00
|
|
|
{
|
2024-04-10 17:27:07 -07:00
|
|
|
$asset = Asset::factory()->create(['last_checkout' => now()->subMonth()]);
|
2024-04-10 13:49:07 -07:00
|
|
|
|
|
|
|
$this->actingAs(User::factory()->checkoutAssets()->create())
|
|
|
|
->post(route('hardware.checkout.store', $asset), [
|
|
|
|
'checkout_to_type' => 'user',
|
|
|
|
'assigned_user' => User::factory()->create()->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$asset->refresh();
|
|
|
|
|
|
|
|
$this->assertTrue(Carbon::parse($asset->last_checkout)->diffInSeconds(now()) < 2);
|
2024-03-20 14:28:27 -07:00
|
|
|
}
|
2024-05-31 12:42:32 -07:00
|
|
|
|
2024-08-06 13:30:34 -07:00
|
|
|
public function testAssetCheckoutPageIsRedirectedIfModelIsInvalid()
|
2024-05-31 12:42:32 -07:00
|
|
|
{
|
|
|
|
|
|
|
|
$asset = Asset::factory()->create();
|
|
|
|
$asset->model_id = 0;
|
|
|
|
$asset->forceSave();
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->admin()->create())
|
|
|
|
->get(route('hardware.checkout.create', ['assetId' => $asset->id]))
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertSessionHas('error')
|
|
|
|
->assertRedirect(route('hardware.show',['hardware' => $asset->id]));
|
|
|
|
}
|
|
|
|
|
2024-08-06 13:30:34 -07:00
|
|
|
public function testAssetCheckoutPagePostIsRedirectedIfRedirectSelectionIsIndex()
|
2024-05-31 12:42:32 -07:00
|
|
|
{
|
|
|
|
$asset = Asset::factory()->create();
|
2024-07-26 07:07:43 -07:00
|
|
|
|
2024-05-31 12:42:32 -07:00
|
|
|
$this->actingAs(User::factory()->admin()->create())
|
2024-07-26 07:07:43 -07:00
|
|
|
->from(route('hardware.checkout.create', $asset))
|
2024-05-31 12:42:32 -07:00
|
|
|
->post(route('hardware.checkout.store', $asset), [
|
|
|
|
'checkout_to_type' => 'user',
|
2024-07-26 07:07:43 -07:00
|
|
|
'assigned_user' => User::factory()->create()->id,
|
|
|
|
'redirect_option' => 'index',
|
2024-05-31 12:42:32 -07:00
|
|
|
])
|
|
|
|
->assertStatus(302)
|
2024-07-26 07:07:43 -07:00
|
|
|
->assertRedirect(route('hardware.index'));
|
|
|
|
}
|
|
|
|
|
2024-08-06 13:30:34 -07:00
|
|
|
public function testAssetCheckoutPagePostIsRedirectedIfRedirectSelectionIsItem()
|
2024-07-26 07:07:43 -07:00
|
|
|
{
|
|
|
|
$asset = Asset::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->admin()->create())
|
|
|
|
->from(route('hardware.checkout.create', $asset))
|
|
|
|
->post(route('hardware.checkout.store' , $asset), [
|
|
|
|
'checkout_to_type' => 'user',
|
|
|
|
'assigned_user' => User::factory()->create()->id,
|
|
|
|
'redirect_option' => 'item',
|
|
|
|
])
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertSessionHasNoErrors()
|
2024-05-31 12:42:32 -07:00
|
|
|
->assertRedirect(route('hardware.show', ['hardware' => $asset->id]));
|
|
|
|
}
|
2024-07-26 07:07:43 -07:00
|
|
|
|
2024-08-06 13:30:34 -07:00
|
|
|
public function testAssetCheckoutPagePostIsRedirectedIfRedirectSelectionIsUserTarget()
|
2024-07-26 07:07:43 -07:00
|
|
|
{
|
|
|
|
$user = User::factory()->create();
|
|
|
|
$asset = Asset::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->admin()->create())
|
|
|
|
->from(route('hardware.checkout.create', $asset))
|
|
|
|
->post(route('hardware.checkout.store' , $asset), [
|
|
|
|
'checkout_to_type' => 'user',
|
|
|
|
'assigned_user' => $user->id,
|
|
|
|
'redirect_option' => 'target',
|
|
|
|
'assigned_qty' => 1,
|
|
|
|
])
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.show', ['user' => $user]));
|
|
|
|
}
|
|
|
|
|
2024-08-06 13:30:34 -07:00
|
|
|
public function testAssetCheckoutPagePostIsRedirectedIfRedirectSelectionIsAssetTarget()
|
2024-07-26 07:07:43 -07:00
|
|
|
{
|
|
|
|
$target = Asset::factory()->create();
|
|
|
|
$asset = Asset::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->admin()->create())
|
|
|
|
->from(route('hardware.checkout.create', $asset))
|
|
|
|
->post(route('hardware.checkout.store' , $asset), [
|
|
|
|
'checkout_to_type' => 'asset',
|
|
|
|
'assigned_asset' => $target->id,
|
|
|
|
'redirect_option' => 'target',
|
|
|
|
'assigned_qty' => 1,
|
|
|
|
])
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('hardware.show', ['hardware' => $target]));
|
|
|
|
}
|
|
|
|
|
2024-08-06 13:30:34 -07:00
|
|
|
public function testAssetCheckoutPagePostIsRedirectedIfRedirectSelectionIsLocationTarget()
|
2024-07-26 07:07:43 -07:00
|
|
|
{
|
|
|
|
$target = Location::factory()->create();
|
|
|
|
$asset = Asset::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->admin()->create())
|
|
|
|
->from(route('hardware.checkout.create', $asset))
|
|
|
|
->post(route('hardware.checkout.store' , $asset), [
|
|
|
|
'checkout_to_type' => 'location',
|
|
|
|
'assigned_location' => $target->id,
|
|
|
|
'redirect_option' => 'target',
|
|
|
|
'assigned_qty' => 1,
|
|
|
|
])
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('locations.show', ['location' => $target]));
|
|
|
|
}
|
2024-03-20 13:29:25 -07:00
|
|
|
}
|