2024-09-16 10:29:20 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\Departments\Api;
|
|
|
|
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\Department;
|
|
|
|
use App\Models\User;
|
2024-09-16 16:55:20 -07:00
|
|
|
use Tests\Concerns\TestsFullMultipleCompaniesSupport;
|
2024-09-16 10:29:20 -07:00
|
|
|
use Tests\Concerns\TestsPermissionsRequirement;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
2024-09-16 16:55:20 -07:00
|
|
|
class DeleteDepartmentsTest extends TestCase implements TestsFullMultipleCompaniesSupport, TestsPermissionsRequirement
|
2024-09-16 10:29:20 -07:00
|
|
|
{
|
|
|
|
public function testRequiresPermission()
|
|
|
|
{
|
|
|
|
$department = Department::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->create())
|
|
|
|
->deleteJson(route('api.departments.destroy', $department))
|
|
|
|
->assertForbidden();
|
2024-09-16 14:32:38 -07:00
|
|
|
|
|
|
|
$this->assertDatabaseHas('departments', ['id' => $department->id]);
|
2024-09-16 10:29:20 -07:00
|
|
|
}
|
|
|
|
|
2024-09-16 16:55:20 -07:00
|
|
|
public function testAdheresToFullMultipleCompaniesSupportScoping()
|
2024-09-16 10:29:20 -07:00
|
|
|
{
|
|
|
|
[$companyA, $companyB] = Company::factory()->count(2)->create();
|
|
|
|
|
|
|
|
$departmentA = Department::factory()->for($companyA)->create();
|
|
|
|
$departmentB = Department::factory()->for($companyB)->create();
|
|
|
|
$departmentC = Department::factory()->for($companyB)->create();
|
|
|
|
|
|
|
|
$superUser = $companyA->users()->save(User::factory()->superuser()->make());
|
|
|
|
$userInCompanyA = $companyA->users()->save(User::factory()->deleteDepartments()->make());
|
|
|
|
$userInCompanyB = $companyB->users()->save(User::factory()->deleteDepartments()->make());
|
|
|
|
|
|
|
|
$this->settings->enableMultipleFullCompanySupport();
|
|
|
|
|
|
|
|
$this->actingAsForApi($userInCompanyA)
|
|
|
|
->deleteJson(route('api.departments.destroy', $departmentB))
|
|
|
|
->assertStatusMessageIs('error');
|
|
|
|
|
|
|
|
$this->actingAsForApi($userInCompanyB)
|
|
|
|
->deleteJson(route('api.departments.destroy', $departmentA))
|
|
|
|
->assertStatusMessageIs('error');
|
|
|
|
|
|
|
|
$this->actingAsForApi($superUser)
|
|
|
|
->deleteJson(route('api.departments.destroy', $departmentC))
|
|
|
|
->assertStatusMessageIs('success');
|
|
|
|
|
2024-09-16 14:34:55 -07:00
|
|
|
$this->assertDatabaseHas('departments', ['id' => $departmentA->id]);
|
|
|
|
$this->assertDatabaseHas('departments', ['id' => $departmentB->id]);
|
|
|
|
$this->assertDatabaseMissing('departments', ['id' => $departmentC->id]);
|
2024-09-16 10:29:20 -07:00
|
|
|
}
|
2024-09-16 14:25:11 -07:00
|
|
|
|
|
|
|
public function testCannotDeleteDepartmentThatStillHasUsers()
|
|
|
|
{
|
|
|
|
$department = Department::factory()->hasUsers()->create();
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->deleteDepartments()->create())
|
|
|
|
->deleteJson(route('api.departments.destroy', $department))
|
|
|
|
->assertStatusMessageIs('error');
|
|
|
|
|
2024-09-16 14:34:55 -07:00
|
|
|
$this->assertDatabaseHas('departments', ['id' => $department->id]);
|
2024-09-16 14:25:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCanDeleteDepartment()
|
|
|
|
{
|
|
|
|
$department = Department::factory()->create();
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->deleteDepartments()->create())
|
|
|
|
->deleteJson(route('api.departments.destroy', $department))
|
|
|
|
->assertStatusMessageIs('success');
|
|
|
|
|
|
|
|
$this->assertDatabaseMissing('departments', ['id' => $department->id]);
|
|
|
|
}
|
2024-09-16 10:29:20 -07:00
|
|
|
}
|