refactor: fix 7 CRUD system inconsistencies for robustness and maintainability

- Add user-assignment dependency check to DepartmentService.deleteByUuid (409 when users assigned)
- Standardize POST detection to isMethod('POST') in 10 create/edit actions
- Align RoleService code uniqueness to warning pattern (matching departments)
- Normalize repository create() return types from mixed to int|false (3 repos + 3 interfaces)
- Add JSON response branches to 4 non-user delete actions
- Document delete authorization ordering pattern
- Decompose AdminSettingsService.updateFromAdmin into domain-scoped private methods

Workflow: .agents/runs/CRUD-CONSISTENCY-001/
Reviewed by: Code Reviewer (pass), Security Reviewer (pass), Acceptance Tester (pass)
Quality gates: QG-001 PHPUnit pass, QG-002 PHPStan pass, QG-003 Architecture pass

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-22 18:25:33 +01:00
parent 984d0430d3
commit c429ff43cd
29 changed files with 495 additions and 207 deletions

View File

@@ -207,6 +207,10 @@ class DepartmentServiceTest extends TestCase
->with('abc-123')
->willReturn($department);
$userDeptRepo = $this->createMock(UserDepartmentRepositoryInterface::class);
$userDeptRepo->expects($this->any())->method('countUsersByDepartmentIds')->with([5])->willReturn([]);
$this->userServicesFactory->expects($this->any())->method('createUserDepartmentRepository')->willReturn($userDeptRepo);
$this->departmentRepository
->expects($this->once())
->method('delete')
@@ -263,6 +267,10 @@ class DepartmentServiceTest extends TestCase
->with('abc-999')
->willReturn(['id' => 9, 'uuid' => 'abc-999', 'description' => 'Ops']);
$userDeptRepo = $this->createMock(UserDepartmentRepositoryInterface::class);
$userDeptRepo->expects($this->any())->method('countUsersByDepartmentIds')->with([9])->willReturn([]);
$this->userServicesFactory->expects($this->any())->method('createUserDepartmentRepository')->willReturn($userDeptRepo);
$this->departmentRepository
->expects($this->once())
->method('delete')
@@ -280,6 +288,72 @@ class DepartmentServiceTest extends TestCase
$this->assertSame('delete_failed', $result['error']);
}
public function testDeleteByUuidReturns409WhenUsersAssigned(): void
{
$this->departmentRepository
->expects($this->once())
->method('findByUuid')
->with('abc-dept')
->willReturn(['id' => 5, 'uuid' => 'abc-dept', 'description' => 'Sales']);
$userDeptRepo = $this->createMock(UserDepartmentRepositoryInterface::class);
$userDeptRepo->expects($this->once())
->method('countUsersByDepartmentIds')
->with([5])
->willReturn([5 => 3]);
$this->userServicesFactory->expects($this->once())
->method('createUserDepartmentRepository')
->willReturn($userDeptRepo);
$this->departmentRepository
->expects($this->never())
->method('delete');
$this->systemAuditService
->expects($this->never())
->method('record');
$result = $this->service->deleteByUuid('abc-dept');
$this->assertFalse($result['ok']);
$this->assertSame(409, $result['status']);
$this->assertSame('department_has_users', $result['error']);
$this->assertSame(3, $result['user_count']);
}
public function testDeleteByUuidDeletesWhenNoUsersAssigned(): void
{
$this->departmentRepository
->expects($this->once())
->method('findByUuid')
->with('abc-empty')
->willReturn(['id' => 7, 'uuid' => 'abc-empty', 'description' => 'Empty Dept']);
$userDeptRepo = $this->createMock(UserDepartmentRepositoryInterface::class);
$userDeptRepo->expects($this->once())
->method('countUsersByDepartmentIds')
->with([7])
->willReturn([]);
$this->userServicesFactory->expects($this->once())
->method('createUserDepartmentRepository')
->willReturn($userDeptRepo);
$this->departmentRepository
->expects($this->once())
->method('delete')
->with(7)
->willReturn(true);
$this->systemAuditService
->expects($this->once())
->method('record');
$result = $this->service->deleteByUuid('abc-empty');
$this->assertTrue($result['ok']);
$this->assertSame('abc-empty', $result['department']['uuid']);
}
public function testSyncTenantsUsesFirstId(): void
{
$this->departmentRepository