@@ -3,6 +3,7 @@
|
||||
use App\Enums\PurchasingRole;
|
||||
use App\Models\AuditLog;
|
||||
use App\Models\ConflictDeclaration;
|
||||
use App\Models\ConflictRelease;
|
||||
use App\Models\CostCenter;
|
||||
use App\Models\Department;
|
||||
use App\Models\ProposalEvaluation;
|
||||
@@ -13,6 +14,8 @@ use App\Models\Tenant;
|
||||
use App\Models\User;
|
||||
use App\Services\ConflictGuard;
|
||||
use App\Services\RequisitionWorkflow;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use Inertia\Testing\AssertableInertia as Assert;
|
||||
|
||||
function personalConflict(int $supplierId, ?int $requisitionId = null, bool $hasConflict = true): array
|
||||
@@ -20,6 +23,76 @@ function personalConflict(int $supplierId, ?int $requisitionId = null, bool $has
|
||||
return ['scope' => $requisitionId ? 'process_supplier' : 'supplier', 'supplier_id' => $supplierId, 'requisition_id' => $requisitionId, 'has_conflict' => $hasConflict, 'known_since' => now()->subYears(3)->format('Y-m-d'), 'reason' => 'Relação pessoal relevante declarada para efeitos de transparência.'];
|
||||
}
|
||||
|
||||
function configureConflictDirection(User $user): int
|
||||
{
|
||||
$db = DB::class;
|
||||
$db::table('organization_access')->insert(['tenant_id' => $user->tenant_id, 'active' => true, 'version' => 1, 'created_at' => now(), 'updated_at' => now()]);
|
||||
$profile = $db::table('organization_profiles')->insertGetId(['tenant_id' => $user->tenant_id, 'name' => 'Direcção da empresa', 'permissions' => json_encode(['conflict_release_authorise']), 'version' => 1, 'created_at' => now(), 'updated_at' => now()]);
|
||||
|
||||
return $db::table('organization_memberships')->insertGetId(['tenant_id' => $user->tenant_id, 'user_id' => $user->id, 'profile_id' => $profile, 'maintenance' => false, 'created_at' => now(), 'updated_at' => now()]);
|
||||
}
|
||||
|
||||
test('direction release preserves declarations applies to the complete correction chain and replays once', function () {
|
||||
config(['conflict-releases.qa_enabled' => true]);
|
||||
$this->post('/conflitos', personalConflict($this->supplier->id))->assertSessionHasNoErrors();
|
||||
$root = ConflictDeclaration::query()->firstOrFail();
|
||||
$this->put('/conflitos/'.$root->id, ['reason' => 'Fundamento corrigido antes da decisão da direcção.', 'known_since' => now()->format('Y-m-d')])->assertSessionHasNoErrors();
|
||||
$latest = ConflictDeclaration::query()->latest('id')->firstOrFail();
|
||||
$before = ConflictDeclaration::query()->get()->toArray();
|
||||
$input = ['operation_key' => (string) Str::uuid(), 'reason' => 'Direcção autoriza expressamente o levantamento deste impedimento.'];
|
||||
$guard = app(ConflictGuard::class);
|
||||
expect($guard->hasConflict($this->buyer->id, $this->supplier->id, $this->r->id))->toBeTrue();
|
||||
$this->actingAs($this->admin)->post('/conflitos/'.$latest->id.'/levantamento', $input)->assertForbidden();
|
||||
configureConflictDirection($this->admin);
|
||||
$this->post('/conflitos/'.$root->id.'/levantamento', $input)->assertConflict();
|
||||
$this->get('/conflitos/'.$root->id)->assertInertia(fn (Assert $page) => $page->where('canAuthoriseRelease', true));
|
||||
$this->post('/conflitos/'.$latest->id.'/levantamento', $input)->assertSessionHasNoErrors()->assertRedirect();
|
||||
$this->post('/conflitos/'.$latest->id.'/levantamento', $input)->assertSessionHasNoErrors()->assertRedirect();
|
||||
expect($guard->hasConflict($this->buyer->id, $this->supplier->id, $this->r->id))->toBeFalse()
|
||||
->and(ConflictDeclaration::query()->get()->toArray())->toBe($before)
|
||||
->and(ConflictRelease::query()->count())->toBe(1)
|
||||
->and(AuditLog::query()->where('action', 'conflict.release_authorised')->count())->toBe(1);
|
||||
$this->get('/conflitos/'.$root->id)->assertInertia(fn (Assert $page) => $page->where('canAuthoriseRelease', false)->where('release.declaration_id', $latest->id));
|
||||
$this->post('/conflitos/'.$latest->id.'/levantamento', [...$input, 'reason' => 'Alteração indevida do fundamento após decisão.'])->assertConflict();
|
||||
ConflictDeclaration::query()->create([...$root->only(['tenant_id', 'user_id', 'supplier_id', 'requisition_id', 'has_conflict', 'known_since', 'reason']), 'declared_at' => now()]);
|
||||
expect($guard->hasConflict($this->buyer->id, $this->supplier->id, $this->r->id))->toBeTrue();
|
||||
});
|
||||
|
||||
test('direction authority is current scoped and never inherited from admin or finance', function () {
|
||||
config(['conflict-releases.qa_enabled' => true]);
|
||||
$this->post('/conflitos', personalConflict($this->supplier->id))->assertSessionHasNoErrors();
|
||||
$root = ConflictDeclaration::query()->firstOrFail();
|
||||
$input = ['operation_key' => (string) Str::uuid(), 'reason' => 'Autorização expressa da direcção para este conflito.'];
|
||||
$url = '/conflitos/'.$root->id.'/levantamento';
|
||||
$this->actingAs($this->finance)->post($url, $input)->assertForbidden();
|
||||
$membership = configureConflictDirection($this->admin);
|
||||
$this->actingAs($this->admin);
|
||||
DB::table('organization_memberships')->where('id', $membership)->update(['expires_at' => now()->subMinute()]);
|
||||
$this->post($url, $input)->assertForbidden();
|
||||
DB::table('organization_memberships')->where('id', $membership)->update(['expires_at' => null, 'maintenance' => true]);
|
||||
$this->post($url, $input)->assertForbidden();
|
||||
DB::table('organization_memberships')->where('id', $membership)->update(['maintenance' => false]);
|
||||
$this->post($url, [...$input, 'reason' => 'curto'])->assertSessionHasErrors('reason');
|
||||
$other = Tenant::query()->create(['name' => 'Outra empresa sintética']);
|
||||
$foreign = User::factory()->create(['tenant_id' => $other->id, 'purchasing_role' => PurchasingRole::Configured]);
|
||||
configureConflictDirection($foreign);
|
||||
$this->actingAs($foreign)->post($url, $input)->assertNotFound();
|
||||
expect(ConflictRelease::query()->withoutGlobalScopes()->count())->toBe(0);
|
||||
});
|
||||
|
||||
test('release capability remains disabled by default and release evidence is append only', function () {
|
||||
$this->post('/conflitos', personalConflict($this->supplier->id))->assertSessionHasNoErrors();
|
||||
$root = ConflictDeclaration::query()->firstOrFail();
|
||||
configureConflictDirection($this->admin);
|
||||
$input = ['operation_key' => (string) Str::uuid(), 'reason' => 'Direcção autoriza o levantamento com fundamento registado.'];
|
||||
$this->actingAs($this->admin)->post('/conflitos/'.$root->id.'/levantamento', $input)->assertStatus(503);
|
||||
config(['conflict-releases.qa_enabled' => true]);
|
||||
$this->post('/conflitos/'.$root->id.'/levantamento', $input)->assertSessionHasNoErrors();
|
||||
$release = ConflictRelease::query()->firstOrFail();
|
||||
expect(fn () => $release->update(['reason' => 'Outro fundamento']))->toThrow(LogicException::class);
|
||||
expect(fn () => $release->delete())->toThrow(LogicException::class);
|
||||
});
|
||||
|
||||
beforeEach(function () {
|
||||
$this->tenant = Tenant::query()->create(['name' => 'Conflitos A']);
|
||||
$this->department = Department::query()->create(['tenant_id' => $this->tenant->id, 'name' => 'Operações']);
|
||||
|
||||
Reference in New Issue
Block a user