2024-01-29 15:59:23 -08:00
|
|
|
<?php
|
|
|
|
|
2024-06-04 10:48:53 -07:00
|
|
|
namespace Tests\Feature\Checkouts\Api;
|
2024-01-29 15:59:23 -08:00
|
|
|
|
|
|
|
use App\Models\Accessory;
|
2024-01-29 16:59:57 -08:00
|
|
|
use App\Models\Actionlog;
|
2024-01-29 15:59:23 -08:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Notifications\CheckoutAccessoryNotification;
|
|
|
|
use Illuminate\Support\Facades\Notification;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class AccessoryCheckoutTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testCheckingOutAccessoryRequiresCorrectPermission()
|
|
|
|
{
|
2024-01-29 16:48:40 -08:00
|
|
|
$this->actingAsForApi(User::factory()->create())
|
|
|
|
->postJson(route('api.accessories.checkout', Accessory::factory()->create()))
|
|
|
|
->assertForbidden();
|
2024-01-29 15:59:23 -08:00
|
|
|
}
|
|
|
|
|
2024-01-30 12:43:07 -08:00
|
|
|
public function testValidationWhenCheckingOutAccessory()
|
2024-01-29 15:59:23 -08:00
|
|
|
{
|
2024-01-29 16:48:40 -08:00
|
|
|
$this->actingAsForApi(User::factory()->checkoutAccessories()->create())
|
|
|
|
->postJson(route('api.accessories.checkout', Accessory::factory()->create()), [
|
2024-07-29 01:54:53 -07:00
|
|
|
// missing assigned_user, assigned_location, assigned_asset
|
2024-01-29 16:48:40 -08:00
|
|
|
])
|
|
|
|
->assertStatusMessageIs('error');
|
2024-01-29 15:59:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAccessoryMustBeAvailableWhenCheckingOut()
|
|
|
|
{
|
2024-01-29 16:48:40 -08:00
|
|
|
$this->actingAsForApi(User::factory()->checkoutAccessories()->create())
|
|
|
|
->postJson(route('api.accessories.checkout', Accessory::factory()->withoutItemsRemaining()->create()), [
|
2024-07-29 01:54:53 -07:00
|
|
|
'assigned_user' => User::factory()->create()->id,
|
|
|
|
'checkout_to_type' => 'user'
|
2024-01-29 16:48:40 -08:00
|
|
|
])
|
2024-07-17 16:25:07 -07:00
|
|
|
->assertOk()
|
|
|
|
->assertStatusMessageIs('error')
|
2024-07-17 19:48:06 -07:00
|
|
|
->assertJson(
|
|
|
|
[
|
|
|
|
'status' => 'error',
|
|
|
|
'messages' =>
|
|
|
|
[
|
|
|
|
'checkout_qty' =>
|
|
|
|
[
|
|
|
|
trans_choice('admin/accessories/message.checkout.checkout_qty.lte', 0,
|
|
|
|
[
|
|
|
|
'number_currently_remaining' => 0,
|
|
|
|
'checkout_qty' => 1,
|
|
|
|
'number_remaining_after_checkout' => 0
|
|
|
|
])
|
|
|
|
],
|
|
|
|
|
|
|
|
],
|
|
|
|
'payload' => null,
|
|
|
|
])
|
2024-07-17 16:25:07 -07:00
|
|
|
->assertStatus(200)
|
|
|
|
->json();
|
2024-01-29 15:59:23 -08:00
|
|
|
}
|
|
|
|
|
2024-07-17 19:48:06 -07:00
|
|
|
public function testAccessoryCanBeCheckedOutWithoutQty()
|
2024-01-29 15:59:23 -08:00
|
|
|
{
|
2024-01-29 16:48:40 -08:00
|
|
|
$accessory = Accessory::factory()->create();
|
|
|
|
$user = User::factory()->create();
|
2024-07-17 16:25:07 -07:00
|
|
|
$admin = User::factory()->checkoutAccessories()->create();
|
2024-01-29 16:48:40 -08:00
|
|
|
|
2024-07-17 19:48:06 -07:00
|
|
|
$this->actingAsForApi($admin)
|
2024-01-29 16:48:40 -08:00
|
|
|
->postJson(route('api.accessories.checkout', $accessory), [
|
2024-07-29 01:54:53 -07:00
|
|
|
'assigned_user' => $user->id,
|
|
|
|
'checkout_to_type' => 'user'
|
2024-07-17 16:25:07 -07:00
|
|
|
])
|
|
|
|
->assertOk()
|
|
|
|
->assertStatusMessageIs('success')
|
|
|
|
->assertStatus(200)
|
2024-07-17 19:48:06 -07:00
|
|
|
->assertJson(['messages' => trans('admin/accessories/message.checkout.success')])
|
2024-07-17 16:25:07 -07:00
|
|
|
->json();
|
|
|
|
|
2024-07-29 01:54:53 -07:00
|
|
|
$this->assertTrue($accessory->checkouts()->where('assigned_type', User::class)->where('assigned_to', $user->id)->count() > 0);
|
2024-07-17 16:25:07 -07:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
1,
|
|
|
|
Actionlog::where([
|
|
|
|
'action_type' => 'checkout',
|
|
|
|
'target_id' => $user->id,
|
|
|
|
'target_type' => User::class,
|
|
|
|
'item_id' => $accessory->id,
|
|
|
|
'item_type' => Accessory::class,
|
|
|
|
'user_id' => $admin->id,
|
2024-07-17 19:48:06 -07:00
|
|
|
])->count(),'Log entry either does not exist or there are more than expected'
|
2024-07-17 16:25:07 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAccessoryCanBeCheckedOutWithQty()
|
|
|
|
{
|
|
|
|
$accessory = Accessory::factory()->create(['qty' => 20]);
|
|
|
|
$user = User::factory()->create();
|
|
|
|
$admin = User::factory()->checkoutAccessories()->create();
|
|
|
|
|
2024-07-17 19:48:06 -07:00
|
|
|
$this->actingAsForApi($admin)
|
2024-07-17 16:25:07 -07:00
|
|
|
->postJson(route('api.accessories.checkout', $accessory), [
|
2024-07-29 01:54:53 -07:00
|
|
|
'assigned_user' => $user->id,
|
|
|
|
'checkout_to_type' => 'user',
|
2024-07-17 16:25:07 -07:00
|
|
|
'checkout_qty' => 2,
|
|
|
|
])
|
|
|
|
->assertOk()
|
|
|
|
->assertStatusMessageIs('success')
|
|
|
|
->assertStatus(200)
|
2024-07-17 19:48:06 -07:00
|
|
|
->assertJson(['messages' => trans('admin/accessories/message.checkout.success')])
|
2024-07-17 16:25:07 -07:00
|
|
|
->json();
|
|
|
|
|
2024-07-29 01:54:53 -07:00
|
|
|
$this->assertTrue($accessory->checkouts()->where('assigned_type', User::class)->where('assigned_to', $user->id)->count() > 0);
|
2024-07-17 16:25:07 -07:00
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
1,
|
|
|
|
Actionlog::where([
|
|
|
|
'action_type' => 'checkout',
|
|
|
|
'target_id' => $user->id,
|
|
|
|
'target_type' => User::class,
|
|
|
|
'item_id' => $accessory->id,
|
|
|
|
'item_type' => Accessory::class,
|
|
|
|
'user_id' => $admin->id,
|
|
|
|
])->count(),
|
|
|
|
'Log entry either does not exist or there are more than expected'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAccessoryCannotBeCheckedOutToInvalidUser()
|
|
|
|
{
|
|
|
|
$accessory = Accessory::factory()->create();
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->checkoutAccessories()->create())
|
|
|
|
->postJson(route('api.accessories.checkout', $accessory), [
|
2024-07-29 01:54:53 -07:00
|
|
|
'assigned_user' => 'invalid-user-id',
|
|
|
|
'checkout_to_type' => 'user',
|
2024-07-17 16:25:07 -07:00
|
|
|
'note' => 'oh hi there',
|
|
|
|
])
|
|
|
|
->assertOk()
|
|
|
|
->assertStatusMessageIs('error')
|
|
|
|
->assertStatus(200)
|
|
|
|
->json();
|
2024-01-29 16:48:40 -08:00
|
|
|
|
2024-07-29 01:54:53 -07:00
|
|
|
$this->assertFalse($accessory->checkouts()->where('assigned_type', User::class)->where('assigned_to', $user->id)->count() > 0);
|
2024-01-29 15:59:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUserSentNotificationUponCheckout()
|
|
|
|
{
|
|
|
|
Notification::fake();
|
|
|
|
|
|
|
|
$accessory = Accessory::factory()->requiringAcceptance()->create();
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->checkoutAccessories()->create())
|
|
|
|
->postJson(route('api.accessories.checkout', $accessory), [
|
2024-07-29 01:54:53 -07:00
|
|
|
'assigned_user' => $user->id,
|
|
|
|
'checkout_to_type' => 'user',
|
2024-01-29 15:59:23 -08:00
|
|
|
]);
|
|
|
|
|
|
|
|
Notification::assertSentTo($user, CheckoutAccessoryNotification::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testActionLogCreatedUponCheckout()
|
|
|
|
{
|
2024-01-29 16:48:40 -08:00
|
|
|
$accessory = Accessory::factory()->create();
|
|
|
|
$actor = User::factory()->checkoutAccessories()->create();
|
|
|
|
$user = User::factory()->create();
|
2024-01-29 15:59:23 -08:00
|
|
|
|
2024-01-29 16:48:40 -08:00
|
|
|
$this->actingAsForApi($actor)
|
|
|
|
->postJson(route('api.accessories.checkout', $accessory), [
|
2024-07-29 01:54:53 -07:00
|
|
|
'assigned_user' => $user->id,
|
|
|
|
'checkout_to_type' => 'user',
|
2024-01-29 16:48:40 -08:00
|
|
|
'note' => 'oh hi there',
|
|
|
|
]);
|
|
|
|
|
2024-01-29 16:59:57 -08:00
|
|
|
$this->assertEquals(
|
|
|
|
1,
|
|
|
|
Actionlog::where([
|
|
|
|
'action_type' => 'checkout',
|
|
|
|
'target_id' => $user->id,
|
|
|
|
'target_type' => User::class,
|
|
|
|
'item_id' => $accessory->id,
|
|
|
|
'item_type' => Accessory::class,
|
|
|
|
'user_id' => $actor->id,
|
|
|
|
'note' => 'oh hi there',
|
|
|
|
])->count(),
|
|
|
|
'Log entry either does not exist or there are more than expected'
|
|
|
|
);
|
2024-01-29 15:59:23 -08:00
|
|
|
}
|
|
|
|
}
|