2024-07-24 14:16:42 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\Users\Ui;
|
|
|
|
|
|
|
|
use App\Models\Accessory;
|
2024-08-05 15:50:09 -07:00
|
|
|
use App\Models\Asset;
|
2024-07-24 14:16:42 -07:00
|
|
|
use App\Models\Consumable;
|
|
|
|
use App\Models\Statuslabel;
|
|
|
|
use App\Models\User;
|
2024-08-05 15:42:03 -07:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2024-07-24 14:16:42 -07:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class BulkDeleteUsersTest extends TestCase
|
|
|
|
{
|
2024-08-05 15:43:38 -07:00
|
|
|
public function testRequiresCorrectPermission()
|
|
|
|
{
|
|
|
|
$this->actingAs(User::factory()->create())
|
|
|
|
->post(route('users/bulksave'), [
|
|
|
|
'ids' => [
|
|
|
|
User::factory()->create()->id,
|
|
|
|
],
|
|
|
|
'status_id' => Statuslabel::factory()->create()->id,
|
|
|
|
])
|
|
|
|
->assertForbidden();
|
|
|
|
}
|
|
|
|
|
2024-08-05 15:50:09 -07:00
|
|
|
public function testValidation()
|
|
|
|
{
|
|
|
|
$user = User::factory()->create();
|
|
|
|
Asset::factory()->assignedToUser($user)->create();
|
|
|
|
|
|
|
|
$actor = $this->actingAs(User::factory()->editUsers()->create());
|
|
|
|
|
|
|
|
// "ids" required
|
|
|
|
$actor->post(route('users/bulksave'), [
|
|
|
|
// 'ids' => [
|
|
|
|
// $user->id,
|
|
|
|
// ],
|
|
|
|
'status_id' => Statuslabel::factory()->create()->id,
|
|
|
|
])->assertSessionHas('error')->assertRedirect();
|
|
|
|
|
|
|
|
// "status_id" needed when provided users have assets associated
|
|
|
|
$actor->post(route('users/bulksave'), [
|
|
|
|
'ids' => [
|
|
|
|
$user->id,
|
|
|
|
],
|
|
|
|
// 'status_id' => Statuslabel::factory()->create()->id,
|
|
|
|
])->assertSessionHas('error')->assertRedirect();
|
|
|
|
}
|
|
|
|
|
2024-08-05 16:02:59 -07:00
|
|
|
public function testAccessoriesCanBeBulkCheckedIn()
|
2024-07-24 14:16:42 -07:00
|
|
|
{
|
|
|
|
[$accessoryA, $accessoryB] = Accessory::factory()->count(2)->create();
|
2024-08-05 15:11:18 -07:00
|
|
|
[$userA, $userB, $userC] = User::factory()->count(3)->create();
|
2024-07-24 14:16:42 -07:00
|
|
|
|
|
|
|
// Add checkouts for multiple accessories to multiple users to get different ids in the mix
|
2024-08-05 15:16:40 -07:00
|
|
|
$this->attachAccessoryToUsers($accessoryA, [$userA, $userB, $userC]);
|
|
|
|
$this->attachAccessoryToUsers($accessoryB, [$userA, $userB]);
|
2024-07-24 14:16:42 -07:00
|
|
|
|
2024-08-05 16:02:59 -07:00
|
|
|
$this->assertTrue($userA->accessories->isNotEmpty());
|
|
|
|
$this->assertTrue($userB->accessories->isNotEmpty());
|
|
|
|
$this->assertTrue($userC->accessories->isNotEmpty());
|
|
|
|
|
2024-07-24 14:16:42 -07:00
|
|
|
$this->actingAs(User::factory()->editUsers()->create())
|
|
|
|
->post(route('users/bulksave'), [
|
|
|
|
'ids' => [
|
|
|
|
$userA->id,
|
2024-08-05 15:11:18 -07:00
|
|
|
$userC->id,
|
2024-07-24 14:16:42 -07:00
|
|
|
],
|
|
|
|
'status_id' => Statuslabel::factory()->create()->id,
|
|
|
|
])
|
|
|
|
->assertRedirect();
|
|
|
|
|
2024-08-05 16:02:59 -07:00
|
|
|
$this->assertTrue($userA->fresh()->accessories->isEmpty());
|
|
|
|
$this->assertTrue($userB->fresh()->accessories->isNotEmpty());
|
|
|
|
$this->assertTrue($userC->fresh()->accessories->isEmpty());
|
|
|
|
|
2024-07-24 14:16:42 -07:00
|
|
|
// These assertions check against a bug where the wrong value from
|
|
|
|
// accessories_users was being populated in action_logs.item_id.
|
2024-08-05 15:42:03 -07:00
|
|
|
$this->assertActionLogCheckInEntryFor($userA, $accessoryA);
|
|
|
|
$this->assertActionLogCheckInEntryFor($userA, $accessoryB);
|
|
|
|
$this->assertActionLogCheckInEntryFor($userC, $accessoryA);
|
2024-07-24 14:16:42 -07:00
|
|
|
}
|
|
|
|
|
2024-08-05 16:02:59 -07:00
|
|
|
public function testConsumablesCanBeBulkCheckedIn()
|
2024-07-24 14:16:42 -07:00
|
|
|
{
|
|
|
|
[$consumableA, $consumableB] = Consumable::factory()->count(2)->create();
|
2024-08-05 15:11:18 -07:00
|
|
|
[$userA, $userB, $userC] = User::factory()->count(3)->create();
|
2024-07-24 14:16:42 -07:00
|
|
|
|
|
|
|
// Add checkouts for multiple consumables to multiple users to get different ids in the mix
|
2024-08-05 15:16:40 -07:00
|
|
|
$this->attachConsumableToUsers($consumableA, [$userA, $userB, $userC]);
|
|
|
|
$this->attachConsumableToUsers($consumableB, [$userA, $userB]);
|
2024-07-24 14:16:42 -07:00
|
|
|
|
2024-08-05 16:02:59 -07:00
|
|
|
$this->assertTrue($userA->consumables->isNotEmpty());
|
|
|
|
$this->assertTrue($userB->consumables->isNotEmpty());
|
|
|
|
$this->assertTrue($userC->consumables->isNotEmpty());
|
|
|
|
|
2024-07-24 14:16:42 -07:00
|
|
|
$this->actingAs(User::factory()->editUsers()->create())
|
|
|
|
->post(route('users/bulksave'), [
|
|
|
|
'ids' => [
|
|
|
|
$userA->id,
|
2024-08-05 15:11:18 -07:00
|
|
|
$userC->id,
|
2024-07-24 14:16:42 -07:00
|
|
|
],
|
|
|
|
'status_id' => Statuslabel::factory()->create()->id,
|
|
|
|
])
|
|
|
|
->assertRedirect();
|
|
|
|
|
2024-08-05 16:02:59 -07:00
|
|
|
$this->assertTrue($userA->fresh()->consumables->isEmpty());
|
|
|
|
$this->assertTrue($userB->fresh()->consumables->isNotEmpty());
|
|
|
|
$this->assertTrue($userC->fresh()->consumables->isEmpty());
|
|
|
|
|
2024-07-24 14:16:42 -07:00
|
|
|
// These assertions check against a bug where the wrong value from
|
|
|
|
// consumables_users was being populated in action_logs.item_id.
|
2024-08-05 15:42:03 -07:00
|
|
|
$this->assertActionLogCheckInEntryFor($userA, $consumableA);
|
|
|
|
$this->assertActionLogCheckInEntryFor($userA, $consumableB);
|
|
|
|
$this->assertActionLogCheckInEntryFor($userC, $consumableA);
|
2024-07-24 14:16:42 -07:00
|
|
|
}
|
|
|
|
|
2024-08-05 16:10:12 -07:00
|
|
|
public function testUsersCanBeDeletedInBulk()
|
|
|
|
{
|
|
|
|
[$userA, $userB, $userC] = User::factory()->count(3)->create();
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->editUsers()->create())
|
|
|
|
->post(route('users/bulksave'), [
|
|
|
|
'ids' => [
|
|
|
|
$userA->id,
|
|
|
|
$userC->id,
|
|
|
|
],
|
|
|
|
'delete_user' => '1',
|
|
|
|
])
|
|
|
|
->assertRedirect();
|
|
|
|
|
|
|
|
$this->assertSoftDeleted($userA);
|
|
|
|
$this->assertNotSoftDeleted($userB);
|
|
|
|
$this->assertSoftDeleted($userC);
|
|
|
|
}
|
|
|
|
|
2024-08-05 15:16:40 -07:00
|
|
|
private function attachAccessoryToUsers(Accessory $accessory, array $users): void
|
2024-07-24 14:16:42 -07:00
|
|
|
{
|
2024-08-05 15:16:40 -07:00
|
|
|
foreach ($users as $user) {
|
|
|
|
$accessory->users()->attach($accessory->id, [
|
|
|
|
'accessory_id' => $accessory->id,
|
|
|
|
'assigned_to' => $user->id,
|
|
|
|
]);
|
|
|
|
}
|
2024-07-24 14:16:42 -07:00
|
|
|
}
|
|
|
|
|
2024-08-05 15:16:40 -07:00
|
|
|
private function attachConsumableToUsers(Consumable $consumable, array $users): void
|
2024-07-24 14:16:42 -07:00
|
|
|
{
|
2024-08-05 15:16:40 -07:00
|
|
|
foreach ($users as $user) {
|
|
|
|
$consumable->users()->attach($consumable->id, [
|
|
|
|
'consumable_id' => $consumable->id,
|
|
|
|
'assigned_to' => $user->id,
|
|
|
|
]);
|
|
|
|
}
|
2024-07-24 14:16:42 -07:00
|
|
|
}
|
2024-08-05 15:42:03 -07:00
|
|
|
|
|
|
|
private function assertActionLogCheckInEntryFor(User $user, Model $model): void
|
|
|
|
{
|
|
|
|
$this->assertDatabaseHas('action_logs', [
|
|
|
|
'action_type' => 'checkin from',
|
|
|
|
'target_id' => $user->id,
|
|
|
|
'target_type' => User::class,
|
|
|
|
'note' => 'Bulk checkin items',
|
|
|
|
'item_type' => get_class($model),
|
|
|
|
'item_id' => $model->id,
|
|
|
|
]);
|
|
|
|
}
|
2024-07-24 14:16:42 -07:00
|
|
|
}
|