2024-01-29 14:21:30 -08:00
|
|
|
<?php
|
|
|
|
|
2024-06-04 10:48:53 -07:00
|
|
|
namespace Tests\Feature\Checkouts\Ui;
|
2024-01-29 14:21:30 -08:00
|
|
|
|
|
|
|
use App\Models\Accessory;
|
2024-01-29 16:59:57 -08:00
|
|
|
use App\Models\Actionlog;
|
2024-01-29 14:21:30 -08:00
|
|
|
use App\Models\User;
|
2024-01-29 15:56:18 -08:00
|
|
|
use App\Notifications\CheckoutAccessoryNotification;
|
|
|
|
use Illuminate\Support\Facades\Notification;
|
2024-01-29 14:21:30 -08:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class AccessoryCheckoutTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testCheckingOutAccessoryRequiresCorrectPermission()
|
|
|
|
{
|
|
|
|
$this->actingAs(User::factory()->create())
|
|
|
|
->post(route('accessories.checkout.store', Accessory::factory()->create()))
|
|
|
|
->assertForbidden();
|
|
|
|
}
|
|
|
|
|
2024-01-30 12:43:07 -08:00
|
|
|
public function testValidationWhenCheckingOutAccessory()
|
2024-01-29 14:21:30 -08:00
|
|
|
{
|
|
|
|
$this->actingAs(User::factory()->checkoutAccessories()->create())
|
2024-01-29 16:48:40 -08:00
|
|
|
->post(route('accessories.checkout.store', Accessory::factory()->create()), [
|
2024-01-29 14:21:30 -08:00
|
|
|
// missing assigned_to
|
|
|
|
])
|
|
|
|
->assertSessionHas('error');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAccessoryMustBeAvailableWhenCheckingOut()
|
|
|
|
{
|
|
|
|
$this->actingAs(User::factory()->checkoutAccessories()->create())
|
|
|
|
->post(route('accessories.checkout.store', Accessory::factory()->withoutItemsRemaining()->create()), [
|
|
|
|
'assigned_to' => User::factory()->create()->id,
|
|
|
|
])
|
|
|
|
->assertSessionHas('error');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAccessoryCanBeCheckedOut()
|
|
|
|
{
|
|
|
|
$accessory = Accessory::factory()->create();
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->checkoutAccessories()->create())
|
|
|
|
->post(route('accessories.checkout.store', $accessory), [
|
|
|
|
'assigned_to' => $user->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertTrue($accessory->users->contains($user));
|
|
|
|
}
|
|
|
|
|
2024-01-29 15:56:18 -08:00
|
|
|
public function testUserSentNotificationUponCheckout()
|
2024-01-29 14:21:30 -08:00
|
|
|
{
|
2024-01-29 15:56:18 -08:00
|
|
|
Notification::fake();
|
|
|
|
|
|
|
|
$accessory = Accessory::factory()->requiringAcceptance()->create();
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->checkoutAccessories()->create())
|
|
|
|
->post(route('accessories.checkout.store', $accessory), [
|
|
|
|
'assigned_to' => $user->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
Notification::assertSentTo($user, CheckoutAccessoryNotification::class);
|
2024-01-29 14:21:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testActionLogCreatedUponCheckout()
|
|
|
|
{
|
2024-01-29 15:56:18 -08:00
|
|
|
$accessory = Accessory::factory()->create();
|
|
|
|
$actor = User::factory()->checkoutAccessories()->create();
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAs($actor)
|
|
|
|
->post(route('accessories.checkout.store', $accessory), [
|
|
|
|
'assigned_to' => $user->id,
|
|
|
|
'note' => 'oh hi there',
|
|
|
|
]);
|
2024-01-29 14:21:30 -08:00
|
|
|
|
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 14:21:30 -08:00
|
|
|
}
|
|
|
|
}
|