2024-05-07 10:59:24 -07:00
|
|
|
<?php
|
|
|
|
|
2024-06-04 10:48:53 -07:00
|
|
|
namespace Tests\Feature\Users\Ui;
|
2024-05-07 10:59:24 -07:00
|
|
|
|
2024-06-05 06:16:28 -07:00
|
|
|
use Tests\TestCase;
|
2024-06-05 06:02:04 -07:00
|
|
|
use App\Models\LicenseSeat;
|
2024-05-07 10:59:24 -07:00
|
|
|
use App\Models\Location;
|
2024-06-05 06:03:33 -07:00
|
|
|
use App\Models\Accessory;
|
2024-05-07 10:59:24 -07:00
|
|
|
use App\Models\User;
|
2024-06-12 04:31:41 -07:00
|
|
|
use App\Models\Company;
|
2024-06-05 06:16:28 -07:00
|
|
|
|
|
|
|
use App\Models\Asset;
|
2024-05-07 10:59:24 -07:00
|
|
|
|
2024-06-03 16:54:59 -07:00
|
|
|
class DeleteUserTest extends TestCase
|
2024-05-07 10:59:24 -07:00
|
|
|
{
|
|
|
|
|
2024-06-22 09:45:42 -07:00
|
|
|
public function testUserCanDeleteAnotherUser()
|
|
|
|
{
|
|
|
|
$user = User::factory()->deleteUsers()->viewUsers()->create();
|
|
|
|
$this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())->assertTrue($user->isDeletable());
|
|
|
|
|
|
|
|
$response = $this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())
|
|
|
|
->delete(route('users.destroy', ['user' => $user->id]))
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.index'));
|
|
|
|
|
|
|
|
$this->followRedirects($response)->assertSee(trans('general.notification_success'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testErrorReturnedIfUserDoesNotExist()
|
|
|
|
{
|
|
|
|
$response = $this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())
|
|
|
|
->delete(route('users.destroy', ['user' => '40596803548609346']))
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.index'));
|
2024-06-22 10:36:02 -07:00
|
|
|
$this->followRedirects($response)->assertSee(trans('alert-danger'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testErrorReturnedIfUserIsAlreadyDeleted()
|
|
|
|
{
|
|
|
|
$user = User::factory()->deletedUser()->viewUsers()->create();
|
|
|
|
$response = $this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())
|
|
|
|
->delete(route('users.destroy', $user->id))
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.index'));
|
|
|
|
|
|
|
|
$this->followRedirects($response)->assertSee(trans('general.error'));
|
2024-06-22 09:45:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-22 11:47:59 -07:00
|
|
|
public function testFmcsPermissionsToDeleteUser()
|
2024-06-12 04:12:15 -07:00
|
|
|
{
|
|
|
|
|
|
|
|
$this->settings->enableMultipleFullCompanySupport();
|
|
|
|
|
|
|
|
[$companyA, $companyB] = Company::factory()->count(2)->create();
|
|
|
|
|
|
|
|
$superuser = User::factory()->superuser()->create();
|
2024-06-22 11:51:56 -07:00
|
|
|
$userFromA = User::factory()->deleteUsers()->for($companyA)->create();
|
|
|
|
$userFromB = User::factory()->deleteUsers()->for($companyB)->create();
|
2024-06-12 04:12:15 -07:00
|
|
|
|
2024-06-22 11:51:56 -07:00
|
|
|
$response = $this->followingRedirects()->actingAs($userFromA)
|
2024-06-12 04:12:15 -07:00
|
|
|
->delete(route('users.destroy', ['user' => $userFromB->id]))
|
|
|
|
->assertStatus(403);
|
2024-06-22 11:47:59 -07:00
|
|
|
$this->followRedirects($response)->assertSee('sad-panda.png');
|
2024-06-12 04:12:15 -07:00
|
|
|
|
2024-06-22 12:07:46 -07:00
|
|
|
$userFromB->refresh();
|
|
|
|
$this->assertNull($userFromB->deleted_at);
|
|
|
|
|
|
|
|
|
2024-06-22 11:51:56 -07:00
|
|
|
$response = $this->actingAs($userFromB)
|
2024-06-12 04:12:15 -07:00
|
|
|
->delete(route('users.destroy', ['user' => $userFromA->id]))
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.index'));
|
2024-06-22 11:47:59 -07:00
|
|
|
$this->followRedirects($response)->assertSee('sad-panda.png');
|
|
|
|
|
2024-06-22 12:07:46 -07:00
|
|
|
$userFromA->refresh();
|
|
|
|
$this->assertNull($userFromA->deleted_at);
|
|
|
|
|
2024-06-22 11:47:59 -07:00
|
|
|
$response = $this->actingAs($superuser)
|
|
|
|
->delete(route('users.destroy', ['user' => $userFromA->id]))
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.index'));
|
|
|
|
$this->followRedirects($response)->assertSee('Success');
|
2024-06-12 04:12:15 -07:00
|
|
|
|
2024-06-22 12:07:46 -07:00
|
|
|
$userFromA->refresh();
|
|
|
|
$this->assertNotNull($userFromA->deleted_at);
|
|
|
|
|
2024-06-12 04:12:15 -07:00
|
|
|
}
|
2024-05-07 10:59:24 -07:00
|
|
|
|
2024-06-12 04:31:41 -07:00
|
|
|
|
2024-05-07 10:59:24 -07:00
|
|
|
public function testDisallowUserDeletionIfStillManagingPeople()
|
|
|
|
{
|
2024-05-31 20:01:09 -07:00
|
|
|
$manager = User::factory()->create();
|
2024-06-05 06:09:57 -07:00
|
|
|
User::factory()->count(1)->create(['manager_id' => $manager->id]);
|
2024-05-31 20:01:09 -07:00
|
|
|
|
2024-06-05 06:13:51 -07:00
|
|
|
$this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())->assertFalse($manager->isDeletable());
|
2024-05-31 20:01:09 -07:00
|
|
|
|
2024-05-31 20:24:47 -07:00
|
|
|
$response = $this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())
|
2024-05-31 20:01:09 -07:00
|
|
|
->delete(route('users.destroy', $manager->id))
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.index'));
|
2024-05-31 20:24:47 -07:00
|
|
|
|
|
|
|
$this->followRedirects($response)->assertSee('Error');
|
2024-05-07 10:59:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDisallowUserDeletionIfStillManagingLocations()
|
|
|
|
{
|
2024-05-31 20:01:09 -07:00
|
|
|
$manager = User::factory()->create();
|
2024-06-05 06:09:57 -07:00
|
|
|
Location::factory()->count(2)->create(['manager_id' => $manager->id]);
|
2024-05-31 20:01:09 -07:00
|
|
|
|
2024-06-05 06:13:51 -07:00
|
|
|
$this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())->assertFalse($manager->isDeletable());
|
2024-05-31 20:01:09 -07:00
|
|
|
|
2024-05-31 20:24:47 -07:00
|
|
|
$response = $this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())
|
2024-05-31 20:01:09 -07:00
|
|
|
->delete(route('users.destroy', $manager->id))
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.index'));
|
2024-05-31 20:24:47 -07:00
|
|
|
|
|
|
|
$this->followRedirects($response)->assertSee('Error');
|
2024-05-07 10:59:24 -07:00
|
|
|
}
|
|
|
|
|
2024-06-05 06:02:04 -07:00
|
|
|
public function testDisallowUserDeletionIfStillHaveAccessories()
|
|
|
|
{
|
|
|
|
$user = User::factory()->create();
|
2024-06-05 06:09:57 -07:00
|
|
|
Accessory::factory()->count(3)->checkedOutToUser($user)->create();
|
2024-06-05 06:02:04 -07:00
|
|
|
|
2024-06-05 06:13:51 -07:00
|
|
|
$this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())->assertFalse($user->isDeletable());
|
2024-06-05 06:02:04 -07:00
|
|
|
|
|
|
|
$response = $this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())
|
|
|
|
->delete(route('users.destroy', $user->id))
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.index'));
|
|
|
|
|
|
|
|
$this->followRedirects($response)->assertSee('Error');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDisallowUserDeletionIfStillHaveLicenses()
|
|
|
|
{
|
|
|
|
$user = User::factory()->create();
|
2024-06-05 06:09:57 -07:00
|
|
|
LicenseSeat::factory()->count(4)->create(['assigned_to' => $user->id]);
|
2024-06-05 06:02:04 -07:00
|
|
|
|
2024-06-05 06:13:51 -07:00
|
|
|
$this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())->assertFalse($user->isDeletable());
|
2024-06-05 06:02:04 -07:00
|
|
|
|
|
|
|
$response = $this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())
|
|
|
|
->delete(route('users.destroy', $user->id))
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.index'));
|
|
|
|
|
|
|
|
$this->followRedirects($response)->assertSee('Error');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-07 10:59:24 -07:00
|
|
|
public function testAllowUserDeletionIfNotManagingLocations()
|
|
|
|
{
|
2024-05-31 20:01:09 -07:00
|
|
|
$manager = User::factory()->create();
|
2024-06-05 06:13:51 -07:00
|
|
|
$this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())->assertTrue($manager->isDeletable());
|
2024-05-31 20:01:09 -07:00
|
|
|
|
2024-05-31 20:24:47 -07:00
|
|
|
$response = $this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())
|
2024-05-31 20:01:09 -07:00
|
|
|
->delete(route('users.destroy', $manager->id))
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.index'));
|
2024-05-31 20:24:47 -07:00
|
|
|
|
|
|
|
$this->followRedirects($response)->assertSee('Success');
|
|
|
|
|
2024-05-07 10:59:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDisallowUserDeletionIfNoDeletePermissions()
|
|
|
|
{
|
2024-05-31 20:01:09 -07:00
|
|
|
$manager = User::factory()->create();
|
2024-05-07 10:59:24 -07:00
|
|
|
Location::factory()->create(['manager_id' => $manager->id]);
|
2024-06-05 06:13:51 -07:00
|
|
|
$this->actingAs(User::factory()->editUsers()->viewUsers()->create())->assertFalse($manager->isDeletable());
|
2024-05-07 10:59:24 -07:00
|
|
|
}
|
|
|
|
|
2024-06-05 06:16:28 -07:00
|
|
|
public function testDisallowUserDeletionIfTheyStillHaveAssets()
|
|
|
|
{
|
|
|
|
$user = User::factory()->create();
|
2024-06-05 06:50:45 -07:00
|
|
|
$asset = Asset::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAs(User::factory()->checkoutAssets()->create())
|
|
|
|
->post(route('hardware.checkout.store', $asset->id), [
|
|
|
|
'checkout_to_type' => 'user',
|
|
|
|
'assigned_user' => $user->id,
|
|
|
|
'name' => 'Changed Name',
|
|
|
|
]);
|
2024-06-05 06:16:28 -07:00
|
|
|
|
|
|
|
$this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())->assertFalse($user->isDeletable());
|
|
|
|
|
|
|
|
$response = $this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())
|
|
|
|
->delete(route('users.destroy', $user->id))
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.index'));
|
|
|
|
|
|
|
|
$this->followRedirects($response)->assertSee('Error');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-05 06:02:04 -07:00
|
|
|
public function testUsersCannotDeleteThemselves()
|
|
|
|
{
|
2024-06-05 06:13:51 -07:00
|
|
|
$manager = User::factory()->deleteUsers()->viewUsers()->create();
|
|
|
|
$this->actingAs(User::factory()->deleteUsers()->viewUsers()->create())->assertTrue($manager->isDeletable());
|
2024-06-05 06:02:04 -07:00
|
|
|
|
|
|
|
$response = $this->actingAs($manager)
|
|
|
|
->delete(route('users.destroy', $manager->id))
|
|
|
|
->assertStatus(302)
|
|
|
|
->assertRedirect(route('users.index'));
|
|
|
|
|
|
|
|
$this->followRedirects($response)->assertSee('Error');
|
|
|
|
}
|
|
|
|
|
2024-05-07 10:59:24 -07:00
|
|
|
|
|
|
|
}
|