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

@@ -66,6 +66,41 @@ class RoleServiceTest extends TestCase
$this->assertTrue($result['ok']);
$this->assertSame('uuid-42', $result['uuid']);
$this->assertSame(42, $result['id']);
$this->assertEmpty($result['warnings']);
}
public function testCreateFromAdminReturnsDuplicateCodeAsWarning(): void
{
$this->roleRepo
->expects($this->any())
->method('existsByCode')
->with('DUPE', 0)
->willReturn(true);
$this->roleRepo
->expects($this->any())
->method('create')
->willReturn(50);
$this->roleRepo
->expects($this->any())
->method('find')
->with(50)
->willReturn(['id' => 50, 'uuid' => 'uuid-50', 'description' => 'Dupe Role']);
$this->auditService
->expects($this->once())
->method('record');
$result = $this->service->createFromAdmin([
'description' => 'Dupe Role',
'code' => 'DUPE',
'active' => 1,
], 1);
$this->assertTrue($result['ok']);
$this->assertNotEmpty($result['warnings']);
$this->assertEmpty($result['errors'] ?? []);
}
public function testCreateFromAdminRejectsEmptyDescription(): void
@@ -184,7 +219,7 @@ class RoleServiceTest extends TestCase
$this->assertTrue($result['ok']);
}
public function testUpdateFromAdminRejectsDuplicateCode(): void
public function testUpdateFromAdminReturnsDuplicateCodeAsWarning(): void
{
$this->roleRepo
->expects($this->once())
@@ -193,17 +228,30 @@ class RoleServiceTest extends TestCase
->willReturn(true);
$this->roleRepo
->expects($this->never())
->method('update');
->expects($this->once())
->method('find')
->with(10)
->willReturn(['id' => 10, 'uuid' => 'uuid-10', 'active' => 1]);
$this->roleRepo
->expects($this->once())
->method('update')
->with(10, $this->anything())
->willReturn(true);
$this->auditService
->expects($this->once())
->method('record');
$result = $this->service->updateFromAdmin(10, [
'description' => 'Manager',
'code' => 'ADMIN',
'active' => 1,
]);
], 5);
$this->assertFalse($result['ok']);
$this->assertNotEmpty($result['errors']);
$this->assertTrue($result['ok']);
$this->assertNotEmpty($result['warnings']);
$this->assertEmpty($result['errors'] ?? []);
}
public function testUpdateFromAdminReturnsErrorWhenRepositoryUpdateFails(): void