2024-01-29 17:56:55 -08:00
|
|
|
<?php
|
|
|
|
|
2024-06-04 10:48:53 -07:00
|
|
|
namespace Tests\Feature\Checkouts\Ui;
|
2024-01-29 17:56:55 -08:00
|
|
|
|
2024-01-30 12:43:20 -08:00
|
|
|
use App\Models\Actionlog;
|
2024-07-26 06:27:07 -07:00
|
|
|
use App\Models\Asset;
|
|
|
|
use App\Models\Component;
|
2024-01-30 12:43:20 -08:00
|
|
|
use App\Models\Consumable;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Notifications\CheckoutConsumableNotification;
|
|
|
|
use Illuminate\Support\Facades\Notification;
|
2024-01-29 17:56:55 -08:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class ConsumableCheckoutTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testCheckingOutConsumableRequiresCorrectPermission()
|
|
|
|
{
|
2024-01-30 12:43:20 -08:00
|
|
|
$this->actingAs(User::factory()->create())
|
|
|
|
->post(route('consumables.checkout.store', Consumable::factory()->create()))
|
|
|
|
->assertForbidden();
|
2024-01-29 17:56:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidationWhenCheckingOutConsumable()
|
|
|
|
{
|
2024-01-30 12:43:20 -08:00
|
|
|
$this->actingAs(User::factory()->checkoutConsumables()->create())
|
|
|
|
->post(route('consumables.checkout.store', Consumable::factory()->create()), [
|
|
|
|
// missing assigned_to
|
|
|
|
])
|
|
|
|
->assertSessionHas('error');
|
2024-01-29 17:56:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testConsumableMustBeAvailableWhenCheckingOut()
|
|
|
|
{
|
2024-01-30 12:43:20 -08:00
|
|
|
$this->actingAs(User::factory()->checkoutConsumables()->create())
|
|
|
|
->post(route('consumables.checkout.store', Consumable::factory()->withoutItemsRemaining()->create()), [
|
|
|
|
'assigned_to' => User::factory()->create()->id,
|
|
|
|
])
|
|
|
|
->assertSessionHas('error');
|
2024-01-29 17:56:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testConsumableCanBeCheckedOut()
|
|
|
|
{
|
2024-01-30 12:43:20 -08:00
|
|
|
$consumable = Consumable::factory()->create();
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->checkoutConsumables()->create())
|
|
|
|
->post(route('consumables.checkout.store', $consumable), [
|
|
|
|
'assigned_to' => $user->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertTrue($user->consumables->contains($consumable));
|
2024-01-29 17:56:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUserSentNotificationUponCheckout()
|
|
|
|
{
|
2024-01-30 12:43:20 -08:00
|
|
|
Notification::fake();
|
|
|
|
|
|
|
|
$consumable = Consumable::factory()->create();
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->checkoutConsumables()->create())
|
|
|
|
->post(route('consumables.checkout.store', $consumable), [
|
|
|
|
'assigned_to' => $user->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
Notification::assertSentTo($user, CheckoutConsumableNotification::class);
|
2024-01-29 17:56:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testActionLogCreatedUponCheckout()
|
|
|
|
{
|
2024-01-30 12:43:20 -08:00
|
|
|
$consumable = Consumable::factory()->create();
|
|
|
|
$actor = User::factory()->checkoutConsumables()->create();
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAs($actor)
|
|
|
|
->post(route('consumables.checkout.store', $consumable), [
|
|
|
|
'assigned_to' => $user->id,
|
|
|
|
'note' => 'oh hi there',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
1,
|
|
|
|
Actionlog::where([
|
|
|
|
'action_type' => 'checkout',
|
|
|
|
'target_id' => $user->id,
|
|
|
|
'target_type' => User::class,
|
|
|
|
'item_id' => $consumable->id,
|
|
|
|
'item_type' => Consumable::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 17:56:55 -08:00
|
|
|
}
|
2024-07-26 06:27:07 -07:00
|
|
|
|
|
|
|
public function testConsumableCheckoutPagePostIsRedirectedIfRedirectSelectionIsIndex()
|
|
|
|
{
|
|
|
|
$consumable = Consumable::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->admin()->create())
|
|
|
|
->from(route('consumables.index'))
|
|
|
|
->post(route('consumables.checkout.store', $consumable), [
|
|
|
|
'assigned_to' => User::factory()->create()->id,
|
|
|
|
'redirect_option' => 'index',
|
|
|
|
'assigned_qty' => 1,
|
|
|
|
])
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('consumables.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConsumableCheckoutPagePostIsRedirectedIfRedirectSelectionIsItem()
|
|
|
|
{
|
|
|
|
$consumable = Consumable::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->admin()->create())
|
|
|
|
->from(route('consumables.index'))
|
|
|
|
->post(route('consumables.checkout.store' , $consumable), [
|
|
|
|
'assigned_to' => User::factory()->create()->id,
|
|
|
|
'redirect_option' => 'item',
|
|
|
|
'assigned_qty' => 1,
|
|
|
|
])
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('consumables.show', ['consumable' => $consumable->id]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConsumableCheckoutPagePostIsRedirectedIfRedirectSelectionIsTarget()
|
|
|
|
{
|
|
|
|
$user = User::factory()->create();
|
|
|
|
$consumable = Consumable::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->admin()->create())
|
|
|
|
->from(route('components.index'))
|
|
|
|
->post(route('consumables.checkout.store' , $consumable), [
|
|
|
|
'assigned_to' => $user->id,
|
|
|
|
'redirect_option' => 'target',
|
|
|
|
'assigned_qty' => 1,
|
|
|
|
])
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.show', ['user' => $user]));
|
|
|
|
}
|
|
|
|
|
2024-01-29 17:56:55 -08:00
|
|
|
}
|