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-07-29 02:06:36 -07:00
|
|
|
use App\Models\Asset;
|
|
|
|
use App\Models\Location;
|
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
|
|
|
{
|
2024-07-17 19:48:06 -07:00
|
|
|
$accessory = Accessory::factory()->create();
|
|
|
|
$response = $this->actingAs(User::factory()->superuser()->create())
|
2024-07-17 21:04:17 -07:00
|
|
|
->from(route('accessories.checkout.show', $accessory))
|
2024-07-17 19:48:06 -07:00
|
|
|
->post(route('accessories.checkout.store', $accessory), [
|
2024-01-29 14:21:30 -08:00
|
|
|
// missing assigned_to
|
|
|
|
])
|
2024-07-17 19:48:06 -07:00
|
|
|
->assertStatus(302)
|
|
|
|
->assertSessionHas('errors')
|
|
|
|
->assertRedirect(route('accessories.checkout.store', $accessory));
|
|
|
|
|
|
|
|
$this->followRedirects($response)->assertSee(trans('general.error'));
|
2024-01-29 14:21:30 -08:00
|
|
|
}
|
|
|
|
|
2024-07-17 21:04:17 -07:00
|
|
|
public function testAccessoryMustHaveAvailableItemsForCheckoutWhenCheckingOut()
|
2024-01-29 14:21:30 -08:00
|
|
|
{
|
2024-07-17 19:48:06 -07:00
|
|
|
|
2024-07-17 21:04:17 -07:00
|
|
|
$accessory = Accessory::factory()->withoutItemsRemaining()->create();
|
2024-07-17 19:48:06 -07:00
|
|
|
$response = $this->actingAs(User::factory()->viewAccessories()->checkoutAccessories()->create())
|
2024-07-17 21:04:17 -07:00
|
|
|
->from(route('accessories.checkout.show', $accessory))
|
|
|
|
->post(route('accessories.checkout.store', $accessory), [
|
2024-07-29 01:54:53 -07:00
|
|
|
'assigned_user' => User::factory()->create()->id,
|
|
|
|
'checkout_to_type' => 'user',
|
2024-01-29 14:21:30 -08:00
|
|
|
])
|
2024-07-17 19:48:06 -07:00
|
|
|
->assertStatus(302)
|
2024-07-17 21:04:17 -07:00
|
|
|
->assertSessionHas('errors')
|
|
|
|
->assertRedirect(route('accessories.checkout.store', $accessory));
|
|
|
|
$response->assertInvalid(['checkout_qty']);
|
2024-07-17 19:48:06 -07:00
|
|
|
$this->followRedirects($response)->assertSee(trans('general.error'));
|
2024-01-29 14:21:30 -08:00
|
|
|
}
|
|
|
|
|
2024-07-17 19:48:06 -07:00
|
|
|
public function testAccessoryCanBeCheckedOutWithoutQuantity()
|
2024-01-29 14:21:30 -08:00
|
|
|
{
|
|
|
|
$accessory = Accessory::factory()->create();
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->checkoutAccessories()->create())
|
|
|
|
->post(route('accessories.checkout.store', $accessory), [
|
2024-07-29 01:54:53 -07:00
|
|
|
'assigned_user' => $user->id,
|
|
|
|
'checkout_to_type' => 'user',
|
2024-07-17 19:48:06 -07:00
|
|
|
'note' => 'oh hi there',
|
|
|
|
]);
|
|
|
|
|
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 19:48:06 -07:00
|
|
|
|
|
|
|
$this->assertDatabaseHas('action_logs', [
|
|
|
|
'action_type' => 'checkout',
|
|
|
|
'target_id' => $user->id,
|
|
|
|
'target_type' => User::class,
|
|
|
|
'item_id' => $accessory->id,
|
|
|
|
'item_type' => Accessory::class,
|
|
|
|
'note' => 'oh hi there',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAccessoryCanBeCheckedOutWithQuantity()
|
|
|
|
{
|
2024-07-17 20:29:49 -07:00
|
|
|
$accessory = Accessory::factory()->create(['qty'=>5]);
|
2024-07-17 19:48:06 -07:00
|
|
|
$user = User::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->checkoutAccessories()->create())
|
2024-07-17 20:54:07 -07:00
|
|
|
->from(route('accessories.checkout.show', $accessory))
|
2024-07-17 19:48:06 -07:00
|
|
|
->post(route('accessories.checkout.store', $accessory), [
|
2024-07-29 01:54:53 -07:00
|
|
|
'assigned_user' => $user->id,
|
|
|
|
'checkout_to_type' => 'user',
|
2024-07-17 19:48:06 -07:00
|
|
|
'checkout_qty' => 3,
|
|
|
|
'note' => 'oh hi there',
|
2024-01-29 14:21:30 -08:00
|
|
|
]);
|
|
|
|
|
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 19:48:06 -07:00
|
|
|
|
|
|
|
$this->assertDatabaseHas('action_logs', [
|
|
|
|
'action_type' => 'checkout',
|
|
|
|
'target_id' => $user->id,
|
|
|
|
'target_type' => User::class,
|
|
|
|
'item_id' => $accessory->id,
|
|
|
|
'item_type' => Accessory::class,
|
|
|
|
'note' => 'oh hi there',
|
|
|
|
]);
|
2024-01-29 14:21:30 -08:00
|
|
|
}
|
|
|
|
|
2024-07-29 02:06:36 -07:00
|
|
|
public function testAccessoryCanBeCheckedOutToLocationWithQuantity()
|
|
|
|
{
|
|
|
|
$accessory = Accessory::factory()->create(['qty'=>5]);
|
|
|
|
$location = Location::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->checkoutAccessories()->create())
|
|
|
|
->from(route('accessories.checkout.show', $accessory))
|
|
|
|
->post(route('accessories.checkout.store', $accessory), [
|
|
|
|
'assigned_location' => $location->id,
|
|
|
|
'checkout_to_type' => 'location',
|
|
|
|
'checkout_qty' => 3,
|
|
|
|
'note' => 'oh hi there',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertTrue($accessory->checkouts()->where('assigned_type', Location::class)->where('assigned_to', $location->id)->count() > 0);
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('action_logs', [
|
|
|
|
'action_type' => 'checkout',
|
|
|
|
'target_id' => $location->id,
|
|
|
|
'target_type' => Location::class,
|
|
|
|
'item_id' => $accessory->id,
|
|
|
|
'item_type' => Accessory::class,
|
|
|
|
'note' => 'oh hi there',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAccessoryCanBeCheckedOutToAssetWithQuantity()
|
|
|
|
{
|
|
|
|
$accessory = Accessory::factory()->create(['qty'=>5]);
|
|
|
|
$asset = Asset::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->checkoutAccessories()->create())
|
|
|
|
->from(route('accessories.checkout.show', $accessory))
|
|
|
|
->post(route('accessories.checkout.store', $accessory), [
|
|
|
|
'assigned_asset' => $asset->id,
|
|
|
|
'checkout_to_type' => 'asset',
|
|
|
|
'checkout_qty' => 3,
|
|
|
|
'note' => 'oh hi there',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertTrue($accessory->checkouts()->where('assigned_type', Asset::class)->where('assigned_to', $asset->id)->count() > 0);
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('action_logs', [
|
|
|
|
'action_type' => 'checkout',
|
|
|
|
'target_id' => $asset->id,
|
|
|
|
'target_type' => Asset::class,
|
|
|
|
'item_id' => $accessory->id,
|
|
|
|
'item_type' => Accessory::class,
|
|
|
|
'note' => 'oh hi there',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
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())
|
2024-07-17 20:54:07 -07:00
|
|
|
->from(route('accessories.checkout.show', $accessory))
|
2024-01-29 15:56:18 -08:00
|
|
|
->post(route('accessories.checkout.store', $accessory), [
|
2024-07-29 01:54:53 -07:00
|
|
|
'assigned_user' => $user->id,
|
|
|
|
'checkout_to_type' => 'user',
|
2024-01-29 15:56:18 -08:00
|
|
|
]);
|
|
|
|
|
|
|
|
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)
|
2024-07-17 20:54:07 -07:00
|
|
|
->from(route('accessories.checkout.show', $accessory))
|
2024-01-29 15:56:18 -08:00
|
|
|
->post(route('accessories.checkout.store', $accessory), [
|
2024-07-29 01:54:53 -07:00
|
|
|
'assigned_user' => $user->id,
|
|
|
|
'checkout_to_type' => 'user',
|
2024-01-29 15:56:18 -08:00
|
|
|
'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,
|
2024-09-17 14:16:41 -07:00
|
|
|
'created_by' => $actor->id,
|
2024-01-29 16:59:57 -08:00
|
|
|
'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
|
|
|
}
|
2024-07-26 06:27:07 -07:00
|
|
|
|
|
|
|
public function testAccessoryCheckoutPagePostIsRedirectedIfRedirectSelectionIsIndex()
|
|
|
|
{
|
|
|
|
$accessory = Accessory::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->admin()->create())
|
|
|
|
->from(route('accessories.index'))
|
|
|
|
->post(route('accessories.checkout.store', $accessory), [
|
2024-07-29 10:15:01 -07:00
|
|
|
'assigned_user' => User::factory()->create()->id,
|
|
|
|
'checkout_to_type' => 'user',
|
2024-07-26 06:27:07 -07:00
|
|
|
'redirect_option' => 'index',
|
|
|
|
'assigned_qty' => 1,
|
|
|
|
])
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('accessories.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAccessoryCheckoutPagePostIsRedirectedIfRedirectSelectionIsItem()
|
|
|
|
{
|
|
|
|
$accessory = Accessory::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->admin()->create())
|
|
|
|
->from(route('accessories.index'))
|
|
|
|
->post(route('accessories.checkout.store' , $accessory), [
|
2024-07-29 10:15:01 -07:00
|
|
|
'assigned_user' => User::factory()->create()->id,
|
|
|
|
'checkout_to_type' => 'user',
|
2024-07-26 06:27:07 -07:00
|
|
|
'redirect_option' => 'item',
|
|
|
|
'assigned_qty' => 1,
|
|
|
|
])
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertSessionHasNoErrors()
|
|
|
|
->assertRedirect(route('accessories.show', ['accessory' => $accessory->id]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAccessoryCheckoutPagePostIsRedirectedIfRedirectSelectionIsTarget()
|
|
|
|
{
|
|
|
|
$user = User::factory()->create();
|
|
|
|
$accessory = Accessory::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->admin()->create())
|
|
|
|
->from(route('accessories.index'))
|
|
|
|
->post(route('accessories.checkout.store' , $accessory), [
|
2024-07-29 10:15:01 -07:00
|
|
|
'assigned_user' => $user->id,
|
|
|
|
'checkout_to_type' => 'user',
|
2024-07-26 06:27:07 -07:00
|
|
|
'redirect_option' => 'target',
|
|
|
|
'assigned_qty' => 1,
|
|
|
|
])
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.show', ['user' => $user]));
|
|
|
|
}
|
2024-01-29 14:21:30 -08:00
|
|
|
}
|